Skip to content

Commit

Permalink
Merge pull request #450 from moyogo/flatten-components
Browse files Browse the repository at this point in the history
Flatten components: handle transformed (scaled) components
  • Loading branch information
moyogo authored Jan 19, 2021
2 parents 304431e + 027ead9 commit fefa663
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Lib/ufo2ft/filters/flattenComponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def _flattenComponent(glyphSet, component):
all_flattened_components = []
for nested in glyph.components:
flattened_components = _flattenComponent(glyphSet, nested)
for i, (_, tr) in enumerate(flattened_components):
tr = tr.transform(component.transformation)
flattened_components[i] = (flattened_components[i][0], tr)
for i, (name, tr) in enumerate(flattened_components):
flat_tr = Transform(*component.transformation)
flat_tr = flat_tr.translate(tr.dx, tr.dy)
flat_tr = flat_tr.transform((tr.xx, tr.xy, tr.yx, tr.yy, 0, 0))
flattened_components[i] = (name, flat_tr)
all_flattened_components.extend(flattened_components)
return all_flattened_components
64 changes: 63 additions & 1 deletion tests/filters/flattenComponents_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@
),
],
},
{
"name": "scaledComponentGlyph",
"width": 600,
"outline": [
(
"addComponent",
("contourGlyph", (0.5, 0, 0, 0.5, 50, 50)),
),
],
},
{
"name": "nestedScaledComponentGlyph",
"width": 600,
"outline": [
(
"addComponent",
("scaledComponentGlyph", (1, 0, 0, 1, 40, 40)),
),
],
},
{
"name": "scaledNestedComponentGlyph",
"width": 600,
"outline": [
(
"addComponent",
("scaledComponentGlyph", (1.2, 0, 0, 1.2, 40, 40)),
),
],
},
]
}
]
Expand Down Expand Up @@ -127,13 +157,37 @@ def test_nested_contour_and_component_glyph(self, font):
for c in font["nestedNestedContourAndComponentGlyph"].components
] == [("contourAndComponentGlyph", (1, 0, 0, 1, 95, 0))]

def test_scaled_component_glyph(self, font):
philter = FlattenComponentsFilter(
include={
"scaledComponentGlyph",
"nestedScaledComponentGlyph",
"scaledNestedComponentGlyph",
}
)
modified = philter(font)
assert modified == {
"nestedScaledComponentGlyph",
"scaledNestedComponentGlyph",
}
assert [
(c.baseGlyph, c.transformation)
for c in font["nestedScaledComponentGlyph"].components
] == [("contourGlyph", (0.5, 0, 0, 0.5, 90, 90))]
assert [
(c.baseGlyph, c.transformation)
for c in font["scaledNestedComponentGlyph"].components
] == [("contourGlyph", (0.6, 0, 0, 0.6, 100, 100))]

def test_whole_font(self, font):
philter = FlattenComponentsFilter()
modified = philter(font)
assert modified == {
"nestedComponentGlyph",
"componentAndNestedComponentsGlyph",
"nestedNestedContourAndComponentGlyph",
"nestedScaledComponentGlyph",
"scaledNestedComponentGlyph",
}
assert [
(c.baseGlyph, c.transformation)
Expand All @@ -157,9 +211,17 @@ def test_whole_font(self, font):
(c.baseGlyph, c.transformation)
for c in font["nestedNestedContourAndComponentGlyph"].components
] == [("contourAndComponentGlyph", (1, 0, 0, 1, 95, 0))]
assert [
(c.baseGlyph, c.transformation)
for c in font["nestedScaledComponentGlyph"].components
] == [("contourGlyph", (0.5, 0, 0, 0.5, 90, 90))]
assert [
(c.baseGlyph, c.transformation)
for c in font["scaledNestedComponentGlyph"].components
] == [("contourGlyph", (0.6, 0, 0, 0.6, 100, 100))]

def test_logger(self, font):
with CapturingLogHandler(logger, level="INFO") as captor:
philter = FlattenComponentsFilter()
_ = philter(font)
captor.assertRegex("Flattened composite glyphs: 3")
captor.assertRegex("Flattened composite glyphs: 5")

0 comments on commit fefa663

Please sign in to comment.