Skip to content

Commit

Permalink
[builder] Reorder .notdef and space glyphs in public.glyphOrder
Browse files Browse the repository at this point in the history
Unless "Keep GlyphOrder" (or the older "TrueType Keep GlyphOrder")
custom parameter is set to True.
  • Loading branch information
khaledhosny committed Oct 21, 2024
1 parent c815cd8 commit a2c681d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Lib/glyphsLib/builder/custom_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,26 @@ def to_ufo(self, builder, glyphs, ufo):
]
ufo.set_lib_value(GLYPH_ORDER_KEY, glyphs_glyphOrder)

# if "Keep GlyphOrder" is not set, we reorder ".notdef"
# and "space" to the beginning of the glyph order.
#
# There is an older "TrueType Keep GlyphOrder" that is specific to
# TrueType export, but we don't know what the export format is so we
# treat it the same as "Keep GlyphOrder".
keep_glyphOrder = glyphs.get_custom_value(
"Keep GlyphOrder"
) or glyphs.get_custom_value("TrueType Keep GlyphOrder")
ufo_glyphOrder = ufo.get_lib_value(GLYPH_ORDER_KEY)
if not keep_glyphOrder and ufo_glyphOrder:
space_index = 0
if ".notdef" in ufo_glyphOrder:
ufo_glyphOrder.remove(".notdef")
ufo_glyphOrder.insert(0, ".notdef")
space_index = 1
if "space" in ufo_glyphOrder:
ufo_glyphOrder.remove("space")
ufo_glyphOrder.insert(space_index, "space")


register(GlyphOrderParamHandler())

Expand Down
61 changes: 61 additions & 0 deletions tests/builder/builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,3 +2758,64 @@ def test_glyphs_to_ufo_with_partial_glyphOrder(self, ufo_module):
ufo = self.from_glyphs(ufo_module)
assert ["xxx1", "f", "xxx2", "c", "a"] == ufo.lib["public.glyphOrder"]
assert GLYPHS_PREFIX + "glyphOrder" not in ufo.lib

def test_glyphs_to_ufo_with_keepGlyphOrder(self, ufo_module):
self.prepare(ufo_module)
self.font.glyphs.append(GSGlyph("space"))

# If "Keep GlyphOrder" is True, no reordering happens
self.font.customParameters["Keep GlyphOrder"] = True
ufo = self.from_glyphs(ufo_module)
assert ["c", "a", "f", "space"] == ufo.lib["public.glyphOrder"]

# If "Keep GlyphOrder" is False, space is reordered
self.font.customParameters["Keep GlyphOrder"] = False
ufo = self.from_glyphs(ufo_module)
assert ["space", "c", "a", "f"] == ufo.lib["public.glyphOrder"]

# Absence of "Keep GlyphOrder" is equivalent to False
del self.font.customParameters["Keep GlyphOrder"]
ufo = self.from_glyphs(ufo_module)
assert ["space", "c", "a", "f"] == ufo.lib["public.glyphOrder"]

def test_glyphs_to_ufo_with_keepGlyphOrder_and_notdef(self, ufo_module):
self.prepare(ufo_module)
self.font.glyphs.append(GSGlyph("space"))
self.font.glyphs.append(GSGlyph(".notdef"))

# If "Keep GlyphOrder" is True, no reordering happens
self.font.customParameters["Keep GlyphOrder"] = True
ufo = self.from_glyphs(ufo_module)
assert ["c", "a", "f", "space", ".notdef"] == ufo.lib["public.glyphOrder"]

# If "Keep GlyphOrder" is False, space is reordered
self.font.customParameters["Keep GlyphOrder"] = False
ufo = self.from_glyphs(ufo_module)
assert [".notdef", "space", "c", "a", "f"] == ufo.lib["public.glyphOrder"]

# Absence of "Keep GlyphOrder" is equivalent to False
del self.font.customParameters["Keep GlyphOrder"]
ufo = self.from_glyphs(ufo_module)
assert [".notdef", "space", "c", "a", "f"] == ufo.lib["public.glyphOrder"]

def test_glyphs_to_ufo_with_keepGlyphOrder_and_explicit_glyphOrder(
self, ufo_module
):
self.prepare(ufo_module)
self.font.glyphs.append(GSGlyph("space"))
self.font.customParameters["glyphOrder"] = ["c", "a", "space", "f"]

# If "Keep GlyphOrder" is True, no reordering happens
self.font.customParameters["Keep GlyphOrder"] = True
ufo = self.from_glyphs(ufo_module)
assert ["c", "a", "space", "f"] == ufo.lib["public.glyphOrder"]

# If "Keep GlyphOrder" is False, space is reordered
self.font.customParameters["Keep GlyphOrder"] = False
ufo = self.from_glyphs(ufo_module)
assert ["space", "c", "a", "f"] == ufo.lib["public.glyphOrder"]

# Absence of "Keep GlyphOrder" is equivalent to False
del self.font.customParameters["Keep GlyphOrder"]
ufo = self.from_glyphs(ufo_module)
assert ["space", "c", "a", "f"] == ufo.lib["public.glyphOrder"]

0 comments on commit a2c681d

Please sign in to comment.