Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

5.5 KB
Raw Blame
import type { PdfFontRequest } from "../hooks/use-register-fonts";
import type { FormeFont } from "@formepdf/react";
import { prepareFontBytes } from "./font-bytes";

// One download per font file for the life of the page or server process.
const cache = new Map<string, Promise<Uint8Array>>();
// A download that stops sending fails the render instead of holding the preview on its loader. One that is slow but
// still sending finishes: CJK faces are 10–15 MB, and a resume can need five at once (#3627).
const STALL_TIMEOUT_MS = 30_000;

// Google serves the same files from fonts.gstatic.cn, which loads where fonts.gstatic.com is blocked or slow
// (mainland China). Each download starts on the host that last answered; when that host fails, or sends nothing
// within FALLBACK_AFTER_MS, the other host starts too and the first to answer wins.
const GSTATIC_HOSTS = ["fonts.gstatic.com", "fonts.gstatic.cn"];
const FALLBACK_AFTER_MS = 3000;
let preferredHost = GSTATIC_HOSTS[0] ?? "";

function fetchFont(src: string, signal: AbortSignal, fetcher: typeof fetch): Promise<Response> {
	const url = new URL(src);
	const check = (response: Response) => {
		if (!response.ok) throw new Error(`Font ${src}: HTTP ${response.status}`);
		return response;
	};
	if (!GSTATIC_HOSTS.includes(url.hostname)) return fetcher(src, { signal }).then(check);

	const hosts = [preferredHost, ...GSTATIC_HOSTS.filter((host) => host !== preferredHost)];
	const attempts = hosts.map(() => new AbortController());
	signal.addEventListener("abort", () => attempts.forEach((attempt) => attempt.abort(signal.reason)), { once: true });

	return new Promise((resolve, reject) => {
		let started = 0;
		let failed = 0;
		let settled = false;
		const start = (index: number) => {
			const host = hosts[index];
			const attempt = attempts[index];
			if (settled || index < started || !host || !attempt) return;
			started = index + 1;
			const fallback = setTimeout(() => start(index + 1), FALLBACK_AFTER_MS);
			const target = new URL(url);
			target.hostname = host;
			fetcher(target, { signal: attempt.signal })
				.then(check)
				.then((response) => {
					clearTimeout(fallback);
					if (settled) return;
					settled = true;
					preferredHost = host;
					attempts.forEach((other) => other !== attempt && other.abort());
					resolve(response);
				})
				.catch((error: unknown) => {
					clearTimeout(fallback);
					failed += 1;
					if (settled) return;
					if (failed === hosts.length) {
						settled = true;
						reject(error);
					} else start(index + 1);
				});
		};
		start(0);
	});
}

/** Downloads a font file's bytes from its source, trying the gstatic mirror when Google's host fails. */
export async function downloadFont(src: string, fetcher: typeof fetch = fetch): Promise<Uint8Array> {
	const controller = new AbortController();
	let stall: ReturnType<typeof setTimeout> | undefined;
	const progress = () => {
		clearTimeout(stall);
		stall = setTimeout(() => controller.abort(new Error(`Font ${src}: download stalled`)), STALL_TIMEOUT_MS);
	};
	progress();
	try {
		const response = await fetchFont(src, controller.signal, fetcher);
		progress();
		const reader = response.body?.getReader();
		if (!reader) throw new Error(`Font ${src}: download has no streaming body`);
		const chunks: Uint8Array[] = [];
		let size = 0;
		try {
			for (;;) {
				const { value, done } = await reader.read();
				if (done) break;
				progress();
				chunks.push(value);
				size += value.byteLength;
			}
		} finally {
			await reader.cancel();
		}
		const bytes = new Uint8Array(size);
		let offset = 0;
		for (const chunk of chunks) {
			bytes.set(chunk, offset);
			offset += chunk.byteLength;
		}
		return bytes;
	} finally {
		clearTimeout(stall);
	}
}

let readFont = downloadFont;

/** Replaces how font files are read, e.g. with a persistent cache around `downloadFont` (the mobile app). */
export function configureFontReader(reader: (src: string) => Promise<Uint8Array>) {
	readFont = reader;
	cache.clear();
}

const load = (src: string) => {
	let bytes = cache.get(src);
	if (!bytes) {
		bytes = readFont(src).then(prepareFontBytes);
		// A failed download may succeed next time.
		bytes.catch(() => cache.delete(src));
		cache.set(src, bytes);
	}
	return bytes;
};

/**
 * The font faces a document needs, as bytes Forme can embed. A face that fails to load is left out and listed in
 * `missing`; Forme falls back to its built-in fonts for that text.
 * An explicit mirror base rewrites only Computer Modern downloads, leaving catalog sources unchanged.
 */
export async function loadFonts(
	requests: readonly PdfFontRequest[],
	webFontBaseUrl?: string,
): Promise<{ fonts: FormeFont[]; warnings: string[]; missing: string[] }> {
	const mirrorBase = webFontBaseUrl?.trim().replace(/\/+$/, "");
	const computerModernBase = "https://cdn.jsdelivr.net/gh/bitmaks/cm-web-fonts@latest";
	const warnings: string[] = [];
	const missing: string[] = [];
	const loaded = await Promise.all(
		requests.map(async (request): Promise<FormeFont | null> => {
			try {
				const source =
					mirrorBase && request.src.startsWith(computerModernBase)
						? mirrorBase + request.src.slice(computerModernBase.length)
						: request.src;
				return { family: request.family, weight: request.weight, italic: request.italic, src: await load(source) };
			} catch (error) {
				warnings.push(error instanceof Error ? error.message : `Font ${request.src} could not be loaded.`);
				missing.push(request.family);
				return null;
			}
		}),
	);
	return { fonts: loaded.filter((font): font is FormeFont => font !== null), warnings, missing };
}