qrdx 0.0.53 → 0.0.55

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.
Files changed (114) hide show
  1. package/README.md +1 -1
  2. package/dist/core.js +7 -7
  3. package/dist/core.js.map +1 -1
  4. package/dist/core.mjs +7 -7
  5. package/dist/core.mjs.map +1 -1
  6. package/dist/element.browser.mjs +6 -67
  7. package/dist/element.browser.mjs.map +1 -1
  8. package/dist/index.js +7 -7
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +7 -7
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/qr-code-canvas.js +1 -1
  13. package/dist/qr-code-canvas.js.map +1 -1
  14. package/dist/qr-code-canvas.mjs +1 -1
  15. package/dist/qr-code-canvas.mjs.map +1 -1
  16. package/dist/schemas.js +2 -0
  17. package/dist/schemas.js.map +1 -0
  18. package/dist/schemas.mjs +2 -0
  19. package/dist/schemas.mjs.map +1 -0
  20. package/dist/src/art.d.ts +1 -1
  21. package/dist/src/art.d.ts.map +1 -1
  22. package/dist/src/engine.d.ts +2 -1
  23. package/dist/src/engine.d.ts.map +1 -1
  24. package/dist/src/eps.d.ts.map +1 -1
  25. package/dist/src/frame-import.d.ts +1 -1
  26. package/dist/src/frame-import.d.ts.map +1 -1
  27. package/dist/src/frame-spec.d.ts +5 -4
  28. package/dist/src/frame-spec.d.ts.map +1 -1
  29. package/dist/src/frame.d.ts +1 -1
  30. package/dist/src/frame.d.ts.map +1 -1
  31. package/dist/src/frames/builtin.d.ts +1 -1
  32. package/dist/src/frames/builtin.d.ts.map +1 -1
  33. package/dist/src/qr-outline.d.ts +1 -2
  34. package/dist/src/qr-outline.d.ts.map +1 -1
  35. package/dist/src/qr-shape.d.ts +1 -1
  36. package/dist/src/qr-shape.d.ts.map +1 -1
  37. package/dist/src/silhouette.d.ts +1 -2
  38. package/dist/src/silhouette.d.ts.map +1 -1
  39. package/dist/src/template/dot-ring-layout.d.ts +26 -3
  40. package/dist/src/template/dot-ring-layout.d.ts.map +1 -1
  41. package/dist/src/template/dot-ring.d.ts +22 -4
  42. package/dist/src/template/dot-ring.d.ts.map +1 -1
  43. package/dist/src/utils.d.ts +1 -1
  44. package/dist/src/utils.d.ts.map +1 -1
  45. package/dist/types/art.d.ts +1 -18
  46. package/dist/types/art.d.ts.map +1 -1
  47. package/dist/types/color-helpers.d.ts +26 -0
  48. package/dist/types/color-helpers.d.ts.map +1 -0
  49. package/dist/types/color.d.ts +1 -24
  50. package/dist/types/color.d.ts.map +1 -1
  51. package/dist/types/constants.d.ts +97 -0
  52. package/dist/types/constants.d.ts.map +1 -0
  53. package/dist/types/corner-dot.d.ts +2 -12
  54. package/dist/types/corner-dot.d.ts.map +1 -1
  55. package/dist/types/corner-eye.d.ts +2 -11
  56. package/dist/types/corner-eye.d.ts.map +1 -1
  57. package/dist/types/frame-spec-parse.d.ts +21 -0
  58. package/dist/types/frame-spec-parse.d.ts.map +1 -0
  59. package/dist/types/frame-spec-rules.d.ts +95 -0
  60. package/dist/types/frame-spec-rules.d.ts.map +1 -0
  61. package/dist/types/frame-spec.d.ts +2 -83
  62. package/dist/types/frame-spec.d.ts.map +1 -1
  63. package/dist/types/frame.d.ts +1 -8
  64. package/dist/types/frame.d.ts.map +1 -1
  65. package/dist/types/image-pattern.d.ts +1 -1
  66. package/dist/types/image-pattern.d.ts.map +1 -1
  67. package/dist/types/index.d.ts +3 -22
  68. package/dist/types/index.d.ts.map +1 -1
  69. package/dist/types/index.js +2 -2
  70. package/dist/types/index.js.map +1 -1
  71. package/dist/types/index.mjs +2 -2
  72. package/dist/types/index.mjs.map +1 -1
  73. package/dist/types/plain.d.ts +17 -0
  74. package/dist/types/plain.d.ts.map +1 -0
  75. package/dist/types/qr.d.ts +2 -46
  76. package/dist/types/qr.d.ts.map +1 -1
  77. package/dist/types/schemas.d.ts +12 -0
  78. package/dist/types/schemas.d.ts.map +1 -0
  79. package/dist/vanilla.browser.mjs +28 -28
  80. package/dist/vanilla.browser.mjs.map +1 -1
  81. package/package.json +3 -3
  82. package/src/art.tsx +3 -6
  83. package/src/body-patterns.tsx +1 -1
  84. package/src/engine.ts +53 -1
  85. package/src/eps.ts +6 -1
  86. package/src/frame-import.ts +7 -6
  87. package/src/frame-spec.ts +17 -23
  88. package/src/frame.tsx +3 -2
  89. package/src/frames/builtin.ts +2 -5
  90. package/src/helpers.ts +1 -1
  91. package/src/qr-outline.ts +2 -2
  92. package/src/qr-shape.ts +2 -5
  93. package/src/rasterize.ts +1 -1
  94. package/src/scan-score.ts +2 -2
  95. package/src/silhouette.ts +2 -2
  96. package/src/template/dot-ring-layout.ts +281 -15
  97. package/src/template/dot-ring.tsx +27 -8
  98. package/src/utils/print-shared.ts +1 -1
  99. package/src/utils.tsx +6 -5
  100. package/types/art.ts +10 -20
  101. package/types/color-helpers.ts +132 -0
  102. package/types/color.ts +9 -122
  103. package/types/constants.ts +222 -0
  104. package/types/corner-dot.ts +8 -40
  105. package/types/corner-eye.ts +2 -31
  106. package/types/frame-spec-parse.ts +683 -0
  107. package/types/frame-spec-rules.ts +645 -0
  108. package/types/frame-spec.ts +38 -635
  109. package/types/frame.ts +14 -34
  110. package/types/image-pattern.ts +2 -26
  111. package/types/index.ts +6 -162
  112. package/types/plain.ts +117 -0
  113. package/types/qr.ts +29 -51
  114. package/types/schemas.ts +62 -0
