From ea59c39e219f8568667235a93d987eca9c1a0bc1 Mon Sep 17 00:00:00 2001 From: ftCLI Date: Tue, 5 Nov 2024 16:33:23 +0100 Subject: [PATCH 1/4] Ensure '.notdef' is first glyph and update CFF table --- Lib/fontTools/ttLib/reorderGlyphs.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/fontTools/ttLib/reorderGlyphs.py b/Lib/fontTools/ttLib/reorderGlyphs.py index 3221261f1..0e4a99fc1 100644 --- a/Lib/fontTools/ttLib/reorderGlyphs.py +++ b/Lib/fontTools/ttLib/reorderGlyphs.py @@ -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)}" ) + # 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 # glyph indexes have been fully. # 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)) for reorder in _REORDER_RULES.get(reorder_key, []): 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 + } From a238ed2c318e04babee1f14932e080cb925f1544 Mon Sep 17 00:00:00 2001 From: ftCLI Date: Tue, 5 Nov 2024 17:10:39 +0100 Subject: [PATCH 2/4] Remove '.notdef' glyph handling code --- Lib/fontTools/ttLib/reorderGlyphs.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Lib/fontTools/ttLib/reorderGlyphs.py b/Lib/fontTools/ttLib/reorderGlyphs.py index 0e4a99fc1..72a7cce15 100644 --- a/Lib/fontTools/ttLib/reorderGlyphs.py +++ b/Lib/fontTools/ttLib/reorderGlyphs.py @@ -258,12 +258,6 @@ def reorderGlyphs(font: ttLib.TTFont, new_glyph_order: List[str]): 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 # glyph indexes have been fully. # Cf. https://github.com/fonttools/fonttools/issues/2060 From 2906e4043d92d044861d3b982091fdbc94f19cc2 Mon Sep 17 00:00:00 2001 From: ftCLI Date: Tue, 5 Nov 2024 17:39:57 +0100 Subject: [PATCH 3/4] Add test for reorderGlyphs function with CFF fonts --- Tests/ttLib/reorderGlyphs_test.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/ttLib/reorderGlyphs_test.py b/Tests/ttLib/reorderGlyphs_test.py index cfda2d285..02ac2f49d 100644 --- a/Tests/ttLib/reorderGlyphs_test.py +++ b/Tests/ttLib/reorderGlyphs_test.py @@ -59,6 +59,17 @@ def test_ttfont_reorder_glyphs(): assert list(reversed(old_coverage2)) == new_coverage2 +def test_reorder_glyphs_cff(): + font_path = DATA_DIR / "TestVGID-Regular.otf" + font = TTFont(str(font_path)) + ga = font.getGlyphOrder() + ga = list(reversed(ga)) + reorderGlyphs(font, ga) + + assert list(font["CFF "].cff.topDictIndex[0].CharStrings.charStrings.keys()) == ga + assert font["CFF "].cff.topDictIndex[0].charset == ga + + def test_reorder_glyphs_bad_length(caplog): font_path = DATA_DIR / "Test-Regular.ttf" font = TTFont(str(font_path)) From 1cb153b2c52a1ae52fdafed4a08d34a73131302e Mon Sep 17 00:00:00 2001 From: ftCLI Date: Tue, 5 Nov 2024 17:42:26 +0100 Subject: [PATCH 4/4] Remove unused imports from reorderGlyphs.py --- Lib/fontTools/ttLib/reorderGlyphs.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/fontTools/ttLib/reorderGlyphs.py b/Lib/fontTools/ttLib/reorderGlyphs.py index 72a7cce15..fd950ba0e 100644 --- a/Lib/fontTools/ttLib/reorderGlyphs.py +++ b/Lib/fontTools/ttLib/reorderGlyphs.py @@ -19,9 +19,7 @@ from typing import ( Deque, Iterable, List, - NamedTuple, Tuple, - Union, )