forked from zathras777/zttf
/
dumpscad.py
81 lines (74 loc) · 2.31 KB
/
dumpscad.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
import sys
from zttf.ttfile import TTFile
from struct import unpack
def glyphBox(f,g):
data = f.get_glyph_data(g)
if not len(data):
return (0,0,0,0)
return unpack(">hhhhh", data[:10])[1:]
def describeChar(c):
if c >= 32 and c<127 and c != ord('\\') and c != ord('"'):
return '"'+chr(c)+'"'
else:
return "chr(%d)" % c
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: {} <font filename>".format(sys.argv[0]))
sys.exit(0)
t = TTFile(sys.argv[1])
t = TTFile(sys.argv[1])
if not t.is_valid:
print("Invalid")
sys.exit(0)
f = t.faces[0]
glyphs = {}
for c in range(0, 65536):
try:
g = f.char_to_glyph(c+0xF000 if f.name == "Webdings" and c<256 else c)
if g:
glyphs[c] = g
except:
pass
chars = sorted(glyphs.keys())
"""
maxAscent = 0
for r in (range(ord('A'),ord('Z')+1),range(ord('a'),ord('z')+1)):
for c in r:
if c in glyphs:
data = f.get_glyph_data(glyphs[c])
ascent = unpack(">hhhhh", data[:10])[4]
print(chr(c),ascent)
maxAscent = max(ascent,maxAscent)
"""
ascent = f.tables[b'os2'].sTypoAscender
fontID = re.sub(r"[^0-9A-Za-z]", "_", f.name)
if fontID[0].isdigit():
fontID = "_" + fontID
print("""FONT_%s = [
["%s", // family
%d], // style
%d, // ascender
%d, // descender
%d, // line gap
%f, // units_per_em
[""" % (fontID, f.font_family, f.tables[b'head'].mac_style, ascent,
f.tables[b'os2'].sTypoDescender,
f.tables[b'os2'].typo_line_gap,
f.units_per_em))
for c in chars:
try:
glyph = glyphs[c]
box = glyphBox(f,glyph)
line = " [%s,%d,%d,%d,%d,%d,%d,[" % ( describeChar(c), f.glyph_metrics[glyph][0],
f.glyph_metrics[glyph][1], box[0], box[1], box[2], box[3] )
kerns=[]
for c2 in chars:
r = f.char_to_glyph(c2)
if (glyph,r) in f.glyph_kern:
kerns.append("[%s,%d]" % (describeChar(c2), f.glyph_kern[(glyph,r)]))
line += ','.join(kerns)+"]],"
print(line)
except:
pass
print(" ]\n];\n")