From 7f6d2af45af8e27b78b78c44a71105859acf92d1 Mon Sep 17 00:00:00 2001 From: Sascha Brawer Date: Wed, 10 Jun 2015 11:19:06 +0200 Subject: [PATCH] =?UTF-8?q?[GX]=20Optimize=20=E2=80=98gvar=E2=80=99=20tabl?= =?UTF-8?q?es=20for=20tuples=20affecting=20all=20points=20in=20a=20glyph?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After this change, round-tripping through TTX shrinks the size of all our test fonts. Specifically, Skia.ttf shrinks from 478K to 473K, JamRegular.ttf from 113K to 107K, and BuffaloGalRegular from 67K to 61K. --- Lib/fontTools/ttLib/tables/_g_v_a_r.py | 5 +++++ Lib/fontTools/ttLib/tables/_g_v_a_r_test.py | 17 +++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Lib/fontTools/ttLib/tables/_g_v_a_r.py b/Lib/fontTools/ttLib/tables/_g_v_a_r.py index 4322573d8..67d378bf8 100644 --- a/Lib/fontTools/ttLib/tables/_g_v_a_r.py +++ b/Lib/fontTools/ttLib/tables/_g_v_a_r.py @@ -490,6 +490,11 @@ class GlyphVariation(object): @staticmethod def compilePoints(points, numPointsInGlyph): + # If the set consists of all points in the glyph, it gets encoded with + # a special encoding: a single zero byte. + if len(points) == numPointsInGlyph: + return b"\0" + # In the 'gvar' table, the packing of point numbers is a little surprising. # It consists of multiple runs, each being a delta-encoded list of integers. # For example, the point set {17, 18, 19, 20, 21, 22, 23} gets encoded as 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 bdfe625f4..b76d0e45d 100644 --- a/Lib/fontTools/ttLib/tables/_g_v_a_r_test.py +++ b/Lib/fontTools/ttLib/tables/_g_v_a_r_test.py @@ -271,8 +271,8 @@ class GlyphVariationTest(unittest.TestCase): tuple, data = gvar.compile(axisTags, sharedCoordIndices, sharedPoints=None) # len(data)=13; flags=PRIVATE_POINT_NUMBERS; tupleIndex=0x77 # embeddedCoord=[]; intermediateCoord=[] - self.assertEqual("00 0D 20 77", hexencode(tuple)) - self.assertEqual("03 02 00 01 01 " # 3 points: [0, 1, 2] + self.assertEqual("00 09 20 77", hexencode(tuple)) + self.assertEqual("00 " # all points in glyph "02 07 08 09 " # deltaX: [7, 8, 9] "02 04 05 06", # deltaY: [4, 5, 6] hexencode(data)) @@ -285,8 +285,8 @@ class GlyphVariationTest(unittest.TestCase): tuple, data = gvar.compile(axisTags, sharedCoordIndices, sharedPoints=None) # len(data)=13; flags=PRIVATE_POINT_NUMBERS; tupleIndex=0x77 # embeddedCoord=[]; intermediateCoord=[(0.0, 0.0), (1.0, 1.0)] - self.assertEqual("00 0D 60 77 00 00 00 00 40 00 40 00", hexencode(tuple)) - self.assertEqual("03 02 00 01 01 " # 3 points: [0, 1, 2] + self.assertEqual("00 09 60 77 00 00 00 00 40 00 40 00", hexencode(tuple)) + self.assertEqual("00 " # all points in glyph "02 07 08 09 " # deltaX: [7, 8, 9] "02 04 05 06", # deltaY: [4, 5, 6] hexencode(data)) @@ -322,8 +322,8 @@ class GlyphVariationTest(unittest.TestCase): tuple, data = gvar.compile(axisTags, sharedCoordIndices={}, sharedPoints=None) # len(data)=13; flags=PRIVATE_POINT_NUMBERS|EMBEDDED_TUPLE_COORD # embeddedCoord=[(0.5, 0.8)]; intermediateCoord=[] - self.assertEqual("00 0D A0 00 20 00 33 33", hexencode(tuple)) - self.assertEqual("03 02 00 01 01 " # 3 points: [0, 1, 2] + self.assertEqual("00 09 A0 00 20 00 33 33", hexencode(tuple)) + self.assertEqual("00 " # all points in glyph "02 07 08 09 " # deltaX: [7, 8, 9] "02 04 05 06", # deltaY: [4, 5, 6] hexencode(data)) @@ -335,8 +335,8 @@ class GlyphVariationTest(unittest.TestCase): tuple, data = gvar.compile(axisTags, sharedCoordIndices={}, sharedPoints=None) # len(data)=13; flags=PRIVATE_POINT_NUMBERS|INTERMEDIATE_TUPLE|EMBEDDED_TUPLE_COORD # embeddedCoord=(0.5, 0.8); intermediateCoord=[(0.4, 0.7), (0.6, 0.9)] - self.assertEqual("00 0D E0 00 20 00 33 33 19 9A 2C CD 26 66 39 9A", hexencode(tuple)) - self.assertEqual("03 02 00 01 01 " # 3 points: [0, 1, 2] + self.assertEqual("00 09 E0 00 20 00 33 33 19 9A 2C CD 26 66 39 9A", hexencode(tuple)) + self.assertEqual("00 " # all points in glyph "02 07 08 09 " # deltaX: [7, 8, 9] "02 04 05 06", # deltaY: [4, 5, 6] hexencode(data)) @@ -380,6 +380,7 @@ class GlyphVariationTest(unittest.TestCase): def test_compilePoints(self): compilePoints = lambda p: GlyphVariation.compilePoints(set(p), numPointsInGlyph=999) + self.assertEqual("00", hexencode(compilePoints(range(999)))) # all points in glyph self.assertEqual("01 00 07", hexencode(compilePoints([7]))) self.assertEqual("01 80 FF FF", hexencode(compilePoints([65535]))) self.assertEqual("02 01 09 06", hexencode(compilePoints([9, 15])))