-
Notifications
You must be signed in to change notification settings - Fork 0
/
uf2fier.py
116 lines (91 loc) · 3.15 KB
/
uf2fier.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
#!/usr/bin/env python3
import subprocess
from sys import argv
class UF2Font:
def __init__(self, name, data):
self.data = {encoding: UF2Char(encoding, 0x00, [0x00] * 0x20)
for encoding in range(0x100)}
self.name = name
self.widths = [0x00] * 0x100
for k, v in data.items():
self.data[k] = v
self.widths[k] = v.width
def __repr__(self):
width_lines = []
for i in range(0, len(self.widths), 16):
line = " ".join(
f"{self.widths[j]:02x}{self.widths[j + 1]:02x}"
for j in range(i, i + 16, 2)
)
width_lines.append(f"\t\t{line}")
glyph_lines = "\n".join(str(char) for char in self.data.values())
return (
f"@Font-{self.name}\n"
"\t&widths [\n"
+ "\n".join(width_lines) + " ]\n"
"\t&glyphs [\n"
+ glyph_lines + " ]\n"
)
class UF2Char:
def __init__(self, code, width, data):
self.code = code
self.width = width
self.data = tuple(data)
def __repr__(self):
return (
("\t\t( code: %02x, width: %02x )\n" % (self.code, self.width)) +
"\t\t%02x%02x %02x%02x %02x%02x %02x%02x "
"%02x%02x %02x%02x %02x%02x %02x%02x\n\t\t"
"%02x%02x %02x%02x %02x%02x %02x%02x "
"%02x%02x %02x%02x %02x%02x %02x%02x"
) % self.data
return (
f"( code: {self.code:02x}, width: {self.width:02x} )\n"
"{data_lines}\n"
)
def parse_bdf_file(input_file):
with open(input_file, 'r') as file:
data = {}
name = input_file.split('.')[0]
encoding = None
width = 0
bitmap = []
in_bitmap = False
i = 0
for line in file:
line = line.strip()
if line.startswith('ENCODING'):
encoding = int(line.split()[1])
if encoding > 255:
encoding = None
elif encoding is not None and line.startswith('DWIDTH'):
width = int(line.split()[1])
elif encoding is not None and line.startswith('BITMAP'):
bitmap = [0x00] * 0x20
in_bitmap = True
i = 0
elif in_bitmap:
if line.startswith('ENDCHAR'):
data[encoding] = UF2Char(encoding, width, bitmap)
encoding = None
width = None
in_bitmap = False
else:
bitmap[i] = int(line, 16)
i += 1
return UF2Font(name, data)
def main(input_file):
# Parse BDF file
font = parse_bdf_file(input_file)
# Write the .tal file
tal_file = f"{input_file.split('.')[0]}.tal"
with open(tal_file, 'w') as file:
file.write(str(font))
# Assemble the .uf2 file
uf2_file = f"{input_file.split('.')[0]}.uf2"
subprocess.run(['uxnasm', tal_file, uf2_file], check=True)
if __name__ == "__main__":
if len(argv) != 2:
print("Usage: python uf2fier.py <input-file.bdf>")
else:
main(argv[1])