@formepdf/react 0.2.0 → 0.3.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.
package/dist/font.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export interface FontRegistration {
2
+ family: string;
3
+ src: string | Uint8Array;
4
+ fontWeight?: number | 'normal' | 'bold';
5
+ fontStyle?: 'normal' | 'italic' | 'oblique';
6
+ }
7
+ export declare const Font: {
8
+ register(options: FontRegistration): void;
9
+ clear(): void;
10
+ getRegistered(): FontRegistration[];
11
+ };
package/dist/font.js ADDED
@@ -0,0 +1,23 @@
1
+ const globalFonts = [];
2
+ function normalizeWeight(w) {
3
+ if (w === undefined || w === 'normal')
4
+ return 400;
5
+ if (w === 'bold')
6
+ return 700;
7
+ return typeof w === 'number' ? w : (parseInt(w, 10) || 400);
8
+ }
9
+ export const Font = {
10
+ register(options) {
11
+ globalFonts.push({
12
+ ...options,
13
+ fontWeight: normalizeWeight(options.fontWeight),
14
+ fontStyle: options.fontStyle || 'normal',
15
+ });
16
+ },
17
+ clear() {
18
+ globalFonts.length = 0;
19
+ },
20
+ getRegistered() {
21
+ return [...globalFonts];
22
+ },
23
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, PageBreak } from './components.js';
2
2
  export { serialize, mapStyle, mapDimension, parseColor, expandEdges, expandCorners } from './serialize.js';
3
3
  export { StyleSheet } from './stylesheet.js';
4
+ export { Font } from './font.js';
5
+ export type { FontRegistration } from './font.js';
4
6
  export { render, renderToObject } from './render.js';
5
- export type { Style, Edges, Corners, EdgeColors, DocumentProps, PageProps, ViewProps, TextProps, ImageProps, ColumnDef, TableProps, RowProps, CellProps, FixedProps, SvgProps, TextRun, FormeDocument, FormeNode, FormeNodeKind, FormeStyle, FormePageConfig, FormePageSize, FormeEdges, FormeMetadata, FormeColumnDef, FormeColumnWidth, FormeDimension, FormeColor, FormeEdgeValues, FormeCornerValues, } from './types.js';
7
+ export type { Style, Edges, Corners, EdgeColors, DocumentProps, PageProps, ViewProps, TextProps, ImageProps, ColumnDef, TableProps, RowProps, CellProps, FixedProps, SvgProps, TextRun, FormeDocument, FormeFont, FormeNode, FormeNodeKind, FormeStyle, FormePageConfig, FormePageSize, FormeEdges, FormeMetadata, FormeColumnDef, FormeColumnWidth, FormeDimension, FormeColor, FormeEdgeValues, FormeCornerValues, } from './types.js';
package/dist/index.js CHANGED
@@ -4,5 +4,7 @@ export { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, PageBr
4
4
  export { serialize, mapStyle, mapDimension, parseColor, expandEdges, expandCorners } from './serialize.js';
5
5
  // StyleSheet
6
6
  export { StyleSheet } from './stylesheet.js';
7
+ // Font registration
8
+ export { Font } from './font.js';
7
9
  // Render functions
8
10
  export { render, renderToObject } from './render.js';
package/dist/serialize.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { isValidElement, Children, Fragment } from 'react';
2
2
  import { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, PageBreak } from './components.js';
3
+ import { Font } from './font.js';
3
4
  const VALID_PARENTS = {
4
5
  Page: {
5
6
  allowed: ['Document'],
@@ -87,7 +88,9 @@ export function serialize(element) {
87
88
  metadata.subject = props.subject;
88
89
  if (props.creator !== undefined)
89
90
  metadata.creator = props.creator;
90
- return {
91
+ // Merge global + document fonts (document fonts override on conflict)
92
+ const mergedFonts = mergeFonts(Font.getRegistered(), props.fonts);
93
+ const result = {
91
94
  children,
92
95
  metadata,
93
96
  defaultPage: {
@@ -96,6 +99,10 @@ export function serialize(element) {
96
99
  wrap: true,
97
100
  },
98
101
  };
102
+ if (mergedFonts.length > 0) {
103
+ result.fonts = mergedFonts;
104
+ }
105
+ return result;
99
106
  }
100
107
  // ─── Page serialization ──────────────────────────────────────────────
101
108
  function serializePage(element) {
@@ -723,3 +730,32 @@ function mapColumnWidth(w) {
723
730
  return { Fixed: w.fixed };
724
731
  return 'Auto';
725
732
  }
733
+ // ─── Font merging ─────────────────────────────────────────────────
734
+ function normalizeFontWeight(w) {
735
+ if (w === undefined || w === 'normal')
736
+ return 400;
737
+ if (w === 'bold')
738
+ return 700;
739
+ return typeof w === 'number' ? w : (parseInt(w, 10) || 400);
740
+ }
741
+ function fontKey(family, weight, italic) {
742
+ return `${family}:${weight}:${italic}`;
743
+ }
744
+ function mergeFonts(globalFonts, docFonts) {
745
+ const map = new Map();
746
+ for (const f of globalFonts) {
747
+ const weight = normalizeFontWeight(f.fontWeight);
748
+ const italic = f.fontStyle === 'italic' || f.fontStyle === 'oblique';
749
+ const key = fontKey(f.family, weight, italic);
750
+ map.set(key, { family: f.family, src: f.src, weight, italic });
751
+ }
752
+ if (docFonts) {
753
+ for (const f of docFonts) {
754
+ const weight = normalizeFontWeight(f.fontWeight);
755
+ const italic = f.fontStyle === 'italic' || f.fontStyle === 'oblique';
756
+ const key = fontKey(f.family, weight, italic);
757
+ map.set(key, { family: f.family, src: f.src, weight, italic });
758
+ }
759
+ }
760
+ return Array.from(map.values());
761
+ }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { ReactNode } from 'react';
2
+ import type { FontRegistration } from './font.js';
2
3
  /** Edge values for padding, margin, borderWidth */
3
4
  export interface Edges {
4
5
  top: number;
@@ -97,6 +98,7 @@ export interface DocumentProps {
97
98
  author?: string;
98
99
  subject?: string;
99
100
  creator?: string;
101
+ fonts?: FontRegistration[];
100
102
  children?: ReactNode;
101
103
  }
102
104
  export interface PageProps {
@@ -168,10 +170,17 @@ export interface TextRun {
168
170
  style?: FormeStyle;
169
171
  href?: string;
170
172
  }
173
+ export interface FormeFont {
174
+ family: string;
175
+ src: string | Uint8Array;
176
+ weight: number;
177
+ italic: boolean;
178
+ }
171
179
  export interface FormeDocument {
172
180
  children: FormeNode[];
173
181
  metadata: FormeMetadata;
174
182
  defaultPage: FormePageConfig;
183
+ fonts?: FormeFont[];
175
184
  }
176
185
  export interface FormeMetadata {
177
186
  title?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formepdf/react",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "JSX-to-JSON serializer for Forme PDF engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",