支持 woff2

This commit is contained in:
崮生(子虚) 2026-04-11 00:17:27 +08:00
parent 0bfc9c9b27
commit 93e36bc692
3 changed files with 12 additions and 21 deletions

View File

@ -199,14 +199,12 @@ async function handleListFonts(req: Request, res: Response) {
/** GET /api/config — 返回公开配置 */
async function handleGetConfig(req: Request, res: Response) {
const isLlrt = release_name === "llrt";
return {
req,
res: jsonResponse({
enableTempUpload,
adminUploadEnabled: !!adminApiKey,
/** LLRT 不支持 wasm无法输出 woff2 */
supportedOutTypes: isLlrt ? ["ttf"] : ["woff2", "ttf"],
supportedOutTypes: ["woff2", "ttf"],
}),
};
}
@ -314,12 +312,9 @@ async function handleFontSubset(req: Request, res: Response) {
};
}
/** 默认 ttf兼容性最好LLRT 不支持 wasm 只能用 ttf */
const isLlrt = release_name === "llrt";
/** 默认 ttf兼容性最好 */
const outTypeParam = params.get("outType") || "";
const outType = (outTypeParam === "woff2" || outTypeParam === "ttf")
? (isLlrt && outTypeParam === "woff2" ? "ttf" : outTypeParam)
: "ttf";
const outType = (outTypeParam === "woff2" || outTypeParam === "ttf") ? outTypeParam : "ttf";
const newFont = await fontSubset(oldFontBuffer, text, {
outType: outType,

View File

@ -6,7 +6,7 @@
*/
import { execSync } from "node:child_process";
import { existsSync } from "node:fs";
import { copyFile, mkdir, writeFile, rm } from "node:fs/promises";
import { mkdir, writeFile, rm } from "node:fs/promises";
import { arch, platform } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
@ -100,13 +100,7 @@ function runTsup() {
execSync("pnpm tsup", { stdio: "inherit", cwd: ROOT_DIR });
}
/** 复制 woff2.wasm 到 tsup 输出目录(打包后 __dirname 指向此处) */
async function copyWoff2Wasm() {
const src = join(ROOT_DIR, "vendor/fonteditor-core/woff2/woff2.wasm");
const dst = join(ROOT_DIR, "dist_backend/backend/woff2.wasm");
await copyFile(src, dst);
console.log("Copied woff2.wasm to dist_backend/backend/");
}
/** woff2 已使用纯 JS 实现vendor/fonteditor-core/woff2/index.js无需复制 wasm */
/** 使用 LLRT compile 生成 .lrt 文件 */
function runLlrtCompile() {
@ -126,7 +120,6 @@ function runLlrtCompile() {
async function main() {
await ensureLlrt();
runTsup();
await copyWoff2Wasm();
runLlrtCompile();
}

View File

@ -14,14 +14,17 @@ try {
zlib = require("zlib");
}
const brotliCompressSync = zlib.brotliCompressSync;
const constants = zlib.constants;
/** LLRT 的 zlib 没有 constants直接使用数值常量与 Node.js zlib.constants 一致) */
const BROTLI_PARAM_QUALITY = zlib.constants?.BROTLI_PARAM_QUALITY ?? 3;
const BROTLI_PARAM_SIZE_HINT = zlib.constants?.BROTLI_PARAM_SIZE_HINT ?? 4;
/**
* Brotli 压缩参数quality 8
* 测试表明 FONT 模式对小数据集<200KB无速度优势且增加 0.14% 体积保持 GENERIC
*/
const BROTLI_OPTIONS_BASE = {
params: { [constants.BROTLI_PARAM_QUALITY]: 8 },
params: { [BROTLI_PARAM_QUALITY]: 8 },
};
/* ======== Known Table Tags 索引表 ======== */
@ -757,8 +760,8 @@ function encodeTTFToWOFF2(ttfBuffer) {
/* Brotli 压缩,传入 SIZE_HINT 帮助预分配内部缓冲区 */
const brotliOptions = totalTableDataSize > 0
? { params: {
[constants.BROTLI_PARAM_QUALITY]: 8,
[constants.BROTLI_PARAM_SIZE_HINT]: totalTableDataSize,
[BROTLI_PARAM_QUALITY]: 8,
[BROTLI_PARAM_SIZE_HINT]: totalTableDataSize,
}}
: BROTLI_OPTIONS_BASE;
const compressedData = brotliCompressSync(uncompressedData, brotliOptions);