#!/usr/bin/env python3 """Append the Chinese Joker-description glyph subset to the project's 8x8 ASCII font.""" from argparse import ArgumentParser from pathlib import Path import re from PIL import Image, ImageDraw, ImageFont ASCII_FONT_HEIGHT = 48 CELL_SIZE = 8 GLYPHS_PER_ROW = 16 def read_codepoints(table_path: Path) -> list[int]: table = table_path.read_text(encoding="utf-8") return [int(value, 16) for value in re.findall(r"CHINESE_GLYPH\(0x([0-9A-Fa-f]+)\)", table)] def main() -> None: parser = ArgumentParser() parser.add_argument("--font", required=True, help="Fusion Pixel Font 8px zh_hans TTF") parser.add_argument("--table", default="include/def_chinese_glyph_table.h") parser.add_argument("--image", default="font/gbalatro_sys8.png") args = parser.parse_args() image_path = Path(args.image) base = Image.open(image_path).convert("RGB").crop((0, 0, 128, ASCII_FONT_HEIGHT)) codepoints = read_codepoints(Path(args.table)) extra_rows = (len(codepoints) + GLYPHS_PER_ROW - 1) // GLYPHS_PER_ROW output = Image.new("RGB", (128, ASCII_FONT_HEIGHT + extra_rows * CELL_SIZE), "white") output.paste(base, (0, 0)) draw = ImageDraw.Draw(output) font = ImageFont.truetype(args.font, CELL_SIZE) for index, codepoint in enumerate(codepoints): x = (index % GLYPHS_PER_ROW) * CELL_SIZE y = ASCII_FONT_HEIGHT + (index // GLYPHS_PER_ROW) * CELL_SIZE draw.text((x, y - 1), chr(codepoint), font=font, fill="black") output.save(image_path, optimize=True) print(f"Appended {len(codepoints)} Chinese glyphs to {image_path}") if __name__ == "__main__": main()