Skip to content

Commit 6011d9d

Browse files
fix(core): accented Latin letters draw in a bundled family at its bundled weights (heygen-com#7299)
* fix(core): accented Latin letters draw in a bundled family at its bundled weights * fix(core): trim the comment added with the bundled-weight faces
1 parent 66cbabc commit 6011d9d

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎packages/core/src/fonts/deterministicFonts-bundledSubset.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,38 @@ describe("bundled Latin precedence", () => {
128128
expect(bundled).toContain(`unicode-range: ${LATIN_RANGE};`);
129129
});
130130
});
131+
132+
describe("variable supplement at bundled weights", () => {
133+
it("also emits a variable face's Latin Extended subset at each bundled weight it spans", async () => {
134+
const { injectDeterministicFontFaces } = await import("./deterministicFonts.js");
135+
const EXT = "U+0100-02BA, U+02BD-02C5, U+1E00-1EFF";
136+
const fetchImpl = (async (input: unknown) => {
137+
const url = String(input);
138+
if (url.startsWith("https://fonts.googleapis.com/")) {
139+
return new Response(`@font-face {
140+
font-family: 'Outfit'; font-style: normal; font-weight: 100 900;
141+
src: url(https://fonts.gstatic.com/s/outfit/ext.woff2) format('woff2');
142+
unicode-range: ${EXT};
143+
}`);
144+
}
145+
return new Response("OUTFIT_LATIN_EXT_BYTES");
146+
}) as unknown as typeof fetch;
147+
const html = `<!doctype html><html><head><style>p{font-family:"Outfit";font-weight:700}</style></head><body><p>čšćžđ ąęłńśźż řěůď ğışİ őű ășț ạảấ</p></body></html>`;
148+
const result = await injectDeterministicFontFaces(html, {
149+
fetchImpl,
150+
allowSystemFontCapture: false,
151+
});
152+
const faces = (result.match(/@font-face \{[\s\S]*?\}/g) ?? []).filter((face) =>
153+
face.includes(b64("OUTFIT_LATIN_EXT_BYTES")),
154+
);
155+
const weights = faces.map((face) => /font-weight: ([\d ]+);/.exec(face)?.[1]);
156+
expect(weights).toEqual(expect.arrayContaining(["400", "700", "900", "100 900"]));
157+
for (const face of faces) expect(face).toContain(`unicode-range: ${EXT};`);
158+
// Croatian, Polish, Czech, Turkish, Hungarian, Romanian and Vietnamese letters beyond Latin-1 are all in its range.
159+
const inExt = (ch: string) => {
160+
const cp = ch.codePointAt(0) ?? 0;
161+
return (cp >= 0x100 && cp <= 0x2ba) || (cp >= 0x1e00 && cp <= 0x1eff);
162+
};
163+
for (const ch of "čšćžđąęłńśźżřěůďğışİőűășțạảấ") expect(inExt(ch)).toBe(true);
164+
});
165+
});

‎packages/core/src/fonts/deterministicFonts.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,22 @@ function partitionWeightRuns(
688688
return runs;
689689
}
690690

691+
function bundledWeightsWithin(
692+
weight: string,
693+
style: string,
694+
covered: ReadonlySet<string>,
695+
): string[] {
696+
const range = /^(\d+)\s+(\d+)$/.exec(weight.trim());
697+
if (!range) return [];
698+
const [low, high] = [Number(range[1]), Number(range[2])];
699+
return [...covered].flatMap((key) => {
700+
const [value, coveredStyle] = key.split(":");
701+
return coveredStyle === style && Number(value) >= low && Number(value) <= high
702+
? [value ?? ""]
703+
: [];
704+
});
705+
}
706+
691707
// Path 1: pre-bundled fonts via FONT_ALIASES — emit embedded faces,
692708
// then fetch from Google Fonts to fill missing weights and character subsets.
693709
async function bundledFamilyFaceRules(
@@ -744,6 +760,11 @@ async function bundledFamilyFaceRules(
744760
const last = run[run.length - 1];
745761
if (!first || !last) continue;
746762
const weight = run.length > 1 ? `${first.weight} ${last.weight}` : first.weight;
763+
// The exact-weight bundled face (Latin only) wins over a range face: emit the range at each bundled weight too.
764+
for (const exact of bundledWeightsWithin(weight, first.style, coveredWeights))
765+
rules.push(
766+
buildFontFaceRule(emitFamily, first.dataUri, exact, first.style, first.unicodeRange),
767+
);
747768
rules.push(
748769
buildFontFaceRule(emitFamily, first.dataUri, weight, first.style, first.unicodeRange),
749770
);

0 commit comments

Comments
 (0)