-
Notifications
You must be signed in to change notification settings - Fork 7
/
make_font.py
executable file
·55 lines (39 loc) · 1.07 KB
/
make_font.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import re
regex = re.compile(r'char (0x[0-9a-fA-F]+)\n(([\.\*]{8}\n){16}\n)')
script_tmpl = '''
char hankaku[{length}] = {{
{body}
}};
'''
def main():
if len(sys.argv) != 2:
print('usage:', sys.argv[0], '[file]', file=sys.stderr)
sys.exit(1)
fpath = sys.argv[1]
with open(fpath) as f:
lines = [','.join(l) for l in scan(f.read())]
# char_len = len(lines)
body = ','.join(lines)
result = script_tmpl.format(length=4096, body=body)
print(result)
def scan(f):
get_token = regex.match
mo = get_token(f, 0)
while mo is not None:
num = mo.group(1)
body = mo.group(2)
yield make_font_hex(num, body)
mo = get_token(f, mo.end())
def make_font_hex(head, body):
return [bits2hex(str2bits(line)) for line in body.split()]
def bits2hex(bits):
return hex(int(bits, 2))
def str2bits(line):
return ''.join(map(char2bit, line))
def char2bit(char):
return '0' if char == '.' else '1'
if __name__ == '__main__':
main()