diff --git a/packages/cli/src/cloud/fontPreflight.test.ts b/packages/cli/src/cloud/fontPreflight.test.ts new file mode 100644 index 0000000000..e3c3c9a35c --- /dev/null +++ b/packages/cli/src/cloud/fontPreflight.test.ts @@ -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 = `

x

`; + +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[] = []; + const probes: string[] = []; + const left = await embedSystemFonts(s, async (html, options) => { + calls.push(options as Record); + probes.push(html); + if ((options as { allowSystemFontCapture: boolean }).allowSystemFontCapture) + return `${html}`; + 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(""); + const entry = s.fileMap.get("index.html")!.toString(); + expect(entry).toContain('`; + 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(/]*>([\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 = ``; + 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"); +} diff --git a/packages/cli/src/commands/cloud/render.fonts.test.ts b/packages/cli/src/commands/cloud/render.fonts.test.ts new file mode 100644 index 0000000000..1411af2215 --- /dev/null +++ b/packages/cli/src/commands/cloud/render.fonts.test.ts @@ -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()), + 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()), + errorBox: vi.fn(), +})); + +let project: string; +const fetchSpy = vi.fn(); + +beforeEach(() => { + project = mkdtempSync(join(tmpdir(), "hf-cloud-fonts-")); + writeFileSync( + join(project, "index.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", + ), + ); + }); +}); diff --git a/packages/cli/src/commands/cloud/render.ts b/packages/cli/src/commands/cloud/render.ts index f12faf3d3a..e3932bad11 100644 --- a/packages/cli/src/commands/cloud/render.ts +++ b/packages/cli/src/commands/cloud/render.ts @@ -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. diff --git a/packages/core/src/fonts/deterministicFonts-googleSubsets.test.ts b/packages/core/src/fonts/deterministicFonts-googleSubsets.test.ts index 9627c6705d..cca1f35c1a 100644 --- a/packages/core/src/fonts/deterministicFonts-googleSubsets.test.ts +++ b/packages/core/src/fonts/deterministicFonts-googleSubsets.test.ts @@ -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 }, ); }); }); diff --git a/packages/core/src/fonts/deterministicFonts-systemTtc.test.ts b/packages/core/src/fonts/deterministicFonts-systemTtc.test.ts new file mode 100644 index 0000000000..1fdf0a6492 --- /dev/null +++ b/packages/core/src/fonts/deterministicFonts-systemTtc.test.ts @@ -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 = `

x

`; + 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 = `

x

`; + 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,"); + }); +}); diff --git a/packages/core/src/fonts/deterministicFonts.ts b/packages/core/src/fonts/deterministicFonts.ts index 8b4ca06b87..c3dd506f2c 100644 --- a/packages/core/src/fonts/deterministicFonts.ts +++ b/packages/core/src/fonts/deterministicFonts.ts @@ -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 { - 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, diff --git a/packages/core/src/fonts/sfntFixture.ts b/packages/core/src/fonts/sfntFixture.ts new file mode 100644 index 0000000000..ef4a8cc1f1 --- /dev/null +++ b/packages/core/src/fonts/sfntFixture.ts @@ -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 }; +} diff --git a/packages/core/src/fonts/sfntNames.test.ts b/packages/core/src/fonts/sfntNames.test.ts new file mode 100644 index 0000000000..227eff95a7 --- /dev/null +++ b/packages/core/src/fonts/sfntNames.test.ts @@ -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); + }); +}); diff --git a/packages/core/src/fonts/sfntNames.ts b/packages/core/src/fonts/sfntNames.ts new file mode 100644 index 0000000000..b9fac257f1 --- /dev/null +++ b/packages/core/src/fonts/sfntNames.ts @@ -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 { + 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(); + 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(); + 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" }; +} diff --git a/packages/core/src/fonts/systemFontLocator.innerName.test.ts b/packages/core/src/fonts/systemFontLocator.innerName.test.ts new file mode 100644 index 0000000000..e5eb5b8a26 --- /dev/null +++ b/packages/core/src/fonts/systemFontLocator.innerName.test.ts @@ -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([]); + }); +}); diff --git a/packages/core/src/fonts/systemFontLocator.ts b/packages/core/src/fonts/systemFontLocator.ts index 215151b7c5..7bae042615 100644 --- a/packages/core/src/fonts/systemFontLocator.ts +++ b/packages/core/src/fonts/systemFontLocator.ts @@ -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 = { @@ -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 | 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 { + const index = new Map(); + 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( + (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(); for (const v of variants) { @@ -324,5 +382,6 @@ function dedupeVariants(variants: LocatedFontVariant[]): LocatedFontVariant[] { export function clearSystemFontCache(): void { cache.clear(); + innerNameIndex = null; allowedDirsCache = null; }