[GX] Resurrect 'gvar' table from XML

This commit is contained in:
Sascha Brawer 2015-04-27 17:10:59 +02:00
parent 908bc15b74
commit 21098228f0
2 changed files with 56 additions and 5 deletions

View File

@ -63,11 +63,7 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
for i in xrange(self.glyphCount):
glyphName = glyphs[i]
glyph = ttFont["glyf"][glyphName]
if glyph.isComposite():
numPoints = len(glyph.components) + 4
else:
# Empty glyphs (eg. space, nonmarkingreturn) have no "coordinates" attribute.
numPoints = len(getattr(glyph, "coordinates", [])) + 4
numPoints = self.getNumPoints_(glyph)
gvarData = data[self.offsetToData + offsets[i] : self.offsetToData + offsets[i + 1]]
self.variations[glyphName] = self.decompileTuples_(numPoints, sharedCoords, axisTags, gvarData)
@ -190,6 +186,39 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
writer.endtag("glyphVariations")
writer.newline()
def fromXML(self, name, attrs, content, ttFont):
if name == "version":
self.version = safeEval(attrs["value"])
elif name == "reserved":
self.reserved = safeEval(attrs["value"])
elif name == "glyphVariations":
if not hasattr(self, "variations"):
self.variations = {}
glyphName = attrs["glyph"]
glyph = ttFont["glyf"][glyphName]
numPoints = self.getNumPoints_(glyph)
glyphVariations = []
for element in content:
if isinstance(element, tuple):
name, attrs, content = element
if name == "tuple":
gvar = GlyphVariation({}, GlyphCoordinates.zeros(numPoints))
glyphVariations.append(gvar)
for tupleElement in content:
if isinstance(tupleElement, tuple):
tupleName, tupleAttrs, tupleContent = tupleElement
gvar.fromXML(tupleName, tupleAttrs, tupleContent)
self.variations[glyphName] = glyphVariations
@staticmethod
def getNumPoints_(glyph):
NUM_PHANTOM_POINTS = 4
if glyph.isComposite():
return len(glyph.components) + NUM_PHANTOM_POINTS
else:
# Empty glyphs (eg. space, nonmarkingreturn) have no "coordinates" attribute.
return len(getattr(glyph, "coordinates", [])) + NUM_PHANTOM_POINTS
class GlyphVariation:
def __init__(self, axes, coordinates):
@ -226,6 +255,19 @@ class GlyphVariation:
writer.endtag("tuple")
writer.newline()
def fromXML(self, name, attrs, content):
if name == "coord":
axis = attrs["axis"]
value = attrs["value"]
minValue = attrs.get("min", value)
maxValue = attrs.get("max", value)
self.axes[axis] = (safeEval(minValue), safeEval(value), safeEval(maxValue))
elif name == "delta":
point = safeEval(attrs["pt"])
x = safeEval(attrs["x"])
y = safeEval(attrs["y"])
self.coordinates[point] = (x, y)
@staticmethod
def decompileCoord_(axisTags, data, offset):
coord = {}

View File

@ -131,6 +131,15 @@ class GlyphVariationTest(unittest.TestCase):
'</tuple>'
], GlyphVariationTest.xml_lines(writer))
def test_fromXML(self):
g = GlyphVariation({}, GlyphCoordinates.zeros(4))
g.fromXML("coord", {"axis":"wght", "value":"1.0"}, [])
g.fromXML("coord", {"axis":"wdth", "min":"0.3", "value":"0.4", "max":"0.5"}, [])
g.fromXML("delta", {"pt":"1", "x":"33", "y":"44"}, [])
g.fromXML("delta", {"pt":"2", "x":"-2", "y":"170"}, [])
self.assertEqual({"wght":(1.0, 1.0, 1.0), "wdth":(0.3, 0.4, 0.5)}, g.axes)
self.assertEqual("0,0 33,44 -2,170 0,0", " ".join(["%d,%d" % c for c in g.coordinates]))
def test_decompileCoord(self):
decompileCoord = GlyphVariation.decompileCoord_
data = hexdecode("DE AD C0 00 20 00 DE AD")