qrdx 0.0.59 → 0.0.61

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 (96) hide show
  1. package/README.md +4 -4
  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 -6
  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/node.js +2 -2
  13. package/dist/node.js.map +1 -1
  14. package/dist/node.mjs +2 -2
  15. package/dist/node.mjs.map +1 -1
  16. package/dist/qr-code-canvas.js +1 -1
  17. package/dist/qr-code-canvas.js.map +1 -1
  18. package/dist/qr-code-canvas.mjs +1 -1
  19. package/dist/qr-code-canvas.mjs.map +1 -1
  20. package/dist/schemas.js +1 -1
  21. package/dist/schemas.js.map +1 -1
  22. package/dist/schemas.mjs +1 -1
  23. package/dist/schemas.mjs.map +1 -1
  24. package/dist/src/api.d.ts +2 -3
  25. package/dist/src/api.d.ts.map +1 -1
  26. package/dist/src/corner-dot.d.ts.map +1 -1
  27. package/dist/src/corner-eye.d.ts.map +1 -1
  28. package/dist/src/engine.d.ts +1 -1
  29. package/dist/src/engine.d.ts.map +1 -1
  30. package/dist/src/looks.d.ts +45 -17
  31. package/dist/src/looks.d.ts.map +1 -1
  32. package/dist/src/qr-code-canvas.d.ts +2 -8
  33. package/dist/src/qr-code-canvas.d.ts.map +1 -1
  34. package/dist/src/qr-code-svg.d.ts +2 -3
  35. package/dist/src/qr-code-svg.d.ts.map +1 -1
  36. package/dist/src/qr-code.d.ts +2 -2
  37. package/dist/src/qr-code.d.ts.map +1 -1
  38. package/dist/src/qr-frame-svg.d.ts +2 -3
  39. package/dist/src/qr-frame-svg.d.ts.map +1 -1
  40. package/dist/src/rasterize.d.ts +8 -0
  41. package/dist/src/rasterize.d.ts.map +1 -1
  42. package/dist/src/react-elements.d.ts.map +1 -1
  43. package/dist/src/scan-risk.d.ts.map +1 -1
  44. package/dist/src/scan-score.d.ts +34 -0
  45. package/dist/src/scan-score.d.ts.map +1 -1
  46. package/dist/src/shapes.d.ts +19 -0
  47. package/dist/src/shapes.d.ts.map +1 -1
  48. package/dist/src/utils/oklab.d.ts +16 -0
  49. package/dist/src/utils/oklab.d.ts.map +1 -0
  50. package/dist/src/utils.d.ts.map +1 -1
  51. package/dist/types/constants.d.ts +40 -5
  52. package/dist/types/constants.d.ts.map +1 -1
  53. package/dist/types/corner-dot.d.ts +11 -6
  54. package/dist/types/corner-dot.d.ts.map +1 -1
  55. package/dist/types/corner-eye.d.ts +21 -8
  56. package/dist/types/corner-eye.d.ts.map +1 -1
  57. package/dist/types/frame-spec.d.ts +1 -1
  58. package/dist/types/frame.d.ts +2 -2
  59. package/dist/types/image-pattern.d.ts +3 -3
  60. package/dist/types/image-pattern.d.ts.map +1 -1
  61. package/dist/types/index.js +2 -2
  62. package/dist/types/index.js.map +1 -1
  63. package/dist/types/index.mjs +2 -2
  64. package/dist/types/index.mjs.map +1 -1
  65. package/dist/types/plain.d.ts +1 -1
  66. package/dist/types/plain.d.ts.map +1 -1
  67. package/dist/types/qr.d.ts +96 -46
  68. package/dist/types/qr.d.ts.map +1 -1
  69. package/dist/types/react.d.ts +21 -3
  70. package/dist/types/react.d.ts.map +1 -1
  71. package/dist/vanilla.browser.mjs +29 -29
  72. package/dist/vanilla.browser.mjs.map +1 -1
  73. package/package.json +4 -6
  74. package/src/api.tsx +3 -4
  75. package/src/corner-dot.tsx +15 -0
  76. package/src/corner-eye.ts +163 -72
  77. package/src/engine.ts +4 -0
  78. package/src/looks.ts +272 -69
  79. package/src/qr-code-canvas.tsx +34 -27
  80. package/src/qr-code-svg.tsx +3 -4
  81. package/src/qr-code.tsx +7 -5
  82. package/src/qr-frame-svg.tsx +3 -4
  83. package/src/rasterize.ts +14 -1
  84. package/src/react-elements.ts +40 -2
  85. package/src/scan-risk.ts +19 -3
  86. package/src/scan-score.ts +231 -48
  87. package/src/shapes.ts +123 -0
  88. package/src/utils/oklab.ts +93 -0
  89. package/src/utils.tsx +20 -4
  90. package/types/constants.ts +59 -3
  91. package/types/corner-dot.ts +17 -7
  92. package/types/corner-eye.ts +26 -8
  93. package/types/image-pattern.ts +11 -4
  94. package/types/plain.ts +4 -0
  95. package/types/qr.ts +5 -3
  96. package/types/react.ts +22 -3
