qrdx 0.0.1

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 (84) hide show
  1. package/README.md +414 -0
  2. package/dist/index.js +11 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/index.mjs +11 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/dist/src/api.d.ts +3 -0
  7. package/dist/src/api.d.ts.map +1 -0
  8. package/dist/src/codegen.d.ts +101 -0
  9. package/dist/src/codegen.d.ts.map +1 -0
  10. package/dist/src/constants.d.ts +15 -0
  11. package/dist/src/constants.d.ts.map +1 -0
  12. package/dist/src/corner-dot.d.ts +4 -0
  13. package/dist/src/corner-dot.d.ts.map +1 -0
  14. package/dist/src/corner-eye.d.ts +3 -0
  15. package/dist/src/corner-eye.d.ts.map +1 -0
  16. package/dist/src/detection.d.ts +13 -0
  17. package/dist/src/detection.d.ts.map +1 -0
  18. package/dist/src/download.d.ts +36 -0
  19. package/dist/src/download.d.ts.map +1 -0
  20. package/dist/src/helpers.d.ts +36 -0
  21. package/dist/src/helpers.d.ts.map +1 -0
  22. package/dist/src/index.d.ts +25 -0
  23. package/dist/src/index.d.ts.map +1 -0
  24. package/dist/src/qr-code.d.ts +20 -0
  25. package/dist/src/qr-code.d.ts.map +1 -0
  26. package/dist/src/template/arrow.d.ts +3 -0
  27. package/dist/src/template/arrow.d.ts.map +1 -0
  28. package/dist/src/template/default.d.ts +3 -0
  29. package/dist/src/template/default.d.ts.map +1 -0
  30. package/dist/src/template/halloween.d.ts +3 -0
  31. package/dist/src/template/halloween.d.ts.map +1 -0
  32. package/dist/src/template/index.d.ts +7 -0
  33. package/dist/src/template/index.d.ts.map +1 -0
  34. package/dist/src/template/square-border.d.ts +3 -0
  35. package/dist/src/template/square-border.d.ts.map +1 -0
  36. package/dist/src/template/standard-box.d.ts +3 -0
  37. package/dist/src/template/standard-box.d.ts.map +1 -0
  38. package/dist/src/template/striked-box.d.ts +3 -0
  39. package/dist/src/template/striked-box.d.ts.map +1 -0
  40. package/dist/src/templates.d.ts +8 -0
  41. package/dist/src/templates.d.ts.map +1 -0
  42. package/dist/src/utils.d.ts +38 -0
  43. package/dist/src/utils.d.ts.map +1 -0
  44. package/dist/types/common.d.ts +9 -0
  45. package/dist/types/common.d.ts.map +1 -0
  46. package/dist/types/corner-dot.d.ts +3 -0
  47. package/dist/types/corner-dot.d.ts.map +1 -0
  48. package/dist/types/corner-eye.d.ts +3 -0
  49. package/dist/types/corner-eye.d.ts.map +1 -0
  50. package/dist/types/image-pattern.d.ts +3 -0
  51. package/dist/types/image-pattern.d.ts.map +1 -0
  52. package/dist/types/index.d.ts +10 -0
  53. package/dist/types/index.d.ts.map +1 -0
  54. package/dist/types/qr.d.ts +35 -0
  55. package/dist/types/qr.d.ts.map +1 -0
  56. package/dist/types/template.d.ts +34 -0
  57. package/dist/types/template.d.ts.map +1 -0
  58. package/package.json +51 -0
  59. package/src/api.tsx +85 -0
  60. package/src/codegen.ts +1043 -0
  61. package/src/constants.ts +26 -0
  62. package/src/corner-dot.tsx +101 -0
  63. package/src/corner-eye.ts +165 -0
  64. package/src/detection.ts +266 -0
  65. package/src/download.tsx +235 -0
  66. package/src/helpers.ts +59 -0
  67. package/src/index.tsx +306 -0
  68. package/src/qr-code.tsx +116 -0
  69. package/src/template/arrow.tsx +39 -0
  70. package/src/template/default.tsx +26 -0
  71. package/src/template/halloween.tsx +146 -0
  72. package/src/template/index.ts +6 -0
  73. package/src/template/square-border.tsx +42 -0
  74. package/src/template/standard-box.tsx +42 -0
  75. package/src/template/striked-box.tsx +41 -0
  76. package/src/templates.tsx +32 -0
  77. package/src/utils.tsx +750 -0
  78. package/types/common.ts +4 -0
  79. package/types/corner-dot.ts +13 -0
  80. package/types/corner-eye.ts +13 -0
  81. package/types/image-pattern.ts +12 -0
  82. package/types/index.ts +16 -0
  83. package/types/qr.ts +41 -0
  84. package/types/template.ts +41 -0
