Skip to content

Commit

Permalink
reorder_glyphs: check the length and set of glyphs of new_glyph_order…
Browse files Browse the repository at this point in the history
… matches the font's
  • Loading branch information
anthrotype committed Sep 1, 2022
1 parent 0e4c713 commit f935762
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/nanoemoji/reorder_glyphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ def _access_path(path: SubTablePath):


def reorder_glyphs(font: ttLib.TTFont, new_glyph_order: List[str]):
old_glyph_order = font.getGlyphOrder()
if len(new_glyph_order) != len(old_glyph_order):
raise ValueError(
f"New glyph order contains {len(new_glyph_order)} glyphs, "
f"but font has {len(old_glyph_order)} glyphs"
)

if set(old_glyph_order) != set(new_glyph_order):
raise ValueError(
"New glyph order does not contain the same set of glyphs as the font:\n"
f"* only in new: {set(new_glyph_order) - set(old_glyph_order)}\n"
f"* only in old: {set(old_glyph_order) - set(new_glyph_order)}"
)

# 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
Expand Down
16 changes: 16 additions & 0 deletions tests/reorder_glyphs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,19 @@ def _pair_pos(font):

# Confirm swap applied to Coverage and pair pos parallel array
assert _pair_pos(font) == (("b", [("c", -16), ("b", -14)]), ("a", [("b", -12)]))


def test_invalid_new_glyph_order():
font = ttLib.TTFont()
font.setGlyphOrder([".notdef", "a", "b", "c"])

with pytest.raises(
ValueError, match="New glyph order contains 3 glyphs, but font has 4 glyphs"
):
reorder_glyphs(font, new_glyph_order=[".notdef", "a", "b"])

with pytest.raises(
ValueError,
match="New glyph order does not contain the same set of glyphs as the font:",
):
reorder_glyphs(font, new_glyph_order=[".notdef", "a", "b", "d"])

0 comments on commit f935762

Please sign in to comment.