aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/tools/bitmap_font/adafruit_bitmap_font/bitmap_font.py
blob: c536e98078a787e7e9e95fc50db9ee43fc680bd0 (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
import sys

def load_font(filename, bitmap=None):
    if not bitmap:
        import displayio
        bitmap = displayio.Bitmap
    f = open(filename, "rb")
    first_four = f.read(4)
    #print(first_four)
    if filename.endswith("bdf") and first_four == b"STAR":
        from . import bdf
        return bdf.BDF(f, bitmap)
    elif filename.endswith("pcf") and first_four == b"\x01fcp":
        import pcf
        return pcf.PCF(f)
    elif filename.endswith("ttf") and first_four == b"\x00\x01\x00\x00":
        import ttf
        return ttf.TTF(f)



if __name__ == "__main__":
    f = load_font(sys.argv[1])
    # print(f.characters)
    for c in "Adafruit CircuitPython":
        o = ord(c)
        if o not in f.characters:
            continue
        glyph = f.characters[o]
        print(glyph)
    for i in range(10):
        for c in "Adafruit CircuitPython":
            o = ord(c)
            if o not in f.characters:
                continue
            glyph = f.characters[o]
            # print(glyph)
            shifted_i = i + (glyph["bounds"][1] - 8) + glyph["bounds"][3]
            if 0 <= shifted_i < len(glyph["bitmap"]):
                pixels = glyph["bitmap"][shifted_i]
            else:
                pixels = ""
            print(pixels + " " * (glyph["shift"][0] - len(pixels)), end="")
        print()