Skip to content

Commit

Permalink
[outlineCompiler] Make space the 2nd glyph unless its order is explic…
Browse files Browse the repository at this point in the history
…itly set

Fixes #880
  • Loading branch information
khaledhosny committed Oct 16, 2024
1 parent feeb474 commit bab3a73
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Lib/ufo2ft/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ def makeOfficialGlyphOrder(font, glyphOrder=None):
If not explicit glyphOrder is defined, sort glyphs alphabetically.
If ".notdef" glyph is present in the font, force this to always be
the first glyph (at index 0).
the first glyph (at index 0). Also, if "space" is present in the font and
missing from glyphOrder, force it to be the second glyph (at index 1).
"""
if glyphOrder is None:
glyphOrder = getattr(font, "glyphOrder", ())
reorderSpace = "space" not in glyphOrder
print(reorderSpace)
names = set(font.keys())
order = []
if ".notdef" in names:
names.remove(".notdef")
order.append(".notdef")
if reorderSpace and "space" in names:
names.remove("space")
order.append("space")
for name in glyphOrder:
if name not in names:
continue
Expand Down
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/_notdef.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name=".notdef" format="2">
<outline>
</outline>
</glyph>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/a.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="a" format="2">
<outline>
</outline>
</glyph>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/b.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="b" format="2">
<outline>
</outline>
</glyph>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/c.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="c" format="2">
<outline>
</outline>
</glyph>
24 changes: 24 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/contents.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>b</key>
<string>b.glif</string>
<key>a</key>
<string>a.glif</string>
<key>c</key>
<string>c.glif</string>
<key>d</key>
<string>d.glif</string>
<key>space</key>
<string>space.glif</string>
<key>.notdef</key>
<string>_notdef.glif</string>
<key>e</key>
<string>e.glif</string>
<key>f</key>
<string>f.glif</string>
<key>g</key>
<string>g.glif</string>
</dict>
</plist>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/d.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="d" format="2">
<outline>
</outline>
</glyph>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/e.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="e" format="2">
<outline>
</outline>
</glyph>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/f.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="f" format="2">
<outline>
</outline>
</glyph>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/g.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="g" format="2">
<outline>
</outline>
</glyph>
5 changes: 5 additions & 0 deletions tests/data/TestSpaceOrder.ufo/glyphs/space.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="space" format="2">
<outline>
</outline>
</glyph>
10 changes: 10 additions & 0 deletions tests/data/TestSpaceOrder.ufo/layercontents.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<array>
<string>public.default</string>
<string>glyphs</string>
</array>
</array>
</plist>
10 changes: 10 additions & 0 deletions tests/data/TestSpaceOrder.ufo/metainfo.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>creator</key>
<string>com.github.fonttools.ufoLib</string>
<key>formatVersion</key>
<integer>3</integer>
</dict>
</plist>
61 changes: 61 additions & 0 deletions tests/outlineCompiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,67 @@ def test_compile_strange_glyph_order(self, quadufo):
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER

def test_compile_reorder_space_glyph(self, quadufo):
"""
Test that ufo2ft always puts .notdef first, and put space second if no
explicit glyph order is set.
"""
EXPECTED_ORDER = [
".notdef",
"space",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
]
# Check with no public.glyphOrder
del quadufo.lib["public.glyphOrder"]
assert not quadufo.glyphOrder
compiler = OutlineTTFCompiler(quadufo)
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER

# Empty glyphOrder is considered the same
quadufo.glyphOrder = []
compiler = OutlineTTFCompiler(quadufo)
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER

# Non-empty glyphOrder without "space" is considered the same
quadufo.glyphOrder = [n for n in EXPECTED_ORDER if n != "space"]
compiler = OutlineTTFCompiler(quadufo)
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER

EXPECTED_ORDER = [
".notdef",
"a",
"b",
"c",
"d",
"space",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
]
quadufo.glyphOrder = EXPECTED_ORDER
compiler = OutlineTTFCompiler(quadufo)
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER


class NamesTest:
@pytest.mark.parametrize(
Expand Down

0 comments on commit bab3a73

Please sign in to comment.