mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
feat(cli): cloud render carries the system fonts a project uses in its zip (#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
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describeUnresolvedFonts, embedSystemFonts } from "./fontPreflight.js";
|
||||
|
||||
const PAGE = `<html><head><style>h1{font-family:"PingFang SC"}</style></head><body><h1>x</h1></body></html>`;
|
||||
|
||||
function setup() {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-font-pre-"));
|
||||
writeFileSync(join(dir, "index.html"), PAGE);
|
||||
return { dir, entryPath: "index.html", fileMap: new Map([["index.html", Buffer.from(PAGE)]]) };
|
||||
}
|
||||
|
||||
const missing = (families: string[]) =>
|
||||
Object.assign(new Error("x"), { code: "FONT_FETCH_FAILED", unresolvedFamilies: families });
|
||||
|
||||
describe("embedSystemFonts", () => {
|
||||
it("puts a system font the cloud lacks into the entry page as an inline @font-face", async () => {
|
||||
const s = setup();
|
||||
const calls: Record<string, unknown>[] = [];
|
||||
const probes: string[] = [];
|
||||
const left = await embedSystemFonts(s, async (html, options) => {
|
||||
calls.push(options as Record<string, unknown>);
|
||||
probes.push(html);
|
||||
if ((options as { allowSystemFontCapture: boolean }).allowSystemFontCapture)
|
||||
return `${html}<style>@font-face{font-family:"PingFang SC";src:url(data:font/woff2;base64,AAAA)}</style>`;
|
||||
throw missing(["PingFang SC"]);
|
||||
});
|
||||
expect(left).toEqual([]);
|
||||
expect(calls.map((c) => c.allowSystemFontCapture)).toEqual([false, true]);
|
||||
expect(calls.map((c) => c.systemFontsByInnerName)).toEqual([undefined, true]);
|
||||
// The resolver only injects into a full document, so the probe for the capture pass must be one.
|
||||
expect(probes[1]).toContain("<head>");
|
||||
const entry = s.fileMap.get("index.html")!.toString();
|
||||
expect(entry).toContain('<style data-hf-system-fonts>@font-face{font-family:"PingFang SC"');
|
||||
expect(entry.indexOf("data-hf-system-fonts")).toBeLessThan(entry.indexOf("</head>"));
|
||||
});
|
||||
|
||||
it("returns the fonts whose file is not on this machine and leaves the page alone", async () => {
|
||||
const s = setup();
|
||||
const left = await embedSystemFonts(s, async () => {
|
||||
throw missing(["PingFang SC"]);
|
||||
});
|
||||
expect(left).toEqual(["PingFang SC"]);
|
||||
expect(s.fileMap.get("index.html")!.toString()).toBe(PAGE);
|
||||
});
|
||||
|
||||
it("changes nothing when every font resolves", async () => {
|
||||
const s = setup();
|
||||
expect(await embedSystemFonts(s, async (html) => html)).toEqual([]);
|
||||
expect(s.fileMap.get("index.html")!.toString()).toBe(PAGE);
|
||||
});
|
||||
|
||||
it("does not block on a network failure: the server decides", async () => {
|
||||
const s = setup();
|
||||
const left = await embedSystemFonts(s, async () => {
|
||||
throw Object.assign(new Error("x"), { code: "FONT_FETCH_UNAVAILABLE" });
|
||||
});
|
||||
expect(left).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("describeUnresolvedFonts", () => {
|
||||
it("names the font and a look-alike when there is one", () => {
|
||||
expect(describeUnresolvedFonts(["PingFang SC", "Zapfino"])).toBe(
|
||||
"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" +
|
||||
"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.",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
// Carries the system fonts a project uses inside its zip, as the cloud has no copy of them.
|
||||
import { bundleWithLocalizedFonts } from "../utils/bundleWithLocalizedFonts.js";
|
||||
import { loadProducer } from "../utils/producer.js";
|
||||
import { readStaged, type StagedFile } from "../utils/publishProject.js";
|
||||
|
||||
// Free look-alikes for system fonts the cloud lacks.
|
||||
const LOOK_ALIKES: Record<string, string> = {
|
||||
"pingfang sc": "Noto Sans SC",
|
||||
"pingfang tc": "Noto Sans TC",
|
||||
"pingfang hk": "Noto Sans HK",
|
||||
"hiragino sans": "Noto Sans JP",
|
||||
"hiragino kaku gothic pro": "Noto Sans JP",
|
||||
"yu gothic": "Noto Sans JP",
|
||||
"apple sd gothic neo": "Noto Sans KR",
|
||||
"malgun gothic": "Noto Sans KR",
|
||||
"microsoft yahei": "Noto Sans SC",
|
||||
"microsoft jhenghei": "Noto Sans TC",
|
||||
"sf pro": "Inter",
|
||||
"sf pro display": "Inter",
|
||||
"sf pro text": "Inter",
|
||||
"helvetica neue": "Inter",
|
||||
helvetica: "Inter",
|
||||
arial: "Inter",
|
||||
"segoe ui": "Inter",
|
||||
georgia: "Lora",
|
||||
"times new roman": "Tinos",
|
||||
menlo: "JetBrains Mono",
|
||||
consolas: "JetBrains Mono",
|
||||
};
|
||||
|
||||
function lookAlikeFor(family: string): string | undefined {
|
||||
return LOOK_ALIKES[family.trim().toLowerCase()];
|
||||
}
|
||||
|
||||
type Injector = (html: string, options: object) => Promise<string>;
|
||||
|
||||
const QUIET = { warn() {}, info() {} };
|
||||
|
||||
async function injectorOr(inject?: Injector): Promise<Injector> {
|
||||
return (
|
||||
inject ??
|
||||
((await loadProducer()) as { injectDeterministicFontFaces: Injector })
|
||||
.injectDeterministicFontFaces
|
||||
);
|
||||
}
|
||||
|
||||
/** The families the resolver names in a fail-closed error, or null when it failed another way. */
|
||||
function namedFamilies(err: unknown): string[] | null {
|
||||
const e = err as { code?: unknown; unresolvedFamilies?: unknown };
|
||||
if (e.code !== "FONT_FETCH_FAILED" || !Array.isArray(e.unresolvedFamilies)) return null;
|
||||
return e.unresolvedFamilies.filter((f): f is string => typeof f === "string");
|
||||
}
|
||||
|
||||
/** The families the cloud cannot resolve; empty when all resolve or it cannot tell. */
|
||||
async function unresolvedCloudFonts(
|
||||
dir: string,
|
||||
entryFile: string | undefined,
|
||||
run: Injector,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
await bundleWithLocalizedFonts(
|
||||
dir,
|
||||
(html) =>
|
||||
run(html, { failClosedFontFetch: true, allowSystemFontCapture: false, logger: QUIET }),
|
||||
{ entryFile },
|
||||
);
|
||||
return [];
|
||||
} catch (err) {
|
||||
// Only a named font counts; a network failure is the server's call.
|
||||
return namedFamilies(err) ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the system fonts the project uses to its entry page as inline @font-face rules, so the zip carries them.
|
||||
* Returns the families whose font file is not on this machine; those cannot render in the cloud.
|
||||
*/
|
||||
export async function embedSystemFonts(
|
||||
opts: { dir: string; entryFile?: string; entryPath: string; fileMap: Map<string, StagedFile> },
|
||||
inject?: Injector,
|
||||
): Promise<string[]> {
|
||||
const run = await injectorOr(inject);
|
||||
const wanted = await unresolvedCloudFonts(opts.dir, opts.entryFile, run);
|
||||
if (wanted.length === 0) return [];
|
||||
const css = wanted.map((f, i) => `.f${i}{font-family:"${f}"}`).join("");
|
||||
const probe = `<!doctype html><html><head><style>${css}</style></head><body></body></html>`;
|
||||
let out: string;
|
||||
try {
|
||||
out = await run(probe, {
|
||||
failClosedFontFetch: true,
|
||||
allowSystemFontCapture: true,
|
||||
systemFontsByInnerName: true,
|
||||
logger: QUIET,
|
||||
});
|
||||
} catch (err) {
|
||||
return namedFamilies(err) ?? wanted;
|
||||
}
|
||||
const faces = [...out.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g)]
|
||||
.map((m) => m[1] ?? "")
|
||||
.filter((css) => css.includes("@font-face"))
|
||||
.join("\n");
|
||||
const entry = opts.fileMap.get(opts.entryPath);
|
||||
if (!faces || !entry) return wanted;
|
||||
const html = readStaged(entry).toString("utf-8");
|
||||
const tag = `<style data-hf-system-fonts>${faces}</style>`;
|
||||
const at = html.search(/<\/head>/i);
|
||||
opts.fileMap.set(
|
||||
opts.entryPath,
|
||||
Buffer.from(at < 0 ? tag + html : html.slice(0, at) + tag + html.slice(at), "utf-8"),
|
||||
);
|
||||
return [];
|
||||
}
|
||||
|
||||
/** One line per font, with the swap when there is one. */
|
||||
export function describeUnresolvedFonts(families: readonly string[]): string {
|
||||
return families
|
||||
.map((family) => {
|
||||
const swap = lookAlikeFor(family);
|
||||
return `HyperFrames couldn't find a font file for ${family} on this computer, so the cloud can't have it: ${
|
||||
swap ? `use ${swap}, ` : ""
|
||||
}switch to a font in your project or render on this computer.`;
|
||||
})
|
||||
.join("\n");
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { runCommand } from "citty";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("../../cloud/fontPreflight.js", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("../../cloud/fontPreflight.js")>()),
|
||||
embedSystemFonts: vi.fn(async () => ["PingFang SC"]),
|
||||
}));
|
||||
|
||||
import render from "./render.js";
|
||||
import { errorBox } from "../../ui/format.js";
|
||||
import { CliRuntimeError, consumeCommandResult } from "../../utils/commandResult.js";
|
||||
|
||||
vi.mock("../../ui/format.js", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("../../ui/format.js")>()),
|
||||
errorBox: vi.fn(),
|
||||
}));
|
||||
|
||||
let project: string;
|
||||
const fetchSpy = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
project = mkdtempSync(join(tmpdir(), "hf-cloud-fonts-"));
|
||||
writeFileSync(
|
||||
join(project, "index.html"),
|
||||
`<!doctype html><html><body><div data-composition-id="main" data-width="1920" data-height="1080" data-duration="1"></div></body></html>`,
|
||||
);
|
||||
vi.stubEnv("HEYGEN_API_KEY", "fixture-key");
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(project, { recursive: true, force: true });
|
||||
vi.unstubAllEnvs();
|
||||
vi.unstubAllGlobals();
|
||||
fetchSpy.mockClear();
|
||||
consumeCommandResult();
|
||||
});
|
||||
|
||||
describe("cloud render font preflight", () => {
|
||||
it("stops before any upload and names the font that is not on this computer", async () => {
|
||||
await expect(runCommand(render, { rawArgs: [project, "--no-wait", "--json"] })).rejects.toThrow(
|
||||
CliRuntimeError,
|
||||
);
|
||||
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
expect(errorBox).toHaveBeenCalledWith(
|
||||
"Fonts not on this computer",
|
||||
expect.stringContaining(
|
||||
"HyperFrames couldn't find a font file for PingFang SC on this computer, so the cloud can't have it: use Noto Sans SC",
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -72,6 +72,7 @@ import {
|
||||
pollUntilTerminal,
|
||||
} from "../../cloud/index.js";
|
||||
import { reportApiError } from "../../cloud/errors.js";
|
||||
import { describeUnresolvedFonts, embedSystemFonts } from "../../cloud/fontPreflight.js";
|
||||
import { parseEnumFlag, parseIntFlag, parseNumericFlag } from "../../cloud/parsing.js";
|
||||
import { uploadAssetViaDirectUpload, uploadZipViaDirectUpload } from "../../cloud/upload.js";
|
||||
import {
|
||||
@@ -669,6 +670,16 @@ async function prepareLocalArchive(
|
||||
console.log(`${c.accent("◆")} Zipping ${c.accent(project.name)}`);
|
||||
}
|
||||
const { fileMap, entryPath } = stageProjectFiles(project, composition);
|
||||
const missingFonts = await embedSystemFonts({
|
||||
dir: project.dir,
|
||||
entryFile: composition,
|
||||
entryPath,
|
||||
fileMap,
|
||||
});
|
||||
if (missingFonts.length > 0) {
|
||||
errorBox("Fonts not on this computer", describeUnresolvedFonts(missingFonts));
|
||||
failCommand();
|
||||
}
|
||||
const offloaded = await offloadMedia(project, fileMap, entryPath, asJson, upload, variablesText);
|
||||
|
||||
// Media left on disk barely compresses: past the limit, its size is the report and nothing is read.
|
||||
|
||||
@@ -618,6 +618,7 @@ describe("authored Google font stylesheet", () => {
|
||||
expect(result).toContain(b64(LOCAL_FONT_BYTES));
|
||||
expect(systemFontLocator.locateSystemFontVariants).toHaveBeenCalledWith(
|
||||
"Hf Authored Fail Test",
|
||||
{ byInnerName: false },
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// @vitest-environment node
|
||||
import { writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { fixtureFontDir, makeTtc } from "./sfntFixture.js";
|
||||
|
||||
const fonts = fixtureFontDir();
|
||||
writeFileSync(
|
||||
join(fonts, "PingFang.ttc"),
|
||||
makeTtc([{ families: ["PingFang SC"] }, { families: ["PingFang TC"] }]).file,
|
||||
);
|
||||
|
||||
const { injectDeterministicFontFaces } = await import("./deterministicFonts.js");
|
||||
|
||||
describe("system font capture of a family inside a .ttc collection", () => {
|
||||
it("embeds that face alone, found by the name inside the file", async () => {
|
||||
const html = `<!doctype html><html><head><style>h1{font-family:"PingFang SC"}</style></head><body><h1>x</h1></body></html>`;
|
||||
const out = await injectDeterministicFontFaces(html, {
|
||||
failClosedFontFetch: true,
|
||||
allowSystemFontCapture: true,
|
||||
systemFontsByInnerName: true,
|
||||
logger: { warn() {}, info() {} },
|
||||
fetchImpl: (async () => new Response("", { status: 400 })) as unknown as typeof fetch,
|
||||
});
|
||||
const embedded = [...out.matchAll(/base64,([A-Za-z0-9+/=]+)/g)].map((m) =>
|
||||
Buffer.from(m[1]!, "base64").toString("latin1"),
|
||||
);
|
||||
expect(embedded).toHaveLength(1);
|
||||
expect(embedded[0]).toContain("glyph-data-PingFang SC");
|
||||
expect(embedded[0]).not.toContain("glyph-data-PingFang TC");
|
||||
});
|
||||
|
||||
it("embeds nothing from inside a collection unless asked", async () => {
|
||||
const html = `<!doctype html><html><head><style>h1{font-family:"PingFang SC"}</style></head><body><h1>x</h1></body></html>`;
|
||||
const out = await injectDeterministicFontFaces(html, {
|
||||
allowSystemFontCapture: true,
|
||||
logger: { warn() {}, info() {} },
|
||||
fetchImpl: (async () => new Response("", { status: 400 })) as unknown as typeof fetch,
|
||||
});
|
||||
expect(out).not.toContain("base64,");
|
||||
});
|
||||
});
|
||||
@@ -12,6 +12,7 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { FONT_ALIAS_MAP, resolveAliasDisplayName } from "./aliases.js";
|
||||
import { extractSfntFace } from "./sfntNames.js";
|
||||
import { locateSystemFontVariants, SYSTEM_FONT_SIZE_LIMIT } from "./systemFontLocator.js";
|
||||
import { parseHTML } from "linkedom";
|
||||
import postcss, { type AtRule, type Declaration, type Rule } from "postcss";
|
||||
@@ -804,15 +805,17 @@ async function systemFamilyFaceRules(
|
||||
lookupFamily: string,
|
||||
emitFamily: string,
|
||||
log: FontLogger,
|
||||
byInnerName: boolean,
|
||||
): Promise<string[] | null> {
|
||||
const variants = locateSystemFontVariants(lookupFamily);
|
||||
const variants = locateSystemFontVariants(lookupFamily, { byInnerName });
|
||||
if (variants.length === 0) return null;
|
||||
const rules: string[] = [];
|
||||
let totalBytes = 0;
|
||||
for (const variant of variants) {
|
||||
const fontBuffer = readFileSync(variant.path);
|
||||
const single = variant.face === undefined ? null : extractSfntFace(variant.path, variant.face);
|
||||
const fontBuffer = single?.bytes ?? readFileSync(variant.path);
|
||||
totalBytes += fontBuffer.length;
|
||||
const dataUri = await fontToDataUri(fontBuffer, variant.format);
|
||||
const dataUri = await fontToDataUri(fontBuffer, single?.format ?? variant.format);
|
||||
rules.push(buildFontFaceRule(emitFamily, dataUri, variant.weight, variant.style));
|
||||
}
|
||||
if (totalBytes > SYSTEM_FONT_SIZE_LIMIT) {
|
||||
@@ -851,7 +854,12 @@ async function resolveFamilyFaceRules(
|
||||
return (
|
||||
(await googleFamilyFaceRules(lookupFamily, emitFamily, optional, options, fontText)) ??
|
||||
(options.allowSystemFontCapture && !pageNamedThisFile(lookupFamily, options)
|
||||
? await systemFamilyFaceRules(lookupFamily, emitFamily, options.log)
|
||||
? await systemFamilyFaceRules(
|
||||
lookupFamily,
|
||||
emitFamily,
|
||||
options.log,
|
||||
options.systemFontsByInnerName,
|
||||
)
|
||||
: null)
|
||||
);
|
||||
}
|
||||
@@ -1276,6 +1284,7 @@ interface InternalFontFetchOptions {
|
||||
failClosedFontFetch: boolean;
|
||||
fetchImpl: typeof fetch;
|
||||
allowSystemFontCapture: boolean;
|
||||
systemFontsByInnerName: boolean;
|
||||
abortSignal?: AbortSignal;
|
||||
retryPolicy: FontFetchRetryPolicy;
|
||||
retryDeadlineMs: number;
|
||||
@@ -1847,6 +1856,7 @@ export interface InjectDeterministicFontFacesOptions {
|
||||
* to contain the same fonts as the authoring machine.
|
||||
*/
|
||||
allowSystemFontCapture?: boolean;
|
||||
systemFontsByInnerName?: boolean;
|
||||
}
|
||||
|
||||
// Keep the complete CSS request under the broadly supported ~2 KB URL limit.
|
||||
@@ -2035,6 +2045,7 @@ export async function injectDeterministicFontFaces(
|
||||
failClosedFontFetch,
|
||||
fetchImpl,
|
||||
allowSystemFontCapture,
|
||||
systemFontsByInnerName: options.systemFontsByInnerName === true,
|
||||
abortSignal: options.abortSignal,
|
||||
retryPolicy,
|
||||
retryDeadlineMs: Date.now() + retryPolicy.maxElapsedMs,
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
// Builds small font files in memory so tests need no binary fixtures.
|
||||
import { mkdirSync, mkdtempSync } from "node:fs";
|
||||
import { afterAll } from "vitest";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
/** Points the locator's per-user font folder, on every platform, at an empty folder and returns it. */
|
||||
export function fixtureFontDir(): string {
|
||||
const home = mkdtempSync(join(tmpdir(), "hf-home-"));
|
||||
const set = {
|
||||
HOME: home,
|
||||
USERPROFILE: home,
|
||||
WINDIR: join(home, "Win"),
|
||||
LOCALAPPDATA: join(home, "Local"),
|
||||
};
|
||||
const before = Object.fromEntries(Object.keys(set).map((k) => [k, process.env[k]]));
|
||||
Object.assign(process.env, set);
|
||||
afterAll(() => {
|
||||
for (const [k, v] of Object.entries(before)) {
|
||||
if (v === undefined) delete process.env[k];
|
||||
else process.env[k] = v;
|
||||
}
|
||||
});
|
||||
const dir =
|
||||
process.platform === "win32"
|
||||
? join(home, "Local", "Microsoft", "Windows", "Fonts")
|
||||
: process.platform === "darwin"
|
||||
? join(home, "Library", "Fonts")
|
||||
: join(home, ".fonts");
|
||||
mkdirSync(dir, { recursive: true });
|
||||
return dir;
|
||||
}
|
||||
|
||||
interface FaceSpec {
|
||||
families: string[];
|
||||
weight?: number;
|
||||
italic?: boolean;
|
||||
flavor?: "ttf" | "otf";
|
||||
}
|
||||
|
||||
function nameTable(families: string[]): Buffer {
|
||||
const strings = families.map((f) => Buffer.from(f, "utf16le").swap16());
|
||||
const header = Buffer.alloc(6 + families.length * 12);
|
||||
header.writeUInt16BE(0, 0);
|
||||
header.writeUInt16BE(families.length, 2);
|
||||
header.writeUInt16BE(header.length, 4);
|
||||
let at = 0;
|
||||
strings.forEach((s, i) => {
|
||||
const r = 6 + i * 12;
|
||||
header.writeUInt16BE(3, r);
|
||||
header.writeUInt16BE(1, r + 2);
|
||||
header.writeUInt16BE(0x409, r + 4);
|
||||
header.writeUInt16BE(i === 0 ? 1 : 16, r + 6);
|
||||
header.writeUInt16BE(s.length, r + 8);
|
||||
header.writeUInt16BE(at, r + 10);
|
||||
at += s.length;
|
||||
});
|
||||
return Buffer.concat([header, ...strings]);
|
||||
}
|
||||
|
||||
function os2Table(weight: number, italic: boolean): Buffer {
|
||||
const t = Buffer.alloc(78);
|
||||
t.writeUInt16BE(weight, 4);
|
||||
t.writeUInt16BE(italic ? 1 : 0, 62);
|
||||
return t;
|
||||
}
|
||||
|
||||
/** One face: its tables laid out from `base`, so a collection can place it anywhere in the file. */
|
||||
function face(spec: FaceSpec, base: number): { head: Buffer; body: Buffer } {
|
||||
const tables: [string, Buffer][] = [
|
||||
["OS/2", os2Table(spec.weight ?? 400, spec.italic ?? false)],
|
||||
["glyf", Buffer.from("glyph-data-" + spec.families[0])],
|
||||
["name", nameTable(spec.families)],
|
||||
];
|
||||
const head = Buffer.alloc(12 + tables.length * 16);
|
||||
head.write(spec.flavor === "otf" ? "OTTO" : "\0\x01\0\0", 0, "latin1");
|
||||
head.writeUInt16BE(tables.length, 4);
|
||||
let at = base + head.length;
|
||||
const body: Buffer[] = [];
|
||||
tables.forEach(([tag, data], i) => {
|
||||
const r = 12 + i * 16;
|
||||
head.write(tag, r, "latin1");
|
||||
head.writeUInt32BE(0, r + 4);
|
||||
head.writeUInt32BE(at, r + 8);
|
||||
head.writeUInt32BE(data.length, r + 12);
|
||||
const padded = Buffer.alloc((data.length + 3) & ~3);
|
||||
data.copy(padded);
|
||||
body.push(padded);
|
||||
at += padded.length;
|
||||
});
|
||||
return { head, body: Buffer.concat(body) };
|
||||
}
|
||||
|
||||
export function makeSfnt(spec: FaceSpec): Buffer {
|
||||
const { head, body } = face(spec, 0);
|
||||
return Buffer.concat([head, body]);
|
||||
}
|
||||
|
||||
export function makeTtc(specs: FaceSpec[]): { file: Buffer; offsets: number[] } {
|
||||
let at = 12 + specs.length * 4;
|
||||
const offsets: number[] = [];
|
||||
const parts: Buffer[] = [];
|
||||
for (const spec of specs) {
|
||||
const { head, body } = face(spec, at);
|
||||
offsets.push(at);
|
||||
parts.push(head, body);
|
||||
at += head.length + body.length;
|
||||
}
|
||||
const header = Buffer.alloc(12 + specs.length * 4);
|
||||
header.write("ttcf", 0, "latin1");
|
||||
header.writeUInt32BE(0x00010000, 4);
|
||||
header.writeUInt32BE(specs.length, 8);
|
||||
offsets.forEach((o, i) => header.writeUInt32BE(o, 12 + i * 4));
|
||||
return { file: Buffer.concat([header, ...parts]), offsets };
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { extractSfntFace, readSfntFaces } from "./sfntNames.js";
|
||||
import { makeSfnt, makeTtc } from "./sfntFixture.js";
|
||||
|
||||
const dir = () => mkdtempSync(join(tmpdir(), "hf-sfnt-"));
|
||||
|
||||
describe("readSfntFaces", () => {
|
||||
it("reads the family names, weight and style of a single font", () => {
|
||||
const path = join(dir(), "a.ttf");
|
||||
writeFileSync(path, makeSfnt({ families: ["Segoe UI"], weight: 700, italic: true }));
|
||||
expect(readSfntFaces(path)).toEqual([
|
||||
{ offset: 0, families: ["Segoe UI"], weight: "700", style: "italic" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("reads every face of a collection, each with its own names", () => {
|
||||
const { file, offsets } = makeTtc([
|
||||
{ families: ["PingFang SC", "PingFang SC Regular"], weight: 400 },
|
||||
{ families: ["PingFang TC"], weight: 600 },
|
||||
]);
|
||||
const path = join(dir(), "PingFang.ttc");
|
||||
writeFileSync(path, file);
|
||||
expect(readSfntFaces(path)).toEqual([
|
||||
{
|
||||
offset: offsets[0],
|
||||
families: ["PingFang SC", "PingFang SC Regular"],
|
||||
weight: "400",
|
||||
style: "normal",
|
||||
},
|
||||
{ offset: offsets[1], families: ["PingFang TC"], weight: "600", style: "normal" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns nothing for a file that is not a font", () => {
|
||||
const path = join(dir(), "x.ttf");
|
||||
writeFileSync(path, "not a font");
|
||||
expect(readSfntFaces(path)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractSfntFace", () => {
|
||||
it("makes one collection member a standalone font with its own tables", () => {
|
||||
const { file, offsets } = makeTtc([
|
||||
{ families: ["PingFang SC"] },
|
||||
{ families: ["PingFang TC"] },
|
||||
]);
|
||||
const path = join(dir(), "PingFang.ttc");
|
||||
writeFileSync(path, file);
|
||||
const { bytes, format } = extractSfntFace(path, offsets[1]!);
|
||||
const alone = join(dir(), "alone.ttf");
|
||||
writeFileSync(alone, bytes);
|
||||
expect(format).toBe("ttf");
|
||||
expect(readSfntFaces(alone).map((f) => f.families)).toEqual([["PingFang TC"]]);
|
||||
expect(bytes.includes(Buffer.from("glyph-data-PingFang TC"))).toBe(true);
|
||||
expect(bytes.includes(Buffer.from("glyph-data-PingFang SC"))).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,135 @@
|
||||
import { closeSync, openSync, readFileSync, readSync } from "node:fs";
|
||||
|
||||
/** One face of a font file: where it starts and the family names written inside it. */
|
||||
export interface SfntFace {
|
||||
offset: number;
|
||||
families: string[];
|
||||
weight: string;
|
||||
style: "normal" | "italic";
|
||||
}
|
||||
|
||||
const NAME_FAMILY_IDS = new Set([1, 16, 21]);
|
||||
const MAX_FACES = 64;
|
||||
|
||||
function readAt(fd: number, position: number, length: number): Buffer {
|
||||
const buf = Buffer.alloc(length);
|
||||
const read = readSync(fd, buf, 0, length, position);
|
||||
return read === length ? buf : buf.subarray(0, read);
|
||||
}
|
||||
|
||||
function tableDirectory(
|
||||
fd: number,
|
||||
faceOffset: number,
|
||||
): Map<string, { offset: number; length: number }> {
|
||||
const header = readAt(fd, faceOffset, 12);
|
||||
const count = header.length === 12 ? header.readUInt16BE(4) : 0;
|
||||
const dir = readAt(fd, faceOffset + 12, count * 16);
|
||||
const tables = new Map<string, { offset: number; length: number }>();
|
||||
for (let i = 0; i + 16 <= dir.length; i += 16) {
|
||||
tables.set(dir.toString("latin1", i, i + 4), {
|
||||
offset: dir.readUInt32BE(i + 8),
|
||||
length: dir.readUInt32BE(i + 12),
|
||||
});
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
function familiesIn(name: Buffer): string[] {
|
||||
if (name.length < 6) return [];
|
||||
const count = name.readUInt16BE(2);
|
||||
const strings = name.readUInt16BE(4);
|
||||
const found = new Set<string>();
|
||||
for (let i = 0; i < count && 6 + i * 12 + 12 <= name.length; i++) {
|
||||
const at = 6 + i * 12;
|
||||
const platform = name.readUInt16BE(at);
|
||||
const nameId = name.readUInt16BE(at + 6);
|
||||
if (!NAME_FAMILY_IDS.has(nameId)) continue;
|
||||
const length = name.readUInt16BE(at + 8);
|
||||
const raw = name.subarray(
|
||||
strings + name.readUInt16BE(at + 10),
|
||||
strings + name.readUInt16BE(at + 10) + length,
|
||||
);
|
||||
// Windows (3) and Unicode (0) names are UTF-16BE; Macintosh (1) names are single-byte.
|
||||
const text =
|
||||
platform === 1 ? raw.toString("latin1") : Buffer.from(raw).swap16().toString("utf16le");
|
||||
if (text.trim()) found.add(text.trim());
|
||||
}
|
||||
return [...found];
|
||||
}
|
||||
|
||||
function readFace(fd: number, offset: number): SfntFace | null {
|
||||
const tables = tableDirectory(fd, offset);
|
||||
const nameTable = tables.get("name");
|
||||
if (!nameTable) return null;
|
||||
const families = familiesIn(readAt(fd, nameTable.offset, Math.min(nameTable.length, 1 << 20)));
|
||||
const os2Table = tables.get("OS/2");
|
||||
const os2 = os2Table
|
||||
? readAt(fd, os2Table.offset, Math.min(os2Table.length, 64))
|
||||
: Buffer.alloc(0);
|
||||
const weightClass = os2.length >= 6 ? os2.readUInt16BE(4) : 400;
|
||||
const italic = os2.length >= 64 && (os2.readUInt16BE(62) & 1) === 1;
|
||||
return {
|
||||
offset,
|
||||
families,
|
||||
weight: String(weightClass >= 100 && weightClass <= 900 ? weightClass : 400),
|
||||
style: italic ? "italic" : "normal",
|
||||
};
|
||||
}
|
||||
|
||||
/** Every face of a font file, with the family names inside it; a collection (.ttc) yields one per member. */
|
||||
export function readSfntFaces(path: string): SfntFace[] {
|
||||
let fd: number | undefined;
|
||||
try {
|
||||
fd = openSync(path, "r");
|
||||
const head = readAt(fd, 0, 12);
|
||||
if (head.length < 12) return [];
|
||||
const collection = head.toString("latin1", 0, 4) === "ttcf";
|
||||
const table = collection ? readAt(fd, 12, Math.min(head.readUInt32BE(8), MAX_FACES) * 4) : head;
|
||||
const offsets = collection
|
||||
? Array.from({ length: Math.floor(table.length / 4) }, (_, i) => table.readUInt32BE(i * 4))
|
||||
: [0];
|
||||
return offsets.map((offset) => readFace(fd!, offset)).filter((f): f is SfntFace => f !== null);
|
||||
} catch {
|
||||
return [];
|
||||
} finally {
|
||||
if (fd !== undefined) closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
/** One face of a font file as a standalone font, which a browser can load; a collection cannot be loaded whole. */
|
||||
export function extractSfntFace(
|
||||
path: string,
|
||||
faceOffset: number,
|
||||
): { bytes: Buffer; format: "ttf" | "otf" } {
|
||||
const file = readFileSync(path);
|
||||
const count = file.readUInt16BE(faceOffset + 4);
|
||||
const records = Array.from({ length: count }, (_, i) => {
|
||||
const at = faceOffset + 12 + i * 16;
|
||||
return {
|
||||
tag: file.subarray(at, at + 4),
|
||||
checksum: file.readUInt32BE(at + 4),
|
||||
data: file.subarray(
|
||||
file.readUInt32BE(at + 8),
|
||||
file.readUInt32BE(at + 8) + file.readUInt32BE(at + 12),
|
||||
),
|
||||
};
|
||||
});
|
||||
const dirEnd = 12 + count * 16;
|
||||
const parts: Buffer[] = [Buffer.alloc(dirEnd)];
|
||||
let next = dirEnd;
|
||||
const out = parts[0]!;
|
||||
file.copy(out, 0, faceOffset, faceOffset + 12);
|
||||
records.forEach((record, i) => {
|
||||
const at = 12 + i * 16;
|
||||
record.tag.copy(out, at);
|
||||
out.writeUInt32BE(record.checksum, at + 4);
|
||||
out.writeUInt32BE(next, at + 8);
|
||||
out.writeUInt32BE(record.data.length, at + 12);
|
||||
const padded = Buffer.alloc((record.data.length + 3) & ~3);
|
||||
record.data.copy(padded);
|
||||
parts.push(padded);
|
||||
next += padded.length;
|
||||
});
|
||||
const bytes = Buffer.concat(parts);
|
||||
return { bytes, format: bytes.toString("latin1", 0, 4) === "OTTO" ? "otf" : "ttf" };
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { fixtureFontDir, makeSfnt, makeTtc } from "./sfntFixture.js";
|
||||
|
||||
const fonts = fixtureFontDir();
|
||||
|
||||
const { clearSystemFontCache, locateSystemFontVariants } = await import("./systemFontLocator.js");
|
||||
|
||||
const INNER = { byInnerName: true };
|
||||
const ttc = makeTtc([
|
||||
{ families: ["PingFang SC"], weight: 400 },
|
||||
{ families: ["PingFang SC"], weight: 600 },
|
||||
{ families: ["PingFang TC"], weight: 400 },
|
||||
]);
|
||||
writeFileSync(join(fonts, "PingFang.ttc"), ttc.file);
|
||||
writeFileSync(join(fonts, "Georgia.ttf"), makeSfnt({ families: ["Georgia"] }));
|
||||
|
||||
afterEach(() => clearSystemFontCache());
|
||||
|
||||
describe("locateSystemFontVariants by the names inside the file", () => {
|
||||
it("finds PingFang SC in PingFang.ttc, one variant per face, each loadable alone", () => {
|
||||
const variants = locateSystemFontVariants("PingFang SC", INNER);
|
||||
expect(variants.map((v) => [v.weight, v.face, v.format])).toEqual([
|
||||
["400", ttc.offsets[0], "ttc"],
|
||||
["600", ttc.offsets[1], "ttc"],
|
||||
]);
|
||||
expect(variants.every((v) => v.path === join(fonts, "PingFang.ttc"))).toBe(true);
|
||||
});
|
||||
|
||||
it("does not return a face of another family in the same collection", () => {
|
||||
expect(locateSystemFontVariants("PingFang TC", INNER).map((v) => v.face)).toEqual([
|
||||
ttc.offsets[2],
|
||||
]);
|
||||
});
|
||||
|
||||
it("still matches by file name when the name is the family", () => {
|
||||
expect(locateSystemFontVariants("Georgia")).toEqual([
|
||||
{ path: join(fonts, "Georgia.ttf"), format: "ttf", weight: "400", style: "normal" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("does not look inside files unless asked, so preview and local renders are unchanged", () => {
|
||||
expect(locateSystemFontVariants("PingFang SC")).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps only the regular and the bold face of a family with many weights", () => {
|
||||
const many = makeTtc(
|
||||
[100, 300, 400, 500, 600, 800].map((weight) => ({ families: ["Heavy Sans"], weight })),
|
||||
);
|
||||
writeFileSync(join(fonts, "Hvy.ttc"), many.file);
|
||||
expect(locateSystemFontVariants("Heavy Sans", INNER).map((v) => v.weight)).toEqual([
|
||||
"400",
|
||||
"600",
|
||||
]);
|
||||
});
|
||||
|
||||
it("finds nothing for a family no file names", () => {
|
||||
expect(locateSystemFontVariants("Zapfino")).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { execFileSync } from "node:child_process";
|
||||
import { existsSync, lstatSync, readdirSync, realpathSync } from "node:fs";
|
||||
import { homedir, platform } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { readSfntFaces } from "./sfntNames.js";
|
||||
|
||||
export const SYSTEM_FONT_SIZE_LIMIT = 5 * 1024 * 1024;
|
||||
const FC_MATCH_TIMEOUT_MS = 3000;
|
||||
@@ -136,6 +137,7 @@ export function fontDirectories(): string[] {
|
||||
"/Library/Fonts",
|
||||
"/System/Library/Fonts",
|
||||
"/System/Library/Fonts/Supplemental",
|
||||
"/System/Library/AssetsV2",
|
||||
];
|
||||
}
|
||||
if (platform() === "win32") {
|
||||
@@ -168,15 +170,15 @@ export interface FontFileEntry {
|
||||
* path, filename, and derived family name. Shared by the font listing route
|
||||
* and the per-family locator to avoid duplicating the directory scan loop.
|
||||
*/
|
||||
export function collectFontFileEntries(dir: string, depth = 0): FontFileEntry[] {
|
||||
if (!existsSync(dir) || depth > 2) return [];
|
||||
export function collectFontFileEntries(dir: string, depth = 0, maxDepth = 2): FontFileEntry[] {
|
||||
if (!existsSync(dir) || depth > maxDepth) return [];
|
||||
const entries: FontFileEntry[] = [];
|
||||
|
||||
try {
|
||||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||
const fullPath = join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
entries.push(...collectFontFileEntries(fullPath, depth + 1));
|
||||
entries.push(...collectFontFileEntries(fullPath, depth + 1, maxDepth));
|
||||
continue;
|
||||
}
|
||||
if (!FONT_EXT_RE.test(entry.name)) continue;
|
||||
@@ -263,6 +265,7 @@ export function locateSystemFont(family: string): LocatedFont | null {
|
||||
export interface LocatedFontVariant extends LocatedFont {
|
||||
weight: string;
|
||||
style: "normal" | "italic";
|
||||
face?: number;
|
||||
}
|
||||
|
||||
const WEIGHT_TOKENS: Record<string, string> = {
|
||||
@@ -297,7 +300,10 @@ function inferWeightAndStyle(fileName: string): { weight: string; style: "normal
|
||||
return { weight: "400", style };
|
||||
}
|
||||
|
||||
export function locateSystemFontVariants(family: string): LocatedFontVariant[] {
|
||||
export function locateSystemFontVariants(
|
||||
family: string,
|
||||
options: { byInnerName?: boolean } = {},
|
||||
): LocatedFontVariant[] {
|
||||
const normalized = normalizeName(family);
|
||||
if (!normalized) return [];
|
||||
|
||||
@@ -310,9 +316,61 @@ export function locateSystemFontVariants(family: string): LocatedFontVariant[] {
|
||||
const { weight, style } = inferWeightAndStyle(c.path);
|
||||
variants.push({ path: c.path, format: c.format, weight, style });
|
||||
}
|
||||
if (variants.length === 0 && options.byInnerName) {
|
||||
return regularAndBold(dedupeVariants(variantsByInnerName(normalized)));
|
||||
}
|
||||
return dedupeVariants(variants);
|
||||
}
|
||||
|
||||
const INNER_NAME_MAX_DEPTH = 4;
|
||||
let innerNameIndex: Map<string, LocatedFontVariant[]> | null = null;
|
||||
|
||||
function innerNameVariants(entry: FontFileEntry): [string, LocatedFontVariant][] {
|
||||
const ext = entry.fileName.match(FONT_EXT_RE)?.[1]?.toLowerCase() ?? "";
|
||||
if (ext !== "ttf" && ext !== "otf" && ext !== "ttc") return [];
|
||||
const collection = ext === "ttc";
|
||||
return readSfntFaces(entry.path).flatMap((face) =>
|
||||
face.families.map((family): [string, LocatedFontVariant] => [
|
||||
normalizeName(family),
|
||||
{
|
||||
path: entry.path,
|
||||
format: collection ? "ttc" : extensionToFormat(ext),
|
||||
weight: face.weight,
|
||||
style: face.style,
|
||||
...(collection && { face: face.offset }),
|
||||
},
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
function buildInnerNameIndex(): Map<string, LocatedFontVariant[]> {
|
||||
const index = new Map<string, LocatedFontVariant[]>();
|
||||
const entries = fontDirectories().flatMap((dir) =>
|
||||
collectFontFileEntries(dir, 0, INNER_NAME_MAX_DEPTH),
|
||||
);
|
||||
for (const [family, variant] of entries.flatMap(innerNameVariants)) {
|
||||
index.set(family, [...(index.get(family) ?? []), variant]);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
function regularAndBold(variants: LocatedFontVariant[]): LocatedFontVariant[] {
|
||||
const nearest = (target: number) =>
|
||||
variants.reduce<LocatedFontVariant | undefined>(
|
||||
(best, v) =>
|
||||
!best || Math.abs(Number(v.weight) - target) < Math.abs(Number(best.weight) - target)
|
||||
? v
|
||||
: best,
|
||||
undefined,
|
||||
);
|
||||
return [...new Set([nearest(400), nearest(700)])].filter((v): v is LocatedFontVariant => !!v);
|
||||
}
|
||||
|
||||
function variantsByInnerName(normalized: string): LocatedFontVariant[] {
|
||||
innerNameIndex ??= buildInnerNameIndex();
|
||||
return innerNameIndex.get(normalized) ?? [];
|
||||
}
|
||||
|
||||
function dedupeVariants(variants: LocatedFontVariant[]): LocatedFontVariant[] {
|
||||
const seen = new Map<string, LocatedFontVariant>();
|
||||
for (const v of variants) {
|
||||
@@ -324,5 +382,6 @@ function dedupeVariants(variants: LocatedFontVariant[]): LocatedFontVariant[] {
|
||||
|
||||
export function clearSystemFontCache(): void {
|
||||
cache.clear();
|
||||
innerNameIndex = null;
|
||||
allowedDirsCache = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user