@formepdf/shared 0.29.0 → 0.31.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,6 @@
1
+ /**
2
+ * CSS named colours (CSS Color Module Level 4: 148 names plus `transparent`), as [r, g, b, alpha] in 0-255. The same table backs the Python SDK
3
+ * (`formepdf/_colors.py`) and the HTML path (`html/src/colors.rs`); a test
4
+ * in each keeps them identical.
5
+ */
6
+ export declare const NAMED_COLORS: Readonly<Record<string, readonly [number, number, number, number]>>;
package/dist/colors.js ADDED
@@ -0,0 +1,156 @@
1
+ /**
2
+ * CSS named colours (CSS Color Module Level 4: 148 names plus `transparent`), as [r, g, b, alpha] in 0-255. The same table backs the Python SDK
3
+ * (`formepdf/_colors.py`) and the HTML path (`html/src/colors.rs`); a test
4
+ * in each keeps them identical.
5
+ */
6
+ export const NAMED_COLORS = {
7
+ aliceblue: [240, 248, 255, 255],
8
+ antiquewhite: [250, 235, 215, 255],
9
+ aqua: [0, 255, 255, 255],
10
+ aquamarine: [127, 255, 212, 255],
11
+ azure: [240, 255, 255, 255],
12
+ beige: [245, 245, 220, 255],
13
+ bisque: [255, 228, 196, 255],
14
+ black: [0, 0, 0, 255],
15
+ blanchedalmond: [255, 235, 205, 255],
16
+ blue: [0, 0, 255, 255],
17
+ blueviolet: [138, 43, 226, 255],
18
+ brown: [165, 42, 42, 255],
19
+ burlywood: [222, 184, 135, 255],
20
+ cadetblue: [95, 158, 160, 255],
21
+ chartreuse: [127, 255, 0, 255],
22
+ chocolate: [210, 105, 30, 255],
23
+ coral: [255, 127, 80, 255],
24
+ cornflowerblue: [100, 149, 237, 255],
25
+ cornsilk: [255, 248, 220, 255],
26
+ crimson: [220, 20, 60, 255],
27
+ cyan: [0, 255, 255, 255],
28
+ darkblue: [0, 0, 139, 255],
29
+ darkcyan: [0, 139, 139, 255],
30
+ darkgoldenrod: [184, 134, 11, 255],
31
+ darkgray: [169, 169, 169, 255],
32
+ darkgreen: [0, 100, 0, 255],
33
+ darkgrey: [169, 169, 169, 255],
34
+ darkkhaki: [189, 183, 107, 255],
35
+ darkmagenta: [139, 0, 139, 255],
36
+ darkolivegreen: [85, 107, 47, 255],
37
+ darkorange: [255, 140, 0, 255],
38
+ darkorchid: [153, 50, 204, 255],
39
+ darkred: [139, 0, 0, 255],
40
+ darksalmon: [233, 150, 122, 255],
41
+ darkseagreen: [143, 188, 143, 255],
42
+ darkslateblue: [72, 61, 139, 255],
43
+ darkslategray: [47, 79, 79, 255],
44
+ darkslategrey: [47, 79, 79, 255],
45
+ darkturquoise: [0, 206, 209, 255],
46
+ darkviolet: [148, 0, 211, 255],
47
+ deeppink: [255, 20, 147, 255],
48
+ deepskyblue: [0, 191, 255, 255],
49
+ dimgray: [105, 105, 105, 255],
50
+ dimgrey: [105, 105, 105, 255],
51
+ dodgerblue: [30, 144, 255, 255],
52
+ firebrick: [178, 34, 34, 255],
53
+ floralwhite: [255, 250, 240, 255],
54
+ forestgreen: [34, 139, 34, 255],
55
+ fuchsia: [255, 0, 255, 255],
56
+ gainsboro: [220, 220, 220, 255],
57
+ ghostwhite: [248, 248, 255, 255],
58
+ gold: [255, 215, 0, 255],
59
+ goldenrod: [218, 165, 32, 255],
60
+ gray: [128, 128, 128, 255],
61
+ green: [0, 128, 0, 255],
62
+ greenyellow: [173, 255, 47, 255],
63
+ grey: [128, 128, 128, 255],
64
+ honeydew: [240, 255, 240, 255],
65
+ hotpink: [255, 105, 180, 255],
66
+ indianred: [205, 92, 92, 255],
67
+ indigo: [75, 0, 130, 255],
68
+ ivory: [255, 255, 240, 255],
69
+ khaki: [240, 230, 140, 255],
70
+ lavender: [230, 230, 250, 255],
71
+ lavenderblush: [255, 240, 245, 255],
72
+ lawngreen: [124, 252, 0, 255],
73
+ lemonchiffon: [255, 250, 205, 255],
74
+ lightblue: [173, 216, 230, 255],
75
+ lightcoral: [240, 128, 128, 255],
76
+ lightcyan: [224, 255, 255, 255],
77
+ lightgoldenrodyellow: [250, 250, 210, 255],
78
+ lightgray: [211, 211, 211, 255],
79
+ lightgreen: [144, 238, 144, 255],
80
+ lightgrey: [211, 211, 211, 255],
81
+ lightpink: [255, 182, 193, 255],
82
+ lightsalmon: [255, 160, 122, 255],
83
+ lightseagreen: [32, 178, 170, 255],
84
+ lightskyblue: [135, 206, 250, 255],
85
+ lightslategray: [119, 136, 153, 255],
86
+ lightslategrey: [119, 136, 153, 255],
87
+ lightsteelblue: [176, 196, 222, 255],
88
+ lightyellow: [255, 255, 224, 255],
89
+ lime: [0, 255, 0, 255],
90
+ limegreen: [50, 205, 50, 255],
91
+ linen: [250, 240, 230, 255],
92
+ magenta: [255, 0, 255, 255],
93
+ maroon: [128, 0, 0, 255],
94
+ mediumaquamarine: [102, 205, 170, 255],
95
+ mediumblue: [0, 0, 205, 255],
96
+ mediumorchid: [186, 85, 211, 255],
97
+ mediumpurple: [147, 112, 219, 255],
98
+ mediumseagreen: [60, 179, 113, 255],
99
+ mediumslateblue: [123, 104, 238, 255],
100
+ mediumspringgreen: [0, 250, 154, 255],
101
+ mediumturquoise: [72, 209, 204, 255],
102
+ mediumvioletred: [199, 21, 133, 255],
103
+ midnightblue: [25, 25, 112, 255],
104
+ mintcream: [245, 255, 250, 255],
105
+ mistyrose: [255, 228, 225, 255],
106
+ moccasin: [255, 228, 181, 255],
107
+ navajowhite: [255, 222, 173, 255],
108
+ navy: [0, 0, 128, 255],
109
+ oldlace: [253, 245, 230, 255],
110
+ olive: [128, 128, 0, 255],
111
+ olivedrab: [107, 142, 35, 255],
112
+ orange: [255, 165, 0, 255],
113
+ orangered: [255, 69, 0, 255],
114
+ orchid: [218, 112, 214, 255],
115
+ palegoldenrod: [238, 232, 170, 255],
116
+ palegreen: [152, 251, 152, 255],
117
+ paleturquoise: [175, 238, 238, 255],
118
+ palevioletred: [219, 112, 147, 255],
119
+ papayawhip: [255, 239, 213, 255],
120
+ peachpuff: [255, 218, 185, 255],
121
+ peru: [205, 133, 63, 255],
122
+ pink: [255, 192, 203, 255],
123
+ plum: [221, 160, 221, 255],
124
+ powderblue: [176, 224, 230, 255],
125
+ purple: [128, 0, 128, 255],
126
+ rebeccapurple: [102, 51, 153, 255],
127
+ red: [255, 0, 0, 255],
128
+ rosybrown: [188, 143, 143, 255],
129
+ royalblue: [65, 105, 225, 255],
130
+ saddlebrown: [139, 69, 19, 255],
131
+ salmon: [250, 128, 114, 255],
132
+ sandybrown: [244, 164, 96, 255],
133
+ seagreen: [46, 139, 87, 255],
134
+ seashell: [255, 245, 238, 255],
135
+ sienna: [160, 82, 45, 255],
136
+ silver: [192, 192, 192, 255],
137
+ skyblue: [135, 206, 235, 255],
138
+ slateblue: [106, 90, 205, 255],
139
+ slategray: [112, 128, 144, 255],
140
+ slategrey: [112, 128, 144, 255],
141
+ snow: [255, 250, 250, 255],
142
+ springgreen: [0, 255, 127, 255],
143
+ steelblue: [70, 130, 180, 255],
144
+ tan: [210, 180, 140, 255],
145
+ teal: [0, 128, 128, 255],
146
+ thistle: [216, 191, 216, 255],
147
+ tomato: [255, 99, 71, 255],
148
+ transparent: [0, 0, 0, 0],
149
+ turquoise: [64, 224, 208, 255],
150
+ violet: [238, 130, 238, 255],
151
+ wheat: [245, 222, 179, 255],
152
+ white: [255, 255, 255, 255],
153
+ whitesmoke: [245, 245, 245, 255],
154
+ yellow: [255, 255, 0, 255],
155
+ yellowgreen: [154, 205, 50, 255],
156
+ };
package/dist/index.d.ts CHANGED
@@ -5,6 +5,6 @@ export { recordCanvasOperations } from './canvas.js';
5
5
  export { STRONG_DEFAULTS, EM_DEFAULTS, CODE_DEFAULTS, LINK_DEFAULTS, HEADING_DEFAULTS, mapListMarker, } from './semantics.js';
