refactor(metrics): 度量载体改为内置 Roboto Flex, 去掉设备端生成 (issue #17)

原方案在设备上按候选列表挖空系统字体, 依赖设备端字体文件名与是否存在, 既复杂又脆。
改为从 Google Fonts 取 Roboto Flex 裁出只有度量的载体 (1.79MB -> 16KB) 直接入库:

- src/system/fonts/FontMM-Metrics.ttf: 度量 1900/-500 (upem 2048) = 1.171 em,
  即 AOSP Roboto 拉丁基线; 字符集只剩 U+0020/U+00A0; OFL-1.1 且无保留字体名,
  许可文本随包附上 (LICENSE-OFL-RobotoFlex)
- apply.sh 只引用该文件并按开关调 fontmm-wght 插/移条目, 设备端不再生成字体:
  删掉候选字体列表、挖空步骤与相应失败分支; 关闭开关也不再删载体 (它是模块自带文件)
- fontmm-subset -metrics 保留为维护端工具, 重新生成命令写进 DEVELOPMENT.md
- dev/ci.mjs 增加「载体文件必须存在」与「不得被占位脚本清空」两项检查
- README / DEVELOPMENT 措辞同步
This commit is contained in:
YuleBest committed 2026-09-16 16:53:37 +08:00
1 parent 176263e400
commit 05ddf554c2
8 files changed
+141 -59

No files matched your search

+20 -15
View File
@@ -203,25 +203,30 @@ src/tools/fontmm-subset -check src/FONTS/hans.ttf # 应返回 cjk=<非0> /
`min(ascent)` / `max(descent)`)。本模块 `fonts.xml` 中 `sans-serif` 首位正是「英文 & 数字」
槽位字体,所以换英文字体(连带中文行距)都会跟着变。
**做法**:把一份现成字体**挖空**成只有度量、没有文字字形的载体,插到各家族首位。9 档字重
必须全覆盖:家族匹配是「取与请求字重最接近的条目」,只放 400 档的话粗体等仍会落到用户字体上。
**做法**:用一个只有度量、没有文字字形的载体字体(`src/system/fonts/FontMM-Metrics.ttf`,
16KB),插到各家族首位当基准。9 档字重必须全覆盖:家族匹配是「取与请求字重最接近的条目」,
只放 400 档的话粗体等仍会落到用户字体上。
载体来源按优先级取系统自带、且不被本模块覆盖的字体:
载体由 **Roboto Flex**(Google Fonts,OFL-1.1,无保留字体名)裁出,度量 `1900 / -500`
(upem 2048)= 1.171 em,即 AOSP 的 Roboto 拉丁基线;字符集只剩 U+0020 / U+00A0。
文件直接入库(与 `NotoColorEmoji.ttf` 等内置 fallback 字体同样的做法),设备端不做任何生成,
许可文本见 `src/system/fonts/LICENSE-OFL-RobotoFlex`。
| 顺序 | 路径 | 度量(hhea) |
| ---- | ---- | ------------ |
| 1 | `/system/fonts/NotoSansCJKjp-Regular.otc`(兼容 `-Regular.ttc` / `-sc-Regular.otf`) | 1160 / -288 = 1.448 em(AOSP 中文基线) |
| 2 | `/system/fonts/Roboto-Regular.ttf` | 1900 / -500 = 1.171 em(拉丁基线) |
需要重新生成或换度量基准时(改度量来源只需换 `-in` 的文件):
挖空由 `fontmm-subset -metrics` 完成:Noto CJK 从 19.6MB 变 2.2KB,`hhea` 度量与空格字宽
原样保留,字符集只剩 U+0020 / U+00A0 / U+3000。文字仍由用户选择的字体渲染,载体只贡献
行距与空格字宽。
```bash
curl -Lo /tmp/RobotoFlex.ttf 'https://github.com/google/fonts/raw/main/ofl/robotoflex/RobotoFlex%5BGRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght%5D.ttf'
g++ -O1 -std=c++17 -DHB_NO_MT -I dev/.cache/harfbuzz/harfbuzz-*/src \
dev/.cache/harfbuzz/harfbuzz-*/src/harfbuzz-subset.cc \
native/src/subset/fontmm-subset.cc -o /tmp/fontmm-subset-host
/tmp/fontmm-subset-host -metrics -in /tmp/RobotoFlex.ttf -out src/system/fonts/FontMM-Metrics.ttf
```
**分工**:`apply.sh` 读开关 `FONTS/metrics.txt`(WebUI 写)→ 生成载体到
`system/fonts/FontMM-Metrics.ttf` → 调 `fontmm-wght -mode 0 -metrics on|off -sync` 插入/移除
条目(`auto` 表示由工具自己读开关文件)。`on` 但载体文件不存在时不插入条目,避免
`fonts.xml` 引用缺失字体;关闭是幂等的,能把 XML 逐字节还原(Go 单测覆盖)。
`dev/ci.mjs` 校验载体文件名在 `apply.sh` 与 Go 两侧一致。
**分工**:`apply.sh` 读开关 `FONTS/metrics.txt`(WebUI 写)→ 调
`fontmm-wght -mode 0 -metrics on|off -sync` 插入/移除载体条目(`auto` 表示由工具自己读
开关文件)。载体缺失时不插入条目,避免 `fonts.xml` 引用缺失字体;关闭是幂等的,能把 XML
逐字节还原(Go 单测覆盖)。`dev/ci.mjs` 校验载体文件名在 `apply.sh` 与 Go 两侧一致、
载体文件确实随仓库提供。
**已知限制**:只能把行距钉在一个**下限**——所选字体度量比载体大时,该行仍取字体自身的
度量(并集语义决定),行距不会因此变小。另外 DenyList 应用里载体不会被预加载,行距退回字体自身。
+3 -3
View File
@@ -91,9 +91,9 @@
安卓的行高由「字体家族里与请求字重最接近的那个字体」与「每行实际用到的字体」共同决定 —— 本模块的配置里排在 `sans-serif` 最前面的正是「英文 & 数字」槽位,所以**换英文字体时,中文行距也会跟着变**。
开启后,模块会把系统自带字体**挖空**成一个只有度量、没有文字字形的载体,插到各家族最前面当基准:行距固定在系统标准上,换字体不再影响它,空格宽度也随之统一。
开启后,模块会把内置的度量字体(由 Roboto Flex 裁出,只有度量、没有文字字形)插到各家族最前面当基准:行距固定在标准值上,换字体不再影响它,空格宽度也随之统一。
- 载体只有 2KB 上下;文字仍然由你选择的字体渲染,它只提供行距与空格宽度
- 载体只有十几 KB;文字仍然由你选择的字体渲染,它只提供行距与空格宽度
- 默认关闭;切换后需要重新点一次「应用」才生效
- 它只能把行距**钉在一个下限**:若所选字体本身的行距比系统标准还大,那一行仍以该字体为准
@@ -149,7 +149,7 @@ A:不会。裁剪会完整保留可变字体的字重轴,字重覆写不受
**Q:换了字体后行距看起来和上一款不一样?**
A:行距由字体自身的度量决定,换字体就会跟着变。开启「高级配置 → 固定行距 & 字距」后会以系统自带的度量为基准,换字体不再改变行距。
A:行距由字体自身的度量决定,换字体就会跟着变。开启「高级配置 → 固定行距 & 字距」后会以模块内置的标准度量为基准,换字体不再改变行距。
## 致谢
+15
View File
@@ -210,6 +210,21 @@ await check('度量载体与开关文件名一致 (apply.sh / Go / WebUI)', asyn
throw new Error(`载体文件名不一致: apply.sh=${carrier[1]}, Go=${goCarrier[1]}`);
}
// 载体随模块打包, 缺失时开关会静默失效
const carrierFile = path.join(SRC_DIR, 'system', 'fonts', carrier[1]);
try {
const { size } = await fsp.stat(carrierFile);
if (size === 0) throw new Error('文件为空');
} catch (e) {
throw new Error(`缺少度量载体 ${path.relative(ROOT, carrierFile)} (${e.message})`);
}
// 占位字体脚本会把列表内的文件清空, 载体不能被卷进去
const emptyFont = await fsp.readFile(path.join(ROOT, 'dev', 'empty-font.mjs'), 'utf8');
if (emptyFont.includes(carrier[1])) {
throw new Error(`dev/empty-font.mjs 的占位列表包含度量载体 ${carrier[1]}, 会被清空`);
}
// 开关文件名: apply.sh 与 Go 读、WebUI 写, 三处必须同名
const sh = /\$\{?FONTS_DIR\}?\/metrics\.txt/.test(applySh);
const golang = /"FONTS", "metrics\.txt"/.test(go);
+6 -37
View File
@@ -165,50 +165,19 @@ echo "[*] 处理行距字距..."
# 固定行距/字距 (issue #17): 行高取「paint 度量」与「该行使用字体度量」的并集, 而 paint
# 度量来自家族里与请求字重最接近的条目 —— 本模块 fonts.xml 中 sans-serif 首位正是
# 英文槽位字体, 因此换字体 (连带中文行距) 都会变。开启后把一个只有度量、没有文字
# 字形的载体放到家族首位, 行距就钉在系统自带的这套标准上。
# 载体来源: 系统自带的现成字体 (优先中文标准 Noto CJK, 退回 Roboto), 现场挖空。
# 字形的载体放到家族首位, 行距就钉在模块内置的这套标准上 (Roboto Flex 度量)。
# 载体随模块打包 (src/system/fonts/), 无需设备端生成; 这里只按开关插/移条目。
METRICS_CARRIER="$SYS_FONT_DIR/FontMM-Metrics.ttf"
build_metrics_carrier() {
local tool="$MODDIR/tools/fontmm-subset"
local src="" f=""
for f in \
/system/fonts/NotoSansCJKjp-Regular.otc \
/system/fonts/NotoSansCJK-Regular.ttc \
/system/fonts/NotoSansCJKsc-Regular.otf \
/system/fonts/Roboto-Regular.ttf; do
[ -f "$f" ] && src="$f" && break
done
if [ -z "$src" ]; then
echo "[-] 系统里没有可用作度量的字体, 跳过固定行距字距"
return 1
fi
if [ ! -f "$tool" ]; then
echo "[-] 未找到字体工具, 跳过固定行距字距"
return 1
fi
echo "[*] 挖空 $(basename "$src") 作为度量载体..."
if ! run_tool "$tool" -metrics -in "$src" -out "$METRICS_CARRIER" 2>&1; then
echo "[!] 生成度量载体失败, 跳过固定行距字距"
rm -f "$METRICS_CARRIER"
return 1
fi
return 0
}
# 开关由 WebUI 写在 FONTS/metrics.txt (随 FONTS 一起保留)
METRICS_MODE="off"
if [ "$(cat "$FONTS_DIR/metrics.txt" 2>/dev/null)" = "1" ]; then
if [ ! -f "$METRICS_CARRIER" ]; then
echo "[-] 缺少度量载体 $METRICS_CARRIER, 跳过固定行距字距"
elif [ "$(cat "$FONTS_DIR/metrics.txt" 2>/dev/null)" = "1" ]; then
METRICS_MODE="on"
if ! build_metrics_carrier; then
METRICS_MODE="off"
fi
echo "[*] 已启用固定行距字距 (载体: $(basename "$METRICS_CARRIER"))"
else
echo "[-] 未启用固定行距字距"
rm -f "$METRICS_CARRIER"
fi
# 按开关插/移载体条目 (mode 0 不动字重, 与 WebUI 的字重覆写互不影响)
Binary file not shown.
+93
View File
@@ -0,0 +1,93 @@
Copyright 2017 The Roboto Flex Project Authors (https://github.com/TypeNetwork/Roboto-Flex)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
+1 -1
View File
@@ -150,7 +150,7 @@
<div class="wght-mode-info">
<div class="wght-mode-title">固定行距 &amp; 字距</div>
<div class="wght-mode-sub" id="metrics-sub">
把系统字体的行距字距度量挖出来当基准,换字体后行距不再跟着变。开启后需重新「应用」。
用内置的度量字体把行距字距钉在标准值上,换字体后行距不再跟着变。开启后需重新「应用」。
</div>
</div>
<md-switch id="metrics-switch" aria-label="固定行距字距"></md-switch>
+3 -3
View File
@@ -4,9 +4,9 @@
// 度量来自 typeface 家族里与请求字重最接近的那个条目 —— 本模块的 fonts.xml 中
// sans-serif 首个条目正是英文槽位字体, 于是换英文字体 (进而中文行距) 都会跟着变。
//
// 做法: 让 apply.sh 把系统自带的现成字体挖空 (只留度量与空格字形), 作为家族首位
// 的度量载体; 开关记在 FONTS/metrics.txt, 由 apply.sh 里的 fontmm-wght 按开关
// 插入/移除载体条目。
// 做法: 模块内置一个只有度量、没有文字字形的载体字体 (FontMM-Metrics.ttf, 由
// Roboto Flex 裁出), 开启后由 apply.sh 里的 fontmm-wght 把它插到各家族首位当基准。
// 开关记在 FONTS/metrics.txt。
import '@material/web/switch/switch.js';
import { exec, toast } from './ksu';
import { FONTS_DIR } from './constants';