election-game/art/fonts/split-webfonts.py

252 lines
12 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# /// script
# requires-python = ">=3.12"
# dependencies = ["fonttools>=4.55", "brotli>=1.1"]
# ///
"""
把遊戲的兩個字型切成很多小片(unicode-range),瀏覽器只下載畫面上用到的那幾片。
原本整個字型一次下載:jf open 粉圓 2.1 MB、Tongsuan Rounded Heavy 1.3 MB,首頁、進一局都要先抓 3.4 MB。
切法(跟 Google Fonts 的中文字型一樣是照常用程度切,只是「常用」用的是這個遊戲自己的字):
1. 拉丁字母、標點、全形符號放第 0 片(每個畫面都要)。
2. 主要畫面上看得到的字(screens.txt,首頁的字最前面),一片 SLICE 個字:首頁只要 1 片、一局再多 2–3 片。
3. 縣市、鄉鎮區名、劇本候選人(一局的地圖與區列表一定有)照出現次數接著排。
4. 程式裡的字(前端畫面、卡牌與規則的文字),再來是村里、歷屆參選人、廟名,都照出現次數。
5. 字型裡其他的字照字碼排,每片 REST 個字(很少用到)。
輸出:web/public/assets/fonts/<ui|rounded>/<n>.woff2,與 web/src/theme/fonts.generated.css(@font-face)。
OFL 1.1:切過的字型是修改版,不能沿用保留字型名稱(open huninn、huninn、Varela、GenSenRounded),內部名稱改成 Tongsuan。
用法(在專案根目錄):uv run art/fonts/split-webfonts.py
資料(地名、人名)讀 backend/data/build;沒有產生資料時只用程式裡的字,切片的順序會不同,但每個字都還在。
"""
from __future__ import annotations
import io
import json
import re
import shutil
from collections import Counter
from pathlib import Path
from fontTools import subset
from fontTools.ttLib import TTFont
ROOT = Path(__file__).resolve().parents[2]
OUT = ROOT / "web/public/assets/fonts"
CSS = ROOT / "web/src/theme/fonts.generated.css"
BUILD = ROOT / "backend/data/build"
# 常用字一片 SLICE 個,其他的字一片 REST 個
SLICE = 200
REST = 480
FONTS = [
{
"dir": "ui",
"src": ROOT / "art/fonts/jf-openhuninn-2.1.woff2",
"family": "Tongsuan UI Regular",
"ps": "TongsuanUI-Regular",
# 原本整個字型都收(約 1.2 萬字)
"keep": None,
"faces": [("Tongsuan UI", "100 600")],
"credit": "jf open huninn 2.1 by justfont (SIL OFL 1.1), sliced for the web",
},
{
"dir": "rounded",
"src": ROOT / "art/fonts/gensen-rounded2-tw-heavy.woff2",
"family": "Tongsuan Rounded Heavy",
"ps": "TongsuanRounded-Heavy",
# 和 2026-10-26 的子集一樣:遊戲用到的字+Big5 常用字(標題用的粗體,沒必要收全部 3 萬字)
"keep": "big5-common",
"faces": [("Tongsuan UI", "700 950"), ("Tongsuan Rounded", "100 950")],
"credit": "GenSenRounded2 TW Heavy by ButTaiwan (SIL OFL 1.1), subset and sliced for the web",
},
]
CJK = re.compile(r"[㐀-䶿一-鿿豈-﫿]")
def strip_comments(text: str) -> str:
"""去掉 // 與 /* */ 註解(只是為了算頻率,不必完全正確);網址裡的 // 前面是冒號,不會被當成註解"""
text = re.sub(r"/\*.*?\*/", " ", text, flags=re.S)
return re.sub(r"(?<![:\"'`])//[^\n]*", " ", text)
def screen_text() -> str:
"""主要畫面上看得到的字,照畫面的順序(screens.txt:每行「畫面名稱<tab>字」,# 開頭是說明)"""
rows = (Path(__file__).with_name("screens.txt")).read_text("utf-8").splitlines()
return "".join(r.split("\t", 1)[1] for r in rows if "\t" in r and not r.startswith("#"))
def code_text() -> str:
"""遊戲畫面上的字:前端程式(開發用頁面除外)與後端的卡牌、規則、內容文字"""
parts: list[str] = []
globs = [
(ROOT / "web/src", ("*.ts", "*.tsx")),
(ROOT / "backend/packages/rogue/src", ("*.ts",)),
(ROOT / "backend/packages/content/src", ("*.ts",)),
(ROOT / "backend/packages/protocol/src", ("*.ts",)),
]
skip = ("viewer/", "art/ArtGallery", "art/FaceGallery", "art/AvatarCheck", ".generated.")
for base, patterns in globs:
for pattern in patterns:
for f in sorted(base.rglob(pattern)):
if any(s in f.as_posix() for s in skip):
continue
parts.append(strip_comments(f.read_text("utf-8")))
return "\n".join(parts)
def place_text() -> str:
"""一局的畫面一定會出現的名字:縣市、鄉鎮區、劇本裡的候選人(劇本、資料包);沒有產生資料時是空字串"""
parts: list[str] = []
for f in sorted((BUILD / "scenarios").rglob("*.json")):
try:
d = json.loads(f.read_text("utf-8"))
except (OSError, ValueError):
continue
parts += [str(d.get("areaName", "")), str(d.get("countyName", ""))]
parts += [str(c.get("name", "")) for c in d.get("candidates", []) if isinstance(c, dict)]
for f in sorted((BUILD / "packs").glob("*/towns.json")):
parts += [t["name"] for t in json.loads(f.read_text("utf-8")).get("towns", [])]
return "\n".join(parts)
def name_text() -> str:
"""比較少出現的名字:村里、歷屆參選人、廟名"""
parts: list[str] = []
for f in sorted((BUILD / "packs").glob("*/villages.json")):
parts += [v["name"] for v in json.loads(f.read_text("utf-8")).get("villages", [])]
people = BUILD / "people/records.json"
if people.exists():
parts += [p["name"] for p in json.loads(people.read_text("utf-8")).get("people", [])]
temples = BUILD / "temples/temples.json"
if temples.exists():
for rows in json.loads(temples.read_text("utf-8")).values():
parts += [t.get("name", "") + t.get("deity", "") for t in rows]
return "\n".join(parts)
def big5_common() -> set[int]:
"""Big5 常用字(一級,0xA440–0xC67E)"""
out: set[int] = set()
for hi in range(0xA4, 0xC7):
for lo in list(range(0x40, 0x7F)) + list(range(0xA1, 0xFF)):
if (hi, lo) > (0xC6, 0x7E):
break
try:
out.add(ord(bytes([hi, lo]).decode("big5")))
except UnicodeDecodeError:
pass
return out
def ranges(cps: list[int], bridge: set[int] | None = None) -> str:
"""字碼清單 → CSS unicode-range(連續的合併成 U+4E00-4E0F)。
bridge:可以跨過的字碼(字型裡沒有的字),中間只有這種字碼就合併,範圍短很多;不能跨過別片的字,範圍重疊時瀏覽器兩片都會下載"""
cps = sorted(cps)
out: list[str] = []
i = 0
while i < len(cps):
j = i
while j + 1 < len(cps) and (cps[j + 1] == cps[j] + 1 or (bridge is not None and all(c in bridge for c in range(cps[j] + 1, cps[j + 1])))):
j += 1
out.append(f"U+{cps[i]:X}" if i == j else f"U+{cps[i]:X}-{cps[j]:X}")
i = j + 1
return ",".join(out)
def slices_for(cmap: set[int], keep: set[int] | None, screens: str, ui: Counter[str], tiers: list[Counter[str]]) -> list[tuple[list[int], bool]]:
"""切好的每一片:(字碼, 是不是「其他的字」)。其他的字照字碼連續切,CSS 可以寫成一整段範圍"""
wanted = cmap if keep is None else cmap & keep
# 第 0 片:不是漢字的(拉丁、標點、注音、全形),遊戲裡用到的才放;其他符號放最後一片
basic = [c for c in wanted if not CJK.match(chr(c)) and (chr(c) in ui or c <= 0x24F or 0x2000 <= c <= 0x206F or 0x3000 <= c <= 0x303F or 0xFF00 <= c <= 0xFFEF)]
other_symbols = sorted(c for c in wanted if not CJK.match(chr(c)) and c not in basic)
han = {c for c in wanted if CJK.match(chr(c))}
# 畫面上的字照畫面順序,接著地名、程式、其他名字照出現次數,連在一起切
known: list[int] = list(dict.fromkeys(ord(ch) for ch in screens if ord(ch) in han))
for counter in tiers:
done = set(known)
known += sorted((c for c in han if chr(c) in counter and c not in done), key=lambda c: (-counter[chr(c)], c))
rest = sorted(han - set(known))
out = [(sorted(basic), False)] + [(known[i : i + SLICE], False) for i in range(0, len(known), SLICE)]
out += [(rest[i : i + REST], True) for i in range(0, len(rest), REST)]
return out + ([(other_symbols, False)] if other_symbols else [])
def rename(font: TTFont, family: str, ps: str, credit: str) -> None:
"""OFL:修改版不能用保留字型名稱;名稱表只留新的名字與授權、出處"""
name = font["name"]
keep = {0, 7, 8, 9, 11, 12, 13, 14}
name.names = [n for n in name.names if n.nameID in keep]
for nid, value in ((1, family), (2, "Regular"), (3, f"{ps};sliced"), (4, family), (5, "Version 1.0"), (6, ps), (10, credit)):
name.setName(value, nid, 3, 1, 0x409)
def build() -> None:
code = code_text()
ui = Counter(CJK.findall(code))
ui.update(ch for ch in code if not CJK.match(ch))
places = Counter(CJK.findall(place_text()))
names = Counter(CJK.findall(name_text()))
screens = screen_text()
ui.update(ch for ch in screens if not CJK.match(ch))
big5 = big5_common()
css: list[str] = [
"/* 自動產生(art/fonts/split-webfonts.py),不要手改。字型切成小片,瀏覽器只下載畫面上用到的字。",
" 授權:SIL OFL 1.1(web/public/assets/LICENSES.md)。 */",
]
for spec in FONTS:
src = TTFont(spec["src"])
cmap = set(src.getBestCmap())
keep = None
if spec["keep"] == "big5-common":
keep = big5 | {ord(c) for c in (*ui, *places, *names, *screens)} | {c for c in cmap if c < 0x3400}
parts = slices_for(cmap, keep, screens, ui, [places, ui, names])
# 原始字型解壓一次(woff2 每次重讀很慢),每一片從未壓縮的副本切
raw = io.BytesIO()
src.flavor = None
src.save(raw)
out_dir = OUT / spec["dir"]
shutil.rmtree(out_dir, ignore_errors=True)
out_dir.mkdir(parents=True)
total = 0
# 「其他的字」的片可以跨過字型裡沒有的字碼(CSS 小很多);字型裡有的字一定只在一片的範圍裡
absent = set(range(0x3400, 0xFB00)) - {c for p, _ in parts for c in p}
rules: list[str] = []
for i, (cps, is_rest) in enumerate(parts):
options = subset.Options()
options.flavor = "woff2"
options.layout_features = ["kern", "liga", "calt", "ccmp", "locl", "mark", "mkmk", "palt", "halt"]
options.hinting = False
options.name_IDs = ["*"]
options.name_languages = ["*"]
options.notdef_outline = False
options.drop_tables += ["vhea", "vmtx", "VORG", "BASE", "meta", "DSIG"]
raw.seek(0)
font = TTFont(raw)
sub = subset.Subsetter(options)
sub.populate(unicodes=cps)
sub.subset(font)
rename(font, spec["family"], spec["ps"], spec["credit"])
path = out_dir / f"{i}.woff2"
font.flavor = "woff2"
font.save(path)
total += path.stat().st_size
span = ranges(cps, absent if is_rest else None)
for family, weight in spec["faces"]:
rules.append(
f'@font-face{{font-family:"{family}";src:url("/assets/fonts/{spec["dir"]}/{i}.woff2") format("woff2");'
f"font-weight:{weight};font-display:swap;unicode-range:{span}}}"
)
css += rules
print(f"{spec['dir']}: {len(parts)} 片、{sum(len(p) for p, _ in parts)} 字、{total / 1024:.0f} KB")
CSS.write_text("\n".join(css) + "\n", "utf-8")
print(f"{CSS.relative_to(ROOT)}:{CSS.stat().st_size / 1024:.0f} KB")
if __name__ == "__main__":
build()