qrdx 0.0.46 → 0.0.48

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 (212) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +358 -300
  3. package/dist/index.js +27 -7
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +27 -7
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/qr-code-canvas.js +12 -0
  8. package/dist/qr-code-canvas.js.map +1 -0
  9. package/dist/qr-code-canvas.mjs +12 -0
  10. package/dist/qr-code-canvas.mjs.map +1 -0
  11. package/dist/src/api.d.ts.map +1 -1
  12. package/dist/src/art-trace.d.ts +46 -0
  13. package/dist/src/art-trace.d.ts.map +1 -0
  14. package/dist/src/art.d.ts +138 -0
  15. package/dist/src/art.d.ts.map +1 -0
  16. package/dist/src/body-patterns.d.ts +34 -0
  17. package/dist/src/body-patterns.d.ts.map +1 -0
  18. package/dist/src/codegen.d.ts +5 -5
  19. package/dist/src/codegen.d.ts.map +1 -1
  20. package/dist/src/codeword-map.d.ts +40 -0
  21. package/dist/src/codeword-map.d.ts.map +1 -0
  22. package/dist/src/constants.d.ts +4 -7
  23. package/dist/src/constants.d.ts.map +1 -1
  24. package/dist/src/corner-dot.d.ts +3 -1
  25. package/dist/src/corner-dot.d.ts.map +1 -1
  26. package/dist/src/corner-eye.d.ts +5 -2
  27. package/dist/src/corner-eye.d.ts.map +1 -1
  28. package/dist/src/detection.d.ts.map +1 -1
  29. package/dist/src/download.d.ts +50 -6
  30. package/dist/src/download.d.ts.map +1 -1
  31. package/dist/src/eps.d.ts +62 -0
  32. package/dist/src/eps.d.ts.map +1 -0
  33. package/dist/src/frame-icons.d.ts +11 -0
  34. package/dist/src/frame-icons.d.ts.map +1 -0
  35. package/dist/src/frame-import.d.ts +61 -0
  36. package/dist/src/frame-import.d.ts.map +1 -0
  37. package/dist/src/frame-spec.d.ts +24 -0
  38. package/dist/src/frame-spec.d.ts.map +1 -0
  39. package/dist/src/frame-styles.d.ts +50 -0
  40. package/dist/src/frame-styles.d.ts.map +1 -0
  41. package/dist/src/frame.d.ts +126 -0
  42. package/dist/src/frame.d.ts.map +1 -0
  43. package/dist/src/frames/builtin.d.ts +8 -0
  44. package/dist/src/frames/builtin.d.ts.map +1 -0
  45. package/dist/src/frames/official.d.ts +18 -0
  46. package/dist/src/frames/official.d.ts.map +1 -0
  47. package/dist/src/helpers.d.ts +15 -2
  48. package/dist/src/helpers.d.ts.map +1 -1
  49. package/dist/src/index.d.ts +25 -13
  50. package/dist/src/index.d.ts.map +1 -1
  51. package/dist/src/logo-budget.d.ts +54 -0
  52. package/dist/src/logo-budget.d.ts.map +1 -0
  53. package/dist/src/logo-cleanup.d.ts +25 -0
  54. package/dist/src/logo-cleanup.d.ts.map +1 -0
  55. package/dist/src/logo.d.ts +28 -0
  56. package/dist/src/logo.d.ts.map +1 -0
  57. package/dist/src/print.d.ts +73 -0
  58. package/dist/src/print.d.ts.map +1 -0
  59. package/dist/src/qr-code-canvas.d.ts +13 -0
  60. package/dist/src/qr-code-canvas.d.ts.map +1 -0
  61. package/dist/src/qr-code.d.ts +4 -2
  62. package/dist/src/qr-code.d.ts.map +1 -1
  63. package/dist/src/qr-outline.d.ts +41 -0
  64. package/dist/src/qr-outline.d.ts.map +1 -0
  65. package/dist/src/qr-shape.d.ts +72 -0
  66. package/dist/src/qr-shape.d.ts.map +1 -0
  67. package/dist/src/scan-risk.d.ts +40 -0
  68. package/dist/src/scan-risk.d.ts.map +1 -0
  69. package/dist/src/scan-score.d.ts +154 -0
  70. package/dist/src/scan-score.d.ts.map +1 -0
  71. package/dist/src/shapes.d.ts +36 -0
  72. package/dist/src/shapes.d.ts.map +1 -0
  73. package/dist/src/silhouette.d.ts +28 -0
  74. package/dist/src/silhouette.d.ts.map +1 -0
  75. package/dist/src/utils/color.d.ts.map +1 -1
  76. package/dist/src/utils/fmt.d.ts +24 -0
  77. package/dist/src/utils/fmt.d.ts.map +1 -0
  78. package/dist/src/utils/paint.d.ts +80 -0
  79. package/dist/src/utils/paint.d.ts.map +1 -0
  80. package/dist/src/utils/parse-color.d.ts +21 -0
  81. package/dist/src/utils/parse-color.d.ts.map +1 -0
  82. package/dist/src/utils/path-data.d.ts +24 -0
  83. package/dist/src/utils/path-data.d.ts.map +1 -0
  84. package/dist/src/utils/png.d.ts +16 -0
  85. package/dist/src/utils/png.d.ts.map +1 -0
  86. package/dist/src/utils/polygon-clip.d.ts +26 -0
  87. package/dist/src/utils/polygon-clip.d.ts.map +1 -0
  88. package/dist/src/utils/print-shared.d.ts +20 -0
  89. package/dist/src/utils/print-shared.d.ts.map +1 -0
  90. package/dist/src/utils/svg-geometry.d.ts +49 -0
  91. package/dist/src/utils/svg-geometry.d.ts.map +1 -0
  92. package/dist/src/utils/svg-gradient.d.ts +59 -0
  93. package/dist/src/utils/svg-gradient.d.ts.map +1 -0
  94. package/dist/src/utils/svg-painter.d.ts +152 -0
  95. package/dist/src/utils/svg-painter.d.ts.map +1 -0
  96. package/dist/src/utils/webp.d.ts +25 -0
  97. package/dist/src/utils/webp.d.ts.map +1 -0
  98. package/dist/src/utils.d.ts +18 -2
  99. package/dist/src/utils.d.ts.map +1 -1
  100. package/dist/src/webp.d.ts +19 -0
  101. package/dist/src/webp.d.ts.map +1 -0
  102. package/dist/types/art.d.ts +64 -0
  103. package/dist/types/art.d.ts.map +1 -0
  104. package/dist/types/color.d.ts +125 -7
  105. package/dist/types/color.d.ts.map +1 -1
  106. package/dist/types/corner-dot.d.ts +52 -2
  107. package/dist/types/corner-dot.d.ts.map +1 -1
  108. package/dist/types/corner-eye.d.ts +43 -2
  109. package/dist/types/corner-eye.d.ts.map +1 -1
  110. package/dist/types/frame-spec.d.ts +3509 -0
  111. package/dist/types/frame-spec.d.ts.map +1 -0
  112. package/dist/types/frame.d.ts +68 -0
  113. package/dist/types/frame.d.ts.map +1 -0
  114. package/dist/types/image-pattern.d.ts +15 -1
  115. package/dist/types/image-pattern.d.ts.map +1 -1
  116. package/dist/types/index.d.ts +15 -9
  117. package/dist/types/index.d.ts.map +1 -1
  118. package/dist/types/index.js +6 -0
  119. package/dist/types/index.js.map +1 -0
  120. package/dist/types/index.mjs +6 -0
  121. package/dist/types/index.mjs.map +1 -0
  122. package/dist/types/qr.d.ts +1684 -4
  123. package/dist/types/qr.d.ts.map +1 -1
  124. package/dist/types/template.d.ts +2 -2
  125. package/dist/types/template.d.ts.map +1 -1
  126. package/package.json +70 -25
  127. package/src/api.tsx +67 -17
  128. package/src/art-trace.ts +554 -0
  129. package/src/art.tsx +2226 -0
  130. package/src/body-patterns.tsx +389 -0
  131. package/src/codegen.ts +10 -5
  132. package/src/codeword-map.ts +265 -0
  133. package/src/constants.ts +4 -8
  134. package/src/corner-dot.tsx +209 -33
  135. package/src/corner-eye.ts +328 -60
  136. package/src/detection.ts +5 -11
  137. package/src/download.tsx +174 -70
  138. package/src/eps.ts +701 -0
  139. package/src/frame-icons.ts +325 -0
  140. package/src/frame-import.ts +1198 -0
  141. package/src/frame-spec.ts +518 -0
  142. package/src/frame-styles.ts +1095 -0
  143. package/src/frame.tsx +630 -0
  144. package/src/frames/builtin/banner-bottom.json +58 -0
  145. package/src/frames/builtin/circle.json +81 -0
  146. package/src/frames/builtin/rounded-border.json +60 -0
  147. package/src/frames/builtin/ticket.json +51 -0
  148. package/src/frames/builtin.ts +20 -0
  149. package/src/frames/official/attendance.json +110 -0
  150. package/src/frames/official/audio-guide.json +95 -0
  151. package/src/frames/official/book-now.json +103 -0
  152. package/src/frames/official/check-in.json +110 -0
  153. package/src/frames/official/christmas.json +131 -0
  154. package/src/frames/official/donate.json +77 -0
  155. package/src/frames/official/event-ticket.json +77 -0
  156. package/src/frames/official/feedback-bubble.json +73 -0
  157. package/src/frames/official/follow-us.json +83 -0
  158. package/src/frames/official/get-the-app.json +107 -0
  159. package/src/frames/official/halloween.json +80 -0
  160. package/src/frames/official/new-year.json +94 -0
  161. package/src/frames/official/order-at-table.json +86 -0
  162. package/src/frames/official/product-info.json +119 -0
  163. package/src/frames/official/register-product.json +77 -0
  164. package/src/frames/official/review-us.json +127 -0
  165. package/src/frames/official/rsvp-card.json +112 -0
  166. package/src/frames/official/save-contact.json +117 -0
  167. package/src/frames/official/scan-to-pay.json +94 -0
  168. package/src/frames/official/shop-window.json +81 -0
  169. package/src/frames/official/sign-rider.json +81 -0
  170. package/src/frames/official/table-tent.json +75 -0
  171. package/src/frames/official/wifi-card.json +89 -0
  172. package/src/frames/official/winter.json +142 -0
  173. package/src/frames/official.ts +77 -0
  174. package/src/helpers.ts +168 -11
  175. package/src/index.tsx +150 -172
  176. package/src/logo-budget.ts +226 -0
  177. package/src/logo-cleanup.ts +146 -0
  178. package/src/logo.tsx +137 -0
  179. package/src/print.ts +794 -0
  180. package/src/qr-code-canvas.tsx +241 -0
  181. package/src/qr-code.tsx +51 -16
  182. package/src/qr-outline.ts +511 -0
  183. package/src/qr-shape.ts +255 -0
  184. package/src/scan-risk.ts +197 -0
  185. package/src/scan-score.ts +1629 -0
  186. package/src/shapes.ts +169 -0
  187. package/src/silhouette.ts +334 -0
  188. package/src/utils/color.ts +10 -1
  189. package/src/utils/fmt.ts +59 -0
  190. package/src/utils/paint.ts +317 -0
  191. package/src/utils/parse-color.ts +135 -0
  192. package/src/utils/path-data.ts +182 -0
  193. package/src/utils/png.ts +175 -0
  194. package/src/utils/polygon-clip.ts +163 -0
  195. package/src/utils/print-shared.ts +74 -0
  196. package/src/utils/svg-geometry.ts +838 -0
  197. package/src/utils/svg-gradient.ts +407 -0
  198. package/src/utils/svg-painter.ts +946 -0
  199. package/src/utils/webp.ts +556 -0
  200. package/src/utils.tsx +794 -385
  201. package/src/webp.ts +84 -0
  202. package/types/art.ts +74 -0
  203. package/types/color.ts +132 -12
  204. package/types/corner-dot.ts +41 -4
  205. package/types/corner-eye.ts +31 -4
  206. package/types/frame-spec.ts +748 -0
  207. package/types/frame.ts +67 -0
  208. package/types/image-pattern.ts +15 -0
  209. package/types/index.ts +106 -4
  210. package/types/qr.ts +180 -2
  211. package/types/template.ts +2 -2
  212. package/dist/cli.js +0 -4319
