@formepdf/shared 0.29.0 → 0.30.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/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
@@ -227,6 +227,11 @@ export function mapStyle(style) {
227
227
  result.textTransform = TEXT_TRANSFORM_MAP[style.textTransform];
228
228
  if (style.hyphens !== undefined)
229
229
  result.hyphens = HYPHENS_MAP[style.hyphens];
230
+ if (style.fontFeatureSettings !== undefined || style.fontVariantNumeric !== undefined) {
231
+ const features = parseFontFeatures(style.fontVariantNumeric, style.fontFeatureSettings);
232
+ if (features)
233
+ result.fontFeatures = features;
234
+ }
230
235
  if (style.lang !== undefined)
231
236
  result.lang = style.lang;
232
237
  if (style.direction !== undefined)
@@ -478,6 +483,64 @@ export function parseColor(hex) {
478
483
  // Fallback: black
479
484
  return { r: 0, g: 0, b: 0, a: 1 };
480
485
  }
486
+ /** `font-variant-numeric` keywords and the OpenType feature each turns on. */
487
+ const VARIANT_NUMERIC_FEATURES = {
488
+ 'lining-nums': 'lnum',
489
+ 'oldstyle-nums': 'onum',
490
+ 'proportional-nums': 'pnum',
491
+ 'tabular-nums': 'tnum',
492
+ 'diagonal-fractions': 'frac',
493
+ 'stacked-fractions': 'afrc',
494
+ ordinal: 'ordn',
495
+ 'slashed-zero': 'zero',
496
+ };
497
+ /**
498
+ * Combine `fontVariantNumeric` and `fontFeatureSettings` into the engine's
499
+ * feature list. As in CSS, the low-level `font-feature-settings` is applied
500
+ * after `font-variant-numeric`, so it wins for the same tag. `normal` in
501
+ * either clears it (an empty list, which also stops inheritance). Returns
502
+ * `null` when neither parses, so the property is dropped, as other
503
+ * unparseable values are.
504
+ */
505
+ function parseFontFeatures(variantNumeric, featureSettings) {
506
+ const byTag = new Map();
507
+ let parsed = false;
508
+ if (variantNumeric !== undefined) {
509
+ const words = variantNumeric.trim().toLowerCase().split(/\s+/).filter(Boolean);
510
+ if (words.length === 1 && words[0] === 'normal') {
511
+ parsed = true;
512
+ }
513
+ else {
514
+ for (const word of words) {
515
+ const tag = VARIANT_NUMERIC_FEATURES[word];
516
+ if (tag) {
517
+ byTag.set(tag, 1);
518
+ parsed = true;
519
+ }
520
+ }
521
+ }
522
+ }
523
+ if (featureSettings !== undefined) {
524
+ const trimmed = featureSettings.trim();
525
+ if (trimmed.toLowerCase() === 'normal') {
526
+ parsed = true;
527
+ }
528
+ else {
529
+ for (const item of trimmed.split(',')) {
530
+ const m = item.trim().match(/^(["'])(.{4})\1(?:\s+(on|off|\d+))?$/i);
531
+ if (!m)
532
+ continue;
533
+ const raw = m[3]?.toLowerCase();
534
+ const value = raw === undefined || raw === 'on' ? 1 : raw === 'off' ? 0 : parseInt(raw, 10);
535
+ byTag.set(m[2], value);
536
+ parsed = true;
537
+ }
538
+ }
539
+ }
540
+ if (!parsed)
541
+ return null;
542
+ return [...byTag].map(([tag, value]) => ({ tag, value }));
543
+ }
481
544
  /**
482
545
  * Parse a CSS-like `transform` string into the engine's TransformOp[].
483
546
  *
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.30.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",