-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkfont.py
177 lines (136 loc) · 4.96 KB
/
mkfont.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from PIL import Image
from bitarray import bitarray
from os import listdir, makedirs
from os.path import realpath, dirname, join, isfile, basename, splitext, exists
here = dirname(realpath(__file__))
input_dir = join(here, "pixeldata")
input_files = [join(input_dir, f) for f in listdir(input_dir) if isfile(join(input_dir, f)) and f.lower().endswith(".png")]
if not exists('output'):
makedirs('output')
def extract_pixel_data(img, marker_line):
result = []
inside_character = False
for i in range(0, img.width):
p = img.getpixel((i, marker_line))
if p == (255, 0, 0):
if not inside_character:
inside_character = True
start = i
elif p == (0, 255, 0):
if inside_character:
result.append((start, i))
inside_character = False
else:
result.append((i, i))
return result
def build_tables(img, bounds, y_pos):
jumps, widths, chars = [], [], []
cursor = 0
counter = 0
for i in range(0, 256):
table_index = get_char_mapping(i)
if table_index != -1:
(start, end) = bounds[table_index]
width = end - start + 1
jumps.append(("\n " if i % 16 == 0 and i else "") + str(cursor).rjust(5))
widths.append(("\n " if i % 16 == 0 and i else "") + str(width).rjust(2))
for x in range(start, end + 1):
bits = bitarray(endian="little")
for y in range(y_pos, y_pos + 8):
p = img.getpixel((x, y))
if p == (0, 0, 0):
bits.append(1)
else:
bits.append(0)
byte = bits.tobytes()[0]
if x == start:
b = bytearray([i])
comment = "/* " + b.decode("iso8859-15") + " */ "
else:
comment = ""
chars.append(("\n " if x == start and counter else "") + comment + f"{byte:#04x}")
cursor += width
counter += 1
else:
jumps.append(("\n " if i % 16 == 0 and i else "") + str(-1).rjust(5))
widths.append(("\n " if i % 16 == 0 and i else "") + str(-1).rjust(2))
jumps_str = ", ".join(jumps)
widths_str = ", ".join(widths)
chars_str = ", ".join(chars)
return jumps_str, widths_str, chars_str
# ISO-8859-15
def get_char_mapping(v):
# Special supported characters from upper block
if v == 0xc4: return 94 # Ä
if v == 0xd6: return 95 # Ö
if v == 0xdc: return 96 # Ü
if v == 0xe4: return 97 # ä
if v == 0xf6: return 98 # ö
if v == 0xdf: return 99 # ß
if v == 0xfc: return 100 # ü
if v == 0xa4: return 101 # €
# ASCII block (non control chars up to 127)
if 33 <= v <= 126:
return v - 33
# No mapping
return -1
def print_char(img, bounds, variant, v):
index = get_char_mapping(0xdf)
if index != -1:
(start, end) = bounds[index]
for y in range(variant["y_pos"], variant["y_pos"] + 8):
for x in range(start, end + 1):
p = img.getpixel((x, y))
if p == (0, 0, 0):
print("x", end='')
else:
print(" ", end='')
print("")
else:
print("No character data.")
for input_file in input_files:
img = Image.open(input_file)
font_name = splitext(basename(input_file))[0].capitalize()
variants = [
{"name": "Regular", "marker_line": 9, "y_pos": 10},
{"name": "Bold", "marker_line": 0, "y_pos": 1}
]
for v in variants:
bounds = extract_pixel_data(img, v["marker_line"])
variant = v["name"]
(jumps, widths, chars) = build_tables(img, bounds, v["y_pos"])
#print_char(img, bounds, v, 0xdf)
h_file = f"""#ifndef {font_name.upper()}_{variant.upper()}_H_
#define {font_name.upper()}_{variant.upper()}_H_
#include "pixelfont.h"
class {font_name}{variant} : public PixelFont {{
private:
static const uint8_t chars[];
static const int jumps[];
static const int widths[];
const uint8_t* get_chars() override {{
return chars;
}}
const int* get_jumps() override {{
return jumps;
}}
const int* get_widths() override {{
return widths;
}}
}};
const uint8_t {font_name}{variant}::chars[] = {{
{chars}
}};
const int {font_name}{variant}::jumps[] = {{
{jumps}
}};
const int {font_name}{variant}::widths[] = {{
{widths}
}};
#endif //{font_name.upper()}_{variant.upper()}_H_
"""
output_file = join(here, "output", font_name.lower() + f"-{variant.lower()}.h")
print(basename(input_file) + " -> " + basename(output_file))
f = open(output_file, "w")
f.write(h_file)
f.close()