Skip to content

Commit

Permalink
[outlineCompiler] Make space the 2nd glyph unless public.glyphOrder i…
Browse files Browse the repository at this point in the history
…s set

Fixes #880
  • Loading branch information
khaledhosny committed Oct 15, 2024
1 parent feeb474 commit 903f7d0
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Lib/ufo2ft/outlineCompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
)
from ufo2ft.instructionCompiler import InstructionCompiler
from ufo2ft.util import (
_GlyphSet,
_copyGlyph,
_getNewGlyphFactory,
calcCodePageRanges,
Expand Down Expand Up @@ -122,15 +123,15 @@ def __init__(
self.ufo = font
# use the previously filtered glyphSet, if any
if glyphSet is None:
glyphSet = {g.name: g for g in font}
glyphSet = _GlyphSet.from_layer(font)
# this is set to False by Interpolatable{O,T}TFCompiler when building a VF's
# non-default masters. E.g. it's used by makeMissingRequiredGlyphs method below.
self.compilingVFDefaultSource = compilingVFDefaultSource
self.makeMissingRequiredGlyphs(font, glyphSet, self.sfntVersion, notdefGlyph)
self.allGlyphs = glyphSet
# store the glyph order
if glyphOrder is None:
glyphOrder = font.glyphOrder
glyphOrder = font.lib.get("public.glyphOrder")
self.glyphOrder = self.makeOfficialGlyphOrder(glyphOrder)
# make a reusable character mapping
self.unicodeToGlyphNameMapping = self.makeUnicodeToGlyphNameMapping()
Expand Down
7 changes: 7 additions & 0 deletions Lib/ufo2ft/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ def makeOfficialGlyphOrder(font, glyphOrder=None):
If ".notdef" glyph is present in the font, force this to always be
the first glyph (at index 0).
"""
reorderSpace = False
if glyphOrder is None:
glyphOrder = getattr(font, "glyphOrder", ())
reorderSpace = True
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 Expand Up @@ -95,9 +100,11 @@ def from_layer(cls, font, layerName=None, copy=False, skipExportGlyphs=None):
if copy:
self = _copyLayer(layer, obj_type=cls)
self.lib = deepcopy(layer.lib)
self.glyphOrder = deepcopy(font.glyphOrder)
else:
self = cls((g.name, g) for g in layer)
self.lib = layer.lib
self.glyphOrder = font.glyphOrder
self.name = layer.name if layerName is not None else None

# If any glyphs in the skipExportGlyphs list are used as components, decompose
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>
12 changes: 12 additions & 0 deletions tests/outlineCompiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,18 @@ def test_compile_strange_glyph_order(self, quadufo):
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER

def test_compile_reorder_space_glyph(self, FontClass):
"""Move space and .notdef to end of glyph ids ufo2ft always puts
.notdef first, and put space second of no explicit glyph order is set.
"""
DEAFAULT_ORDER = ["b", "a", "c", "d", "space", ".notdef", "e", "f", "g"]
EXPECTED_ORDER = [".notdef", "space", "a", "b", "c", "d", "e", "f", "g"]
ufo = FontClass(getpath("TestSpaceOrder.ufo"))
#assert list(ufo.keys()) == DEAFAULT_ORDER
compiler = OutlineTTFCompiler(ufo)
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER


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

0 comments on commit 903f7d0

Please sign in to comment.