@@ -4,147 +4,45 @@
4
4
  // compiled to the same plain shapes the built-in frames use. There is no
5
5
  // markup, no URL and no image in it, so a spec from anyone is safe to store,
6
6
  // share and render. Every field is validated with hard limits (see
7
- // FRAME_SPEC_LIMITS); unknown keys are rejected.
7
+ // FRAME_SPEC_LIMITS); unknown keys are rejected. The engine validates with
8
+ // frame-spec-parse.ts, the same rules without zod
9
+ // (tests/frame-spec-parse.test.ts keeps the two in step).
8
10
 
9
11
  import { z } from "zod";
10
- import { countPathSegments, tokenizePathData } from "../src/utils/path-data";
11
- import { clipPolygon, segmentsBounds } from "../src/utils/polygon-clip";
12
- import { parsePathData } from "../src/utils/svg-geometry";
12
+ import { FRAME_TEXT_MAX_LENGTH } from "./constants";
13
+ import { frameSchema } from "./frame";
13
14
  import {
14
- FRAME_FONT_SCALE_MAX,
15
- FRAME_TEXT_MAX_LENGTH,
16
- frameSchema,
17
- } from "./frame";
18
-
19
- export const FRAME_SPEC_KIND = "qrdx.frame";
20
- export const FRAME_SPEC_VERSION = 1;
21
-
22
- /** Value of `frame.style` that draws `frame.spec`. */
23
- export const CUSTOM_FRAME_STYLE = "custom";
24
-
25
- export const FRAME_SPEC_LIMITS = {
26
- /** Artboard sides, in spec units */
27
- minArtboard: 50,
28
- maxArtboard: 2000,
29
- /** Largest absolute coordinate anywhere in a spec */
30
- maxCoordinate: 4000,
31
- /** Decimals a number may have */
32
- maxDecimals: 3,
33
- maxShapes: 64,
34
- maxLabels: 8,
35
- maxPolygonPoints: 256,
36
- /** Characters in one path's data */
37
- maxPathLength: 16_000,
38
- /** Segments across the spec: path commands (arcs count 4), polygon points, dashes */
39
- maxSegments: 2000,
40
- /** Gradient steps on one shape */
41
- maxGradientSteps: 64,
42
- /** Gradient work across the spec: steps × the shape's segments */
43
- maxGradientWork: 16_000,
44
- /** Size of the spec as JSON, in UTF-8 bytes */
45
- maxBytes: 32_768,
46
- /** The code must be at least this share of the artboard's shorter side */
47
- minQrShare: 0.5,
48
- /** Light padding around the code, as a share of its size */
49
- minPadding: 0.04,
50
- defaultPadding: 0.08,
51
- /** Characters in a label */
52
- maxTextLength: FRAME_TEXT_MAX_LENGTH,
53
- } as const;
54
-
55
- /** Built-in glyphs a spec can place with an `icon` shape (no images). */
56
- export const FRAME_ICONS = [
57
- "wifi",
58
- "star",
59
- "heart",
60
- "phone",
61
- "headphones",
62
- "calendar",
63
- "user",
64
- "chat",
65
- "bag",
66
- "bell",
67
- "cup",
68
- "card",
69
- "check",
70
- "info",
71
- "home",
72
- "pin",
73
- "share",
74
- "download",
75
- "clock",
76
- "snowflake",
77
- "pumpkin",
78
- "tree",
79
- "sparkle",
80
- "gift",
81
- ] as const;
82
-
83
- /**
84
- * Largest label font as a share of its row height, so ascenders and
85
- * descenders stay inside the row (and so off the code's padding).
86
- */
87
- export const FRAME_SPEC_LABEL_MAX_FONT = 0.95;
88
-
89
- /** Most segments any built-in icon has (counted against maxSegments). */
90
- export const FRAME_ICON_MAX_SEGMENTS = 200;
91
-
92
- /**
93
- * Theme colors a spec can bind to: `frame` is `frame.color`, `text` is
94
- * `frame.textColor`, `accent` is `frame.secondaryColor` (the gradient end)
95
- * and `background` is the code's background color.
96
- */
97
- export const FRAME_COLOR_TOKENS = [
98
- "frame",
99
- "text",
100
- "accent",
101
- "background",
102
- ] as const;
103
-
104
- // ── Text ───────────────────────────────────────────────────────────────────
105
-
106
- // Controls, invisible and direction-changing characters, and lone UTF-16
107
- // surrogates (which can't be encoded into a data URI).
108
- const LINE_BREAKS = /[\t\n\v\f\r\u0085\u2028\u2029]/g;
109
- const INVISIBLE =
110
- // biome-ignore lint/suspicious/noControlCharactersInRegex: stripping them is the point
111
- // biome-ignore lint/suspicious/noMisleadingCharacterClass: each code point in the class is stripped on its own
112
- /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u115f\u1160\u17b4\u17b5\u180b-\u180f\u200b-\u200f\u202a-\u202e\u2060-\u206f\u3164\ufe00-\ufe0f\ufeff\uffa0\ufff0-\ufffb]|\udb40[\udc00-\udcff]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g;
113
-
114
- /**
115
- * Label text as a frame may draw it: NFC, no control, invisible or
116
- * bidirectional-override characters, single spaces, trimmed and at most
117
- * `maxLength` characters (code points).
118
- */
119
- export function sanitizeFrameText(
120
- text: string,
121
- maxLength: number = FRAME_TEXT_MAX_LENGTH
122
- ): string {
123
- const clean = text
124
- .slice(0, 1000)
125
- .normalize("NFC")
126
- .replace(LINE_BREAKS, " ")
127
- .replace(INVISIBLE, "")
128
- .replace(/\s+/g, " ")
129
- .trim();
130
- return Array.from(clean).slice(0, Math.max(0, maxLength)).join("").trim();
131
- }
132
-
133
- /** True when `text` uses only Latin-1 (what PDF and EPS labels can print). */
134
- export function isLatin1Text(text: string): boolean {
135
- for (const char of text) {
136
- if ((char.codePointAt(0) ?? 0) > 0xff) {
137
- return false;
138
- }
139
- }
140
- return true;
141
- }
15
+ CUSTOM_FRAME_STYLE,
16
+ checkFrameSpec,
17
+ FRAME_COLOR_TOKENS,
18
+ FRAME_ICONS,
19
+ FRAME_SPEC_KIND,
20
+ FRAME_SPEC_LIMITS,
21
+ FRAME_SPEC_VERSION,
22
+ hasPrecision,
23
+ } from "./frame-spec-rules";
24
+
25
+ // The limits, text rules and geometry are in frame-spec-rules.ts (no zod)
26
+ export {
27
+ CUSTOM_FRAME_STYLE,
28
+ FRAME_COLOR_TOKENS,
29
+ FRAME_ICON_MAX_SEGMENTS,
30
+ FRAME_ICONS,
31
+ FRAME_SPEC_CAP_HALF,
32
+ FRAME_SPEC_DEFAULT_FONT,
33
+ FRAME_SPEC_KIND,
34
+ FRAME_SPEC_LABEL_MAX_FONT,
35
+ FRAME_SPEC_LIMITS,
36
+ FRAME_SPEC_VERSION,
37
+ frameSpecPadding,
38
+ frameSpecQrZone,
39
+ frameSpecShapeBounds,
40
+ isLatin1Text,
41
+ sanitizeFrameText,
42
+ } from "./frame-spec-rules";
142
43
 
