Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/core/src/fonts/deterministicFonts-bundledSubset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,38 @@ describe("bundled Latin precedence", () => {
expect(bundled).toContain(`unicode-range: ${LATIN_RANGE};`);
});
});

describe("variable supplement at bundled weights", () => {
it("also emits a variable face's Latin Extended subset at each bundled weight it spans", async () => {
const { injectDeterministicFontFaces } = await import("./deterministicFonts.js");
const EXT = "U+0100-02BA, U+02BD-02C5, U+1E00-1EFF";
const fetchImpl = (async (input: unknown) => {
const url = String(input);
if (url.startsWith("https://fonts.googleapis.com/")) {
return new Response(`@font-face {
font-family: 'Outfit'; font-style: normal; font-weight: 100 900;
src: url(https://fonts.gstatic.com/s/outfit/ext.woff2) format('woff2');
unicode-range: ${EXT};
}`);
}
return new Response("OUTFIT_LATIN_EXT_BYTES");
}) as unknown as typeof fetch;
const html = `<!doctype html><html><head><style>p{font-family:"Outfit";font-weight:700}</style></head><body><p>čšćžđ ąęłńśźż řěůď ğışİ őű ășț ạảấ</p></body></html>`;
const result = await injectDeterministicFontFaces(html, {
fetchImpl,
allowSystemFontCapture: false,
});
const faces = (result.match(/@font-face \{[\s\S]*?\}/g) ?? []).filter((face) =>
face.includes(b64("OUTFIT_LATIN_EXT_BYTES")),
);
const weights = faces.map((face) => /font-weight: ([\d ]+);/.exec(face)?.[1]);
expect(weights).toEqual(expect.arrayContaining(["400", "700", "900", "100 900"]));
for (const face of faces) expect(face).toContain(`unicode-range: ${EXT};`);
// Croatian, Polish, Czech, Turkish, Hungarian, Romanian and Vietnamese letters beyond Latin-1 are all in its range.
const inExt = (ch: string) => {
const cp = ch.codePointAt(0) ?? 0;
return (cp >= 0x100 && cp <= 0x2ba) || (cp >= 0x1e00 && cp <= 0x1eff);
};
for (const ch of "čšćžđąęłńśźżřěůďğışİőűășțạảấ") expect(inExt(ch)).toBe(true);
});
});
21 changes: 21 additions & 0 deletions packages/core/src/fonts/deterministicFonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,22 @@ function partitionWeightRuns(
return runs;
}

function bundledWeightsWithin(
weight: string,
style: string,
covered: ReadonlySet<string>,
): string[] {
const range = /^(\d+)\s+(\d+)$/.exec(weight.trim());
if (!range) return [];
const [low, high] = [Number(range[1]), Number(range[2])];
return [...covered].flatMap((key) => {
const [value, coveredStyle] = key.split(":");
return coveredStyle === style && Number(value) >= low && Number(value) <= high
? [value ?? ""]
: [];
});
}

// Path 1: pre-bundled fonts via FONT_ALIASES — emit embedded faces,
// then fetch from Google Fonts to fill missing weights and character subsets.
async function bundledFamilyFaceRules(
Expand Down Expand Up @@ -744,6 +760,11 @@ async function bundledFamilyFaceRules(
const last = run[run.length - 1];
if (!first || !last) continue;
const weight = run.length > 1 ? `${first.weight} ${last.weight}` : first.weight;
// The exact-weight bundled face (Latin only) wins over a range face: emit the range at each bundled weight too.
for (const exact of bundledWeightsWithin(weight, first.style, coveredWeights))
rules.push(
buildFontFaceRule(emitFamily, first.dataUri, exact, first.style, first.unicodeRange),
);
rules.push(
buildFontFaceRule(emitFamily, first.dataUri, weight, first.style, first.unicodeRange),
);
Expand Down
Loading