From 1e543d58abefa0476245ad5736474ae7834064a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Fri, 10 Apr 2026 13:55:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20OTF=E2=86=92TTF=20?= =?UTF-8?q?=E5=AD=90=E9=9B=86=E5=AD=97=E4=BD=93=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E7=A9=BA=E7=99=BD=20+=20=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E5=9F=BA=E5=87=86=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - API 默认 outType 改为 ttf(兼容性最好) - Cache-Control 从 immutable 改为 24h 缓存 - 基准测试改为 DOM 渲染 + puppeteer 截图 + pngjs 解码(更贴近真实浏览器) - 增加 maxp 表验证(maxPoints/maxContours 为 0 直接报错) Co-Authored-By: Claude Opus 4.6 --- backend/app.ts | 6 +++--- package.json | 1 + pnpm-lock.yaml | 9 +++++++++ 基准测试.test.ts | 33 +++++++++++++-------------------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/backend/app.ts b/backend/app.ts index b654582..ab23543 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -314,12 +314,12 @@ async function handleFontSubset(req: Request, res: Response) { }; } - /** LLRT 不支持 wasm,默认 ttf;Node.js 默认 woff2(体积更小) */ + /** 默认 ttf(兼容性最好);LLRT 不支持 wasm 只能用 ttf */ const isLlrt = release_name === "llrt"; const outTypeParam = params.get("outType") || ""; const outType = (outTypeParam === "woff2" || outTypeParam === "ttf") ? (isLlrt && outTypeParam === "woff2" ? "ttf" : outTypeParam) - : (isLlrt ? "ttf" : "woff2"); + : "ttf"; const newFont = await fontSubset(oldFontBuffer, text, { outType: outType, @@ -337,7 +337,7 @@ async function handleFontSubset(req: Request, res: Response) { status: 200, headers: { "Content-Type": contentTypes[outType] || "font/ttf", - "Cache-Control": "public, max-age=31536000, immutable", + "Cache-Control": "public, max-age=86400", }, }), }; diff --git a/package.json b/package.json index 848beda..a01672a 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@types/node": "^25.5.2", "@xmldom/xmldom": "^0.9.9", "jsdom": "^29.0.2", + "pngjs": "^7.0.0", "puppeteer": "^24.40.0", "skia-canvas": "^3.0.8", "tsup": "^8.5.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8da2935..af6c8ca 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,6 +24,9 @@ importers: jsdom: specifier: ^29.0.2 version: 29.0.2 + pngjs: + specifier: ^7.0.0 + version: 7.0.0 puppeteer: specifier: ^24.40.0 version: 24.40.0(typescript@6.0.2) @@ -1279,6 +1282,10 @@ packages: pkg-types@1.3.1: resolution: {integrity: sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ==} + pngjs@7.0.0: + resolution: {integrity: sha512-LKWqWJRhstyYo9pGvgor/ivk2w94eSjE3RGVuzLGlr3NmD8bf7RcYGze1mNdEHRP6TRP6rMuDHk5t44hnTRyow==} + engines: {node: '>=14.19.0'} + postcss-load-config@6.0.1: resolution: {integrity: sha512-oPtTM4oerL+UXmx+93ytZVN82RrlY/wPUV8IeDxFrzIjXOLF1pN+EmKPLbubvKHT2HC20xXsCAH2Z+CKV6Oz/g==} engines: {node: '>= 18'} @@ -2790,6 +2797,8 @@ snapshots: mlly: 1.8.2 pathe: 2.0.3 + pngjs@7.0.0: {} + postcss-load-config@6.0.1(postcss@8.5.9): dependencies: lilconfig: 3.1.3 diff --git a/基准测试.test.ts b/基准测试.test.ts index 4ae1839..7774461 100644 --- a/基准测试.test.ts +++ b/基准测试.test.ts @@ -19,6 +19,7 @@ import { createServer, type Server, type IncomingMessage, type ServerResponse } import { performance } from "node:perf_hooks"; import puppeteer, { type Page } from "puppeteer"; import { fontSubset } from "./backend/font_util/font.js"; +import { PNG } from "pngjs"; const BENCHMARK_DIR = "benchmark_results"; const ROUNDS = 10; @@ -46,7 +47,7 @@ function createFontServer(): Promise<{ server: Server; port: number }> {
${text.replace(/