Path: blob/master/RSDKv5/RSDK/Dev/devfont_to_hpp.py
1168 views
# Requires the Pillow dependency1import sys, math, itertools2from PIL import Image3from pathlib import Path45WIDTH = 86HEIGHT = 87CHARCOUNT = 1288COLCOUNT = 16910IMGWIDTH = (COLCOUNT if CHARCOUNT >= COLCOUNT else CHARCOUNT) * WIDTH11IMGHEIGHT = math.ceil(CHARCOUNT / COLCOUNT) * HEIGHT1213def encode_font(image_path):14print(f"Opening {image_path}...")1516img = Image.open(image_path).convert("1")17assert img.size == (IMGWIDTH, IMGHEIGHT), f"Image does not match expected size ({IMGWIDTH}x{IMGHEIGHT})"1819print("Encoding pixel data...")20data = list()2122row = 023col = 024for char in range(CHARCOUNT):25h = row * HEIGHT26for y in range(h, h + HEIGHT):27for x in range(WIDTH):28data.append("0x01" if img.getpixel((x + (col * WIDTH), y)) else "0x00")29col += 130if col >= COLCOUNT:31col = 032row += 13334return data3536def main():37assert len(sys.argv) > 1, "No input file given"3839img_file = Path(sys.argv[1])40data = encode_font(img_file)4142# Using img_file.with_name() will save it in the same directory as img_file43output_file = Path(sys.argv[2]) if len(sys.argv) > 2 else img_file.with_name(img_file.stem + ".hpp")44print(f"Writing to {output_file}...")45try:46with output_file.open("w") as hpp_file:47hpp_file.write(f"#ifndef DEVFONT_H\n#define DEVFONT_H\n\n")48if CHARCOUNT == 128:49hpp_file.write("// First 128 characters of Code page 437 on old IBM computers\n")50hpp_file.write("uint8 devTextStencil[] = {\n ")5152# Print the list while dividing it into lines of 24 values53array = []54for row in itertools.batched(data, 24):55array.append(", ".join(pixel for pixel in row) + ",")56hpp_file.write("\n ".join(array)[:-1])5758hpp_file.write("\n};\n\n")59hpp_file.write("#endif")6061print("Done!")62except Exception as e:63print(f"[ERROR] {e}")6465if __name__ == "__main__":66main()676869