qrdx 0.0.54 → 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 (113) 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 +1 -1
  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/scan-score.ts +2 -2
  94. package/src/silhouette.ts +2 -2
  95. package/src/template/dot-ring-layout.ts +281 -15
  96. package/src/template/dot-ring.tsx +27 -8
  97. package/src/utils/print-shared.ts +1 -1
  98. package/src/utils.tsx +6 -5
  99. package/types/art.ts +10 -20
  100. package/types/color-helpers.ts +132 -0
  101. package/types/color.ts +9 -122
  102. package/types/constants.ts +222 -0
  103. package/types/corner-dot.ts +8 -40
  104. package/types/corner-eye.ts +2 -31
  105. package/types/frame-spec-parse.ts +683 -0
  106. package/types/frame-spec-rules.ts +645 -0
  107. package/types/frame-spec.ts +38 -635
  108. package/types/frame.ts +14 -34
  109. package/types/image-pattern.ts +2 -26
  110. package/types/index.ts +6 -162
  111. package/types/plain.ts +117 -0
  112. package/types/qr.ts +29 -51
  113. package/types/schemas.ts +62 -0
@@ -0,0 +1,645 @@
1
+ /** biome-ignore-all lint/style/noMagicNumbers: spec limits */
2
+ // The rules of a frame spec that don't need zod: its limits, the text a
3
+ // label may show, its geometry and the checks across fields. The engine
4
+ // validates and draws specs with these and frame-spec-parse.ts, so drawing
5
+ // a frame never loads zod; frame-spec.ts builds frameSpecSchema (zod) on
6
+ // the same rules and re-exports them.
7
+
8
+ import { countPathSegments, tokenizePathData } from "../src/utils/path-data";
9
+ import { clipPolygon, segmentsBounds } from "../src/utils/polygon-clip";
10
+ import { parsePathData } from "../src/utils/svg-geometry";
11
+ import { FRAME_FONT_SCALE_MAX, FRAME_TEXT_MAX_LENGTH } from "./constants";
12
+ import type { FrameSpec, FrameSpecLabel, FrameSpecShape } from "./frame-spec";
13
+
14
+ export const FRAME_SPEC_KIND = "qrdx.frame";
15
+ export const FRAME_SPEC_VERSION = 1;
16
+
17
+ /** Value of `frame.style` that draws `frame.spec`. */
18
+ export const CUSTOM_FRAME_STYLE = "custom";
19
+
20
+ export const FRAME_SPEC_LIMITS = {
21
+ /** Artboard sides, in spec units */
22
+ minArtboard: 50,
23
+ maxArtboard: 2000,
24
+ /** Largest absolute coordinate anywhere in a spec */
25
+ maxCoordinate: 4000,
26
+ /** Decimals a number may have */
27
+ maxDecimals: 3,
28
+ maxShapes: 64,
29
+ maxLabels: 8,
30
+ maxPolygonPoints: 256,
31
+ /** Characters in one path's data */
32
+ maxPathLength: 16_000,
33
+ /** Segments across the spec: path commands (arcs count 4), polygon points, dashes */
34
+ maxSegments: 2000,
35
+ /** Gradient steps on one shape */
36
+ maxGradientSteps: 64,
37
+ /** Gradient work across the spec: steps × the shape's segments */
38
+ maxGradientWork: 16_000,
39
+ /** Size of the spec as JSON, in UTF-8 bytes */
40
+ maxBytes: 32_768,
41
+ /** The code must be at least this share of the artboard's shorter side */
42
+ minQrShare: 0.5,
43
+ /** Light padding around the code, as a share of its size */
44
+ minPadding: 0.04,
45
+ defaultPadding: 0.08,
46
+ /** Characters in a label */
47
+ maxTextLength: FRAME_TEXT_MAX_LENGTH,
48
+ } as const;
49
+
50
+ /** Built-in glyphs a spec can place with an `icon` shape (no images). */
51
+ export const FRAME_ICONS = [
52
+ "wifi",
53
+ "star",
54
+ "heart",
55
+ "phone",
56
+ "headphones",
57
+ "calendar",
58
+ "user",
59
+ "chat",
60
+ "bag",
61
+ "bell",
62
+ "cup",
63
+ "card",
64
+ "check",
65
+ "info",
66
+ "home",
67
+ "pin",
68
+ "share",
69
+ "download",
70
+ "clock",
71
+ "snowflake",
72
+ "pumpkin",
73
+ "tree",
74
+ "sparkle",
75
+ "gift",
76
+ ] as const;
77
+
78
+ /**
79
+ * Largest label font as a share of its row height, so ascenders and
80
+ * descenders stay inside the row (and so off the code's padding).
81
+ */
82
+ export const FRAME_SPEC_LABEL_MAX_FONT = 0.95;
83
+
84
+ /** Most segments any built-in icon has (counted against maxSegments). */
85
+ export const FRAME_ICON_MAX_SEGMENTS = 200;
86
+
87
+ /**
88
+ * Theme colors a spec can bind to: `frame` is `frame.color`, `text` is
89
+ * `frame.textColor`, `accent` is `frame.secondaryColor` (the gradient end)
90
+ * and `background` is the code's background color.
91
+ */
92
+ export const FRAME_COLOR_TOKENS = [
93
+ "frame",
94
+ "text",
95
+ "accent",
96
+ "background",
97
+ ] as const;
98
+
99
+ // ── Text ───────────────────────────────────────────────────────────────────
100
+
101
+ // Controls, invisible and direction-changing characters, and lone UTF-16
102
+ // surrogates (which can't be encoded into a data URI).
103
+ const LINE_BREAKS = /[\t\n\v\f\r\u0085\u2028\u2029]/g;
104
+ const INVISIBLE =
105
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: stripping them is the point
106
+ // biome-ignore lint/suspicious/noMisleadingCharacterClass: each code point in the class is stripped on its own
107
+ /[\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;
108
+
109
+ /**
110
+ * Label text as a frame may draw it: NFC, no control, invisible or
111
+ * bidirectional-override characters, single spaces, trimmed and at most
112
+ * `maxLength` characters (code points).
113
+ */
114
+ export function sanitizeFrameText(
115
+ text: string,
116
+ maxLength: number = FRAME_TEXT_MAX_LENGTH
117
+ ): string {
118
+ const clean = text
119
+ .slice(0, 1000)
120
+ .normalize("NFC")
121
+ .replace(LINE_BREAKS, " ")
122
+ .replace(INVISIBLE, "")
123
+ .replace(/\s+/g, " ")
124
+ .trim();
125
+ return Array.from(clean).slice(0, Math.max(0, maxLength)).join("").trim();
126
+ }
127
+
128
+ /** True when `text` uses only Latin-1 (what PDF and EPS labels can print). */
129
+ export function isLatin1Text(text: string): boolean {
130
+ for (const char of text) {
131
+ if ((char.codePointAt(0) ?? 0) > 0xff) {
132
+ return false;
133
+ }
134
+ }
135
+ return true;
136
+ }
137
+
138
+ /** True when `value` has at most FRAME_SPEC_LIMITS.maxDecimals decimals. */
139
+ export const hasPrecision = (value: number) =>
140
+ Math.abs(value * 1000 - Math.round(value * 1000)) < 1e-6;
141
+
142
+ // ── Cross-field checks ─────────────────────────────────────────────────────
143
+
144
+ interface Box {
145
+ x0: number;
146
+ y0: number;
147
+ x1: number;
148
+ y1: number;
149
+ }
150
+
151
+ /** The code plus its padding: nothing may be drawn over it. */
152
+ export function frameSpecQrZone(spec: FrameSpec): Box {
153
+ const padding = frameSpecPadding(spec);
154
+ return {
155
+ x0: spec.qr.x - padding,
156
+ y0: spec.qr.y - padding,
157
+ x1: spec.qr.x + spec.qr.size + padding,
158
+ y1: spec.qr.y + spec.qr.size + padding,
159
+ };
160
+ }
161
+
162
+ /** The code itself, without its padding. */
163
+ function frameSpecQrBox(spec: FrameSpec): Box {
164
+ return {
165
+ x0: spec.qr.x,
166
+ y0: spec.qr.y,
167
+ x1: spec.qr.x + spec.qr.size,
168
+ y1: spec.qr.y + spec.qr.size,
169
+ };
170
+ }
171
+
172
+ /** The plate's padding around the code, in spec units. */
173
+ export function frameSpecPadding(spec: FrameSpec): number {
174
+ return spec.qr.padding ?? spec.qr.size * FRAME_SPEC_LIMITS.defaultPadding;
175
+ }
176
+
177
+ /** Font size of a text label with no `fontSize`, as a share of its row. */
178
+ export const FRAME_SPEC_DEFAULT_FONT = 0.52;
179
+
180
+ /**
181
+ * Half the height a label's capitals fill, as a share of its font size: a
182
+ * text label's letters are centered on its row, so they fill the row's
183
+ * middle, this far above and below it.
184
+ */
185
+ export const FRAME_SPEC_CAP_HALF = 0.35;
186
+
187
+ /** The largest font a text label draws (at the largest fontScale). */
188
+ function textLabelMaxFont(
189
+ label: Extract<FrameSpecLabel, { type: "text" }>
190
+ ): number {
191
+ const base = label.fontSize ?? label.box.height * FRAME_SPEC_DEFAULT_FONT;
192
+ return Math.min(
193
+ base * FRAME_FONT_SCALE_MAX,
194
+ label.box.height * FRAME_SPEC_LABEL_MAX_FONT
195
+ );
196
+ }
197
+
198
+ const overlaps = (a: Box, b: Box) =>
199
+ Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0) > 1e-6 &&
200
+ Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0) > 1e-6;
201
+
202
+ // ── The plate ─────────────────────────────────────────────────────────────
203
+
204
+ type Point = [number, number];
205
+
206
+ /** Points along the plate's outline (its corners rounded by `qr.radius`). */
207
+ function plateOutline(spec: FrameSpec): Point[] {
208
+ const zone = frameSpecQrZone(spec);
209
+ const r = Math.min(
210
+ spec.qr.radius ?? 0,
211
+ (zone.x1 - zone.x0) / 2,
212
+ (zone.y1 - zone.y0) / 2
213
+ );
214
+ const corners: [number, number, number][] = [
215
+ [zone.x0 + r, zone.y0 + r, Math.PI],
216
+ [zone.x1 - r, zone.y0 + r, -Math.PI / 2],
217
+ [zone.x1 - r, zone.y1 - r, 0],
218
+ [zone.x0 + r, zone.y1 - r, Math.PI / 2],
219
+ ];
220
+ const points: Point[] = [];
221
+ for (const [cx, cy, start] of corners) {
222
+ for (let i = 0; i <= 8; i++) {
223
+ const angle = start + (Math.PI / 2) * (i / 8);
224
+ points.push([cx + r * Math.cos(angle), cy + r * Math.sin(angle)]);
225
+ }
226
+ }
227
+ return points;
228
+ }
229
+
230
+ function insideRoundedBox(
231
+ [x, y]: Point,
232
+ box: { x: number; y: number; width: number; height: number },
233
+ radius: number
234
+ ): boolean {
235
+ const tolerance = 0.01;
236
+ if (
237
+ x < box.x - tolerance ||
238
+ y < box.y - tolerance ||
239
+ x > box.x + box.width + tolerance ||
240
+ y > box.y + box.height + tolerance
241
+ ) {
242
+ return false;
243
+ }
244
+ const r = Math.min(radius, box.width / 2, box.height / 2);
245
+ const cx = Math.min(Math.max(x, box.x + r), box.x + box.width - r);
246
+ const cy = Math.min(Math.max(y, box.y + r), box.y + box.height - r);
247
+ return Math.hypot(x - cx, y - cy) <= r + tolerance;
248
+ }
249
+
250
+ /** Area of a simple polygon (shoelace). */
251
+ function polygonArea(points: Point[]): number {
252
+ let twice = 0;
253
+ points.forEach(([x0, y0], i) => {
254
+ const [x1, y1] = points[(i + 1) % points.length] as Point;
255
+ twice += x0 * y1 - x1 * y0;
256
+ });
257
+ return Math.abs(twice) / 2;
258
+ }
259
+
260
+ /**
261
+ * True when `shape` may paint under the plate: anything but a `background`
262
+ * fill, a ring whose hole holds the whole plate, or a polygon that stays
263
+ * off it. Paths, lines, ellipses and icons count when their bounds reach
264
+ * the plate.
265
+ */
266
+ function paintsUnderPlate(
267
+ shape: FrameSpecShape,
268
+ spec: FrameSpec,
269
+ bounds: Box
270
+ ): boolean {
271
+ const zone = frameSpecQrZone(spec);
272
+ if (!overlaps(bounds, zone)) {
273
+ return false;
274
+ }
275
+ if ("stroke" in shape && shape.stroke) {
276
+ return true;
277
+ }
278
+ if (shape.fill === "background") {
279
+ return false;
280
+ }
281
+ if (shape.type === "rect" && shape.hole) {
282
+ const { hole } = shape;
283
+ return !plateOutline(spec).every((point) =>
284
+ insideRoundedBox(point, hole, hole.radius ?? 0)
285
+ );
286
+ }
287
+ if (shape.type === "circle" && shape.hole) {
288
+ const hole = shape.hole;
289
+ return !plateOutline(spec).every(
290
+ ([x, y]) => Math.hypot(x - shape.cx, y - shape.cy) <= hole + 0.01
291
+ );
292
+ }
293
+ if (shape.type === "polygon") {
294
+ const clipped = clipPolygon(shape.points as Point[], zone);
295
+ return clipped.length > 2 && polygonArea(clipped) > 1e-6;
296
+ }
297
+ return true;
298
+ }
299
+
300
+ /**
301
+ * Bounds of a spec shape (null for path data outside the grammar) and the
302
+ * segments it counts against FRAME_SPEC_LIMITS.maxSegments.
303
+ */
304
+ export function frameSpecShapeBounds(shape: FrameSpecShape): {
305
+ bounds: Box | null;
306
+ segments: number;
307
+ } {
308
+ const outline = outlineBounds(shape);
309
+ const stroke = "stroke" in shape ? shape.stroke : undefined;
310
+ if (!(stroke && outline.bounds)) {
311
+ return outline;
312
+ }
313
+ // Half the width outside a curve; a mitered corner reaches further (up to
314
+ // SVG's default miter limit of 4 half widths)
315
+ const reach =
316
+ shape.type === "circle" || shape.type === "ellipse"
317
+ ? stroke.width / 2
318
+ : shape.type === "rect"
319
+ ? (stroke.width * Math.SQRT2) / 2
320
+ : stroke.width * 2;
321
+ const { x0, y0, x1, y1 } = outline.bounds;
322
+ return {
323
+ bounds: { x0: x0 - reach, y0: y0 - reach, x1: x1 + reach, y1: y1 + reach },
324
+ // The outline is drawn a second time, as the stroke
325
+ segments: outline.segments * 2,
326
+ };
327
+ }
328
+
329
+ function outlineBounds(shape: FrameSpecShape): {
330
+ bounds: Box | null;
331
+ segments: number;
332
+ } {
333
+ switch (shape.type) {
334
+ case "rect":
335
+ return {
336
+ bounds: {
337
+ x0: shape.x,
338
+ y0: shape.y,
339
+ x1: shape.x + shape.width,
340
+ y1: shape.y + shape.height,
341
+ },
342
+ segments: shape.hole ? 18 : 9,
343
+ };
344
+ case "circle":
345
+ return {
346
+ bounds: {
347
+ x0: shape.cx - shape.r,
348
+ y0: shape.cy - shape.r,
349
+ x1: shape.cx + shape.r,
350
+ y1: shape.cy + shape.r,
351
+ },
352
+ segments: shape.hole ? 10 : 5,
353
+ };
354
+ case "ellipse":
355
+ return {
356
+ bounds: {
357
+ x0: shape.cx - shape.rx,
358
+ y0: shape.cy - shape.ry,
359
+ x1: shape.cx + shape.rx,
360
+ y1: shape.cy + shape.ry,
361
+ },
362
+ segments: 5,
363
+ };
364
+ case "polygon": {
365
+ const xs = shape.points.map(([x]) => x);
366
+ const ys = shape.points.map(([, y]) => y);
367
+ return {
368
+ bounds: {
369
+ x0: Math.min(...xs),
370
+ y0: Math.min(...ys),
371
+ x1: Math.max(...xs),
372
+ y1: Math.max(...ys),
373
+ },
374
+ segments: shape.points.length,
375
+ };
376
+ }
377
+ case "path": {
378
+ const commands = tokenizePathData(shape.d, FRAME_SPEC_LIMITS.maxDecimals);
379
+ if (!commands) {
380
+ return { bounds: null, segments: 0 };
381
+ }
382
+ const bounds = segmentsBounds(parsePathData(shape.d));
383
+ return { bounds, segments: countPathSegments(commands) };
384
+ }
385
+ case "line": {
386
+ const half = shape.width / 2;
387
+ const length = Math.hypot(shape.x2 - shape.x1, shape.y2 - shape.y1);
388
+ const dashes = shape.dash
389
+ ? Math.ceil(length / (shape.dash[0] + shape.dash[1]))
390
+ : 1;
391
+ return {
392
+ bounds: {
393
+ x0: Math.min(shape.x1, shape.x2) - half,
394
+ y0: Math.min(shape.y1, shape.y2) - half,
395
+ x1: Math.max(shape.x1, shape.x2) + half,
396
+ y1: Math.max(shape.y1, shape.y2) + half,
397
+ },
398
+ segments: dashes * (shape.cap === "round" ? 4 : 5),
399
+ };
400
+ }
401
+ default:
402
+ return {
403
+ bounds: {
404
+ x0: shape.x,
405
+ y0: shape.y,
406
+ x1: shape.x + shape.size,
407
+ y1: shape.y + shape.size,
408
+ },
409
+ // Upper bound for the built-in icons (tests/frame-spec.test.ts)
410
+ segments: FRAME_ICON_MAX_SEGMENTS,
411
+ };
412
+ }
413
+ }
414
+
415
+ /**
416
+ * The checks across fields that a frame spec's structure can't express:
417
+ * size, placement, overlaps and budgets. Reports each problem through
418
+ * `issue`; both frameSpecSchema (zod) and the engine's own parser
419
+ * (frame-spec-parse.ts) run it on a structurally valid spec.
420
+ */
421
+ export function checkFrameSpec(
422
+ spec: FrameSpec,
423
+ issue: (message: string, path?: (string | number)[]) => void
424
+ ): void {
425
+ const limits = FRAME_SPEC_LIMITS;
426
+
427
+ const bytes = new TextEncoder().encode(JSON.stringify(spec)).length;
428
+ if (bytes > limits.maxBytes) {
429
+ issue(`The spec is ${bytes} bytes; the limit is ${limits.maxBytes}`);
430
+ return;
431
+ }
432
+
433
+ const { width, height } = spec.artboard;
434
+ const tolerance = Math.max(width, height) * 0.005;
435
+ const artboard: Box = {
436
+ x0: -tolerance,
437
+ y0: -tolerance,
438
+ x1: width + tolerance,
439
+ y1: height + tolerance,
440
+ };
441
+ const inside = (box: Box) =>
442
+ box.x0 >= artboard.x0 &&
443
+ box.y0 >= artboard.y0 &&
444
+ box.x1 <= artboard.x1 &&
445
+ box.y1 <= artboard.y1;
446
+
447
+ // The code: big enough, inside the artboard with its padding
448
+ const { qr } = spec;
449
+ const padding = frameSpecPadding(spec);
450
+ if (qr.plate !== "none" && padding < qr.size * limits.minPadding - 1e-9) {
451
+ issue(
452
+ `qr.padding must be at least ${limits.minPadding * 100}% of qr.size`,
453
+ ["qr", "padding"]
454
+ );
455
+ }
456
+ if (qr.size < Math.min(width, height) * limits.minQrShare - 1e-9) {
457
+ issue(
458
+ `qr.size must be at least ${limits.minQrShare * 100}% of the artboard's shorter side`,
459
+ ["qr", "size"]
460
+ );
461
+ }
462
+ const zone = frameSpecQrZone(spec);
463
+ if (
464
+ zone.x0 < -1e-9 ||
465
+ zone.y0 < -1e-9 ||
466
+ zone.x1 > width + 1e-9 ||
467
+ zone.y1 > height + 1e-9
468
+ ) {
469
+ issue("The code and its padding must sit inside the artboard", ["qr"]);
470
+ }
471
+ if ((qr.radius ?? 0) > (qr.size + 2 * padding) / 2) {
472
+ issue("qr.radius is larger than half the plate", ["qr", "radius"]);
473
+ }
474
+
475
+ // Shapes: inside the artboard, within the segment budget
476
+ let segments = 0;
477
+ let gradientWork = 0;
478
+ spec.shapes.forEach((shape, index) => {
479
+ const path = ["shapes", index];
480
+ const { bounds, segments: count } = frameSpecShapeBounds(shape);
481
+ if (!bounds) {
482
+ issue(
483
+ "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",
484
+ [...path, "d"]
485
+ );
486
+ return;
487
+ }
488
+ if (!inside(bounds)) {
489
+ issue("The shape extends outside the artboard", path);
490
+ }
491
+ if (shape.type === "rect" && shape.hole) {
492
+ const { hole } = shape;
493
+ if (
494
+ hole.x < shape.x ||
495
+ hole.y < shape.y ||
496
+ hole.x + hole.width > shape.x + shape.width + 1e-9 ||
497
+ hole.y + hole.height > shape.y + shape.height + 1e-9
498
+ ) {
499
+ issue("The hole must sit inside its rect", [...path, "hole"]);
500
+ }
501
+ }
502
+ if (shape.type === "circle" && shape.hole && shape.hole >= shape.r) {
503
+ issue("The hole must be smaller than the circle", [...path, "hole"]);
504
+ }
505
+ if (
506
+ "stroke" in shape &&
507
+ shape.stroke &&
508
+ shape.stroke.width > Math.min(width, height) * 0.1 + 1e-9
509
+ ) {
510
+ issue("A stroke may be at most 10% of the artboard's shorter side", [
511
+ ...path,
512
+ "stroke",
513
+ "width",
514
+ ]);
515
+ }
516
+ if (qr.plate === "background" && paintsUnderPlate(shape, spec, bounds)) {
517
+ issue(
518
+ 'With qr.plate "background", only background fills (or a ring whose hole holds the plate) may lie under the plate',
519
+ path
520
+ );
521
+ }
522
+ segments += count;
523
+ if (typeof shape.fill === "object" && "gradient" in shape.fill) {
524
+ gradientWork += (shape.fill.gradient.steps ?? 32) * count;
525
+ }
526
+ });
527
+ if (segments > limits.maxSegments) {
528
+ issue(
529
+ `The shapes have ${segments} segments; the limit is ${limits.maxSegments}`,
530
+ ["shapes"]
531
+ );
532
+ }
533
+ if (gradientWork > limits.maxGradientWork) {
534
+ issue("Gradient fills are too detailed (fewer steps or simpler shapes)", [
535
+ "shapes",
536
+ ]);
537
+ }
538
+
539
+ // Labels: clean text, inside the artboard, never over the code
540
+ const bands: { y: number; height: number }[] = [];
541
+ const ids = new Set<string>();
542
+ const slots = new Set<string>();
543
+ spec.labels.forEach((label, index) => {
544
+ const path = ["labels", index];
545
+ if (ids.has(label.id)) {
546
+ issue(`Label id "${label.id}" is used twice`, [...path, "id"]);
547
+ }
548
+ ids.add(label.id);
549
+ if (label.slot !== "fixed") {
550
+ if (slots.has(label.slot)) {
551
+ issue(`Only one label may use the ${label.slot} slot`, [
552
+ ...path,
553
+ "slot",
554
+ ]);
555
+ }
556
+ slots.add(label.slot);
557
+ }
558
+ const maxLength = label.maxLength ?? limits.maxTextLength;
559
+ if (label.text !== sanitizeFrameText(label.text, maxLength)) {
560
+ issue(
561
+ `Label text must be at most ${maxLength} characters, trimmed, with no control or invisible characters (see sanitizeFrameText)`,
562
+ [...path, "text"]
563
+ );
564
+ }
565
+ let box: Box;
566
+ if (label.type === "text") {
567
+ box = {
568
+ x0: label.box.x,
569
+ y0: label.box.y,
570
+ x1: label.box.x + label.box.width,
571
+ y1: label.box.y + label.box.height,
572
+ };
573
+ if (
574
+ label.fontSize !== undefined &&
575
+ label.fontSize > label.box.height * FRAME_SPEC_LABEL_MAX_FONT + 1e-9
576
+ ) {
577
+ issue("fontSize may be at most 95% of the label's row height", [
578
+ ...path,
579
+ "fontSize",
580
+ ]);
581
+ }
582
+ // The row may reach into the padding, but never over the code, and
583
+ // the band its capitals fill (at the largest font) stays off both
584
+ const middle = label.box.y + label.box.height / 2;
585
+ const reach = textLabelMaxFont(label) * FRAME_SPEC_CAP_HALF;
586
+ const letters: Box = {
587
+ x0: box.x0,
588
+ x1: box.x1,
589
+ y0: middle - reach,
590
+ y1: middle + reach,
591
+ };
592
+ if (overlaps(box, frameSpecQrBox(spec)) || overlaps(letters, zone)) {
593
+ issue("The label overlaps the code or its padding", [...path, "box"]);
594
+ }
595
+ } else {
596
+ const { cx, cy, radius } = label.curve;
597
+ const reach = label.fontSize * FRAME_FONT_SCALE_MAX * 0.6;
598
+ box = {
599
+ x0: cx - radius - reach,
600
+ y0: cy - radius - reach,
601
+ x1: cx + radius + reach,
602
+ y1: cy + radius + reach,
603
+ };
604
+ const nearest = Math.hypot(
605
+ Math.max(zone.x0 - cx, 0, cx - zone.x1),
606
+ Math.max(zone.y0 - cy, 0, cy - zone.y1)
607
+ );
608
+ const farthest = Math.hypot(
609
+ Math.max(Math.abs(zone.x0 - cx), Math.abs(zone.x1 - cx)),
610
+ Math.max(Math.abs(zone.y0 - cy), Math.abs(zone.y1 - cy))
611
+ );
612
+ if (!(farthest <= radius - reach || nearest >= radius + reach)) {
613
+ issue("The curved label crosses the code or its padding", [
614
+ ...path,
615
+ "curve",
616
+ ]);
617
+ }
618
+ }
619
+ if (!inside(box)) {
620
+ issue("The label extends outside the artboard", path);
621
+ }
622
+ if (label.collapse) {
623
+ const band = label.collapse;
624
+ const end = band.y + band.height;
625
+ if (band.y < -1e-9 || end > height + 1e-9) {
626
+ issue("The collapse band must sit inside the artboard", [
627
+ ...path,
628
+ "collapse",
629
+ ]);
630
+ }
631
+ if (end > zone.y0 + 1e-6 && band.y < zone.y1 - 1e-6) {
632
+ issue("The collapse band crosses the code or its padding", [
633
+ ...path,
634
+ "collapse",
635
+ ]);
636
+ }
637
+ for (const other of bands) {
638
+ if (end > other.y + 1e-6 && band.y < other.y + other.height - 1e-6) {
639
+ issue("Collapse bands may not overlap", [...path, "collapse"]);
640
+ }
641
+ }
642
+ bands.push(band);
643
+ }
644
+ });
645
+ }