Skip to content

Commit

Permalink
[FlattenComponents] Don't throw weird errors when components are miss…
Browse files Browse the repository at this point in the history
…ing (#669)

* Don't throw weird errors when components are missing

* Raise error instead of warning

* Format string nit
  • Loading branch information
simoncozens authored Oct 21, 2022
1 parent f8f6bac commit 71ef6ca
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Lib/ufo2ft/filters/flattenComponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def filter(self, glyph):
return flattened
pen = glyph.getPen()
for comp in list(glyph.components):
flattened_tuples = _flattenComponent(self.context.glyphSet, comp)
flattened_tuples = _flattenComponent(
self.context.glyphSet, comp, found_in=glyph
)
if flattened_tuples[0] != (comp.baseGlyph, comp.transformation):
flattened = True
glyph.removeComponent(comp)
Expand All @@ -32,8 +34,12 @@ def filter(self, glyph):
return flattened


def _flattenComponent(glyphSet, component):
def _flattenComponent(glyphSet, component, found_in):
"""Returns a list of tuples (baseGlyph, transform) of nested component."""
if component.baseGlyph not in glyphSet:
raise ValueError(
f"Could not find component '{component.baseGlyph}' used in '{found_in.name}'"
)

glyph = glyphSet[component.baseGlyph]
# Any contour will cause components to be decomposed
Expand All @@ -43,7 +49,7 @@ def _flattenComponent(glyphSet, component):

all_flattened_components = []
for nested in glyph.components:
flattened_components = _flattenComponent(glyphSet, nested)
flattened_components = _flattenComponent(glyphSet, nested, found_in=glyph)
for i, (name, tr) in enumerate(flattened_components):
flat_tr = Transform(*component.transformation)
flat_tr = flat_tr.translate(tr.dx, tr.dy)
Expand Down

0 comments on commit 71ef6ca

Please sign in to comment.