From e0de5a8413104067f59da651a86b04384dba8f8c Mon Sep 17 00:00:00 2001 From: Nikolaus Waxweiler Date: Tue, 12 Jan 2021 15:26:57 +0000 Subject: [PATCH] Add MissingComponentError for pens --- Lib/fontTools/pens/basePen.py | 12 +++++++++--- Lib/fontTools/pens/hashPointPen.py | 6 +++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Lib/fontTools/pens/basePen.py b/Lib/fontTools/pens/basePen.py index 1593024f3..c8c4c5511 100644 --- a/Lib/fontTools/pens/basePen.py +++ b/Lib/fontTools/pens/basePen.py @@ -147,6 +147,10 @@ class LoggingPen(LogMixin, AbstractPen): pass +class MissingComponentError(KeyError): + """Indicates a component pointing to a non-existent glyph in the glyphset.""" + + class DecomposingPen(LoggingPen): """ Implements a 'addComponent' method that decomposes components @@ -155,10 +159,12 @@ class DecomposingPen(LoggingPen): You must override moveTo, lineTo, curveTo and qCurveTo. You may additionally override closePath, endPath and addComponent. + + By default a warning message is logged when a base glyph is missing; + set the class variable ``skipMissingComponents`` to False if you want + to raise a :class:`MissingComponentError` exception. """ - # By default a warning message is logged when a base glyph is missing; - # set this to False if you want to raise a 'KeyError' exception skipMissingComponents = True def __init__(self, glyphSet): @@ -176,7 +182,7 @@ class DecomposingPen(LoggingPen): glyph = self.glyphSet[glyphName] except KeyError: if not self.skipMissingComponents: - raise + raise MissingComponentError(glyphName) self.log.warning( "glyph '%s' is missing from glyphSet; skipped" % glyphName) else: diff --git a/Lib/fontTools/pens/hashPointPen.py b/Lib/fontTools/pens/hashPointPen.py index f3276f701..9aef5d870 100644 --- a/Lib/fontTools/pens/hashPointPen.py +++ b/Lib/fontTools/pens/hashPointPen.py @@ -1,6 +1,7 @@ # Modified from https://github.com/adobe-type-tools/psautohint/blob/08b346865710ed3c172f1eb581d6ef243b203f99/python/psautohint/ufoFont.py#L800-L838 import hashlib +from fontTools.pens.basePen import MissingComponentError from fontTools.pens.pointPen import AbstractPointPen @@ -69,5 +70,8 @@ class HashPointPen(AbstractPointPen): ): tr = "".join([f"{t:+}" for t in transformation]) self.data.append("[") - self.glyphset[baseGlyphName].drawPoints(self) + try: + self.glyphset[baseGlyphName].drawPoints(self) + except KeyError: + raise MissingComponentError(baseGlyphName) self.data.append(f"({tr})]")