summaryrefslogtreecommitdiff
path: root/misc/glyphs-scripts/assign-fallback-codepoints.py
diff options
context:
space:
mode:
authorRasmus Andersson <rasmus@figma.com>2019-05-27 02:39:24 +0300
committerRasmus Andersson <rasmus@figma.com>2019-05-27 02:39:24 +0300
commite3af7653ac4a94a13dea672ba412eabd4cb1be93 (patch)
treeeaa2d99d4cbfdd473a3e6301c9f7cd285872a107 /misc/glyphs-scripts/assign-fallback-codepoints.py
parenta7d65d5d5ff8f76f6ea2a1468a134ff62e283988 (diff)
downloadinter-e3af7653ac4a94a13dea672ba412eabd4cb1be93.tar.xz
tooling: Adds glyphs script which assigns private-use Unicode codepoint to glyphs which are not mapped to a codepoint
Diffstat (limited to 'misc/glyphs-scripts/assign-fallback-codepoints.py')
-rw-r--r--misc/glyphs-scripts/assign-fallback-codepoints.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/misc/glyphs-scripts/assign-fallback-codepoints.py b/misc/glyphs-scripts/assign-fallback-codepoints.py
new file mode 100644
index 000000000..ba4c16dbd
--- /dev/null
+++ b/misc/glyphs-scripts/assign-fallback-codepoints.py
@@ -0,0 +1,63 @@
+#
+# Assigns private-use codepoints to glyphs which are not mapped
+# to any Unicode codepoints.
+#
+# This script will ignore glyphs which name starts with "." as well as
+# empty glyphs and glyphs which are not exported.
+#
+import sys
+from collections import OrderedDict
+
+DRY_RUN = True
+
+font = Glyphs.font
+font.disableUpdateInterface()
+
+def isEmpty(g):
+ for master in g.parent.masters:
+ layer = g.layers[master.id]
+ if layer.bounds is not None and layer.bounds.size.width > 0:
+ return False
+ return True
+
+try:
+ # find next unallocated private-use codepoint
+ nextcp = 0xE000
+ for g in font.glyphs:
+ if g.unicodes is not None:
+ for c in [int(c, 16) for c in g.unicodes]:
+ if c <= 0xEFFF and c > nextcp:
+ nextcp = c + 1
+
+ print('nextcp: %X' % nextcp)
+ if DRY_RUN:
+ print('DRY_RUN mode (no actual changes will be made)')
+ print('————————————————')
+
+ # for printing
+ mappings = OrderedDict()
+ longest_gname = 0
+
+ # assign private-use codepoints to glyphs that have no existing unicode mapping
+ for g in font.glyphs:
+ # only care for glyphs which are being exported (also ignore "special" glyphs)
+ if g.export and g.name[0] != '.' and (g.unicodes is None or len(g.unicodes) == 0):
+ # error on empty glyphs -- there should be no unmapped empty glyphs
+ if isEmpty(g):
+ sys.stderr.write('ERR: glyph %r is empty but has no unicode mapping (skipping)\n' % g.name)
+ continue
+ unicodes = [hex(nextcp).upper()[2:]]
+ mappings[g.name] = unicodes[0]
+ if len(g.name) > longest_gname:
+ longest_gname = len(g.name)
+ if not DRY_RUN:
+ g.unicodes = unicodes
+ nextcp += 1
+
+ # print mappings made
+ for gname, cp in mappings.iteritems():
+ print('%s %s' % (gname.ljust(longest_gname), cp))
+
+finally:
+ font.enableUpdateInterface()
+