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

[outlineCompiler] Make space the 2nd glyph unless its order is explicitly set #881

Merged
merged 1 commit into from
Oct 16, 2024
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
7 changes: 6 additions & 1 deletion Lib/ufo2ft/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ 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).
Comment on lines +35 to +36
Copy link
Member

@anthrotype anthrotype Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now thinking that this doesn't check that the glyph named 'space' is actually empty before putting it at index 1, and the whole point of doing this (which isn't even mentioned here, I suppose it should) was to make sure COLRv0 fonts work in old Windows. Maybe it should just move the first empty glyph it finds to second place, whatever its name. I think that's what gftools-fix does.

"""
if glyphOrder is None:
glyphOrder = getattr(font, "glyphOrder", ())
reorderSpace = "space" not in glyphOrder
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
63 changes: 62 additions & 1 deletion 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 Expand Up @@ -915,7 +976,7 @@ def test_warn_name_exceeds_max_length(self, testufo, caplog):
assert long_name in result.getGlyphOrder()

def test_duplicate_glyph_names(self, testufo):
order = ["ab", "ab.1", "a-b", "a/b", "ba"]
order = ["ab", "ab.1", "a-b", "a/b", "ba", "space"]
testufo.lib["public.glyphOrder"] = order
testufo.lib["public.postscriptNames"] = {"ba": "ab"}
for name in order:
Expand Down