/** * WebFont SDK — 按需增量加载字体片段,无闪烁(本文件由 packages/webfont-sdk 构建,勿手改) * * 架构:核心增量引擎 + 两种注册模式 * - 核心:IncrementalEngine 按 fontKey 管理字符集,只请求增量;失败字符自动记忆不重试 * - CSS 模式(WebFont):loadFont(轮询)/ observeFont(DOM 事件)/ loadText(手动传文本) * - FontFace 模式(WebFontCanvas):Canvas/canvas 场景,FontFace + unicodeRange 注册 * * 用法: * // 轮询模式 * WebFont.loadFont({ fontName, selector, family, interval }); * * // 事件驱动模式 * var obs = WebFont.observeFont({ fontName, selector, family }); * obs.dispose(); * * // 直接传文本模式 * var loader = WebFont.loadText({ fontName, text: "你好世界", family }); * loader.update("追加文字"); * loader.dispose(); * * // Canvas 模式(leafer / 原生 canvas) * var face = WebFontCanvas.loadFontFace({ fontName }, function (chunk) { 在此重绘 }); * face.update("画布上的文字"); * await WebFontCanvas.ready(); */ (function() { //#region src/engine.ts function createHttpProvider(baseUrl) { return (fontName, text, outType) => { const url = `${baseUrl}/api?font=${encodeURIComponent(fontName)}&text=${encodeURIComponent(text)}&outType=${outType}`; return Promise.resolve({ url, format: outType === "woff2" ? "woff2" : "truetype" }); }; } var IncrementalEngine = class { constructor(config = {}) { this.states = /* @__PURE__ */ new Map(); this.active = 0; this.queue = []; this.flying = 0; this.config = { maxConcurrent: config.maxConcurrent ?? 4, provider: config.provider ?? null }; } /** fontKey:fontName + family 唯一确定一个增量组 */ static fontKey(fontName, family) { return fontName + "|" + family; } setProvider(provider) { this.config.provider = provider; } getState(key) { return this.states.get(key); } /** 获取或创建字体状态;已存在时按传入项更新 baseUrl / outType / 回调 */ ensureState(key, fontName, options) { let state = this.states.get(key); if (!state) { state = { fontName, baseUrl: options.baseUrl, outType: options.outType, loadedChars: /* @__PURE__ */ new Set(), failedChars: /* @__PURE__ */ new Set(), pendingChars: /* @__PURE__ */ new Set(), onLoadChunk: options.onLoadChunk ?? null, provider: options.provider ?? null }; this.states.set(key, state); return state; } state.baseUrl = options.baseUrl; state.outType = options.outType; if (options.onLoadChunk) state.onLoadChunk = options.onLoadChunk; if (options.provider !== void 0) state.provider = options.provider; return state; } /** 删除状态(销毁时) */ removeState(key) { this.states.delete(key); } /** 是否还有在途任务(请求中或注册中,ready() 轮询用) */ hasPending() { if (this.flying > 0) return true; for (const s of this.states.values()) if (s.pendingChars.size > 0) return true; return false; } /** 清除失败记录(下次遇到这些字符会重新请求) */ retryFailed(key) { this.states.get(key)?.failedChars.clear(); } /** * 提交一批文本:过滤出新字符并异步请求子集。 * 乐观标记 pending,成功移入 loaded、失败移入 failed。 * 超过 maxCharsPerChunk 时自动分批(uni 小程序 loadFontFace 无 unicode-range, * 单次需携带全量累积文本,长文本按批切分避免 URL 超限)。 */ submitText(key, text, maxCharsPerChunk = Infinity) { const state = this.states.get(key); if (!state) return; const newChars = []; for (const ch of text) { if (state.loadedChars.has(ch) || state.pendingChars.has(ch) || state.failedChars.has(ch)) continue; /** 跳过控制字符 */ if (ch.charCodeAt(0) < 32) continue; newChars.push(ch); state.pendingChars.add(ch); } if (newChars.length === 0) return; for (let i = 0; i < newChars.length; i += maxCharsPerChunk) { const batch = newChars.slice(i, i + maxCharsPerChunk); this.enqueue(() => this.loadChunk(state, batch)); } } /** 执行一次子集请求 + 注册(在并发槽内完成) */ async loadChunk(state, chars) { this.flying++; try { const text = chars.join(""); const result = await (state.provider ?? this.config.provider ?? createHttpProvider(state.baseUrl))(state.fontName, text, state.outType); /** 注册完成后才把字符记为已加载:注册失败可走 failedChars 重试路径 */ await state.onLoadChunk?.({ fontName: state.fontName, chars, url: result.url, format: result.format }); for (const ch of chars) { state.loadedChars.add(ch); state.pendingChars.delete(ch); } } catch { for (const ch of chars) { state.pendingChars.delete(ch); state.failedChars.add(ch); } } finally { this.flying--; } } /** 并发池:超出 maxConcurrent 的任务排队等待 */ enqueue(fn) { if (this.active < this.config.maxConcurrent) this.execute(fn); else this.queue.push(fn); } /** * 执行一个任务,完成后从队列取下一个。 * 注意:这里必须直接调用 next(fn),不能递归调用外层 run 闭包—— * 那样会把下一个任务替换成本次任务重跑(闭包捕获),队列真身丢失 */ execute(fn) { this.active++; fn().finally(() => { this.active--; const next = this.queue.shift(); if (next) this.execute(next); }); } setMaxConcurrent(n) { this.config.maxConcurrent = Math.max(1, n | 0); } }; //#endregion //#region src/css-mode.ts /** * CSS 模式 —— 注入 @font-face + unicode-range 样式(DOM 场景) * * 与原 public/webfont-sdk.js 行为一致: * - 片段就绪时注入