Skip to content

Commit 8821646

Browse files
committed
fix(cli): settle post-seek font subset loads before audit screenshots
1 parent a2a6810 commit 8821646

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

‎packages/cli/src/capture/captureCompositionFrame.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,26 @@ afterEach(() => {
4747
});
4848

4949
describe("seekCompositionTimeline", () => {
50-
it("keeps the existing raced double-frame settle as the default", async () => {
50+
it("keeps the raced double-frame settle and adds a bounded font wait by default", async () => {
5151
const { page, evaluate, waitForFunction } = fakeSeekPage();
5252

5353
await seekCompositionTimeline(page, 1.25);
5454

5555
expect(waitForFunction).not.toHaveBeenCalled();
56-
expect(evaluate).toHaveBeenCalledTimes(2);
56+
expect(evaluate).toHaveBeenCalledTimes(3);
5757
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 1.25, false);
5858
expect(evaluate.mock.calls[1]?.[0]).toContain("window.setTimeout(finish, 100)");
59+
// Post-seek font settle: a seek can reveal glyphs whose unicode-range
60+
// subsets only start loading after the next layout (CJK snapshot reports).
61+
expect(evaluate).toHaveBeenNthCalledWith(3, expect.any(Function), 500);
62+
});
63+
64+
it("waitForFontsMs: 0 disables the post-seek font wait", async () => {
65+
const { page, evaluate } = fakeSeekPage();
66+
67+
await seekCompositionTimeline(page, 1.25, { waitForFontsMs: 0 });
68+
69+
expect(evaluate).toHaveBeenCalledTimes(2);
5970
});
6071

6172
it("prefers renderSeek so the runtime synchronizes clip visibility", async () => {
@@ -128,6 +139,7 @@ describe("seekCompositionTimeline", () => {
128139
fallbackToBridgeAndTimelines: true,
129140
waitForPreferredSeekTargetMs: 500,
130141
animationFrameSettle: "none",
142+
waitForFontsMs: 0,
131143
settleMs: 150,
132144
});
133145
await vi.advanceTimersByTimeAsync(150);

‎packages/cli/src/capture/captureCompositionFrame.ts‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { resolveCompositionViewportFromHtml } from "../utils/compositionViewport
66
const SHADER_TRANSITIONS_TIMEOUT_MS = 90_000;
77
const CAPTURE_SETTLE_MS = 1500;
88
const PREFERRED_SEEK_TARGET_WAIT_MS = 500;
9+
const DEFAULT_POST_SEEK_FONT_WAIT_MS = 500;
910

1011
// The audit-grade seek tuning shared by check and the deprecated inspect/layout:
1112
// bridge+timeline fallback, ordered double-rAF settle, bounded font wait, sleep.
@@ -257,8 +258,13 @@ export async function seekCompositionTimeline(
257258
);
258259
}
259260

260-
if (options.waitForFontsMs !== undefined) {
261-
await waitForCompositionFonts(page, options.waitForFontsMs);
261+
// On by default: every seek caller here is a visual-audit path (snapshot,
262+
// check, compare, validate, layout) and a seek that reveals unrequested
263+
// glyphs must not screenshot before their font subsets load. Costs one
264+
// evaluate + one rAF when nothing is loading. Pass 0 to disable.
265+
const waitForFontsMs = options.waitForFontsMs ?? DEFAULT_POST_SEEK_FONT_WAIT_MS;
266+
if (waitForFontsMs > 0) {
267+
await waitForCompositionFonts(page, waitForFontsMs);
262268
}
263269
if (options.settleMs !== undefined) {
264270
const settleMs = Math.max(0, options.settleMs);
@@ -299,12 +305,31 @@ export async function waitForCompositionFonts(
299305
.evaluate((ms: number) => {
300306
const fonts = Reflect.get(document, "fonts");
301307
if (typeof fonts !== "object" || fonts === null) return Promise.resolve();
302-
const ready = Reflect.get(fonts, "ready");
303-
if (!ready) return Promise.resolve();
304-
return Promise.race([
305-
Promise.resolve(ready).then(() => undefined),
306-
new Promise<void>((resolve) => setTimeout(resolve, ms)),
307-
]);
308+
// A seek can reveal glyphs whose font faces were never requested —
309+
// CJK @font-face splits into unicode-range subsets that only load when
310+
// first laid out. `fonts.ready` is already-resolved at that moment, so
311+
// awaiting it immediately races the load request and screenshots blank
312+
// glyphs (wild reports: Traditional Chinese / Microsoft YaHei check
313+
// snapshots). Force a synchronous layout so pending subsets actually
314+
// start loading, give the loader one frame to flip `fonts.status`,
315+
// THEN await readiness.
316+
void document.body?.offsetHeight;
317+
return new Promise<void>((resolveWait) => {
318+
const deadline = setTimeout(resolveWait, ms);
319+
requestAnimationFrame(() => {
320+
const status = Reflect.get(fonts, "status");
321+
const ready = Reflect.get(fonts, "ready");
322+
if (status !== "loading" || !ready) {
323+
clearTimeout(deadline);
324+
resolveWait();
325+
return;
326+
}
327+
Promise.resolve(ready).then(() => {
328+
clearTimeout(deadline);
329+
resolveWait();
330+
});
331+
});
332+
});
308333
}, timeoutMs)
309334
.catch(() => {});
310335
}

0 commit comments

Comments
 (0)