From a5c6ac8f0356bde8077e30c1e1866e380b78bc70 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Sun, 13 Sep 2026 17:59:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=89=8D=E7=AB=AF=E7=95=8C=E9=9D=A2):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E5=AD=97=E9=80=89=E6=8B=A9=E5=B1=82?= =?UTF-8?q?=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/webui/ofdgo.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/assets/webui/ofdgo.js b/assets/webui/ofdgo.js index 8b5ecb4..3321fc7 100644 --- a/assets/webui/ofdgo.js +++ b/assets/webui/ofdgo.js @@ -19,6 +19,7 @@ let wasmWorker = null; let wasmRequestID = 0; let wasmRecoveryTimer = 0; let fontDatabase = null; +let textMeasure = null; const state = { ready: false, @@ -1750,8 +1751,9 @@ function mountPageSVG(index, page, openSeq = state.openSeq) { function createTextLayer(text) { const layer = document.createElement("div"); layer.className = "text-layer"; - const measure = document.createElement("canvas").getContext("2d"); + const measure = textMeasure ||= document.createElement("canvas").getContext("2d"); measure.font = "100px sans-serif"; + const widths = new Map(); for (const run of text.runs || []) { const line = document.createElement("span"); line.className = "text-run"; @@ -1759,7 +1761,11 @@ function createTextLayer(text) { for (const item of run.spans || []) { const span = document.createElement("span"); span.textContent = chars.slice(item.start, item.end).join(""); - const width = measure.measureText(span.textContent).width || 1; + let width = widths.get(span.textContent); + if (width === undefined) { + width = measure.measureText(span.textContent).width || 1; + widths.set(span.textContent, width); + } const [a, b, c, d, e, f] = item.matrix.map((value) => value * MM_TO_PX); span.style.transform = `matrix(${a / width}, ${b / width}, ${c / 100}, ${d / 100}, ${e}, ${f})`; line.append(span);