Skip to content

Commit af0c815

Browse files
fix(fonts): stop failing cloud renders on var() shorthands, font-X-400 names and linked font sheets (#7166)
* fix(fonts): stop failing cloud renders on var() shorthands, font-X-400 names and linked font sheets * style(producer): shorten the project font sheet comment * style(core): shorten the field font families test header * fix(fonts): split font size parsing and fit the CLI size budget * fix(producer): read project font sheets for family names only, never fetch from them
1 parent 2ac0114 commit af0c815

4 files changed

Lines changed: 236 additions & 14 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// @vitest-environment node
2+
// Family spellings that failed distributed renders closed although the browser draws them.
3+
4+
import { afterAll, beforeEach, describe, expect, it } from "vitest";
5+
import { mkdtempSync, rmSync } from "node:fs";
6+
import { tmpdir } from "node:os";
7+
import { join } from "node:path";
8+
import { EMBEDDED_FONT_DATA } from "./fontData.generated.js";
9+
import {
10+
_clearGoogleFontCssCacheForTests,
11+
injectDeterministicFontFaces,
12+
} from "./deterministicFonts.js";
13+
14+
const FONT_CACHE_ENV = "HYPERFRAMES_FONT_CACHE_DIR";
15+
const originalFontCache = process.env[FONT_CACHE_ENV];
16+
const fontCache = mkdtempSync(join(tmpdir(), "hf-font-field-families-"));
17+
18+
beforeEach(() => {
19+
process.env[FONT_CACHE_ENV] = fontCache;
20+
_clearGoogleFontCssCacheForTests();
21+
});
22+
23+
afterAll(() => {
24+
rmSync(fontCache, { recursive: true, force: true });
25+
if (originalFontCache !== undefined) process.env[FONT_CACHE_ENV] = originalFontCache;
26+
else delete process.env[FONT_CACHE_ENV];
27+
});
28+
29+
// Google Fonts answers 400 for a name it does not know; here it knows none.
30+
const noGoogleFonts = (async () => new Response("", { status: 400 })) as unknown as typeof fetch;
31+
32+
const inject = (html: string) =>
33+
injectDeterministicFontFaces(html, {
34+
failClosedFontFetch: true,
35+
allowSystemFontCapture: false,
36+
fetchImpl: noGoogleFonts,
37+
});
38+
39+
const MONTSERRAT_400 = EMBEDDED_FONT_DATA.get("@fontsource/montserrat:400:normal");
40+
41+
describe("field font families under fail-closed", () => {
42+
it("reads a font shorthand whose weight is a var() ahead of the size", async () => {
43+
const html = `<html><head><style>
44+
:root { --font-display: Montserrat, sans-serif; --weight: 700; }
45+
h1 { font: var(--weight) 1000px/1000px var(--font-display); }
46+
</style></head><body><h1>hi</h1></body></html>`;
47+
48+
const out = await inject(html);
49+
50+
expect(out).toContain('font-family: "Montserrat"');
51+
});
52+
53+
it("still takes a lone var() as the size when no literal size follows", async () => {
54+
const html = `<html><head><style>
55+
:root { --size: 40px; }
56+
h1 { font: var(--size) Montserrat; }
57+
</style></head><body><h1>hi</h1></body></html>`;
58+
59+
expect(await inject(html)).toContain('font-family: "Montserrat"');
60+
});
61+
62+
it("serves a class-style family name from the family it names", async () => {
63+
const html = `<html><head></head><body>
64+
<div style="font-family: font-Montserrat-400">hi</div>
65+
</body></html>`;
66+
67+
const out = await inject(html);
68+
69+
expect(MONTSERRAT_400).toBeDefined();
70+
expect(out).toContain('font-family: "font-Montserrat-400"');
71+
expect(out).toContain(`url("${MONTSERRAT_400}")`);
72+
});
73+
});

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,27 @@ function findDeclaredFamilyAlias(
990990
return match;
991991
}
992992

993+
// Design exports name a family by class, `font-<Family>-<weight>`; serve all <Family> faces under that name.
994+
const WEIGHTED_CLASS_FAMILY = /^font-(.+)-[1-9]00$/i;
995+
996+
async function resolveWeightedClassFamily(
997+
authoredFamily: string,
998+
optional: boolean,
999+
options: InternalFontFetchOptions,
1000+
fontText?: string,
1001+
): Promise<string[] | null> {
1002+
const family = WEIGHTED_CLASS_FAMILY.exec(authoredFamily)?.[1];
1003+
if (!family) return null;
1004+
return resolveFamilyFaceRules(
1005+
normalizeFamilyName(family),
1006+
family,
1007+
authoredFamily,
1008+
optional,
1009+
options,
1010+
fontText,
1011+
);
1012+
}
1013+
9931014
// Emits under the authored name instead of rewriting usages, as FONT_ALIASES does,
9941015
// so script-assigned and var()-indirected usages match too.
9951016
async function resolveDeclaredFamilyAlias(
@@ -1065,7 +1086,8 @@ async function buildFontFaceCss(
10651086
optional,
10661087
options,
10671088
fontText,
1068-
));
1089+
)) ??
1090+
(await resolveWeightedClassFamily(originalCaseFamily, optional, options, fontText));
10691091
options.diag.endFamily(Boolean(familyRules));
10701092
if (familyRules) {
10711093
rules.push(...familyRules);
@@ -1823,6 +1845,7 @@ export interface InjectDeterministicFontFacesOptions {
18231845
* planning; tests may lower delays and timeouts without replacing timers.
18241846
*/
18251847
fontFetchRetryPolicy?: Partial<FontFetchRetryPolicy>;
1848+
projectFontFamilies?: readonly string[];
18261849
/**
18271850
* When `true` (default for local renders), fonts that aren't resolved by
18281851
* the bundled alias map or Google Fonts are located on the local filesystem,
@@ -2029,6 +2052,8 @@ export async function injectDeterministicFontFaces(
20292052
};
20302053

20312054
const existingFaces = extractExistingFontFaces(html);
2055+
for (const family of options.projectFontFamilies ?? [])
2056+
existingFaces.add(normalizeFamilyName(family));
20322057
const requestedFamilies = extractRequestedFontFamilies(html);
20332058
const pendingFamilies = new Map<string, ResolvedFamily>();
20342059

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// @vitest-environment node
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3+
import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
6+
import { compileForRender } from "./htmlCompiler.js";
7+
8+
vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({
9+
...(await importOriginal<typeof import("../utils/urlDownloader.js")>()),
10+
downloadToTemp: async () => {
11+
throw new Error("offline test");
12+
},
13+
fetchPublicHttpsText: async () =>
14+
`@font-face { font-family: "Project Display"; src: url("https://cdn.example.com/display.woff2"); }`,
15+
}));
16+
17+
// A distributed render must never ask the network for a font the project itself ships.
18+
const FONT_FILE = join(
19+
__dirname,
20+
"../../tests/heygen-promo-preview-assets/src/TT_Norms_Pro_Medium.woff2",
21+
);
22+
23+
const page = (head: string, body = "") => `<!doctype html>
24+
<html><head>${head}</head>
25+
<body><div id="root" data-composition-id="root" data-width="320" data-height="180" data-duration="1">
26+
<h1 style="font-family: 'Project Display', sans-serif">hello</h1>${body}
27+
</div></body></html>`;
28+
29+
const FACE = `@font-face { font-family: "Project Display"; src: url("fonts/display.woff2") format("woff2"); }`;
30+
31+
describe("compileForRender with fonts shipped in the project", () => {
32+
let projectDir: string;
33+
let fetchSpy: ReturnType<typeof vi.fn>;
34+
35+
beforeEach(() => {
36+
projectDir = mkdtempSync(join(tmpdir(), "hf-project-fonts-"));
37+
mkdirSync(join(projectDir, "fonts"));
38+
copyFileSync(FONT_FILE, join(projectDir, "fonts/display.woff2"));
39+
fetchSpy = vi.fn(async () => new Response("", { status: 400 }));
40+
vi.stubGlobal("fetch", fetchSpy);
41+
});
42+
43+
afterEach(() => {
44+
vi.unstubAllGlobals();
45+
rmSync(projectDir, { recursive: true, force: true });
46+
});
47+
48+
const compile = (html: string) => {
49+
writeFileSync(join(projectDir, "index.html"), html);
50+
return compileForRender(projectDir, join(projectDir, "index.html"), join(projectDir, ".dl"), {
51+
failClosedFontFetch: true,
52+
allowSystemFontCapture: false,
53+
});
54+
};
55+
56+
it("accepts a family whose @font-face in the page points at a project file", async () => {
57+
const { html } = await compile(page(`<style>${FACE}</style>`));
58+
expect(html).toContain("Project Display");
59+
expect(html).not.toContain('url("fonts/display.woff2")');
60+
expect(fetchSpy).not.toHaveBeenCalled();
61+
});
62+
63+
it("accepts a family whose @font-face lives in a project stylesheet", async () => {
64+
writeFileSync(join(projectDir, "fonts.css"), FACE);
65+
const { html } = await compile(page(`<link rel="stylesheet" href="fonts.css">`));
66+
expect(html).toContain('<link rel="stylesheet" href="fonts.css">');
67+
expect(fetchSpy).not.toHaveBeenCalled();
68+
});
69+
70+
it("resolves a project stylesheet's font urls against the stylesheet's folder", async () => {
71+
mkdirSync(join(projectDir, "css"));
72+
writeFileSync(join(projectDir, "css/fonts.css"), FACE.replace("fonts/", "../fonts/"));
73+
const { html } = await compile(page(`<link rel="stylesheet" href="./css/fonts.css">`));
74+
expect(html).toContain('<link rel="stylesheet" href="./css/fonts.css">');
75+
expect(fetchSpy).not.toHaveBeenCalled();
76+
});
77+
78+
it("still fails closed when a project stylesheet names a font file the project lacks", async () => {
79+
writeFileSync(
80+
join(projectDir, "fonts.css"),
81+
FACE.replace("fonts/display.woff2", "fonts/missing.woff2"),
82+
);
83+
await expect(compile(page(`<link rel="stylesheet" href="fonts.css">`))).rejects.toThrow(
84+
/Unresolved fonts in fail-closed mode: Project Display/,
85+
);
86+
});
87+
88+
it("accepts a family whose @font-face lives in a remote stylesheet", async () => {
89+
const { html } = await compile(
90+
page(`<link rel="stylesheet" href="https://cdn.example.com/fonts.css">`),
91+
);
92+
expect(html).toContain("Project Display");
93+
});
94+
});

‎packages/producer/src/services/htmlCompiler.ts‎

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { decodedUrlPath } from "@hyperframes/parsers/asset-paths";
1212
*/
1313

1414
import { createHash } from "node:crypto";
15-
import { createReadStream, existsSync, mkdirSync } from "fs";
15+
import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs";
1616
import { join, dirname, resolve, basename, relative } from "path";
1717
import { parseHTML } from "linkedom";
1818
import {
@@ -1747,6 +1747,32 @@ async function inlineExternalFontStylesheets(html: string): Promise<string> {
17471747
return result;
17481748
}
17491749

1750+
// Families a linked project stylesheet declares with an @font-face file the project ships.
1751+
function projectStylesheetFontFamilies(html: string, projectDir: string): string[] {
1752+
const families: string[] = [];
1753+
for (const [tag] of html.matchAll(/<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi)) {
1754+
const href = /\bhref=["']([^"']+)["']/i.exec(tag)?.[1];
1755+
if (!href || /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(href)) continue;
1756+
const sheet = resolve(projectDir, decodedUrlPath(href).replace(/^(?:\.?\/)+/, ""));
1757+
if (!isPathInside(sheet, projectDir) || !existsSync(sheet)) continue;
1758+
for (const face of extractFontFaceBlocks(readFileSync(sheet, "utf8"))) {
1759+
const family = /font-family\s*:\s*([^;}]+)/i
1760+
.exec(face)?.[1]
1761+
?.trim()
1762+
.replace(/^['"]|['"]$/g, "");
1763+
const shipsFile = [...face.matchAll(LOCAL_FONTFACE_URL_RE)].some(([, url = ""]) => {
1764+
const path = decodedUrlPath(url);
1765+
const file = path.startsWith("/")
1766+
? resolve(projectDir, `.${path}`)
1767+
: resolve(dirname(sheet), path);
1768+
return isPathInside(file, projectDir) && existsSync(file);
1769+
});
1770+
if (family && shipsFile) families.push(family);
1771+
}
1772+
}
1773+
return families;
1774+
}
1775+
17501776
/**
17511777
* Collect all remote `url(https://...)` references inside `@font-face` blocks
17521778
* found in `<style>` tags.
@@ -1791,16 +1817,17 @@ export async function localizeRemoteFontFaces(
17911817
html: string,
17921818
downloadDir: string,
17931819
): Promise<{ html: string; remoteMediaAssets: Map<string, string> }> {
1794-
// Phase 1: Inline @font-face rules from external <link rel="stylesheet"> tags.
1795-
const processed = await inlineExternalFontStylesheets(html);
1796-
1797-
// Phase 2: Download font file URLs from all @font-face blocks (both
1798-
// pre-existing inline blocks and the ones just inlined from external sheets).
1799-
const urlSet = collectFontFaceUrls(processed);
1820+
return localizeFontFaceUrls(await inlineExternalFontStylesheets(html), downloadDir);
1821+
}
18001822

1823+
/** Download the remote font files of the page's inline @font-face rules and point the rules at them. */
1824+
function localizeFontFaceUrls(
1825+
html: string,
1826+
downloadDir: string,
1827+
): Promise<{ html: string; remoteMediaAssets: Map<string, string> }> {
18011828
return downloadAndRewriteUrls(
1802-
urlSet,
1803-
processed,
1829+
collectFontFaceUrls(html),
1830+
html,
18041831
join(downloadDir, REMOTE_MEDIA_SUBDIR),
18051832
"Remote font download failed",
18061833
"Localized remote font face(s)",
@@ -2102,16 +2129,19 @@ export async function compileForRender(
21022129
const usesThreeDTransforms = detectThreeDTransformUsage(sanitizedHtml);
21032130
const hasAncestorBackgroundImage = detectAncestorBackgroundImage(sanitizedHtml);
21042131

2132+
// Remote font stylesheets join the page first, or the font check below cannot see them.
2133+
const fontSheetHtml = await inlineExternalFontStylesheets(
2134+
promoteCssImportsToLinkTags(sanitizedHtml),
2135+
);
21052136
const normalizedFontHtml = normalizeSystemFontPrimaryFamilies(
2106-
injectTextRenderingRule(
2107-
coalesceHeadStylesAndBodyScripts(promoteCssImportsToLinkTags(sanitizedHtml)),
2108-
),
2137+
injectTextRenderingRule(coalesceHeadStylesAndBodyScripts(fontSheetHtml)),
21092138
);
21102139

21112140
const coalescedHtml = await injectDeterministicFontFaces(normalizedFontHtml, {
21122141
logger: defaultLogger,
21132142
failClosedFontFetch: options.failClosedFontFetch === true,
21142143
allowSystemFontCapture: options.allowSystemFontCapture,
2144+
projectFontFamilies: projectStylesheetFontFamilies(fontSheetHtml, projectDir),
21152145
abortSignal: options.abortSignal,
21162146
});
21172147

@@ -2168,7 +2198,7 @@ export async function compileForRender(
21682198
// allow http://localhost:PORT as origin), causing Chrome to silently fall
21692199
// back to the next font in the stack.
21702200
const { html: htmlWithLocalizedFonts, remoteMediaAssets: remoteFontAssets } =
2171-
await localizeRemoteFontFaces(htmlWithLocalBg, downloadDir);
2201+
await localizeFontFaceUrls(htmlWithLocalBg, downloadDir);
21722202

21732203
// Chrome cannot decode HEIC, and ffprobe reads a tiled one as video streams,
21742204
// so convert to JPEG before the media-type preflight sees it.

0 commit comments

Comments
 (0)