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 03b3498
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 2 additions & 3 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,13 @@ 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
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
13 changes: 13 additions & 0 deletions tests/outlineCompiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,19 @@ 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", "b", "a", "c", "d", "e", "f", "g"]
ufo = FontClass()
for n in DEAFAULT_ORDER:
ufo.newGlyph(n)
compiler = OutlineTTFCompiler(ufo)
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER


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

0 comments on commit 03b3498

Please sign in to comment.