-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathadd_glyphs.py
executable file
·56 lines (46 loc) · 1.53 KB
/
add_glyphs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
import glob, sys
from fontTools import ttx
from png import PNG
if len (sys.argv) < 4:
print >>sys.stderr, """
Usage:
add_glyphs.py font.ttx out-font.ttx strike-prefix...
This will search for files that have strike-prefix followed
by a hex number, and end in ".png". For example, if strike-prefix
is "icons/uni", then files with names like "icons/uni1f4A9.png" will
be loaded. The script then adds cmap and htmx entries for the Unicode
characters found. The advance width will be chosen based on image
aspect ratio. If Unicode values outside the BMP are desired, the
existing cmap table should be of the appropriate (format 12) type.
Only the first cmap table is modified.
"""
sys.exit (1)
in_file = sys.argv[1]
out_file = sys.argv[2]
img_prefix = sys.argv[3]
del sys.argv
font = ttx.TTFont()
font.importXML (in_file)
img_files = {}
glb = "%s*.png" % img_prefix
print "Looking for images matching '%s'." % glb
for img_file in glob.glob (glb):
u = int (img_file[len (img_prefix):-4], 16)
img_files[u] = img_file
if not img_files:
raise Exception ("No image files found in '%s'." % glb)
ascent = font['hhea'].ascent
descent = -font['hhea'].descent
g = font['GlyphOrder'].glyphOrder
c = font['cmap'].tables[0].cmap
h = font['hmtx'].metrics
for (u, filename) in img_files.items ():
print "Adding glyph for U+%04X" % u
n = "uni%04x" % u
g.append (n)
c[u] = n
(img_width, img_height) = PNG (filename).get_size ()
advance = int (round ((float (ascent+descent) * img_width / img_height)))
h[n] = [advance, 0]
font.saveXML (out_file)