From a82affb34c2a6489b9e9c5d00384461de0682063 Mon Sep 17 00:00:00 2001 From: Sascha Brawer Date: Mon, 27 Apr 2015 13:46:34 +0200 Subject: [PATCH] [GX] Move XML generation for GlyphVariations into separate method --- Lib/fontTools/ttLib/tables/_g_v_a_r.py | 54 +++++++++++---------- Lib/fontTools/ttLib/tables/_g_v_a_r_test.py | 38 ++++++++++++++- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/Lib/fontTools/ttLib/tables/_g_v_a_r.py b/Lib/fontTools/ttLib/tables/_g_v_a_r.py index 3daea6bc1..43216a90e 100644 --- a/Lib/fontTools/ttLib/tables/_g_v_a_r.py +++ b/Lib/fontTools/ttLib/tables/_g_v_a_r.py @@ -260,32 +260,8 @@ class table__g_v_a_r(DefaultTable.DefaultTable): continue writer.begintag("glyphVariation", glyph=glyphName) writer.newline() - for tupleIndex in xrange(len(tuples)): - tuple = tuples[tupleIndex] - 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() + for tuple in tuples: + tuple.toXML(writer, axisTags) writer.endtag("glyphVariation") writer.newline() @@ -298,3 +274,29 @@ class GlyphVariation: def __repr__(self): axes = ",".join(sorted(['%s=%s' % (name, value) for (name, value) in self.axes.items()])) return '' % (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() diff --git a/Lib/fontTools/ttLib/tables/_g_v_a_r_test.py b/Lib/fontTools/ttLib/tables/_g_v_a_r_test.py index 52d206d32..67ee3843f 100644 --- a/Lib/fontTools/ttLib/tables/_g_v_a_r_test.py +++ b/Lib/fontTools/ttLib/tables/_g_v_a_r_test.py @@ -1,10 +1,11 @@ from __future__ import print_function, division, absolute_import, unicode_literals from fontTools.misc.py23 import * +from fontTools.misc.xmlWriter import XMLWriter from fontTools import ttLib 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 - def hexdecode(s): 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)) +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([ + '', + '', + '', + '', + '', + '', + '' + ], 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([ + '', + '', + '', + '', + '' + ], 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__": unittest.main()