qr-code-styling-node 1.6.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.
- package/README.md +103 -5
- package/lib/art/art.d.ts +51 -0
- package/lib/art/functionModules.d.ts +7 -0
- package/lib/core/QRCanvas.d.ts +4 -0
- package/lib/core/QRCodeStyling.d.ts +27 -1
- package/lib/core/QRSVG.d.ts +10 -1
- package/lib/qr-code-styling.common.js +1 -1
- package/lib/qr-code-styling.common.js.map +1 -1
- package/lib/qr-code-styling.js +1 -1
- package/lib/qr-code-styling.js.map +1 -1
- package/lib/qr-code-styling.mjs +764 -16
- package/lib/qr-code-styling.mjs.map +4 -4
- package/lib/tools/imageSize.d.ts +17 -0
- package/lib/tools/svgDom.d.ts +37 -0
- package/lib/types/index.d.ts +60 -0
- package/package.json +6 -2
package/lib/qr-code-styling.mjs
CHANGED
|
@@ -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 "&";
|
|
530
|
+
case "<":
|
|
531
|
+
return "<";
|
|
532
|
+
case ">":
|
|
533
|
+
return ">";
|
|
534
|
+
case '"':
|
|
535
|
+
return """;
|
|
536
|
+
case " ":
|
|
537
|
+
return "	";
|
|
538
|
+
case "\n":
|
|
539
|
+
return "
";
|
|
540
|
+
default:
|
|
541
|
+
return "
";
|
|
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
|
-
|
|
606
|
-
|
|
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 (((
|
|
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,6 +1733,136 @@ var QRCornerDot2 = class {
|
|
|
1313
1733
|
}
|
|
1314
1734
|
};
|
|
1315
1735
|
|
|
1736
|
+
// src/tools/imageSize.ts
|
|
1737
|
+
var decodeBase64 = (base64) => {
|
|
1738
|
+
if (typeof atob === "function") {
|
|
1739
|
+
const binary = atob(base64);
|
|
1740
|
+
const bytes = new Uint8Array(binary.length);
|
|
1741
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
1742
|
+
return bytes;
|
|
1743
|
+
}
|
|
1744
|
+
return new Uint8Array(Buffer.from(base64, "base64"));
|
|
1745
|
+
};
|
|
1746
|
+
var encodeBase64 = (bytes) => {
|
|
1747
|
+
if (typeof btoa === "function") {
|
|
1748
|
+
let binary = "";
|
|
1749
|
+
const chunk = 32768;
|
|
1750
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
1751
|
+
binary += String.fromCharCode.apply(null, Array.from(bytes.subarray(i, i + chunk)));
|
|
1752
|
+
}
|
|
1753
|
+
return btoa(binary);
|
|
1754
|
+
}
|
|
1755
|
+
return Buffer.from(bytes).toString("base64");
|
|
1756
|
+
};
|
|
1757
|
+
var sniffMimeType = (bytes) => {
|
|
1758
|
+
if (bytes[0] === 137 && bytes[1] === 80 && bytes[2] === 78 && bytes[3] === 71) return "image/png";
|
|
1759
|
+
if (bytes[0] === 255 && bytes[1] === 216) return "image/jpeg";
|
|
1760
|
+
if (bytes[0] === 71 && bytes[1] === 73 && bytes[2] === 70) return "image/gif";
|
|
1761
|
+
if (ascii(bytes, 0, 4) === "RIFF" && ascii(bytes, 8, 12) === "WEBP") return "image/webp";
|
|
1762
|
+
const head = ascii(bytes, 0, Math.min(bytes.length, 1024)).toLowerCase();
|
|
1763
|
+
if (head.includes("<svg")) return "image/svg+xml";
|
|
1764
|
+
return "application/octet-stream";
|
|
1765
|
+
};
|
|
1766
|
+
var loadImageData = async (source) => {
|
|
1767
|
+
var _a;
|
|
1768
|
+
const dataUrl = /^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,([\s\S]*)$/.exec(source);
|
|
1769
|
+
if (dataUrl) {
|
|
1770
|
+
const [, mimeType, , base64, payload] = dataUrl;
|
|
1771
|
+
const bytes2 = base64 ? decodeBase64(payload) : new TextEncoder().encode(decodeURIComponent(payload));
|
|
1772
|
+
return { bytes: bytes2, mimeType: mimeType || sniffMimeType(bytes2) };
|
|
1773
|
+
}
|
|
1774
|
+
if (typeof fetch !== "function") {
|
|
1775
|
+
throw new Error("Loading an image URL needs a global fetch (Node.js 18+), or pass the image as a data URL.");
|
|
1776
|
+
}
|
|
1777
|
+
const response = await fetch(source);
|
|
1778
|
+
if (!response.ok) throw new Error(`Failed to load image ${source}: HTTP ${response.status}`);
|
|
1779
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
1780
|
+
const header = (_a = response.headers.get("content-type")) == null ? void 0 : _a.split(";")[0].trim();
|
|
1781
|
+
return { bytes, mimeType: header && header.startsWith("image/") ? header : sniffMimeType(bytes) };
|
|
1782
|
+
};
|
|
1783
|
+
var toDataUrl = ({ bytes, mimeType }) => `data:${mimeType};base64,${encodeBase64(bytes)}`;
|
|
1784
|
+
function ascii(bytes, start, end) {
|
|
1785
|
+
let result = "";
|
|
1786
|
+
for (let i = start; i < end && i < bytes.length; i++) result += String.fromCharCode(bytes[i]);
|
|
1787
|
+
return result;
|
|
1788
|
+
}
|
|
1789
|
+
var uint16BE = (b, i) => b[i] << 8 | b[i + 1];
|
|
1790
|
+
var uint16LE = (b, i) => b[i] | b[i + 1] << 8;
|
|
1791
|
+
var uint24LE = (b, i) => b[i] | b[i + 1] << 8 | b[i + 2] << 16;
|
|
1792
|
+
var uint32BE = (b, i) => (b[i] << 24 | b[i + 1] << 16 | b[i + 2] << 8 | b[i + 3]) >>> 0;
|
|
1793
|
+
var svgLength = (value) => {
|
|
1794
|
+
if (!value) return void 0;
|
|
1795
|
+
const match = /^\s*([\d.]+)\s*(px)?\s*$/.exec(value);
|
|
1796
|
+
return match ? parseFloat(match[1]) : void 0;
|
|
1797
|
+
};
|
|
1798
|
+
var svgSize = (bytes) => {
|
|
1799
|
+
var _a, _b;
|
|
1800
|
+
const text = new TextDecoder().decode(bytes);
|
|
1801
|
+
const tag = (_a = /<svg\b[^>]*>/i.exec(text)) == null ? void 0 : _a[0];
|
|
1802
|
+
if (!tag) return null;
|
|
1803
|
+
const attr = (name) => {
|
|
1804
|
+
var _a2;
|
|
1805
|
+
return (_a2 = new RegExp(`\\s${name}\\s*=\\s*(["'])([^"']*)\\1`, "i").exec(tag)) == null ? void 0 : _a2[2];
|
|
1806
|
+
};
|
|
1807
|
+
let width = svgLength(attr("width"));
|
|
1808
|
+
let height = svgLength(attr("height"));
|
|
1809
|
+
const viewBox = (_b = attr("viewBox")) == null ? void 0 : _b.trim().split(/[\s,]+/).map(Number);
|
|
1810
|
+
if (viewBox && viewBox.length === 4 && viewBox[2] > 0 && viewBox[3] > 0) {
|
|
1811
|
+
const ratio = viewBox[2] / viewBox[3];
|
|
1812
|
+
if (width === void 0 && height === void 0) {
|
|
1813
|
+
width = viewBox[2];
|
|
1814
|
+
height = viewBox[3];
|
|
1815
|
+
} else if (width === void 0 && height !== void 0) {
|
|
1816
|
+
width = height * ratio;
|
|
1817
|
+
} else if (height === void 0 && width !== void 0) {
|
|
1818
|
+
height = width / ratio;
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
return { width: width != null ? width : 300, height: height != null ? height : 150 };
|
|
1822
|
+
};
|
|
1823
|
+
var jpegSize = (b) => {
|
|
1824
|
+
let i = 2;
|
|
1825
|
+
while (i + 9 < b.length) {
|
|
1826
|
+
if (b[i] !== 255) {
|
|
1827
|
+
i++;
|
|
1828
|
+
continue;
|
|
1829
|
+
}
|
|
1830
|
+
const marker = b[i + 1];
|
|
1831
|
+
if (marker === 216 || marker === 1 || marker >= 208 && marker <= 215) {
|
|
1832
|
+
i += 2;
|
|
1833
|
+
continue;
|
|
1834
|
+
}
|
|
1835
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
1836
|
+
return { height: uint16BE(b, i + 5), width: uint16BE(b, i + 7) };
|
|
1837
|
+
}
|
|
1838
|
+
i += 2 + uint16BE(b, i + 2);
|
|
1839
|
+
}
|
|
1840
|
+
return null;
|
|
1841
|
+
};
|
|
1842
|
+
var webpSize = (b) => {
|
|
1843
|
+
const chunk = ascii(b, 12, 16);
|
|
1844
|
+
if (chunk === "VP8 ") return { width: uint16LE(b, 26) & 16383, height: uint16LE(b, 28) & 16383 };
|
|
1845
|
+
if (chunk === "VP8L") {
|
|
1846
|
+
const bits = b[21] | b[22] << 8 | b[23] << 16 | b[24] << 24;
|
|
1847
|
+
return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
|
|
1848
|
+
}
|
|
1849
|
+
if (chunk === "VP8X") return { width: uint24LE(b, 24) + 1, height: uint24LE(b, 27) + 1 };
|
|
1850
|
+
return null;
|
|
1851
|
+
};
|
|
1852
|
+
var getImageSize = ({ bytes, mimeType }) => {
|
|
1853
|
+
const type = mimeType === "application/octet-stream" ? sniffMimeType(bytes) : mimeType;
|
|
1854
|
+
let size = null;
|
|
1855
|
+
if (type === "image/png") size = { width: uint32BE(bytes, 16), height: uint32BE(bytes, 20) };
|
|
1856
|
+
else if (type === "image/gif") size = { width: uint16LE(bytes, 6), height: uint16LE(bytes, 8) };
|
|
1857
|
+
else if (type === "image/jpeg" || type === "image/jpg") size = jpegSize(bytes);
|
|
1858
|
+
else if (type === "image/webp") size = webpSize(bytes);
|
|
1859
|
+
else if (type === "image/svg+xml") size = svgSize(bytes);
|
|
1860
|
+
if (!size || !size.width || !size.height) {
|
|
1861
|
+
throw new Error(`Cannot read the size of the image (${type}). Supported: PNG, JPEG, GIF, WebP, SVG.`);
|
|
1862
|
+
}
|
|
1863
|
+
return size;
|
|
1864
|
+
};
|
|
1865
|
+
|
|
1316
1866
|
// src/core/QRSVG.ts
|
|
1317
1867
|
var squareMask2 = [
|
|
1318
1868
|
[1, 1, 1, 1, 1, 1, 1],
|
|
@@ -1335,18 +1885,23 @@ var dotMask2 = [
|
|
|
1335
1885
|
var QRSVG = class {
|
|
1336
1886
|
//TODO don't pass all options to this class
|
|
1337
1887
|
constructor(options) {
|
|
1888
|
+
/** True when no browser DOM or jsdom is available and the built-in virtual DOM is used. */
|
|
1889
|
+
this._virtual = false;
|
|
1338
1890
|
var _a;
|
|
1339
1891
|
if (options.jsdom) {
|
|
1340
1892
|
this._window = new options.jsdom("", { resources: "usable" }).window;
|
|
1341
|
-
} else {
|
|
1893
|
+
} else if (hasBrowserDom()) {
|
|
1342
1894
|
this._window = window;
|
|
1895
|
+
} else {
|
|
1896
|
+
this._window = createVirtualWindow();
|
|
1897
|
+
this._virtual = true;
|
|
1343
1898
|
}
|
|
1344
1899
|
this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
1345
1900
|
this._element.setAttribute("width", String(options.width));
|
|
1346
1901
|
this._element.setAttribute("height", String(options.height));
|
|
1347
1902
|
this._defs = this._window.document.createElementNS("http://www.w3.org/2000/svg", "defs");
|
|
1348
1903
|
this._element.appendChild(this._defs);
|
|
1349
|
-
if (options.imageOptions.saveAsBlob) {
|
|
1904
|
+
if (options.imageOptions.saveAsBlob && !this._virtual) {
|
|
1350
1905
|
if ((_a = options.nodeCanvas) == null ? void 0 : _a.createCanvas) {
|
|
1351
1906
|
this._canvas = options.nodeCanvas.createCanvas(options.width, options.height);
|
|
1352
1907
|
} else {
|
|
@@ -1401,9 +1956,7 @@ var QRSVG = class {
|
|
|
1401
1956
|
dotSize
|
|
1402
1957
|
});
|
|
1403
1958
|
}
|
|
1404
|
-
|
|
1405
|
-
this.drawBackground();
|
|
1406
|
-
this.drawDots((i, j) => {
|
|
1959
|
+
const filter = (i, j) => {
|
|
1407
1960
|
var _a, _b, _c, _d, _e, _f;
|
|
1408
1961
|
if (this._options.imageOptions.hideBackgroundDots) {
|
|
1409
1962
|
if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
|
|
@@ -1417,7 +1970,24 @@ var QRSVG = class {
|
|
|
1417
1970
|
return false;
|
|
1418
1971
|
}
|
|
1419
1972
|
return true;
|
|
1420
|
-
}
|
|
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
|
+
}
|
|
1421
1991
|
this.drawCorners();
|
|
1422
1992
|
if (this._options.image) {
|
|
1423
1993
|
this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
|
|
@@ -1474,7 +2044,8 @@ var QRSVG = class {
|
|
|
1474
2044
|
y: yBeginning,
|
|
1475
2045
|
height: count * dotSize,
|
|
1476
2046
|
width: count * dotSize,
|
|
1477
|
-
name: "dot-color"
|
|
2047
|
+
name: "dot-color",
|
|
2048
|
+
imageHref: this._dotsImageHref
|
|
1478
2049
|
});
|
|
1479
2050
|
for (let i = 0; i < count; i++) {
|
|
1480
2051
|
for (let j = 0; j < count; j++) {
|
|
@@ -1632,6 +2203,26 @@ var QRSVG = class {
|
|
|
1632
2203
|
}
|
|
1633
2204
|
});
|
|
1634
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
|
+
}
|
|
1635
2226
|
imageToBlob() {
|
|
1636
2227
|
var _a;
|
|
1637
2228
|
if (!this._image) return;
|
|
@@ -1644,11 +2235,23 @@ var QRSVG = class {
|
|
|
1644
2235
|
if (ctx) {
|
|
1645
2236
|
ctx.drawImage(this._image, 0, 0, this._canvas.width, this._canvas.height);
|
|
1646
2237
|
this._imageUri = this._canvas.toDataURL("image/png");
|
|
1647
|
-
console.log(this._imageUri);
|
|
1648
2238
|
}
|
|
1649
2239
|
}
|
|
1650
2240
|
}
|
|
2241
|
+
async _loadImageWithoutDom() {
|
|
2242
|
+
const source = this._options.image;
|
|
2243
|
+
if (!source) throw "Image is not defined";
|
|
2244
|
+
const data = await loadImageData(source);
|
|
2245
|
+
const { width, height } = getImageSize(data);
|
|
2246
|
+
this._image = { width, height };
|
|
2247
|
+
if (this._options.imageOptions.saveAsBlob && !source.startsWith("data:")) {
|
|
2248
|
+
this._imageUri = toDataUrl(data);
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
1651
2251
|
loadImage() {
|
|
2252
|
+
if (this._virtual) {
|
|
2253
|
+
return this._loadImageWithoutDom();
|
|
2254
|
+
}
|
|
1652
2255
|
return new Promise((resolve, reject) => {
|
|
1653
2256
|
var _a;
|
|
1654
2257
|
const options = this._options;
|
|
@@ -1702,9 +2305,21 @@ var QRSVG = class {
|
|
|
1702
2305
|
y,
|
|
1703
2306
|
height,
|
|
1704
2307
|
width,
|
|
1705
|
-
name
|
|
2308
|
+
name,
|
|
2309
|
+
imageHref
|
|
1706
2310
|
}) {
|
|
1707
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
|
+
}
|
|
1708
2323
|
const rect = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
1709
2324
|
rect.setAttribute("x", String(x));
|
|
1710
2325
|
rect.setAttribute("y", String(y));
|
|
@@ -1894,6 +2509,11 @@ var QRCodeStyling = class _QRCodeStyling {
|
|
|
1894
2509
|
this._options = options ? sanitizeOptions(mergeDeep(QROptions_default, options)) : QROptions_default;
|
|
1895
2510
|
this.update();
|
|
1896
2511
|
}
|
|
2512
|
+
/** Canvas output needs a browser canvas or node-canvas. */
|
|
2513
|
+
_canUseCanvas() {
|
|
2514
|
+
var _a;
|
|
2515
|
+
return !!((_a = this._options.nodeCanvas) == null ? void 0 : _a.createCanvas) || hasBrowserDom();
|
|
2516
|
+
}
|
|
1897
2517
|
static _clearContainer(container) {
|
|
1898
2518
|
if (container) {
|
|
1899
2519
|
container.innerHTML = "";
|
|
@@ -1913,6 +2533,11 @@ var QRCodeStyling = class _QRCodeStyling {
|
|
|
1913
2533
|
await promise;
|
|
1914
2534
|
return svg;
|
|
1915
2535
|
} else {
|
|
2536
|
+
if (!this._canUseCanvas()) {
|
|
2537
|
+
throw new Error(
|
|
2538
|
+
`Rendering "${extension}" outside the browser needs node-canvas (pass { nodeCanvas: require("canvas") }) or, for png, resvg (pass { resvg: require("@resvg/resvg-js").Resvg }). SVG output needs neither.`
|
|
2539
|
+
);
|
|
2540
|
+
}
|
|
1916
2541
|
let promise, canvas;
|
|
1917
2542
|
if (this._canvas && this._canvasDrawingPromise) {
|
|
1918
2543
|
canvas = this._canvas;
|
|
@@ -1934,7 +2559,12 @@ var QRCodeStyling = class _QRCodeStyling {
|
|
|
1934
2559
|
this._qr = qrcode(this._options.qrOptions.typeNumber, this._options.qrOptions.errorCorrectionLevel);
|
|
1935
2560
|
this._qr.addData(this._options.data, this._options.qrOptions.mode || getMode(this._options.data));
|
|
1936
2561
|
this._qr.make();
|
|
1937
|
-
if (this._options.type === drawTypes_default.canvas) {
|
|
2562
|
+
if (this._options.type === drawTypes_default.canvas && !this._canUseCanvas()) {
|
|
2563
|
+
this._canvas = void 0;
|
|
2564
|
+
this._canvasDrawingPromise = void 0;
|
|
2565
|
+
this._svg = void 0;
|
|
2566
|
+
this._svgDrawingPromise = void 0;
|
|
2567
|
+
} else if (this._options.type === drawTypes_default.canvas) {
|
|
1938
2568
|
this._canvas = new QRCanvas(this._options);
|
|
1939
2569
|
this._canvasDrawingPromise = this._canvas.drawQR(this._qr);
|
|
1940
2570
|
this._svgDrawingPromise = void 0;
|
|
@@ -1965,9 +2595,127 @@ var QRCodeStyling = class _QRCodeStyling {
|
|
|
1965
2595
|
}
|
|
1966
2596
|
this._container = container;
|
|
1967
2597
|
}
|
|
2598
|
+
static _serializeSVG(svg) {
|
|
2599
|
+
const serializer = new svg._window.XMLSerializer();
|
|
2600
|
+
return `<?xml version="1.0" standalone="no"?>\r
|
|
2601
|
+
${serializer.serializeToString(svg.getElement())}`;
|
|
2602
|
+
}
|
|
2603
|
+
async _getSVGString() {
|
|
2604
|
+
const svg = await this._getQRStylingElement("svg");
|
|
2605
|
+
return { svgString: _QRCodeStyling._serializeSVG(svg), svg };
|
|
2606
|
+
}
|
|
2607
|
+
/**
|
|
2608
|
+
* resvg follows the SVG spec more strictly than browsers: it does not accept quoted url('#id')
|
|
2609
|
+
* references, and it hides elements whose clip-path points to a missing id. Browsers ignore both,
|
|
2610
|
+
* so normalize them to get the picture a browser would show. The public SVG output is unchanged.
|
|
2611
|
+
*/
|
|
2612
|
+
static _normalizeSVGForResvg(svgString) {
|
|
2613
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2614
|
+
svgString.replace(/\sid="([^"]+)"/g, (match, id) => {
|
|
2615
|
+
ids.add(id);
|
|
2616
|
+
return match;
|
|
2617
|
+
});
|
|
2618
|
+
return svgString.replace(
|
|
2619
|
+
/\sclip-path="url\('#([^']+)'\)"/g,
|
|
2620
|
+
(_match, id) => ids.has(id) ? ` clip-path="url(#${id})"` : ""
|
|
2621
|
+
).replace(/url\('#([^']+)'\)/g, "url(#$1)");
|
|
2622
|
+
}
|
|
2623
|
+
async _rasterizeWithResvg(extension) {
|
|
2624
|
+
const Resvg = this._options.resvg;
|
|
2625
|
+
if (!Resvg) throw new Error("resvg is not configured");
|
|
2626
|
+
if (extension.toLowerCase() !== "png") {
|
|
2627
|
+
throw new Error(`resvg can only produce png, not "${extension}". Use extension "png" or pass nodeCanvas.`);
|
|
2628
|
+
}
|
|
2629
|
+
const previous = this._options.imageOptions.saveAsBlob;
|
|
2630
|
+
const previousSvg = this._svg;
|
|
2631
|
+
const previousPromise = this._svgDrawingPromise;
|
|
2632
|
+
let svgString;
|
|
2633
|
+
try {
|
|
2634
|
+
if (this._options.image && !previous && !this._options.image.startsWith("data:")) {
|
|
2635
|
+
this._options.imageOptions.saveAsBlob = true;
|
|
2636
|
+
this._svg = void 0;
|
|
2637
|
+
this._svgDrawingPromise = void 0;
|
|
2638
|
+
}
|
|
2639
|
+
({ svgString } = await this._getSVGString());
|
|
2640
|
+
} finally {
|
|
2641
|
+
this._options.imageOptions.saveAsBlob = previous;
|
|
2642
|
+
this._svg = previousSvg;
|
|
2643
|
+
this._svgDrawingPromise = previousPromise;
|
|
2644
|
+
}
|
|
2645
|
+
const png = new Resvg(_QRCodeStyling._normalizeSVGForResvg(svgString), { fitTo: { mode: "original" } }).render().asPng();
|
|
2646
|
+
return typeof Buffer !== "undefined" ? Buffer.from(png) : png;
|
|
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
|
+
}
|
|
1968
2709
|
async getRawData(extension = "png") {
|
|
1969
2710
|
if (!this._qr) throw "QR code is empty";
|
|
2711
|
+
if (extension.toLowerCase() !== "svg" && !this._canUseCanvas() && this._options.resvg) {
|
|
2712
|
+
return await this._rasterizeWithResvg(extension);
|
|
2713
|
+
}
|
|
1970
2714
|
const element = await this._getQRStylingElement(extension);
|
|
2715
|
+
if (extension.toLowerCase() === "svg" && element._virtual) {
|
|
2716
|
+
const svgString = _QRCodeStyling._serializeSVG(element);
|
|
2717
|
+
return typeof Buffer !== "undefined" ? Buffer.from(svgString) : new Blob([svgString], { type: "image/svg+xml" });
|
|
2718
|
+
}
|
|
1971
2719
|
if (extension.toLowerCase() === "svg") {
|
|
1972
2720
|
const serializer = new element._window.XMLSerializer();
|
|
1973
2721
|
const source = serializer.serializeToString(element.getElement());
|