mirror of
https://github.com/2234839/web-font.git
synced 2026-11-26 04:03:34 +08:00
- /api/glyph-match 改为 NDJSON 流式:start/font/glyph/fontDone/done 事件, 前端逐字体渲染预览与分数,消除整包等待 - 评分直接消费裁剪管线内存 ttf 对象(按字形 unicode 重建子集 cmap), 免去序列化再解析;源界明朝/装甲明朝等 re-parse 失败字体首次可评分 - glyph_bitmap 对快路径字形(_origBuf 原始字节引用)按需调用 vendor parseGlyf 解码坐标,修复内存对象方案下「借」字镂空回归 - 所有裁剪调用点接入 withMemoryGate 内存闸门(cgroup 探测并发控制) - HTTP 服务支持流式响应(LLRT 下经 _stream 附加属性传递), 客户端断连 EPIPE/Controller closed 全链路容错,不再打死进程 - 单字体处理失败 try-catch 隔离,不中断整个搜索流 - app.ts 端口支持 PORT 环境变量覆盖,便于并行测试实例
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import type { FontEditor } from "../../vendor/fonteditor-core/lib/ttf/font";
|
||
import { fontSubsetToObject, type SubsetResult } from "./font";
|
||
import { stat } from "../interface";
|
||
import { readFontBuffer } from "../shared";
|
||
|
||
/**
|
||
* 根据源字体大小预留裁剪任务内存。
|
||
* 轮廓解析、布局表重写和序列化会同时持有源文件及多份临时结构,不能只按源文件大小限流。
|
||
*/
|
||
export async function estimateSubsetMemory(fontPath: string): Promise<number> {
|
||
const { size } = await stat(fontPath);
|
||
return Math.max(64 * 1024 * 1024, size * 4);
|
||
}
|
||
|
||
/**
|
||
* 字体裁剪的唯一文件入口。
|
||
* HTTP 裁剪接口和截图找字体都调用这里,不额外缓存完整字体对象。
|
||
* 返回的 ttf 为内存子集对象(可直接光栅化评分,免去序列化再解析);
|
||
* OTF(CFF)输入时 ttf 为 null。
|
||
*/
|
||
export async function subsetFontFile(
|
||
fontPath: string,
|
||
text: string,
|
||
outType: FontEditor.FontType,
|
||
): Promise<SubsetResult & { bytes: Uint8Array; readMs: number; subsetMs: number }> {
|
||
const sourceType = fontPath.split(".").pop() as FontEditor.FontType;
|
||
const readStarted = Date.now();
|
||
const fontBuffer = await readFontBuffer(fontPath);
|
||
const subsetStarted = Date.now();
|
||
const result = fontSubsetToObject(fontBuffer, text, { sourceType, outType });
|
||
return {
|
||
...result,
|
||
bytes: result.write(outType),
|
||
readMs: subsetStarted - readStarted,
|
||
subsetMs: Date.now() - subsetStarted,
|
||
};
|
||
}
|