Merge pull request #2145 from fonttools/pens-MissingComponentError

Add MissingComponentError for pens
This commit is contained in:
Nikolaus Waxweiler 2021-01-12 17:55:22 +00:00 committed by GitHub
commit a9eecc4f64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -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:

View File

@ -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})]")