qr-code-styling-node 1.7.0 → 1.8.0

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.
@@ -521,6 +521,398 @@ var QRCornerDot = class {
521
521
  }
522
522
  };
523
523
 
524
+ // src/tools/svgDom.ts
525
+ var SVG_NS = "http://www.w3.org/2000/svg";
526
+ var escapeAttribute = (value) => value.replace(/[&<>"\t\n\r]/g, (char) => {
527
+ switch (char) {
528
+ case "&":
529
+ return "&amp;";
530
+ case "<":
531
+ return "&lt;";
532
+ case ">":
533
+ return "&gt;";
534
+ case '"':
535
+ return "&quot;";
536
+ case " ":
537
+ return "&#x9;";
538
+ case "\n":
539
+ return "&#xA;";
540
+ default:
541
+ return "&#xD;";
542
+ }
543
+ });
544
+ var VirtualSVGElement = class _VirtualSVGElement {
545
+ constructor(tagName) {
546
+ this.namespaceURI = SVG_NS;
547
+ this.parentNode = null;
548
+ this.childNodes = [];
549
+ this._attributes = /* @__PURE__ */ new Map();
550
+ this.tagName = tagName;
551
+ }
552
+ get nodeName() {
553
+ return this.tagName;
554
+ }
555
+ setAttribute(name, value) {
556
+ this._attributes.set(name, String(value));
557
+ }
558
+ getAttribute(name) {
559
+ const value = this._attributes.get(name);
560
+ return value === void 0 ? null : value;
561
+ }
562
+ removeAttribute(name) {
563
+ this._attributes.delete(name);
564
+ }
565
+ appendChild(child) {
566
+ if (child.parentNode) child.parentNode.removeChild(child);
567
+ child.parentNode = this;
568
+ this.childNodes.push(child);
569
+ return child;
570
+ }
571
+ removeChild(child) {
572
+ const index = this.childNodes.indexOf(child);
573
+ if (index !== -1) this.childNodes.splice(index, 1);
574
+ child.parentNode = null;
575
+ return child;
576
+ }
577
+ replaceChild(newChild, oldChild) {
578
+ const index = this.childNodes.indexOf(oldChild);
579
+ if (index !== -1) {
580
+ if (newChild.parentNode) newChild.parentNode.removeChild(newChild);
581
+ this.childNodes[index] = newChild;
582
+ newChild.parentNode = this;
583
+ oldChild.parentNode = null;
584
+ }
585
+ return oldChild;
586
+ }
587
+ cloneNode(deep = false) {
588
+ const clone = new _VirtualSVGElement(this.tagName);
589
+ this._attributes.forEach((value, name) => clone.setAttribute(name, value));
590
+ if (deep) this.childNodes.forEach((child) => clone.appendChild(child.cloneNode(true)));
591
+ return clone;
592
+ }
593
+ serialize(isRoot = true) {
594
+ let markup = `<${this.tagName}`;
595
+ if (isRoot) markup += ` xmlns="${SVG_NS}"`;
596
+ this._attributes.forEach((value, name) => {
597
+ markup += ` ${name}="${escapeAttribute(value)}"`;
598
+ });
599
+ if (!this.childNodes.length) return `${markup}/>`;
600
+ markup += ">";
601
+ for (const child of this.childNodes) markup += child.serialize(false);
602
+ return `${markup}</${this.tagName}>`;
603
+ }
604
+ };
605
+ var VirtualXMLSerializer = class {
606
+ serializeToString(node) {
607
+ return node.serialize(true);
608
+ }
609
+ };
610
+ var VirtualDocument = class {
611
+ createElementNS(_namespace, tagName) {
612
+ return new VirtualSVGElement(tagName);
613
+ }
614
+ };
615
+ var createVirtualWindow = () => ({
616
+ document: new VirtualDocument(),
617
+ XMLSerializer: VirtualXMLSerializer,
618
+ isVirtual: true
619
+ });
620
+ var hasBrowserDom = () => typeof window !== "undefined" && typeof document !== "undefined" && typeof document.createElementNS === "function";
621
+
622
+ // src/art/functionModules.ts
623
+ var ALIGNMENT_POSITIONS = [
624
+ [],
625
+ [6, 18],
626
+ [6, 22],
627
+ [6, 26],
628
+ [6, 30],
629
+ [6, 34],
630
+ [6, 22, 38],
631
+ [6, 24, 42],
632
+ [6, 26, 46],
633
+ [6, 28, 50],
634
+ [6, 30, 54],
635
+ [6, 32, 58],
636
+ [6, 34, 62],
637
+ [6, 26, 46, 66],
638
+ [6, 26, 48, 70],
639
+ [6, 26, 50, 74],
640
+ [6, 30, 54, 78],
641
+ [6, 30, 56, 82],
642
+ [6, 30, 58, 86],
643
+ [6, 34, 62, 90],
644
+ [6, 28, 50, 72, 94],
645
+ [6, 26, 50, 74, 98],
646
+ [6, 30, 54, 78, 102],
647
+ [6, 28, 54, 80, 106],
648
+ [6, 32, 58, 84, 110],
649
+ [6, 30, 58, 86, 114],
650
+ [6, 34, 62, 90, 118],
651
+ [6, 26, 50, 74, 98, 122],
652
+ [6, 30, 54, 78, 102, 126],
653
+ [6, 26, 52, 78, 104, 130],
654
+ [6, 30, 56, 82, 108, 134],
655
+ [6, 34, 60, 86, 112, 138],
656
+ [6, 30, 58, 86, 114, 142],
657
+ [6, 34, 62, 90, 118, 146],
658
+ [6, 30, 54, 78, 102, 126, 150],
659
+ [6, 24, 50, 76, 102, 128, 154],
660
+ [6, 28, 54, 80, 106, 132, 158],
661
+ [6, 32, 58, 84, 110, 136, 162],
662
+ [6, 26, 54, 82, 110, 138, 166],
663
+ [6, 30, 58, 86, 114, 142, 170]
664
+ ];
665
+ function getModuleRoles(count) {
666
+ const version = (count - 17) / 4;
667
+ const roles = new Uint8Array(count * count);
668
+ const mark = (row, col, role) => {
669
+ if (row >= 0 && col >= 0 && row < count && col < count) roles[row * count + col] = role;
670
+ };
671
+ for (let r = 0; r < 8; r++) {
672
+ for (let c = 0; c < 8; c++) {
673
+ mark(r, c, 2);
674
+ mark(r, count - 1 - c, 2);
675
+ mark(count - 1 - r, c, 2);
676
+ }
677
+ }
678
+ for (let i = 8; i < count - 8; i++) {
679
+ mark(6, i, 1);
680
+ mark(i, 6, 1);
681
+ }
682
+ const positions = ALIGNMENT_POSITIONS[version - 1] || [];
683
+ positions.forEach(
684
+ (row) => positions.forEach((col) => {
685
+ if (roles[row * count + col] === 2) return;
686
+ for (let r = -2; r <= 2; r++) for (let c = -2; c <= 2; c++) mark(row + r, col + c, 1);
687
+ })
688
+ );
689
+ for (let i = 0; i < 9; i++) {
690
+ if (roles[8 * count + i] !== 2) mark(8, i, 1);
691
+ if (roles[i * count + 8] !== 2) mark(i, 8, 1);
692
+ }
693
+ for (let i = 0; i < 8; i++) {
694
+ mark(8, count - 1 - i, 1);
695
+ mark(count - 1 - i, 8, 1);
696
+ }
697
+ if (version >= 7) {
698
+ for (let a = 0; a < 6; a++) {
699
+ for (let b = 0; b < 3; b++) {
700
+ mark(a, count - 11 + b, 1);
701
+ mark(count - 11 + b, a, 1);
702
+ }
703
+ }
704
+ }
705
+ return (row, col) => ["data", "function", "finder"][roles[row * count + col]];
706
+ }
707
+
708
+ // src/art/art.ts
709
+ var DEFAULT_CONTRAST = { halftone: 0.5, colorize: 0.7 };
710
+ var clamp01 = (value) => Math.min(1, Math.max(0, value));
711
+ var getContrast = (art) => clamp01(
712
+ typeof art.contrast === "number" && !isNaN(art.contrast) ? art.contrast : DEFAULT_CONTRAST[art.mode === "colorize" ? "colorize" : "halftone"]
713
+ );
714
+ var getLimits = (contrast) => ({
715
+ dark: 0.45 - 0.35 * contrast,
716
+ light: 0.55 + 0.35 * contrast
717
+ });
718
+ var luminance = ([r, g, b]) => (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
719
+ var toDark = (color, limit) => {
720
+ const lum = luminance(color);
721
+ if (lum <= limit) return color;
722
+ const factor = limit / lum;
723
+ return [color[0] * factor, color[1] * factor, color[2] * factor];
724
+ };
725
+ var toLight = (color, limit) => {
726
+ const lum = luminance(color);
727
+ if (lum >= limit) return color;
728
+ const t = (limit - lum) / (1 - lum);
729
+ return [color[0] + (255 - color[0]) * t, color[1] + (255 - color[1]) * t, color[2] + (255 - color[2]) * t];
730
+ };
731
+ var css = ([r, g, b]) => `rgb(${Math.round(r)},${Math.round(g)},${Math.round(b)})`;
732
+ function createCanvas(options, width, height) {
733
+ var _a;
734
+ if ((_a = options.nodeCanvas) == null ? void 0 : _a.createCanvas) {
735
+ return options.nodeCanvas.createCanvas(width, height);
736
+ }
737
+ if (hasBrowserDom()) {
738
+ const canvas = document.createElement("canvas");
739
+ canvas.width = width;
740
+ canvas.height = height;
741
+ return canvas;
742
+ }
743
+ throw new Error('QR art reads the picture through a canvas. On a server, pass { nodeCanvas: require("canvas") }.');
744
+ }
745
+ function loadPicture(options, art) {
746
+ var _a;
747
+ if ((_a = options.nodeCanvas) == null ? void 0 : _a.loadImage) {
748
+ return options.nodeCanvas.loadImage(art.image);
749
+ }
750
+ return new Promise((resolve, reject) => {
751
+ var _a2;
752
+ const image = new Image();
753
+ const crossOrigin = (_a2 = art.crossOrigin) != null ? _a2 : options.imageOptions.crossOrigin;
754
+ if (typeof crossOrigin === "string") image.crossOrigin = crossOrigin;
755
+ image.onload = () => resolve(image);
756
+ image.onerror = () => reject(new Error(`QR art: failed to load image ${art.image}`));
757
+ image.src = art.image;
758
+ });
759
+ }
760
+ function drawCover(context, image, width, height) {
761
+ const scale = Math.max(width / image.width, height / image.height);
762
+ const sourceWidth = width / scale;
763
+ const sourceHeight = height / scale;
764
+ context.drawImage(
765
+ image,
766
+ (image.width - sourceWidth) / 2,
767
+ (image.height - sourceHeight) / 2,
768
+ sourceWidth,
769
+ sourceHeight,
770
+ 0,
771
+ 0,
772
+ width,
773
+ height
774
+ );
775
+ }
776
+ function readPixels(options, image, width, height) {
777
+ const canvas = createCanvas(options, width, height);
778
+ const context = canvas.getContext("2d");
779
+ context.imageSmoothingEnabled = true;
780
+ drawCover(context, image, width, height);
781
+ return { canvas, context, data: context.getImageData(0, 0, width, height).data };
782
+ }
783
+ function parseColor(options, color, fallback) {
784
+ if (!color) return fallback;
785
+ const canvas = createCanvas(options, 1, 1);
786
+ const context = canvas.getContext("2d");
787
+ context.fillStyle = color;
788
+ context.fillRect(0, 0, 1, 1);
789
+ const pixel = context.getImageData(0, 0, 1, 1).data;
790
+ return pixel[3] === 0 ? fallback : [pixel[0], pixel[1], pixel[2]];
791
+ }
792
+ function planHalftone(options, art, image, qr, area, filter) {
793
+ var _a, _b, _c, _d, _e, _f;
794
+ const { count, dotSize } = area;
795
+ const sub = 3;
796
+ const cells = count * sub;
797
+ const pixels = readPixels(options, image, cells, cells).data;
798
+ const role = getModuleRoles(count);
799
+ const contrast = getContrast(art);
800
+ const limits = getLimits(contrast);
801
+ const strong = getLimits(Math.max(contrast, 0.8));
802
+ const darkColor = parseColor(
803
+ options,
804
+ (_c = (_b = (_a = options.dotsOptions.gradient) == null ? void 0 : _a.colorStops[0]) == null ? void 0 : _b.color) != null ? _c : options.dotsOptions.color,
805
+ [0, 0, 0]
806
+ );
807
+ const lightColor = parseColor(
808
+ options,
809
+ (_f = (_e = (_d = options.backgroundOptions.gradient) == null ? void 0 : _d.colorStops[0]) == null ? void 0 : _e.color) != null ? _f : options.backgroundOptions.color,
810
+ [255, 255, 255]
811
+ );
812
+ const pixelAt = (cx, cy) => {
813
+ const index = (cy * cells + cx) * 4;
814
+ return [pixels[index], pixels[index + 1], pixels[index + 2]];
815
+ };
816
+ const shade = (color, dark, limit) => {
817
+ if (!art.colored) return css(dark ? darkColor : lightColor);
818
+ return css(dark ? toDark(color, limit.dark) : toLight(color, limit.light));
819
+ };
820
+ const grid = new Array(cells * cells).fill(null);
821
+ const centers = [];
822
+ const bounds = [0, 1, 2, 3].map((k) => Math.round(k * dotSize / sub));
823
+ const centerSize = Math.max(1, Math.round(dotSize * (1 / 3 + contrast / 3)));
824
+ const centerOffset = (dotSize - centerSize) / 2;
825
+ for (let i = 0; i < count; i++) {
826
+ for (let j = 0; j < count; j++) {
827
+ const moduleRole = role(i, j);
828
+ if (moduleRole === "finder" || !filter(i, j)) continue;
829
+ const dark = qr.isDark(i, j);
830
+ for (let a = 0; a < sub; a++) {
831
+ for (let b = 0; b < sub; b++) {
832
+ const cx = i * sub + a;
833
+ const cy = j * sub + b;
834
+ const color = pixelAt(cx, cy);
835
+ const cellDark = moduleRole === "function" ? dark : luminance(color) < 0.5;
836
+ grid[cy * cells + cx] = shade(color, cellDark, moduleRole === "function" ? strong : limits);
837
+ }
838
+ }
839
+ if (moduleRole === "data") {
840
+ const center = pixelAt(i * sub + 1, j * sub + 1);
841
+ centers.push({
842
+ x: area.x + i * dotSize + centerOffset,
843
+ y: area.y + j * dotSize + centerOffset,
844
+ width: centerSize,
845
+ height: centerSize,
846
+ color: shade(center, dark, strong)
847
+ });
848
+ }
849
+ }
850
+ }
851
+ const rects = [];
852
+ for (let cy = 0; cy < cells; cy++) {
853
+ let cx = 0;
854
+ while (cx < cells) {
855
+ const color = grid[cy * cells + cx];
856
+ let end = cx + 1;
857
+ while (end < cells && grid[cy * cells + end] === color) end++;
858
+ if (color) {
859
+ const moduleX0 = Math.floor(cx / sub);
860
+ const moduleX1 = Math.floor((end - 1) / sub);
861
+ const moduleY = Math.floor(cy / sub);
862
+ const x0 = area.x + moduleX0 * dotSize + bounds[cx % sub];
863
+ const x1 = area.x + moduleX1 * dotSize + bounds[(end - 1) % sub + 1];
864
+ const y0 = area.y + moduleY * dotSize + bounds[cy % sub];
865
+ const y1 = area.y + moduleY * dotSize + bounds[cy % sub + 1];
866
+ rects.push({ x: x0, y: y0, width: x1 - x0, height: y1 - y0, color });
867
+ }
868
+ cx = end;
869
+ }
870
+ }
871
+ return { mode: "halftone", rects: rects.concat(centers) };
872
+ }
873
+ function prepareColorize(options, art, image, area) {
874
+ const size = area.count * area.dotSize;
875
+ const { data } = readPixels(options, image, size, size);
876
+ const limits = getLimits(getContrast(art));
877
+ const makeLayer = (transform) => {
878
+ const layer = createCanvas(options, options.width, options.height);
879
+ const context = layer.getContext("2d");
880
+ const imageData = context.createImageData(size, size);
881
+ for (let index = 0; index < data.length; index += 4) {
882
+ const [r, g, b] = transform([data[index], data[index + 1], data[index + 2]]);
883
+ imageData.data[index] = r;
884
+ imageData.data[index + 1] = g;
885
+ imageData.data[index + 2] = b;
886
+ imageData.data[index + 3] = 255;
887
+ }
888
+ context.putImageData(imageData, area.x, area.y);
889
+ return layer;
890
+ };
891
+ return {
892
+ mode: "colorize",
893
+ dots: makeLayer((color) => toDark(color, limits.dark)),
894
+ background: art.background === false ? void 0 : makeLayer((color) => toLight(color, limits.light))
895
+ };
896
+ }
897
+ function getQRArea(options, count) {
898
+ const minSize = Math.min(options.width, options.height) - options.margin * 2;
899
+ const dotSize = Math.floor(minSize / count);
900
+ return {
901
+ count,
902
+ dotSize,
903
+ x: Math.floor((options.width - count * dotSize) / 2),
904
+ y: Math.floor((options.height - count * dotSize) / 2)
905
+ };
906
+ }
907
+ async function prepareArt(options, qr, filter) {
908
+ const art = options.art;
909
+ if (!art.image) throw new Error("QR art needs art.image");
910
+ createCanvas(options, 1, 1);
911
+ const image = await loadPicture(options, art);
912
+ const area = getQRArea(options, qr.getModuleCount());
913
+ return art.mode === "colorize" ? prepareColorize(options, art, image, area) : planHalftone(options, art, image, qr, area, filter);
914
+ }
915
+
524
916
  // src/constants/gradientTypes.ts
525
917
  var gradientTypes_default = {
526
918
  radial: "radial",
@@ -578,6 +970,7 @@ var QRCanvas = class {
578
970
  }
579
971
  }
580
972
  async drawQR(qr) {
973
+ var _a;
581
974
  const count = qr.getModuleCount();
582
975
  const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
583
976
  const dotSize = Math.floor(minSize / count);
@@ -602,23 +995,39 @@ var QRCanvas = class {
602
995
  dotSize
603
996
  });
604
997
  }
