summaryrefslogtreecommitdiff
path: root/src/sliceitoff/images/fonts.py
blob: 98aa6e6aa92c4be3fd8f598149dfa3b1387df35b (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
import os
import pygame

DEBUG = os.getenv("DEBUG")

class Fonts:
    fonts = {}

    @staticmethod
    def load_fonts(base_path):
        filename_fontlist = os.path.join(base_path, "assets", "fonts.lst")
        with open( filename_fontlist ) as fontlist_file:
            for line in fontlist_file:
                name, *path = line.strip().split()
                filename = os.path.join(base_path, *path)
                __class__.fonts[name] = Font(filename)
        return True


class Font:
    def __init__(self, filename, height = 16):
        if DEBUG:
            print(f"Loading font {filename = }")
        self.surfaces = []
        with open(filename, mode="rb") as fnt_file:
            for _ in range(256):
                surface = pygame.Surface((8,height), pygame.SRCALPHA)
                for line in range(16):
                    byte = fnt_file.read(1)[0]
                    if line >= height:
                        continue
                    for bit in range(8):
                        if byte & 0x80:
                            surface.set_at((bit,line),"white")
                        byte <<= 1
                self.surfaces.append(surface)
    
    def get(self, ch):
        return self.surfaces[ord(ch)%256]