Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { describe, expect, it } from "bun:test";
import { existsSync } from "node:fs";
import { injectDeterministicFontFaces } from "./deterministicFonts.js";
import { fontFormatHint, injectDeterministicFontFaces } from "./deterministicFonts.js";

// A font family that is NOT in FONT_ALIASES but exists on macOS as
// /System/Library/Fonts/Supplemental/Impact.ttf. When Google Fonts
Expand All @@ -36,6 +36,11 @@ function makeHttp400Fetch(): typeof fetch {
}

describe("system font capture — allowSystemFontCapture option", () => {
it("uses the CSS collection hint for raw TTC data URIs", () => {
expect(fontFormatHint("data:font/collection;base64,AA==")).toBe("collection");
expect(fontFormatHint("data:font/woff2;base64,AA==")).toBe("woff2");
});

it("does not attempt system font capture when allowSystemFontCapture is false", async () => {
const html = makeHtml("NotARealFontFamilyForTest");
const result = await injectDeterministicFontFaces(html, {
Expand Down
6 changes: 5 additions & 1 deletion packages/producer/src/services/deterministicFonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ function extractRequestedFontFamilies(html: string): Map<string, string> {
return requested;
}

export function fontFormatHint(src: string): "collection" | "woff2" {
return src.startsWith("data:font/collection;") ? "collection" : "woff2";
}

function buildFontFaceRule(
familyName: string,
src: string,
Expand All @@ -442,7 +446,7 @@ function buildFontFaceRule(
return [
"@font-face {",
` font-family: "${familyName}";`,
` src: url("${src}") format("woff2");`,
` src: url("${src}") format("${fontFormatHint(src)}");`,
` font-style: ${style};`,
` font-weight: ${weight};`,
" font-display: block;",
Expand Down
16 changes: 12 additions & 4 deletions packages/producer/src/services/fontCompression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,17 @@ describe("fontToDataUri", () => {
expect(uri).toMatch(/^data:font\/otf;base64,/);
});

it("falls back with correct MIME type for ttc", async () => {
const garbage = Buffer.from("not a font");
const uri = await fontToDataUri(garbage, "ttc");
expect(uri).toMatch(/^data:font\/collection;base64,/);
it("embeds ttc collections raw without attempting slow woff2 compression", async () => {
const warnings: unknown[][] = [];
const originalWarn = console.warn;
console.warn = (...args: unknown[]) => warnings.push(args);
try {
const raw = Buffer.from("ttc-bytes");
const uri = await fontToDataUri(raw, "ttc");
expect(uri).toBe(`data:font/collection;base64,${raw.toString("base64")}`);
expect(warnings).toHaveLength(0);
} finally {
console.warn = originalWarn;
}
});
});
3 changes: 3 additions & 0 deletions packages/producer/src/services/fontCompression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export async function fontToDataUri(input: Buffer, originalFormat: string): Prom
if (originalFormat === "woff2") {
return `data:font/woff2;base64,${input.toString("base64")}`;
}
if (originalFormat === "ttc") {
return `data:font/collection;base64,${input.toString("base64")}`;
}
try {
const compressed = await compressToWoff2(input);
return `data:font/woff2;base64,${compressed.toString("base64")}`;
Expand Down
Loading