qrdx 0.0.46 → 0.0.47
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/LICENSE +21 -0
- package/README.md +353 -300
- package/dist/index.js +27 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -7
- package/dist/index.mjs.map +1 -1
- package/dist/qr-code-canvas.js +12 -0
- package/dist/qr-code-canvas.js.map +1 -0
- package/dist/qr-code-canvas.mjs +12 -0
- package/dist/qr-code-canvas.mjs.map +1 -0
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/art-trace.d.ts +46 -0
- package/dist/src/art-trace.d.ts.map +1 -0
- package/dist/src/art.d.ts +138 -0
- package/dist/src/art.d.ts.map +1 -0
- package/dist/src/body-patterns.d.ts +34 -0
- package/dist/src/body-patterns.d.ts.map +1 -0
- package/dist/src/codegen.d.ts +5 -5
- package/dist/src/codegen.d.ts.map +1 -1
- package/dist/src/codeword-map.d.ts +40 -0
- package/dist/src/codeword-map.d.ts.map +1 -0
- package/dist/src/constants.d.ts +4 -7
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts +3 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts +5 -2
- package/dist/src/corner-eye.d.ts.map +1 -1
- package/dist/src/detection.d.ts.map +1 -1
- package/dist/src/download.d.ts +22 -5
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/eps.d.ts +62 -0
- package/dist/src/eps.d.ts.map +1 -0
- package/dist/src/frame-icons.d.ts +11 -0
- package/dist/src/frame-icons.d.ts.map +1 -0
- package/dist/src/frame-import.d.ts +61 -0
- package/dist/src/frame-import.d.ts.map +1 -0
- package/dist/src/frame-spec.d.ts +24 -0
- package/dist/src/frame-spec.d.ts.map +1 -0
- package/dist/src/frame-styles.d.ts +50 -0
- package/dist/src/frame-styles.d.ts.map +1 -0
- package/dist/src/frame.d.ts +126 -0
- package/dist/src/frame.d.ts.map +1 -0
- package/dist/src/frames/builtin.d.ts +8 -0
- package/dist/src/frames/builtin.d.ts.map +1 -0
- package/dist/src/frames/official.d.ts +18 -0
- package/dist/src/frames/official.d.ts.map +1 -0
- package/dist/src/helpers.d.ts +15 -2
- package/dist/src/helpers.d.ts.map +1 -1
- package/dist/src/index.d.ts +24 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/logo-budget.d.ts +54 -0
- package/dist/src/logo-budget.d.ts.map +1 -0
- package/dist/src/logo-cleanup.d.ts +25 -0
- package/dist/src/logo-cleanup.d.ts.map +1 -0
- package/dist/src/logo.d.ts +28 -0
- package/dist/src/logo.d.ts.map +1 -0
- package/dist/src/print.d.ts +73 -0
- package/dist/src/print.d.ts.map +1 -0
- package/dist/src/qr-code-canvas.d.ts +13 -0
- package/dist/src/qr-code-canvas.d.ts.map +1 -0
- package/dist/src/qr-code.d.ts +4 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/qr-outline.d.ts +41 -0
- package/dist/src/qr-outline.d.ts.map +1 -0
- package/dist/src/qr-shape.d.ts +72 -0
- package/dist/src/qr-shape.d.ts.map +1 -0
- package/dist/src/scan-risk.d.ts +40 -0
- package/dist/src/scan-risk.d.ts.map +1 -0
- package/dist/src/scan-score.d.ts +154 -0
- package/dist/src/scan-score.d.ts.map +1 -0
- package/dist/src/shapes.d.ts +36 -0
- package/dist/src/shapes.d.ts.map +1 -0
- package/dist/src/silhouette.d.ts +28 -0
- package/dist/src/silhouette.d.ts.map +1 -0
- package/dist/src/utils/color.d.ts.map +1 -1
- package/dist/src/utils/fmt.d.ts +24 -0
- package/dist/src/utils/fmt.d.ts.map +1 -0
- package/dist/src/utils/paint.d.ts +80 -0
- package/dist/src/utils/paint.d.ts.map +1 -0
- package/dist/src/utils/parse-color.d.ts +21 -0
- package/dist/src/utils/parse-color.d.ts.map +1 -0
- package/dist/src/utils/path-data.d.ts +24 -0
- package/dist/src/utils/path-data.d.ts.map +1 -0
- package/dist/src/utils/png.d.ts +16 -0
- package/dist/src/utils/png.d.ts.map +1 -0
- package/dist/src/utils/polygon-clip.d.ts +26 -0
- package/dist/src/utils/polygon-clip.d.ts.map +1 -0
- package/dist/src/utils/print-shared.d.ts +20 -0
- package/dist/src/utils/print-shared.d.ts.map +1 -0
- package/dist/src/utils/svg-geometry.d.ts +49 -0
- package/dist/src/utils/svg-geometry.d.ts.map +1 -0
- package/dist/src/utils/svg-gradient.d.ts +59 -0
- package/dist/src/utils/svg-gradient.d.ts.map +1 -0
- package/dist/src/utils/svg-painter.d.ts +152 -0
- package/dist/src/utils/svg-painter.d.ts.map +1 -0
- package/dist/src/utils.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/types/art.d.ts +64 -0
- package/dist/types/art.d.ts.map +1 -0
- package/dist/types/color.d.ts +125 -7
- package/dist/types/color.d.ts.map +1 -1
- package/dist/types/corner-dot.d.ts +52 -2
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +43 -2
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/frame-spec.d.ts +3509 -0
- package/dist/types/frame-spec.d.ts.map +1 -0
- package/dist/types/frame.d.ts +68 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/image-pattern.d.ts +15 -1
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +6 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/qr.d.ts +1684 -4
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/template.d.ts +2 -2
- package/dist/types/template.d.ts.map +1 -1
- package/package.json +68 -25
- package/src/api.tsx +67 -17
- package/src/art-trace.ts +554 -0
- package/src/art.tsx +2226 -0
- package/src/body-patterns.tsx +389 -0
- package/src/codegen.ts +10 -5
- package/src/codeword-map.ts +265 -0
- package/src/constants.ts +4 -8
- package/src/corner-dot.tsx +209 -33
- package/src/corner-eye.ts +328 -60
- package/src/detection.ts +5 -11
- package/src/download.tsx +123 -52
- package/src/eps.ts +701 -0
- package/src/frame-icons.ts +325 -0
- package/src/frame-import.ts +1198 -0
- package/src/frame-spec.ts +518 -0
- package/src/frame-styles.ts +1095 -0
- package/src/frame.tsx +630 -0
- package/src/frames/builtin/banner-bottom.json +58 -0
- package/src/frames/builtin/circle.json +81 -0
- package/src/frames/builtin/rounded-border.json +60 -0
- package/src/frames/builtin/ticket.json +51 -0
- package/src/frames/builtin.ts +20 -0
- package/src/frames/official/attendance.json +110 -0
- package/src/frames/official/audio-guide.json +95 -0
- package/src/frames/official/book-now.json +103 -0
- package/src/frames/official/check-in.json +110 -0
- package/src/frames/official/christmas.json +131 -0
- package/src/frames/official/donate.json +77 -0
- package/src/frames/official/event-ticket.json +77 -0
- package/src/frames/official/feedback-bubble.json +73 -0
- package/src/frames/official/follow-us.json +83 -0
- package/src/frames/official/get-the-app.json +107 -0
- package/src/frames/official/halloween.json +80 -0
- package/src/frames/official/new-year.json +94 -0
- package/src/frames/official/order-at-table.json +86 -0
- package/src/frames/official/product-info.json +119 -0
- package/src/frames/official/register-product.json +77 -0
- package/src/frames/official/review-us.json +127 -0
- package/src/frames/official/rsvp-card.json +112 -0
- package/src/frames/official/save-contact.json +117 -0
- package/src/frames/official/scan-to-pay.json +94 -0
- package/src/frames/official/shop-window.json +81 -0
- package/src/frames/official/sign-rider.json +81 -0
- package/src/frames/official/table-tent.json +75 -0
- package/src/frames/official/wifi-card.json +89 -0
- package/src/frames/official/winter.json +142 -0
- package/src/frames/official.ts +77 -0
- package/src/helpers.ts +168 -11
- package/src/index.tsx +141 -172
- package/src/logo-budget.ts +226 -0
- package/src/logo-cleanup.ts +146 -0
- package/src/logo.tsx +137 -0
- package/src/print.ts +794 -0
- package/src/qr-code-canvas.tsx +241 -0
- package/src/qr-code.tsx +51 -16
- package/src/qr-outline.ts +511 -0
- package/src/qr-shape.ts +255 -0
- package/src/scan-risk.ts +197 -0
- package/src/scan-score.ts +1629 -0
- package/src/shapes.ts +169 -0
- package/src/silhouette.ts +334 -0
- package/src/utils/color.ts +10 -1
- package/src/utils/fmt.ts +59 -0
- package/src/utils/paint.ts +317 -0
- package/src/utils/parse-color.ts +135 -0
- package/src/utils/path-data.ts +182 -0
- package/src/utils/png.ts +175 -0
- package/src/utils/polygon-clip.ts +163 -0
- package/src/utils/print-shared.ts +74 -0
- package/src/utils/svg-geometry.ts +838 -0
- package/src/utils/svg-gradient.ts +407 -0
- package/src/utils/svg-painter.ts +946 -0
- package/src/utils.tsx +794 -385
- package/types/art.ts +74 -0
- package/types/color.ts +132 -12
- package/types/corner-dot.ts +41 -4
- package/types/corner-eye.ts +31 -4
- package/types/frame-spec.ts +748 -0
- package/types/frame.ts +67 -0
- package/types/image-pattern.ts +15 -0
- package/types/index.ts +106 -4
- package/types/qr.ts +180 -2
- package/types/template.ts +2 -2
- package/dist/cli.js +0 -4319
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: spec limits */
|
|
2
|
+
// A frame as data: the shapes, labels, colors and QR placement the engine
|
|
3
|
+
// draws. It renders identically in SVG, canvas, PDF and EPS because it is
|
|
4
|
+
// compiled to the same plain shapes the built-in frames use. There is no
|
|
5
|
+
// markup, no URL and no image in it, so a spec from anyone is safe to store,
|
|
6
|
+
// share and render. Every field is validated with hard limits (see
|
|
7
|
+
// FRAME_SPEC_LIMITS); unknown keys are rejected.
|
|
8
|
+
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { countPathSegments, tokenizePathData } from "../src/utils/path-data";
|
|
11
|
+
import { segmentsBounds } from "../src/utils/polygon-clip";
|
|
12
|
+
import { parsePathData } from "../src/utils/svg-geometry";
|
|
13
|
+
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
|
+
}
|
|
142
|
+
|
|
143
|
+
// ── Schemas ────────────────────────────────────────────────────────────────
|
|
144
|
+
|
|
145
|
+
const hasPrecision = (value: number) =>
|
|
146
|
+
Math.abs(value * 1000 - Math.round(value * 1000)) < 1e-6;
|
|
147
|
+
|
|
148
|
+
const number = z
|
|
149
|
+
.number()
|
|
150
|
+
.min(-FRAME_SPEC_LIMITS.maxCoordinate)
|
|
151
|
+
.max(FRAME_SPEC_LIMITS.maxCoordinate)
|
|
152
|
+
.refine(hasPrecision, {
|
|
153
|
+
message: `Numbers may have at most ${FRAME_SPEC_LIMITS.maxDecimals} decimals`,
|
|
154
|
+
});
|
|
155
|
+
const positive = number.refine((value) => value > 0, {
|
|
156
|
+
message: "Must be greater than 0",
|
|
157
|
+
});
|
|
158
|
+
const nonNegative = number.refine((value) => value >= 0, {
|
|
159
|
+
message: "Must not be negative",
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const hexColorSchema = z
|
|
163
|
+
.string()
|
|
164
|
+
.regex(/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/, "Use a #rgb or #rrggbb color");
|
|
165
|
+
|
|
166
|
+
const baseColorSchema = z.union([z.enum(FRAME_COLOR_TOKENS), hexColorSchema]);
|
|
167
|
+
|
|
168
|
+
/** A theme token, a fixed hex color, or an opaque mix of two of them. */
|
|
169
|
+
export const frameSpecColorSchema = z.union([
|
|
170
|
+
baseColorSchema,
|
|
171
|
+
z
|
|
172
|
+
.object({
|
|
173
|
+
mix: z.tuple([baseColorSchema, baseColorSchema]),
|
|
174
|
+
amount: z.number().min(0).max(1).refine(hasPrecision),
|
|
175
|
+
})
|
|
176
|
+
.strict(),
|
|
177
|
+
]);
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* A solid color, or a gradient drawn as solid color steps (so EPS, which
|
|
181
|
+
* has no transparency or blending, draws it the same as SVG).
|
|
182
|
+
*/
|
|
183
|
+
export const frameSpecPaintSchema = z.union([
|
|
184
|
+
frameSpecColorSchema,
|
|
185
|
+
z
|
|
186
|
+
.object({
|
|
187
|
+
gradient: z
|
|
188
|
+
.object({
|
|
189
|
+
from: frameSpecColorSchema,
|
|
190
|
+
to: frameSpecColorSchema,
|
|
191
|
+
direction: z.enum(["horizontal", "vertical"]).optional(),
|
|
192
|
+
steps: z
|
|
193
|
+
.int()
|
|
194
|
+
.min(2)
|
|
195
|
+
.max(FRAME_SPEC_LIMITS.maxGradientSteps)
|
|
196
|
+
.optional(),
|
|
197
|
+
})
|
|
198
|
+
.strict(),
|
|
199
|
+
})
|
|
200
|
+
.strict(),
|
|
201
|
+
]);
|
|
202
|
+
|
|
203
|
+
const boxFields = {
|
|
204
|
+
x: number,
|
|
205
|
+
y: number,
|
|
206
|
+
width: positive,
|
|
207
|
+
height: positive,
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const rectShapeSchema = z
|
|
211
|
+
.object({
|
|
212
|
+
type: z.literal("rect"),
|
|
213
|
+
...boxFields,
|
|
214
|
+
radius: nonNegative.optional(),
|
|
215
|
+
fill: frameSpecPaintSchema,
|
|
216
|
+
/** Area cut out of the rect */
|
|
217
|
+
hole: z
|
|
218
|
+
.object({ ...boxFields, radius: nonNegative.optional() })
|
|
219
|
+
.strict()
|
|
220
|
+
.optional(),
|
|
221
|
+
})
|
|
222
|
+
.strict();
|
|
223
|
+
|
|
224
|
+
const circleShapeSchema = z
|
|
225
|
+
.object({
|
|
226
|
+
type: z.literal("circle"),
|
|
227
|
+
cx: number,
|
|
228
|
+
cy: number,
|
|
229
|
+
r: positive,
|
|
230
|
+
/** Radius of a hole in the middle (a ring) */
|
|
231
|
+
hole: positive.optional(),
|
|
232
|
+
fill: frameSpecPaintSchema,
|
|
233
|
+
})
|
|
234
|
+
.strict();
|
|
235
|
+
|
|
236
|
+
const ellipseShapeSchema = z
|
|
237
|
+
.object({
|
|
238
|
+
type: z.literal("ellipse"),
|
|
239
|
+
cx: number,
|
|
240
|
+
cy: number,
|
|
241
|
+
rx: positive,
|
|
242
|
+
ry: positive,
|
|
243
|
+
fill: frameSpecPaintSchema,
|
|
244
|
+
})
|
|
245
|
+
.strict();
|
|
246
|
+
|
|
247
|
+
const polygonShapeSchema = z
|
|
248
|
+
.object({
|
|
249
|
+
type: z.literal("polygon"),
|
|
250
|
+
points: z
|
|
251
|
+
.array(z.tuple([number, number]))
|
|
252
|
+
.min(3)
|
|
253
|
+
.max(FRAME_SPEC_LIMITS.maxPolygonPoints),
|
|
254
|
+
fill: frameSpecPaintSchema,
|
|
255
|
+
})
|
|
256
|
+
.strict();
|
|
257
|
+
|
|
258
|
+
const pathShapeSchema = z
|
|
259
|
+
.object({
|
|
260
|
+
type: z.literal("path"),
|
|
261
|
+
/** SVG path data: M L H V C S Q T A Z, plain numbers, no exponents */
|
|
262
|
+
d: z.string().min(2).max(FRAME_SPEC_LIMITS.maxPathLength),
|
|
263
|
+
fill: frameSpecPaintSchema,
|
|
264
|
+
fillRule: z.enum(["nonzero", "evenodd"]).optional(),
|
|
265
|
+
})
|
|
266
|
+
.strict();
|
|
267
|
+
|
|
268
|
+
const lineShapeSchema = z
|
|
269
|
+
.object({
|
|
270
|
+
type: z.literal("line"),
|
|
271
|
+
x1: number,
|
|
272
|
+
y1: number,
|
|
273
|
+
x2: number,
|
|
274
|
+
y2: number,
|
|
275
|
+
/** Thickness (lines are filled outlines, never strokes) */
|
|
276
|
+
width: positive,
|
|
277
|
+
cap: z.enum(["butt", "round"]).optional(),
|
|
278
|
+
/** Dash and gap lengths, for perforations */
|
|
279
|
+
dash: z.tuple([positive, positive]).optional(),
|
|
280
|
+
fill: frameSpecPaintSchema,
|
|
281
|
+
})
|
|
282
|
+
.strict();
|
|
283
|
+
|
|
284
|
+
const iconShapeSchema = z
|
|
285
|
+
.object({
|
|
286
|
+
type: z.literal("icon"),
|
|
287
|
+
icon: z.enum(FRAME_ICONS),
|
|
288
|
+
/** Top-left corner of the icon's square */
|
|
289
|
+
x: number,
|
|
290
|
+
y: number,
|
|
291
|
+
size: positive,
|
|
292
|
+
fill: frameSpecPaintSchema,
|
|
293
|
+
})
|
|
294
|
+
.strict();
|
|
295
|
+
|
|
296
|
+
export const frameSpecShapeSchema = z.discriminatedUnion("type", [
|
|
297
|
+
rectShapeSchema,
|
|
298
|
+
circleShapeSchema,
|
|
299
|
+
ellipseShapeSchema,
|
|
300
|
+
polygonShapeSchema,
|
|
301
|
+
pathShapeSchema,
|
|
302
|
+
lineShapeSchema,
|
|
303
|
+
iconShapeSchema,
|
|
304
|
+
]);
|
|
305
|
+
|
|
306
|
+
const labelFields = {
|
|
307
|
+
/** Stable id, for editors: a-z, 0-9 and "-" */
|
|
308
|
+
id: z.string().regex(/^[a-z][a-z0-9-]{0,31}$/),
|
|
309
|
+
/**
|
|
310
|
+
* `primary` shows `frame.text`, `secondary` shows `frame.secondaryText`
|
|
311
|
+
* (each falls back to `text`; "" hides the label), `fixed` always shows
|
|
312
|
+
* `text`.
|
|
313
|
+
*/
|
|
314
|
+
slot: z.enum(["primary", "secondary", "fixed"]),
|
|
315
|
+
/** Default text */
|
|
316
|
+
text: z.string().max(200),
|
|
317
|
+
/** Longest text the label takes (up to 24 characters) */
|
|
318
|
+
maxLength: z.int().min(1).max(FRAME_TEXT_MAX_LENGTH).optional(),
|
|
319
|
+
color: frameSpecColorSchema,
|
|
320
|
+
weight: z.enum(["bold", "regular"]).optional(),
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const textLabelSchema = z
|
|
324
|
+
.object({
|
|
325
|
+
type: z.literal("text"),
|
|
326
|
+
...labelFields,
|
|
327
|
+
/** Row the label is fitted in: centered vertically, shrunk to the width */
|
|
328
|
+
box: z.object(boxFields).strict(),
|
|
329
|
+
align: z.enum(["start", "middle", "end"]).optional(),
|
|
330
|
+
/** Font size at fontScale 1 (default: 52% of the row height) */
|
|
331
|
+
fontSize: positive.optional(),
|
|
332
|
+
})
|
|
333
|
+
.strict();
|
|
334
|
+
|
|
335
|
+
const curvedLabelSchema = z
|
|
336
|
+
.object({
|
|
337
|
+
type: z.literal("curved"),
|
|
338
|
+
...labelFields,
|
|
339
|
+
/** Letters set along a circle, over the top or along the bottom */
|
|
340
|
+
curve: z
|
|
341
|
+
.object({
|
|
342
|
+
cx: number,
|
|
343
|
+
cy: number,
|
|
344
|
+
radius: positive,
|
|
345
|
+
side: z.enum(["top", "bottom"]),
|
|
346
|
+
/** Most of the circle the text may use, in degrees (default 140) */
|
|
347
|
+
spread: z.number().min(20).max(300).refine(hasPrecision).optional(),
|
|
348
|
+
})
|
|
349
|
+
.strict(),
|
|
350
|
+
/** Font size at fontScale 1 (shrunk to fit the spread) */
|
|
351
|
+
fontSize: positive,
|
|
352
|
+
})
|
|
353
|
+
.strict();
|
|
354
|
+
|
|
355
|
+
export const frameSpecLabelSchema = z.discriminatedUnion("type", [
|
|
356
|
+
textLabelSchema,
|
|
357
|
+
curvedLabelSchema,
|
|
358
|
+
]);
|
|
359
|
+
|
|
360
|
+
const artboardSide = number.refine(
|
|
361
|
+
(value) =>
|
|
362
|
+
value >= FRAME_SPEC_LIMITS.minArtboard &&
|
|
363
|
+
value <= FRAME_SPEC_LIMITS.maxArtboard,
|
|
364
|
+
{
|
|
365
|
+
message: `Artboard sides must be ${FRAME_SPEC_LIMITS.minArtboard} to ${FRAME_SPEC_LIMITS.maxArtboard} units`,
|
|
366
|
+
}
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
const frameSpecObjectSchema = z
|
|
370
|
+
.object({
|
|
371
|
+
kind: z.literal(FRAME_SPEC_KIND),
|
|
372
|
+
version: z.literal(FRAME_SPEC_VERSION),
|
|
373
|
+
/** Size of the whole artwork, in spec units */
|
|
374
|
+
artboard: z.object({ width: artboardSide, height: artboardSide }).strict(),
|
|
375
|
+
/**
|
|
376
|
+
* Where the code goes (its own margin included). The engine paints an
|
|
377
|
+
* opaque plate of the code's background under it and `padding` around
|
|
378
|
+
* it, so no artwork ever shows in the quiet zone.
|
|
379
|
+
*/
|
|
380
|
+
qr: z
|
|
381
|
+
.object({
|
|
382
|
+
x: number,
|
|
383
|
+
y: number,
|
|
384
|
+
size: positive,
|
|
385
|
+
/** Light space around the code (default 8% of size, at least 4%) */
|
|
386
|
+
padding: nonNegative.optional(),
|
|
387
|
+
/** Corner radius of the plate */
|
|
388
|
+
radius: nonNegative.optional(),
|
|
389
|
+
})
|
|
390
|
+
.strict(),
|
|
391
|
+
/** Default colors, used when the theme sets none */
|
|
392
|
+
colors: z
|
|
393
|
+
.object({
|
|
394
|
+
frame: hexColorSchema,
|
|
395
|
+
text: hexColorSchema.optional(),
|
|
396
|
+
accent: hexColorSchema.optional(),
|
|
397
|
+
})
|
|
398
|
+
.strict(),
|
|
399
|
+
/** Painted behind the code, in order */
|
|
400
|
+
shapes: z.array(frameSpecShapeSchema).max(FRAME_SPEC_LIMITS.maxShapes),
|
|
401
|
+
labels: z.array(frameSpecLabelSchema).max(FRAME_SPEC_LIMITS.maxLabels),
|
|
402
|
+
/**
|
|
403
|
+
* Color print bleed continues with when the artwork fills its whole
|
|
404
|
+
* artboard edge to edge ("none" by default: bleed stays paper).
|
|
405
|
+
*/
|
|
406
|
+
bleed: z.union([z.literal("none"), frameSpecColorSchema]).optional(),
|
|
407
|
+
})
|
|
408
|
+
.strict();
|
|
409
|
+
|
|
410
|
+
export type FrameSpecColor = z.infer<typeof frameSpecColorSchema>;
|
|
411
|
+
export type FrameSpecPaint = z.infer<typeof frameSpecPaintSchema>;
|
|
412
|
+
export type FrameSpecShape = z.infer<typeof frameSpecShapeSchema>;
|
|
413
|
+
export type FrameSpecLabel = z.infer<typeof frameSpecLabelSchema>;
|
|
414
|
+
export type FrameSpecIcon = (typeof FRAME_ICONS)[number];
|
|
415
|
+
export type FrameSpec = z.infer<typeof frameSpecObjectSchema>;
|
|
416
|
+
|
|
417
|
+
// ── Cross-field checks ─────────────────────────────────────────────────────
|
|
418
|
+
|
|
419
|
+
interface Box {
|
|
420
|
+
x0: number;
|
|
421
|
+
y0: number;
|
|
422
|
+
x1: number;
|
|
423
|
+
y1: number;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** The code plus its padding: nothing may be drawn over it. */
|
|
427
|
+
export function frameSpecQrZone(spec: FrameSpec): Box {
|
|
428
|
+
const padding = frameSpecPadding(spec);
|
|
429
|
+
return {
|
|
430
|
+
x0: spec.qr.x - padding,
|
|
431
|
+
y0: spec.qr.y - padding,
|
|
432
|
+
x1: spec.qr.x + spec.qr.size + padding,
|
|
433
|
+
y1: spec.qr.y + spec.qr.size + padding,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** The plate's padding around the code, in spec units. */
|
|
438
|
+
export function frameSpecPadding(spec: FrameSpec): number {
|
|
439
|
+
return spec.qr.padding ?? spec.qr.size * FRAME_SPEC_LIMITS.defaultPadding;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const overlaps = (a: Box, b: Box) =>
|
|
443
|
+
Math.min(a.x1, b.x1) - Math.max(a.x0, b.x0) > 1e-6 &&
|
|
444
|
+
Math.min(a.y1, b.y1) - Math.max(a.y0, b.y0) > 1e-6;
|
|
445
|
+
|
|
446
|
+
function shapeBounds(shape: FrameSpecShape): {
|
|
447
|
+
bounds: Box | null;
|
|
448
|
+
segments: number;
|
|
449
|
+
} {
|
|
450
|
+
switch (shape.type) {
|
|
451
|
+
case "rect":
|
|
452
|
+
return {
|
|
453
|
+
bounds: {
|
|
454
|
+
x0: shape.x,
|
|
455
|
+
y0: shape.y,
|
|
456
|
+
x1: shape.x + shape.width,
|
|
457
|
+
y1: shape.y + shape.height,
|
|
458
|
+
},
|
|
459
|
+
segments: shape.hole ? 18 : 9,
|
|
460
|
+
};
|
|
461
|
+
case "circle":
|
|
462
|
+
return {
|
|
463
|
+
bounds: {
|
|
464
|
+
x0: shape.cx - shape.r,
|
|
465
|
+
y0: shape.cy - shape.r,
|
|
466
|
+
x1: shape.cx + shape.r,
|
|
467
|
+
y1: shape.cy + shape.r,
|
|
468
|
+
},
|
|
469
|
+
segments: shape.hole ? 10 : 5,
|
|
470
|
+
};
|
|
471
|
+
case "ellipse":
|
|
472
|
+
return {
|
|
473
|
+
bounds: {
|
|
474
|
+
x0: shape.cx - shape.rx,
|
|
475
|
+
y0: shape.cy - shape.ry,
|
|
476
|
+
x1: shape.cx + shape.rx,
|
|
477
|
+
y1: shape.cy + shape.ry,
|
|
478
|
+
},
|
|
479
|
+
segments: 5,
|
|
480
|
+
};
|
|
481
|
+
case "polygon": {
|
|
482
|
+
const xs = shape.points.map(([x]) => x);
|
|
483
|
+
const ys = shape.points.map(([, y]) => y);
|
|
484
|
+
return {
|
|
485
|
+
bounds: {
|
|
486
|
+
x0: Math.min(...xs),
|
|
487
|
+
y0: Math.min(...ys),
|
|
488
|
+
x1: Math.max(...xs),
|
|
489
|
+
y1: Math.max(...ys),
|
|
490
|
+
},
|
|
491
|
+
segments: shape.points.length,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
case "path": {
|
|
495
|
+
const commands = tokenizePathData(shape.d, FRAME_SPEC_LIMITS.maxDecimals);
|
|
496
|
+
if (!commands) {
|
|
497
|
+
return { bounds: null, segments: 0 };
|
|
498
|
+
}
|
|
499
|
+
const bounds = segmentsBounds(parsePathData(shape.d));
|
|
500
|
+
return { bounds, segments: countPathSegments(commands) };
|
|
501
|
+
}
|
|
502
|
+
case "line": {
|
|
503
|
+
const half = shape.width / 2;
|
|
504
|
+
const length = Math.hypot(shape.x2 - shape.x1, shape.y2 - shape.y1);
|
|
505
|
+
const dashes = shape.dash
|
|
506
|
+
? Math.ceil(length / (shape.dash[0] + shape.dash[1]))
|
|
507
|
+
: 1;
|
|
508
|
+
return {
|
|
509
|
+
bounds: {
|
|
510
|
+
x0: Math.min(shape.x1, shape.x2) - half,
|
|
511
|
+
y0: Math.min(shape.y1, shape.y2) - half,
|
|
512
|
+
x1: Math.max(shape.x1, shape.x2) + half,
|
|
513
|
+
y1: Math.max(shape.y1, shape.y2) + half,
|
|
514
|
+
},
|
|
515
|
+
segments: dashes * (shape.cap === "round" ? 4 : 5),
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
default:
|
|
519
|
+
return {
|
|
520
|
+
bounds: {
|
|
521
|
+
x0: shape.x,
|
|
522
|
+
y0: shape.y,
|
|
523
|
+
x1: shape.x + shape.size,
|
|
524
|
+
y1: shape.y + shape.size,
|
|
525
|
+
},
|
|
526
|
+
// Upper bound for the built-in icons (tests/frame-spec.test.ts)
|
|
527
|
+
segments: FRAME_ICON_MAX_SEGMENTS,
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function checkFrameSpec(spec: FrameSpec, ctx: z.RefinementCtx) {
|
|
533
|
+
const issue = (message: string, path: (string | number)[] = []) =>
|
|
534
|
+
ctx.addIssue({ code: "custom", message, path });
|
|
535
|
+
const limits = FRAME_SPEC_LIMITS;
|
|
536
|
+
|
|
537
|
+
const bytes = new TextEncoder().encode(JSON.stringify(spec)).length;
|
|
538
|
+
if (bytes > limits.maxBytes) {
|
|
539
|
+
issue(`The spec is ${bytes} bytes; the limit is ${limits.maxBytes}`);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const { width, height } = spec.artboard;
|
|
544
|
+
const tolerance = Math.max(width, height) * 0.005;
|
|
545
|
+
const artboard: Box = {
|
|
546
|
+
x0: -tolerance,
|
|
547
|
+
y0: -tolerance,
|
|
548
|
+
x1: width + tolerance,
|
|
549
|
+
y1: height + tolerance,
|
|
550
|
+
};
|
|
551
|
+
const inside = (box: Box) =>
|
|
552
|
+
box.x0 >= artboard.x0 &&
|
|
553
|
+
box.y0 >= artboard.y0 &&
|
|
554
|
+
box.x1 <= artboard.x1 &&
|
|
555
|
+
box.y1 <= artboard.y1;
|
|
556
|
+
|
|
557
|
+
// The code: big enough, inside the artboard with its padding
|
|
558
|
+
const { qr } = spec;
|
|
559
|
+
const padding = frameSpecPadding(spec);
|
|
560
|
+
if (padding < qr.size * limits.minPadding - 1e-9) {
|
|
561
|
+
issue(
|
|
562
|
+
`qr.padding must be at least ${limits.minPadding * 100}% of qr.size`,
|
|
563
|
+
["qr", "padding"]
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
if (qr.size < Math.min(width, height) * limits.minQrShare - 1e-9) {
|
|
567
|
+
issue(
|
|
568
|
+
`qr.size must be at least ${limits.minQrShare * 100}% of the artboard's shorter side`,
|
|
569
|
+
["qr", "size"]
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
const zone = frameSpecQrZone(spec);
|
|
573
|
+
if (
|
|
574
|
+
zone.x0 < -1e-9 ||
|
|
575
|
+
zone.y0 < -1e-9 ||
|
|
576
|
+
zone.x1 > width + 1e-9 ||
|
|
577
|
+
zone.y1 > height + 1e-9
|
|
578
|
+
) {
|
|
579
|
+
issue("The code and its padding must sit inside the artboard", ["qr"]);
|
|
580
|
+
}
|
|
581
|
+
if ((qr.radius ?? 0) > (qr.size + 2 * padding) / 2) {
|
|
582
|
+
issue("qr.radius is larger than half the plate", ["qr", "radius"]);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// Shapes: inside the artboard, within the segment budget
|
|
586
|
+
let segments = 0;
|
|
587
|
+
let gradientWork = 0;
|
|
588
|
+
spec.shapes.forEach((shape, index) => {
|
|
589
|
+
const path = ["shapes", index];
|
|
590
|
+
const { bounds, segments: count } = shapeBounds(shape);
|
|
591
|
+
if (!bounds) {
|
|
592
|
+
issue(
|
|
593
|
+
"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",
|
|
594
|
+
[...path, "d"]
|
|
595
|
+
);
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
if (!inside(bounds)) {
|
|
599
|
+
issue("The shape extends outside the artboard", path);
|
|
600
|
+
}
|
|
601
|
+
if (shape.type === "rect" && shape.hole) {
|
|
602
|
+
const { hole } = shape;
|
|
603
|
+
if (
|
|
604
|
+
hole.x < shape.x ||
|
|
605
|
+
hole.y < shape.y ||
|
|
606
|
+
hole.x + hole.width > shape.x + shape.width + 1e-9 ||
|
|
607
|
+
hole.y + hole.height > shape.y + shape.height + 1e-9
|
|
608
|
+
) {
|
|
609
|
+
issue("The hole must sit inside its rect", [...path, "hole"]);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (shape.type === "circle" && shape.hole && shape.hole >= shape.r) {
|
|
613
|
+
issue("The hole must be smaller than the circle", [...path, "hole"]);
|
|
614
|
+
}
|
|
615
|
+
segments += count;
|
|
616
|
+
if (typeof shape.fill === "object" && "gradient" in shape.fill) {
|
|
617
|
+
gradientWork += (shape.fill.gradient.steps ?? 32) * count;
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
if (segments > limits.maxSegments) {
|
|
621
|
+
issue(
|
|
622
|
+
`The shapes have ${segments} segments; the limit is ${limits.maxSegments}`,
|
|
623
|
+
["shapes"]
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
if (gradientWork > limits.maxGradientWork) {
|
|
627
|
+
issue("Gradient fills are too detailed (fewer steps or simpler shapes)", [
|
|
628
|
+
"shapes",
|
|
629
|
+
]);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Labels: clean text, inside the artboard, never over the code
|
|
633
|
+
const ids = new Set<string>();
|
|
634
|
+
const slots = new Set<string>();
|
|
635
|
+
spec.labels.forEach((label, index) => {
|
|
636
|
+
const path = ["labels", index];
|
|
637
|
+
if (ids.has(label.id)) {
|
|
638
|
+
issue(`Label id "${label.id}" is used twice`, [...path, "id"]);
|
|
639
|
+
}
|
|
640
|
+
ids.add(label.id);
|
|
641
|
+
if (label.slot !== "fixed") {
|
|
642
|
+
if (slots.has(label.slot)) {
|
|
643
|
+
issue(`Only one label may use the ${label.slot} slot`, [
|
|
644
|
+
...path,
|
|
645
|
+
"slot",
|
|
646
|
+
]);
|
|
647
|
+
}
|
|
648
|
+
slots.add(label.slot);
|
|
649
|
+
}
|
|
650
|
+
const maxLength = label.maxLength ?? limits.maxTextLength;
|
|
651
|
+
if (label.text !== sanitizeFrameText(label.text, maxLength)) {
|
|
652
|
+
issue(
|
|
653
|
+
`Label text must be at most ${maxLength} characters, trimmed, with no control or invisible characters (see sanitizeFrameText)`,
|
|
654
|
+
[...path, "text"]
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
let box: Box;
|
|
658
|
+
if (label.type === "text") {
|
|
659
|
+
box = {
|
|
660
|
+
x0: label.box.x,
|
|
661
|
+
y0: label.box.y,
|
|
662
|
+
x1: label.box.x + label.box.width,
|
|
663
|
+
y1: label.box.y + label.box.height,
|
|
664
|
+
};
|
|
665
|
+
if (
|
|
666
|
+
label.fontSize !== undefined &&
|
|
667
|
+
label.fontSize > label.box.height * FRAME_SPEC_LABEL_MAX_FONT + 1e-9
|
|
668
|
+
) {
|
|
669
|
+
issue("fontSize may be at most 95% of the label's row height", [
|
|
670
|
+
...path,
|
|
671
|
+
"fontSize",
|
|
672
|
+
]);
|
|
673
|
+
}
|
|
674
|
+
if (overlaps(box, zone)) {
|
|
675
|
+
issue("The label overlaps the code or its padding", [...path, "box"]);
|
|
676
|
+
}
|
|
677
|
+
} else {
|
|
678
|
+
const { cx, cy, radius } = label.curve;
|
|
679
|
+
const reach = label.fontSize * FRAME_FONT_SCALE_MAX * 0.6;
|
|
680
|
+
box = {
|
|
681
|
+
x0: cx - radius - reach,
|
|
682
|
+
y0: cy - radius - reach,
|
|
683
|
+
x1: cx + radius + reach,
|
|
684
|
+
y1: cy + radius + reach,
|
|
685
|
+
};
|
|
686
|
+
const nearest = Math.hypot(
|
|
687
|
+
Math.max(zone.x0 - cx, 0, cx - zone.x1),
|
|
688
|
+
Math.max(zone.y0 - cy, 0, cy - zone.y1)
|
|
689
|
+
);
|
|
690
|
+
const farthest = Math.hypot(
|
|
691
|
+
Math.max(Math.abs(zone.x0 - cx), Math.abs(zone.x1 - cx)),
|
|
692
|
+
Math.max(Math.abs(zone.y0 - cy), Math.abs(zone.y1 - cy))
|
|
693
|
+
);
|
|
694
|
+
if (!(farthest <= radius - reach || nearest >= radius + reach)) {
|
|
695
|
+
issue("The curved label crosses the code or its padding", [
|
|
696
|
+
...path,
|
|
697
|
+
"curve",
|
|
698
|
+
]);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
if (!inside(box)) {
|
|
702
|
+
issue("The label extends outside the artboard", path);
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Zod schema for a frame spec, with every limit and cross-field check
|
|
709
|
+
* (FRAME_SPEC_LIMITS). Validate specs from users with this before storing
|
|
710
|
+
* them; the engine validates again before drawing.
|
|
711
|
+
*/
|
|
712
|
+
export const frameSpecSchema =
|
|
713
|
+
frameSpecObjectSchema.superRefine(checkFrameSpec);
|
|
714
|
+
|
|
715
|
+
/** Human-readable problems with `input` as a frame spec (empty when valid). */
|
|
716
|
+
export function getFrameSpecIssues(input: unknown): string[] {
|
|
717
|
+
const result = frameSpecSchema.safeParse(input);
|
|
718
|
+
if (result.success) {
|
|
719
|
+
return [];
|
|
720
|
+
}
|
|
721
|
+
return result.error.issues.map((issue) =>
|
|
722
|
+
issue.path.length > 0
|
|
723
|
+
? `${issue.path.join(".")}: ${issue.message}`
|
|
724
|
+
: issue.message
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// ── Frame options ──────────────────────────────────────────────────────────
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* A frame drawn from a spec: `{ style: "custom", spec }`. `text` and
|
|
732
|
+
* `secondaryText` fill the spec's primary and secondary labels; `color`,
|
|
733
|
+
* `textColor` and `secondaryColor` override its frame, text and accent
|
|
734
|
+
* colors; `fontScale` scales its labels.
|
|
735
|
+
*/
|
|
736
|
+
export const customFrameSchema = frameSchema.extend({
|
|
737
|
+
style: z.literal(CUSTOM_FRAME_STYLE),
|
|
738
|
+
spec: frameSpecSchema,
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
/** A built-in frame (`frameSchema`) or a custom one (`customFrameSchema`). */
|
|
742
|
+
export const anyFrameSchema = z.discriminatedUnion("style", [
|
|
743
|
+
frameSchema,
|
|
744
|
+
customFrameSchema,
|
|
745
|
+
]);
|
|
746
|
+
|
|
747
|
+
export type QRCustomFrame = z.infer<typeof customFrameSchema>;
|
|
748
|
+
export type AnyQRFrame = z.infer<typeof anyFrameSchema>;
|