605
- this.clear();
606
- this.drawBackground();
607
- this.drawDots((i, j) => {
608
- var _a, _b, _c, _d, _e, _f;
998
+ const filter = (i, j) => {
999
+ var _a2, _b, _c, _d, _e, _f;
609
1000
  if (this._options.imageOptions.hideBackgroundDots) {
610
1001
  if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
611
1002
  return false;
612
1003
  }
613
1004
  }
614
- if (((_a = squareMask[i]) == null ? void 0 : _a[j]) || ((_b = squareMask[i - count + 7]) == null ? void 0 : _b[j]) || ((_c = squareMask[i]) == null ? void 0 : _c[j - count + 7])) {
1005
+ if (((_a2 = squareMask[i]) == null ? void 0 : _a2[j]) || ((_b = squareMask[i - count + 7]) == null ? void 0 : _b[j]) || ((_c = squareMask[i]) == null ? void 0 : _c[j - count + 7])) {
615
1006
  return false;
616
1007
  }
617
1008
  if (((_d = dotMask[i]) == null ? void 0 : _d[j]) || ((_e = dotMask[i - count + 7]) == null ? void 0 : _e[j]) || ((_f = dotMask[i]) == null ? void 0 : _f[j - count + 7])) {
618
1009
  return false;
619
1010
  }
620
1011
  return true;
621
- });
1012
+ };
1013
+ let art;
1014
+ if (this._options.art) {
1015
+ art = await prepareArt(this._options, qr, filter);
1016
+ }
1017
+ this.clear();
1018
+ this.drawBackground();
1019
+ if ((art == null ? void 0 : art.mode) === "halftone") {
1020
+ this._drawArtRects(art.rects);
1021
+ this.drawDots(() => false);
1022
+ } else {
1023
+ if ((art == null ? void 0 : art.mode) === "colorize") {
1024
+ const context = this.context;
1025
+ if (art.background) context.drawImage(art.background, 0, 0);
1026
+ this._dotsFill = (_a = context.createPattern(art.dots, "no-repeat")) != null ? _a : void 0;
1027
+ }
1028
+ this.drawDots(filter);
1029
+ this._dotsFill = void 0;
1030
+ }
622
1031
  this.drawCorners();
