mirror of
https://github.com/YuleBest/FontMM.git
synced 2026-12-04 03:11:48 +08:00
fix(metrics): 行距统一支持字体集合 (.ttc/.otc) 并逐字体回显结果 (issue #17)
实测反馈: 某个字体的段高仍然离谱。原因是它作为字体集合 (.ttc/.otc) 被 -line-metrics 直接跳过 —— 而中文场景里 .ttc 相当常见, 跳过就等于没统一。 - sfntFaces(): 解析 ttcf 头, 逐个 face 处理; 按表偏移去重, 集合内多个 face 共享的 hhea/OS2 只改一次 (否则会把已缩放的度量再缩一次) - 表校验和: 更新所有引用被改表的目录项 (集合里可能多个 face 指向同一张表) - head.checkSumAdjustment: 先把所有 head 的该字段归零再对全文件求和一次, 写回同一个值 (原先逐个 head 累加, 集合多 head 时会算错) - apply.sh: 逐字体回显旧/新度量 (upem 不同则数值不同, 看 em 占比), 失败时带上工具输出; 跳过 0 字节占位文件 - 补充字库 (Plangothic/Tibetan/Braille 等) 有意不参与统一: 用户字体通常已覆盖日常用字, 为极少数生僻字每次应用多写 100MB+ 不划算, 且它们字形本就高, 压低会叠行 验证: .ttc (Noto CJK, 5 face 共享表) 与单体字体改写后表校验和、head 调整值全部通过, FreeType 正常解析; 端到端跑 apply.sh: 装入的 .ttc 字体由 1160/-288 变为 1161/-289 (1.45 em), 各 upem 字体按同一 em 占比统一。
This commit is contained in:
1 parent
51fceda51b
commit
05490f6e7c
3 files changed
+122
-51
No files matched your search
+6
-2
@@ -212,6 +212,10 @@ src/tools/fontmm-subset -check src/FONTS/hans.ttf # 应返回 cjk=<非0> /
|
||||
跳变与文字重叠)。只改度量字段,字形、`hmtx`、布局表原样保留,表长度不变所以无需重排文件;
|
||||
只有 `hhea` / `OS/2` 的表校验和与 `head.checkSumAdjustment` 需要重算。
|
||||
`FONTS/` 里的原始字体不动,改写只作用于装入的那一份。
|
||||
**字体集合(`.ttc` / `.otc`)同样支持**:逐个 face 处理,并按表偏移去重(集合里多个 face
|
||||
常共享同一份 `hhea`/`OS/2`,重复改写会把已缩放的度量再缩一次);`checkSumAdjustment`
|
||||
先把所有 head 的该字段归零、对全文件求和一次,再写回同一个值。
|
||||
应用日志会逐字体回显旧/新度量,便于核对(不同 upem 数值不同,看 em 占比是否一致)。
|
||||
2. **度量载体**:`src/system/fonts/FontMM-Metrics.ttf`(16KB,由 Roboto Flex 挖空而来,只有
|
||||
度量与空格字形)插到各家族首位,让 paint 度量那一侧也一致。9 档字重全覆盖 —— 家族匹配是
|
||||
「取与请求字重最接近的条目」,只放 400 档的话粗体等仍会落到用户字体上。
|
||||
@@ -245,9 +249,9 @@ Roboto Flex 为 OFL-1.1 且无保留字体名,许可文本见 `src/system/font
|
||||
**已知限制**:
|
||||
|
||||
- 只统一「会被安装替换」的字体。模块自带的生僻字 fallback(Plangothic / Tibetan /
|
||||
Braille 等)保持自身度量 —— 它们的字形本来就高(Tibetan 约 2.8 em、SatisarSharada
|
||||
Braille 等)**有意不统一**:用户所选字体通常已覆盖日常用字,为极少数生僻字每次应用
|
||||
多改写 100MB+ 文件不划算;且它们字形本来就高(Tibetan 约 2.8 em、SatisarSharada
|
||||
约 4.2 em),强行压低会叠行。含这些字形的行仍会比标准行更高。
|
||||
- 字体集合(`.ttc`)不是单体 sfnt,会被跳过(该字体保持原度量,不阻断安装)。
|
||||
- DenyList 应用里载体不会被预加载,行距退回字体自身(字体已被统一,因此仍是同一档)。
|
||||
|
||||
## 可复现构建
|
||||
|
||||
@@ -186,15 +186,35 @@ struct TableRef {
|
||||
bool found = false;
|
||||
};
|
||||
|
||||
static TableRef findTable(const unsigned char *data, size_t size, const char *tag) {
|
||||
TableRef ref;
|
||||
if (size < 12) return ref;
|
||||
// 各 face 的偏移表起点。单体 sfnt 返回 {0}; 字体集合 (ttcf, 即 .ttc/.otc) 返回每个 face。
|
||||
// 集合里的 face 共享底层表(通常连 head/hhea 都是同一份), 因此调用方需要按表偏移去重。
|
||||
static std::vector<size_t> sfntFaces(const unsigned char *data, size_t size) {
|
||||
std::vector<size_t> faces;
|
||||
if (size < 12) return faces;
|
||||
if (rdU32(data, 0) == 0x74746366u) { // 'ttcf'
|
||||
uint32_t numFonts = rdU32(data, 8);
|
||||
// 防止异常文件里的巨大值导致越界循环
|
||||
if (numFonts > 1024) return faces;
|
||||
for (uint32_t i = 0; i < numFonts; i++) {
|
||||
size_t p = 12 + i * 4;
|
||||
if (p + 4 > size) break;
|
||||
size_t off = rdU32(data, p);
|
||||
if (off + 12 <= size) faces.push_back(off);
|
||||
}
|
||||
return faces;
|
||||
}
|
||||
uint32_t version = rdU32(data, 0);
|
||||
// 只处理单体 sfnt (TrueType / CFF); 字体集合 (ttcf) 需按 face 索引解析, 暂不支持
|
||||
if (version != 0x00010000u && version != 0x4F54544Fu && version != 0x74727565u) return ref;
|
||||
unsigned numTables = rdU16(data, 4);
|
||||
if (version != 0x00010000u && version != 0x4F54544Fu && version != 0x74727565u) return faces;
|
||||
faces.push_back(0);
|
||||
return faces;
|
||||
}
|
||||
|
||||
static TableRef findTable(const unsigned char *data, size_t size, const char *tag, size_t face) {
|
||||
TableRef ref;
|
||||
if (face + 12 > size) return ref;
|
||||
unsigned numTables = rdU16(data, face + 4);
|
||||
for (unsigned i = 0; i < numTables; i++) {
|
||||
size_t dir = 12 + i * 16;
|
||||
size_t dir = face + 12 + i * 16;
|
||||
if (dir + 16 > size) return ref;
|
||||
if (memcmp(data + dir, tag, 4) != 0) continue;
|
||||
ref.dirOffset = dir;
|
||||
@@ -209,59 +229,103 @@ static TableRef findTable(const unsigned char *data, size_t size, const char *ta
|
||||
|
||||
// upem (head 表偏移 18 处的 uint16)
|
||||
static unsigned unitsPerEm(const unsigned char *data, size_t size) {
|
||||
TableRef head = findTable(data, size, "head");
|
||||
if (!head.found || head.length < 20) return 0;
|
||||
return rdU16(data, head.offset + 18);
|
||||
for (size_t face : sfntFaces(data, size)) {
|
||||
TableRef head = findTable(data, size, "head", face);
|
||||
if (head.found && head.length >= 20) return rdU16(data, head.offset + 18);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 把行距总量缩放到 targetTotal (字体单位), 上下比例保持; 返回是否改动
|
||||
// 把行距总量缩放到 targetTotal (字体单位), 上下比例保持; 返回是否改动。
|
||||
// 支持字体集合 (.ttc/.otc): 逐个 face 处理, 按表偏移去重 —— 集合里多个 face 常共享
|
||||
// 同一份 hhea/OS/2, 重复改写会把已经缩放的度量再缩放一次。
|
||||
static bool scaleLineMetrics(std::vector<unsigned char> &buf, unsigned targetTotal, int &oldAsc,
|
||||
int &oldDesc, int &newAsc, int &newDesc) {
|
||||
unsigned char *data = buf.data();
|
||||
size_t size = buf.size();
|
||||
|
||||
TableRef hhea = findTable(data, size, "hhea");
|
||||
if (!hhea.found || hhea.length < 10) return false;
|
||||
std::vector<size_t> faces = sfntFaces(data, size);
|
||||
if (faces.empty()) return false;
|
||||
|
||||
const int asc = rdI16(data, hhea.offset + 4);
|
||||
const int desc = rdI16(data, hhea.offset + 6);
|
||||
const int gap = rdI16(data, hhea.offset + 8);
|
||||
oldAsc = asc;
|
||||
oldDesc = desc;
|
||||
auto seen = [](const std::vector<size_t> &v, size_t off) {
|
||||
for (size_t x : v) {
|
||||
if (x == off) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// 原始总量: 上升部 + 下降部 + 行间隙 (lineGap 归零后并入)
|
||||
const int total = asc - desc + gap;
|
||||
if (total <= 0) return false;
|
||||
std::vector<size_t> patched; // 已改写的 hhea / OS2 表偏移
|
||||
std::vector<size_t> headOffsets; // 待重算 checkSumAdjustment 的 head 表偏移
|
||||
bool any = false, reported = false;
|
||||
|
||||
// 按原始上下比例分配目标总量 (比例保持不变 → 基线位置不跳变)
|
||||
const int upper = asc + gap; // lineGap 视为上升部的一部分
|
||||
newAsc = static_cast<int>(static_cast<long long>(targetTotal) * upper / total);
|
||||
newDesc = newAsc - static_cast<int>(targetTotal);
|
||||
if (newAsc <= 0 || newDesc >= 0) return false;
|
||||
for (size_t face : faces) {
|
||||
TableRef head = findTable(data, size, "head", face);
|
||||
if (head.found && !seen(headOffsets, head.offset)) headOffsets.push_back(head.offset);
|
||||
|
||||
wrI16(data, hhea.offset + 4, newAsc);
|
||||
wrI16(data, hhea.offset + 6, newDesc);
|
||||
wrI16(data, hhea.offset + 8, 0); // lineGap 统一归零
|
||||
TableRef hhea = findTable(data, size, "hhea", face);
|
||||
if (!hhea.found || hhea.length < 10 || seen(patched, hhea.offset)) continue;
|
||||
|
||||
// OS/2 (版本 0 起字段位置一致): sTypoAscender/Descender/LineGap 与 usWinAscent/Descent
|
||||
TableRef os2 = findTable(data, size, "OS/2");
|
||||
if (os2.found && os2.length >= 74) {
|
||||
wrI16(data, os2.offset + 68, newAsc);
|
||||
wrI16(data, os2.offset + 70, newDesc);
|
||||
wrI16(data, os2.offset + 72, 0);
|
||||
wrU16(data, os2.offset + 74, static_cast<uint16_t>(newAsc));
|
||||
wrU16(data, os2.offset + 76, static_cast<uint16_t>(-newDesc));
|
||||
wrU32(data, os2.dirOffset + 4, tableChecksum(data + os2.offset, os2.length));
|
||||
const int asc = rdI16(data, hhea.offset + 4);
|
||||
const int desc = rdI16(data, hhea.offset + 6);
|
||||
const int gap = rdI16(data, hhea.offset + 8);
|
||||
|
||||
// 原始总量: 上升部 + 下降部 + 行间隙 (行间隙归零后并入上升部)
|
||||
const int total = asc - desc + gap;
|
||||
if (total <= 0) continue;
|
||||
|
||||
// 按原始上下比例分配目标总量 (比例保持不变 → 基线位置不跳变)
|
||||
const int upper = asc + gap;
|
||||
const int na = static_cast<int>(static_cast<long long>(targetTotal) * upper / total);
|
||||
const int nd = na - static_cast<int>(targetTotal);
|
||||
if (na <= 0 || nd >= 0) continue;
|
||||
|
||||
if (!reported) {
|
||||
oldAsc = asc;
|
||||
oldDesc = desc;
|
||||
newAsc = na;
|
||||
newDesc = nd;
|
||||
reported = true;
|
||||
}
|
||||
|
||||
wrI16(data, hhea.offset + 4, na);
|
||||
wrI16(data, hhea.offset + 6, nd);
|
||||
wrI16(data, hhea.offset + 8, 0);
|
||||
patched.push_back(hhea.offset);
|
||||
|
||||
// OS/2 (版本 0 起字段位置一致): sTypo 与 usWin 一起改, 避免不同渲染路径取到不同值
|
||||
TableRef os2 = findTable(data, size, "OS/2", face);
|
||||
if (os2.found && os2.length >= 78 && !seen(patched, os2.offset)) {
|
||||
wrI16(data, os2.offset + 68, na);
|
||||
wrI16(data, os2.offset + 70, nd);
|
||||
wrI16(data, os2.offset + 72, 0);
|
||||
wrU16(data, os2.offset + 74, static_cast<uint16_t>(na));
|
||||
wrU16(data, os2.offset + 76, static_cast<uint16_t>(-nd));
|
||||
patched.push_back(os2.offset);
|
||||
}
|
||||
any = true;
|
||||
}
|
||||
|
||||
wrU32(data, hhea.dirOffset + 4, tableChecksum(data + hhea.offset, hhea.length));
|
||||
if (!any) return false;
|
||||
|
||||
// head.checkSumAdjustment: 归零后对全文件求和, 再取补
|
||||
TableRef head = findTable(data, size, "head");
|
||||
if (!head.found || head.length < 12) return false;
|
||||
wrU32(data, head.offset + 8, 0);
|
||||
const uint32_t sum = tableChecksum(data, size);
|
||||
wrU32(data, head.offset + 8, 0xB1B0AFBAu - sum);
|
||||
// 表校验和: 更新所有引用了被改写表的目录项 (集合里可能多个 face 指向同一张表)
|
||||
for (size_t face : faces) {
|
||||
unsigned numTables = rdU16(data, face + 4);
|
||||
for (unsigned i = 0; i < numTables; i++) {
|
||||
size_t dir = face + 12 + i * 16;
|
||||
if (dir + 16 > size) break;
|
||||
size_t off = rdU32(data, dir + 8);
|
||||
size_t len = rdU32(data, dir + 12);
|
||||
if (!seen(patched, off) || off + len > size) continue;
|
||||
wrU32(data, dir + 4, tableChecksum(data + off, len));
|
||||
}
|
||||
}
|
||||
|
||||
// head.checkSumAdjustment: 先把所有 head 的该字段归零, 再对全文件求和一次,
|
||||
// 最后写回同一个值 —— 集合 (.ttc) 里可能有多个 head, 逐个头累加会因为前一个
|
||||
// 已经写入的值而算错。
|
||||
for (size_t ho : headOffsets) wrU32(data, ho + 8, 0);
|
||||
const uint32_t adjustment = 0xB1B0AFBAu - tableChecksum(data, size);
|
||||
for (size_t ho : headOffsets) wrU32(data, ho + 8, adjustment);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+7
-4
@@ -188,15 +188,18 @@ unify_one() {
|
||||
local target="$1" file="$2"
|
||||
local tool="$MODDIR/tools/fontmm-subset"
|
||||
local tmp="$MODDIR/.line-metrics.tmp"
|
||||
local out=""
|
||||
|
||||
[ -f "$file" ] || return 0
|
||||
if run_tool "$tool" -line-metrics -in "$file" -out "$tmp" -line-total "$target" >/dev/null 2>&1; then
|
||||
[ -s "$file" ] || return 0 # 空占位文件跳过
|
||||
if out=$(run_tool "$tool" -line-metrics -in "$file" -out "$tmp" -line-total "$target" 2>&1); then
|
||||
mv -f "$tmp" "$file"
|
||||
# 逐字体回显旧/新度量, 便于实机核对 (upem 不同则数值不同, 看 em 占比)
|
||||
echo "[*] $(basename "$file"): $(echo "$out" | tr '\n' ' ' | sed 's/line_metrics //')"
|
||||
return 0
|
||||
fi
|
||||
# 字体结构不受支持 (例如字体集合) 时不阻断: 该字体保持原度量
|
||||
# 不受支持的字体不阻断安装: 该字体保持原度量
|
||||
rm -f "$tmp"
|
||||
echo "[-] $(basename "$file") 行距度量未统一 (字体结构不受支持), 保持原样"
|
||||
echo "[!] $(basename "$file") 行距度量未统一: $(echo "$out" | tr '\n' ' ')"
|
||||
return 1
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user