ttGlyphPen: gracefully handle missing components while decomposing

This commit is contained in:
Cosimo Lupo 2018-06-11 18:40:11 +01:00
parent bf87025826
commit ae69133924
No known key found for this signature in database
GPG Key ID: 59D54DB0C9976482
2 changed files with 15 additions and 5 deletions

View File

@ -1,7 +1,7 @@
from __future__ import print_function, division, absolute_import from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from array import array from array import array
from fontTools.pens.basePen import AbstractPen from fontTools.pens.basePen import LoggingPen
from fontTools.pens.transformPen import TransformPen from fontTools.pens.transformPen import TransformPen
from fontTools.ttLib.tables import ttProgram from fontTools.ttLib.tables import ttProgram
from fontTools.ttLib.tables._g_l_y_f import Glyph from fontTools.ttLib.tables._g_l_y_f import Glyph
@ -17,7 +17,7 @@ __all__ = ["TTGlyphPen"]
MAX_F2DOT14 = 0x7FFF / (1 << 14) MAX_F2DOT14 = 0x7FFF / (1 << 14)
class TTGlyphPen(AbstractPen): class TTGlyphPen(LoggingPen):
"""Pen used for drawing to a TrueType glyph. """Pen used for drawing to a TrueType glyph.
If `handleOverflowingTransforms` is True, the components' transform values If `handleOverflowingTransforms` is True, the components' transform values
@ -115,9 +115,18 @@ class TTGlyphPen(AbstractPen):
if (self.points or if (self.points or
(self.handleOverflowingTransforms and overflowing)): (self.handleOverflowingTransforms and overflowing)):
# can't have both coordinates and components, so decompose # can't have both coordinates and components, so decompose
tpen = TransformPen(self, transformation) try:
self.glyphSet[glyphName].draw(tpen) baseGlyph = self.glyphSet[glyphName]
continue except KeyError:
self.log.debug(
"can't decompose non-existing component '%s'; skipped",
glyphName
)
continue
else:
tpen = TransformPen(self, transformation)
baseGlyph.draw(tpen)
continue
component = GlyphComponent() component = GlyphComponent()
component.glyphName = glyphName component.glyphName = glyphName

View File

@ -94,6 +94,7 @@ class TTGlyphPenTest(TestCase):
pen.lineTo((1, 0)) pen.lineTo((1, 0))
pen.closePath() pen.closePath()
pen.addComponent(componentName, (1, 0, 0, 1, 2, 0)) pen.addComponent(componentName, (1, 0, 0, 1, 2, 0))
pen.addComponent("missing", (1, 0, 0, 1, 0, 0)) # skipped
compositeGlyph = pen.glyph() compositeGlyph = pen.glyph()
pen.moveTo((0, 0)) pen.moveTo((0, 0))