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:
@@ -0,0 +1,31 @@
|
||||
import { createElement as h } from "react";
|
||||
import path from "node:path";
|
||||
import { renderToBuffer, Document, Page, Text } from "@react-pdf/renderer";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { registerBundledFonts, stack } from "./register";
|
||||
|
||||
registerBundledFonts(path.resolve(process.cwd(), "public/fonts"));
|
||||
|
||||
const render = (page: object, style: object) =>
|
||||
renderToBuffer(
|
||||
h(Document, null, h(Page, page as never, h(Text, { style: style as never }, "Total ₹1,23,456.78 नमस्ते ಕನ್ನಡ"))),
|
||||
);
|
||||
|
||||
describe("bundled fonts render", () => {
|
||||
it("renders every family-stack in italic and bold-italic without throwing", async () => {
|
||||
for (const fontWeight of [300, 400, 500, 600, 700]) {
|
||||
const buf = await render({ size: "A4" }, { fontFamily: stack("IBM Plex Sans"), fontStyle: "italic", fontWeight });
|
||||
expect(buf.subarray(0, 5).toString()).toBe("%PDF-");
|
||||
}
|
||||
const mono = await render({ size: "A4" }, { fontFamily: stack("IBM Plex Mono"), fontStyle: "italic", fontWeight: 600 });
|
||||
expect(mono.length).toBeGreaterThan(1000);
|
||||
});
|
||||
|
||||
it("accepts mm strings in the Page size object", async () => {
|
||||
const buf = await render({ size: { width: "210mm", height: "297mm" } }, { fontFamily: stack("IBM Plex Sans") });
|
||||
const m = /\/MediaBox \[0 0 ([\d.]+) ([\d.]+)\]/.exec(buf.toString("latin1"));
|
||||
expect(m).not.toBeNull();
|
||||
expect(Number(m![1])).toBeCloseTo(595.28, 1);
|
||||
expect(Number(m![2])).toBeCloseTo(841.89, 1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user