diff --git a/internal/gui/assets/webfonts.js b/internal/gui/assets/webfonts.js index 0eacc3f9c..973aea074 100644 --- a/internal/gui/assets/webfonts.js +++ b/internal/gui/assets/webfonts.js @@ -18,11 +18,15 @@ window.webFonts = (() => { let ctx, local = null, asked = false, base = []; const extra = [], seen = new Map(); const quoted = window.desktopFonts.quoted; + // The two generics' widths are measured once: each family then costs one + // or two measures, not two or four. + let generic; + const width = (f) => (ctx.font = "72px " + f, ctx.measureText("mmmlli WwQ@ 0123 中文").width); function drawn(name) { if (!seen.has(name)) { ctx ||= document.createElement("canvas").getContext("2d"); - const w = (f) => (ctx.font = "72px " + f, ctx.measureText("mmmlli WwQ@ 0123 中文").width); - seen.set(name, !!name && ["monospace", "serif"].some((b) => w(quoted(name) + "," + b) !== w(b))); + generic ||= ["monospace", "serif"].map((b) => [b, width(b)]); + seen.set(name, !!name && generic.some(([b, w]) => width(quoted(name) + "," + b) !== w)); } return seen.get(name); } @@ -64,8 +68,17 @@ window.webFonts = (() => { local = group(await window.queryLocalFonts()); } // list: the tab's families, from the server's (/api/fonts) + // The probe yields every few milliseconds: a measure of a family not yet + // drawn loads it, and probing a Mac's whole list at once held the click + // that opened Settings in one task, 2.6 s with the CPU slowed 4x. async function list(server) { - base = server.filter((f) => drawn(f.name)); + const out = []; + let start = performance.now(); + for (const f of server) { + if (drawn(f.name)) out.push(f); + if (performance.now() - start > 8) await new Promise((r) => setTimeout(r)), start = performance.now(); + } + base = out; if (!asked && window.queryLocalFonts) try { if ((await navigator.permissions.query({ name: "local-fonts" })).state === "granted") await read(), asked = true; } catch {} diff --git a/internal/gui/tests/fonts.test.cjs b/internal/gui/tests/fonts.test.cjs index 3a8d4930c..aa9e049c8 100644 --- a/internal/gui/tests/fonts.test.cjs +++ b/internal/gui/tests/fonts.test.cjs @@ -368,6 +368,40 @@ for (const [engine, runtime] of engines) { } finally { await browser.close(); } }); + // unscientific9: a tab's first open of Settings stuck while the probe + // measured every server family in the click's task. Each measure is made + // to cost 1 ms here, so 400 families held one task for 1.6 s before; the + // probe now yields, and measures the two generics once. + for (const lang of ["en", "zh"]) test(`${engine} ${lang}: a browser tab's font probe never holds the page`, async () => { + const browser = await runtime.launch(); + try { + const fonts = Array.from({ length: 400 }, (_, i) => ({ name: "Probe Family " + i, styles: [face("Probe Family " + i)] })); + fonts.push({ name: "Georgia", styles: [face("Georgia")] }); + const init = () => { + const measure = CanvasRenderingContext2D.prototype.measureText; + window.__probe = { calls: 0, longest: 0 }; + let start = null; + CanvasRenderingContext2D.prototype.measureText = function (text) { + const now = performance.now(); + if (start === null) start = now, setTimeout(() => { start = null; }); + for (const end = now + 1; performance.now() < end;); + window.__probe.calls++; + window.__probe.longest = Math.max(window.__probe.longest, performance.now() - start); + return measure.call(this, text); + }; + }; + const { page, errors, close } = await open(browser, lang, { web: true, list: fonts, init }); + const probe = await page.evaluate(() => window.__probe); + const shown = await page.evaluate(() => fontList.map(f => f.name)); + assert(!shown.some(n => n.startsWith("Probe Family")), "a family this browser doesn't draw isn't offered"); + assert(probe.calls >= fonts.length, "every family was measured: " + probe.calls); + assert(probe.calls <= 2 * fonts.length + 4, "the generics are measured once: " + probe.calls + " measures"); + assert(probe.longest < 300, "the probe held one task for " + probe.longest.toFixed(0) + " ms"); + assert.deepEqual(errors, []); + await close(); + } finally { await browser.close(); } + }); + // Norcleeh (magpie in Docker, its web GUI): the tab listed the server's // fonts, the container's, never the ones on his own computer. Where the // browser has the Local Font Access API, the press that opens a family