@polotno/schema 0.5.0 → 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/lib/defaults.d.ts CHANGED
@@ -53,3 +53,29 @@ export declare const TEXT_DEFAULTS: {
53
53
  readonly curveEnabled: false;
54
54
  readonly curvePower: 0.5;
55
55
  };
56
+ /**
57
+ * The smallest font size a text element, a table or a table cell can hold. Not
58
+ * a rendering preference — an INVARIANT of the format: each of those models
59
+ * clamps `fontSize` below it at both doors, a loaded snapshot and `set()`, so
60
+ * no element the canvas has ever touched holds less. Clamping one door only is
61
+ * how the table path drifted: `set()` held the floor while a loaded design
62
+ * kept its sub-1 size, and the canvas and the exporters disagreed again.
63
+ * `schema-equivalence.test.ts` is the seam that proves they no longer do.
64
+ *
65
+ * The schema clamps to it for the same reason: a renderer that meets a smaller
66
+ * value has no canvas behavior to mirror, so it invents one. pdf-export read
67
+ * `font-size: 0px` as *absent* and inherited a 16px root default, svg-export's
68
+ * shrink loop walked to `-1px`, html-export emitted an invisible `0px`. Three
69
+ * renderers, three answers, none of them the canvas.
70
+ *
71
+ * @polotno/core's `FIT_MIN_FONT_SIZE` (the floor its fit loops converge to) and
72
+ * the editor's model clamps both derive from this — one rule, stated once.
73
+ */
74
+ export declare const MIN_FONT_SIZE = 1;
75
+ /**
76
+ * Apply that floor. The one implementation: the zod schema, the text model and
77
+ * the table model all call this, so the rule cannot be spelled two ways.
78
+ * `undefined` passes through — an omitted table-cell size means "inherit from
79
+ * the table", which is not a size to clamp.
80
+ */
81
+ export declare const clampFontSize: <T extends number | undefined>(size: T) => T;
package/lib/defaults.js CHANGED
@@ -101,3 +101,29 @@ export const TEXT_DEFAULTS = {
101
101
  curveEnabled: false,
102
102
  curvePower: 0.5,
103
103
  };
104
+ /**
105
+ * The smallest font size a text element, a table or a table cell can hold. Not
106
+ * a rendering preference — an INVARIANT of the format: each of those models
107
+ * clamps `fontSize` below it at both doors, a loaded snapshot and `set()`, so
108
+ * no element the canvas has ever touched holds less. Clamping one door only is
109
+ * how the table path drifted: `set()` held the floor while a loaded design
110
+ * kept its sub-1 size, and the canvas and the exporters disagreed again.
111
+ * `schema-equivalence.test.ts` is the seam that proves they no longer do.
112
+ *
113
+ * The schema clamps to it for the same reason: a renderer that meets a smaller
114
+ * value has no canvas behavior to mirror, so it invents one. pdf-export read
115
+ * `font-size: 0px` as *absent* and inherited a 16px root default, svg-export's
116
+ * shrink loop walked to `-1px`, html-export emitted an invisible `0px`. Three
117
+ * renderers, three answers, none of them the canvas.
118
+ *
119
+ * @polotno/core's `FIT_MIN_FONT_SIZE` (the floor its fit loops converge to) and
120
+ * the editor's model clamps both derive from this — one rule, stated once.
121
+ */
122
+ export const MIN_FONT_SIZE = 1;
123
+ /**
124
+ * Apply that floor. The one implementation: the zod schema, the text model and
125
+ * the table model all call this, so the rule cannot be spelled two ways.
126
+ * `undefined` passes through — an omitted table-cell size means "inherit from
127
+ * the table", which is not a size to clamp.
128
+ */
129
+ export const clampFontSize = (size) => (size != null && size < MIN_FONT_SIZE ? MIN_FONT_SIZE : size);
@@ -456,7 +456,7 @@
456
456
  },
457
457
  "fontSize": {
458
458
  "default": 14,
459
- "description": "Font size in pixels.",
459
+ "description": "Font size in pixels (minimum 1).",
460
460
  "type": "number"
461
461
  },
462
462
  "fontFamily": {
@@ -2573,7 +2573,7 @@
2573
2573
  }
2574
2574
  },