6
6
  export { buildBarChartKind, buildLineChartKind, buildPieChartKind, buildAreaChartKind, buildDotPlotKind, } from './charts.js';
7
7
  export type { BarChartProps, LineChartProps, PieChartProps, AreaChartProps, DotPlotProps, } from './charts.js';
8
- export type { Style, GridTrackSize, Edges, Corners, EdgeColors, CertificationConfig, SignatureConfig, ColumnDef, BarcodeFormat, ChartDataPoint, PieDataPoint, ChartSeries, DotPlotGroup, CanvasContext, CanvasOp, TextRun, ListMarker, FormeDocument, FormeDocumentClaimProps, FormeFont, FormeNode, FormeNodeKind, FormeStyle, FormeMetadata, FormePageConfig, FormePageSize, FormeEdges, FormeBoxShadow, FormeGradientStop, FormeBackground, FormeMarginEdges, FormeColumnDef, FormeColumnWidth, FormeDimension, FormeColor, FormeEdgeValues, FormeCornerValues, FormeGridTrackSize, FormeGridPlacement, FormeListMarkerType, FormeTransformOp, } from './types.js';
8
+ export type { Style, GridTrackSize, Edges, Corners, EdgeColors, CertificationConfig, SignatureConfig, ColumnDef, BarcodeFormat, ChartDataPoint, PieDataPoint, ChartSeries, DotPlotGroup, CanvasContext, CanvasOp, TextRun, ListMarker, FormeDocument, FormeDocumentClaimProps, FormeFont, FormeNode, FormeNodeKind, FormeStyle, FormeMetadata, FormePageConfig, FormePageSize, FormeEdges, FormeBoxShadow, FormeGradientStop, FormeBackground, FormeMarginEdges, FormeColumnDef, FormeColumnWidth, FormeDimension, FormeColor, FormeEdgeValues, FormeCornerValues, FormeGridTrackSize, FormeGridPlacement, FormeListMarkerType, FormeTransformOp, FormeFontFeature, } from './types.js';
9
9
  export { parseMarkup } from './parser.js';