143
44
  // ── Schemas ────────────────────────────────────────────────────────────────
144
45
 
145
- const hasPrecision = (value: number) =>
146
- Math.abs(value * 1000 - Math.round(value * 1000)) < 1e-6;
147
-
148
46
  const number = z
149
47
  .number()
150
48
  .min(-FRAME_SPEC_LIMITS.maxCoordinate)
@@ -473,511 +371,16 @@ export type FrameSpecLabel = z.infer<typeof frameSpecLabelSchema>;
473
371
  export type FrameSpecIcon = (typeof FRAME_ICONS)[number];
474
372
  export type FrameSpec = z.infer<typeof frameSpecObjectSchema>;
475
373
 
476
- // ── Cross-field checks ─────────────────────────────────────────────────────
477
-
478
- interface Box {
479
- x0: number;
480
- y0: number;
481
- x1: number;
482
- y1: number;
483
- }
484
-
485
- /** The code plus its padding: nothing may be drawn over it. */
486
- export function frameSpecQrZone(spec: FrameSpec): Box {
487
- const padding = frameSpecPadding(spec);
488
- return {
489
- x0: spec.qr.x - padding,
490
- y0: spec.qr.y - padding,
491
- x1: spec.qr.x + spec.qr.size + padding,
492
- y1: spec.qr.y + spec.qr.size + padding,
493
- };
494
- }
495
-
496
- /** The code itself, without its padding. */
497
- function frameSpecQrBox(spec: FrameSpec): Box {
498
- return {
499
- x0: spec.qr.x,
500
- y0: spec.qr.y,
501
- x1: spec.qr.x + spec.qr.size,
502
- y1: spec.qr.y + spec.qr.size,
503
- };
504
- }
505
-
506
- /** The plate's padding around the code, in spec units. */
507
- export function frameSpecPadding(spec: FrameSpec): number {
508
- return spec.qr.padding ?? spec.qr.size * FRAME_SPEC_LIMITS.defaultPadding;
509
- }
510
-
511
- /** Font size of a text label with no `fontSize`, as a share of its row. */
512
- export const FRAME_SPEC_DEFAULT_FONT = 0.52;
513
-
514
- /**
515
- * Half the height a label's capitals fill, as a share of its font size: a
516
- * text label's letters are centered on its row, so they fill the row's
517
- * middle, this far above and below it.
518
- */
519
- export const FRAME_SPEC_CAP_HALF = 0.35;
520
-
521
- /** The largest font a text label draws (at the largest fontScale). */
522
- function textLabelMaxFont(
523
- label: Extract<FrameSpecLabel, { type: "text" }>
524
- ): number {
525
- const base = label.fontSize ?? label.box.height * FRAME_SPEC_DEFAULT_FONT;
526
- return Math.min(
527
- base * FRAME_FONT_SCALE_MAX,
528
- label.box.height * FRAME_SPEC_LABEL_MAX_FONT
529
- );
530
- }
531
-
532
- const overlaps = (a: Box, b: Box) =>
533
- Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0) > 1e-6 &&
534
- Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0) > 1e-6;
535
-
536
- // ── The plate ─────────────────────────────────────────────────────────────
537
-
538
- type Point = [number, number];
539
-
540
- /** Points along the plate's outline (its corners rounded by `qr.radius`). */
541
- function plateOutline(spec: FrameSpec): Point[] {
542
- const zone = frameSpecQrZone(spec);
543
- const r = Math.min(
544
- spec.qr.radius ?? 0,
545
- (zone.x1 - zone.x0) / 2,
546
- (zone.y1 - zone.y0) / 2
547
- );
548
- const corners: [number, number, number][] = [
549
- [zone.x0 + r, zone.y0 + r, Math.PI],
550
- [zone.x1 - r, zone.y0 + r, -Math.PI / 2],
551
- [zone.x1 - r, zone.y1 - r, 0],
552
- [zone.x0 + r, zone.y1 - r, Math.PI / 2],
553
- ];
554
- const points: Point[] = [];
555
- for (const [cx, cy, start] of corners) {
556
- for (let i = 0; i <= 8; i++) {
557
- const angle = start + (Math.PI / 2) * (i / 8);
558
- points.push([cx + r * Math.cos(angle), cy + r * Math.sin(angle)]);
559
- }
560
- }
561
- return points;
562
- }
563
-
564
- function insideRoundedBox(
565
- [x, y]: Point,
566
- box: { x: number; y: number; width: number; height: number },
567
- radius: number
568
- ): boolean {
569
- const tolerance = 0.01;
570
- if (
571
- x < box.x - tolerance ||
572
- y < box.y - tolerance ||
573
- x > box.x + box.width + tolerance ||
574
- y > box.y + box.height + tolerance
575
- ) {
576
- return false;
577
- }
578
- const r = Math.min(radius, box.width / 2, box.height / 2);
579
- const cx = Math.min(Math.max(x, box.x + r), box.x + box.width - r);
580
- const cy = Math.min(Math.max(y, box.y + r), box.y + box.height - r);
581
- return Math.hypot(x - cx, y - cy) <= r + tolerance;
582
- }
583
-
584
- /** Area of a simple polygon (shoelace). */
585
- function polygonArea(points: Point[]): number {
586
- let twice = 0;
587
- points.forEach(([x0, y0], i) => {
588
- const [x1, y1] = points[(i + 1) % points.length] as Point;
589
- twice += x0 * y1 - x1 * y0;
590
- });
591
- return Math.abs(twice) / 2;
592
- }
593
-
594
- /**
595
- * True when `shape` may paint under the plate: anything but a `background`
596
- * fill, a ring whose hole holds the whole plate, or a polygon that stays
597
- * off it. Paths, lines, ellipses and icons count when their bounds reach
598
- * the plate.
599
- */
600
- function paintsUnderPlate(
601
- shape: FrameSpecShape,
602
- spec: FrameSpec,
603
- bounds: Box
604
- ): boolean {
605
- const zone = frameSpecQrZone(spec);
606
- if (!overlaps(bounds, zone)) {
607
- return false;
608
- }
609
- if ("stroke" in shape && shape.stroke) {
610
- return true;
611
- }
612
- if (shape.fill === "background") {
613
- return false;
614
- }
615
- if (shape.type === "rect" && shape.hole) {
616
- const { hole } = shape;
617
- return !plateOutline(spec).every((point) =>
618
- insideRoundedBox(point, hole, hole.radius ?? 0)
619
- );
620
- }
621
- if (shape.type === "circle" && shape.hole) {
622
- const hole = shape.hole;
623
- return !plateOutline(spec).every(
624
- ([x, y]) => Math.hypot(x - shape.cx, y - shape.cy) <= hole + 0.01
625
- );
626
- }
627
- if (shape.type === "polygon") {
628
- const clipped = clipPolygon(shape.points as Point[], zone);
629
- return clipped.length > 2 && polygonArea(clipped) > 1e-6;
630
- }
631
- return true;
632
- }
633
-
634
- /**
635
- * Bounds of a spec shape (null for path data outside the grammar) and the
636
- * segments it counts against FRAME_SPEC_LIMITS.maxSegments.
637
- */
638
- export function frameSpecShapeBounds(shape: FrameSpecShape): {
639
- bounds: Box | null;
640
- segments: number;
641
- } {
642
- const outline = outlineBounds(shape);
643
- const stroke = "stroke" in shape ? shape.stroke : undefined;
644
- if (!(stroke && outline.bounds)) {
645
- return outline;
646
- }
647
- // Half the width outside a curve; a mitered corner reaches further (up to
648
- // SVG's default miter limit of 4 half widths)
649
- const reach =
650
- shape.type === "circle" || shape.type === "ellipse"
651
- ? stroke.width / 2
652
- : shape.type === "rect"
653
- ? (stroke.width * Math.SQRT2) / 2
654
- : stroke.width * 2;
655
- const { x0, y0, x1, y1 } = outline.bounds;
656
- return {
657
- bounds: { x0: x0 - reach, y0: y0 - reach, x1: x1 + reach, y1: y1 + reach },
658
- // The outline is drawn a second time, as the stroke
659
- segments: outline.segments * 2,
660
- };
661
- }
662
-
663
- function outlineBounds(shape: FrameSpecShape): {
664
- bounds: Box | null;
665
- segments: number;
666
- } {
667
- switch (shape.type) {
668
- case "rect":
669
- return {
670
- bounds: {
671
- x0: shape.x,
672
- y0: shape.y,
673
- x1: shape.x + shape.width,
674
- y1: shape.y + shape.height,
675
- },
676
- segments: shape.hole ? 18 : 9,
677
- };
678
- case "circle":
679
- return {
680
- bounds: {
681
- x0: shape.cx - shape.r,
682
- y0: shape.cy - shape.r,
683
- x1: shape.cx + shape.r,
684
- y1: shape.cy + shape.r,
685
- },
686
- segments: shape.hole ? 10 : 5,
687
- };
688
- case "ellipse":
689
- return {
690
- bounds: {
691
- x0: shape.cx - shape.rx,
692
- y0: shape.cy - shape.ry,
693
- x1: shape.cx + shape.rx,
694
- y1: shape.cy + shape.ry,
695
- },
696
- segments: 5,
697
- };
698
- case "polygon": {
699
- const xs = shape.points.map(([x]) => x);
700
- const ys = shape.points.map(([, y]) => y);
701
- return {
702
- bounds: {
703
- x0: Math.min(...xs),
704
- y0: Math.min(...ys),
705
- x1: Math.max(...xs),
706
- y1: Math.max(...ys),
707
- },
708
- segments: shape.points.length,
709
- };
710
- }
711
- case "path": {
712
- const commands = tokenizePathData(shape.d, FRAME_SPEC_LIMITS.maxDecimals);
713
- if (!commands) {
714
- return { bounds: null, segments: 0 };
715
- }
716
- const bounds = segmentsBounds(parsePathData(shape.d));
717
- return { bounds, segments: countPathSegments(commands) };
718
- }
719
- case "line": {
720
- const half = shape.width / 2;
721
- const length = Math.hypot(shape.x2 - shape.x1, shape.y2 - shape.y1);
722
- const dashes = shape.dash
723
- ? Math.ceil(length / (shape.dash[0] + shape.dash[1]))
724
- : 1;
725
- return {
726
- bounds: {
727
- x0: Math.min(shape.x1, shape.x2) - half,
728
- y0: Math.min(shape.y1, shape.y2) - half,
729
- x1: Math.max(shape.x1, shape.x2) + half,
730
- y1: Math.max(shape.y1, shape.y2) + half,
731
- },
732
- segments: dashes * (shape.cap === "round" ? 4 : 5),
733
- };
734
- }
735
- default:
736
- return {
737
- bounds: {
738
- x0: shape.x,
739
- y0: shape.y,
740
- x1: shape.x + shape.size,
741
- y1: shape.y + shape.size,
742
- },
743
- // Upper bound for the built-in icons (tests/frame-spec.test.ts)
744
- segments: FRAME_ICON_MAX_SEGMENTS,
745
- };
746
- }
747
- }
748
-
749
- function checkFrameSpec(spec: FrameSpec, ctx: z.RefinementCtx) {
750
- const issue = (message: string, path: (string | number)[] = []) =>
751
- ctx.addIssue({ code: "custom", message, path });
752
- const limits = FRAME_SPEC_LIMITS;
753
-
754
- const bytes = new TextEncoder().encode(JSON.stringify(spec)).length;
755
- if (bytes > limits.maxBytes) {
756
- issue(`The spec is ${bytes} bytes; the limit is ${limits.maxBytes}`);
757
- return;
758
- }
759
-
760
- const { width, height } = spec.artboard;
761
- const tolerance = Math.max(width, height) * 0.005;
762
- const artboard: Box = {
763
- x0: -tolerance,
764
- y0: -tolerance,
765
- x1: width + tolerance,
766
- y1: height + tolerance,
767
- };
768
- const inside = (box: Box) =>
769
- box.x0 >= artboard.x0 &&
770
- box.y0 >= artboard.y0 &&
771
- box.x1 <= artboard.x1 &&
772
- box.y1 <= artboard.y1;
773
-
774
- // The code: big enough, inside the artboard with its padding
775
- const { qr } = spec;
776
- const padding = frameSpecPadding(spec);
777
- if (qr.plate !== "none" && padding < qr.size * limits.minPadding - 1e-9) {
778
- issue(
779
- `qr.padding must be at least ${limits.minPadding * 100}% of qr.size`,
780
- ["qr", "padding"]
781
- );
782
- }
783
- if (qr.size < Math.min(width, height) * limits.minQrShare - 1e-9) {
784
- issue(
785
- `qr.size must be at least ${limits.minQrShare * 100}% of the artboard's shorter side`,
786
- ["qr", "size"]
787
- );
788
- }
789
- const zone = frameSpecQrZone(spec);
790
- if (
791
- zone.x0 < -1e-9 ||
792
- zone.y0 < -1e-9 ||
793
- zone.x1 > width + 1e-9 ||
794
- zone.y1 > height + 1e-9
795
- ) {
796
- issue("The code and its padding must sit inside the artboard", ["qr"]);
797
- }
798
- if ((qr.radius ?? 0) > (qr.size + 2 * padding) / 2) {
799
- issue("qr.radius is larger than half the plate", ["qr", "radius"]);
800
- }
801
-
802
- // Shapes: inside the artboard, within the segment budget
803
- let segments = 0;
804
- let gradientWork = 0;
805
- spec.shapes.forEach((shape, index) => {
806
- const path = ["shapes", index];
807
- const { bounds, segments: count } = frameSpecShapeBounds(shape);
808
- if (!bounds) {
809
- issue(
810
- "Path data must use only M L H V C S Q T A Z, plain numbers (up to 3 decimals) and start with M",
811
- [...path, "d"]
812
- );
813
- return;
814
- }
815
- if (!inside(bounds)) {
816
- issue("The shape extends outside the artboard", path);
817
- }
818
- if (shape.type === "rect" && shape.hole) {
819
- const { hole } = shape;
820
- if (
821
- hole.x < shape.x ||
822
- hole.y < shape.y ||
823
- hole.x + hole.width > shape.x + shape.width + 1e-9 ||
824
- hole.y + hole.height > shape.y + shape.height + 1e-9
825
- ) {
826
- issue("The hole must sit inside its rect", [...path, "hole"]);
827
- }
828
- }
829
- if (shape.type === "circle" && shape.hole && shape.hole >= shape.r) {
830
- issue("The hole must be smaller than the circle", [...path, "hole"]);
831
- }
832
- if (
833
- "stroke" in shape &&
834
- shape.stroke &&
835
- shape.stroke.width > Math.min(width, height) * 0.1 + 1e-9
836
- ) {
837
- issue("A stroke may be at most 10% of the artboard's shorter side", [
838
- ...path,
839
- "stroke",
840
- "width",
841
- ]);
842
- }
843
- if (qr.plate === "background" && paintsUnderPlate(shape, spec, bounds)) {
844
- issue(
845
- 'With qr.plate "background", only background fills (or a ring whose hole holds the plate) may lie under the plate',
846
- path
847
- );
848
- }
849
- segments += count;
850
- if (typeof shape.fill === "object" && "gradient" in shape.fill) {
851
- gradientWork += (shape.fill.gradient.steps ?? 32) * count;
852
- }
853
- });
854
- if (segments > limits.maxSegments) {
855
- issue(
856
- `The shapes have ${segments} segments; the limit is ${limits.maxSegments}`,
857
- ["shapes"]
858
- );
859
- }
860
- if (gradientWork > limits.maxGradientWork) {
861
- issue("Gradient fills are too detailed (fewer steps or simpler shapes)", [
862
- "shapes",
863
- ]);
864
- }
865
-
866
- // Labels: clean text, inside the artboard, never over the code
867
- const bands: { y: number; height: number }[] = [];
868
- const ids = new Set<string>();
869
- const slots = new Set<string>();
870
- spec.labels.forEach((label, index) => {
871
- const path = ["labels", index];
872
- if (ids.has(label.id)) {
873
- issue(`Label id "${label.id}" is used twice`, [...path, "id"]);
874
- }
875
- ids.add(label.id);
876
- if (label.slot !== "fixed") {
877
- if (slots.has(label.slot)) {
878
- issue(`Only one label may use the ${label.slot} slot`, [
879
- ...path,
880
- "slot",
881
- ]);
882
- }
883
- slots.add(label.slot);
884
- }
885
- const maxLength = label.maxLength ?? limits.maxTextLength;
886
- if (label.text !== sanitizeFrameText(label.text, maxLength)) {
887
- issue(
888
- `Label text must be at most ${maxLength} characters, trimmed, with no control or invisible characters (see sanitizeFrameText)`,
889
- [...path, "text"]
890
- );
891
- }
892
- let box: Box;
893
- if (label.type === "text") {
894
- box = {
895
- x0: label.box.x,
896
- y0: label.box.y,
897
- x1: label.box.x + label.box.width,
898
- y1: label.box.y + label.box.height,
899
- };
900
- if (
901
- label.fontSize !== undefined &&
902
- label.fontSize > label.box.height * FRAME_SPEC_LABEL_MAX_FONT + 1e-9
903
- ) {
904
- issue("fontSize may be at most 95% of the label's row height", [
905
- ...path,
906
- "fontSize",
907
- ]);
908
- }
909
- // The row may reach into the padding, but never over the code, and
910
- // the band its capitals fill (at the largest font) stays off both
911
- const middle = label.box.y + label.box.height / 2;
912
- const reach = textLabelMaxFont(label) * FRAME_SPEC_CAP_HALF;
913
- const letters: Box = {
914
- x0: box.x0,
915
- x1: box.x1,
916
- y0: middle - reach,
917
- y1: middle + reach,
918
- };
919
- if (overlaps(box, frameSpecQrBox(spec)) || overlaps(letters, zone)) {
920
- issue("The label overlaps the code or its padding", [...path, "box"]);
921
- }
922
- } else {
923
- const { cx, cy, radius } = label.curve;
924
- const reach = label.fontSize * FRAME_FONT_SCALE_MAX * 0.6;
925
- box = {
926
- x0: cx - radius - reach,
927
- y0: cy - radius - reach,
928
- x1: cx + radius + reach,
929
- y1: cy + radius + reach,
930
- };
931
- const nearest = Math.hypot(
932
- Math.max(zone.x0 - cx, 0, cx - zone.x1),
933
- Math.max(zone.y0 - cy, 0, cy - zone.y1)
934
- );
935
- const farthest = Math.hypot(
936
- Math.max(Math.abs(zone.x0 - cx), Math.abs(zone.x1 - cx)),
937
- Math.max(Math.abs(zone.y0 - cy), Math.abs(zone.y1 - cy))
938
- );
939
- if (!(farthest <= radius - reach || nearest >= radius + reach)) {
940
- issue("The curved label crosses the code or its padding", [
941
- ...path,
942
- "curve",
943
- ]);
944
- }
945
- }
946
- if (!inside(box)) {
947
- issue("The label extends outside the artboard", path);
948
- }
949
- if (label.collapse) {
950
- const band = label.collapse;
951
- const end = band.y + band.height;
952
- if (band.y < -1e-9 || end > height + 1e-9) {
953
- issue("The collapse band must sit inside the artboard", [
954
- ...path,
955
- "collapse",
956
- ]);
957
- }
958
- if (end > zone.y0 + 1e-6 && band.y < zone.y1 - 1e-6) {
959
- issue("The collapse band crosses the code or its padding", [
960
- ...path,
961
- "collapse",
962
- ]);
963
- }
964
- for (const other of bands) {
965
- if (end > other.y + 1e-6 && band.y < other.y + other.height - 1e-6) {
966
- issue("Collapse bands may not overlap", [...path, "collapse"]);
967
- }
968
- }
969
- bands.push(band);
970
- }
971
- });
972
- }
973
-
974
374
  /**
975
375
  * Zod schema for a frame spec, with every limit and cross-field check
976
376
  * (FRAME_SPEC_LIMITS). Validate specs from users with this before storing
977
377
  * them; the engine validates again before drawing.
978
378
  */
979
- export const frameSpecSchema =
980
- frameSpecObjectSchema.superRefine(checkFrameSpec);
379
+ export const frameSpecSchema = frameSpecObjectSchema.superRefine((spec, ctx) =>
380
+ checkFrameSpec(spec, (message, path = []) =>
381
+ ctx.addIssue({ code: "custom", message, path })
382
+ )
383
+ );
981
384
 
982
385
  /** Human-readable problems with `input` as a frame spec (empty when valid). */
983
386
  export function getFrameSpecIssues(input: unknown): string[] {