-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunihex2dat.py
76 lines (66 loc) · 1.84 KB
/
unihex2dat.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
import sys, binascii, zlib, struct
info = []
udata = ""
for line in open(sys.argv[1]).xreadlines():
line = line.strip()
ucs, hex = line.split(':')
ucs = int(ucs, 16)
hex = binascii.a2b_hex(hex)
w = len(hex) / 16
ofs = len(udata)
info.append((ucs, w, ofs))
udata = udata + hex
table = []
low = -100
high = -100
rw = 0
rofs = 0
for ucs, w, ofs in info:
if ucs != high + 1 or w != rw:
if low >= 0:
table.append((low, high, rw, rofs))
low = ucs
high = ucs
rw = w
rofs = ofs
else:
high = ucs
table.append((low, high, rw, rofs))
cdata = zlib.compress(udata)
print len(info), "characters"
print len(table), "ranges"
print len(udata), "bytes of uncompressed glyph data"
print len(cdata), "bytes of compressed glyph data"
f = open("unifont.dat", "wb")
f.write(struct.pack(">III", len(table), len(cdata), len(udata)))
for low, high, w, ofs in table:
print '\t%x %x (%d) %d %d' % (low, high, high - low + 1, w, ofs)
f.write(struct.pack(">HHBI", low, high, w, ofs))
f.write(cdata)
f.close()
f = open("unifont.h", "w")
print >>f, "#define unifont_count %d" % len(table)
print >>f, "static const unsigned short unifont_low[%d] = {" % len(table)
for low, high, w, ofs in table: print >>f, "%d," % low
print >>f, "}"
print >>f, "static const unsigned short unifont_high[%d] = {" % len(table)
for low, high, w, ofs in table: print >>f, "%d," % high
print >>f, "}"
print >>f, "static const unsigned char unifont_width[%d] = {" % len(table)
for low, high, w, ofs in table: print >>f, "%d," % w
print >>f, "}"
print >>f, "static const unsigned int unifont_offset[%d] = {" % len(table)
for low, high, w, ofs in table: print >>f, "%d," % ofs
print >>f, "}"
print >>f, "static const unsigned char unifont_data[%d] = {" % len(udata)
i = 0
s = "\t"
for x in udata:
if i == 7:
print >>f, s
s = "\t"
i = 0
else:
s = s + ("0x%02x," % ord(x))
i = i + 1
print >>f, "}"