10
10
  export { encodeProps, reviveBytesMarker } from './encode.js';
package/dist/style.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { NAMED_COLORS } from './colors.js';
1
2
  // ─── Style mapping ──────────────────────────────────────────────────
2
3
  const FLEX_DIRECTION_MAP = {
3
4
  'row': 'Row',
@@ -227,6 +228,11 @@ export function mapStyle(style) {
227
228
  result.textTransform = TEXT_TRANSFORM_MAP[style.textTransform];
228
229
  if (style.hyphens !== undefined)
229
230
  result.hyphens = HYPHENS_MAP[style.hyphens];
231
+ if (style.fontFeatureSettings !== undefined || style.fontVariantNumeric !== undefined) {
232
+ const features = parseFontFeatures(style.fontVariantNumeric, style.fontFeatureSettings);
233
+ if (features)
234
+ result.fontFeatures = features;
235
+ }
230
236
  if (style.lang !== undefined)
231
237
  result.lang = style.lang;
232
238
  if (style.direction !== undefined)
@@ -455,7 +461,19 @@ export function parseColor(hex) {
455
461
  a: 1,
456
462
  };
457
463
  }
464
+ // CSS colour names (`red`, `rebeccapurple`, `transparent`). Read as hex,
465
+ // "red" became r: NaN, which serializes as null and failed the whole
466
+ // render with a schema error that never mentioned colour.
467
+ const named = NAMED_COLORS[s.toLowerCase()];
468
+ if (named) {
469
+ return { r: named[0] / 255, g: named[1] / 255, b: named[2] / 255, a: named[3] / 255 };
470
+ }
458
471
  const h = s.replace(/^#/, '');
472
+ // Anything else that is not hex falls back to black below rather than
473
+ // producing NaN channels.
474
+ if (!/^[0-9a-fA-F]+$/.test(h)) {
475
+ return { r: 0, g: 0, b: 0, a: 1 };
476
+ }
459
477
  if (h.length === 3) {
460
478
  const r = parseInt(h[0] + h[0], 16) / 255;
461
479
  const g = parseInt(h[1] + h[1], 16) / 255;
@@ -478,6 +496,64 @@ export function parseColor(hex) {
478
496
  // Fallback: black
479
497
  return { r: 0, g: 0, b: 0, a: 1 };
480
498
  }
499
+ /** `font-variant-numeric` keywords and the OpenType feature each turns on. */
500
+ const VARIANT_NUMERIC_FEATURES = {
501
+ 'lining-nums': 'lnum',
502
+ 'oldstyle-nums': 'onum',
503
+ 'proportional-nums': 'pnum',
504
+ 'tabular-nums': 'tnum',
505
+ 'diagonal-fractions': 'frac',
506
+ 'stacked-fractions': 'afrc',
507
+ ordinal: 'ordn',
508
+ 'slashed-zero': 'zero',
509
+ };
510
+ /**
511
+ * Combine `fontVariantNumeric` and `fontFeatureSettings` into the engine's
512
+ * feature list. As in CSS, the low-level `font-feature-settings` is applied
513
+ * after `font-variant-numeric`, so it wins for the same tag. `normal` in
514
+ * either clears it (an empty list, which also stops inheritance). Returns
515
+ * `null` when neither parses, so the property is dropped, as other
516
+ * unparseable values are.
517
+ */
518
+ function parseFontFeatures(variantNumeric, featureSettings) {
519
+ const byTag = new Map();
520
+ let parsed = false;
521
+ if (variantNumeric !== undefined) {
522
+ const words = variantNumeric.trim().toLowerCase().split(/\s+/).filter(Boolean);
523
+ if (words.length === 1 && words[0] === 'normal') {
524
+ parsed = true;
525
+ }
526
+ else {
527
+ for (const word of words) {
528
+ const tag = VARIANT_NUMERIC_FEATURES[word];
529
+ if (tag) {
530
+ byTag.set(tag, 1);
531
+ parsed = true;
532
+ }
533
+ }
534
+ }
535
+ }
536
+ if (featureSettings !== undefined) {
537
+ const trimmed = featureSettings.trim();
538
+ if (trimmed.toLowerCase() === 'normal') {
539
+ parsed = true;
540
+ }
541
+ else {
542
+ for (const item of trimmed.split(',')) {
543
+ const m = item.trim().match(/^(["'])(.{4})\1(?:\s+(on|off|\d+))?$/i);
544
+ if (!m)
545
+ continue;
546
+ const raw = m[3]?.toLowerCase();
547
+ const value = raw === undefined || raw === 'on' ? 1 : raw === 'off' ? 0 : parseInt(raw, 10);
548
+ byTag.set(m[2], value);
549
+ parsed = true;
550
+ }
551
+ }
552
+ }
553
+ if (!parsed)
554
+ return null;
555
+ return [...byTag].map(([tag, value]) => ({ tag, value }));
556
+ }
481
557
  /**
482
558
  * Parse a CSS-like `transform` string into the engine's TransformOp[].
483
559
  *
package/dist/types.d.ts CHANGED
@@ -123,6 +123,20 @@ export interface Style {
123
123
  textDecoration?: 'none' | 'underline' | 'line-through';
124
124
  textTransform?: 'none' | 'uppercase' | 'lowercase' | 'capitalize';
125
125
  hyphens?: 'none' | 'manual' | 'auto';
126
+ /**
127
+ * OpenType features, as CSS `font-feature-settings`: `'"tnum" 1'`,
128
+ * `'"zero", "ss01" on'`, `'normal'`. Applies to fonts with font data
129
+ * (registered fonts and the builtin Noto Sans); the standard fonts are not
130
+ * shaped and ignore it. Inherited.
131
+ */
132
+ fontFeatureSettings?: string;
133
+ /**
134
+ * CSS `font-variant-numeric`: `'tabular-nums'`, `'oldstyle-nums'`,
135
+ * `'proportional-nums'`, `'lining-nums'`, `'slashed-zero'`,
136
+ * `'diagonal-fractions'`, `'stacked-fractions'`, `'ordinal'`, or several
137
+ * separated by spaces. `fontFeatureSettings` wins for the same feature.
138
+ */
139
+ fontVariantNumeric?: string;
126
140
  /** Language tag (BCP 47, e.g. "en-US", "de"). Controls hyphenation dictionary. */
127
141
  lang?: string;
128
142
  /** Text direction for BiDi support (Arabic, Hebrew). */
@@ -722,6 +736,11 @@ export interface FormeGridPlacement {
722
736
  rowSpan?: number;
723
737
  }
724
738
  /** Style in the Forme JSON format (camelCase field names, PascalCase enum values) */
739
+ /** One OpenType feature for shaping, e.g. `{ tag: 'tnum', value: 1 }`. */
740
+ export interface FormeFontFeature {
741
+ tag: string;
742
+ value: number;
743
+ }
725
744
  export interface FormeStyle {
726
745
  display?: string;
727
746
  width?: FormeDimension;
@@ -763,6 +782,7 @@ export interface FormeStyle {
763
782
  textDecoration?: string;
764
783
  textTransform?: string;
765
784
  hyphens?: string;
785
+ fontFeatures?: FormeFontFeature[];
766
786
  lang?: string;
767
787
  direction?: string;
768
788
  textOverflow?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formepdf/shared",
3
- "version": "0.29.0",
3
+ "version": "0.31.0",
4
4
  "description": "Framework-agnostic document model and serialization core shared by Forme adapters",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",