Ensure '.notdef' is first glyph and update CFF table

This commit is contained in:
ftCLI 2024-11-05 16:33:23 +01:00
parent e04dfaab53
commit ea59c39e21

View File

@ -258,6 +258,12 @@ def reorderGlyphs(font: ttLib.TTFont, new_glyph_order: List[str]):
f"* only in old: {set(old_glyph_order) - set(new_glyph_order)}" f"* only in old: {set(old_glyph_order) - set(new_glyph_order)}"
) )
# Glyph 0 must be assigned to a .notdef glyph.
# https://learn.microsoft.com/en-us/typography/opentype/spec/recom#glyph-0-the-notdef-glyph
if ".notdef" in new_glyph_order:
new_glyph_order.remove(".notdef")
new_glyph_order.insert(0, ".notdef")
# Changing the order of glyphs in a TTFont requires that all tables that use # Changing the order of glyphs in a TTFont requires that all tables that use
# glyph indexes have been fully. # glyph indexes have been fully.
# Cf. https://github.com/fonttools/fonttools/issues/2060 # Cf. https://github.com/fonttools/fonttools/issues/2060
@ -276,3 +282,11 @@ def reorderGlyphs(font: ttLib.TTFont, new_glyph_order: List[str]):
reorder_key = (type(value), getattr(value, "Format", None)) reorder_key = (type(value), getattr(value, "Format", None))
for reorder in _REORDER_RULES.get(reorder_key, []): for reorder in _REORDER_RULES.get(reorder_key, []):
reorder.apply(font, value) reorder.apply(font, value)
if "CFF " in font:
cff_table = font["CFF "]
charstrings = cff_table.cff.topDictIndex[0].CharStrings.charStrings
cff_table.cff.topDictIndex[0].charset = new_glyph_order
cff_table.cff.topDictIndex[0].CharStrings.charStrings = {
k: charstrings.get(k) for k in new_glyph_order
}