92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Append missing 普/通/业 glyphs to CHS16 MiSans tables."""
|
|
import glob
|
|
import os
|
|
import re
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
HEADER = os.path.join(ROOT, "device", "LCD_ILI9341", "Drv_ILI9341_Lcd_ChineseFont.h")
|
|
|
|
cands = []
|
|
for base in [
|
|
os.path.normpath(os.path.join(ROOT, "..", "..", "Doc", "UI")),
|
|
r"c:\Users\qjyu\Documents\SoundWalker\一诺国际吉他\Doc\UI",
|
|
]:
|
|
if os.path.isdir(base):
|
|
cands += glob.glob(os.path.join(base, "**", "*.ttf"), recursive=True)
|
|
|
|
ttf = None
|
|
for p in cands:
|
|
name = os.path.basename(p).lower()
|
|
if "misans" in name and "medium" in name:
|
|
ttf = p
|
|
break
|
|
if not ttf:
|
|
for p in cands:
|
|
if "misans" in os.path.basename(p).lower():
|
|
ttf = p
|
|
break
|
|
if not ttf:
|
|
raise SystemExit(f"MiSans ttf not found, candidates={cands[:10]}")
|
|
|
|
print("using", ttf)
|
|
font = ImageFont.truetype(ttf, 16)
|
|
|
|
|
|
def render(ch, size=16):
|
|
img = Image.new("L", (size, size), 0)
|
|
draw = ImageDraw.Draw(img)
|
|
bbox = draw.textbbox((0, 0), ch, font=font)
|
|
tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
|
|
x = (size - tw) // 2 - bbox[0]
|
|
y = (size - th) // 2 - bbox[1]
|
|
draw.text((x, y), ch, fill=255, font=font)
|
|
out = []
|
|
row_bytes = (size + 7) // 8
|
|
for row in range(size):
|
|
for bi in range(row_bytes):
|
|
val = 0
|
|
for bit in range(8):
|
|
col = bi * 8 + bit
|
|
if col < size and img.getpixel((col, row)) > 127:
|
|
val |= 0x80 >> bit
|
|
out.append(val)
|
|
return out
|
|
|
|
|
|
chars = [("普", 0xC6, 0xD5), ("通", 0xCD, 0xA8), ("业", 0xD2, 0xB5)]
|
|
glyphs = [(name, h, l, render(name)) for name, h, l in chars]
|
|
|
|
text = open(HEADER, encoding="utf-8", errors="ignore").read()
|
|
|
|
m = re.search(r"(const char CHS16Table\[\]\[2\] = \{)(.*?)(\n\};)", text, re.S)
|
|
if not m:
|
|
raise SystemExit("CHS16Table not found")
|
|
body = m.group(2)
|
|
for name, h, l, data in glyphs:
|
|
needle = "{0x%02X,0x%02X}" % (h, l)
|
|
if needle.lower() in body.lower().replace(" ", ""):
|
|
print("already in CHS16", name)
|
|
else:
|
|
body = body.rstrip() + "\n {0x%02X,0x%02X}, // %s\n" % (h, l, name)
|
|
print("added to CHS16", name)
|
|
text = text[: m.start()] + m.group(1) + body + m.group(3) + text[m.end() :]
|
|
|
|
m2 = re.search(r"(const unsigned char Chinese_font_16\[\]\[32\] = \{)(.*?)(\n\};)", text, re.S)
|
|
if not m2:
|
|
raise SystemExit("Chinese_font_16 not found")
|
|
fbody = m2.group(2).rstrip()
|
|
n = len(re.findall(r"/\*\"", fbody))
|
|
print("existing font16 glyphs", n)
|
|
for i, (name, h, l, data) in enumerate(glyphs):
|
|
# skip if comment already has this char as last additions - check table-driven: always append if we added to table
|
|
idx = n + i
|
|
arr = ",".join(f"0x{b:02X}" for b in data)
|
|
fbody += f"\n{{{arr}}},/*\"{name}\",{idx}*/"
|
|
print("added font16", name, "idx", idx)
|
|
text = text[: m2.start()] + m2.group(1) + fbody + m2.group(3) + text[m2.end() :]
|
|
|
|
open(HEADER, "w", encoding="utf-8", newline="\n").write(text)
|
|
print("patched OK")
|