mirror of
https://github.com/2234839/web-font.git
synced 2026-09-04 23:02:27 +08:00
perf(woff2): ttf→woff2 跳过 TTF checksum 计算
woff2 重新编码,不消费 TTF directory 的 per-table checksum 与 head.checkSumAdjustment(encodeTTFToWOFF2 只读 directory 的 tag/offset/length, 重建自己的 directory)。TTFWriter 加 skipCheckSum 选项,font.write 的 woff2 分支传入,跳过 checkSumArrayBuffer 调用 + directory 回填 + head adjustment。 node --prof 思源 3500 字大字集:checkSumArrayBuffer 5.1%(JS nonlib 第二大) 从热点列表消失。端到端 min -2%(brotli 主导稀释收益),但消除的是语义上 纯无用的计算(被 woff2 丢弃)。head.checkSumAdjustment 保持 0 占位(resolveTTF 已置 0),浏览器渲染不依赖该字段。 ttf 直出(消费 directory checksum)与 woff(ttf2woff 读 directory checksum 写 WOFF 表目录)分支不受影响,仅 woff2 生效。 基准测试全 SSIM 与历史固有值一致(FiraCode 0.9923/初夏纯标点 0.9920/篆体 0.9876 等),woff2 输出字节 IDENTICAL(思源千字文中段 17052/FiraCode 8892/ 霞鹜 3840/初夏 6164/6864/得意黑 2532/1628)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
2ed666ec58
commit
67d2eb5fa5
3
vendor/fonteditor-core/lib/ttf/font.js
vendored
3
vendor/fonteditor-core/lib/ttf/font.js
vendored
@ -171,7 +171,8 @@ var Font = exports.Font = /*#__PURE__*/function () {
|
|||||||
buffer = new _ttfwriter.default(options).write(this.data);
|
buffer = new _ttfwriter.default(options).write(this.data);
|
||||||
buffer = (0, _ttf2woff.default)(buffer, options);
|
buffer = (0, _ttf2woff.default)(buffer, options);
|
||||||
} else if (options.type === 'woff2') {
|
} else if (options.type === 'woff2') {
|
||||||
buffer = new _ttfwriter.default(options).write(this.data);
|
/** 优化316: woff2 重新编码,不消费 TTF directory checksum 与 head.checkSumAdjustment,跳过计算省大字集 ~5% */
|
||||||
|
buffer = new _ttfwriter.default(Object.assign({}, options, { skipCheckSum: true })).write(this.data);
|
||||||
buffer = (0, _ttftowoff.default)(buffer, options);
|
buffer = (0, _ttftowoff.default)(buffer, options);
|
||||||
} else if (options.type === 'svg') {
|
} else if (options.type === 'svg') {
|
||||||
buffer = (0, _ttf2svg.default)(this.data, options);
|
buffer = (0, _ttf2svg.default)(this.data, options);
|
||||||
|
|||||||
39
vendor/fonteditor-core/lib/ttf/ttfwriter.js
vendored
39
vendor/fonteditor-core/lib/ttf/ttfwriter.js
vendored
@ -28,7 +28,12 @@ var TTFWriter = exports.default = /*#__PURE__*/function () {
|
|||||||
writeZeroContoursGlyfData: options.writeZeroContoursGlyfData || false,
|
writeZeroContoursGlyfData: options.writeZeroContoursGlyfData || false,
|
||||||
hinting: options.hinting || false,
|
hinting: options.hinting || false,
|
||||||
kerning: options.kerning || false,
|
kerning: options.kerning || false,
|
||||||
support: options.support
|
support: options.support,
|
||||||
|
/** 优化316: woff2/woff/eot 输出会重新编码,不消费 TTF directory 的 per-table checksum 与
|
||||||
|
* head.checkSumAdjustment(encodeTTFToWOFF2 只读 directory 的 tag/offset/length,重建自己的 directory)。
|
||||||
|
* 跳过 checksum 计算可省大字集下 ~5% 的 checkSumArrayBuffer 开销。head.checkSumAdjustment 保持 0 占位
|
||||||
|
* (resolveTTF 已置 0),浏览器渲染不依赖该字段。仅 ttf 直出(消费 directory checksum)时不能跳过。 */
|
||||||
|
skipCheckSum: options.skipCheckSum || false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,10 +141,12 @@ var TTFWriter = exports.default = /*#__PURE__*/function () {
|
|||||||
* 优化184: 内联 writeEmpty 为 fullView.fill(0),避免 writer.writeEmpty 的函数调用 + 边界检查开销 */
|
* 优化184: 内联 writeEmpty 为 fullView.fill(0),避免 writer.writeEmpty 的函数调用 + 边界检查开销 */
|
||||||
var supportTableList = ttf.support.tables;
|
var supportTableList = ttf.support.tables;
|
||||||
var buf = writer.getBuffer();
|
var buf = writer.getBuffer();
|
||||||
|
/** 优化316: skipCheckSum(woff2/woff/eot)时跳过 checksum 计算,head.checkSumAdjustment 保持 0 占位 */
|
||||||
|
var skipCheckSum = this.options.skipCheckSum;
|
||||||
var wholeCheckSum = 0;
|
var wholeCheckSum = 0;
|
||||||
|
/** fullView 仍需用于表 padding fill(0),skipCheckSum 时不必创建 fullDataView */
|
||||||
var fullView = new Uint8Array(buf);
|
var fullView = new Uint8Array(buf);
|
||||||
/** 优化261: 预创建 DataView,复用于所有表的校验和计算,避免每次 checkSumArrayBuffer 创建新 DataView */
|
var fullDataView = skipCheckSum ? null : new DataView(buf);
|
||||||
var fullDataView = new DataView(buf);
|
|
||||||
for (var si = 0, sl = supportTableList.length; si < sl; si++) {
|
for (var si = 0, sl = supportTableList.length; si < sl; si++) {
|
||||||
var table = supportTableList[si];
|
var table = supportTableList[si];
|
||||||
var tableStart = writer.offset;
|
var tableStart = writer.offset;
|
||||||
@ -153,20 +160,24 @@ var TTFWriter = exports.default = /*#__PURE__*/function () {
|
|||||||
fullView.fill(0, wView.byteOffset + writer.offset, wView.byteOffset + writer.offset + (4 - pad));
|
fullView.fill(0, wView.byteOffset + writer.offset, wView.byteOffset + writer.offset + (4 - pad));
|
||||||
writer.offset += 4 - pad;
|
writer.offset += 4 - pad;
|
||||||
}
|
}
|
||||||
table.checkSum = _checkSumArrayBuffer(buf, tableStart, table.size, fullView, fullDataView);
|
if (!skipCheckSum) {
|
||||||
wholeCheckSum = (wholeCheckSum + table.checkSum) >>> 0;
|
table.checkSum = _checkSumArrayBuffer(buf, tableStart, table.size, fullView, fullDataView);
|
||||||
|
wholeCheckSum = (wholeCheckSum + table.checkSum) >>> 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 优化111: 重新写入校验和,直接 view 写入 */
|
if (!skipCheckSum) {
|
||||||
var csView = writer.view;
|
/* 优化111: 重新写入校验和,直接 view 写入 */
|
||||||
for (var ci = 0, cl = supportTableList.length; ci < cl; ci++) {
|
var csView = writer.view;
|
||||||
var offset2 = 12 + ci * 16 + 4;
|
for (var ci = 0, cl = supportTableList.length; ci < cl; ci++) {
|
||||||
csView.setUint32(offset2, supportTableList[ci].checkSum, false);
|
var offset2 = 12 + ci * 16 + 4;
|
||||||
}
|
csView.setUint32(offset2, supportTableList[ci].checkSum, false);
|
||||||
|
}
|
||||||
|
|
||||||
/* 优化179: 用累加的各表校验和替代全局 checkSum,避免重遍历整个 buffer */
|
/* 优化179: 用累加的各表校验和替代全局 checkSum,避免重遍历整个 buffer */
|
||||||
var ttfCheckSum = (0xB1B0AFBA - wholeCheckSum) >>> 0;
|
var ttfCheckSum = (0xB1B0AFBA - wholeCheckSum) >>> 0;
|
||||||
csView.setUint32(ttfHeadOffset + 8, ttfCheckSum, false);
|
csView.setUint32(ttfHeadOffset + 8, ttfCheckSum, false);
|
||||||
|
}
|
||||||
/** 优化260: delete → null 赋值,避免 V8 隐藏类转换 */
|
/** 优化260: delete → null 赋值,避免 V8 隐藏类转换 */
|
||||||
ttf.writeOptions = null;
|
ttf.writeOptions = null;
|
||||||
ttf.support = null;
|
ttf.support = null;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user