@formepdf/react 0.4.4 → 0.6.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/charts.d.ts +41 -0
- package/dist/charts.js +338 -0
- package/dist/components.d.ts +56 -1
- package/dist/components.js +61 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/serialize.d.ts +1 -1
- package/dist/serialize.js +340 -9
- package/dist/types.d.ts +289 -3
- package/package.json +1 -1
package/dist/serialize.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isValidElement, Children, Fragment } from 'react';
|
|
2
|
-
import { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, PageBreak } from './components.js';
|
|
2
|
+
import { Document, Page, View, Text, Image, Table, Row, Cell, Fixed, Svg, QrCode, Canvas, Watermark, PageBreak } from './components.js';
|
|
3
3
|
import { Font } from './font.js';
|
|
4
4
|
import { isRefMarker, getRefPath, isEachMarker, getEachPath, getEachTemplate, isExprMarker, getExpr, REF_SENTINEL, REF_SENTINEL_END, } from './template-proxy.js';
|
|
5
5
|
const VALID_PARENTS = {
|
|
@@ -89,6 +89,8 @@ export function serialize(element) {
|
|
|
89
89
|
metadata.subject = props.subject;
|
|
90
90
|
if (props.creator !== undefined)
|
|
91
91
|
metadata.creator = props.creator;
|
|
92
|
+
if (props.lang !== undefined)
|
|
93
|
+
metadata.lang = props.lang;
|
|
92
94
|
// Merge global + document fonts (document fonts override on conflict)
|
|
93
95
|
const mergedFonts = mergeFonts(Font.getRegistered(), props.fonts);
|
|
94
96
|
const result = {
|
|
@@ -100,6 +102,10 @@ export function serialize(element) {
|
|
|
100
102
|
wrap: true,
|
|
101
103
|
},
|
|
102
104
|
};
|
|
105
|
+
if (props.tagged !== undefined)
|
|
106
|
+
result.tagged = props.tagged;
|
|
107
|
+
if (props.pdfa !== undefined)
|
|
108
|
+
result.pdfa = props.pdfa;
|
|
103
109
|
if (mergedFonts.length > 0) {
|
|
104
110
|
result.fonts = mergedFonts;
|
|
105
111
|
}
|
|
@@ -194,6 +200,15 @@ function serializeChild(child, parent = null) {
|
|
|
194
200
|
if (element.type === Svg) {
|
|
195
201
|
return serializeSvg(element);
|
|
196
202
|
}
|
|
203
|
+
if (element.type === QrCode) {
|
|
204
|
+
return serializeQrCode(element);
|
|
205
|
+
}
|
|
206
|
+
if (element.type === Canvas) {
|
|
207
|
+
return serializeCanvas(element);
|
|
208
|
+
}
|
|
209
|
+
if (element.type === Watermark) {
|
|
210
|
+
return serializeWatermark(element);
|
|
211
|
+
}
|
|
197
212
|
if (element.type === PageBreak) {
|
|
198
213
|
return {
|
|
199
214
|
kind: { type: 'PageBreak' },
|
|
@@ -296,12 +311,17 @@ function serializeImage(element) {
|
|
|
296
311
|
kind.width = props.width;
|
|
297
312
|
if (props.height !== undefined)
|
|
298
313
|
kind.height = props.height;
|
|
299
|
-
|
|
314
|
+
const node = {
|
|
300
315
|
kind,
|
|
301
316
|
style: mapStyle(props.style),
|
|
302
317
|
children: [],
|
|
303
318
|
sourceLocation: extractSourceLocation(element),
|
|
304
319
|
};
|
|
320
|
+
if (props.href)
|
|
321
|
+
node.href = props.href;
|
|
322
|
+
if (props.alt)
|
|
323
|
+
node.alt = props.alt;
|
|
324
|
+
return node;
|
|
305
325
|
}
|
|
306
326
|
function serializeTable(element, _parent = null) {
|
|
307
327
|
const props = element.props;
|
|
@@ -364,13 +384,94 @@ function serializeSvg(element) {
|
|
|
364
384
|
};
|
|
365
385
|
if (props.viewBox)
|
|
366
386
|
kind.view_box = props.viewBox;
|
|
387
|
+
const node = {
|
|
388
|
+
kind,
|
|
389
|
+
style: mapStyle(props.style),
|
|
390
|
+
children: [],
|
|
391
|
+
sourceLocation: extractSourceLocation(element),
|
|
392
|
+
};
|
|
393
|
+
if (props.href)
|
|
394
|
+
node.href = props.href;
|
|
395
|
+
if (props.alt)
|
|
396
|
+
node.alt = props.alt;
|
|
397
|
+
return node;
|
|
398
|
+
}
|
|
399
|
+
function serializeQrCode(element) {
|
|
400
|
+
const props = element.props;
|
|
401
|
+
const kind = { type: 'QrCode', data: props.data };
|
|
402
|
+
if (props.size !== undefined)
|
|
403
|
+
kind.size = props.size;
|
|
404
|
+
const style = mapStyle(props.style);
|
|
405
|
+
if (props.color)
|
|
406
|
+
style.color = parseColor(props.color);
|
|
367
407
|
return {
|
|
368
408
|
kind,
|
|
409
|
+
style,
|
|
410
|
+
children: [],
|
|
411
|
+
sourceLocation: extractSourceLocation(element),
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
function serializeCanvas(element) {
|
|
415
|
+
const props = element.props;
|
|
416
|
+
const operations = [];
|
|
417
|
+
// Create a recording context that captures draw calls as CanvasOp[]
|
|
418
|
+
const ctx = {
|
|
419
|
+
moveTo(x, y) { operations.push({ op: 'MoveTo', x, y }); },
|
|
420
|
+
lineTo(x, y) { operations.push({ op: 'LineTo', x, y }); },
|
|
421
|
+
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
422
|
+
operations.push({ op: 'BezierCurveTo', cp1x, cp1y, cp2x, cp2y, x, y });
|
|
423
|
+
},
|
|
424
|
+
quadraticCurveTo(cpx, cpy, x, y) {
|
|
425
|
+
operations.push({ op: 'QuadraticCurveTo', cpx, cpy, x, y });
|
|
426
|
+
},
|
|
427
|
+
closePath() { operations.push({ op: 'ClosePath' }); },
|
|
428
|
+
rect(x, y, w, h) { operations.push({ op: 'Rect', x, y, width: w, height: h }); },
|
|
429
|
+
circle(cx, cy, r) { operations.push({ op: 'Circle', cx, cy, r }); },
|
|
430
|
+
ellipse(cx, cy, rx, ry) { operations.push({ op: 'Ellipse', cx, cy, rx, ry }); },
|
|
431
|
+
arc(cx, cy, r, startAngle, endAngle, counterclockwise = false) {
|
|
432
|
+
operations.push({ op: 'Arc', cx, cy, r, start_angle: startAngle, end_angle: endAngle, counterclockwise });
|
|
433
|
+
},
|
|
434
|
+
stroke() { operations.push({ op: 'Stroke' }); },
|
|
435
|
+
fill() { operations.push({ op: 'Fill' }); },
|
|
436
|
+
fillAndStroke() { operations.push({ op: 'FillAndStroke' }); },
|
|
437
|
+
setFillColor(r, g, b) { operations.push({ op: 'SetFillColor', r, g, b }); },
|
|
438
|
+
setStrokeColor(r, g, b) { operations.push({ op: 'SetStrokeColor', r, g, b }); },
|
|
439
|
+
setLineWidth(w) { operations.push({ op: 'SetLineWidth', width: w }); },
|
|
440
|
+
setLineCap(cap) { operations.push({ op: 'SetLineCap', cap }); },
|
|
441
|
+
setLineJoin(join) { operations.push({ op: 'SetLineJoin', join }); },
|
|
442
|
+
save() { operations.push({ op: 'Save' }); },
|
|
443
|
+
restore() { operations.push({ op: 'Restore' }); },
|
|
444
|
+
};
|
|
445
|
+
// Execute the draw callback to record operations
|
|
446
|
+
props.draw(ctx);
|
|
447
|
+
return {
|
|
448
|
+
kind: { type: 'Canvas', width: props.width, height: props.height, operations },
|
|
369
449
|
style: mapStyle(props.style),
|
|
370
450
|
children: [],
|
|
371
451
|
sourceLocation: extractSourceLocation(element),
|
|
372
452
|
};
|
|
373
453
|
}
|
|
454
|
+
function serializeWatermark(element) {
|
|
455
|
+
const props = element.props;
|
|
456
|
+
const fontSize = props.fontSize ?? 60;
|
|
457
|
+
const angle = props.angle ?? -45;
|
|
458
|
+
const colorStr = props.color ?? 'rgba(0,0,0,0.1)';
|
|
459
|
+
// Parse color — extract alpha for opacity
|
|
460
|
+
const parsedColor = parseColor(colorStr);
|
|
461
|
+
const style = mapStyle(props.style);
|
|
462
|
+
style.color = { r: parsedColor.r, g: parsedColor.g, b: parsedColor.b, a: 1 };
|
|
463
|
+
// Multiply color alpha with any existing opacity
|
|
464
|
+
const colorOpacity = parsedColor.a;
|
|
465
|
+
const styleOpacity = (style.opacity !== undefined) ? style.opacity : 1;
|
|
466
|
+
style.opacity = colorOpacity * styleOpacity;
|
|
467
|
+
style.fontSize = fontSize;
|
|
468
|
+
return {
|
|
469
|
+
kind: { type: 'Watermark', text: props.text, font_size: fontSize, angle },
|
|
470
|
+
style,
|
|
471
|
+
children: [],
|
|
472
|
+
sourceLocation: extractSourceLocation(element),
|
|
473
|
+
};
|
|
474
|
+
}
|
|
374
475
|
// ─── Children helpers ────────────────────────────────────────────────
|
|
375
476
|
function flattenChildren(children) {
|
|
376
477
|
const result = [];
|
|
@@ -489,6 +590,20 @@ const TEXT_TRANSFORM_MAP = {
|
|
|
489
590
|
'lowercase': 'Lowercase',
|
|
490
591
|
'capitalize': 'Capitalize',
|
|
491
592
|
};
|
|
593
|
+
const HYPHENS_MAP = {
|
|
594
|
+
'none': 'none',
|
|
595
|
+
'manual': 'manual',
|
|
596
|
+
'auto': 'auto',
|
|
597
|
+
};
|
|
598
|
+
const TEXT_OVERFLOW_MAP = {
|
|
599
|
+
'wrap': 'Wrap',
|
|
600
|
+
'ellipsis': 'Ellipsis',
|
|
601
|
+
'clip': 'Clip',
|
|
602
|
+
};
|
|
603
|
+
const OVERFLOW_MAP = {
|
|
604
|
+
'visible': 'Visible',
|
|
605
|
+
'hidden': 'Hidden',
|
|
606
|
+
};
|
|
492
607
|
export function mapStyle(style) {
|
|
493
608
|
if (!style)
|
|
494
609
|
return {};
|
|
@@ -567,6 +682,42 @@ export function mapStyle(style) {
|
|
|
567
682
|
result.rowGap = style.rowGap;
|
|
568
683
|
if (style.columnGap !== undefined)
|
|
569
684
|
result.columnGap = style.columnGap;
|
|
685
|
+
// Display mode
|
|
686
|
+
if (style.display !== undefined) {
|
|
687
|
+
result.display = style.display === 'grid' ? 'Grid' : 'Flex';
|
|
688
|
+
}
|
|
689
|
+
// CSS Grid
|
|
690
|
+
if (style.gridTemplateColumns !== undefined) {
|
|
691
|
+
result.gridTemplateColumns = parseGridTemplate(style.gridTemplateColumns);
|
|
692
|
+
}
|
|
693
|
+
if (style.gridTemplateRows !== undefined) {
|
|
694
|
+
result.gridTemplateRows = parseGridTemplate(style.gridTemplateRows);
|
|
695
|
+
}
|
|
696
|
+
if (style.gridAutoRows !== undefined) {
|
|
697
|
+
result.gridAutoRows = mapGridTrack(style.gridAutoRows);
|
|
698
|
+
}
|
|
699
|
+
if (style.gridAutoColumns !== undefined) {
|
|
700
|
+
result.gridAutoColumns = mapGridTrack(style.gridAutoColumns);
|
|
701
|
+
}
|
|
702
|
+
// Grid placement (individual props → single gridPlacement object)
|
|
703
|
+
if (style.gridColumnStart !== undefined || style.gridColumnEnd !== undefined ||
|
|
704
|
+
style.gridRowStart !== undefined || style.gridRowEnd !== undefined ||
|
|
705
|
+
style.gridColumnSpan !== undefined || style.gridRowSpan !== undefined) {
|
|
706
|
+
const placement = {};
|
|
707
|
+
if (style.gridColumnStart !== undefined)
|
|
708
|
+
placement.columnStart = style.gridColumnStart;
|
|
709
|
+
if (style.gridColumnEnd !== undefined)
|
|
710
|
+
placement.columnEnd = style.gridColumnEnd;
|
|
711
|
+
if (style.gridRowStart !== undefined)
|
|
712
|
+
placement.rowStart = style.gridRowStart;
|
|
713
|
+
if (style.gridRowEnd !== undefined)
|
|
714
|
+
placement.rowEnd = style.gridRowEnd;
|
|
715
|
+
if (style.gridColumnSpan !== undefined)
|
|
716
|
+
placement.columnSpan = style.gridColumnSpan;
|
|
717
|
+
if (style.gridRowSpan !== undefined)
|
|
718
|
+
placement.rowSpan = style.gridRowSpan;
|
|
719
|
+
result.gridPlacement = placement;
|
|
720
|
+
}
|
|
570
721
|
// Typography
|
|
571
722
|
if (style.fontFamily !== undefined)
|
|
572
723
|
result.fontFamily = style.fontFamily;
|
|
@@ -587,6 +738,16 @@ export function mapStyle(style) {
|
|
|
587
738
|
result.textDecoration = TEXT_DECORATION_MAP[style.textDecoration];
|
|
588
739
|
if (style.textTransform !== undefined)
|
|
589
740
|
result.textTransform = TEXT_TRANSFORM_MAP[style.textTransform];
|
|
741
|
+
if (style.hyphens !== undefined)
|
|
742
|
+
result.hyphens = HYPHENS_MAP[style.hyphens];
|
|
743
|
+
if (style.lang !== undefined)
|
|
744
|
+
result.lang = style.lang;
|
|
745
|
+
if (style.direction !== undefined)
|
|
746
|
+
result.direction = style.direction;
|
|
747
|
+
if (style.textOverflow !== undefined)
|
|
748
|
+
result.textOverflow = TEXT_OVERFLOW_MAP[style.textOverflow];
|
|
749
|
+
if (style.overflow !== undefined)
|
|
750
|
+
result.overflow = OVERFLOW_MAP[style.overflow];
|
|
590
751
|
// Color
|
|
591
752
|
if (style.color !== undefined)
|
|
592
753
|
result.color = parseColor(style.color);
|
|
@@ -594,9 +755,40 @@ export function mapStyle(style) {
|
|
|
594
755
|
result.backgroundColor = parseColor(style.backgroundColor);
|
|
595
756
|
if (style.opacity !== undefined)
|
|
596
757
|
result.opacity = style.opacity;
|
|
597
|
-
// Border
|
|
598
|
-
|
|
599
|
-
|
|
758
|
+
// Border — cascade: border < borderTop/Right/Bottom/Left < borderWidth/borderColor < borderTopWidth/borderTopColor
|
|
759
|
+
// Step 1: Parse string shorthands into intermediate per-side values
|
|
760
|
+
let shortWidth = { top: undefined, right: undefined, bottom: undefined, left: undefined };
|
|
761
|
+
let shortColor = { top: undefined, right: undefined, bottom: undefined, left: undefined };
|
|
762
|
+
if (style.border !== undefined) {
|
|
763
|
+
const parsed = parseBorderString(style.border);
|
|
764
|
+
if (parsed.width !== undefined)
|
|
765
|
+
shortWidth = { top: parsed.width, right: parsed.width, bottom: parsed.width, left: parsed.width };
|
|
766
|
+
if (parsed.color !== undefined)
|
|
767
|
+
shortColor = { top: parsed.color, right: parsed.color, bottom: parsed.color, left: parsed.color };
|
|
768
|
+
}
|
|
769
|
+
// Per-side string shorthands override all-side shorthand
|
|
770
|
+
for (const [side, prop] of [['top', 'borderTop'], ['right', 'borderRight'], ['bottom', 'borderBottom'], ['left', 'borderLeft']]) {
|
|
771
|
+
const val = style[prop];
|
|
772
|
+
if (val === undefined)
|
|
773
|
+
continue;
|
|
774
|
+
if (typeof val === 'number') {
|
|
775
|
+
shortWidth[side] = val;
|
|
776
|
+
}
|
|
777
|
+
else {
|
|
778
|
+
const parsed = parseBorderString(val);
|
|
779
|
+
if (parsed.width !== undefined)
|
|
780
|
+
shortWidth[side] = parsed.width;
|
|
781
|
+
if (parsed.color !== undefined)
|
|
782
|
+
shortColor[side] = parsed.color;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
// Step 2: Build borderWidth — existing borderWidth/borderTopWidth override shorthands
|
|
786
|
+
const hasBorderWidth = style.borderWidth !== undefined || style.borderTopWidth !== undefined || style.borderRightWidth !== undefined || style.borderBottomWidth !== undefined || style.borderLeftWidth !== undefined;
|
|
787
|
+
const hasShortWidth = shortWidth.top !== undefined || shortWidth.right !== undefined || shortWidth.bottom !== undefined || shortWidth.left !== undefined;
|
|
788
|
+
if (hasBorderWidth || hasShortWidth) {
|
|
789
|
+
const base = style.borderWidth !== undefined
|
|
790
|
+
? expandEdgeValues(style.borderWidth)
|
|
791
|
+
: { top: shortWidth.top ?? 0, right: shortWidth.right ?? 0, bottom: shortWidth.bottom ?? 0, left: shortWidth.left ?? 0 };
|
|
600
792
|
result.borderWidth = {
|
|
601
793
|
top: style.borderTopWidth ?? base.top,
|
|
602
794
|
right: style.borderRightWidth ?? base.right,
|
|
@@ -604,14 +796,22 @@ export function mapStyle(style) {
|
|
|
604
796
|
left: style.borderLeftWidth ?? base.left,
|
|
605
797
|
};
|
|
606
798
|
}
|
|
607
|
-
|
|
799
|
+
// Step 3: Build borderColor — existing borderColor/borderTopColor override shorthands
|
|
800
|
+
const hasBorderColor = style.borderColor !== undefined || style.borderTopColor !== undefined || style.borderRightColor !== undefined || style.borderBottomColor !== undefined || style.borderLeftColor !== undefined;
|
|
801
|
+
const hasShortColor = shortColor.top !== undefined || shortColor.right !== undefined || shortColor.bottom !== undefined || shortColor.left !== undefined;
|
|
802
|
+
if (hasBorderColor || hasShortColor) {
|
|
608
803
|
const defaultColor = parseColor('#000000');
|
|
609
|
-
let base = {
|
|
804
|
+
let base = {
|
|
805
|
+
top: shortColor.top ?? defaultColor,
|
|
806
|
+
right: shortColor.right ?? defaultColor,
|
|
807
|
+
bottom: shortColor.bottom ?? defaultColor,
|
|
808
|
+
left: shortColor.left ?? defaultColor,
|
|
809
|
+
};
|
|
610
810
|
if (typeof style.borderColor === 'string') {
|
|
611
811
|
const c = parseColor(style.borderColor);
|
|
612
812
|
base = { top: c, right: c, bottom: c, left: c };
|
|
613
813
|
}
|
|
614
|
-
else if (style.borderColor) {
|
|
814
|
+
else if (style.borderColor && typeof style.borderColor === 'object') {
|
|
615
815
|
base = {
|
|
616
816
|
top: parseColor(style.borderColor.top),
|
|
617
817
|
right: parseColor(style.borderColor.right),
|
|
@@ -658,6 +858,60 @@ export function mapStyle(style) {
|
|
|
658
858
|
result.minOrphanLines = style.minOrphanLines;
|
|
659
859
|
return result;
|
|
660
860
|
}
|
|
861
|
+
// ─── Grid helpers ───────────────────────────────────────────────────
|
|
862
|
+
/** Convert a single GridTrackSize to the Forme JSON format. */
|
|
863
|
+
function mapGridTrack(track) {
|
|
864
|
+
if (typeof track === 'number')
|
|
865
|
+
return { Pt: track };
|
|
866
|
+
if (track === 'auto')
|
|
867
|
+
return 'Auto';
|
|
868
|
+
if (typeof track === 'string') {
|
|
869
|
+
const frMatch = track.match(/^([0-9.]+)fr$/);
|
|
870
|
+
if (frMatch)
|
|
871
|
+
return { Fr: parseFloat(frMatch[1]) };
|
|
872
|
+
// Try numeric string
|
|
873
|
+
const num = parseFloat(track);
|
|
874
|
+
if (!isNaN(num))
|
|
875
|
+
return { Pt: num };
|
|
876
|
+
return 'Auto';
|
|
877
|
+
}
|
|
878
|
+
if (typeof track === 'object' && 'min' in track && 'max' in track) {
|
|
879
|
+
return { MinMax: [mapGridTrack(track.min), mapGridTrack(track.max)] };
|
|
880
|
+
}
|
|
881
|
+
return 'Auto';
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Expand `repeat(N, tracks)` in a grid template string.
|
|
885
|
+
* E.g. `"repeat(3, 1fr)"` → `"1fr 1fr 1fr"`
|
|
886
|
+
* `"200 repeat(2, 1fr) 200"` → `"200 1fr 1fr 200"`
|
|
887
|
+
*/
|
|
888
|
+
function expandRepeat(input) {
|
|
889
|
+
return input.replace(/repeat\(\s*(\d+)\s*,\s*([^)]+)\)/g, (_match, count, tracks) => {
|
|
890
|
+
return (tracks.trim() + ' ').repeat(parseInt(count, 10)).trim();
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* Parse a grid template string shorthand into an array of FormeGridTrackSize.
|
|
895
|
+
* E.g. `"1fr 2fr 200"` → `[{Fr:1}, {Fr:2}, {Pt:200}]`
|
|
896
|
+
* Supports `repeat(N, tracks)` syntax.
|
|
897
|
+
*/
|
|
898
|
+
function parseGridTemplate(value) {
|
|
899
|
+
if (Array.isArray(value)) {
|
|
900
|
+
return value.map(mapGridTrack);
|
|
901
|
+
}
|
|
902
|
+
const expanded = expandRepeat(value);
|
|
903
|
+
return expanded.split(/\s+/).filter(Boolean).map((token) => {
|
|
904
|
+
if (token === 'auto')
|
|
905
|
+
return 'Auto';
|
|
906
|
+
const frMatch = token.match(/^([0-9.]+)fr$/);
|
|
907
|
+
if (frMatch)
|
|
908
|
+
return { Fr: parseFloat(frMatch[1]) };
|
|
909
|
+
const num = parseFloat(token);
|
|
910
|
+
if (!isNaN(num))
|
|
911
|
+
return { Pt: num };
|
|
912
|
+
return 'Auto';
|
|
913
|
+
});
|
|
914
|
+
}
|
|
661
915
|
export function mapDimension(val) {
|
|
662
916
|
if (typeof val === 'number') {
|
|
663
917
|
return { Pt: val };
|
|
@@ -676,7 +930,28 @@ export function mapDimension(val) {
|
|
|
676
930
|
return 'Auto';
|
|
677
931
|
}
|
|
678
932
|
export function parseColor(hex) {
|
|
679
|
-
const
|
|
933
|
+
const s = hex.trim();
|
|
934
|
+
// rgba(r, g, b, a)
|
|
935
|
+
const rgbaMatch = s.match(/^rgba\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*\)$/);
|
|
936
|
+
if (rgbaMatch) {
|
|
937
|
+
return {
|
|
938
|
+
r: parseFloat(rgbaMatch[1]) / 255,
|
|
939
|
+
g: parseFloat(rgbaMatch[2]) / 255,
|
|
940
|
+
b: parseFloat(rgbaMatch[3]) / 255,
|
|
941
|
+
a: parseFloat(rgbaMatch[4]),
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
// rgb(r, g, b)
|
|
945
|
+
const rgbMatch = s.match(/^rgb\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*\)$/);
|
|
946
|
+
if (rgbMatch) {
|
|
947
|
+
return {
|
|
948
|
+
r: parseFloat(rgbMatch[1]) / 255,
|
|
949
|
+
g: parseFloat(rgbMatch[2]) / 255,
|
|
950
|
+
b: parseFloat(rgbMatch[3]) / 255,
|
|
951
|
+
a: 1,
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
const h = s.replace(/^#/, '');
|
|
680
955
|
if (h.length === 3) {
|
|
681
956
|
const r = parseInt(h[0] + h[0], 16) / 255;
|
|
682
957
|
const g = parseInt(h[1] + h[1], 16) / 255;
|
|
@@ -699,10 +974,54 @@ export function parseColor(hex) {
|
|
|
699
974
|
// Fallback: black
|
|
700
975
|
return { r: 0, g: 0, b: 0, a: 1 };
|
|
701
976
|
}
|
|
977
|
+
/**
|
|
978
|
+
* Parse a CSS-style 1-4 value edge shorthand.
|
|
979
|
+
* Accepts: `"8"`, `"8 16"`, `"8 16 24"`, `"8 16 24 32"` (with optional `px` suffix).
|
|
980
|
+
* Also accepts number arrays: `[8]`, `[8, 16]`, `[8, 16, 24]`, `[8, 16, 24, 32]`.
|
|
981
|
+
*/
|
|
982
|
+
function parseCSSEdges(val) {
|
|
983
|
+
const values = Array.isArray(val)
|
|
984
|
+
? val
|
|
985
|
+
: val.trim().split(/\s+/).map(s => parseFloat(s.replace(/px$/i, '')));
|
|
986
|
+
switch (values.length) {
|
|
987
|
+
case 1: return { top: values[0], right: values[0], bottom: values[0], left: values[0] };
|
|
988
|
+
case 2: return { top: values[0], right: values[1], bottom: values[0], left: values[1] };
|
|
989
|
+
case 3: return { top: values[0], right: values[1], bottom: values[2], left: values[1] };
|
|
990
|
+
default: return { top: values[0], right: values[1], bottom: values[2], left: values[3] };
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
const BORDER_STYLE_KEYWORDS = new Set([
|
|
994
|
+
'solid', 'dashed', 'dotted', 'double', 'groove', 'ridge', 'inset', 'outset', 'none', 'hidden',
|
|
995
|
+
]);
|
|
996
|
+
/**
|
|
997
|
+
* Parse a CSS border shorthand string like `"1px solid #000"`.
|
|
998
|
+
* Returns extracted width and/or color. Style keywords are recognized but ignored.
|
|
999
|
+
*/
|
|
1000
|
+
function parseBorderString(val) {
|
|
1001
|
+
const tokens = val.trim().split(/\s+/);
|
|
1002
|
+
let width;
|
|
1003
|
+
let color;
|
|
1004
|
+
for (const token of tokens) {
|
|
1005
|
+
const lower = token.toLowerCase();
|
|
1006
|
+
if (BORDER_STYLE_KEYWORDS.has(lower))
|
|
1007
|
+
continue;
|
|
1008
|
+
const num = parseFloat(lower.replace(/px$/i, ''));
|
|
1009
|
+
if (!isNaN(num) && /^[\d.]/.test(lower)) {
|
|
1010
|
+
width = num;
|
|
1011
|
+
}
|
|
1012
|
+
else {
|
|
1013
|
+
color = parseColor(token);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
return { width, color };
|
|
1017
|
+
}
|
|
702
1018
|
export function expandEdges(val) {
|
|
703
1019
|
if (typeof val === 'number') {
|
|
704
1020
|
return { top: val, right: val, bottom: val, left: val };
|
|
705
1021
|
}
|
|
1022
|
+
if (typeof val === 'string' || Array.isArray(val)) {
|
|
1023
|
+
return parseCSSEdges(val);
|
|
1024
|
+
}
|
|
706
1025
|
return { top: val.top, right: val.right, bottom: val.bottom, left: val.left };
|
|
707
1026
|
}
|
|
708
1027
|
function expandEdgeValues(val) {
|
|
@@ -809,6 +1128,8 @@ export function serializeTemplate(element) {
|
|
|
809
1128
|
metadata.subject = processTemplateValue(props.subject);
|
|
810
1129
|
if (props.creator !== undefined)
|
|
811
1130
|
metadata.creator = processTemplateValue(props.creator);
|
|
1131
|
+
if (props.lang !== undefined)
|
|
1132
|
+
metadata.lang = processTemplateValue(props.lang);
|
|
812
1133
|
const mergedFonts = mergeFonts(Font.getRegistered(), props.fonts);
|
|
813
1134
|
const result = {
|
|
814
1135
|
children,
|
|
@@ -819,6 +1140,10 @@ export function serializeTemplate(element) {
|
|
|
819
1140
|
wrap: true,
|
|
820
1141
|
},
|
|
821
1142
|
};
|
|
1143
|
+
if (props.tagged !== undefined)
|
|
1144
|
+
result.tagged = props.tagged;
|
|
1145
|
+
if (props.pdfa !== undefined)
|
|
1146
|
+
result.pdfa = props.pdfa;
|
|
822
1147
|
if (mergedFonts.length > 0) {
|
|
823
1148
|
result.fonts = mergedFonts;
|
|
824
1149
|
}
|
|
@@ -911,6 +1236,12 @@ function serializeTemplateChild(child, parent = null) {
|
|
|
911
1236
|
return serializeTemplateFixed(element);
|
|
912
1237
|
if (element.type === Svg)
|
|
913
1238
|
return serializeSvg(element);
|
|
1239
|
+
if (element.type === QrCode)
|
|
1240
|
+
return serializeQrCode(element);
|
|
1241
|
+
if (element.type === Canvas)
|
|
1242
|
+
return serializeCanvas(element);
|
|
1243
|
+
if (element.type === Watermark)
|
|
1244
|
+
return serializeWatermark(element);
|
|
914
1245
|
if (element.type === PageBreak) {
|
|
915
1246
|
return { kind: { type: 'PageBreak' }, style: {}, children: [] };
|
|
916
1247
|
}
|