2575
2575
  "fontSize": {
2576
- "description": "Font size in pixels (inherits from the table when omitted).",
2576
+ "description": "Font size in pixels (inherits from the table when omitted, minimum 1).",
2577
2577
  "type": "number"
2578
2578
  },
2579
2579
  "fontFamily": {
@@ -2668,7 +2668,7 @@
2668
2668
  },
2669
2669
  "fontSize": {
2670
2670
  "default": 30,
2671
- "description": "Default font size in pixels for cells.",
2671
+ "description": "Default font size in pixels for cells (minimum 1).",
2672
2672
  "type": "number"
2673
2673
  },
2674
2674
  "fontFamily": {
package/lib/schema.js CHANGED
@@ -15,11 +15,17 @@
15
15
  // `validateDesign({ mode: 'canonical' })`.
16
16
  //
17
17
  // Defaults match the core MST models. We do NOT reproduce loadJSON's imperative
18
- // quirk handling (locked shorthand, svgSource fallback, fontSize clamp, cell
19
- // stripping) — normalize is "fill defaults + validate", exact for clean input.
18
+ // quirk handling (locked shorthand, svgSource fallback, cell stripping) —
19
+ // normalize is "fill defaults + validate", exact for clean input.
20
+ //
21
+ // The fontSize floor is the ONE exception, and it is not a loadJSON quirk: the
22
+ // text and table models clamp it at every door, so it is an invariant of the
23
+ // format rather than a behavior of one entry point. Leaving it out did not keep
24
+ // the schema neutral, it just moved the decision into each renderer — see
25
+ // MIN_FONT_SIZE in ./defaults for what they each decided.
20
26
  import { z } from 'zod';
21
27
  import { polotnoError } from './errors.js';
22
- import { TEXT_DEFAULTS } from './defaults.js';
28
+ import { MIN_FONT_SIZE, clampFontSize, TEXT_DEFAULTS } from './defaults.js';
23
29
  export const CURRENT_SCHEMA_VERSION = 4;
24
30
  // Every element type the design format knows. Kept in sync with the
25
31
  // element discriminatedUnion in build() below.
@@ -198,10 +204,18 @@ function build(o) {
198
204
  .string()
199
205
  .default('')
200
206
  .describe('Placeholder text shown when empty.'),
207
+ // Every authored font size is clamped (see clampFontSize in ./defaults) — a
208
+ // clamp and not a `.min()` refusal, because the editor accepts these
209
+ // designs and rejecting them here would make the schema stricter than the
210
+ // canvas. `.overwrite()` and not `.transform()`: it keeps the node a
211
+ // ZodNumber, so `.default()`/`.optional()` still chain onto it, `z.infer`
212
+ // stays `number` (types-parity.ts fails on a ZodPipe here), and the JSON
213
+ // Schema generator still has a number to emit.
201
214
  fontSize: z
202
215
  .number()
216
+ .overwrite(clampFontSize)
203
217
  .default(TEXT_DEFAULTS.fontSize)
204
- .describe('Font size in pixels.'),
218
+ .describe(`Font size in pixels (minimum ${MIN_FONT_SIZE}).`),
205
219
  fontFamily: z
206
220
  .string()
207
221
  .default(TEXT_DEFAULTS.fontFamily)
@@ -413,8 +427,9 @@ function build(o) {
413
427
  .describe('Per-side border overrides for this cell.'),
414
428
  fontSize: z
415
429
  .number()
430
+ .overwrite(clampFontSize)
416
431
  .optional()
417
- .describe('Font size in pixels (inherits from the table when omitted).'),
432
+ .describe(`Font size in pixels (inherits from the table when omitted, minimum ${MIN_FONT_SIZE}).`),
418
433
  fontFamily: z
419
434
  .string()
420
435
  .optional()
@@ -508,8 +523,9 @@ function build(o) {
508
523
  .describe('Default border style.'),
509
524
  fontSize: z
510
525
  .number()
526
+ .overwrite(clampFontSize)
511
527
  .default(30)
512
- .describe('Default font size in pixels for cells.'),
528
+ .describe(`Default font size in pixels for cells (minimum ${MIN_FONT_SIZE}).`),
513
529
  fontFamily: z
514
530
  .string()
515
531
  .default('Roboto')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polotno/schema",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Canonical Polotno design format: types, normalize, validate, JSON Schema. Authored in zod, DOM-free.",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "type": "module",