Merge pull request #2430 from fonttools/svg-compile-reuse-offset
[SVG] reuse SVG doc offsets on compile; remove deprecated version-1 support
This commit is contained in:
commit
20188a0e36
@ -1,26 +1,6 @@
|
|||||||
from fontTools.misc.textTools import bytesjoin, strjoin, tobytes, tostr
|
"""Compiles/decompiles SVG table.
|
||||||
from fontTools.misc import sstruct
|
|
||||||
from . import DefaultTable
|
|
||||||
try:
|
|
||||||
import xml.etree.cElementTree as ET
|
|
||||||
except ImportError:
|
|
||||||
import xml.etree.ElementTree as ET
|
|
||||||
from io import BytesIO
|
|
||||||
import struct
|
|
||||||
import logging
|
|
||||||
|
|
||||||
|
https://docs.microsoft.com/en-us/typography/opentype/spec/svg
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
__doc__="""
|
|
||||||
Compiles/decompiles version 0 and 1 SVG tables from/to XML.
|
|
||||||
|
|
||||||
Version 1 is the first SVG definition, implemented in Mozilla before Aug 2013, now deprecated.
|
|
||||||
This module will decompile this correctly, but will compile a version 1 table
|
|
||||||
only if you add the secret element "<version1/>" to the SVG element in the TTF file.
|
|
||||||
|
|
||||||
Version 0 is the joint Adobe-Mozilla proposal, which supports color palettes.
|
|
||||||
|
|
||||||
The XML format is:
|
The XML format is:
|
||||||
<SVG>
|
<SVG>
|
||||||
@ -31,53 +11,29 @@ The XML format is:
|
|||||||
<svgDoc endGlyphID="n" startGlyphID="m">
|
<svgDoc endGlyphID="n" startGlyphID="m">
|
||||||
<![CDATA[ <complete SVG doc> ]]
|
<![CDATA[ <complete SVG doc> ]]
|
||||||
</svgDoc>
|
</svgDoc>
|
||||||
|
|
||||||
<colorPalettes>
|
|
||||||
<colorParamUINameID>n</colorParamUINameID>
|
|
||||||
...
|
|
||||||
<colorParamUINameID>m</colorParamUINameID>
|
|
||||||
<colorPalette uiNameID="n">
|
|
||||||
<colorRecord red="<int>" green="<int>" blue="<int>" alpha="<int>" />
|
|
||||||
...
|
|
||||||
<colorRecord red="<int>" green="<int>" blue="<int>" alpha="<int>" />
|
|
||||||
</colorPalette>
|
|
||||||
...
|
|
||||||
<colorPalette uiNameID="m">
|
|
||||||
<colorRecord red="<int> green="<int>" blue="<int>" alpha="<int>" />
|
|
||||||
...
|
|
||||||
<colorRecord red=<int>" green="<int>" blue="<int>" alpha="<int>" />
|
|
||||||
</colorPalette>
|
|
||||||
</colorPalettes>
|
|
||||||
</SVG>
|
</SVG>
|
||||||
|
|
||||||
Color values must be less than 256.
|
|
||||||
|
|
||||||
The number of color records in each </colorPalette> must be the same as
|
|
||||||
the number of <colorParamUINameID> elements.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
XML = ET.XML
|
from fontTools.misc.textTools import bytesjoin, strjoin, tobytes, tostr
|
||||||
XMLElement = ET.Element
|
from fontTools.misc import sstruct
|
||||||
xmlToString = ET.tostring
|
from . import DefaultTable
|
||||||
|
from io import BytesIO
|
||||||
|
import struct
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
SVG_format_0 = """
|
SVG_format_0 = """
|
||||||
> # big endian
|
> # big endian
|
||||||
version: H
|
version: H
|
||||||
offsetToSVGDocIndex: L
|
offsetToSVGDocIndex: L
|
||||||
offsetToColorPalettes: L
|
reserved: L
|
||||||
"""
|
"""
|
||||||
|
|
||||||
SVG_format_0Size = sstruct.calcsize(SVG_format_0)
|
SVG_format_0Size = sstruct.calcsize(SVG_format_0)
|
||||||
|
|
||||||
SVG_format_1 = """
|
|
||||||
> # big endian
|
|
||||||
version: H
|
|
||||||
numIndicies: H
|
|
||||||
"""
|
|
||||||
|
|
||||||
SVG_format_1Size = sstruct.calcsize(SVG_format_1)
|
|
||||||
|
|
||||||
doc_index_entry_format_0 = """
|
doc_index_entry_format_0 = """
|
||||||
> # big endian
|
> # big endian
|
||||||
startGlyphID: H
|
startGlyphID: H
|
||||||
@ -88,84 +44,26 @@ doc_index_entry_format_0 = """
|
|||||||
|
|
||||||
doc_index_entry_format_0Size = sstruct.calcsize(doc_index_entry_format_0)
|
doc_index_entry_format_0Size = sstruct.calcsize(doc_index_entry_format_0)
|
||||||
|
|
||||||
colorRecord_format_0 = """
|
|
||||||
red: B
|
|
||||||
green: B
|
|
||||||
blue: B
|
|
||||||
alpha: B
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class table_S_V_G_(DefaultTable.DefaultTable):
|
class table_S_V_G_(DefaultTable.DefaultTable):
|
||||||
|
|
||||||
def __init__(self, tag=None):
|
|
||||||
DefaultTable.DefaultTable.__init__(self, tag)
|
|
||||||
self.colorPalettes = None
|
|
||||||
|
|
||||||
def decompile(self, data, ttFont):
|
def decompile(self, data, ttFont):
|
||||||
self.docList = None
|
self.docList = []
|
||||||
self.colorPalettes = None
|
# Version 0 is the standardized version of the table; and current.
|
||||||
pos = 0
|
# https://www.microsoft.com/typography/otspec/svg.htm
|
||||||
self.version = struct.unpack(">H", data[pos:pos+2])[0]
|
sstruct.unpack(SVG_format_0, data[:SVG_format_0Size], self)
|
||||||
|
|
||||||
if self.version == 1:
|
|
||||||
# This is pre-standardization version of the table; and obsolete. But we decompile it for now.
|
|
||||||
# https://wiki.mozilla.org/SVGOpenTypeFonts
|
|
||||||
self.decompile_format_1(data, ttFont)
|
|
||||||
else:
|
|
||||||
if self.version != 0:
|
if self.version != 0:
|
||||||
log.warning(
|
log.warning(
|
||||||
"Unknown SVG table version '%s'. Decompiling as version 0.", self.version)
|
"Unknown SVG table version '%s'. Decompiling as version 0.", self.version)
|
||||||
# This is the standardized version of the table; and current.
|
|
||||||
# https://www.microsoft.com/typography/otspec/svg.htm
|
|
||||||
self.decompile_format_0(data, ttFont)
|
|
||||||
|
|
||||||
def decompile_format_0(self, data, ttFont):
|
|
||||||
dummy, data2 = sstruct.unpack2(SVG_format_0, data, self)
|
|
||||||
# read in SVG Documents Index
|
# read in SVG Documents Index
|
||||||
self.decompileEntryList(data)
|
|
||||||
|
|
||||||
# read in colorPalettes table.
|
|
||||||
self.colorPalettes = colorPalettes = ColorPalettes()
|
|
||||||
pos = self.offsetToColorPalettes
|
|
||||||
if pos > 0:
|
|
||||||
colorPalettes.numColorParams = numColorParams = struct.unpack(">H", data[pos:pos+2])[0]
|
|
||||||
if numColorParams > 0:
|
|
||||||
colorPalettes.colorParamUINameIDs = colorParamUINameIDs = []
|
|
||||||
pos = pos + 2
|
|
||||||
for i in range(numColorParams):
|
|
||||||
nameID = struct.unpack(">H", data[pos:pos+2])[0]
|
|
||||||
colorParamUINameIDs.append(nameID)
|
|
||||||
pos = pos + 2
|
|
||||||
|
|
||||||
colorPalettes.numColorPalettes = numColorPalettes = struct.unpack(">H", data[pos:pos+2])[0]
|
|
||||||
pos = pos + 2
|
|
||||||
if numColorPalettes > 0:
|
|
||||||
colorPalettes.colorPaletteList = colorPaletteList = []
|
|
||||||
for i in range(numColorPalettes):
|
|
||||||
colorPalette = ColorPalette()
|
|
||||||
colorPaletteList.append(colorPalette)
|
|
||||||
colorPalette.uiNameID = struct.unpack(">H", data[pos:pos+2])[0]
|
|
||||||
pos = pos + 2
|
|
||||||
colorPalette.paletteColors = paletteColors = []
|
|
||||||
for j in range(numColorParams):
|
|
||||||
colorRecord, colorPaletteData = sstruct.unpack2(colorRecord_format_0, data[pos:], ColorRecord())
|
|
||||||
paletteColors.append(colorRecord)
|
|
||||||
pos += 4
|
|
||||||
|
|
||||||
def decompile_format_1(self, data, ttFont):
|
|
||||||
self.offsetToSVGDocIndex = 2
|
|
||||||
self.decompileEntryList(data)
|
|
||||||
|
|
||||||
def decompileEntryList(self, data):
|
|
||||||
# data starts with the first entry of the entry list.
|
# data starts with the first entry of the entry list.
|
||||||
pos = subTableStart = self.offsetToSVGDocIndex
|
pos = subTableStart = self.offsetToSVGDocIndex
|
||||||
self.numEntries = numEntries = struct.unpack(">H", data[pos:pos+2])[0]
|
self.numEntries = struct.unpack(">H", data[pos:pos+2])[0]
|
||||||
pos += 2
|
pos += 2
|
||||||
if self.numEntries > 0:
|
if self.numEntries > 0:
|
||||||
data2 = data[pos:]
|
data2 = data[pos:]
|
||||||
self.docList = []
|
entries = []
|
||||||
self.entries = entries = []
|
|
||||||
for i in range(self.numEntries):
|
for i in range(self.numEntries):
|
||||||
docIndexEntry, data2 = sstruct.unpack2(doc_index_entry_format_0, data2, DocumentIndexEntry())
|
docIndexEntry, data2 = sstruct.unpack2(doc_index_entry_format_0, data2, DocumentIndexEntry())
|
||||||
entries.append(docIndexEntry)
|
entries.append(docIndexEntry)
|
||||||
@ -185,13 +83,6 @@ class table_S_V_G_(DefaultTable.DefaultTable):
|
|||||||
self.docList.append( [doc, entry.startGlyphID, entry.endGlyphID] )
|
self.docList.append( [doc, entry.startGlyphID, entry.endGlyphID] )
|
||||||
|
|
||||||
def compile(self, ttFont):
|
def compile(self, ttFont):
|
||||||
if hasattr(self, "version1"):
|
|
||||||
data = self.compileFormat1(ttFont)
|
|
||||||
else:
|
|
||||||
data = self.compileFormat0(ttFont)
|
|
||||||
return data
|
|
||||||
|
|
||||||
def compileFormat0(self, ttFont):
|
|
||||||
version = 0
|
version = 0
|
||||||
offsetToSVGDocIndex = SVG_format_0Size # I start the SVGDocIndex right after the header.
|
offsetToSVGDocIndex = SVG_format_0Size # I start the SVGDocIndex right after the header.
|
||||||
# get SGVDoc info.
|
# get SGVDoc info.
|
||||||
@ -201,8 +92,8 @@ class table_S_V_G_(DefaultTable.DefaultTable):
|
|||||||
datum = struct.pack(">H",numEntries)
|
datum = struct.pack(">H",numEntries)
|
||||||
entryList.append(datum)
|
entryList.append(datum)
|
||||||
curOffset = len(datum) + doc_index_entry_format_0Size*numEntries
|
curOffset = len(datum) + doc_index_entry_format_0Size*numEntries
|
||||||
|
seenDocs = {}
|
||||||
for doc, startGlyphID, endGlyphID in self.docList:
|
for doc, startGlyphID, endGlyphID in self.docList:
|
||||||
docOffset = curOffset
|
|
||||||
docBytes = tobytes(doc, encoding="utf_8")
|
docBytes = tobytes(doc, encoding="utf_8")
|
||||||
if getattr(self, "compressed", False) and not docBytes.startswith(b"\x1f\x8b"):
|
if getattr(self, "compressed", False) and not docBytes.startswith(b"\x1f\x8b"):
|
||||||
import gzip
|
import gzip
|
||||||
@ -214,63 +105,25 @@ class table_S_V_G_(DefaultTable.DefaultTable):
|
|||||||
docBytes = gzipped
|
docBytes = gzipped
|
||||||
del gzipped, bytesIO
|
del gzipped, bytesIO
|
||||||
docLength = len(docBytes)
|
docLength = len(docBytes)
|
||||||
|
if docBytes in seenDocs:
|
||||||
|
docOffset = seenDocs[docBytes]
|
||||||
|
else:
|
||||||
|
docOffset = curOffset
|
||||||
curOffset += docLength
|
curOffset += docLength
|
||||||
|
seenDocs[docBytes] = docOffset
|
||||||
|
docList.append(docBytes)
|
||||||
entry = struct.pack(">HHLL", startGlyphID, endGlyphID, docOffset, docLength)
|
entry = struct.pack(">HHLL", startGlyphID, endGlyphID, docOffset, docLength)
|
||||||
entryList.append(entry)
|
entryList.append(entry)
|
||||||
docList.append(docBytes)
|
|
||||||
entryList.extend(docList)
|
entryList.extend(docList)
|
||||||
svgDocData = bytesjoin(entryList)
|
svgDocData = bytesjoin(entryList)
|
||||||
|
|
||||||
# get colorpalette info.
|
reserved = 0
|
||||||
if self.colorPalettes is None:
|
header = struct.pack(">HLL", version, offsetToSVGDocIndex, reserved)
|
||||||
offsetToColorPalettes = 0
|
data = [header, svgDocData]
|
||||||
palettesData = ""
|
|
||||||
else:
|
|
||||||
offsetToColorPalettes = SVG_format_0Size + len(svgDocData)
|
|
||||||
dataList = []
|
|
||||||
numColorParams = len(self.colorPalettes.colorParamUINameIDs)
|
|
||||||
datum = struct.pack(">H", numColorParams)
|
|
||||||
dataList.append(datum)
|
|
||||||
for uiNameId in self.colorPalettes.colorParamUINameIDs:
|
|
||||||
datum = struct.pack(">H", uiNameId)
|
|
||||||
dataList.append(datum)
|
|
||||||
numColorPalettes = len(self.colorPalettes.colorPaletteList)
|
|
||||||
datum = struct.pack(">H", numColorPalettes)
|
|
||||||
dataList.append(datum)
|
|
||||||
for colorPalette in self.colorPalettes.colorPaletteList:
|
|
||||||
datum = struct.pack(">H", colorPalette.uiNameID)
|
|
||||||
dataList.append(datum)
|
|
||||||
for colorRecord in colorPalette.paletteColors:
|
|
||||||
data = struct.pack(">BBBB", colorRecord.red, colorRecord.green, colorRecord.blue, colorRecord.alpha)
|
|
||||||
dataList.append(data)
|
|
||||||
palettesData = bytesjoin(dataList)
|
|
||||||
|
|
||||||
header = struct.pack(">HLL", version, offsetToSVGDocIndex, offsetToColorPalettes)
|
|
||||||
data = [header, svgDocData, palettesData]
|
|
||||||
data = bytesjoin(data)
|
data = bytesjoin(data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def compileFormat1(self, ttFont):
|
|
||||||
version = 1
|
|
||||||
numEntries = len(self.docList)
|
|
||||||
header = struct.pack(">HH", version, numEntries)
|
|
||||||
dataList = [header]
|
|
||||||
docList = []
|
|
||||||
curOffset = SVG_format_1Size + doc_index_entry_format_0Size*numEntries
|
|
||||||
for doc, startGlyphID, endGlyphID in self.docList:
|
|
||||||
docOffset = curOffset
|
|
||||||
docBytes = tobytes(doc, encoding="utf_8")
|
|
||||||
docLength = len(docBytes)
|
|
||||||
curOffset += docLength
|
|
||||||
entry = struct.pack(">HHLL", startGlyphID, endGlyphID, docOffset, docLength)
|
|
||||||
dataList.append(entry)
|
|
||||||
docList.append(docBytes)
|
|
||||||
dataList.extend(docList)
|
|
||||||
data = bytesjoin(dataList)
|
|
||||||
return data
|
|
||||||
|
|
||||||
def toXML(self, writer, ttFont):
|
def toXML(self, writer, ttFont):
|
||||||
writer.newline()
|
|
||||||
for doc, startGID, endGID in self.docList:
|
for doc, startGID, endGID in self.docList:
|
||||||
writer.begintag("svgDoc", startGlyphID=startGID, endGlyphID=endGID)
|
writer.begintag("svgDoc", startGlyphID=startGID, endGlyphID=endGID)
|
||||||
writer.newline()
|
writer.newline()
|
||||||
@ -279,33 +132,6 @@ class table_S_V_G_(DefaultTable.DefaultTable):
|
|||||||
writer.endtag("svgDoc")
|
writer.endtag("svgDoc")
|
||||||
writer.newline()
|
writer.newline()
|
||||||
|
|
||||||
if (self.colorPalettes is not None) and (self.colorPalettes.numColorParams is not None):
|
|
||||||
writer.begintag("colorPalettes")
|
|
||||||
writer.newline()
|
|
||||||
for uiNameID in self.colorPalettes.colorParamUINameIDs:
|
|
||||||
writer.begintag("colorParamUINameID")
|
|
||||||
writer._writeraw(str(uiNameID))
|
|
||||||
writer.endtag("colorParamUINameID")
|
|
||||||
writer.newline()
|
|
||||||
for colorPalette in self.colorPalettes.colorPaletteList:
|
|
||||||
writer.begintag("colorPalette", [("uiNameID", str(colorPalette.uiNameID))])
|
|
||||||
writer.newline()
|
|
||||||
for colorRecord in colorPalette.paletteColors:
|
|
||||||
colorAttributes = [
|
|
||||||
("red", hex(colorRecord.red)),
|
|
||||||
("green", hex(colorRecord.green)),
|
|
||||||
("blue", hex(colorRecord.blue)),
|
|
||||||
("alpha", hex(colorRecord.alpha)),
|
|
||||||
]
|
|
||||||
writer.begintag("colorRecord", colorAttributes)
|
|
||||||
writer.endtag("colorRecord")
|
|
||||||
writer.newline()
|
|
||||||
writer.endtag("colorPalette")
|
|
||||||
writer.newline()
|
|
||||||
|
|
||||||
writer.endtag("colorPalettes")
|
|
||||||
writer.newline()
|
|
||||||
|
|
||||||
def fromXML(self, name, attrs, content, ttFont):
|
def fromXML(self, name, attrs, content, ttFont):
|
||||||
if name == "svgDoc":
|
if name == "svgDoc":
|
||||||
if not hasattr(self, "docList"):
|
if not hasattr(self, "docList"):
|
||||||
@ -315,14 +141,10 @@ class table_S_V_G_(DefaultTable.DefaultTable):
|
|||||||
startGID = int(attrs["startGlyphID"])
|
startGID = int(attrs["startGlyphID"])
|
||||||
endGID = int(attrs["endGlyphID"])
|
endGID = int(attrs["endGlyphID"])
|
||||||
self.docList.append( [doc, startGID, endGID] )
|
self.docList.append( [doc, startGID, endGID] )
|
||||||
elif name == "colorPalettes":
|
|
||||||
self.colorPalettes = ColorPalettes()
|
|
||||||
self.colorPalettes.fromXML(name, attrs, content, ttFont)
|
|
||||||
if self.colorPalettes.numColorParams == 0:
|
|
||||||
self.colorPalettes = None
|
|
||||||
else:
|
else:
|
||||||
log.warning("Unknown %s %s", name, content)
|
log.warning("Unknown %s %s", name, content)
|
||||||
|
|
||||||
|
|
||||||
class DocumentIndexEntry(object):
|
class DocumentIndexEntry(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.startGlyphID = None # USHORT
|
self.startGlyphID = None # USHORT
|
||||||
@ -332,55 +154,3 @@ class DocumentIndexEntry(object):
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "startGlyphID: %s, endGlyphID: %s, svgDocOffset: %s, svgDocLength: %s" % (self.startGlyphID, self.endGlyphID, self.svgDocOffset, self.svgDocLength)
|
return "startGlyphID: %s, endGlyphID: %s, svgDocOffset: %s, svgDocLength: %s" % (self.startGlyphID, self.endGlyphID, self.svgDocOffset, self.svgDocLength)
|
||||||
|
|
||||||
class ColorPalettes(object):
|
|
||||||
def __init__(self):
|
|
||||||
self.numColorParams = None # USHORT
|
|
||||||
self.colorParamUINameIDs = [] # list of name table name ID values that provide UI description of each color palette.
|
|
||||||
self.numColorPalettes = None # USHORT
|
|
||||||
self.colorPaletteList = [] # list of ColorPalette records
|
|
||||||
|
|
||||||
def fromXML(self, name, attrs, content, ttFont):
|
|
||||||
for element in content:
|
|
||||||
if not isinstance(element, tuple):
|
|
||||||
continue
|
|
||||||
name, attrib, content = element
|
|
||||||
if name == "colorParamUINameID":
|
|
||||||
uiNameID = int(content[0])
|
|
||||||
self.colorParamUINameIDs.append(uiNameID)
|
|
||||||
elif name == "colorPalette":
|
|
||||||
colorPalette = ColorPalette()
|
|
||||||
self.colorPaletteList.append(colorPalette)
|
|
||||||
colorPalette.fromXML(name, attrib, content, ttFont)
|
|
||||||
|
|
||||||
self.numColorParams = len(self.colorParamUINameIDs)
|
|
||||||
self.numColorPalettes = len(self.colorPaletteList)
|
|
||||||
for colorPalette in self.colorPaletteList:
|
|
||||||
if len(colorPalette.paletteColors) != self.numColorParams:
|
|
||||||
raise ValueError("Number of color records in a colorPalette ('%s') does not match the number of colorParamUINameIDs elements ('%s')." % (len(colorPalette.paletteColors), self.numColorParams))
|
|
||||||
|
|
||||||
class ColorPalette(object):
|
|
||||||
def __init__(self):
|
|
||||||
self.uiNameID = None # USHORT. name table ID that describes user interface strings associated with this color palette.
|
|
||||||
self.paletteColors = [] # list of ColorRecords
|
|
||||||
|
|
||||||
def fromXML(self, name, attrs, content, ttFont):
|
|
||||||
self.uiNameID = int(attrs["uiNameID"])
|
|
||||||
for element in content:
|
|
||||||
if isinstance(element, type("")):
|
|
||||||
continue
|
|
||||||
name, attrib, content = element
|
|
||||||
if name == "colorRecord":
|
|
||||||
colorRecord = ColorRecord()
|
|
||||||
self.paletteColors.append(colorRecord)
|
|
||||||
colorRecord.red = eval(attrib["red"])
|
|
||||||
colorRecord.green = eval(attrib["green"])
|
|
||||||
colorRecord.blue = eval(attrib["blue"])
|
|
||||||
colorRecord.alpha = eval(attrib["alpha"])
|
|
||||||
|
|
||||||
class ColorRecord(object):
|
|
||||||
def __init__(self):
|
|
||||||
self.red = 255 # all are one byte values.
|
|
||||||
self.green = 255
|
|
||||||
self.blue = 255
|
|
||||||
self.alpha = 255
|
|
||||||
|
131
Tests/ttLib/tables/S_V_G__test.py
Normal file
131
Tests/ttLib/tables/S_V_G__test.py
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
import struct
|
||||||
|
|
||||||
|
from fontTools.misc import etree
|
||||||
|
from fontTools.misc.testTools import getXML, parseXML
|
||||||
|
from fontTools.ttLib import TTFont
|
||||||
|
from fontTools.ttLib.tables.S_V_G_ import table_S_V_G_
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def dump(table, ttFont=None):
|
||||||
|
print("\n".join(getXML(table.toXML, ttFont)))
|
||||||
|
|
||||||
|
|
||||||
|
def strip_xml_whitespace(xml_string):
|
||||||
|
def strip_or_none(text):
|
||||||
|
text = text.strip() if text else None
|
||||||
|
return text if text else None
|
||||||
|
|
||||||
|
tree = etree.fromstring(xml_string)
|
||||||
|
for e in tree.iter("*"):
|
||||||
|
e.text = strip_or_none(e.text)
|
||||||
|
e.tail = strip_or_none(e.tail)
|
||||||
|
return etree.tostring(tree, encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
SVG_DOCS = [
|
||||||
|
strip_xml_whitespace(svg)
|
||||||
|
for svg in (
|
||||||
|
b"""\
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
|
||||||
|
<defs>
|
||||||
|
<rect x="100" y="-200" width="300" height="400" id="p1"/>
|
||||||
|
</defs>
|
||||||
|
<g id="glyph1">
|
||||||
|
<use xlink:href="#p1" fill="#red"/>
|
||||||
|
</g>
|
||||||
|
<g id="glyph2">
|
||||||
|
<use xlink:href="#p1" fill="#blue"/>
|
||||||
|
</g>
|
||||||
|
<g id="glyph4">
|
||||||
|
<use xlink:href="#p1" fill="#green"/>
|
||||||
|
</g>
|
||||||
|
</svg>""",
|
||||||
|
b"""\
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
|
||||||
|
<g id="glyph3">
|
||||||
|
<path d="M0,0 L100,0 L50,100 Z"/>
|
||||||
|
</g>
|
||||||
|
</svg>""",
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
OTSVG_DATA = b"".join(
|
||||||
|
[
|
||||||
|
# SVG table header
|
||||||
|
b"\x00\x00" # version (0)
|
||||||
|
b"\x00\x00\x00\x0a" # offset to SVGDocumentList (10)
|
||||||
|
b"\x00\x00\x00\x00" # reserved (0)
|
||||||
|
# SVGDocumentList
|
||||||
|
b"\x00\x03" # number of SVGDocumentRecords (3)
|
||||||
|
# SVGDocumentRecord[0]
|
||||||
|
b"\x00\x01" # startGlyphID (1)
|
||||||
|
b"\x00\x02" # endGlyphID (2)
|
||||||
|
b"\x00\x00\x00\x26" # svgDocOffset (2 + 12*3 == 38 == 0x26)
|
||||||
|
+ struct.pack(">L", len(SVG_DOCS[0])) # svgDocLength
|
||||||
|
# SVGDocumentRecord[1]
|
||||||
|
+ b"\x00\x03" # startGlyphID (3)
|
||||||
|
b"\x00\x03" # endGlyphID (3)
|
||||||
|
+ struct.pack(">L", 0x26 + len(SVG_DOCS[0])) # svgDocOffset
|
||||||
|
+ struct.pack(">L", len(SVG_DOCS[1])) # svgDocLength
|
||||||
|
# SVGDocumentRecord[2]
|
||||||
|
+ b"\x00\x04" # startGlyphID (4)
|
||||||
|
b"\x00\x04" # endGlyphID (4)
|
||||||
|
b"\x00\x00\x00\x26" # svgDocOffset (38); records 0 and 2 point to same SVG doc
|
||||||
|
+ struct.pack(">L", len(SVG_DOCS[0])) # svgDocLength
|
||||||
|
]
|
||||||
|
+ SVG_DOCS
|
||||||
|
)
|
||||||
|
|
||||||
|
OTSVG_TTX = [
|
||||||
|
'<svgDoc endGlyphID="2" startGlyphID="1">',
|
||||||
|
f" <![CDATA[{SVG_DOCS[0].decode()}]]>",
|
||||||
|
"</svgDoc>",
|
||||||
|
'<svgDoc endGlyphID="3" startGlyphID="3">',
|
||||||
|
f" <![CDATA[{SVG_DOCS[1].decode()}]]>",
|
||||||
|
"</svgDoc>",
|
||||||
|
'<svgDoc endGlyphID="4" startGlyphID="4">',
|
||||||
|
f" <![CDATA[{SVG_DOCS[0].decode()}]]>",
|
||||||
|
"</svgDoc>",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def font():
|
||||||
|
font = TTFont()
|
||||||
|
font.setGlyphOrder([".notdef"] + ["glyph%05d" % i for i in range(1, 30)])
|
||||||
|
return font
|
||||||
|
|
||||||
|
|
||||||
|
def test_decompile_and_compile(font):
|
||||||
|
table = table_S_V_G_()
|
||||||
|
table.decompile(OTSVG_DATA, font)
|
||||||
|
assert table.compile(font) == OTSVG_DATA
|
||||||
|
|
||||||
|
|
||||||
|
def test_decompile_and_dump_ttx(font):
|
||||||
|
table = table_S_V_G_()
|
||||||
|
table.decompile(OTSVG_DATA, font)
|
||||||
|
|
||||||
|
dump(table, font)
|
||||||
|
assert getXML(table.toXML, font) == OTSVG_TTX
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_from_ttx_and_compile(font):
|
||||||
|
table = table_S_V_G_()
|
||||||
|
for name, attrs, content in parseXML(OTSVG_TTX):
|
||||||
|
table.fromXML(name, attrs, content, font)
|
||||||
|
assert table.compile(font) == OTSVG_DATA
|
||||||
|
|
||||||
|
|
||||||
|
def test_round_trip_ttx(font):
|
||||||
|
table = table_S_V_G_()
|
||||||
|
for name, attrs, content in parseXML(OTSVG_TTX):
|
||||||
|
table.fromXML(name, attrs, content, font)
|
||||||
|
compiled = table.compile(font)
|
||||||
|
|
||||||
|
table = table_S_V_G_()
|
||||||
|
table.decompile(compiled, font)
|
||||||
|
assert getXML(table.toXML, font) == OTSVG_TTX
|
Loading…
x
Reference in New Issue
Block a user