[glyf] remove_hinting should del program from composite glyph

Unlike simple glyphs, which always have 'program' attribute (even when empty, with no instructions)
in composite glyphs, it's the presence of 'program' attribute that determines whether
the optional WE_HAVE_INSTRUCTIONS component flag is set or not.
Thus, the trim method (with remove_hinting=True) should delete the attribute for
composite glyphs.

Fixes https://github.com/fonttools/fonttools/issues/1550
This commit is contained in:
Cosimo Lupo 2019-03-21 12:16:54 +00:00
parent d2c462a0fb
commit c6006a7f8c
2 changed files with 42 additions and 2 deletions

View File

@ -917,6 +917,10 @@ class Glyph(object):
expanding it."""
if not hasattr(self, "data"):
if remove_hinting:
if self.isComposite():
if hasattr(self, "program"):
del self.program
else:
self.program = ttProgram.Program()
self.program.fromBytecode([])
# No padding to trim.

View File

@ -4,6 +4,7 @@ from fontTools.misc.fixedTools import otRound
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.ttLib import TTFont, newTable, TTLibError
from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
from fontTools.ttLib.tables import ttProgram
import sys
import array
import pytest
@ -240,6 +241,41 @@ class glyfTableTest(unittest.TestCase):
with self.assertRaisesRegex(TTLibError, "glyph '.' contains a recursive component reference"):
glyph_A.getCoordinates(glyphSet)
def test_trim_remove_hinting_composite_glyph(self):
glyphSet = {"dummy": TTGlyphPen(None).glyph()}
pen = TTGlyphPen(glyphSet)
pen.addComponent("dummy", (1, 0, 0, 1, 0, 0))
composite = pen.glyph()
p = ttProgram.Program()
p.fromAssembly(['SVTCA[0]'])
composite.program = p
glyphSet["composite"] = composite
glyfTable = newTable("glyf")
glyfTable.glyphs = glyphSet
glyfTable.glyphOrder = sorted(glyphSet)
composite.compact(glyfTable)
self.assertTrue(hasattr(composite, "data"))
# remove hinting from the compacted composite glyph, without expanding it
composite.trim(remove_hinting=True)
# check that, after expanding the glyph, we have no instructions
composite.expand(glyfTable)
self.assertFalse(hasattr(composite, "program"))
# now remove hinting from expanded composite glyph
composite.program = p
composite.trim(remove_hinting=True)
# check we have no instructions
self.assertFalse(hasattr(composite, "program"))
composite.compact(glyfTable)
if __name__ == "__main__":
import sys