summaryrefslogtreecommitdiff
path: root/include/lib/pixelfont/ttf-to-font.py
blob: 38460fe0779f3c0ce52a7b91c13a85ddb51d12c6 (plain)
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
#!/usr/bin/env python3

import PIL
import PIL.ImageDraw
import PIL.ImageFont

def invert(x):
    ret = 0
    for i in range(8):
        if x & (0x80 >> i):
            ret |= 0x01 << i
    return ret


if __name__ == "__main__":
    font = PIL.ImageFont.truetype(
        "/usr/share/fonts/truetype/terminus/TerminusTTF-4.46.0.ttf", size=14
    )

    for char in range(0x20, 0x7F):
        fontimg = PIL.Image.new("1", (16, 16))
        draw = PIL.ImageDraw.Draw(fontimg)
        draw.text((0, 0), chr(char), font=font, fill=255)

        bbox = fontimg.getbbox()

        if bbox is None:
            print(
                "glyph_line_t GLYPH_ATTR chr16_032[] = {0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; // <spac>"
            )
            continue

        left, upper, right, lower = bbox
        fontimg = fontimg.crop((left, 0, right, 15))

        fontimg = fontimg.transpose(PIL.Image.ROTATE_270).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        fontbytes = fontimg.tobytes()

        glyph_len = len(fontbytes)
        glyph_data = "{" + f"0x{glyph_len:02x}," + ",".join(map(lambda x: f"0x{invert(x):02x}", fontbytes)) + "}"

        assert glyph_len % 2 == 0

        print(
            f"glyph_line_t GLYPH_ATTR chr16_{char:03d}[] = {glyph_data}; // {chr(char)}"
        )