Files
balatro-gba-chinese-jocker-…/scripts/generate_chinese_joker_font.py
T
wuxu c7ef3c0047 Add Chinese text rendering support
Extend the bitmap font pipeline with the Chinese Joker glyph subset and encode supported UTF-8 text for Tonc TTE. Move the affine map clear of the expanded font, add a compile-time VRAM overlap guard, and cover every custom glyph with host tests.
2026-07-19 11:35:14 +08:00

49 lines
1.6 KiB
Python

#!/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()