package/src/looks.ts CHANGED
@@ -28,6 +28,9 @@ import {
28
28
  SHADOW_OPACITY_DEFAULT,
29
29
  SHADOW_OPACITY_MAX,
30
30
  SHADOW_OPACITY_MIN,
31
+ WITHDRAWN_CORNER_EYE_DOT_PATTERNS,
32
+ WITHDRAWN_CORNER_EYE_PATTERNS,
33
+ withdrawnFallback,
31
34
  } from "../types/constants";
32
35
  import qrcodegen from "./codegen";
33
36
  import {
@@ -40,6 +43,7 @@ import { capImageSettings } from "./logo-budget";
40
43
  import { isShapedCode } from "./qr-shape";
41
44
  import { getStylePairRisk, type StylePairRisk } from "./scan-risk";
42
45
  import { resolveErrorLevel } from "./utils";
46
+ import { oklchToRgb, rgbToOklch } from "./utils/oklab";
43
47
  import { parseColor, type RGBAColor } from "./utils/parse-color";
44
48
 
45
49
  type EyeShapeProps = Pick<
@@ -61,9 +65,16 @@ export function eyeShapeOf(
61
65
  position: EyePosition
62
66
  ): { outer: CornerEyePattern; inner: CornerEyeDotPattern } {
63
67
  const own = props.eyeShapes?.[position];
68
+ // A shape withdrawn before release draws as the one it maps to
64
69
  return {
65
- outer: own?.outer ?? props.cornerEyePattern ?? DEFAULT_EYE,
66
- inner: own?.inner ?? props.cornerEyeDotPattern ?? DEFAULT_DOT,
70
+ outer: withdrawnFallback(
71
+ WITHDRAWN_CORNER_EYE_PATTERNS,
72
+ own?.outer ?? props.cornerEyePattern ?? DEFAULT_EYE
73
+ ),
74
+ inner: withdrawnFallback(
75
+ WITHDRAWN_CORNER_EYE_DOT_PATTERNS,
76
+ own?.inner ?? props.cornerEyeDotPattern ?? DEFAULT_DOT
77
+ ),
67
78
  };
68
79
  }
69
80
 
@@ -314,115 +325,307 @@ export function isSeeThroughBackground(bgColor: QRProps["bgColor"]): boolean {
314
325
  }
315
326
 
316
327
  /**
317
- * How light a color reads to decoders, 0 to 255: the darker of the two
318
- * grey conversions they use (Rec. 709 weights, as jsQR, and Rec. 601, as
319
- * ZXing), so a saturated color is judged as its darkest reading.
328
+ * How light a color reads to each decoder, 0 to 255: the two grey
329
+ * conversions they use, Rec. 709 weights (jsQR) and Rec. 601 (ZXing). A
330
+ * saturated color reads differently to each, so the halo rule holds for
331
+ * both.
320
332
  */
321
- const decoderLuma = ({ r, g, b }: RGBAColor) =>
322
- Math.min(
323
- 0.2126 * r + 0.7152 * g + 0.0722 * b,
324
- 0.299 * r + 0.587 * g + 0.114 * b
325
- );
333
+ type Lumas = [rec709: number, rec601: number];
334
+ const decoderLumas = ({ r, g, b }: RGBAColor): Lumas => [
335
+ 0.2126 * r + 0.7152 * g + 0.0722 * b,
336
+ 0.299 * r + 0.587 * g + 0.114 * b,
337
+ ];
326
338
 
327
- const HALO_LIGHT_LUMA = decoderLuma(parseColor(HALO_LIGHT_MIN) as RGBAColor);
339
+ const HALO_LIGHT_LUMAS = decoderLumas(parseColor(HALO_LIGHT_MIN) as RGBAColor);
328
340
 
329
- /** How light a color reads (decoderLuma), or null when it can't be read. */
330
- const lumaOf = (color: string | undefined) => {
331
- const parsed = readColor(color);
332
- return parsed ? decoderLuma(parsed) : null;
333
- };
341
+ /** Rounding slack in the halo rule's comparisons. */
342
+ const LUMA_SLACK = 1e-9;
334
343
 
335
- /** The rim color as drawn: opaque, the default when it can't be read. */
344
+ /** True when every decoder reads `lumas` between `lower` and `upper`. */
345
+ const lumasWithin = (lumas: Lumas, lower: Lumas, upper: Lumas) =>
346
+ lumas.every(
347
+ (luma, decoder) =>
348
+ luma >= lower[decoder] - LUMA_SLACK && luma <= upper[decoder] + LUMA_SLACK
349
+ );
350
+
351
+ const toHex = ({ r, g, b }: RGBAColor) =>
352
+ `#${[r, g, b]
353
+ .map((channel) => Math.round(channel).toString(16).padStart(2, "0"))
354
+ .join("")}`;
355
+
356
+ /** The rim color as picked: opaque, the default when it can't be read. */
336
357
  function haloColorOf(halo: QRHalo): string {
337
358
  const parsed = readColor(halo.color || HALO_COLOR_DEFAULT);
338
- return parsed && parsed.a > 0
339
- ? `#${[parsed.r, parsed.g, parsed.b]
340
- .map((channel) => Math.round(channel).toString(16).padStart(2, "0"))
341
- .join("")}`
342
- : HALO_COLOR_DEFAULT;
359
+ return parsed && parsed.a > 0 ? toHex(parsed) : HALO_COLOR_DEFAULT;
360
+ }
361
+
362
+ /** Halvings of the lightness and of the chroma when a rim moves: well past a channel step. */
363
+ const FIT_STEPS = 24;
364
+ const CHROMA_STEPS = 12;
365
+
366
+ /**
367
+ * `color` with its lightness moved until every decoder reads it between
368
+ * `lower` and `upper`, as little as it takes: its OKLab hue and chroma are
369
+ * kept (the chroma reduced only where sRGB has no such color, or where no
370
+ * lightness fits both decoders at it). Null when not even a grey fits.
371
+ */
372
+ function fitLightness(
373
+ color: RGBAColor,
374
+ lower: Lumas,
375
+ upper: Lumas
376
+ ): RGBAColor | null {
377
+ if (lumasWithin(decoderLumas(color), lower, upper)) {
378
+ return color;
379
+ }
380
+ if (color.r === color.g && color.g === color.b) {
381
+ // A grey stays a grey: every decoder reads it as its own channel
382
+ const grey = Math.min(
383
+ Math.floor(Math.min(...upper) + LUMA_SLACK),
384
+ Math.max(color.r, Math.ceil(Math.max(...lower) - LUMA_SLACK))
385
+ );
386
+ return grey >= Math.max(...lower) - LUMA_SLACK
387
+ ? { r: grey, g: grey, b: grey, a: 1 }
388
+ : null;
389
+ }
390
+ const start = rgbToOklch(color);
391
+ const atChroma = (c: number) => {
392
+ const here = oklchToRgb({ l: start.l, c, h: start.h });
393
+ const lumas = decoderLumas(here);
394
+ if (lumasWithin(lumas, lower, upper)) {
395
+ return here;
396
+ }
397
+ // Too dark for a decoder: lighten toward white, which clears `lower`;
398
+ // else darken toward black, which clears `upper`
399
+ const lighten = lumas.some((luma, decoder) => luma < lower[decoder]);
400
+ const clears = (rgb: RGBAColor) =>
401
+ decoderLumas(rgb).every((luma, decoder) =>
402
+ lighten
403
+ ? luma >= lower[decoder] - LUMA_SLACK
404
+ : luma <= upper[decoder] + LUMA_SLACK
405
+ );
406
+ // The lightness nearest the color's own that clears the bound it missed
407
+ let missed = start.l;
408
+ let cleared = lighten ? 1 : 0;
409
+ for (let step = 0; step < FIT_STEPS; step++) {
410
+ const l = (missed + cleared) / 2;
411
+ if (clears(oklchToRgb({ l, c, h: start.h }))) {
412
+ cleared = l;
413
+ } else {
414
+ missed = l;
415
+ }
416
+ }
417
+ const rgb = oklchToRgb({ l: cleared, c, h: start.h });
418
+ return lumasWithin(decoderLumas(rgb), lower, upper) ? rgb : null;
419
+ };
420
+ const full = atChroma(start.c);
421
+ if (full) {
422
+ return full;
423
+ }
424
+ // As much of the chroma as still fits
425
+ let best = atChroma(0);
426
+ if (!best) {
427
+ return null;
428
+ }
429
+ let fits = 0;
430
+ let fails = start.c;
431
+ for (let step = 0; step < CHROMA_STEPS; step++) {
432
+ const c = (fits + fails) / 2;
433
+ const rgb = atChroma(c);
434
+ if (rgb) {
435
+ fits = c;
436
+ best = rgb;
437
+ } else {
438
+ fails = c;
439
+ }
440
+ }
441
+ return best;
442
+ }
443
+
444
+ export interface HaloRefusal {
445
+ /** Which part of the design refuses a halo */
446
+ reason: "see-through" | "picture" | "dark-background";
447
+ message: string;
448
+ fix: string;
343
449
  }
344
450
 
345
- export interface HaloRisk {
346
- /** Which part of the design refuses it */
347
- reason: "see-through" | "picture" | "dark-background" | "rim";
451
+ export interface HaloAdjustment {
452
+ /** The rim is drawn, in another color than the one picked */
453
+ reason: "adjusted";
454
+ /** The color the rim is drawn in */
455
+ color: string;
348
456
  message: string;
349
457
  fix: string;
350
458
  }
351
459
 
460
+ /** Why a halo isn't drawn on a code, or how its color was moved. */
461
+ export type HaloRisk = HaloRefusal | HaloAdjustment;
462
+
463
+ const TURN_OFF = "or turn the halo off";
464
+
352
465
  /**
353
- * Why a halo isn't drawn on this code, or null when it is (or there is
354
- * none). A halo is drawn only where it was measured never to cost a decode
355
- * (tests/halo-decode.test.ts): on an opaque background, in a rim at least
356
- * as light as HALO_LIGHT_MIN and no lighter than any background color (the
357
- * color, every gradient stop). A rim lighter than the background raises
358
- * the light level around every dot, and decoders then misread light
359
- * modules: at every width on a grey page under a see-through code, and
360
- * with grey or colored dots on a light grey background (measured 21 Nov
361
- * 2026, every dot shape, widths 0.1 to 0.5, 4, 6 and 9 px per module,
362
- * four rasteriser and decoder pipelines). A see-through background shows
363
- * whatever is under the code, and a picture its own tones, so neither
364
- * takes a halo.
466
+ * How light a rim may read to each decoder on this background (as light
467
+ * as its darkest color, every gradient stop included), or why no rim is
468
+ * drawn on it: a picture, any see-through paint, or a color darker than
469
+ * HALO_LIGHT_MIN.
365
470
  */
366
- export function getHaloRisk(
367
- props: Pick<QRProps, "halo" | "bgColor">
368
- ): HaloRisk | null {
369
- if (!props.halo) {
370
- return null;
371
- }
372
- const off = "or turn the halo off";
373
- const paint = normalizeColorConfig(props.bgColor, DEFAULT_BGCOLOR);
471
+ function rimCeiling(bgColor: QRProps["bgColor"]): Lumas | HaloRefusal {
472
+ const paint = normalizeColorConfig(bgColor, DEFAULT_BGCOLOR);
374
473
  if (paint.type === "image") {
375
474
  return {
376
475
  reason: "picture",
377
476
  message:
378
477
  "A halo isn't drawn on a picture background: on the picture's darker parts the rim makes the code unreadable",
379
- fix: `Use a background color, ${off}`,
478
+ fix: `Use a background color, ${TURN_OFF}`,
380
479
  };
381
480
  }
382
- if (isSeeThroughBackground(props.bgColor)) {
481
+ if (isSeeThroughBackground(bgColor)) {
383
482
  return {
384
483
  reason: "see-through",
385
484
  message:
386
- "A halo isn't drawn on a see-through background: on a grey surface under the code the rim makes it unreadable",
387
- fix: `Give the code an opaque background, ${off}`,
485
+ "A halo needs an opaque background: on a grey surface under a see-through code the rim makes it unreadable",
486
+ fix: `Give the code an opaque background, ${TURN_OFF}`,
388
487
  };
389
488
  }
390
- const grounds = (
489
+ const colors = (
391
490
  paint.type === "solid"
392
491
  ? [paint.color]
393
492
  : paint.stops.map((stop) => stop.color)
394
- ).map(lumaOf);
395
- const ground = grounds.includes(null)
396
- ? null
397
- : Math.min(...(grounds as number[]));
398
- if (ground === null || ground < HALO_LIGHT_LUMA - 1e-9) {
493
+ ).map(readColor);
494
+ const ceiling: Lumas = [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY];
495
+ for (const color of colors) {
496
+ const lumas = color ? decoderLumas(color) : ([0, 0] as Lumas);
497
+ ceiling[0] = Math.min(ceiling[0], lumas[0]);
498
+ ceiling[1] = Math.min(ceiling[1], lumas[1]);
499
+ }
500
+ if (!lumasWithin(HALO_LIGHT_LUMAS, HALO_LIGHT_LUMAS, ceiling)) {
399
501
  return {
400
502
  reason: "dark-background",
401
- message: `A halo is drawn only on a background at least as light as ${HALO_LIGHT_MIN}: on a darker one the rim makes the code unreadable`,
402
- fix: `Lighten the background to ${HALO_LIGHT_MIN} or lighter, ${off}`,
503
+ message: `A halo needs a light background, at least as light as ${HALO_LIGHT_MIN}: on a darker one the rim makes the code unreadable`,
504
+ fix: `Lighten the background to ${HALO_LIGHT_MIN} or lighter, ${TURN_OFF}`,
403
505
  };
404
506
  }
405
- const rim = lumaOf(haloColorOf(props.halo)) as number;
406
- if (rim < HALO_LIGHT_LUMA - 1e-9 || rim > ground + 1e-9) {
407
- return {
408
- reason: "rim",
409
- message: `A halo is drawn only in a color between ${HALO_LIGHT_MIN} and the background's own: a rim lighter than the background, or darker than ${HALO_LIGHT_MIN}, makes the code unreadable`,
410
- fix: `Pick a halo color no lighter than the background and at least as light as ${HALO_LIGHT_MIN}, ${off}`,
411
- };
507
+ return ceiling;
508
+ }
509
+
510
+ /**
511
+ * The halo's rim on this background: the color it is drawn in, the color
512
+ * picked, or why none is drawn.
513
+ */
514
+ function planRim(
515
+ halo: QRHalo,
516
+ bgColor: QRProps["bgColor"]
517
+ ): { drawn: string; picked: string } | HaloRefusal {
518
+ const ceiling = rimCeiling(bgColor);
519
+ if (!Array.isArray(ceiling)) {
520
+ return ceiling;
521
+ }
522
+ const picked = haloColorOf(halo);
523
+ const fitted = fitLightness(
524
+ parseColor(picked) as RGBAColor,
525
+ HALO_LIGHT_LUMAS,
526
+ ceiling
527
+ );
528
+ // HALO_LIGHT_MIN fits wherever a halo is drawn (rimCeiling)
529
+ return { drawn: fitted ? toHex(fitted) : HALO_LIGHT_MIN, picked };
530
+ }
531
+
532
+ /**
533
+ * The color a halo's rim is drawn in on this background, or null where no
534
+ * halo is drawn (getHaloRisk says why). A rim is drawn only where it was
535
+ * measured never to cost a decode (tests/halo-decode.test.ts): every
536
+ * decoder must read it at least as light as HALO_LIGHT_MIN and no lighter
537
+ * than any background color (the color, every gradient stop). Any color
538
+ * can be picked: one outside that range is drawn in the closest color
539
+ * inside it, its hue and as much of its chroma as fits kept, only its
540
+ * lightness moved (tests/halo-colors-decode.test.ts reads those rims).
541
+ */
542
+ export function fitHaloColor(
543
+ color: string | undefined,
544
+ bgColor: QRProps["bgColor"]
545
+ ): string | null {
546
+ const plan = planRim({ color }, bgColor);
547
+ return "drawn" in plan ? plan.drawn : null;
548
+ }
549
+
550
+ /**
551
+ * Up to `count` tints of `color` a halo draws as they are on this
552
+ * background: its hue at evenly spaced lightness from the darkest rim the
553
+ * rule takes toward the background's own, darkest (the most visible)
554
+ * first. Empty where no halo is drawn.
555
+ */
556
+ export function haloTints(
557
+ color: string | undefined,
558
+ bgColor: QRProps["bgColor"],
559
+ count = 4
560
+ ): string[] {
561
+ const ceiling = rimCeiling(bgColor);
562
+ const base = readColor(color);
563
+ if (!(Array.isArray(ceiling) && base)) {
564
+ return [];
565
+ }
566
+ const tints = new Set<string>();
567
+ for (let index = 0; index < count; index++) {
568
+ const share = index / count;
569
+ const floor = HALO_LIGHT_LUMAS.map(
570
+ (luma, decoder) => luma + share * ((ceiling[decoder] ?? luma) - luma)
571
+ ) as Lumas;
572
+ const tint = fitLightness({ ...base, a: 1 }, floor, ceiling);
573
+ if (tint) {
574
+ tints.add(toHex(tint));
575
+ }
576
+ }
577
+ return [...tints];
578
+ }
579
+
580
+ /**
581
+ * Why a halo isn't drawn on this code, or how its color was moved: null
582
+ * when it is drawn as picked (or there is none). A rim lighter than the
583
+ * background raises the light level around every dot, and decoders then
584
+ * misread light modules: at every width on a grey page under a
585
+ * see-through code, and with grey or colored dots on a light grey
586
+ * background (measured 21 Nov 2026, every dot shape, widths 0.1 to 0.5, 4,
587
+ * 6 and 9 px per module, four rasteriser and decoder pipelines). A
588
+ * see-through background shows whatever is under the code, and a picture
589
+ * its own tones, so neither takes a halo, nor does a background darker
590
+ * than HALO_LIGHT_MIN; those are refusals. On any other background a
591
+ * color outside the rule's range is "adjusted": drawn in the closest one
592
+ * inside it (fitHaloColor).
593
+ */
594
+ export function getHaloRisk(
595
+ props: Pick<QRProps, "halo" | "bgColor">
596
+ ): HaloRisk | null {
597
+ if (!props.halo) {
598
+ return null;
599
+ }
600
+ const plan = planRim(props.halo, props.bgColor);
601
+ if (!("drawn" in plan)) {
602
+ return plan;
603
+ }
604
+ if (plan.drawn === plan.picked) {
605
+ return null;
412
606
  }
413
- return null;
607
+ return {
608
+ reason: "adjusted",
609
+ color: plan.drawn,
610
+ message: `Halo drawn as ${plan.drawn} instead of ${plan.picked} so the code still scans: a rim must read no lighter than the background and at least as light as ${HALO_LIGHT_MIN}`,
611
+ fix: `To have it drawn as picked, choose a color in that range, such as ${plan.drawn}`,
612
+ };
414
613
  }
415
614
 
416
615
  /**
417
616
  * A halo with its defaults filled in, its width in range and its rim
418
- * opaque; null without one, or when `code` is given and the halo is
419
- * refused on it (getHaloRisk).
617
+ * opaque; null without one. With `code`, the rim is in the color drawn on
618
+ * that code's background (fitHaloColor), and null where none is drawn.
420
619
  */
421
620
  export function resolveHalo(
422
621
  halo: QRHalo | undefined,
423
622
  code?: Pick<QRProps, "bgColor">
424
623
  ): { color: string; width: number } | null {
425
- if (!halo || (code && getHaloRisk({ halo, bgColor: code.bgColor }))) {
624
+ if (!halo) {
625
+ return null;
626
+ }
627
+ const plan = code ? planRim(halo, code.bgColor) : null;
628
+ if (plan && !("drawn" in plan)) {
426
629
  return null;
427
630
  }
428
631
  const width = clamp(
@@ -433,7 +636,7 @@ export function resolveHalo(
433
636
  );
434
637
  // The rim is always opaque: a see-through rim would compound where rims
435
638
  // overlap in the print PDF, which paints each shape on its own
436
- return { color: haloColorOf(halo), width };
639
+ return { color: plan ? plan.drawn : haloColorOf(halo), width };
437
640
  }
438
641
 
439
642
  /**
@@ -10,8 +10,17 @@
10
10
  * Copyright (c) Paul O'Shannessy
11
11
  * SPDX-License-Identifier: ISC
12
12
  */
13
- import { type JSX, useEffect, useRef, useState } from "react";
14
- import type { QRPropsCanvas } from "../types/react";
13
+ // createElement rather than JSX: compiled JSX would import
14
+ // react/jsx-runtime, which React has had only since 16.14.
15
+ import {
16
+ createElement,
17
+ Fragment,
18
+ type ReactElement,
19
+ useEffect,
20
+ useRef,
21
+ useState,
22
+ } from "react";
23
+ import type { QRPropsCanvas, ReactQRElement } from "../types/react";
15
24
  import qrcodegen from "./codegen";
16
25
  import {
17
26
  DEFAULT_BGCOLOR,
@@ -35,7 +44,7 @@ import {
35
44
  } from "./utils";
36
45
  import { getSolidColor } from "./utils/color";
37
46
 
38
- export function QRCodeCanvas(props: QRPropsCanvas) {
47
+ export function QRCodeCanvas(props: QRPropsCanvas): ReactQRElement {
39
48
  const {
40
49
  value,
41
50
  size = DEFAULT_SIZE,
@@ -233,31 +242,29 @@ export function QRCodeCanvas(props: QRPropsCanvas) {
233
242
  });
234
243
 
235
244
  const canvasStyle = { height: outputHeight, width: outputWidth, ...style };
236
- let img: JSX.Element | null = null;
245
+ let img: ReactElement | null = null;
237
246
  if (imgSrc != null) {
238
- img = (
239
- <img
240
- alt="QR code"
241
- key={imgSrc}
242
- onLoad={() => {
243
- setIsImageLoaded(true);
244
- }}
245
- ref={_image}
246
- src={imgSrc}
247
- style={{ display: "none" }}
248
- />
249
- );
247
+ img = createElement("img", {
248
+ alt: "QR code",
249
+ key: imgSrc,
250
+ onLoad: () => {
251
+ setIsImageLoaded(true);
252
+ },
253
+ ref: _image,
254
+ src: imgSrc,
255
+ style: { display: "none" },
256
+ });
250
257
  }
251
- return (
252
- <>
253
- <canvas
254
- height={outputHeight}
255
- ref={_canvas}
256
- style={canvasStyle}
257
- width={outputWidth}
258
- {...otherProps}
259
- />
260
- {img}
261
- </>
258
+ return createElement(
259
+ Fragment,
260
+ null,
261
+ createElement("canvas", {
262
+ height: outputHeight,
263
+ ref: _canvas,
264
+ style: canvasStyle,
265
+ width: outputWidth,
266
+ ...otherProps,
267
+ }),
268
+ img
262
269
  );
263
270
  }
@@ -1,5 +1,4 @@
1
- import type { JSX } from "react";
2
- import type { QRPropsSVG } from "../types/react";
1
+ import type { QRPropsSVG, ReactQRElement } from "../types/react";
3
2
  import { withElementFactory } from "./jsx/runtime";
4
3
  import { REACT_ELEMENTS } from "./react-elements";
5
4
  import { createQRCodeTree } from "./utils";
@@ -10,8 +9,8 @@ import { createQRCodeTree } from "./utils";
10
9
  * markup is exactly `toSVGString`'s. No hooks: it renders in React Server
11
10
  * Components too.
12
11
  */
13
- export function QRCodeSVG(props: QRPropsSVG): JSX.Element {
12
+ export function QRCodeSVG(props: QRPropsSVG): ReactQRElement {
14
13
  return withElementFactory(REACT_ELEMENTS, () =>
15
14
  createQRCodeTree(props)
16
- ) as unknown as JSX.Element;
15
+ ) as unknown as ReactQRElement;
17
16
  }
package/src/qr-code.tsx CHANGED
@@ -1,7 +1,9 @@
1
1
  // memo/useMemo are available in React Server Components, so QRCode can render
2
- // on the server; only QRCodeCanvas needs a client boundary.
3
- import { memo, useMemo } from "react";
4
- import type { QRCodePropsSVG } from "../types/react";
2
+ // on the server; only QRCodeCanvas needs a client boundary. createElement
3
+ // rather than JSX: compiled JSX would import react/jsx-runtime, which React
4
+ // has had only since 16.14.
5
+ import { createElement, memo, useMemo } from "react";
6
+ import type { QRCodePropsSVG, ReactQRElement } from "../types/react";
5
7
  import {
6
8
  getQRCodeData,
7
9
  getQRCodeSVGProps,
@@ -14,14 +16,14 @@ import { QRCodeSVG } from "./qr-code-svg";
14
16
  * `scale`). Any other prop (`className`, `aria-label`, `role`, `data-*`…)
15
17
  * goes on the `<svg>`, as with `QRCodeSVG`.
16
18
  */
17
- export const QRCode = memo((props: QRCodePropsSVG) => {
19
+ export const QRCode = memo((props: QRCodePropsSVG): ReactQRElement => {
18
20
  const qrData = useMemo(
19
21
  () => getQRCodeData(props),
20
22
  // biome-ignore lint/correctness/useExhaustiveDependencies: the layout reads exactly these props (QR_CODE_DATA_KEYS, the list getQRCodeData lays out from)
21
23
  QR_CODE_DATA_KEYS.map((key) => props[key])
22
24
  );
23
25
 
24
- return <QRCodeSVG {...getQRCodeSVGProps(props, qrData)} />;
26
+ return createElement(QRCodeSVG, getQRCodeSVGProps(props, qrData));
25
27
  });
26
28
 
27
29
  QRCode.displayName = "QRCode";
@@ -1,5 +1,4 @@
1
- import type { JSX } from "react";
2
- import type { QRFrameSVGProps } from "../types/react";
1
+ import type { QRFrameSVGProps, ReactQRElement } from "../types/react";
3
2
  import { createFrameTree } from "./frame";
4
3
  import { type QRNode, withElementFactory } from "./jsx/runtime";
5
4
  import { REACT_ELEMENTS } from "./react-elements";
@@ -16,11 +15,11 @@ export function QRFrameSVG({
16
15
  bgColor,
17
16
  slot,
18
17
  ...svgProps
19
- }: QRFrameSVGProps): JSX.Element {
18
+ }: QRFrameSVGProps): ReactQRElement {
20
19
  // Drawn with React's elements, the tree's nodes are React elements: the
21
20
  // slot is placed with React's cloneElement.
22
21
  const node = slot as unknown as QRNode | undefined;
23
22
  return withElementFactory(REACT_ELEMENTS, () =>
24
23
  createFrameTree({ frame, size, bgColor, slot: node, svgProps })
25
- ) as unknown as JSX.Element;
24
+ ) as unknown as ReactQRElement;
26
25
  }
package/src/rasterize.ts CHANGED
@@ -203,10 +203,23 @@ export async function rasterizeSvgToPng(
203
203
  }
204
204
  // fitTo "original" (the default) renders at the SVG's own width and
205
205
  // height, so the PNG is exactly as large as the SVG markup says.
206
- const rendered = new Resvg(svg).render();
206
+ const rendered = new Resvg(svg, {
207
+ font: { loadSystemFonts: svgNeedsFonts(svg) },
208
+ }).render();
207
209
  return new Uint8Array(rendered.asPng());
208
210
  }
209
211
 
212
+ /**
213
+ * Whether drawing `svg` can need a font: it has text (a frame's label) or
214
+ * embeds an SVG image (a logo, which may have text of its own). resvg reads
215
+ * every system font each time it's created when asked to (about 50 ms on a
216
+ * Mac, the most of a plain code's render), and nothing else in a code needs
217
+ * them.
218
+ */
219
+ export function svgNeedsFonts(svg: string): boolean {
220
+ return svg.includes("<text") || svg.includes("image/svg+xml");
221
+ }
222
+
210
223
  const describeError = (error: unknown) =>
211
224
  error instanceof Error ? error.message : String(error);
212
225
 
@@ -1,13 +1,51 @@
1
1
  // React's element functions, plugged into the renderer (jsx/runtime.ts) when
2
2
  // a React component draws the code, so it builds React elements directly.
3
+ // Only `createElement`, never react/jsx-runtime, which React has had only
4
+ // since 16.14: the React entry works from React 16.8.
3
5
  import { cloneElement, createElement, Fragment } from "react";
4
- import { jsx, jsxs } from "react/jsx-runtime";
5
6
  import type { ElementFactory } from "./jsx/runtime";
6
7
 
8
+ const createReactElement: ElementFactory["createElement"] = createElement;
9
+
10
+ /** The props without their children, with the key in them if there is one. */
11
+ function configOf(props: object | null, key: unknown) {
12
+ const { children: _children, ...config } = (props ?? {}) as {
13
+ children?: unknown;
14
+ };
15
+ return key === undefined ? config : { ...config, key };
16
+ }
17
+
18
+ /**
19
+ * The automatic runtime's `jsx(type, props, key)` through `createElement`.
20
+ * The children go in as `createElement`'s argument, not inside the props:
21
+ * every React checks the keys of children passed that way exactly as `jsx`
22
+ * checks its `props.children`.
23
+ */
24
+ const jsx: ElementFactory["jsx"] = (type, props, key) =>
25
+ props && "children" in props
26
+ ? createReactElement(
27
+ type,
28
+ configOf(props, key),
29
+ (props as { children: unknown }).children
30
+ )
31
+ : createReactElement(type, configOf(props, key));
32
+
33
+ /**
34
+ * `jsxs`, for children written out in the source (a static array): each
35
+ * child is an argument of its own, as compiled classic JSX passes them, so
36
+ * no React asks them for keys, as with `jsxs`.
37
+ */
38
+ const jsxs: ElementFactory["jsxs"] = (type, props, key) => {
39
+ const children = (props as { children?: unknown } | null)?.children;
40
+ return Array.isArray(children)
41
+ ? createReactElement(type, configOf(props, key), ...children)
42
+ : jsx(type, props, key);
43
+ };
44
+
7
45
  export const REACT_ELEMENTS: ElementFactory = {
8
46
  jsx,
9
47
  jsxs,
10
- createElement,
48
+ createElement: createReactElement,
11
49
  cloneElement: cloneElement as ElementFactory["cloneElement"],
12
50
  Fragment,
13
51
  };