@formepdf/fonts-standard 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ /** A font registration compatible with `@formepdf/*`'s `Font.register`
2
+ * (structural — this package intentionally has no Forme dependencies). */
3
+ export interface FontSpec {
4
+ family: string;
5
+ src: Uint8Array;
6
+ fontWeight: number;
7
+ fontStyle: 'normal' | 'italic';
8
+ }
9
+ /**
10
+ * The 12 Liberation fonts as registrations, ready to pass to a Forme adapter's
11
+ * `Font.register` (or a document's `fonts` array). Buffers are decoded fresh
12
+ * per call so callers may transfer/detach them freely.
13
+ */
14
+ export declare function standardFonts(): FontSpec[];
15
+ /**
16
+ * Base-14 family → Liberation family. The engine consults the same mapping in
17
+ * pdfUa mode to route a standard font through the embedded Liberation program;
18
+ * exported here for the CLI, docs, and JS-side pdfUa configuration. Style
19
+ * (bold/italic) is carried by weight/style, so this maps family to family.
20
+ * Symbol and ZapfDingbats have no metric-compatible substitute and are omitted
21
+ * — a document using them in pdfUa mode is warned, not silently substituted.
22
+ */
23
+ export declare const BASE14_ALIASES: Readonly<Record<string, string>>;
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @formepdf/fonts-standard
3
+ *
4
+ * Metric-compatible, embeddable replacements for the 14 standard PDF fonts,
5
+ * for producing accessible (PDF/UA) and archival (PDF/A) documents — those
6
+ * profiles require every font embedded, which the non-embedded base-14 fonts
7
+ * are not.
8
+ *
9
+ * The Liberation family (Sans/Serif/Mono, SIL OFL 1.1) is metric-compatible
10
+ * with Helvetica/Times/Courier by design, so substituting it changes nothing
11
+ * about layout — Forme lays out on the standard AFM metrics and swaps only
12
+ * the embedded glyph program at write time.
13
+ *
14
+ * The library carries the font bytes inline (base64) and returns them as
15
+ * `Uint8Array` buffers — no file IO — so it works in Node, WASM, and the
16
+ * browser. The fonts are redistributed unmodified under the OFL; see OFL.txt.
17
+ */
18
+ import { FONT_DATA_BASE64 } from './generated/font-data.js';
19
+ const ENTRIES = [
20
+ { file: 'LiberationSans-Regular', family: 'Liberation Sans', fontWeight: 400, fontStyle: 'normal' },
21
+ { file: 'LiberationSans-Bold', family: 'Liberation Sans', fontWeight: 700, fontStyle: 'normal' },
22
+ { file: 'LiberationSans-Italic', family: 'Liberation Sans', fontWeight: 400, fontStyle: 'italic' },
23
+ { file: 'LiberationSans-BoldItalic', family: 'Liberation Sans', fontWeight: 700, fontStyle: 'italic' },
24
+ { file: 'LiberationSerif-Regular', family: 'Liberation Serif', fontWeight: 400, fontStyle: 'normal' },
25
+ { file: 'LiberationSerif-Bold', family: 'Liberation Serif', fontWeight: 700, fontStyle: 'normal' },
26
+ { file: 'LiberationSerif-Italic', family: 'Liberation Serif', fontWeight: 400, fontStyle: 'italic' },
27
+ { file: 'LiberationSerif-BoldItalic', family: 'Liberation Serif', fontWeight: 700, fontStyle: 'italic' },
28
+ { file: 'LiberationMono-Regular', family: 'Liberation Mono', fontWeight: 400, fontStyle: 'normal' },
29
+ { file: 'LiberationMono-Bold', family: 'Liberation Mono', fontWeight: 700, fontStyle: 'normal' },
30
+ { file: 'LiberationMono-Italic', family: 'Liberation Mono', fontWeight: 400, fontStyle: 'italic' },
31
+ { file: 'LiberationMono-BoldItalic', family: 'Liberation Mono', fontWeight: 700, fontStyle: 'italic' },
32
+ ];
33
+ function decodeBase64(b64) {
34
+ // Node Buffer where available (fast); atob fallback for WASM/browser.
35
+ if (typeof Buffer !== 'undefined') {
36
+ return new Uint8Array(Buffer.from(b64, 'base64'));
37
+ }
38
+ const binary = atob(b64);
39
+ const bytes = new Uint8Array(binary.length);
40
+ for (let i = 0; i < binary.length; i++)
41
+ bytes[i] = binary.charCodeAt(i);
42
+ return bytes;
43
+ }
44
+ /**
45
+ * The 12 Liberation fonts as registrations, ready to pass to a Forme adapter's
46
+ * `Font.register` (or a document's `fonts` array). Buffers are decoded fresh
47
+ * per call so callers may transfer/detach them freely.
48
+ */
49
+ export function standardFonts() {
50
+ return ENTRIES.map((e) => ({
51
+ family: e.family,
52
+ src: decodeBase64(FONT_DATA_BASE64[e.file]),
53
+ fontWeight: e.fontWeight,
54
+ fontStyle: e.fontStyle,
55
+ }));
56
+ }
57
+ /**
58
+ * Base-14 family → Liberation family. The engine consults the same mapping in
59
+ * pdfUa mode to route a standard font through the embedded Liberation program;
60
+ * exported here for the CLI, docs, and JS-side pdfUa configuration. Style
61
+ * (bold/italic) is carried by weight/style, so this maps family to family.
62
+ * Symbol and ZapfDingbats have no metric-compatible substitute and are omitted
63
+ * — a document using them in pdfUa mode is warned, not silently substituted.
64
+ */
65
+ export const BASE14_ALIASES = Object.freeze({
66
+ Helvetica: 'Liberation Sans',
67
+ Arial: 'Liberation Sans',
68
+ 'Times-Roman': 'Liberation Serif',
69
+ Times: 'Liberation Serif',
70
+ 'Times New Roman': 'Liberation Serif',
71
+ Courier: 'Liberation Mono',
72
+ 'Courier New': 'Liberation Mono',
73
+ });
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@formepdf/fonts-standard",
3
+ "version": "0.15.0",
4
+ "description": "Embeddable, metric-compatible replacements for the standard 14 PDF fonts (Liberation Sans/Serif/Mono, OFL) — for PDF/UA and PDF/A output",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "build": "node scripts/generate-font-data.mjs && tsc",
16
+ "test": "node scripts/generate-font-data.mjs && vitest run",
17
+ "test:watch": "vitest"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^22.0.0",
21
+ "typescript": "^5.7.0",
22
+ "vitest": "^3.0.0"
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "OFL.txt",
27
+ "AUTHORS"
28
+ ],
29
+ "keywords": [
30
+ "pdf",
31
+ "font",
32
+ "liberation",
33
+ "pdf-ua",
34
+ "pdf-a",
35
+ "accessibility",
36
+ "embed",
37
+ "forme"
38
+ ],
39
+ "license": "OFL-1.1",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/formepdf/forme",
43
+ "directory": "packages/fonts-standard"
44
+ }
45
+ }