Skip to content

Commit

Permalink
[outlineCompiler] Respecr keepGlyphOrder lib key when re-ordering space
Browse files Browse the repository at this point in the history
This is mainly so that glyphsLib can potentially use it to map
GlyphsApp’s “Keep GlyphOrder” custom parameter.
  • Loading branch information
khaledhosny committed Oct 12, 2024
1 parent dcd3087 commit a1f60b8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions Lib/ufo2ft/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CFFOptimization(IntEnum):
USE_PRODUCTION_NAMES = UFO2FT_PREFIX + "useProductionNames"
GLYPHS_DONT_USE_PRODUCTION_NAMES = GLYPHS_PREFIX + "Don't use Production Names"
KEEP_GLYPH_NAMES = UFO2FT_PREFIX + "keepGlyphNames"
KEEP_GLYPH_ORDER_KEY = UFO2FT_PREFIX + "keepGlyphOrder"

COLOR_LAYERS_KEY = UFO2FT_PREFIX + "colorLayers"
COLOR_PALETTES_KEY = UFO2FT_PREFIX + "colorPalettes"
Expand Down
2 changes: 1 addition & 1 deletion Lib/ufo2ft/featureWriters/baseFeatureWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def getOrderedGlyphSet(self):
font,
skipExportGlyphs=set(font.lib.get("public.skipExportGlyphs", [])),
)
glyphOrder = makeOfficialGlyphOrder(glyphSet, font.glyphOrder)
glyphOrder = makeOfficialGlyphOrder(glyphSet, font.glyphOrder, font)
return OrderedDict((gn, glyphSet[gn]) for gn in glyphOrder)

def compileGSUB(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/ufo2ft/outlineCompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def makeOfficialGlyphOrder(self, glyphOrder):
may override this method to handle the order creation
in a different way if desired.
"""
return makeOfficialGlyphOrder(self.allGlyphs, glyphOrder)
return makeOfficialGlyphOrder(self.allGlyphs, glyphOrder, self.ufo)

# --------------
# Table Builders
Expand Down
12 changes: 9 additions & 3 deletions Lib/ufo2ft/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
from fontTools.pens.reverseContourPen import ReverseContourPen
from fontTools.pens.transformPen import TransformPen

from ufo2ft.constants import OPENTYPE_CATEGORIES_KEY, UNICODE_SCRIPT_ALIASES
from ufo2ft.constants import (
KEEP_GLYPH_ORDER_KEY,
OPENTYPE_CATEGORIES_KEY,
UNICODE_SCRIPT_ALIASES,
)
from ufo2ft.errors import InvalidDesignSpaceData, InvalidFontData
from ufo2ft.fontInfoData import getAttrWithFallback

logger = logging.getLogger(__name__)


def makeOfficialGlyphOrder(font, glyphOrder=None):
def makeOfficialGlyphOrder(font, glyphOrder=None, ufo=None):
"""Make the final glyph order for 'font'.
If glyphOrder is None, try getting the font.glyphOrder list.
Expand All @@ -34,14 +38,16 @@ def makeOfficialGlyphOrder(font, glyphOrder=None):
If ".notdef" glyph is present in the font, force this to always be
the first glyph (at index 0).
"""
if ufo is None:
ufo = font
if glyphOrder is None:
glyphOrder = getattr(font, "glyphOrder", ())
names = set(font.keys())
order = []
if ".notdef" in names:
names.remove(".notdef")
order.append(".notdef")
if "space" in names:
if "space" in names and not ufo.lib.get(KEEP_GLYPH_ORDER_KEY, False):
names.remove("space")
order.append("space")
for name in glyphOrder:
Expand Down
29 changes: 29 additions & 0 deletions tests/outlineCompiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
GLYPHS_MATH_CONSTANTS_KEY,
GLYPHS_MATH_EXTENDED_SHAPE_KEY,
GLYPHS_MATH_VARIANTS_KEY,
KEEP_GLYPH_ORDER_KEY,
OPENTYPE_POST_UNDERLINE_POSITION_KEY,
SPARSE_OTF_MASTER_TABLES,
SPARSE_TTF_MASTER_TABLES,
Expand Down Expand Up @@ -761,6 +762,34 @@ def test_compile_strange_glyph_order(self, quadufo):
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER

def test_compile_keep_glyph_order(self, quadufo):
"""Move space and .notdef to end of glyph ids
ufo2ft always space second, unless KEEP_GLYPH_ORDER_KEY
is True.
"""
NEW_ORDER = ["b", "a", "c", "d", "space", ".notdef"]
EXPECTED_ORDER = [
".notdef",
"b",
"a",
"c",
"d",
"space",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
]
quadufo.lib["public.glyphOrder"] = NEW_ORDER
quadufo.lib[KEEP_GLYPH_ORDER_KEY] = True
compiler = OutlineTTFCompiler(quadufo)
compiler.compile()
assert compiler.otf.getGlyphOrder() == EXPECTED_ORDER


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

0 comments on commit a1f60b8

Please sign in to comment.