Skip to content

Instantly share code, notes, and snippets.

@anthrotype
Created July 26, 2018 11:23
Show Gist options
  • Select an option

  • Save anthrotype/2a1f6984c9cc873f8a84c7a664653d54 to your computer and use it in GitHub Desktop.

Select an option

Save anthrotype/2a1f6984c9cc873f8a84c7a664653d54 to your computer and use it in GitHub Desktop.
Sort glyphs in UFO by increasing Unicode codepoint
"""Sort glyphs in UFO by increasing Unicode codepoint to try shrinking the
size of the cmap table.
"""
import sys
from defcon import Font
try:
font = Font(sys.argv[1])
except IndexError:
sys.exit("usage: optimize_glyph_order.py FONT.ufo")
# sort glyphs by increasing unicode codepoint; put unencoded glyphs last
new_order = sorted(
font.glyphOrder,
key=lambda gn: font.unicodeData.unicodeForGlyphName(gn) or sys.maxsize,
)
# ensure .notdef is first glyph, if present
try:
new_order.insert(0, new_order.pop(new_order.index(".notdef")))
except ValueError:
pass
font.glyphOrder = new_order
font.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment