Add MissingComponentError for pens

This commit is contained in:
Nikolaus Waxweiler 2021-01-12 15:26:57 +00:00
parent d0ea03c23b
commit e0de5a8413
2 changed files with 14 additions and 4 deletions

View File

@ -147,6 +147,10 @@ class LoggingPen(LogMixin, AbstractPen):
pass pass
class MissingComponentError(KeyError):
"""Indicates a component pointing to a non-existent glyph in the glyphset."""
class DecomposingPen(LoggingPen): class DecomposingPen(LoggingPen):
""" Implements a 'addComponent' method that decomposes components """ Implements a 'addComponent' method that decomposes components
@ -155,10 +159,12 @@ class DecomposingPen(LoggingPen):
You must override moveTo, lineTo, curveTo and qCurveTo. You may You must override moveTo, lineTo, curveTo and qCurveTo. You may
additionally override closePath, endPath and addComponent. 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 skipMissingComponents = True
def __init__(self, glyphSet): def __init__(self, glyphSet):
@ -176,7 +182,7 @@ class DecomposingPen(LoggingPen):
glyph = self.glyphSet[glyphName] glyph = self.glyphSet[glyphName]
except KeyError: except KeyError:
if not self.skipMissingComponents: if not self.skipMissingComponents:
raise raise MissingComponentError(glyphName)
self.log.warning( self.log.warning(
"glyph '%s' is missing from glyphSet; skipped" % glyphName) "glyph '%s' is missing from glyphSet; skipped" % glyphName)
else: else:

View File

@ -1,6 +1,7 @@
# Modified from https://github.com/adobe-type-tools/psautohint/blob/08b346865710ed3c172f1eb581d6ef243b203f99/python/psautohint/ufoFont.py#L800-L838 # Modified from https://github.com/adobe-type-tools/psautohint/blob/08b346865710ed3c172f1eb581d6ef243b203f99/python/psautohint/ufoFont.py#L800-L838
import hashlib import hashlib
from fontTools.pens.basePen import MissingComponentError
from fontTools.pens.pointPen import AbstractPointPen from fontTools.pens.pointPen import AbstractPointPen
@ -69,5 +70,8 @@ class HashPointPen(AbstractPointPen):
): ):
tr = "".join([f"{t:+}" for t in transformation]) tr = "".join([f"{t:+}" for t in transformation])
self.data.append("[") self.data.append("[")
self.glyphset[baseGlyphName].drawPoints(self) try:
self.glyphset[baseGlyphName].drawPoints(self)
except KeyError:
raise MissingComponentError(baseGlyphName)
self.data.append(f"({tr})]") self.data.append(f"({tr})]")