#!/usr/bin/env python3 # -*- coding: utf-8 -*- """生成 staging-p2/fontsync.sh —— P2(思源宋体自托管)的落地件。**单独一笔授权**。 现状核对(不是推测):`globals.css:201` 的 `--font-serif` 里**已经写着** `"Noto Serif CJK SC"`, 但仓库里没有任何 woff2、也没有 `public/fonts/` ⇒ 这一项一直在静默回退到系统宋体。 所以 P2 不是"引入一个新字族",而是"把已经声明了的名字变成真的存在"。 三件事,都有断言: ① 把子集 woff2 与两份 OFL 全文放进 `public/fonts/`(复制后**逐字节对拍**,OFL 必须同目录可达); ② 追加 `@font-face`(可变字体 200–900、`font-display: swap`); ③ 把 `"Novalon Serif SC"` 插到 `--font-serif` 队首,**保留原有系统兜底**(子集覆盖不到 CMS 新增中文)。 幂等:文件里已有本块的标记就拒绝第二次执行。 """ import hashlib import io import os HERE = os.path.dirname(os.path.abspath(__file__)) REPO = os.path.abspath(os.path.join(HERE, '..', '..')) OUT = os.path.join(HERE, 'staging-p2') MARK = 'novalon-serif-sc v1 (P2 自托管)' FILES = {'notoserifsc-subset.woff2': 'novalon-serif-sc-subset.woff2', 'OFL-NotoSerifSC.txt': 'OFL-NotoSerifSC.txt', 'OFL-LXGWWenKai.txt': 'OFL-LXGWWenKai.txt'} def sha(p): return hashlib.sha256(open(p, 'rb').read()).hexdigest()[:16] def main(): src = os.path.join(HERE, 'fonts') for a in FILES: assert os.path.exists(os.path.join(src, a)), '缺字体件 %s ⇒ 先生成子集再谈自托管' % a css = io.open(os.path.join(REPO, 'src', 'app', 'globals.css'), encoding='utf-8').read() old_line = [l for l in css.split('\n') if l.strip().startswith('--font-serif:')] assert len(old_line) == 1, '--font-serif 锚点命中 %d 行(应恰好 1)' % len(old_line) old_line = old_line[0] new_line = old_line.replace('"Songti SC"', '"Novalon Serif SC", "Songti SC"', 1) assert new_line != old_line, '队首插入失败:找不到 "Songti SC"' face = ('/* %s */\n' '@font-face {\n' ' font-family: "Novalon Serif SC";\n' ' src: url("/fonts/novalon-serif-sc-subset.woff2") format("woff2");\n' ' font-weight: 200 900; /* 可变字体:一个文件覆盖 200–900 */\n' ' font-display: swap; /* 首屏不被字体阻塞 */\n' ' font-style: normal;\n' ' /* 子集只覆盖源码实际用字(1050 码位);CMS 新增中文由后面的系统衬线兜底 */\n' '}\n') % MARK lines = ['#!/usr/bin/env bash', '# P2 思源宋体自托管(**单独一笔授权**)', '# 沙箱彩排:REPO_ROOT=<沙箱> bash staging-p2/fontsync.sh', 'set -euo pipefail', 'REPO_ROOT="${REPO_ROOT:?红:必须显式给出 REPO_ROOT}"', 'export SRC_DIR="${SRC_DIR:-%s}" # 必须 export:下面 python heredoc 读的是环境变量' % os.path.join(HERE, 'fonts'), 'cd "$REPO_ROOT"', 'test -f src/app/globals.css || { echo "红:找不到 src/app/globals.css"; exit 1; }', 'mkdir -p public/fonts', 'cp -p "$SRC_DIR/notoserifsc-subset.woff2" public/fonts/novalon-serif-sc-subset.woff2', 'cp -p "$SRC_DIR/OFL-NotoSerifSC.txt" public/fonts/OFL-NotoSerifSC.txt', 'cp -p "$SRC_DIR/OFL-LXGWWenKai.txt" public/fonts/OFL-LXGWWenKai.txt', 'python3 - <<\'PY\''] payload = ['import io, os, hashlib', "ROOT = os.environ['REPO_ROOT']", "SRC = os.environ['SRC_DIR']", 'FILES = %r' % FILES, 'MARK = %r' % MARK, 'FACE = %r' % face, 'OLD = %r' % old_line, 'NEW = %r' % new_line, 'bad = []', 'for a, b in FILES.items():', ' s = open(os.path.join(SRC, a), "rb").read()', ' d = open(os.path.join(ROOT, "public", "fonts", b), "rb").read()', ' if hashlib.sha256(s).hexdigest() != hashlib.sha256(d).hexdigest():', ' bad.append("%s 与源不逐字节相同 ⇒ 复制被半路改坏" % b)', 'p = os.path.join(ROOT, "src", "app", "globals.css")', 't = io.open(p, encoding="utf-8").read()', 'if MARK in t:', ' raise SystemExit("红:@font-face 块已存在 ⇒ 拒绝重复执行(P2 已落过)")', 'n = t.count(OLD)', 'assert n == 1, "--font-serif 锚点命中 %d 次,期望 1 ⇒ 令牌已被别的改动动过" % n', 't = t.replace(OLD, NEW, 1) + "\\n" + FACE', 'io.open(p, "w", encoding="utf-8").write(t)', 'if bad:', ' raise SystemExit("红:" + "; ".join(bad))', 'print("已放 3 个文件到 public/fonts/ 并逐字节对拍;--font-serif 队首已插入 Novalon Serif SC;@font-face 已追加")'] lines += payload + ['PY', 'echo "落库后请跑:npm run check:contrast && npm run type-check && npm run test:visual:all -- --update-snapshots=none"'] os.makedirs(OUT, exist_ok=True) out = os.path.join(OUT, 'fontsync.sh') io.open(out, 'w', encoding='utf-8').write("\n".join(lines) + "\n") os.chmod(out, 0o755) print('staging-p2/fontsync.sh 已写成(未执行)') print(' 字体件哈希:', ', '.join('%s=%s' % (k, sha(os.path.join(src, k))) for k in FILES)) print(' --font-serif 锚点唯一命中 ✓;标记 %r 用于幂等拒绝' % MARK) if __name__ == '__main__': main()