Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instantiator: when applying DS rules, must also swap anchors #632

Merged
merged 1 commit into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Lib/fontmake/instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,25 +582,32 @@ def swap_glyph_names(font: ufoLib2.Font, name_old: str, name_new: str):
glyph_old.drawPoints(p)
glyph_swap.width = glyph_old.width

glyph_old.clear()
glyph_old.clearContours()
glyph_old.clearComponents()
p = glyph_old.getPointPen()
glyph_new.drawPoints(p)
glyph_old.width = glyph_new.width

glyph_new.clear()
glyph_new.clearContours()
glyph_new.clearComponents()
p = glyph_new.getPointPen()
glyph_swap.drawPoints(p)
glyph_new.width = glyph_swap.width

# 2. Remap components.
# 2. Swap anchors.
glyph_swap.anchors = glyph_old.anchors
glyph_old.anchors = glyph_new.anchors
glyph_new.anchors = glyph_swap.anchors

# 3. Remap components.
for g in font:
for c in g.components:
if c.baseGlyph == name_old:
c.baseGlyph = name_new
elif c.baseGlyph == name_new:
c.baseGlyph = name_old

# 3. Swap literal names in kerning.
# 4. Swap literal names in kerning.
kerning_new = {}
for first, second in font.kerning.keys():
value = font.kerning[(first, second)]
Expand All @@ -615,7 +622,7 @@ def swap_glyph_names(font: ufoLib2.Font, name_old: str, name_new: str):
kerning_new[(first, second)] = value
font.kerning = kerning_new

# 4. Swap names in groups.
# 5. Swap names in groups.
for group_name, group_members in font.groups.items():
group_members_new = []
for name in group_members:
Expand Down
2 changes: 2 additions & 0 deletions tests/data/SwapGlyphNames/A.ufo/glyphs/a.glif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<glyph name="a" format="2">
<advance width="600"/>
<unicode hex="0061"/>
<anchor x="351" y="0" name="bottom"/>
<anchor x="351" y="613" name="top"/>
<outline>
<contour>
<point x="113.0" y="500.0" type="line"/>
Expand Down
2 changes: 2 additions & 0 deletions tests/data/SwapGlyphNames/A.ufo/glyphs/a.swap.glif
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="a.swap" format="2">
<advance width="666.0"/>
<anchor x="153" y="0" name="bottom"/>
<anchor x="153" y="316" name="top"/>
<outline>
<contour>
<point x="127" y="500" type="line"/>
Expand Down
11 changes: 11 additions & 0 deletions tests/test_instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fontTools.designspaceLib as designspaceLib
import pytest
import ufoLib2
from ufoLib2.objects.anchor import Anchor

import fontmake.instantiator

Expand Down Expand Up @@ -140,6 +141,16 @@ def test_swap_glyph_names(data_dir):
]
assert sorted(c.baseGlyph for c in ufo["aaa.swap"].components) == ["a", "a", "y"]

# Test swapped anchors.
assert ufo["a"].anchors == [
Anchor(x=153, y=0, name="bottom"),
Anchor(x=153, y=316, name="top"),
]
assert ufo["a.swap"].anchors == [
Anchor(x=351, y=0, name="bottom"),
Anchor(x=351, y=613, name="top"),
]

# Test swapped glyph kerning.
assert ufo.kerning == {
("public.kern1.a", "x"): 10,
Expand Down