Add bundled TTF fonts, page geometry, column allocator and render model

Static TTF fonts (IBM Plex Sans/Mono, Noto Devanagari/Kannada) with a
fontkit-generated manifest, a verifier and OFL licences. Every family
registers a normal and italic source so fallback stacks cannot throw.
Adds A4/Letter geometry with margin presets, an exact column allocator,
deterministic Indian-grouping money and date formatting, text sanitising
and a pure buildRenderModel.
This commit is contained in:
2026-10-04 04:38:03 +05:30
parent 12d80efcb8
commit bf4b85563b
40 changed files with 2162 additions and 49 deletions
+85
View File
@@ -0,0 +1,85 @@
import { Font } from "@react-pdf/renderer";
import { FONT_MANIFEST } from "./manifest";
/** Every stack ends here: Latin and the rupee sign from Plex, then the Indic scripts. */
export const FONT_STACK: readonly string[] = [
"IBM Plex Sans",
"Noto Sans Devanagari",
"Noto Sans Kannada",
];
/** `[primary, ...tail]` without duplicates, for a style's fontFamily. */
export function stack(primary: string): string[] {
return [primary, ...FONT_STACK.filter((f) => f !== primary)];
}
let registered = false;
/**
* Registers every bundled family from the manifest. Idempotent; never call Font.reset(), the fonts
* would not load again.
*
* `base` is a URL prefix in the app ("/fonts") and a directory under Node (tests). Only static TTF is
* used: WOFF2 embeds glyphs without outlines (invisible text) and variable fonts embed only their
* default instance.
*/
export function registerBundledFonts(base: string): void {
if (registered) return;
registered = true;
for (const fam of FONT_MANIFEST.families) {
const sources: { src: string; fontWeight: number; fontStyle: "normal" | "italic" }[] = [];
const weights = new Set<number>();
for (const font of fam.fonts) {
weights.add(font.weight);
sources.push({
src: `${base}/${fam.folder}/${font.file}`,
fontWeight: font.weight,
fontStyle: font.style,
});
}
// react-pdf resolves every family in a fallback stack with the node's own style and throws for
// a family that has no italic, so any weight without a real italic reuses its upright file.
for (const weight of weights) {
if (sources.some((s) => s.fontWeight === weight && s.fontStyle === "italic")) continue;
const upright = sources.find((s) => s.fontWeight === weight && s.fontStyle === "normal");
if (upright) sources.push({ ...upright, fontStyle: "italic" });
}
Font.register({ family: fam.family, fonts: sources });
}
// The default hyphenation inserts "-" into e-mail addresses, URLs and identifiers.
Font.registerHyphenationCallback((word) => [word]);
}
const MIN_FONT_BYTES = 10 * 1024;
function hasTrueTypeMagic(b: Uint8Array): boolean {
if (b.length < 4) return false;
if (b[0] === 0 && b[1] === 1 && b[2] === 0 && b[3] === 0) return true;
return String.fromCharCode(b[0], b[1], b[2], b[3]) === "true";
}
/**
* Fetches every bundled font and returns a list of problems (empty when all are fine). Tauri answers
* an unknown path with index.html and HTTP 200, so the response bytes must be checked, not the status.
*/
export async function preflightFonts(
fetchBytes: (url: string) => Promise<Uint8Array>,
base: string,
): Promise<string[]> {
const problems: string[] = [];
for (const fam of FONT_MANIFEST.families) {
for (const font of fam.fonts) {
const url = `${base}/${fam.folder}/${font.file}`;
try {
const bytes = await fetchBytes(url);
if (!hasTrueTypeMagic(bytes)) problems.push(`${url}: not a TrueType font`);
else if (bytes.length <= MIN_FONT_BYTES) problems.push(`${url}: only ${bytes.length} bytes`);
} catch (err) {
problems.push(`${url}: ${err instanceof Error ? err.message : String(err)}`);
}
}
}
return problems;
}