KAZIK/tools/generators/generate_font.py

42 lines
1.2 KiB
Python

from PIL import Image, ImageDraw, ImageFont
font_data = []
try:
font = ImageFont.truetype("consola.ttf", 9)
except Exception:
try:
font = ImageFont.truetype("arial.ttf", 9)
except Exception:
font = ImageFont.load_default()
for ch_code in range(256):
try:
ch = bytes([ch_code]).decode("cp1251")
except Exception:
ch = " "
img = Image.new("1", (8, 8), color=0)
draw = ImageDraw.Draw(img)
draw.text((0, -1), ch, font=font, fill=1)
char_bytes = []
pixels = img.load()
for y in range(8):
row_byte = 0
for x in range(8):
if pixels[x, y]:
row_byte |= (1 << (7 - x))
char_bytes.append(row_byte)
font_data.append(char_bytes)
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
with open(REPO_ROOT / "font8x8.inc", "w", encoding="cp1251") as f:
f.write("; --- 8x8 BITMAP FONT FOR CP1251 (Generated) ---\n")
f.write(".data\nALIGN 4\nfont8x8 label byte\n")
for idx, cb in enumerate(font_data):
bytes_str = ", ".join(f"0{b:02X}h" for b in cb)
f.write(f"db {bytes_str} ; char 0x{idx:02X}\n")
print("Generated clean font8x8.inc!")