Skip to content

Commit b4b2fea

Browse files
fix(web): keep a monospace pass cached only once the family resolves
1 parent b370ef3 commit b4b2fea

2 files changed

Lines changed: 70 additions & 18 deletions

File tree

‎apps/web/src/appearanceFonts.test.ts‎

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ describe("areFontAdvancesMonospace", () => {
3131
describe("createCachedFamilyProbe", () => {
3232
it("probes a family once and caches both verdicts", () => {
3333
const probed: string[] = [];
34-
const probe = createCachedFamilyProbe((families) => {
35-
probed.push(families);
36-
return families.includes("Mono");
37-
});
34+
const probe = createCachedFamilyProbe(
35+
(families) => {
36+
probed.push(families);
37+
return families.includes("Mono");
38+
},
39+
() => true,
40+
);
3841
expect(probe("Comic Mono")).toBe(true);
3942
expect(probe("Comic Mono")).toBe(true);
4043
expect(probe("Comic Sans MS")).toBe(false);
@@ -44,19 +47,62 @@ describe("createCachedFamilyProbe", () => {
4447

4548
it("shares the verdict across spellings of the same family list", () => {
4649
const probed: string[] = [];
47-
const probe = createCachedFamilyProbe((families) => {
48-
probed.push(families);
49-
return true;
50-
});
50+
const probe = createCachedFamilyProbe(
51+
(families) => {
52+
probed.push(families);
53+
return true;
54+
},
55+
() => true,
56+
);
5157
expect(probe("Fira Code")).toBe(true);
5258
expect(probe(' "Fira Code" ')).toBe(true);
5359
expect(probed).toEqual(['"Fira Code"']);
5460
});
5561

62+
it("keeps a pass only once the family resolves", () => {
63+
let resolved = false;
64+
let monospace = true;
65+
let probes = 0;
66+
const probe = createCachedFamilyProbe(
67+
() => {
68+
probes += 1;
69+
return monospace;
70+
},
71+
() => resolved,
72+
);
73+
// An absent face measures as the monospace fallback; that pass is not final.
74+
expect(probe("Late Sans")).toBe(true);
75+
expect(probe("Late Sans")).toBe(true);
76+
expect(probes).toBe(2);
77+
// The real face arrives and turns out proportional: the guard sees it.
78+
monospace = false;
79+
resolved = true;
80+
expect(probe("Late Sans")).toBe(false);
81+
expect(probe("Late Sans")).toBe(false);
82+
expect(probes).toBe(3);
83+
});
84+
85+
it("keeps a failure without waiting for resolution", () => {
86+
let probes = 0;
87+
const probe = createCachedFamilyProbe(
88+
() => {
89+
probes += 1;
90+
return false;
91+
},
92+
() => false,
93+
);
94+
expect(probe("Comic Sans MS")).toBe(false);
95+
expect(probe("Comic Sans MS")).toBe(false);
96+
expect(probes).toBe(1);
97+
});
98+
5699
it("accepts empty input without probing", () => {
57-
const probe = createCachedFamilyProbe(() => {
58-
throw new Error("should not probe");
59-
});
100+
const probe = createCachedFamilyProbe(
101+
() => {
102+
throw new Error("should not probe");
103+
},
104+
() => true,
105+
);
60106
expect(probe("")).toBe(true);
61107
expect(probe(" ")).toBe(true);
62108
});

‎apps/web/src/appearanceFonts.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ function probeWidth(fontList: string): number | null {
175175
*/
176176
export function isFontFamilyAvailable(family: string): boolean {
177177
const families = cssFontFamilies(family);
178-
if (families === null) return false;
178+
return families !== null && familiesResolve(families);
179+
}
180+
181+
function familiesResolve(families: string): boolean {
179182
if (/^(system-ui|sans-serif|serif|monospace|ui-monospace)$/i.test(families)) return true;
180183
try {
181184
for (const generic of ["monospace", "serif", "sans-serif"]) {
@@ -208,13 +211,16 @@ export function areFontAdvancesMonospace(advances: readonly number[]): boolean {
208211

209212
/**
210213
* Probing a family is up to 32 measureText calls, and setting the canvas font
211-
* to a new family string forces engine font matching on top. Verdicts cannot
212-
* change while the page lives (the installed font list is itself cached for
213-
* the session), so both passes and failures are kept, keyed by the normalized
214-
* family list.
214+
* to a new family string forces engine font matching on top, so verdicts are
215+
* kept keyed by the normalized family list. A failure always names a resolved
216+
* face and is final. A pass may be the generic fallback speaking for a face
217+
* that has not loaded yet, so it is only kept once the family resolves;
218+
* until then every call probes again, which is what lets the terminal
219+
* revalidate after a late face loads and still reject a proportional one.
215220
*/
216221
export function createCachedFamilyProbe(
217222
probe: (families: string) => boolean,
223+
resolves: (families: string) => boolean,
218224
): (family: string) => boolean {
219225
const verdicts = new Map<string, boolean>();
220226
return (family) => {
@@ -223,7 +229,7 @@ export function createCachedFamilyProbe(
223229
const cached = verdicts.get(families);
224230
if (cached !== undefined) return cached;
225231
const verdict = probe(families);
226-
verdicts.set(families, verdict);
232+
if (!verdict || resolves(families)) verdicts.set(families, verdict);
227233
return verdict;
228234
};
229235
}
@@ -257,7 +263,7 @@ function measureMonospaceFamilies(families: string): boolean {
257263
* Unmeasurable environments answer true, so a missing canvas never blocks a
258264
* legitimate font.
259265
*/
260-
export const isMonospaceFamily = createCachedFamilyProbe(measureMonospaceFamilies);
266+
export const isMonospaceFamily = createCachedFamilyProbe(measureMonospaceFamilies, familiesResolve);
261267

262268
// Nameable faces the platform generics commonly map to, likeliest first.
263269
// Pixel-comparing a generic against these names the actual face; Apple's own

0 commit comments

Comments
 (0)