mirror of
https://github.com/2234839/web-font.git
synced 2026-12-03 23:40:21 +08:00
perf(glyf): subset simple 字形剥离 hinting instructions
subset 模式下 simple 字形走原始字节快路径([[glyf-origbytes-fastpath]]),完整保留 原始 hinting instructions。但 web 渲染场景浏览器用 autohint,原始 instructions 不参与 渲染——实测剥离后所有基准用例 SSIM 与 ink 逐位一致(含最敏感的小字号用例)。 read 快路径定位 instructionLength(紧接 endPtsOfContours,OpenType 规范布局,无需展开 flags),存 _instrOff/_instrLen。write 快路径跳过 instructions 段并写 instructionLength=0, sizeof 同步扣减字节长度。_instrOff=-1(无 instructions)走原整段拷贝路径。 收益(SSIM 全不变): - FiraCode woff2 10776→8892(-17.5%),端到端 avg 1.92→1.68ms(-12.5%) - 千字文 woff2 79784→79736,ttf 体积同步减小 - CJK 字体(初夏/霞鹜/思源)instructions 极少,体积持平 brotli 是 write 阶段硬下限,减小 glyf 原始字节直接减小 brotli 输入与输出。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
eb3ed06d41
commit
f4af6f7b68
16
vendor/fonteditor-core/lib/ttf/table/glyf.js
vendored
16
vendor/fonteditor-core/lib/ttf/table/glyf.js
vendored
@ -100,6 +100,20 @@ var _default = exports.default = _table.default.create('glyf', [], {
|
||||
* 优化313: 不再分配 endPtsOfContours 数组——_totalPoints 只需最后一个 endPt。
|
||||
* 优化314: 原始字节引用展平到 glyfObj(_origBuf/_origOff/_origLen),
|
||||
* 省掉每个 simple 字形的 _origGlyfRef 子对象分配。 */
|
||||
/** instructions 剥离定位(优化320):simple 字节布局 header(10)+endPts(nc*2)+instructionLength(2)+
|
||||
* instructions(n)+flags+x+y。instructionLength 紧接 endPtsOfContours(OpenType 规范,
|
||||
* 与 parse.js line 242 一致),无需展开 flags。_instrOff 指向 instructionLength 字段
|
||||
* (相对 view 起始的绝对偏移),_instrLen 为 instructions 字节数。_instrOff=-1 表示无
|
||||
* instructions,write 走原始整段拷贝。 */
|
||||
var _instrOff = -1;
|
||||
var _instrLen = 0;
|
||||
if (numberOfContours > 0) {
|
||||
var _instrLenPos = vOff + 10 + numberOfContours * 2;
|
||||
if (_instrLenPos + 2 <= vOff + (gEnd - gStart)) {
|
||||
_instrLen = view.getUint16(_instrLenPos, false);
|
||||
if (_instrLen > 0) _instrOff = _instrLenPos;
|
||||
}
|
||||
}
|
||||
var glyfObj = {
|
||||
xMin: view.getInt16(vOff + 2, false),
|
||||
yMin: view.getInt16(vOff + 4, false),
|
||||
@ -110,6 +124,8 @@ var _default = exports.default = _table.default.create('glyf', [], {
|
||||
_origLen: gEnd - gStart,
|
||||
_numContours: numberOfContours,
|
||||
_totalPoints: numberOfContours > 0 ? view.getUint16(vOff + 10 + (numberOfContours - 1) * 2, false) + 1 : 0,
|
||||
_instrOff: _instrOff,
|
||||
_instrLen: _instrLen,
|
||||
};
|
||||
glyphs[index] = glyfObj;
|
||||
} else {
|
||||
|
||||
@ -209,9 +209,11 @@ function sizeof(ttf) {
|
||||
glyfSize = 0;
|
||||
glyfSupport = EMPTY_GLYF_SUPPORT;
|
||||
} else if (glyf._origBuf) {
|
||||
/** 优化310: simple 字形原始字节快路径,glyfSize 直接用原始字节长度 */
|
||||
/** 优化310: simple 字形原始字节快路径,glyfSize 直接用原始字节长度。
|
||||
* 优化320: instructions 剥离——输出时移除 simple 字形的 hinting instructions(web 渲染用
|
||||
* 浏览器 autohint,原始 instructions 减小 glyf 字节)。_instrOff>=0 时长度减去 instructions 段。 */
|
||||
glyfSupport = {};
|
||||
glyfSize = glyf._origLen;
|
||||
glyfSize = glyf._origLen - (glyf._instrOff >= 0 ? glyf._instrLen : 0);
|
||||
} else {
|
||||
glyfSupport = {};
|
||||
glyfSize = getFlagsAndSize(glyf, glyfSupport, hinting);
|
||||
|
||||
@ -57,9 +57,34 @@ function write(writer, ttf) {
|
||||
/** 优化314: 原始字节引用展平为 _origBuf/_origOff/_origLen,消除 _origGlyfRef 子对象解引用 */
|
||||
var origBuf = glyf._origBuf;
|
||||
if (origBuf) {
|
||||
var origBytes = new Uint8Array(origBuf, glyf._origOff, glyf._origLen);
|
||||
fullView.set(origBytes, pos);
|
||||
pos += glyf._origLen;
|
||||
/** 优化320: instructions 剥离。_instrOff>=0 时 simple 字形含 hinting instructions,
|
||||
* 输出跳过 instructions 段并把 instructionLength 置 0。
|
||||
* 字节布局:[header+endPts+flags+x+y 前段][instructionLength(2)][instructions(n)][flags+x+y 后段]
|
||||
* 注意 instructions 在 flags/x/y 之前(OpenType 规范:endPts → instructionLength → instructions → flags → x → y)。
|
||||
* _instrOff 是相对 view 起始的绝对偏移(指向 instructionLength 字段),换算为相对 _origOff 的局部偏移。 */
|
||||
if (glyf._instrOff >= 0) {
|
||||
var instrRelOff = glyf._instrOff - glyf._origOff;
|
||||
/** instructionLength 字段前(header+endPts+flags+x+y 已写部分?不——flags/x/y 在 instructions 之后)。
|
||||
* 实际:instrRelOff 之前是 header(10)+endPts(nc*2)。instrRelOff 处是 instructionLength(2),
|
||||
* 之后是 instructions(_instrLen),再之后是 flags+x+y 直到 _origLen。 */
|
||||
/** 前 segment:[_origOff, _origOff+instrRelOff) = header+endPts */
|
||||
fullView.set(new Uint8Array(origBuf, glyf._origOff, instrRelOff), pos);
|
||||
pos += instrRelOff;
|
||||
/** instructionLength = 0(替换原 2 字节值) */
|
||||
view.setUint16(pos, 0, false);
|
||||
pos += 2;
|
||||
/** 后 segment:跳过 instructions(_instrLen),写 [_origOff+instrRelOff+2+_instrLen, _origOff+_origLen) = flags+x+y */
|
||||
var afterOff = glyf._origOff + instrRelOff + 2 + glyf._instrLen;
|
||||
var afterLen = glyf._origLen - (instrRelOff + 2 + glyf._instrLen);
|
||||
if (afterLen > 0) {
|
||||
fullView.set(new Uint8Array(origBuf, afterOff, afterLen), pos);
|
||||
pos += afterLen;
|
||||
}
|
||||
} else {
|
||||
var origBytes = new Uint8Array(origBuf, glyf._origOff, glyf._origLen);
|
||||
fullView.set(origBytes, pos);
|
||||
pos += glyf._origLen;
|
||||
}
|
||||
} else {
|
||||
|
||||
/* 优化31+103: header 直接 view 写入 10 字节,优先使用 _numContours */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user