-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitefont_encode.py
44 lines (35 loc) · 1.38 KB
/
itefont_encode.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
import struct
import numpy as np
from graphics import grid
CHAR_COUNT = 256
def read_chars(frames):
xpos = 0
empty = np.zeros((8, 0), dtype=np.uint8)
for frame in frames:
if not frame:
yield 0, 0, 0, 0, empty
else:
loc, img = frame
ndr = np.any(img, axis=0)
width = 1 + np.where(ndr)[0][-1] if ndr.any() else 0
assert width <= loc['x2']
packed = np.packbits(img[:, :width], axis=1)
yield xpos, width, 0, loc['x2'], packed
xpos += packed.shape[1]
if __name__ == '__main__':
frames = grid.read_image_grid('chars.png')
frames = [grid.resize_frame(frame) for frame in frames]
indices, widths, flags, trackings, imgs = zip(*read_chars(frames))
widths = list(widths)
max_w = max(widths)
bitmap = np.hstack(imgs)
max_h, row_len = bitmap.shape
with open('OUT.RES', 'wb') as out:
out.write(struct.pack('<3H', max_h, max_w, row_len))
out.write(struct.pack(f'<{CHAR_COUNT}H', *indices))
out.write(struct.pack(f'<{CHAR_COUNT}B', *widths))
out.write(struct.pack(f'<{CHAR_COUNT}B', *flags))
out.write(struct.pack(f'<{CHAR_COUNT}B', *trackings))
out.write(bitmap.tobytes())
# for line in np.unpackbits(bitmap, axis=1).tolist():
# print(''.join(str(x) for x in line).replace('0', '_').replace('1', '@'))