Files
pricing_review/test/字体加固/生成字体.py

91 lines
4.1 KiB
Python
Raw 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.
# -*- coding: utf-8 -*-
"""字体加固(2026-11-05 主人拍板):把 OFL 免费可商用的 Noto Sans SC 子集化打包进软件,
替代「只用系统字体名字引用」的模式,保证三端显示一致且零版权风险。
输入: D:/PricingReview/_dl_fonts/NotoSansSC-VF.ttf (Google Fonts 官方可变字体,SIL OFL 1.1)
输出: CostReview/app/ui/fonts/NotoSansSC-Regular.woff2 / NotoSansSC-Bold.woff2
CostReview/app/ui/fonts/LICENSE-OFL.txt (许可正文,OFL 要求随字体分发保留)
字符集口径(宁可多不可少,生僻字仍由微软雅黑兜底):
1. ASCII 可打印区
2. GB2312 全集(6763 汉字 + 符号,覆盖用户数据常规字)
3. 界面源码(ui/ 全部 html/css/js + gate.py/version_gate.py)里出现的全部非 ASCII 字符
4. 造价行业常用符号补充表(①-⑳ φ ㎡ ㎥ Ⅰ-Ⅻ ✓ 等)
用法: python -I test/字体加固/生成字体.py
依赖: pip install fonttools brotli (仅生成期工具,运行时零依赖)
"""
import os
from fontTools.ttLib import TTFont
from fontTools.varLib.instancer import instantiateVariableFont
from fontTools.subset import Subsetter, Options
ROOT = r"D:\PricingReview"
SRC = os.path.join(ROOT, "_dl_fonts", "NotoSansSC-VF.ttf")
OUT_DIR = os.path.join(ROOT, "CostReview", "app", "ui", "fonts")
UI_DIR = os.path.join(ROOT, "CostReview", "app", "ui")
SERVER_DIR = os.path.join(ROOT, "CostReview", "app", "server")
EXTRA = (
"·、。,;:?!“”‘’()《》〈〉【】〔〕[]{}|~¥%#@&*-=+—…‥「」『』"
"〇①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿"
"ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ½⅓¼⅛×÷±≤≥≠≈≡∞√∑∏∫℃℉‰§¶•→←↑↓↔⇒⇐"
"φΦαβγδΔθλμπσωΩεζηκνξορστυχψωΨ"
"㎡㎥㎝㎜㎏㎎³²¹⁰ⁿ₁₂₃✓✗✕✔✘★☆■□▲△▼▽◆◇○●◎⊙"
"░▒▓█▌▐▁▂▃▄▅▆▇─│┌┐└┘├┤┬┴┼"
)
def collect_chars():
chars = set()
for cp in range(0x20, 0x7F):
chars.add(chr(cp))
# GB2312 全集
for hi in range(0xA1, 0xF8):
for lo in range(0xA1, 0xFF):
try:
chars.add(bytes([hi, lo]).decode("gb2312"))
except UnicodeDecodeError:
pass
# 界面源码里实际出现的非 ASCII 字符(界面文案全覆盖)
files = ["workbench.html", "sessions.html", "admin.html", "common.css",
"common.js", "period.js"]
files += [os.path.join("license_client", "gate.py"), "version_gate.py"]
for fn in files:
p = os.path.join(SERVER_DIR, fn) if fn.endswith(".py") else os.path.join(UI_DIR, fn)
if os.path.exists(p):
txt = open(p, encoding="utf-8", errors="ignore").read()
chars |= {c for c in txt if ord(c) > 0x7F}
chars |= set(EXTRA)
for ctrl in "\n\r\t\x0b\x0c":
chars.discard(ctrl)
return "".join(sorted(c for c in chars if not c.isspace() or c == " "))
def build(text, wght, name):
font = TTFont(SRC)
instantiateVariableFont(font, {"wght": wght}, inplace=True)
opts = Options()
opts.flavor = "woff2"
opts.layout_features = ["*"] # 保留 kern/liga 等排版特性
opts.hinting = False # CJK 网络字体通行做法:去 hinting 减体积
opts.name_IDs = [0, 1, 2, 3, 4, 6]
ss = Subsetter(options=opts)
ss.populate(text=text)
ss.subset(font)
out = os.path.join(OUT_DIR, "NotoSansSC-%s.woff2" % name)
font.save(out)
print("%s (%d): %d KB, glyphs 剩余 %d" % (name, wght, os.path.getsize(out) // 1024,
font["maxp"].numGlyphs))
if __name__ == "__main__":
os.makedirs(OUT_DIR, exist_ok=True)
text = collect_chars()
print("字符集总数: %d" % len(text))
build(text, 400, "Regular")
build(text, 700, "Bold")
lic = os.path.join(OUT_DIR, "LICENSE-OFL.txt")
with open(os.path.join(ROOT, "_dl_fonts", "LICENSE-OFL.txt"), "rb") as f:
data = f.read()
with open(lic, "wb") as f:
f.write(data)
print("LICENSE-OFL.txt ->", lic, len(data), "bytes")