Skip to content

Commit cba66f2

Browse files
feat(cli): cloud render carries the system fonts a project uses in its zip (heygen-com#7165)
* feat(cli): cloud render stops before upload when the cloud lacks a font * refactor(cli): shorten the font preflight comments * feat(cli): cloud render carries the system fonts a project uses in its zip * fix(cli): say only what is known when a system font's file is not found * fix(core): find a system font by the family names inside its file, including .ttc faces * fix(core): look inside font files only when asked, keep regular and bold, cut comments
1 parent 06722eb commit cba66f2

12 files changed

Lines changed: 754 additions & 8 deletions
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { mkdtempSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { describe, expect, it } from "vitest";
5+
import { describeUnresolvedFonts, embedSystemFonts } from "./fontPreflight.js";
6+
7+
const PAGE = `<html><head><style>h1{font-family:"PingFang SC"}</style></head><body><h1>x</h1></body></html>`;
8+
9+
function setup() {
10+
const dir = mkdtempSync(join(tmpdir(), "hf-font-pre-"));
11+
writeFileSync(join(dir, "index.html"), PAGE);
12+
return { dir, entryPath: "index.html", fileMap: new Map([["index.html", Buffer.from(PAGE)]]) };
13+
}
14+
15+
const missing = (families: string[]) =>
16+
Object.assign(new Error("x"), { code: "FONT_FETCH_FAILED", unresolvedFamilies: families });
17+
18+
describe("embedSystemFonts", () => {
19+
it("puts a system font the cloud lacks into the entry page as an inline @font-face", async () => {
20+
const s = setup();
21+
const calls: Record<string, unknown>[] = [];
22+
const probes: string[] = [];
23+
const left = await embedSystemFonts(s, async (html, options) => {
24+
calls.push(options as Record<string, unknown>);
25+
probes.push(html);
26+
if ((options as { allowSystemFontCapture: boolean }).allowSystemFontCapture)
27+
return `${html}<style>@font-face{font-family:"PingFang SC";src:url(data:font/woff2;base64,AAAA)}</style>`;
28+
throw missing(["PingFang SC"]);
29+
});
30+
expect(left).toEqual([]);
31+
expect(calls.map((c) => c.allowSystemFontCapture)).toEqual([false, true]);
32+
expect(calls.map((c) => c.systemFontsByInnerName)).toEqual([undefined, true]);
33+
// The resolver only injects into a full document, so the probe for the capture pass must be one.
34+
expect(probes[1]).toContain("<head>");
35+
const entry = s.fileMap.get("index.html")!.toString();
36+
expect(entry).toContain('<style data-hf-system-fonts>@font-face{font-family:"PingFang SC"');
37+
expect(entry.indexOf("data-hf-system-fonts")).toBeLessThan(entry.indexOf("</head>"));
38+
});
39+
40+
it("returns the fonts whose file is not on this machine and leaves the page alone", async () => {
41+
const s = setup();
42+
const left = await embedSystemFonts(s, async () => {
43+
throw missing(["PingFang SC"]);
44+
});
45+
expect(left).toEqual(["PingFang SC"]);
46+
expect(s.fileMap.get("index.html")!.toString()).toBe(PAGE);
47+
});
48+
49+
it("changes nothing when every font resolves", async () => {
50+
const s = setup();
51+
expect(await embedSystemFonts(s, async (html) => html)).toEqual([]);
52+
expect(s.fileMap.get("index.html")!.toString()).toBe(PAGE);
53+
});
54+
55+
it("does not block on a network failure: the server decides", async () => {
56+
const s = setup();
57+
const left = await embedSystemFonts(s, async () => {
58+
throw Object.assign(new Error("x"), { code: "FONT_FETCH_UNAVAILABLE" });
59+
});
60+
expect(left).toEqual([]);
61+
});
62+
});
63+
64+
describe("describeUnresolvedFonts", () => {
65+
it("names the font and a look-alike when there is one", () => {
66+
expect(describeUnresolvedFonts(["PingFang SC", "Zapfino"])).toBe(
67+
"HyperFrames couldn't find a font file for PingFang SC on this computer, so the cloud can't have it: use Noto Sans SC, switch to a font in your project or render on this computer.\n" +
68+
"HyperFrames couldn't find a font file for Zapfino on this computer, so the cloud can't have it: switch to a font in your project or render on this computer.",
69+
);
70+
});
71+
});
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Carries the system fonts a project uses inside its zip, as the cloud has no copy of them.
2+
import { bundleWithLocalizedFonts } from "../utils/bundleWithLocalizedFonts.js";
3+
import { loadProducer } from "../utils/producer.js";
4+
import { readStaged, type StagedFile } from "../utils/publishProject.js";
5+
6+
// Free look-alikes for system fonts the cloud lacks.
7+
const LOOK_ALIKES: Record<string, string> = {
8+
"pingfang sc": "Noto Sans SC",
9+
"pingfang tc": "Noto Sans TC",
10+
"pingfang hk": "Noto Sans HK",
11+
"hiragino sans": "Noto Sans JP",
12+
"hiragino kaku gothic pro": "Noto Sans JP",
13+
"yu gothic": "Noto Sans JP",
14+
"apple sd gothic neo": "Noto Sans KR",
15+
"malgun gothic": "Noto Sans KR",
16+
"microsoft yahei": "Noto Sans SC",
17+
"microsoft jhenghei": "Noto Sans TC",
18+
"sf pro": "Inter",
19+
"sf pro display": "Inter",
20+
"sf pro text": "Inter",
21+
"helvetica neue": "Inter",
22+
helvetica: "Inter",
23+
arial: "Inter",
24+
"segoe ui": "Inter",
25+
georgia: "Lora",
26+
"times new roman": "Tinos",
27+
menlo: "JetBrains Mono",
28+
consolas: "JetBrains Mono",
29+
};
30+
31+
function lookAlikeFor(family: string): string | undefined {
32+
return LOOK_ALIKES[family.trim().toLowerCase()];
33+
}
34+
35+
type Injector = (html: string, options: object) => Promise<string>;
36+
37+
const QUIET = { warn() {}, info() {} };
38+
39+
async function injectorOr(inject?: Injector): Promise<Injector> {
40+
return (
41+
inject ??
42+
((await loadProducer()) as { injectDeterministicFontFaces: Injector })
43+
.injectDeterministicFontFaces
44+
);
45+
}
46+
47+
/** The families the resolver names in a fail-closed error, or null when it failed another way. */
48+
function namedFamilies(err: unknown): string[] | null {
49+
const e = err as { code?: unknown; unresolvedFamilies?: unknown };
50+
if (e.code !== "FONT_FETCH_FAILED" || !Array.isArray(e.unresolvedFamilies)) return null;
51+
return e.unresolvedFamilies.filter((f): f is string => typeof f === "string");
52+
}
53+
54+
/** The families the cloud cannot resolve; empty when all resolve or it cannot tell. */
55+
async function unresolvedCloudFonts(
56+
dir: string,
57+
entryFile: string | undefined,
58+
run: Injector,
59+
): Promise<string[]> {
60+
try {
61+
await bundleWithLocalizedFonts(
62+
dir,
63+
(html) =>
64+
run(html, { failClosedFontFetch: true, allowSystemFontCapture: false, logger: QUIET }),
65+
{ entryFile },
66+
);
67+
return [];
68+
} catch (err) {
69+
// Only a named font counts; a network failure is the server's call.
70+
return namedFamilies(err) ?? [];
71+
}
72+
}
73+
74+
/**
75+
* Adds the system fonts the project uses to its entry page as inline @font-face rules, so the zip carries them.
76+
* Returns the families whose font file is not on this machine; those cannot render in the cloud.
77+
*/
78+
export async function embedSystemFonts(
79+
opts: { dir: string; entryFile?: string; entryPath: string; fileMap: Map<string, StagedFile> },
80+
inject?: Injector,
81+
): Promise<string[]> {
82+
const run = await injectorOr(inject);
83+
const wanted = await unresolvedCloudFonts(opts.dir, opts.entryFile, run);
84+
if (wanted.length === 0) return [];
85+
const css = wanted.map((f, i) => `.f${i}{font-family:"${f}"}`).join("");
86+
const probe = `<!doctype html><html><head><style>${css}</style></head><body></body></html>`;
87+
let out: string;
88+
try {
89+
out = await run(probe, {
90+
failClosedFontFetch: true,
91+
allowSystemFontCapture: true,
92+
systemFontsByInnerName: true,
93+
logger: QUIET,
94+
});
95+
} catch (err) {
96+
return namedFamilies(err) ?? wanted;
97+
}
98+
const faces = [...out.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g)]
99+
.map((m) => m[1] ?? "")
100+
.filter((css) => css.includes("@font-face"))
101+
.join("\n");
102+
const entry = opts.fileMap.get(opts.entryPath);
103+
if (!faces || !entry) return wanted;
104+
const html = readStaged(entry).toString("utf-8");
105+
const tag = `<style data-hf-system-fonts>${faces}</style>`;
106+
const at = html.search(/<\/head>/i);
107+
opts.fileMap.set(
108+
opts.entryPath,
109+
Buffer.from(at < 0 ? tag + html : html.slice(0, at) + tag + html.slice(at), "utf-8"),
110+
);
111+
return [];
112+
}
113+
114+
/** One line per font, with the swap when there is one. */
115+
export function describeUnresolvedFonts(families: readonly string[]): string {
116+
return families
117+
.map((family) => {
118+
const swap = lookAlikeFor(family);
119+
return `HyperFrames couldn't find a font file for ${family} on this computer, so the cloud can't have it: ${
120+
swap ? `use ${swap}, ` : ""
121+
}switch to a font in your project or render on this computer.`;
122+
})
123+
.join("\n");
124+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { runCommand } from "citty";
2+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6+
7+
vi.mock("../../cloud/fontPreflight.js", async (importOriginal) => ({
8+
...(await importOriginal<typeof import("../../cloud/fontPreflight.js")>()),
9+
embedSystemFonts: vi.fn(async () => ["PingFang SC"]),
10+
}));
11+
12+
import render from "./render.js";
13+
import { errorBox } from "../../ui/format.js";
14+
import { CliRuntimeError, consumeCommandResult } from "../../utils/commandResult.js";
15+
16+
vi.mock("../../ui/format.js", async (importOriginal) => ({
17+
...(await importOriginal<typeof import("../../ui/format.js")>()),
18+
errorBox: vi.fn(),
19+
}));
20+
21+
let project: string;
22+
const fetchSpy = vi.fn();
23+
24+
beforeEach(() => {
25+
project = mkdtempSync(join(tmpdir(), "hf-cloud-fonts-"));
26+
writeFileSync(
27+
join(project, "index.html"),
28+
`<!doctype html><html><body><div data-composition-id="main" data-width="1920" data-height="1080" data-duration="1"></div></body></html>`,
29+
);
30+
vi.stubEnv("HEYGEN_API_KEY", "fixture-key");
31+
vi.stubGlobal("fetch", fetchSpy);
32+
});
33+
34+
afterEach(() => {
35+
rmSync(project, { recursive: true, force: true });
36+
vi.unstubAllEnvs();
37+
vi.unstubAllGlobals();
38+
fetchSpy.mockClear();
39+
consumeCommandResult();
40+
});
41+
42+
describe("cloud render font preflight", () => {
43+
it("stops before any upload and names the font that is not on this computer", async () => {
44+
await expect(runCommand(render, { rawArgs: [project, "--no-wait", "--json"] })).rejects.toThrow(
45+
CliRuntimeError,
46+
);
47+
48+
expect(fetchSpy).not.toHaveBeenCalled();
49+
expect(errorBox).toHaveBeenCalledWith(
50+
"Fonts not on this computer",
51+
expect.stringContaining(
52+
"HyperFrames couldn't find a font file for PingFang SC on this computer, so the cloud can't have it: use Noto Sans SC",
53+
),
54+
);
55+
});
56+
});

‎packages/cli/src/commands/cloud/render.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import {
7272
pollUntilTerminal,
7373
} from "../../cloud/index.js";
7474
import { reportApiError } from "../../cloud/errors.js";
75+
import { describeUnresolvedFonts, embedSystemFonts } from "../../cloud/fontPreflight.js";
7576
import { parseEnumFlag, parseIntFlag, parseNumericFlag } from "../../cloud/parsing.js";
7677
import { uploadAssetViaDirectUpload, uploadZipViaDirectUpload } from "../../cloud/upload.js";
7778
import {
@@ -669,6 +670,16 @@ async function prepareLocalArchive(
669670
console.log(`${c.accent("◆")} Zipping ${c.accent(project.name)}`);
670671
}
671672
const { fileMap, entryPath } = stageProjectFiles(project, composition);
673+
const missingFonts = await embedSystemFonts({
674+
dir: project.dir,
675+
entryFile: composition,
676+
entryPath,
677+
fileMap,
678+
});
679+
if (missingFonts.length > 0) {
680+
errorBox("Fonts not on this computer", describeUnresolvedFonts(missingFonts));
681+
failCommand();
682+
}
672683
const offloaded = await offloadMedia(project, fileMap, entryPath, asJson, upload, variablesText);
673684

674685
// Media left on disk barely compresses: past the limit, its size is the report and nothing is read.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ describe("authored Google font stylesheet", () => {
618618
expect(result).toContain(b64(LOCAL_FONT_BYTES));
619619
expect(systemFontLocator.locateSystemFontVariants).toHaveBeenCalledWith(
620620
"Hf Authored Fail Test",
621+
{ byInnerName: false },
621622
);
622623
});
623624
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @vitest-environment node
2+
import { writeFileSync } from "node:fs";
3+
import { join } from "node:path";
4+
import { describe, expect, it } from "vitest";
5+
import { fixtureFontDir, makeTtc } from "./sfntFixture.js";
6+
7+
const fonts = fixtureFontDir();
8+
writeFileSync(
9+
join(fonts, "PingFang.ttc"),
10+
makeTtc([{ families: ["PingFang SC"] }, { families: ["PingFang TC"] }]).file,
11+
);
12+
13+
const { injectDeterministicFontFaces } = await import("./deterministicFonts.js");
14+
15+
describe("system font capture of a family inside a .ttc collection", () => {
16+
it("embeds that face alone, found by the name inside the file", async () => {
17+
const html = `<!doctype html><html><head><style>h1{font-family:"PingFang SC"}</style></head><body><h1>x</h1></body></html>`;
18+
const out = await injectDeterministicFontFaces(html, {
19+
failClosedFontFetch: true,
20+
allowSystemFontCapture: true,
21+
systemFontsByInnerName: true,
22+
logger: { warn() {}, info() {} },
23+
fetchImpl: (async () => new Response("", { status: 400 })) as unknown as typeof fetch,
24+
});
25+
const embedded = [...out.matchAll(/base64,([A-Za-z0-9+/=]+)/g)].map((m) =>
26+
Buffer.from(m[1]!, "base64").toString("latin1"),
27+
);
28+
expect(embedded).toHaveLength(1);
29+
expect(embedded[0]).toContain("glyph-data-PingFang SC");
30+
expect(embedded[0]).not.toContain("glyph-data-PingFang TC");
31+
});
32+
33+
it("embeds nothing from inside a collection unless asked", async () => {
34+
const html = `<!doctype html><html><head><style>h1{font-family:"PingFang SC"}</style></head><body><h1>x</h1></body></html>`;
35+
const out = await injectDeterministicFontFaces(html, {
36+
allowSystemFontCapture: true,
37+
logger: { warn() {}, info() {} },
38+
fetchImpl: (async () => new Response("", { status: 400 })) as unknown as typeof fetch,
39+
});
40+
expect(out).not.toContain("base64,");
41+
});
42+
});

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { tmpdir } from "node:os";
1212
import { join } from "node:path";
1313

1414
import { FONT_ALIAS_MAP, resolveAliasDisplayName } from "./aliases.js";
15+
import { extractSfntFace } from "./sfntNames.js";
1516
import { locateSystemFontVariants, SYSTEM_FONT_SIZE_LIMIT } from "./systemFontLocator.js";
1617
import { parseHTML } from "linkedom";
1718
import postcss, { type AtRule, type Declaration, type Rule } from "postcss";
@@ -804,15 +805,17 @@ async function systemFamilyFaceRules(
804805
lookupFamily: string,
805806
emitFamily: string,
806807
log: FontLogger,
808+
byInnerName: boolean,
807809
): Promise<string[] | null> {
808-
const variants = locateSystemFontVariants(lookupFamily);
810+
const variants = locateSystemFontVariants(lookupFamily, { byInnerName });
809811
if (variants.length === 0) return null;
810812
const rules: string[] = [];
811813
let totalBytes = 0;
812814
for (const variant of variants) {
813-
const fontBuffer = readFileSync(variant.path);
815+
const single = variant.face === undefined ? null : extractSfntFace(variant.path, variant.face);
816+
const fontBuffer = single?.bytes ?? readFileSync(variant.path);
814817
totalBytes += fontBuffer.length;
815-
const dataUri = await fontToDataUri(fontBuffer, variant.format);
818+
const dataUri = await fontToDataUri(fontBuffer, single?.format ?? variant.format);
816819
rules.push(buildFontFaceRule(emitFamily, dataUri, variant.weight, variant.style));
817820
}
818821
if (totalBytes > SYSTEM_FONT_SIZE_LIMIT) {
@@ -851,7 +854,12 @@ async function resolveFamilyFaceRules(
851854
return (
852855
(await googleFamilyFaceRules(lookupFamily, emitFamily, optional, options, fontText)) ??
853856
(options.allowSystemFontCapture && !pageNamedThisFile(lookupFamily, options)
854-
? await systemFamilyFaceRules(lookupFamily, emitFamily, options.log)
857+
? await systemFamilyFaceRules(
858+
lookupFamily,
859+
emitFamily,
860+
options.log,
861+
options.systemFontsByInnerName,
862+
)
855863
: null)
856864
);
857865
}
@@ -1276,6 +1284,7 @@ interface InternalFontFetchOptions {
12761284
failClosedFontFetch: boolean;
12771285
fetchImpl: typeof fetch;
12781286
allowSystemFontCapture: boolean;
1287+
systemFontsByInnerName: boolean;
12791288
abortSignal?: AbortSignal;
12801289
retryPolicy: FontFetchRetryPolicy;
12811290
retryDeadlineMs: number;
@@ -1847,6 +1856,7 @@ export interface InjectDeterministicFontFacesOptions {
18471856
* to contain the same fonts as the authoring machine.
18481857
*/
18491858
allowSystemFontCapture?: boolean;
1859+
systemFontsByInnerName?: boolean;
18501860
}
18511861

18521862
// Keep the complete CSS request under the broadly supported ~2 KB URL limit.
@@ -2035,6 +2045,7 @@ export async function injectDeterministicFontFaces(
20352045
failClosedFontFetch,
20362046
fetchImpl,
20372047
allowSystemFontCapture,
2048+
systemFontsByInnerName: options.systemFontsByInnerName === true,
20382049
abortSignal: options.abortSignal,
20392050
retryPolicy,
20402051
retryDeadlineMs: Date.now() + retryPolicy.maxElapsedMs,

0 commit comments

Comments
 (0)