From 84b0cacf957566e9758102e0d92ffff28af66e39 Mon Sep 17 00:00:00 2001 From: Sascha Brawer Date: Tue, 22 Aug 2017 20:37:44 +0200 Subject: [PATCH] [AAT] Implement subsetting of `opbd` Optical Bounds table --- Lib/fontTools/subset/__init__.py | 16 ++ Tests/subset/data/TestOPBD-0.ttx | 299 ++++++++++++++++++++++++++++ Tests/subset/data/TestOPBD-1.ttx | 299 ++++++++++++++++++++++++++++ Tests/subset/data/expect_opbd_0.ttx | 18 ++ Tests/subset/data/expect_opbd_1.ttx | 18 ++ Tests/subset/subset_test.py | 25 +++ 6 files changed, 675 insertions(+) create mode 100644 Tests/subset/data/TestOPBD-0.ttx create mode 100644 Tests/subset/data/TestOPBD-1.ttx create mode 100644 Tests/subset/data/expect_opbd_0.ttx create mode 100644 Tests/subset/data/expect_opbd_1.ttx diff --git a/Lib/fontTools/subset/__init__.py b/Lib/fontTools/subset/__init__.py index 4da48c73a..a41c48a4a 100644 --- a/Lib/fontTools/subset/__init__.py +++ b/Lib/fontTools/subset/__init__.py @@ -1694,6 +1694,22 @@ def subset_glyphs(self, s): self.numVertOriginYMetrics = len(self.VOriginRecords) return True # Never drop; has default metrics +@_add_method(ttLib.getTableClass('opbd')) +def subset_glyphs(self, s): + table = self.table.OpticalBounds + if table.Format == 0: + table.OpticalBoundsDeltas = {glyph: table.OpticalBoundsDeltas[glyph] + for glyph in s.glyphs + if glyph in table.OpticalBoundsDeltas} + return len(table.OpticalBoundsDeltas) > 0 + elif table.Format == 1: + table.OpticalBoundsPoints = {glyph: table.OpticalBoundsPoints[glyph] + for glyph in s.glyphs + if glyph in table.OpticalBoundsPoints} + return len(table.OpticalBoundsPoints) > 0 + else: + assert False, "unknown 'opbd' format %s" % table.Format + @_add_method(ttLib.getTableClass('post')) def prune_pre_subset(self, font, options): if not options.glyph_names: diff --git a/Tests/subset/data/TestOPBD-0.ttx b/Tests/subset/data/TestOPBD-0.ttx new file mode 100644 index 000000000..8d7af0538 --- /dev/null +++ b/Tests/subset/data/TestOPBD-0.ttx @@ -0,0 +1,299 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TestOPBD + + + Regular + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/subset/data/TestOPBD-1.ttx b/Tests/subset/data/TestOPBD-1.ttx new file mode 100644 index 000000000..0552181e3 --- /dev/null +++ b/Tests/subset/data/TestOPBD-1.ttx @@ -0,0 +1,299 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TestOPBD + + + Regular + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/subset/data/expect_opbd_0.ttx b/Tests/subset/data/expect_opbd_0.ttx new file mode 100644 index 000000000..55842a01c --- /dev/null +++ b/Tests/subset/data/expect_opbd_0.ttx @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/Tests/subset/data/expect_opbd_1.ttx b/Tests/subset/data/expect_opbd_1.ttx new file mode 100644 index 000000000..080abd914 --- /dev/null +++ b/Tests/subset/data/expect_opbd_1.ttx @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/Tests/subset/subset_test.py b/Tests/subset/subset_test.py index 0faba5047..c7818af54 100644 --- a/Tests/subset/subset_test.py +++ b/Tests/subset/subset_test.py @@ -123,6 +123,31 @@ class SubsetTest(unittest.TestCase): subsetfont = TTFont(subsetpath) self.expect_ttx(subsetfont, self.getpath("expect_keep_math.ttx"), ["GlyphOrder", "CFF ", "MATH", "hmtx"]) + def test_subset_opbd_remove(self): + # In the test font, only the glyphs 'A' and 'zero' have an entry in + # the Optical Bounds table. When subsetting, we do not request any + # of those glyphs. Therefore, the produced subsetted font should + # not contain an 'opbd' table. + _, fontpath = self.compile_font(self.getpath("TestOPBD-0.ttx"), ".ttf") + subsetpath = self.temp_path(".ttf") + subset.main([fontpath, "--glyphs=one", "--output-file=%s" % subsetpath]) + subsetfont = TTFont(subsetpath) + self.assertNotIn("opbd", subsetfont) + + def test_subset_opbd_format_0(self): + _, fontpath = self.compile_font(self.getpath("TestOPBD-0.ttx"), ".ttf") + subsetpath = self.temp_path(".ttf") + subset.main([fontpath, "--glyphs=A", "--output-file=%s" % subsetpath]) + subsetfont = TTFont(subsetpath) + self.expect_ttx(subsetfont, self.getpath("expect_opbd_0.ttx"), ["opbd"]) + + def test_subset_opbd_format_1(self): + _, fontpath = self.compile_font(self.getpath("TestOPBD-1.ttx"), ".ttf") + subsetpath = self.temp_path(".ttf") + subset.main([fontpath, "--glyphs=A", "--output-file=%s" % subsetpath]) + subsetfont = TTFont(subsetpath) + self.expect_ttx(subsetfont, self.getpath("expect_opbd_1.ttx"), ["opbd"]) + def test_subset_prop_remove_default_zero(self): # If all glyphs have an AAT glyph property with value 0, # the "prop" table should be removed from the subsetted font.