-
Notifications
You must be signed in to change notification settings - Fork 9
/
font_png_to_h.py
108 lines (84 loc) · 2.48 KB
/
font_png_to_h.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
# read an ascii ppm generated by gimp
#
import sys
import png
if len(sys.argv) < 2:
print "usage: " + sys.argv[0] + " file.png\n\n"
sys.exit(1)
fn = sys.argv[1]
fh = open(fn, "r")
pngr = png.Reader(fh)
(png_w, png_h, png_data, png_options) = pngr.read()
print png_options
raw_data = list(png_data)
font = [[], []]
for row in (raw_data):
for x in range(0, len(row), 4):
red = row[x]
if (red == 0):
# black
font[1].append(1)
font[0].append(0)
elif (red == 255):
# white
font[1].append(0)
font[0].append(1)
else:
# transparent
font[1].append(0)
font[0].append(0)
print ("got %d bytes of fotn data\n" % (len(font[0])))
#debug output image
max_char = 90
output_w = max_char * 16
output_h = png_h
pngh = open('font.png', 'wb')
pngw = png.Writer(output_w, output_h, greyscale=True)
# debug output png
# font data is one char after the other (12px x 18px)
pngdata = []
for y in range(output_h):
row = []
for x in range(max_char):
for i in range(16):
idx = png_w * y + x * 16 + i
if (font[1][idx] == 1):
color = 0
elif (font[0][idx] == 1):
color = 255
else:
color = 128
row.append(color)
pngdata.append(row)
#print(pngdata)
pngw.write(pngh, pngdata)
pngh.close()
# dump to header
fheader = open('src/font.h', 'w')
fheader.write("#ifndef __FONT_H__\n")
fheader.write("#define __FONT_H__\n")
fheader.write("\n")
fheader.write("// font data\n")
fheader.write("static const uint8_t font_data[2][18][256*2] = {\n")
# blow up font to 16 bits per pixel, add 2 px in front and back
count = 0
for col in range(2):
fheader.write("\n { // color %d" % (col))
for row in range(18):
fheader.write("\n { //row %d\n " % (row))
for char in range(256):
for b in range(2):
byte = 0
for i in range(8):
index = 256*8*2 * row + char * 2 * 8 + b*8 + i
bit = font[col][index]
byte = (byte << 1) | bit
fheader.write(hex(byte) + ", ")
fheader.write("\n ")
count = count + 1
fheader.write(" },")
fheader.write(" },")
fheader.write("};\n")
fheader.write("#endif // __FONT_H__\n")
print ("wrote %d bytes font data" % ( count))