2021-10-13 14:47:41 +02:00
|
|
|
"""Compiles/decompiles SVG table.
|
2016-01-24 14:34:51 +00:00
|
|
|
|
2021-10-13 14:47:41 +02:00
|
|
|
https://docs.microsoft.com/en-us/typography/opentype/spec/svg
|
2013-08-28 17:25:16 -04:00
|
|
|
|
|
|
|
The XML format is:
|
2021-12-02 15:31:49 +00:00
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
|
|
|
<SVG>
|
|
|
|
<svgDoc endGlyphID="1" startGlyphID="1">
|
|
|
|
<![CDATA[ <complete SVG doc> ]]
|
|
|
|
</svgDoc>
|
|
|
|
...
|
|
|
|
<svgDoc endGlyphID="n" startGlyphID="m">
|
|
|
|
<![CDATA[ <complete SVG doc> ]]
|
|
|
|
</svgDoc>
|
|
|
|
</SVG>
|
2021-10-13 14:47:41 +02:00
|
|
|
"""
|
2013-08-28 17:25:16 -04:00
|
|
|
|
2022-06-09 13:02:07 +01:00
|
|
|
from fontTools.misc.textTools import bytesjoin, safeEval, strjoin, tobytes, tostr
|
2021-10-13 14:47:41 +02:00
|
|
|
from fontTools.misc import sstruct
|
|
|
|
from . import DefaultTable
|
2022-06-09 13:02:07 +01:00
|
|
|
from collections.abc import Sequence
|
|
|
|
from dataclasses import dataclass, astuple
|
2021-10-13 14:47:41 +02:00
|
|
|
from io import BytesIO
|
|
|
|
import struct
|
|
|
|
import logging
|
2013-08-28 17:25:16 -04:00
|
|
|
|
|
|
|
|
2021-10-13 14:47:41 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2013-08-28 17:25:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
SVG_format_0 = """
|
|
|
|
> # big endian
|
|
|
|
version: H
|
|
|
|
offsetToSVGDocIndex: L
|
2021-10-13 14:47:41 +02:00
|
|
|
reserved: L
|
2013-08-28 17:25:16 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
SVG_format_0Size = sstruct.calcsize(SVG_format_0)
|
|
|
|
|
|
|
|
doc_index_entry_format_0 = """
|
|
|
|
> # big endian
|
|
|
|
startGlyphID: H
|
|
|
|
endGlyphID: H
|
|
|
|
svgDocOffset: L
|
|
|
|
svgDocLength: L
|
|
|
|
"""
|
|
|
|
|
|
|
|
doc_index_entry_format_0Size = sstruct.calcsize(doc_index_entry_format_0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class table_S_V_G_(DefaultTable.DefaultTable):
|
2015-04-26 02:01:01 -04:00
|
|
|
|
2013-08-28 17:25:16 -04:00
|
|
|
def decompile(self, data, ttFont):
|
2021-10-13 14:47:41 +02:00
|
|
|
self.docList = []
|
|
|
|
# Version 0 is the standardized version of the table; and current.
|
|
|
|
# https://www.microsoft.com/typography/otspec/svg.htm
|
|
|
|
sstruct.unpack(SVG_format_0, data[:SVG_format_0Size], self)
|
|
|
|
if self.version != 0:
|
|
|
|
log.warning(
|
|
|
|
"Unknown SVG table version '%s'. Decompiling as version 0.", self.version)
|
2013-08-28 17:25:16 -04:00
|
|
|
# read in SVG Documents Index
|
|
|
|
# data starts with the first entry of the entry list.
|
2013-10-07 17:23:35 -04:00
|
|
|
pos = subTableStart = self.offsetToSVGDocIndex
|
2021-10-13 14:47:41 +02:00
|
|
|
self.numEntries = struct.unpack(">H", data[pos:pos+2])[0]
|
2013-10-07 17:23:35 -04:00
|
|
|
pos += 2
|
2013-08-28 17:25:16 -04:00
|
|
|
if self.numEntries > 0:
|
|
|
|
data2 = data[pos:]
|
2021-10-13 14:47:41 +02:00
|
|
|
entries = []
|
2015-03-02 15:22:39 -08:00
|
|
|
for i in range(self.numEntries):
|
2013-08-28 17:25:16 -04:00
|
|
|
docIndexEntry, data2 = sstruct.unpack2(doc_index_entry_format_0, data2, DocumentIndexEntry())
|
|
|
|
entries.append(docIndexEntry)
|
|
|
|
|
|
|
|
for entry in entries:
|
2013-10-07 17:23:35 -04:00
|
|
|
start = entry.svgDocOffset + subTableStart
|
2013-08-28 17:25:16 -04:00
|
|
|
end = start + entry.svgDocLength
|
2015-03-02 15:40:04 -08:00
|
|
|
doc = data[start:end]
|
2022-06-09 13:02:07 +01:00
|
|
|
compressed = False
|
2015-03-02 15:40:04 -08:00
|
|
|
if doc.startswith(b"\x1f\x8b"):
|
|
|
|
import gzip
|
2015-08-07 16:16:49 +01:00
|
|
|
bytesIO = BytesIO(doc)
|
|
|
|
with gzip.GzipFile(None, "r", fileobj=bytesIO) as gunzipper:
|
2015-03-02 15:40:04 -08:00
|
|
|
doc = gunzipper.read()
|
2015-08-07 16:16:49 +01:00
|
|
|
del bytesIO
|
2022-06-09 13:02:07 +01:00
|
|
|
compressed = True
|
2015-04-21 11:08:49 -07:00
|
|
|
doc = tostr(doc, "utf_8")
|
2022-06-09 13:02:07 +01:00
|
|
|
self.docList.append(
|
|
|
|
SVGDocument(doc, entry.startGlyphID, entry.endGlyphID, compressed)
|
|
|
|
)
|
2013-08-28 17:25:16 -04:00
|
|
|
|
|
|
|
def compile(self, ttFont):
|
|
|
|
version = 0
|
|
|
|
offsetToSVGDocIndex = SVG_format_0Size # I start the SVGDocIndex right after the header.
|
|
|
|
# get SGVDoc info.
|
|
|
|
docList = []
|
|
|
|
entryList = []
|
|
|
|
numEntries = len(self.docList)
|
|
|
|
datum = struct.pack(">H",numEntries)
|
|
|
|
entryList.append(datum)
|
2013-10-07 17:23:35 -04:00
|
|
|
curOffset = len(datum) + doc_index_entry_format_0Size*numEntries
|
2021-10-13 14:50:08 +02:00
|
|
|
seenDocs = {}
|
2022-06-09 13:02:07 +01:00
|
|
|
allCompressed = getattr(self, "compressed", False)
|
|
|
|
for i, doc in enumerate(self.docList):
|
|
|
|
if isinstance(doc, (list, tuple)):
|
|
|
|
doc = SVGDocument(*doc)
|
|
|
|
self.docList[i] = doc
|
|
|
|
docBytes = tobytes(doc.data, encoding="utf_8")
|
|
|
|
if (allCompressed or doc.compressed) and not docBytes.startswith(b"\x1f\x8b"):
|
2015-03-02 15:40:04 -08:00
|
|
|
import gzip
|
2015-08-07 16:16:49 +01:00
|
|
|
bytesIO = BytesIO()
|
|
|
|
with gzip.GzipFile(None, "w", fileobj=bytesIO) as gzipper:
|
2015-03-02 15:40:04 -08:00
|
|
|
gzipper.write(docBytes)
|
2015-08-07 16:16:49 +01:00
|
|
|
gzipped = bytesIO.getvalue()
|
2015-03-02 15:40:04 -08:00
|
|
|
if len(gzipped) < len(docBytes):
|
|
|
|
docBytes = gzipped
|
2015-08-07 16:16:49 +01:00
|
|
|
del gzipped, bytesIO
|
2015-02-23 14:03:06 -08:00
|
|
|
docLength = len(docBytes)
|
2021-10-13 14:50:08 +02:00
|
|
|
if docBytes in seenDocs:
|
|
|
|
docOffset = seenDocs[docBytes]
|
|
|
|
else:
|
|
|
|
docOffset = curOffset
|
|
|
|
curOffset += docLength
|
|
|
|
seenDocs[docBytes] = docOffset
|
|
|
|
docList.append(docBytes)
|
2022-06-09 13:02:07 +01:00
|
|
|
entry = struct.pack(">HHLL", doc.startGlyphID, doc.endGlyphID, docOffset, docLength)
|
2013-11-27 21:17:35 -05:00
|
|
|
entryList.append(entry)
|
2013-08-28 17:25:16 -04:00
|
|
|
entryList.extend(docList)
|
2013-11-27 21:17:35 -05:00
|
|
|
svgDocData = bytesjoin(entryList)
|
2013-08-28 17:25:16 -04:00
|
|
|
|
2021-10-13 14:47:41 +02:00
|
|
|
reserved = 0
|
|
|
|
header = struct.pack(">HLL", version, offsetToSVGDocIndex, reserved)
|
|
|
|
data = [header, svgDocData]
|
2013-11-27 21:17:35 -05:00
|
|
|
data = bytesjoin(data)
|
2013-08-28 17:25:16 -04:00
|
|
|
return data
|
|
|
|
|
|
|
|
def toXML(self, writer, ttFont):
|
2022-06-09 13:02:07 +01:00
|
|
|
for i, doc in enumerate(self.docList):
|
|
|
|
if isinstance(doc, (list, tuple)):
|
|
|
|
doc = SVGDocument(*doc)
|
|
|
|
self.docList[i] = doc
|
|
|
|
attrs = {"startGlyphID": doc.startGlyphID, "endGlyphID": doc.endGlyphID}
|
|
|
|
if doc.compressed:
|
|
|
|
attrs["compressed"] = 1
|
|
|
|
writer.begintag("svgDoc", **attrs)
|
2013-08-28 17:25:16 -04:00
|
|
|
writer.newline()
|
2022-06-09 13:02:07 +01:00
|
|
|
writer.writecdata(doc.data)
|
2013-08-28 17:25:16 -04:00
|
|
|
writer.newline()
|
|
|
|
writer.endtag("svgDoc")
|
|
|
|
writer.newline()
|
|
|
|
|
2013-11-27 03:19:32 -05:00
|
|
|
def fromXML(self, name, attrs, content, ttFont):
|
2013-08-28 17:25:16 -04:00
|
|
|
if name == "svgDoc":
|
|
|
|
if not hasattr(self, "docList"):
|
|
|
|
self.docList = []
|
2013-11-27 21:17:35 -05:00
|
|
|
doc = strjoin(content)
|
2013-08-28 17:25:16 -04:00
|
|
|
doc = doc.strip()
|
|
|
|
startGID = int(attrs["startGlyphID"])
|
|
|
|
endGID = int(attrs["endGlyphID"])
|
2022-06-09 13:02:07 +01:00
|
|
|
compressed = bool(safeEval(attrs.get("compressed", "0")))
|
|
|
|
self.docList.append(SVGDocument(doc, startGID, endGID, compressed))
|
2013-08-28 17:25:16 -04:00
|
|
|
else:
|
2016-01-24 14:34:51 +00:00
|
|
|
log.warning("Unknown %s %s", name, content)
|
2013-08-28 17:25:16 -04:00
|
|
|
|
2021-10-13 14:47:41 +02:00
|
|
|
|
2013-11-28 14:26:58 -05:00
|
|
|
class DocumentIndexEntry(object):
|
2013-08-28 17:25:16 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.startGlyphID = None # USHORT
|
|
|
|
self.endGlyphID = None # USHORT
|
|
|
|
self.svgDocOffset = None # ULONG
|
|
|
|
self.svgDocLength = None # ULONG
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "startGlyphID: %s, endGlyphID: %s, svgDocOffset: %s, svgDocLength: %s" % (self.startGlyphID, self.endGlyphID, self.svgDocOffset, self.svgDocLength)
|
2022-06-09 13:02:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SVGDocument(Sequence):
|
|
|
|
data: str
|
|
|
|
startGlyphID: int
|
|
|
|
endGlyphID: int
|
|
|
|
compressed: bool = False
|
|
|
|
|
|
|
|
# Previously, the SVG table's docList attribute contained a lists of 3 items:
|
|
|
|
# [doc, startGlyphID, endGlyphID]; later, we added a `compressed` attribute.
|
|
|
|
# For backward compatibility with code that depends of them being sequences of
|
|
|
|
# fixed length=3, we subclass the Sequence abstract base class and pretend only
|
|
|
|
# the first three items are present. 'compressed' is only accessible via named
|
|
|
|
# attribute lookup like regular dataclasses: i.e. `doc.compressed`, not `doc[3]`
|
|
|
|
def __getitem__(self, index):
|
|
|
|
return astuple(self)[:3][index]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return 3
|