mirror of
https://github.com/YuleBest/FontMM.git
synced 2026-12-04 03:11:48 +08:00
fix(metrics): 收紧 head 包围盒, 消除段落上下多出来的空白 (issue #17)
用户实测仍不正常, 拿到问题字体后定位到: 它的 hhea/OS2 已被统一到 1.45 em, 但 head 包围盒是 2.856 em (y[-1048,1808]), 于是每段多出约 1.4 em 空白。 机制: 段落上下的空白不是行距, 而是 includeFontPadding (默认开启) 用 StaticLayout 的 mTopPadding = above - top / mBottomPadding = bottom - below 撑出来的, 其中 top/bottom 是 Skia 的 SkFontMetrics.fTop/fBottom, 取自 head 表的包围盒 —— 与 hhea/OS2 无关。包围盒被极少数大字形撑大的字体就会顶着这圈虚高空白。 - -line-metrics 顺带把 head.yMin/yMax **只收紧、不放大**到行距盒 (yMax <= ascent, yMin >= descender); 正常字体 (包围盒本就在行距盒内) 不动 - head 的目录校验和按 checkSumAdjustment 归零计算, 与该表自身约定一致 - 工具输出追加 ink_y=<旧>, 便于在日志里核对 - 实测问题字体: hhea 1.45 em 时段落上/下空白 647/759 (千分比 em) -> 归零; 改写后表校验和与 head.checkSumAdjustment 全部通过, FreeType/harfbuzz 正常解析 文档补充该机制 (DEVELOPMENT) 与用户说明 (README)。
This commit is contained in:
1 parent
05490f6e7c
commit
43b6a62fd7
3 files changed
+47
-6
No files matched your search
+11
-2
@@ -212,10 +212,18 @@ src/tools/fontmm-subset -check src/FONTS/hans.ttf # 应返回 cjk=<非0> /
|
||||
跳变与文字重叠)。只改度量字段,字形、`hmtx`、布局表原样保留,表长度不变所以无需重排文件;
|
||||
只有 `hhea` / `OS/2` 的表校验和与 `head.checkSumAdjustment` 需要重算。
|
||||
`FONTS/` 里的原始字体不动,改写只作用于装入的那一份。
|
||||
**同时收紧 `head` 包围盒**:段落上下的空白不是行距,而是
|
||||
`includeFontPadding`(默认开启)用 `mTopPadding = above - top`、`mBottomPadding =
|
||||
bottom - below` 撑出来的,其中 `top`/`bottom` 是 Skia 的 `SkFontMetrics.fTop/fBottom`,
|
||||
取自 **`head` 表的包围盒**(不是 `hhea`)。字体包围盒被极少数大字形撑大时(实测某个
|
||||
Sarasa 变体:hhea 1.45 em 而包围盒 2.86 em),每段会多出约 1.4 em 的空白 —— 这正是
|
||||
「段高离谱」的来源。因此改写时把包围盒**只收紧、不放大**到行距盒(`yMax ≤ ascent`、
|
||||
`yMin ≥ descender`),正常字体不受影响。
|
||||
**字体集合(`.ttc` / `.otc`)同样支持**:逐个 face 处理,并按表偏移去重(集合里多个 face
|
||||
常共享同一份 `hhea`/`OS/2`,重复改写会把已缩放的度量再缩一次);`checkSumAdjustment`
|
||||
先把所有 head 的该字段归零、对全文件求和一次,再写回同一个值。
|
||||
应用日志会逐字体回显旧/新度量,便于核对(不同 upem 数值不同,看 em 占比是否一致)。
|
||||
先把所有 head 的该字段归零、对全文件求和一次,再写回同一个值;`head` 的目录校验和按
|
||||
`checkSumAdjustment` 归零计算(与该表本身的约定一致)。
|
||||
应用日志会逐字体回显旧/新度量与包围盒,便于核对(不同 upem 数值不同,看 em 占比是否一致)。
|
||||
2. **度量载体**:`src/system/fonts/FontMM-Metrics.ttf`(16KB,由 Roboto Flex 挖空而来,只有
|
||||
度量与空格字形)插到各家族首位,让 paint 度量那一侧也一致。9 档字重全覆盖 —— 家族匹配是
|
||||
「取与请求字重最接近的条目」,只放 400 档的话粗体等仍会落到用户字体上。
|
||||
@@ -226,6 +234,7 @@ src/tools/fontmm-subset -check src/FONTS/hans.ttf # 应返回 cjk=<非0> /
|
||||
```bash
|
||||
fontmm-subset -line-metrics -in <font.ttf> -out <out.ttf> -line-total <permille>
|
||||
# 输出: line_metrics upem=1000 old=1020/-300 new=927/-273 total=1200
|
||||
# 包围盒被收紧时追加: ink_y=-1048/1808
|
||||
```
|
||||
|
||||
**载体来源与重新生成**(换度量来源只需换 `-in` 的文件):
|
||||
|
||||
@@ -94,6 +94,7 @@
|
||||
开启后模块会做两件事,让行距只由你选的档位决定,不再随字体变化:
|
||||
|
||||
- 把装入系统的字体(中英日韩等槽位字体、等宽、Emoji)的行距度量**按比例统一**到同一档位(字体上下的比例保持不变,字形完全不动;`FONTS/` 里的原始字体不动)
|
||||
- 同时收紧字体的「包围盒」——它决定段落**上下那圈空白**的大小。有些字体这个值虚高,会让每段多出一大圈留白,一并收掉
|
||||
- 额外把一个只含度量、没有文字字形的内置载体放到各家族最前面,让系统读取的那一侧也一致
|
||||
|
||||
档位(按字身框倍数):
|
||||
|
||||
@@ -240,7 +240,7 @@ static unsigned unitsPerEm(const unsigned char *data, size_t size) {
|
||||
// 支持字体集合 (.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) {
|
||||
int &oldDesc, int &newAsc, int &newDesc, int &oldYMin, int &oldYMax) {
|
||||
unsigned char *data = buf.data();
|
||||
size_t size = buf.size();
|
||||
|
||||
@@ -302,6 +302,27 @@ static bool scaleLineMetrics(std::vector<unsigned char> &buf, unsigned targetTot
|
||||
wrU16(data, os2.offset + 76, static_cast<uint16_t>(-nd));
|
||||
patched.push_back(os2.offset);
|
||||
}
|
||||
// 包围盒 (head.yMin/yMax) 不是行距, 但 Android 的 includeFontPadding 用它撑出
|
||||
// 段落上下空白 (StaticLayout: mTopPadding = above - top, top 来自 Skia 的
|
||||
// SkFontMetrics.fTop/fBottom, 而那两个值取自 head 的包围盒)。字体包围盒虚高时
|
||||
// (某些字体被极少数大字形撑大), 段落就会多出一大圈空白 —— 只收紧、不放大,
|
||||
// 避免把正常字体的包围盒撑得比实际墨迹还大。
|
||||
if (head.found && head.length >= 44 && !seen(patched, head.offset)) {
|
||||
const int yMin = rdI16(data, head.offset + 38);
|
||||
const int yMax = rdI16(data, head.offset + 42);
|
||||
if (oldYMax == 0 && oldYMin == 0) {
|
||||
oldYMin = yMin;
|
||||
oldYMax = yMax;
|
||||
}
|
||||
const int tightMax = yMax > na ? na : yMax;
|
||||
const int tightMin = yMin < nd ? nd : yMin;
|
||||
if (tightMax != yMax || tightMin != yMin) {
|
||||
wrI16(data, head.offset + 38, tightMin);
|
||||
wrI16(data, head.offset + 42, tightMax);
|
||||
patched.push_back(head.offset);
|
||||
}
|
||||
}
|
||||
|
||||
any = true;
|
||||
}
|
||||
|
||||
@@ -316,7 +337,14 @@ static bool scaleLineMetrics(std::vector<unsigned char> &buf, unsigned targetTot
|
||||
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));
|
||||
if (memcmp(data + dir, "head", 4) == 0) {
|
||||
// head 的目录校验和按 checkSumAdjustment 归零计算 (与字体本身一致)
|
||||
std::vector<unsigned char> copy(data + off, data + off + len);
|
||||
if (len >= 12) memset(©[8], 0, 4);
|
||||
wrU32(data, dir + 4, tableChecksum(copy.data(), len));
|
||||
} else {
|
||||
wrU32(data, dir + 4, tableChecksum(data + off, len));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -453,7 +481,8 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
unsigned target = static_cast<unsigned>(static_cast<long long>(upem) * lineTotal / 1000);
|
||||
int oldAsc = 0, oldDesc = 0, newAsc = 0, newDesc = 0;
|
||||
if (!scaleLineMetrics(buf, target, oldAsc, oldDesc, newAsc, newDesc)) {
|
||||
int oldYMin = 0, oldYMax = 0;
|
||||
if (!scaleLineMetrics(buf, target, oldAsc, oldDesc, newAsc, newDesc, oldYMin, oldYMax)) {
|
||||
fprintf(stderr, "[x] 改写行距度量失败 (不受支持的字体结构): %s\n", inPath);
|
||||
return 1;
|
||||
}
|
||||
@@ -462,8 +491,10 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
// 机器可读: 旧/新度量与每 em 占比, 供调用方在日志中核对
|
||||
printf("line_metrics upem=%u old=%d/%d new=%d/%d total=%u\n", upem, oldAsc, oldDesc,
|
||||
printf("line_metrics upem=%u old=%d/%d new=%d/%d total=%u", upem, oldAsc, oldDesc,
|
||||
newAsc, newDesc, target);
|
||||
if (oldYMin != 0 || oldYMax != 0) printf(" ink_y=%d/%d", oldYMin, oldYMax);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user