#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 中文标准字:把「覆盖」和「体积」两件事从"查资料"变成"量出来"(第四十七刀) 为什么这么做:上一轮授权核验交给研究代理,回来的结论里有一条明确标着 「马善政以 GB2312 简体为主,繁体『遠』是否齐全 —— 未核实」。 品牌名恰好是「睿新致遠」,繁体在不在是**能不能用它**的第一道闸, 不能停在"未核实"上。 方法(自证式,不靠任何人的转述): 1. 从仓库真扫出站点实际用的汉字集(scan 见 site_chars,407 个 .ts/.tsx / 3613 条串 / 967 字); 2. 用 Google Fonts 的 css2?text= 接口,让服务端**按我们这一集**做子集化,返回一个 woff2; 3. 下载那个 woff2,用 fontTools 读它的 cmap,与请求集做差 ⇒ 缺哪些字是**字形层面的事实**; 4. 同一个 woff2 的字节数就是"我们这份文案的真实子集体积",不是估算。 这条链的副产品正好是子集化方案的可行性证明:如果服务端能按任意字符集切片, 自托管走的就是同一条路(fontTools subset + brotli)。 工具环境:/tmp/fenv(python3 -m venv + pip install fonttools brotli)。 系统 Python 被 PEP 668 挡住,不用 --break-system-packages 去动它。 """ import io import json import os import re import sys import urllib.parse import urllib.request UA = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36") # 候选:family=Google Fonts 族名,label=给人看的名字,note=它凭什么进候选 CANDS = [ ("Noto+Serif+SC:ital,wght@0,400;0,700", "思源宋体 400/700", "OFL 1.1,简繁同族"), ("Noto+Sans+SC:ital,wght@0,400;0,700", "思源黑体 400/700", "OFL 1.1,黑体对照"), ("Ma+Shan+Zheng", "马善政毛笔楷书", "OFL 1.1,毛笔气质;繁体覆盖待验"), ("LXGW+WenKai+TC", "霞鹜文楷 TC", "OFL 1.1+RFN 条款;Google 只有 TC"), ("ZCOOL+XiaoWei", "站酷小薇", "标题用,看气质差异"), ] PUNCT = ",。、;:?!“”‘’()《》〈〉【】—…·~-/\\|@#$%&*+=<>~^_`'\";,.?!:()[]{}0123456789" BRAND = "睿新致遠" # 品牌名,繁体「遠」是这一刀要验的那一个字 EXTRA = "远达道术数网络系统架构咨询方案服务技术团队企业客户项目交付价值战略" def site_chars(): p = "/tmp/site-chars.json" if os.path.exists(p): d = json.load(open(p, encoding="utf-8")) return "".join(d["han"]), d return "", {} def fetch(url): req = urllib.request.Request(url, headers={"User-Agent": UA}) return urllib.request.urlopen(req, timeout=60).read() def css_for(family, text): q = urllib.parse.quote(text) return fetch("https://fonts.googleapis.com/css2?family=%s&text=%s" % (family, q)).decode("utf-8") def subsets(css): """从 CSS 里取出 (unicode_range, url)。text= 子集通常只返回 1 个 @font-face 每字重。""" out = [] for blk in re.findall(r"@font-face\s*\{(.*?)\}", css, re.S): u = re.search(r"url\((https://[^)]+\.woff2)\)", blk) wr = re.search(r"font-weight:\s*([\d ]+)", blk) ur = re.search(r"unicode-range:\s*([^;]+);", blk) if u: out.append((wr.group(1).strip() if wr else "?", ur.group(1).strip() if ur else "", u.group(1))) return out def cmap_of(woff2): from fontTools.ttLib import TTFont f = TTFont(io.BytesIO(woff2)) cs = set() for t in f["cmap"].tables: for cp in t.cmap: try: cs.add(chr(cp)) except ValueError: pass return cs, f def main(): han, d = site_chars() want = sorted(set(han) | set(BRAND) | set(EXTRA) | set(PUNCT)) text = "".join(want) print("请求字符集:%d 个码位(其中汉字 %d)" % (len(want), len(set(han)))) rows = [] for family, label, note in CANDS: try: css = css_for(family, text) except Exception as e: # noqa: BLE001 print("!! %s CSS 取不到:%s" % (label, e)) continue subs = subsets(css) if not subs: print("!! %s 的 CSS 里没有 woff2 —— 接口形状变了," "这不能读成『这个字体没有字』" % label) continue got = set() total = 0 for wgt, ur, url in subs: try: blob = fetch(url) except Exception as e: # noqa: BLE001 print(" woff2 下载失败 %s: %s" % (wgt, e)) continue total += len(blob) try: cs, _f = cmap_of(blob) got |= cs except Exception as e: # noqa: BLE001 print(" cmap 读不出 %s: %s" % (wgt, e)) n_faces = len(subs) missing = [c for c in want if c not in got] missing_han = [c for c in missing if re.match(r"[一-鿿]", c)] rows.append(dict(label=label, note=note, faces=n_faces, kb=round(total / 1024.0, 1), have=len(want) - len(missing), want=len(want), miss=missing, miss_han=missing_han, brand_ok=all(c in got for c in BRAND), yuan_fan="遠" in got, yuan_jian="远" in got)) print("%-16s %2d 个 @font-face 子集合计 %7.1f KB 覆盖 %d/%d 缺汉字 %d 「遠」%s" % (label, n_faces, total / 1024.0, len(want) - len(missing), len(want), len(missing_han), "有" if "遠" in got else "★没有★")) if missing_han: print(" 缺的汉字(前 40):", "".join(missing_han[:40])) json.dump(dict(want=want, n_han=len(set(han)), rows=rows), open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "_fontcov.json"), "w"), ensure_ascii=False, indent=1) print("\n写出 _fontcov.json") if __name__ == "__main__": main()