@lpdfio/lpdf 0.0.3

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/kit.d.ts ADDED
@@ -0,0 +1,109 @@
1
+ /**
2
+ * LpdfKit — document skeleton assembly.
3
+ *
4
+ * Assembles sections (containing layout and canvas blocks) into a document
5
+ * tree ready for `LpdfEngine.renderPdf()` or `kitToXml()`.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { LpdfEngine, LpdfKit, LpdfLayout, LpdfCanvas } from 'lpdf';
10
+ *
11
+ * const doc = LpdfKit.document({
12
+ * sections: [
13
+ * LpdfKit.section({
14
+ * nodes: [
15
+ * LpdfKit.canvas([ LpdfCanvas.layer([ LpdfCanvas.rect(0, 0, 595, 842) ]) ]),
16
+ * LpdfKit.layout([ LpdfLayout.text(['Hello']) ]),
17
+ * ],
18
+ * options: { size: 'a4', margin: '28pt' },
19
+ * }),
20
+ * ],
21
+ * options: { meta: { title: 'My Doc' } },
22
+ * });
23
+ * const bytes = await new LpdfEngine(key).renderPdf(doc);
24
+ * ```
25
+ */
26
+ import type { LpdfNode } from './layout';
27
+ import type { LpdfCanvasLayerNode } from './canvas';
28
+ export interface LpdfTokens {
29
+ colors?: Record<string, string>;
30
+ space?: Record<string, string>;
31
+ grid?: Record<string, string>;
32
+ border?: Record<string, string>;
33
+ radius?: Record<string, string>;
34
+ width?: Record<string, string>;
35
+ text?: Record<string, string>;
36
+ fonts?: Record<string, LpdfFontDef>;
37
+ }
38
+ export type LpdfFontDef = {
39
+ src: string;
40
+ builtin?: never;
41
+ } | {
42
+ builtin: string;
43
+ src?: never;
44
+ };
45
+ export interface LpdfMeta {
46
+ title?: string;
47
+ author?: string;
48
+ subject?: string;
49
+ keywords?: string;
50
+ creator?: string;
51
+ }
52
+ export interface SectionOptions {
53
+ size?: string;
54
+ orientation?: string;
55
+ margin?: string;
56
+ background?: string;
57
+ title?: string;
58
+ debug?: string;
59
+ }
60
+ export interface DocumentOptions {
61
+ size?: string;
62
+ orientation?: string;
63
+ margin?: string;
64
+ background?: string;
65
+ tokens?: LpdfTokens;
66
+ meta?: LpdfMeta;
67
+ debug?: string;
68
+ }
69
+ /** A layout block — wraps an ordered list of layout nodes. */
70
+ export interface LpdfLayoutBlock {
71
+ type: 'layout';
72
+ nodes: LpdfNode[];
73
+ }
74
+ /** A canvas block — wraps an ordered list of canvas layers. */
75
+ export interface LpdfCanvasBlock {
76
+ type: 'canvas';
77
+ nodes: LpdfCanvasLayerNode[];
78
+ }
79
+ export interface LpdfSectionNode {
80
+ type: 'section';
81
+ attrs: Record<string, string>;
82
+ nodes: (LpdfLayoutBlock | LpdfCanvasBlock)[];
83
+ }
84
+ export interface LpdfDocument {
85
+ version: 1;
86
+ type: 'document';
87
+ attrs: Record<string, unknown>;
88
+ nodes: LpdfSectionNode[];
89
+ }
90
+ export interface SectionInput {
91
+ nodes?: (LpdfLayoutBlock | LpdfCanvasBlock)[];
92
+ options?: SectionOptions;
93
+ }
94
+ export interface DocumentInput {
95
+ /** Sections — serialised as `nodes` on the wire (matching kit_to_xml and parse_tree). */
96
+ sections?: LpdfSectionNode[];
97
+ options?: DocumentOptions;
98
+ }
99
+ declare function layout(nodes?: LpdfNode[]): LpdfLayoutBlock;
100
+ declare function canvas(layers?: LpdfCanvasLayerNode[]): LpdfCanvasBlock;
101
+ declare function section(input?: SectionInput): LpdfSectionNode;
102
+ declare function document(input?: DocumentInput): LpdfDocument;
103
+ export declare const LpdfKit: Readonly<{
104
+ layout: typeof layout;
105
+ canvas: typeof canvas;
106
+ section: typeof section;
107
+ document: typeof document;
108
+ }>;
109
+ export {};
package/dist/kit.js ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ /**
3
+ * LpdfKit — document skeleton assembly.
4
+ *
5
+ * Assembles sections (containing layout and canvas blocks) into a document
6
+ * tree ready for `LpdfEngine.renderPdf()` or `kitToXml()`.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { LpdfEngine, LpdfKit, LpdfLayout, LpdfCanvas } from 'lpdf';
11
+ *
12
+ * const doc = LpdfKit.document({
13
+ * sections: [
14
+ * LpdfKit.section({
15
+ * nodes: [
16
+ * LpdfKit.canvas([ LpdfCanvas.layer([ LpdfCanvas.rect(0, 0, 595, 842) ]) ]),
17
+ * LpdfKit.layout([ LpdfLayout.text(['Hello']) ]),
18
+ * ],
19
+ * options: { size: 'a4', margin: '28pt' },
20
+ * }),
21
+ * ],
22
+ * options: { meta: { title: 'My Doc' } },
23
+ * });
24
+ * const bytes = await new LpdfEngine(key).renderPdf(doc);
25
+ * ```
26
+ */
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.LpdfKit = void 0;
29
+ // ── camelCase → kebab-case helper ─────────────────────────────────────────────
30
+ function attrKey(camel) {
31
+ return camel.replace(/[A-Z]/g, c => '-' + c.toLowerCase());
32
+ }
33
+ function buildAttrs(options) {
34
+ const result = {};
35
+ for (const [key, val] of Object.entries(options)) {
36
+ if (val !== undefined) {
37
+ result[attrKey(key)] = val;
38
+ }
39
+ }
40
+ return result;
41
+ }
42
+ // ── Factory functions ─────────────────────────────────────────────────────────
43
+ function layout(nodes = []) {
44
+ return { type: 'layout', nodes };
45
+ }
46
+ function canvas(layers = []) {
47
+ return { type: 'canvas', nodes: layers };
48
+ }
49
+ function section(input = {}) {
50
+ return {
51
+ type: 'section',
52
+ attrs: buildAttrs((input.options ?? {})),
53
+ nodes: input.nodes ?? [],
54
+ };
55
+ }
56
+ function document(input = {}) {
57
+ const { tokens, meta, ...restOpts } = input.options ?? {};
58
+ const attrs = {
59
+ ...buildAttrs(restOpts),
60
+ };
61
+ if (tokens !== undefined)
62
+ attrs['tokens'] = tokens;
63
+ if (meta !== undefined)
64
+ attrs['meta'] = meta;
65
+ return {
66
+ version: 1,
67
+ type: 'document',
68
+ attrs,
69
+ nodes: input.sections ?? [],
70
+ };
71
+ }
72
+ // ── LpdfKit export ────────────────────────────────────────────────────────────
73
+ exports.LpdfKit = Object.freeze({
74
+ layout,
75
+ canvas,
76
+ section,
77
+ document,
78
+ });
@@ -0,0 +1,260 @@
1
+ import type { PageScope } from './_shared';
2
+ export interface StackOptions {
3
+ gap?: string;
4
+ padding?: string;
5
+ background?: string;
6
+ align?: string;
7
+ justify?: string;
8
+ width?: string;
9
+ height?: string;
10
+ border?: string;
11
+ radius?: string;
12
+ debug?: string;
13
+ }
14
+ export interface FlankOptions {
15
+ gap?: string;
16
+ padding?: string;
17
+ background?: string;
18
+ align?: string;
19
+ justify?: string;
20
+ end?: string;
21
+ width?: string;
22
+ height?: string;
23
+ border?: string;
24
+ radius?: string;
25
+ debug?: string;
26
+ }
27
+ export interface SplitOptions {
28
+ gap?: string;
29
+ padding?: string;
30
+ background?: string;
31
+ align?: string;
32
+ equal?: string;
33
+ width?: string;
34
+ height?: string;
35
+ border?: string;
36
+ radius?: string;
37
+ debug?: string;
38
+ }
39
+ export interface ClusterOptions {
40
+ gap?: string;
41
+ padding?: string;
42
+ background?: string;
43
+ align?: string;
44
+ justify?: string;
45
+ width?: string;
46
+ height?: string;
47
+ border?: string;
48
+ radius?: string;
49
+ debug?: string;
50
+ }
51
+ export interface GridOptions {
52
+ cols?: string;
53
+ colWidth?: string;
54
+ gap?: string;
55
+ equal?: string;
56
+ padding?: string;
57
+ background?: string;
58
+ width?: string;
59
+ height?: string;
60
+ border?: string;
61
+ radius?: string;
62
+ debug?: string;
63
+ }
64
+ export interface FrameOptions {
65
+ width?: string;
66
+ height?: string;
67
+ padding?: string;
68
+ background?: string;
69
+ border?: string;
70
+ radius?: string;
71
+ align?: string;
72
+ debug?: string;
73
+ }
74
+ export interface LinkOptions {
75
+ url?: string;
76
+ width?: string;
77
+ height?: string;
78
+ debug?: string;
79
+ }
80
+ export interface TableOptions {
81
+ cols: string;
82
+ border?: string;
83
+ stripe?: string;
84
+ gap?: string;
85
+ padding?: string;
86
+ background?: string;
87
+ width?: string;
88
+ height?: string;
89
+ repeat?: string;
90
+ debug?: string;
91
+ }
92
+ export interface TheadOptions {
93
+ background?: string;
94
+ }
95
+ export interface TrOptions {
96
+ background?: string;
97
+ }
98
+ export interface TdOptions {
99
+ padding?: string;
100
+ background?: string;
101
+ align?: string;
102
+ valign?: string;
103
+ border?: string;
104
+ radius?: string;
105
+ gap?: string;
106
+ debug?: string;
107
+ }
108
+ export interface TextOptions {
109
+ font?: string;
110
+ fontSize?: string;
111
+ textAlign?: string;
112
+ color?: string;
113
+ bold?: string;
114
+ end?: string;
115
+ padding?: string;
116
+ background?: string;
117
+ width?: string;
118
+ height?: string;
119
+ border?: string;
120
+ radius?: string;
121
+ repeat?: string;
122
+ debug?: string;
123
+ }
124
+ export interface SpanOptions {
125
+ font?: string;
126
+ fontSize?: string;
127
+ color?: string;
128
+ bold?: string;
129
+ url?: string;
130
+ underline?: string;
131
+ strike?: string;
132
+ }
133
+ export interface DividerOptions {
134
+ color?: string;
135
+ thickness?: string;
136
+ direction?: string;
137
+ debug?: string;
138
+ }
139
+ export interface ImgOptions {
140
+ name: string;
141
+ height?: string;
142
+ width?: string;
143
+ font?: string;
144
+ fontSize?: string;
145
+ gap?: string;
146
+ padding?: string;
147
+ background?: string;
148
+ border?: string;
149
+ radius?: string;
150
+ repeat?: string;
151
+ debug?: string;
152
+ }
153
+ export interface BarcodeOptions {
154
+ type: string;
155
+ data: string;
156
+ size?: string;
157
+ width?: string;
158
+ height?: string;
159
+ ec?: string;
160
+ hrt?: string;
161
+ color?: string;
162
+ background?: string;
163
+ repeat?: string;
164
+ debug?: string;
165
+ }
166
+ export interface RegionOptions {
167
+ page?: PageScope | string;
168
+ w?: string;
169
+ debug?: string;
170
+ }
171
+ export interface LpdfSpanNode {
172
+ type: 'span';
173
+ attrs: Record<string, string>;
174
+ nodes: string[];
175
+ }
176
+ export interface LpdfContainerNode {
177
+ type: 'stack' | 'flank' | 'split' | 'cluster' | 'grid' | 'frame' | 'link';
178
+ attrs: Record<string, string>;
179
+ nodes: LpdfNode[];
180
+ }
181
+ export interface LpdfTextNode {
182
+ type: 'text';
183
+ attrs: Record<string, string>;
184
+ nodes: (string | LpdfSpanNode)[];
185
+ }
186
+ export interface LpdfDividerNode {
187
+ type: 'divider';
188
+ attrs: Record<string, string>;
189
+ }
190
+ export interface LpdfImgNode {
191
+ type: 'img';
192
+ attrs: Record<string, string>;
193
+ }
194
+ export interface LpdfBarcodeNode {
195
+ type: 'barcode';
196
+ attrs: Record<string, string>;
197
+ }
198
+ export interface LpdfTheadNode {
199
+ type: 'thead';
200
+ attrs: Record<string, string>;
201
+ nodes: LpdfTdNode[];
202
+ }
203
+ export interface LpdfTrNode {
204
+ type: 'tr';
205
+ attrs: Record<string, string>;
206
+ nodes: LpdfTdNode[];
207
+ }
208
+ export interface LpdfTdNode {
209
+ type: 'td';
210
+ attrs: Record<string, string>;
211
+ nodes: LpdfNode[];
212
+ }
213
+ export interface LpdfTableNode {
214
+ type: 'table';
215
+ attrs: Record<string, string>;
216
+ nodes: (LpdfTheadNode | LpdfTrNode)[];
217
+ }
218
+ export interface LpdfRegionNode {
219
+ type: 'layout-region';
220
+ attrs: Record<string, string>;
221
+ nodes: LpdfNode[];
222
+ }
223
+ export type LpdfNode = LpdfContainerNode | LpdfTextNode | LpdfDividerNode | LpdfTableNode | LpdfImgNode | LpdfBarcodeNode | LpdfRegionNode;
224
+ declare function stack(nodes?: LpdfNode[], options?: StackOptions): LpdfContainerNode;
225
+ declare function flank(nodes?: LpdfNode[], options?: FlankOptions): LpdfContainerNode;
226
+ declare function split(nodes?: LpdfNode[], options?: SplitOptions): LpdfContainerNode;
227
+ declare function cluster(nodes?: LpdfNode[], options?: ClusterOptions): LpdfContainerNode;
228
+ declare function grid(nodes?: LpdfNode[], options?: GridOptions): LpdfContainerNode;
229
+ declare function frame(nodes?: LpdfNode[], options?: FrameOptions): LpdfContainerNode;
230
+ declare function link(nodes?: LpdfNode[], options?: LinkOptions): LpdfContainerNode;
231
+ declare function table(options: TableOptions, nodes?: (LpdfTheadNode | LpdfTrNode)[]): LpdfTableNode;
232
+ declare function thead(nodes?: LpdfTdNode[], options?: TheadOptions): LpdfTheadNode;
233
+ declare function tr(nodes?: LpdfTdNode[], options?: TrOptions): LpdfTrNode;
234
+ declare function td(nodes?: LpdfNode[], options?: TdOptions): LpdfTdNode;
235
+ declare function text(nodes?: (string | LpdfSpanNode)[], options?: TextOptions): LpdfTextNode;
236
+ declare function span(nodes?: string[], options?: SpanOptions): LpdfSpanNode;
237
+ declare function divider(options?: DividerOptions): LpdfDividerNode;
238
+ declare function img(options: ImgOptions): LpdfImgNode;
239
+ declare function barcode(options: BarcodeOptions): LpdfBarcodeNode;
240
+ declare function region(pin: string, nodes?: LpdfNode[], options?: RegionOptions): LpdfRegionNode;
241
+ export declare const LpdfLayout: Readonly<{
242
+ stack: typeof stack;
243
+ flank: typeof flank;
244
+ split: typeof split;
245
+ cluster: typeof cluster;
246
+ grid: typeof grid;
247
+ frame: typeof frame;
248
+ link: typeof link;
249
+ text: typeof text;
250
+ span: typeof span;
251
+ divider: typeof divider;
252
+ img: typeof img;
253
+ barcode: typeof barcode;
254
+ table: typeof table;
255
+ thead: typeof thead;
256
+ tr: typeof tr;
257
+ td: typeof td;
258
+ region: typeof region;
259
+ }>;
260
+ export {};
package/dist/layout.js ADDED
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LpdfLayout = void 0;
4
+ // ── camelCase → kebab-case helper ─────────────────────────────────────────────
5
+ function attrKey(camel) {
6
+ return camel.replace(/[A-Z]/g, c => '-' + c.toLowerCase());
7
+ }
8
+ function buildAttrs(options) {
9
+ const result = {};
10
+ for (const [key, val] of Object.entries(options)) {
11
+ if (val !== undefined) {
12
+ result[attrKey(key)] = val;
13
+ }
14
+ }
15
+ return result;
16
+ }
17
+ // ── Helper ────────────────────────────────────────────────────────────────────
18
+ function makeContainer(type, nodes, options) {
19
+ return { type, attrs: buildAttrs(options ?? {}), nodes };
20
+ }
21
+ // ── LpdfLayout factory ────────────────────────────────────────────────────────
22
+ function stack(nodes = [], options) {
23
+ return makeContainer('stack', nodes, options);
24
+ }
25
+ function flank(nodes = [], options) {
26
+ return makeContainer('flank', nodes, options);
27
+ }
28
+ function split(nodes = [], options) {
29
+ return makeContainer('split', nodes, options);
30
+ }
31
+ function cluster(nodes = [], options) {
32
+ return makeContainer('cluster', nodes, options);
33
+ }
34
+ function grid(nodes = [], options) {
35
+ return makeContainer('grid', nodes, options);
36
+ }
37
+ function frame(nodes = [], options) {
38
+ return makeContainer('frame', nodes, options);
39
+ }
40
+ function link(nodes = [], options) {
41
+ return makeContainer('link', nodes, options);
42
+ }
43
+ function table(options, nodes = []) {
44
+ return {
45
+ type: 'table',
46
+ attrs: buildAttrs(options),
47
+ nodes,
48
+ };
49
+ }
50
+ function thead(nodes = [], options) {
51
+ return {
52
+ type: 'thead',
53
+ attrs: buildAttrs((options ?? {})),
54
+ nodes,
55
+ };
56
+ }
57
+ function tr(nodes = [], options) {
58
+ return {
59
+ type: 'tr',
60
+ attrs: buildAttrs((options ?? {})),
61
+ nodes,
62
+ };
63
+ }
64
+ function td(nodes = [], options) {
65
+ return {
66
+ type: 'td',
67
+ attrs: buildAttrs((options ?? {})),
68
+ nodes,
69
+ };
70
+ }
71
+ function text(nodes = [], options) {
72
+ return {
73
+ type: 'text',
74
+ attrs: buildAttrs((options ?? {})),
75
+ nodes,
76
+ };
77
+ }
78
+ function span(nodes = [], options) {
79
+ return {
80
+ type: 'span',
81
+ attrs: buildAttrs((options ?? {})),
82
+ nodes,
83
+ };
84
+ }
85
+ function divider(options) {
86
+ return {
87
+ type: 'divider',
88
+ attrs: buildAttrs((options ?? {})),
89
+ };
90
+ }
91
+ function img(options) {
92
+ return {
93
+ type: 'img',
94
+ attrs: buildAttrs(options),
95
+ };
96
+ }
97
+ function barcode(options) {
98
+ return {
99
+ type: 'barcode',
100
+ attrs: buildAttrs(options),
101
+ };
102
+ }
103
+ function region(pin, nodes = [], options) {
104
+ const attrs = { pin };
105
+ if (options?.page !== undefined)
106
+ attrs['page'] = options.page;
107
+ if (options?.w !== undefined)
108
+ attrs['w'] = options.w;
109
+ if (options?.debug !== undefined)
110
+ attrs['debug'] = options.debug;
111
+ return { type: 'layout-region', attrs, nodes };
112
+ }
113
+ exports.LpdfLayout = Object.freeze({
114
+ stack,
115
+ flank,
116
+ split,
117
+ cluster,
118
+ grid,
119
+ frame,
120
+ link,
121
+ text,
122
+ span,
123
+ divider,
124
+ img,
125
+ barcode,
126
+ table,
127
+ thead,
128
+ tr,
129
+ td,
130
+ region,
131
+ });