@@ -0,0 +1,518 @@
1
+ /** biome-ignore-all lint/style/noMagicNumbers: frame geometry */
2
+ // Draws a frame spec (types/frame-spec.ts): validates it, scales it to the
3
+ // code's size and compiles every shape and label into the same FrameLayout
4
+ // the built-in frames produce, so SVG, canvas, PDF and EPS draw it with the
5
+ // existing code and stay identical. Path data is re-written from the
6
+ // numbers it was read as, never passed through.
7
+ import type { ColorConfig } from "../types/color";
8
+ import { FRAME_FONT_SCALE_MAX, FRAME_FONT_SCALE_MIN } from "../types/frame";
9
+ import {
10
+ FRAME_SPEC_LABEL_MAX_FONT,
11
+ FRAME_SPEC_LIMITS,
12
+ type FrameSpec,
13
+ type FrameSpecColor,
14
+ type FrameSpecLabel,
15
+ type FrameSpecPaint,
16
+ type FrameSpecShape,
17
+ frameSpecPadding,
18
+ frameSpecSchema,
19
+ type QRCustomFrame,
20
+ sanitizeFrameText,
21
+ } from "../types/frame-spec";
22
+ import { DEFAULT_BGCOLOR } from "./constants";
23
+ import type { FrameLabel, FrameLayout, FrameShape } from "./frame";
24
+ import { FRAME_ICON_BOX, getFrameIconPath } from "./frame-icons";
25
+ import { arcLabels, estimateTextEm, mixFrameColors } from "./frame-styles";
26
+ import { circlePath } from "./shapes";
27
+ import { getSolidColor } from "./utils/color";
28
+ import { fmt } from "./utils/fmt";
29
+ import { parseColor } from "./utils/parse-color";
30
+ import { tokenizePathData, writePathData } from "./utils/path-data";
31
+ import {
32
+ clipPolygon,
33
+ flattenSegments,
34
+ type Point,
35
+ polygonsToPath,
36
+ } from "./utils/polygon-clip";
37
+ import {
38
+ parsePathData,
39
+ rectSegments,
40
+ type Segment,
41
+ } from "./utils/svg-geometry";
42
+
43
+ /** Thrown when a custom frame's spec doesn't validate. */
44
+ export class FrameSpecError extends Error {
45
+ readonly issues: string[];
46
+
47
+ constructor(issues: string[]) {
48
+ super(`Invalid frame spec: ${issues.slice(0, 5).join("; ")}`);
49
+ this.name = "FrameSpecError";
50
+ this.issues = issues;
51
+ }
52
+ }
53
+
54
+ // Specs are validated once per object: treat a spec as immutable once drawn.
55
+ const validated = new WeakMap<object, FrameSpec>();
56
+
57
+ /**
58
+ * Validates `spec` with `frameSpecSchema` (every limit applies) and returns
59
+ * the parsed copy. Throws a `FrameSpecError` listing the problems.
60
+ */
61
+ export function resolveFrameSpec(spec: unknown): FrameSpec {
62
+ if (spec && typeof spec === "object") {
63
+ const cached = validated.get(spec);
64
+ if (cached) {
65
+ return cached;
66
+ }
67
+ }
68
+ if (spec === undefined || spec === null) {
69
+ throw new FrameSpecError(['A "custom" frame needs a spec']);
70
+ }
71
+ const result = frameSpecSchema.safeParse(spec);
72
+ if (!result.success) {
73
+ throw new FrameSpecError(
74
+ result.error.issues.map((issue) =>
75
+ issue.path.length > 0
76
+ ? `${issue.path.join(".")}: ${issue.message}`
77
+ : issue.message
78
+ )
79
+ );
80
+ }
81
+ if (typeof spec === "object") {
82
+ validated.set(spec, result.data);
83
+ }
84
+ return result.data;
85
+ }
86
+
87
+ /** Light gap around the code as a share of its width (the plate's padding). */
88
+ export function getFrameSpecGapRatio(spec: unknown): number {
89
+ const valid = resolveFrameSpec(spec);
90
+ return frameSpecPadding(valid) / valid.qr.size;
91
+ }
92
+
93
+ type Resolve = (color: FrameSpecColor) => string;
94
+
95
+ /**
96
+ * The plate under the code is always opaque: the code's background, or
97
+ * white when that is transparent, so artwork never shows through the quiet
98
+ * zone. `background` in a spec means the same color.
99
+ */
100
+ function opaqueBackground(bgColor: ColorConfig | undefined): string {
101
+ const background = getSolidColor(bgColor, DEFAULT_BGCOLOR);
102
+ const parsed = parseColor(background);
103
+ return parsed && parsed.a >= 1 ? background : DEFAULT_BGCOLOR;
104
+ }
105
+
106
+ function colorResolver(
107
+ frame: QRCustomFrame,
108
+ spec: FrameSpec,
109
+ background: string
110
+ ): Resolve {
111
+ const frameColor = frame.color || spec.colors.frame;
112
+ const text = frame.textColor || spec.colors.text || frameColor;
113
+ const accent = frame.secondaryColor || spec.colors.accent || frameColor;
114
+ const base = (ref: string): string => {
115
+ switch (ref) {
116
+ case "frame":
117
+ return frameColor;
118
+ case "text":
119
+ return text;
120
+ case "accent":
121
+ return accent;
122
+ case "background":
123
+ return background;
124
+ default:
125
+ return ref;
126
+ }
127
+ };
128
+ return (color) =>
129
+ typeof color === "string"
130
+ ? base(color)
131
+ : mixFrameColors(base(color.mix[0]), base(color.mix[1]), color.amount);
132
+ }
133
+
134
+ // ── Shapes ─────────────────────────────────────────────────────────────────
135
+
136
+ /** A compiled shape before its paint is applied. */
137
+ type Outline =
138
+ | Omit<Extract<FrameShape, { type: "rect" }>, "fill">
139
+ | Omit<Extract<FrameShape, { type: "polygon" }>, "fill">
140
+ | Omit<Extract<FrameShape, { type: "path" }>, "fill">;
141
+
142
+ function scaledPath(d: string, scale: number, dx = 0, dy = 0): string {
143
+ const commands = tokenizePathData(d);
144
+ // Validated by the schema (paths) or built in (icons)
145
+ return commands ? writePathData(commands, scale, dx, dy) : "";
146
+ }
147
+
148
+ function ellipsePath(cx: number, cy: number, rx: number, ry: number): string {
149
+ return (
150
+ `M${fmt(cx - rx)} ${fmt(cy)}A${fmt(rx)} ${fmt(ry)} 0 1 1 ${fmt(cx + rx)} ${fmt(cy)}` +
151
+ `A${fmt(rx)} ${fmt(ry)} 0 1 1 ${fmt(cx - rx)} ${fmt(cy)}Z`
152
+ );
153
+ }
154
+
155
+ function linePath(
156
+ shape: Extract<FrameSpecShape, { type: "line" }>,
157
+ k: number
158
+ ): Outline {
159
+ const [x1, y1, x2, y2] = [
160
+ shape.x1 * k,
161
+ shape.y1 * k,
162
+ shape.x2 * k,
163
+ shape.y2 * k,
164
+ ];
165
+ const length = Math.hypot(x2 - x1, y2 - y1);
166
+ const half = (shape.width * k) / 2;
167
+ const ux = length > 0 ? (x2 - x1) / length : 1;
168
+ const uy = length > 0 ? (y2 - y1) / length : 0;
169
+ // Normal, to the right of the direction of travel
170
+ const nx = -uy * half;
171
+ const ny = ux * half;
172
+ const piece = (from: number, to: number) => {
173
+ const a: Point = [x1 + ux * from, y1 + uy * from];
174
+ const b: Point = [x1 + ux * to, y1 + uy * to];
175
+ if (shape.cap === "round" && to - from > 2 * half) {
176
+ const a2: Point = [a[0] + ux * half, a[1] + uy * half];
177
+ const b2: Point = [b[0] - ux * half, b[1] - uy * half];
178
+ return (
179
+ `M${fmt(a2[0] - nx)} ${fmt(a2[1] - ny)}L${fmt(b2[0] - nx)} ${fmt(b2[1] - ny)}` +
180
+ `A${fmt(half)} ${fmt(half)} 0 0 1 ${fmt(b2[0] + nx)} ${fmt(b2[1] + ny)}` +
181
+ `L${fmt(a2[0] + nx)} ${fmt(a2[1] + ny)}` +
182
+ `A${fmt(half)} ${fmt(half)} 0 0 1 ${fmt(a2[0] - nx)} ${fmt(a2[1] - ny)}Z`
183
+ );
184
+ }
185
+ return (
186
+ `M${fmt(a[0] - nx)} ${fmt(a[1] - ny)}L${fmt(b[0] - nx)} ${fmt(b[1] - ny)}` +
187
+ `L${fmt(b[0] + nx)} ${fmt(b[1] + ny)}L${fmt(a[0] + nx)} ${fmt(a[1] + ny)}Z`
188
+ );
189
+ };
190
+ if (!shape.dash) {
191
+ return { type: "path", d: piece(0, length), fillRule: "nonzero" };
192
+ }
193
+ // Whole dashes only, from the start; a remainder shorter than a dash is
194
+ // left empty.
195
+ const on = shape.dash[0] * k;
196
+ const period = on + shape.dash[1] * k;
197
+ const parts: string[] = [];
198
+ for (let from = 0; from + on <= length + 1e-6; from += period) {
199
+ parts.push(piece(from, from + on));
200
+ }
201
+ return { type: "path", d: parts.join(""), fillRule: "nonzero" };
202
+ }
203
+
204
+ function outlineOf(shape: FrameSpecShape, k: number): Outline {
205
+ switch (shape.type) {
206
+ case "rect":
207
+ return {
208
+ type: "rect",
209
+ x: shape.x * k,
210
+ y: shape.y * k,
211
+ width: shape.width * k,
212
+ height: shape.height * k,
213
+ radius: (shape.radius ?? 0) * k,
214
+ ...(shape.hole && {
215
+ hole: {
216
+ x: shape.hole.x * k,
217
+ y: shape.hole.y * k,
218
+ width: shape.hole.width * k,
219
+ height: shape.hole.height * k,
220
+ radius: (shape.hole.radius ?? 0) * k,
221
+ },
222
+ }),
223
+ };
224
+ case "circle":
225
+ return {
226
+ type: "path",
227
+ d:
228
+ circlePath(shape.cx * k, shape.cy * k, shape.r * k) +
229
+ (shape.hole
230
+ ? circlePath(shape.cx * k, shape.cy * k, shape.hole * k)
231
+ : ""),
232
+ fillRule: shape.hole ? "evenodd" : "nonzero",
233
+ };
234
+ case "ellipse":
235
+ return {
236
+ type: "path",
237
+ d: ellipsePath(shape.cx * k, shape.cy * k, shape.rx * k, shape.ry * k),
238
+ fillRule: "nonzero",
239
+ };
240
+ case "polygon":
241
+ return {
242
+ type: "polygon",
243
+ points: shape.points.map(([x, y]): Point => [x * k, y * k]),
244
+ };
245
+ case "path":
246
+ return {
247
+ type: "path",
248
+ d: scaledPath(shape.d, k),
249
+ fillRule: shape.fillRule ?? "nonzero",
250
+ };
251
+ case "line":
252
+ return linePath(shape, k);
253
+ default: {
254
+ const icon = getFrameIconPath(shape.icon);
255
+ return {
256
+ type: "path",
257
+ d: scaledPath(
258
+ icon.d,
259
+ (shape.size * k) / FRAME_ICON_BOX,
260
+ shape.x * k,
261
+ shape.y * k
262
+ ),
263
+ fillRule: icon.fillRule,
264
+ };
265
+ }
266
+ }
267
+ }
268
+
269
+ function outlineSegments(outline: Outline): Segment[] {
270
+ if (outline.type === "rect") {
271
+ const { x, y, width, height, radius, hole } = outline;
272
+ return [
273
+ ...rectSegments(x, y, width, height, radius, radius),
274
+ ...(hole
275
+ ? rectSegments(
276
+ hole.x,
277
+ hole.y,
278
+ hole.width,
279
+ hole.height,
280
+ hole.radius,
281
+ hole.radius
282
+ )
283
+ : []),
284
+ ];
285
+ }
286
+ if (outline.type === "polygon") {
287
+ return [
288
+ ...outline.points.map(
289
+ ([x, y], index): Segment =>
290
+ index === 0 ? { op: "M", x, y } : { op: "L", x, y }
291
+ ),
292
+ { op: "Z" },
293
+ ];
294
+ }
295
+ return parsePathData(outline.d);
296
+ }
297
+
298
+ /**
299
+ * A gradient as solid color steps: the outline is cut into strips across
300
+ * the gradient's direction, each filled with the color at its middle.
301
+ */
302
+ function gradientSteps(
303
+ outline: Outline,
304
+ gradient: Extract<FrameSpecPaint, { gradient: unknown }>["gradient"],
305
+ resolve: Resolve
306
+ ): FrameShape[] {
307
+ const segments = outlineSegments(outline);
308
+ const polygons = flattenSegments(segments, 0.2);
309
+ const fillRule =
310
+ outline.type === "rect"
311
+ ? outline.hole
312
+ ? "evenodd"
313
+ : "nonzero"
314
+ : outline.type === "path"
315
+ ? (outline.fillRule ?? "nonzero")
316
+ : "nonzero";
317
+ const vertical = gradient.direction === "vertical";
318
+ const along = (p: Point) => (vertical ? p[1] : p[0]);
319
+ let min = Number.POSITIVE_INFINITY;
320
+ let max = Number.NEGATIVE_INFINITY;
321
+ for (const polygon of polygons) {
322
+ for (const point of polygon) {
323
+ min = Math.min(min, along(point));
324
+ max = Math.max(max, along(point));
325
+ }
326
+ }
327
+ if (!(max > min)) {
328
+ return [];
329
+ }
330
+ const steps = gradient.steps ?? 32;
331
+ const from = resolve(gradient.from);
332
+ const to = resolve(gradient.to);
333
+ const span = max - min;
334
+ // Neighbours overlap slightly so no anti-aliasing seam shows
335
+ const overlap = span * 0.002;
336
+ const shapes: FrameShape[] = [];
337
+ for (let i = 0; i < steps; i++) {
338
+ const a = min + (span * i) / steps;
339
+ const b = Math.min(
340
+ max,
341
+ min + (span * (i + 1)) / steps + (i + 1 < steps ? overlap : 0)
342
+ );
343
+ const box = vertical
344
+ ? {
345
+ x0: Number.NEGATIVE_INFINITY,
346
+ x1: Number.POSITIVE_INFINITY,
347
+ y0: a,
348
+ y1: b,
349
+ }
350
+ : {
351
+ x0: a,
352
+ x1: b,
353
+ y0: Number.NEGATIVE_INFINITY,
354
+ y1: Number.POSITIVE_INFINITY,
355
+ };
356
+ const pieces = polygons
357
+ .map((polygon) => clipPolygon(polygon, box))
358
+ .filter((polygon) => polygon.length > 2);
359
+ if (pieces.length === 0) {
360
+ continue;
361
+ }
362
+ shapes.push({
363
+ type: "path",
364
+ d: polygonsToPath(pieces),
365
+ fill: mixFrameColors(from, to, (i + 0.5) / steps),
366
+ fillRule,
367
+ });
368
+ }
369
+ return shapes;
370
+ }
371
+
372
+ function compileShape(
373
+ shape: FrameSpecShape,
374
+ k: number,
375
+ resolve: Resolve
376
+ ): FrameShape[] {
377
+ const outline = outlineOf(shape, k);
378
+ if (typeof shape.fill === "object" && "gradient" in shape.fill) {
379
+ return gradientSteps(outline, shape.fill.gradient, resolve);
380
+ }
381
+ return [{ ...outline, fill: resolve(shape.fill) } as FrameShape];
382
+ }
383
+
384
+ // ── Labels ─────────────────────────────────────────────────────────────────
385
+
386
+ function labelText(label: FrameSpecLabel, frame: QRCustomFrame): string {
387
+ const maxLength = Math.min(
388
+ label.maxLength ?? FRAME_SPEC_LIMITS.maxTextLength,
389
+ FRAME_SPEC_LIMITS.maxTextLength
390
+ );
391
+ const source =
392
+ label.slot === "primary"
393
+ ? (frame.text ?? label.text)
394
+ : label.slot === "secondary"
395
+ ? (frame.secondaryText ?? label.text)
396
+ : label.text;
397
+ return sanitizeFrameText(source, maxLength);
398
+ }
399
+
400
+ function clampFontScale(scale: number | undefined): number {
401
+ if (scale === undefined || !Number.isFinite(scale)) {
402
+ return 1;
403
+ }
404
+ return Math.min(FRAME_FONT_SCALE_MAX, Math.max(FRAME_FONT_SCALE_MIN, scale));
405
+ }
406
+
407
+ function compileLabel(
408
+ label: FrameSpecLabel,
409
+ frame: QRCustomFrame,
410
+ k: number,
411
+ resolve: Resolve
412
+ ): FrameLabel[] {
413
+ const text = labelText(label, frame);
414
+ if (!text) {
415
+ return [];
416
+ }
417
+ const fontScale = clampFontScale(frame.fontScale);
418
+ const color = resolve(label.color);
419
+ const weight = label.weight === "regular" ? { fontWeight: 400 } : {};
420
+ if (label.type === "curved") {
421
+ const { cx, cy, radius, side, spread = 140 } = label.curve;
422
+ const arc = radius * k * ((spread * Math.PI) / 180);
423
+ const fontSize = Math.min(
424
+ label.fontSize * k * fontScale,
425
+ arc / (estimateTextEm(text) + 0.08 * text.length)
426
+ );
427
+ return arcLabels(
428
+ text,
429
+ cx * k,
430
+ cy * k,
431
+ radius * k,
432
+ fontSize,
433
+ side,
434
+ color,
435
+ label.id
436
+ ).map((letter) => ({ ...letter, ...weight }));
437
+ }
438
+ const x = label.box.x * k;
439
+ const y = label.box.y * k;
440
+ const width = label.box.width * k;
441
+ const height = label.box.height * k;
442
+ const base = (label.fontSize ?? label.box.height * 0.52) * k;
443
+ // At most 95% of the row, so the glyphs stay inside it
444
+ const fontSize = Math.min(
445
+ base * fontScale,
446
+ height * FRAME_SPEC_LABEL_MAX_FONT,
447
+ width / estimateTextEm(text)
448
+ );
449
+ const align = label.align ?? "middle";
450
+ return [
451
+ {
452
+ text,
453
+ x: align === "start" ? x : align === "end" ? x + width : x + width / 2,
454
+ // Cap height of a sans-serif is ~0.7em, so this centers capitals.
455
+ y: y + height / 2 + fontSize * 0.35,
456
+ fontSize,
457
+ maxWidth: width,
458
+ color,
459
+ ...(align !== "middle" && { align }),
460
+ ...weight,
461
+ field: label.id,
462
+ },
463
+ ];
464
+ }
465
+
466
+ // ── Layout ─────────────────────────────────────────────────────────────────
467
+
468
+ /**
469
+ * Layout of a custom frame for a code of `size` pixels: the spec scaled so
470
+ * its QR box is `size` wide, colors resolved from the frame options (then
471
+ * the spec's defaults), labels filled from `text` / `secondaryText`.
472
+ * Throws a `FrameSpecError` when the spec is invalid.
473
+ */
474
+ export function getCustomFrameLayout(
475
+ size: number,
476
+ frame: QRCustomFrame,
477
+ bgColor?: ColorConfig
478
+ ): FrameLayout {
479
+ const spec = resolveFrameSpec(frame.spec);
480
+ const k = size / spec.qr.size;
481
+ const background = opaqueBackground(bgColor);
482
+ const resolve = colorResolver(frame, spec, background);
483
+
484
+ const shapes = spec.shapes.flatMap((shape) =>
485
+ compileShape(shape, k, resolve)
486
+ );
487
+ // The plate: last behind the code, so nothing shows in its quiet zone
488
+ const padding = frameSpecPadding(spec) * k;
489
+ shapes.push({
490
+ type: "rect",
491
+ x: spec.qr.x * k - padding,
492
+ y: spec.qr.y * k - padding,
493
+ width: size + 2 * padding,
494
+ height: size + 2 * padding,
495
+ radius: (spec.qr.radius ?? 0) * k,
496
+ fill: background,
497
+ });
498
+
499
+ const labels = spec.labels.flatMap((label) =>
500
+ compileLabel(label, frame, k, resolve)
501
+ );
502
+ const first = labels[0];
503
+ const main = first && first.rotate === undefined ? first : null;
504
+ const bleed = spec.bleed ?? "none";
505
+
506
+ return {
507
+ width: spec.artboard.width * k,
508
+ height: spec.artboard.height * k,
509
+ qrX: spec.qr.x * k,
510
+ qrY: spec.qr.y * k,
511
+ qrSize: size,
512
+ shapes,
513
+ label: main,
514
+ extraLabels: main ? labels.slice(1) : labels,
515
+ squareEdges: bleed !== "none",
516
+ color: resolve(bleed === "none" ? "frame" : bleed),
517
+ };
518
+ }