[GX] Move XML generation for GlyphVariations into separate method

This commit is contained in:
Sascha Brawer 2015-04-27 13:46:34 +02:00
parent e0bd569a73
commit a82affb34c
2 changed files with 65 additions and 27 deletions

View File

@ -260,32 +260,8 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
continue continue
writer.begintag("glyphVariation", glyph=glyphName) writer.begintag("glyphVariation", glyph=glyphName)
writer.newline() writer.newline()
for tupleIndex in xrange(len(tuples)): for tuple in tuples:
tuple = tuples[tupleIndex] tuple.toXML(writer, axisTags)
writer.begintag("tuple")
writer.newline()
for axis in axisTags:
value = tuple.axes.get(axis)
if value != None:
minValue, value, maxValue = value
if minValue == value and maxValue == value:
writer.simpletag("coord", axis=axis, value=value)
else:
writer.simpletag("coord", axis=axis, value=value,
min=minValue, max=maxValue)
writer.newline()
wrote_any_points = False
for i in xrange(len(tuple.coordinates)):
x, y = tuple.coordinates[i]
if x != 0 or y != 0:
writer.simpletag("delta", pt=i, x=x, y=y)
writer.newline()
wrote_any_points = True
if not wrote_any_points:
writer.comment("all deltas are (0,0)")
writer.newline()
writer.endtag("tuple")
writer.newline()
writer.endtag("glyphVariation") writer.endtag("glyphVariation")
writer.newline() writer.newline()
@ -298,3 +274,29 @@ class GlyphVariation:
def __repr__(self): def __repr__(self):
axes = ",".join(sorted(['%s=%s' % (name, value) for (name, value) in self.axes.items()])) axes = ",".join(sorted(['%s=%s' % (name, value) for (name, value) in self.axes.items()]))
return '<GlyphVariation %s %s>' % (axes, self.coordinates) return '<GlyphVariation %s %s>' % (axes, self.coordinates)
def toXML(self, writer, axisTags):
writer.begintag("tuple")
writer.newline()
for axis in axisTags:
value = self.axes.get(axis)
if value != None:
minValue, value, maxValue = value
if minValue == value and maxValue == value:
writer.simpletag("coord", axis=axis, value=value)
else:
writer.simpletag("coord", axis=axis, value=value,
min=minValue, max=maxValue)
writer.newline()
wrote_any_points = False
for i in xrange(len(self.coordinates)):
x, y = self.coordinates[i]
if x != 0 or y != 0:
writer.simpletag("delta", pt=i, x=x, y=y)
writer.newline()
wrote_any_points = True
if not wrote_any_points:
writer.comment("all deltas are (0,0)")
writer.newline()
writer.endtag("tuple")
writer.newline()

View File

@ -1,10 +1,11 @@
from __future__ import print_function, division, absolute_import, unicode_literals from __future__ import print_function, division, absolute_import, unicode_literals
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools.misc.xmlWriter import XMLWriter
from fontTools import ttLib from fontTools import ttLib
import unittest import unittest
from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
from fontTools.ttLib.tables._g_v_a_r import table__g_v_a_r, GlyphVariation from fontTools.ttLib.tables._g_v_a_r import table__g_v_a_r, GlyphVariation
def hexdecode(s): def hexdecode(s):
return bytesjoin([c.decode("hex") for c in s.split()]) return bytesjoin([c.decode("hex") for c in s.split()])
@ -167,5 +168,40 @@ class GlyphVariationTableTest(unittest.TestCase):
self.assertEqual(([0, 0, 0, 0, 258, -127, -128], 11), decompileDeltas(7, data, 4)) self.assertEqual(([0, 0, 0, 0, 258, -127, -128], 11), decompileDeltas(7, data, 4))
class GlyphVariationTest(unittest.TestCase):
def test_toXML(self):
writer = XMLWriter(StringIO())
axes = {"wdth":(0.3, 0.4, 0.5), "wght":(1.0, 1.0, 1.0)}
g = GlyphVariation(axes, GlyphCoordinates([(9,8), (7,6), (0,0), (-1,-2)]))
g.toXML(writer, ["wght", "wdth"])
self.assertEqual([
'<tuple>',
'<coord axis="wght" value="1.0"/>',
'<coord axis="wdth" max="0.5" min="0.3" value="0.4"/>',
'<delta pt="0" x="9" y="8"/>',
'<delta pt="1" x="7" y="6"/>',
'<delta pt="3" x="-1" y="-2"/>',
'</tuple>'
], GlyphVariationTest.xml_lines(writer))
def test_toXML_allDeltasZero(self):
writer = XMLWriter(StringIO())
axes = {"wdth":(0.3, 0.4, 0.5), "wght":(1.0, 1.0, 1.0)}
g = GlyphVariation(axes, GlyphCoordinates.zeros(5))
g.toXML(writer, ["wght", "wdth"])
self.assertEqual([
'<tuple>',
'<coord axis="wght" value="1.0"/>',
'<coord axis="wdth" max="0.5" min="0.3" value="0.4"/>',
'<!-- all deltas are (0,0) -->',
'</tuple>'
], GlyphVariationTest.xml_lines(writer))
@staticmethod
def xml_lines(writer):
content = writer.file.getvalue().decode("utf-8")
return [line.strip() for line in content.splitlines()][1:]
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()