package/src/utils.tsx ADDED
@@ -0,0 +1,750 @@
1
+ import type { JSX } from "react";
2
+ import React from "react";
3
+ import type { TemplateConfig } from "@/types/template";
4
+ import type {
5
+ BodyPattern,
6
+ Excavation,
7
+ ImageSettings,
8
+ Modules,
9
+ QRPropsSVG,
10
+ } from "../types";
11
+ import qrcodegen from "./codegen";
12
+ import {
13
+ DEFAULT_BGCOLOR,
14
+ DEFAULT_FGCOLOR,
15
+ DEFAULT_IMG_SCALE,
16
+ DEFAULT_LEVEL,
17
+ DEFAULT_MARGIN,
18
+ DEFAULT_SIZE,
19
+ ERROR_LEVEL_MAP,
20
+ } from "./constants";
21
+ import { generateCornerDotPath } from "./corner-dot";
22
+ import { generateCornerSquarePath } from "./corner-eye";
23
+ import { getTemplate } from "./templates";
24
+
25
+ // We could just do this in generatePath, except that we want to support
26
+ // non-Path2D canvas, so we need to keep it an explicit step.
27
+ export function excavateModules(
28
+ modules: Modules,
29
+ excavation: Excavation
30
+ ): Modules {
31
+ return modules.slice().map((row, y) => {
32
+ if (y < excavation.y || y >= excavation.y + excavation.h) {
33
+ return row;
34
+ }
35
+ return row.map((cell, x) => {
36
+ if (x < excavation.x || x >= excavation.x + excavation.w) {
37
+ return cell;
38
+ }
39
+ return false;
40
+ });
41
+ });
42
+ }
43
+
44
+ export function generatePath(modules: Modules, margin = 0): string {
45
+ const ops: string[] = [];
46
+ modules.forEach((row, y) => {
47
+ let start: number | null = null;
48
+ row.forEach((cell, x) => {
49
+ if (!cell && start !== null) {
50
+ // M0 0h7v1H0z injects the space with the move and drops the comma,
51
+ // saving a char per operation
52
+ ops.push(
53
+ `M${start + margin} ${y + margin}h${x - start}v1H${start + margin}z`
54
+ );
55
+ start = null;
56
+ return;
57
+ }
58
+
59
+ // end of row, clean up or skip
60
+ if (x === row.length - 1) {
61
+ if (!cell) {
62
+ // We would have closed the op above already so this can only mean
63
+ // 2+ light modules in a row.
64
+ return;
65
+ }
66
+ if (start === null) {
67
+ // Just a single dark module.
68
+ ops.push(`M${x + margin},${y + margin} h1v1H${x + margin}z`);
69
+ } else {
70
+ // Otherwise finish the current line.
71
+ ops.push(
72
+ `M${start + margin},${y + margin} h${x + 1 - start}v1H${
73
+ start + margin
74
+ }z`
75
+ );
76
+ }
77
+ return;
78
+ }
79
+
80
+ if (cell && start === null) {
81
+ start = x;
82
+ }
83
+ });
84
+ });
85
+ return ops.join("");
86
+ }
87
+
88
+ // Helper function to check if a module is part of a position detection pattern (corner)
89
+ function isCornerModule(x: number, y: number, size: number): boolean {
90
+ // Top-left corner (0,0 to 6,6)
91
+ if (x < 7 && y < 7) {
92
+ return true;
93
+ }
94
+ // Top-right corner (size-7,0 to size-1,6)
95
+ if (x >= size - 7 && y < 7) {
96
+ return true;
97
+ }
98
+ // Bottom-left corner (0,size-7 to 6,size-1)
99
+ if (x < 7 && y >= size - 7) {
100
+ return true;
101
+ }
102
+ return false;
103
+ }
104
+
105
+ // Generate circles for data modules with different patterns
106
+ function generateDataCircles(
107
+ modules: Modules,
108
+ options: {
109
+ margin: number;
110
+ pixelSize: number;
111
+ pattern?: BodyPattern;
112
+ }
113
+ ): JSX.Element[] {
114
+ const { margin, pixelSize, pattern = "circle" } = options;
115
+ const shapes: JSX.Element[] = [];
116
+ const qrSize = modules.length;
117
+ const radius = pixelSize * 0.333_333; // radius for circles
118
+
119
+ modules.forEach((row, y) => {
120
+ row.forEach((cell, x) => {
121
+ if (cell && !isCornerModule(x, y, qrSize)) {
122
+ const cx = (x + 0.5 + margin) * pixelSize;
123
+ const cy = (y + 0.5 + margin) * pixelSize;
124
+ const key = `${x}-${y}`;
125
+
126
+ switch (pattern) {
127
+ case "circle":
128
+ // Pattern 1: Standard circles with gaps
129
+ shapes.push(
130
+ <circle
131
+ cx={cx}
132
+ cy={cy}
133
+ key={key}
134
+ r={radius}
135
+ transform={`rotate(0,${cx},${cy})`}
136
+ />
137
+ );
138
+ break;
139
+
140
+ case "circle-large":
141
+ // Pattern: Large circles with no gaps (overlapping slightly)
142
+ {
143
+ const largeRadius = pixelSize * 0.5; // 50% of pixel size for overlapping circles
144
+ shapes.push(
145
+ <circle
146
+ cx={cx}
147
+ cy={cy}
148
+ key={key}
149
+ r={largeRadius}
150
+ transform={`rotate(0,${cx},${cy})`}
151
+ />
152
+ );
153
+ }
154
+ break;
155
+
156
+ case "square":
157
+ // Pattern 2: Squares
158
+ {
159
+ const halfSize = pixelSize * 0.357_143; // half of 71.4% pixel size
160
+ shapes.push(
161
+ <path
162
+ d={`M ${cx - halfSize} ${cy - halfSize}v ${pixelSize * 0.714_286}h ${pixelSize * 0.714_286}v -${pixelSize * 0.714_286}z`}
163
+ key={key}
164
+ transform={`rotate(0,${cx},${cy})`}
165
+ />
166
+ );
167
+ }
168
+ break;
169
+
170
+ case "diamond":
171
+ // Pattern 3: Squares rotated 45 degrees (diamond shape)
172
+ {
173
+ const halfSize = pixelSize * 0.357_143;
174
+ shapes.push(
175
+ <path
176
+ d={`M ${cx - halfSize} ${cy - halfSize}v ${pixelSize * 0.714_286}h ${pixelSize * 0.714_286}v -${pixelSize * 0.714_286}z`}
177
+ key={key}
178
+ transform={`rotate(45,${cx},${cy})`}
179
+ />
180
+ );
181
+ }
182
+ break;
183
+
184
+ case "circle-mixed":
185
+ // Pattern 4: Circles with varying sizes
186
+ {
187
+ const randomFactor =
188
+ (x + y) % 3 === 0 ? 1.5 : (x + y) % 2 === 0 ? 1.0 : 1.25;
189
+ const variedRadius = radius * randomFactor;
190
+ shapes.push(
191
+ <circle
192
+ cx={cx}
193
+ cy={cy}
194
+ key={key}
195
+ r={variedRadius}
196
+ transform={`rotate(0,${cx},${cy})`}
197
+ />
198
+ );
199
+ }
200
+ break;
201
+
202
+ case "pacman":
203
+ // Pattern 5: Rounded rectangles (pill shapes)
204
+ {
205
+ const halfWidth = pixelSize * 0.5;
206
+ const halfHeight = pixelSize * 0.357_143;
207
+ shapes.push(
208
+ <path
209
+ d={`M ${cx - halfWidth} ${cy - halfHeight}v ${pixelSize * 0.714_286}h ${halfWidth}a ${halfWidth * 0.5} ${halfWidth * 0.5}, 0, 0, 0, 0 -${pixelSize * 0.714_286}h -${halfWidth}v -${pixelSize * 0.714_286}z`}
210
+ key={key}
211
+ transform={`rotate(${((x + y) % 2) === 0 ? 0 : 90},${cx},${cy})`}
212
+ />
213
+ );
214
+ }
215
+ break;
216
+
217
+ case "rounded":
218
+ // Pattern: Clean squares with rounded corners
219
+ {
220
+ const halfSize = pixelSize * 0.5;
221
+ const cornerRadius = pixelSize * 0.2; // 20% radius for smooth corners
222
+ shapes.push(
223
+ <rect
224
+ height={pixelSize}
225
+ key={key}
226
+ rx={cornerRadius}
227
+ ry={cornerRadius}
228
+ transform={`rotate(0,${cx},${cy})`}
229
+ width={pixelSize}
230
+ x={cx - halfSize}
231
+ y={cy - halfSize}
232
+ />
233
+ );
234
+ }
235
+ break;
236
+
237
+ case "clean-square":
238
+ // Pattern 12: Clean rectangles only
239
+ {
240
+ const halfSize = pixelSize * 0.5;
241
+ shapes.push(
242
+ <rect
243
+ height={pixelSize}
244
+ key={key}
245
+ transform={`rotate(0,${cx},${cy})`}
246
+ width={pixelSize}
247
+ x={cx - halfSize}
248
+ y={cy - halfSize}
249
+ />
250
+ );
251
+ }
252
+ break;
253
+
254
+ default:
255
+ // Default to circle
256
+ shapes.push(
257
+ <circle
258
+ cx={cx}
259
+ cy={cy}
260
+ key={key}
261
+ r={radius}
262
+ transform={`rotate(0,${cx},${cy})`}
263
+ />
264
+ );
265
+ }
266
+ }
267
+ });
268
+ });
269
+
270
+ return shapes;
271
+ }
272
+
273
+ export function getImageSettings(
274
+ cells: Modules,
275
+ size: number,
276
+ _margin: number,
277
+ imageSettings?: ImageSettings
278
+ ): null | {
279
+ x: number;
280
+ y: number;
281
+ h: number;
282
+ w: number;
283
+ excavation: Excavation | null;
284
+ } {
285
+ if (imageSettings == null) {
286
+ return null;
287
+ }
288
+
289
+ const qrCodeSize = cells.length;
290
+ const defaultSize = Math.floor(size * DEFAULT_IMG_SCALE);
291
+ const scale = qrCodeSize / size;
292
+ const w = (imageSettings.width || defaultSize) * scale;
293
+ const h = (imageSettings.height || defaultSize) * scale;
294
+
295
+ // Center the image in the QR code area (without margins)
296
+ const x =
297
+ imageSettings.x == null ? qrCodeSize / 2 - w / 2 : imageSettings.x * scale;
298
+ const y =
299
+ imageSettings.y == null ? qrCodeSize / 2 - h / 2 : imageSettings.y * scale;
300
+
301
+ let excavation: Excavation | null = null;
302
+ if (imageSettings.excavate) {
303
+ const floorX = Math.floor(x);
304
+ const floorY = Math.floor(y);
305
+ const ceilW = Math.ceil(w + x - floorX);
306
+ const ceilH = Math.ceil(h + y - floorY);
307
+ excavation = { x: floorX, y: floorY, w: ceilW, h: ceilH };
308
+ }
309
+
310
+ return { x, y, h, w, excavation };
311
+ }
312
+
313
+ export function convertImageSettingsToPixels(
314
+ calculatedImageSettings: {
315
+ x: number;
316
+ y: number;
317
+ w: number;
318
+ h: number;
319
+ excavation: Excavation | null;
320
+ },
321
+ size: number,
322
+ numCells: number,
323
+ margin: number
324
+ ) {
325
+ const pixelRatio = size / numCells;
326
+ const imgWidth = calculatedImageSettings.w * pixelRatio;
327
+ const imgHeight = calculatedImageSettings.h * pixelRatio;
328
+ const imgLeft = (calculatedImageSettings.x + margin) * pixelRatio;
329
+ const imgTop = (calculatedImageSettings.y + margin) * pixelRatio;
330
+
331
+ return { imgWidth, imgHeight, imgLeft, imgTop };
332
+ }
333
+
334
+ export function QRCodeSVG(props: QRPropsSVG) {
335
+ const {
336
+ value,
337
+ size = DEFAULT_SIZE,
338
+ level = DEFAULT_LEVEL,
339
+ bgColor = DEFAULT_BGCOLOR,
340
+ fgColor = DEFAULT_FGCOLOR,
341
+ eyeColor,
342
+ dotColor,
343
+ bodyPattern = "circle",
344
+ cornerEyePattern = "gear",
345
+ cornerEyeDotPattern = "circle",
346
+ margin = DEFAULT_MARGIN,
347
+ isOGContext = false,
348
+ imageSettings,
349
+ templateId,
350
+ customTemplate,
351
+ customProps,
352
+ ...otherProps
353
+ } = props;
354
+
355
+ const shouldUseHigherErrorLevel =
356
+ isOGContext && imageSettings?.excavate && (level === "L" || level === "M");
357
+
358
+ // Use a higher error correction level 'Q' when excavation is enabled
359
+ // to ensure the QR code remains scannable despite the removed modules.
360
+ const effectiveLevel = shouldUseHigherErrorLevel ? "Q" : level;
361
+
362
+ let cells = qrcodegen.QrCode.encodeText(
363
+ value,
364
+ ERROR_LEVEL_MAP[effectiveLevel]
365
+ ).getModules();
366
+
367
+ const numCells = cells.length + margin * 2;
368
+ const calculatedImageSettings = getImageSettings(
369
+ cells,
370
+ size,
371
+ margin,
372
+ imageSettings
373
+ );
374
+
375
+ let image: null | JSX.Element = null;
376
+ if (imageSettings != null && calculatedImageSettings != null) {
377
+ if (calculatedImageSettings.excavation != null) {
378
+ cells = excavateModules(cells, calculatedImageSettings.excavation);
379
+ }
380
+
381
+ // Convert image settings from module coordinates to pixel coordinates
382
+ const { imgWidth, imgHeight, imgLeft, imgTop } =
383
+ convertImageSettingsToPixels(
384
+ calculatedImageSettings,
385
+ size,
386
+ numCells,
387
+ margin
388
+ );
389
+
390
+ if (isOGContext) {
391
+ image = (
392
+ <img
393
+ alt="Logo"
394
+ src={imageSettings.src}
395
+ style={{
396
+ position: "absolute",
397
+ left: `${imgLeft}px`,
398
+ top: `${imgTop}px`,
399
+ width: `${imgWidth}px`,
400
+ height: `${imgHeight}px`,
401
+ }}
402
+ />
403
+ );
404
+ } else {
405
+ image = (
406
+ <image
407
+ height={imgHeight}
408
+ href={imageSettings.src}
409
+ preserveAspectRatio="none"
410
+ width={imgWidth}
411
+ x={imgLeft}
412
+ y={imgTop}
413
+ />
414
+ );
415
+ }
416
+ }
417
+
418
+ // Calculate pixel size for positioning
419
+ const pixelSize = size / numCells;
420
+ const qrSize = cells.length;
421
+ const cornerSize = 7 * pixelSize; // Corner patterns are 7x7 modules
422
+ const cornerDotRadius = 1.5 * pixelSize; // Inner dot radius
423
+
424
+ // Generate data circles with selected pattern
425
+ const dataCircles = generateDataCircles(cells, {
426
+ margin,
427
+ pixelSize,
428
+ pattern: bodyPattern,
429
+ });
430
+
431
+ // Calculate corner positions with margin
432
+ const topLeftX = margin * pixelSize;
433
+ const topLeftY = margin * pixelSize;
434
+ const topRightX = (qrSize - 7 + margin) * pixelSize;
435
+ const topRightY = margin * pixelSize;
436
+ const bottomLeftX = margin * pixelSize;
437
+ const bottomLeftY = (qrSize - 7 + margin) * pixelSize;
438
+
439
+ // QR Code content (can be used standalone or inside wrapper)
440
+ const qrContent = (
441
+ <>
442
+ {/* Background */}
443
+ <rect fill={bgColor} height={size} width={size} x={0} y={0} />
444
+
445
+ {/* Data module circles */}
446
+ <g fill={fgColor}>{dataCircles}</g>
447
+
448
+ {/* Top-left corner square */}
449
+ <g fill={eyeColor}>
450
+ <path
451
+ clipRule="evenodd"
452
+ d={generateCornerSquarePath(
453
+ topLeftX,
454
+ topLeftY,
455
+ cornerSize,
456
+ cornerEyePattern
457
+ )}
458
+ transform={`rotate(0,${topLeftX + cornerSize / 2},${topLeftY + cornerSize / 2})`}
459
+ />
460
+ </g>
461
+
462
+ {/* Top-left corner dot */}
463
+ <g fill={dotColor}>
464
+ {generateCornerDotPath(
465
+ topLeftX + cornerSize / 2,
466
+ topLeftY + cornerSize / 2,
467
+ cornerDotRadius,
468
+ cornerEyeDotPattern
469
+ )}
470
+ </g>
471
+
472
+ {/* Top-right corner square */}
473
+ <g fill={eyeColor}>
474
+ <path
475
+ clipRule="evenodd"
476
+ d={generateCornerSquarePath(
477
+ topRightX,
478
+ topRightY,
479
+ cornerSize,
480
+ cornerEyePattern
481
+ )}
482
+ transform={`rotate(0,${topRightX + cornerSize / 2},${topRightY + cornerSize / 2})`}
483
+ />
484
+ </g>
485
+
486
+ {/* Top-right corner dot */}
487
+ <g fill={dotColor}>
488
+ {generateCornerDotPath(
489
+ topRightX + cornerSize / 2,
490
+ topRightY + cornerSize / 2,
491
+ cornerDotRadius,
492
+ cornerEyeDotPattern
493
+ )}
494
+ </g>
495
+
496
+ {/* Bottom-left corner square */}
497
+ <g fill={eyeColor}>
498
+ <path
499
+ clipRule="evenodd"
500
+ d={generateCornerSquarePath(
501
+ bottomLeftX,
502
+ bottomLeftY,
503
+ cornerSize,
504
+ cornerEyePattern
505
+ )}
506
+ transform={`rotate(0,${bottomLeftX + cornerSize / 2},${bottomLeftY + cornerSize / 2})`}
507
+ />
508
+ </g>
509
+
510
+ {/* Bottom-left corner dot */}
511
+ <g fill={dotColor}>
512
+ {generateCornerDotPath(
513
+ bottomLeftX + cornerSize / 2,
514
+ bottomLeftY + cornerSize / 2,
515
+ cornerDotRadius,
516
+ cornerEyeDotPattern
517
+ )}
518
+ </g>
519
+
520
+ {image}
521
+ </>
522
+ );
523
+
524
+ // Check if template is specified (customTemplate takes precedence over templateId)
525
+ const template =
526
+ customTemplate || (templateId ? getTemplate(templateId) : undefined);
527
+ if (template) {
528
+ // For templates, we need to create a properly sized SVG that fits the template coordinate system
529
+ // Templates expect the QR content to be in a coordinate system that matches their transforms
530
+ const templateSize = 300; // All templates use 300x300 base coordinate system
531
+
532
+ // Create QR content as a complete SVG for templates
533
+ const templateQrContent = (
534
+ <svg
535
+ height={templateSize}
536
+ viewBox={`0 0 ${templateSize} ${templateSize}`}
537
+ width={templateSize}
538
+ >
539
+ {/* Scale all QR elements to fit template coordinate system */}
540
+ <g transform={`scale(${templateSize / size})`}>
541
+ {/* Data module circles */}
542
+ <g fill={fgColor}>{dataCircles}</g>
543
+
544
+ {/* Top-left corner square */}
545
+ <g fill={eyeColor || fgColor}>
546
+ <path
547
+ clipRule="evenodd"
548
+ d={generateCornerSquarePath(
549
+ topLeftX,
550
+ topLeftY,
551
+ cornerSize,
552
+ cornerEyePattern
553
+ )}
554
+ transform={`rotate(0,${topLeftX + cornerSize / 2},${topLeftY + cornerSize / 2})`}
555
+ />
556
+ </g>
557
+
558
+ {/* Top-left corner dot */}
559
+ <g fill={dotColor || fgColor}>
560
+ {generateCornerDotPath(
561
+ topLeftX + cornerSize / 2,
562
+ topLeftY + cornerSize / 2,
563
+ cornerDotRadius,
564
+ cornerEyeDotPattern
565
+ )}
566
+ </g>
567
+
568
+ {/* Top-right corner square */}
569
+ <g fill={eyeColor || fgColor}>
570
+ <path
571
+ clipRule="evenodd"
572
+ d={generateCornerSquarePath(
573
+ topRightX,
574
+ topRightY,
575
+ cornerSize,
576
+ cornerEyePattern
577
+ )}
578
+ transform={`rotate(0,${topRightX + cornerSize / 2},${topRightY + cornerSize / 2})`}
579
+ />
580
+ </g>
581
+
582
+ {/* Top-right corner dot */}
583
+ <g fill={dotColor || fgColor}>
584
+ {generateCornerDotPath(
585
+ topRightX + cornerSize / 2,
586
+ topRightY + cornerSize / 2,
587
+ cornerDotRadius,
588
+ cornerEyeDotPattern
589
+ )}
590
+ </g>
591
+
592
+ {/* Bottom-left corner square */}
593
+ <g fill={eyeColor || fgColor}>
594
+ <path
595
+ clipRule="evenodd"
596
+ d={generateCornerSquarePath(
597
+ bottomLeftX,
598
+ bottomLeftY,
599
+ cornerSize,
600
+ cornerEyePattern
601
+ )}
602
+ transform={`rotate(0,${bottomLeftX + cornerSize / 2},${bottomLeftY + cornerSize / 2})`}
603
+ />
604
+ </g>
605
+
606
+ {/* Bottom-left corner dot */}
607
+ <g fill={dotColor || fgColor}>
608
+ {generateCornerDotPath(
609
+ bottomLeftX + cornerSize / 2,
610
+ bottomLeftY + cornerSize / 2,
611
+ cornerDotRadius,
612
+ cornerEyeDotPattern
613
+ )}
614
+ </g>
615
+
616
+ {image}
617
+ </g>
618
+ </svg>
619
+ );
620
+
621
+ // Scale pixelSize to match the template coordinate system
622
+ // Since QR content is scaled to templateSize, pixelSize should also be scaled
623
+ const scaleFactor = templateSize / size;
624
+ const scaledPixelSize = pixelSize * scaleFactor;
625
+
626
+ const templateConfig: TemplateConfig = {
627
+ pixelSize: scaledPixelSize,
628
+ url: value,
629
+ };
630
+
631
+ // Use template wrapper with the QR content and pass the size
632
+ // Spread customProps so templates can access their custom properties directly
633
+ const wrappedSvg = template.wrapper(
634
+ templateQrContent,
635
+ {
636
+ fgColor,
637
+ bgColor,
638
+ ...(customProps || {}),
639
+ ...otherProps,
640
+ },
641
+ templateConfig
642
+ );
643
+
644
+ // Clone the wrapped SVG element and override width/height to match requested size
645
+ // This ensures downloads get the proper size while keeping viewBox for scaling
646
+ // Remove percentage-based styles that would interfere with absolute sizing
647
+ // @ts-expect-error
648
+ const originalStyle = wrappedSvg?.props?.style || {};
649
+ const newStyle = {
650
+ ...originalStyle,
651
+ width: undefined,
652
+ height: undefined,
653
+ };
654
+
655
+ // @ts-expect-error
656
+ return React.cloneElement(wrappedSvg, {
657
+ width: size,
658
+ height: size,
659
+ style: newStyle,
660
+ });
661
+ }
662
+
663
+ // Standard QR code without wrapper (backward compatibility)
664
+ return (
665
+ <svg
666
+ height={size}
667
+ viewBox={`0 0 ${size} ${size}`}
668
+ width={size}
669
+ {...otherProps}
670
+ >
671
+ {qrContent}
672
+ </svg>
673
+ );
674
+ }
675
+
676
+ // For canvas we're going to switch our drawing mode based on whether or not
677
+ // the environment supports Path2D. We only need the constructor to be
678
+ // supported, but Edge doesn't actually support the path (string) type
679
+ // argument. Luckily it also doesn't support the addPath() method. We can
680
+ // treat that as the same thing.
681
+ export const SUPPORTS_PATH2D = (() => {
682
+ try {
683
+ new Path2D().addPath(new Path2D());
684
+ } catch (_e) {
685
+ return false;
686
+ }
687
+ return true;
688
+ })();
689
+
690
+ // Utility functions for contrast calculation
691
+ export const hexToRgb = (
692
+ hex: string
693
+ ): { r: number; g: number; b: number } | null => {
694
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
695
+ return result
696
+ ? {
697
+ r: Number.parseInt(result[1], 16),
698
+ g: Number.parseInt(result[2], 16),
699
+ b: Number.parseInt(result[3], 16),
700
+ }
701
+ : null;
702
+ };
703
+
704
+ export const getLuminance = (r: number, g: number, b: number): number => {
705
+ const [rs, gs, bs] = [r, g, b].map((c) => {
706
+ c /= 255;
707
+ return c <= 0.039_28 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
708
+ });
709
+ return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
710
+ };
711
+
712
+ export const getContrastRatio = (color1: string, color2: string): number => {
713
+ const rgb1 = hexToRgb(color1);
714
+ const rgb2 = hexToRgb(color2);
715
+
716
+ if (!(rgb1 && rgb2)) {
717
+ return 1;
718
+ }
719
+
720
+ const lum1 = getLuminance(rgb1.r, rgb1.g, rgb1.b);
721
+ const lum2 = getLuminance(rgb2.r, rgb2.g, rgb2.b);
722
+
723
+ const brightest = Math.max(lum1, lum2);
724
+ const darkest = Math.min(lum1, lum2);
725
+
726
+ return (brightest + 0.05) / (darkest + 0.05);
727
+ };
728
+
729
+ export const getContrastLevel = (
730
+ ratio: number
731
+ ): { level: string; warning: boolean; message: string } => {
732
+ if (ratio >= 7) {
733
+ return { level: "AAA", warning: false, message: "Excellent contrast" };
734
+ }
735
+ if (ratio >= 4.5) {
736
+ return { level: "AA", warning: false, message: "Good contrast" };
737
+ }
738
+ if (ratio >= 3) {
739
+ return {
740
+ level: "AA Large",
741
+ warning: true,
742
+ message: "Low contrast - may be difficult to scan",
743
+ };
744
+ }
745
+ return {
746
+ level: "Fail",
747
+ warning: true,
748
+ message: "Poor contrast - QR code may not scan properly",
749
+ };
750
+ };