623
1032
  if (this._options.image) {
624
1033
  this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
@@ -704,8 +1113,19 @@ var QRCanvas = class {
704
1113
  } else if (options.dotsOptions.color) {
705
1114
  canvasContext.fillStyle = canvasContext.strokeStyle = options.dotsOptions.color;
706
1115
  }
1116
+ if (this._dotsFill) {
1117
+ canvasContext.fillStyle = canvasContext.strokeStyle = this._dotsFill;
1118
+ }
707
1119
  canvasContext.fill("evenodd");
708
1120
  }
1121
+ _drawArtRects(rects) {
1122
+ const context = this.context;
1123
+ if (!context) return;
1124
+ rects.forEach(({ x, y, width, height, color }) => {
1125
+ context.fillStyle = color;
1126
+ context.fillRect(x, y, width, height);
1127
+ });
1128
+ }
709
1129
  drawCorners(filter) {
710
1130
  if (!this._qr) {
711
1131
  throw "QR code is not defined";
@@ -1313,104 +1733,6 @@ var QRCornerDot2 = class {
1313
1733
  }
1314
1734
  };
1315
1735
 
1316
- // src/tools/svgDom.ts
1317
- var SVG_NS = "http://www.w3.org/2000/svg";
1318
- var escapeAttribute = (value) => value.replace(/[&<>"\t\n\r]/g, (char) => {
1319
- switch (char) {
1320
- case "&":
1321
- return "&amp;";
1322
- case "<":
1323
- return "&lt;";
1324
- case ">":
1325
- return "&gt;";
1326
- case '"':
1327
- return "&quot;";
1328
- case " ":
1329
- return "&#x9;";
1330
- case "\n":
1331
- return "&#xA;";
1332
- default:
1333
- return "&#xD;";
1334
- }
1335
- });
1336
- var VirtualSVGElement = class _VirtualSVGElement {
1337
- constructor(tagName) {
1338
- this.namespaceURI = SVG_NS;
1339
- this.parentNode = null;
1340
- this.childNodes = [];
1341
- this._attributes = /* @__PURE__ */ new Map();
1342
- this.tagName = tagName;
1343
- }
1344
- get nodeName() {
1345
- return this.tagName;
1346
- }
1347
- setAttribute(name, value) {
1348
- this._attributes.set(name, String(value));
1349
- }
1350
- getAttribute(name) {
1351
- const value = this._attributes.get(name);
1352
- return value === void 0 ? null : value;
1353
- }
1354
- removeAttribute(name) {
1355
- this._attributes.delete(name);
1356
- }
1357
- appendChild(child) {
1358
- if (child.parentNode) child.parentNode.removeChild(child);
1359
- child.parentNode = this;
1360
- this.childNodes.push(child);
1361
- return child;
1362
- }
1363
- removeChild(child) {
1364
- const index = this.childNodes.indexOf(child);
1365
- if (index !== -1) this.childNodes.splice(index, 1);
1366
- child.parentNode = null;
1367
- return child;
1368
- }
1369
- replaceChild(newChild, oldChild) {
1370
- const index = this.childNodes.indexOf(oldChild);
1371
- if (index !== -1) {
1372
- if (newChild.parentNode) newChild.parentNode.removeChild(newChild);
1373
- this.childNodes[index] = newChild;
1374
- newChild.parentNode = this;
1375
- oldChild.parentNode = null;
1376
- }
1377
- return oldChild;
1378
- }
1379
- cloneNode(deep = false) {
1380
- const clone = new _VirtualSVGElement(this.tagName);
1381
- this._attributes.forEach((value, name) => clone.setAttribute(name, value));
1382
- if (deep) this.childNodes.forEach((child) => clone.appendChild(child.cloneNode(true)));
1383
- return clone;
1384
- }
1385
- serialize(isRoot = true) {
1386
- let markup = `<${this.tagName}`;
1387
- if (isRoot) markup += ` xmlns="${SVG_NS}"`;
1388
- this._attributes.forEach((value, name) => {
1389
- markup += ` ${name}="${escapeAttribute(value)}"`;
1390
- });
1391
- if (!this.childNodes.length) return `${markup}/>`;
1392
- markup += ">";
1393
- for (const child of this.childNodes) markup += child.serialize(false);
1394
- return `${markup}</${this.tagName}>`;
1395
- }
1396
- };
1397
- var VirtualXMLSerializer = class {
1398
- serializeToString(node) {
1399
- return node.serialize(true);
1400
- }
1401
- };
1402
- var VirtualDocument = class {
1403
- createElementNS(_namespace, tagName) {
1404
- return new VirtualSVGElement(tagName);
1405
- }
1406
- };
1407
- var createVirtualWindow = () => ({
1408
- document: new VirtualDocument(),
1409
- XMLSerializer: VirtualXMLSerializer,
1410
- isVirtual: true
1411
- });
1412
- var hasBrowserDom = () => typeof window !== "undefined" && typeof document !== "undefined" && typeof document.createElementNS === "function";
1413
-
1414
1736
  // src/tools/imageSize.ts
1415
1737
  var decodeBase64 = (base64) => {
1416
1738
  if (typeof atob === "function") {
@@ -1634,9 +1956,7 @@ var QRSVG = class {
1634
1956
  dotSize
1635
1957
  });
1636
1958
  }
1637
- this.clear();
1638
- this.drawBackground();
1639
- this.drawDots((i, j) => {
1959
+ const filter = (i, j) => {
1640
1960
  var _a, _b, _c, _d, _e, _f;
1641
1961
  if (this._options.imageOptions.hideBackgroundDots) {
1642
1962
  if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
@@ -1650,7 +1970,24 @@ var QRSVG = class {
1650
1970
  return false;
1651
1971
  }
1652
1972
  return true;
1653
- });
1973
+ };
1974
+ let art;
1975
+ if (this._options.art) {
1976
+ art = await prepareArt(this._options, qr, filter);
1977
+ }
1978
+ this.clear();
1979
+ this.drawBackground();
1980
+ if ((art == null ? void 0 : art.mode) === "halftone") {
1981
+ this._drawArtRects(art.rects);
1982
+ this.drawDots(() => false);
1983
+ } else {
1984
+ if ((art == null ? void 0 : art.mode) === "colorize") {
1985
+ if (art.background) this._appendImage(art.background.toDataURL("image/png"), 0, 0);
1986
+ this._dotsImageHref = art.dots.toDataURL("image/png");
1987
+ }
1988
+ this.drawDots(filter);
1989
+ this._dotsImageHref = void 0;
1990
+ }
1654
1991
  this.drawCorners();
1655
1992
  if (this._options.image) {
1656
1993
  this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
@@ -1707,7 +2044,8 @@ var QRSVG = class {
1707
2044
  y: yBeginning,
1708
2045
  height: count * dotSize,
1709
2046
  width: count * dotSize,
1710
- name: "dot-color"
2047
+ name: "dot-color",
2048
+ imageHref: this._dotsImageHref
1711
2049
  });
1712
2050
  for (let i = 0; i < count; i++) {
1713
2051
  for (let j = 0; j < count; j++) {
@@ -1865,6 +2203,26 @@ var QRSVG = class {
1865
2203
  }
1866
2204
  });
1867
2205
  }
2206
+ _drawArtRects(rects) {
2207
+ rects.forEach(({ x, y, width, height, color }) => {
2208
+ const rect = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
2209
+ rect.setAttribute("x", String(x));
2210
+ rect.setAttribute("y", String(y));
2211
+ rect.setAttribute("width", String(width));
2212
+ rect.setAttribute("height", String(height));
2213
+ rect.setAttribute("fill", color);
2214
+ this._element.appendChild(rect);
2215
+ });
2216
+ }
2217
+ _appendImage(href, x, y) {
2218
+ const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
2219
+ image.setAttribute("href", href);
2220
+ image.setAttribute("x", String(x));
2221
+ image.setAttribute("y", String(y));
2222
+ image.setAttribute("width", String(this._options.width));
2223
+ image.setAttribute("height", String(this._options.height));
2224
+ this._element.appendChild(image);
2225
+ }
1868
2226
  imageToBlob() {
1869
2227
  var _a;
1870
2228
  if (!this._image) return;
@@ -1947,9 +2305,21 @@ var QRSVG = class {
1947
2305
  y,
1948
2306
  height,
1949
2307
  width,
1950
- name
2308
+ name,
2309
+ imageHref
1951
2310
  }) {
1952
2311
  const size = width > height ? width : height;
2312
+ if (imageHref) {
2313
+ const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
2314
+ image.setAttribute("href", imageHref);
2315
+ image.setAttribute("x", "0");
2316
+ image.setAttribute("y", "0");
2317
+ image.setAttribute("width", String(this._options.width));
2318
+ image.setAttribute("height", String(this._options.height));
2319
+ image.setAttribute("clip-path", `url('#clip-path-${name}')`);
2320
+ this._element.appendChild(image);
2321
+ return;
2322
+ }
1953
2323
  const rect = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
1954
2324
  rect.setAttribute("x", String(x));
1955
2325
  rect.setAttribute("y", String(y));
@@ -2275,6 +2645,67 @@ ${serializer.serializeToString(svg.getElement())}`;
2275
2645
  const png = new Resvg(_QRCodeStyling._normalizeSVGForResvg(svgString), { fitTo: { mode: "original" } }).render().asPng();
2276
2646
  return typeof Buffer !== "undefined" ? Buffer.from(png) : png;
2277
2647
  }
2648
+ /** Draws the current QR code to RGBA pixels, using a canvas or, failing that, resvg. */
2649
+ async _getPixels() {
2650
+ if (this._canUseCanvas()) {
2651
+ const canvas = (await this._getQRStylingElement("png")).getCanvas();
2652
+ const context = canvas.getContext("2d");
2653
+ return {
2654
+ data: context.getImageData(0, 0, canvas.width, canvas.height).data,
2655
+ width: canvas.width,
2656
+ height: canvas.height
2657
+ };
2658
+ }
2659
+ if (this._options.resvg) {
2660
+ const { svgString } = await this._getSVGString();
2661
+ const rendered = new this._options.resvg(_QRCodeStyling._normalizeSVGForResvg(svgString), {
2662
+ fitTo: { mode: "original" }
2663
+ }).render();
2664
+ return { data: new Uint8ClampedArray(rendered.pixels), width: rendered.width, height: rendered.height };
2665
+ }
2666
+ throw new Error(
2667
+ "verify() needs to draw the QR code: in a browser this works as is; on a server pass nodeCanvas or resvg."
2668
+ );
2669
+ }
2670
+ async _decode(decoder) {
2671
+ const { data, width, height } = await this._getPixels();
2672
+ const pad = Math.max(16, Math.round(Math.min(width, height) / 8));
2673
+ const paddedWidth = width + pad * 2;
2674
+ const paddedHeight = height + pad * 2;
2675
+ const padded = new Uint8ClampedArray(paddedWidth * paddedHeight * 4).fill(255);
2676
+ for (let y = 0; y < height; y++) {
2677
+ padded.set(data.subarray(y * width * 4, (y + 1) * width * 4), ((y + pad) * paddedWidth + pad) * 4);
2678
+ }
2679
+ const result = decoder(padded, paddedWidth, paddedHeight, { inversionAttempts: "attemptBoth" });
2680
+ return result ? result.data : void 0;
2681
+ }
2682
+ /**
2683
+ * Checks that the QR code can be scanned by decoding the rendered picture.
2684
+ * With `adjust: true` and QR art, raises art.contrast step by step until it scans.
2685
+ */
2686
+ async verify({ decoder, adjust = false }) {
2687
+ if (!this._qr) throw "QR code is empty";
2688
+ if (typeof decoder !== "function")
2689
+ throw new Error("verify() needs a decoder, for example jsQR from the jsqr package");
2690
+ let attempts = 1;
2691
+ let decoded = await this._decode(decoder);
2692
+ const art = this._options.art;
2693
+ if (decoded !== this._options.data && adjust && art) {
2694
+ const start = getContrast(art);
2695
+ for (const contrast of [0.65, 0.8, 0.9, 1].filter((value) => value > start)) {
2696
+ attempts++;
2697
+ this.update({ art: { ...art, contrast } });
2698
+ decoded = await this._decode(decoder);
2699
+ if (decoded === this._options.data) break;
2700
+ }
2701
+ }
2702
+ return {
2703
+ readable: decoded === this._options.data,
2704
+ decoded,
2705
+ contrast: this._options.art ? getContrast(this._options.art) : void 0,
2706
+ attempts
2707
+ };
2708
+ }
2278
2709
  async getRawData(extension = "png") {
2279
2710
  if (!this._qr) throw "QR code is empty";
2280
2711
  if (extension.toLowerCase() !== "svg" && !this._canUseCanvas() && this._options.resvg) {