qr-code-styling-node 1.7.0 → 1.9.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 +108 -4
- package/lib/art/art.d.ts +51 -0
- package/lib/art/functionModules.d.ts +7 -0
- package/lib/core/QRCanvas.d.ts +5 -0
- package/lib/core/QRCodeStyling.d.ts +13 -1
- package/lib/core/QROptions.d.ts +2 -0
- package/lib/core/QRSVG.d.ts +10 -2
- package/lib/figures/dot/canvas/QRDot.d.ts +2 -0
- package/lib/figures/dot/svg/QRDot.d.ts +2 -0
- package/lib/figures/shapes.d.ts +39 -0
- 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 +985 -149
- package/lib/qr-code-styling.mjs.map +4 -4
- package/lib/tools/cornerTypes.d.ts +8 -0
- package/lib/tools/frameLayout.d.ts +43 -0
- package/lib/tools/svgDom.d.ts +3 -0
- package/lib/types/index.d.ts +71 -3
- package/package.json +5 -2
package/lib/qr-code-styling.mjs
CHANGED
|
@@ -109,9 +109,120 @@ var dotTypes_default = {
|
|
|
109
109
|
classy: "classy",
|
|
110
110
|
classyRounded: "classy-rounded",
|
|
111
111
|
square: "square",
|
|
112
|
-
extraRounded: "extra-rounded"
|
|
112
|
+
extraRounded: "extra-rounded",
|
|
113
|
+
diamond: "diamond",
|
|
114
|
+
smallSquare: "small-square",
|
|
115
|
+
verticalLines: "vertical-lines",
|
|
116
|
+
horizontalLines: "horizontal-lines"
|
|
113
117
|
};
|
|
114
118
|
|
|
119
|
+
// src/figures/shapes.ts
|
|
120
|
+
function toCanvas(context, shape) {
|
|
121
|
+
shape.forEach((command) => {
|
|
122
|
+
if (command.op === "M") context.moveTo(command.x, command.y);
|
|
123
|
+
else if (command.op === "L") context.lineTo(command.x, command.y);
|
|
124
|
+
else if (command.op === "A")
|
|
125
|
+
context.arc(command.cx, command.cy, command.r, command.start, command.end, !!command.anticlockwise);
|
|
126
|
+
else context.closePath();
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
var round = (value) => Math.round(value * 1e3) / 1e3;
|
|
130
|
+
function toSVGPath(shape) {
|
|
131
|
+
const parts = [];
|
|
132
|
+
shape.forEach((command) => {
|
|
133
|
+
if (command.op === "M" || command.op === "L") {
|
|
134
|
+
parts.push(`${command.op} ${round(command.x)} ${round(command.y)}`);
|
|
135
|
+
} else if (command.op === "A") {
|
|
136
|
+
const { cx, cy, r, start, end, anticlockwise } = command;
|
|
137
|
+
let sweep = anticlockwise ? start - end : end - start;
|
|
138
|
+
while (sweep < 0) sweep += 2 * Math.PI;
|
|
139
|
+
parts.push(`L ${round(cx + r * Math.cos(start))} ${round(cy + r * Math.sin(start))}`);
|
|
140
|
+
parts.push(
|
|
141
|
+
`A ${round(r)} ${round(r)} 0 ${sweep > Math.PI ? 1 : 0} ${anticlockwise ? 0 : 1} ${round(cx + r * Math.cos(end))} ${round(cy + r * Math.sin(end))}`
|
|
142
|
+
);
|
|
143
|
+
} else {
|
|
144
|
+
parts.push("Z");
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
return parts.join(" ");
|
|
148
|
+
}
|
|
149
|
+
var diamond = (x, y, size) => [
|
|
150
|
+
{ op: "M", x: x + size / 2, y },
|
|
151
|
+
{ op: "L", x: x + size, y: y + size / 2 },
|
|
152
|
+
{ op: "L", x: x + size / 2, y: y + size },
|
|
153
|
+
{ op: "L", x, y: y + size / 2 },
|
|
154
|
+
{ op: "Z" }
|
|
155
|
+
];
|
|
156
|
+
var rect = (x, y, width, height) => [
|
|
157
|
+
{ op: "M", x, y },
|
|
158
|
+
{ op: "L", x: x + width, y },
|
|
159
|
+
{ op: "L", x: x + width, y: y + height },
|
|
160
|
+
{ op: "L", x, y: y + height },
|
|
161
|
+
{ op: "Z" }
|
|
162
|
+
];
|
|
163
|
+
var smallSquare = (x, y, size) => {
|
|
164
|
+
const inset = size * 0.15;
|
|
165
|
+
return rect(x + inset, y + inset, size - 2 * inset, size - 2 * inset);
|
|
166
|
+
};
|
|
167
|
+
var line = (x, y, size, vertical, before, after) => {
|
|
168
|
+
const thickness = size * 0.8;
|
|
169
|
+
const r = thickness / 2;
|
|
170
|
+
const offset = (size - thickness) / 2;
|
|
171
|
+
if (vertical) {
|
|
172
|
+
const left = x + offset;
|
|
173
|
+
const right = left + thickness;
|
|
174
|
+
const cx = x + size / 2;
|
|
175
|
+
const shape2 = [];
|
|
176
|
+
if (before) shape2.push({ op: "M", x: left, y }, { op: "L", x: right, y });
|
|
177
|
+
else shape2.push({ op: "M", x: left, y: y + r }, { op: "A", cx, cy: y + r, r, start: Math.PI, end: 0 });
|
|
178
|
+
if (after) shape2.push({ op: "L", x: right, y: y + size }, { op: "L", x: left, y: y + size });
|
|
179
|
+
else
|
|
180
|
+
shape2.push({ op: "L", x: right, y: y + size - r }, { op: "A", cx, cy: y + size - r, r, start: 0, end: Math.PI });
|
|
181
|
+
shape2.push({ op: "Z" });
|
|
182
|
+
return shape2;
|
|
183
|
+
}
|
|
184
|
+
const top = y + offset;
|
|
185
|
+
const bottom = top + thickness;
|
|
186
|
+
const cy = y + size / 2;
|
|
187
|
+
const shape = [];
|
|
188
|
+
if (before) shape.push({ op: "M", x, y: bottom }, { op: "L", x, y: top });
|
|
189
|
+
else
|
|
190
|
+
shape.push(
|
|
191
|
+
{ op: "M", x: x + r, y: bottom },
|
|
192
|
+
{ op: "A", cx: x + r, cy, r, start: Math.PI / 2, end: 3 * Math.PI / 2 }
|
|
193
|
+
);
|
|
194
|
+
if (after) shape.push({ op: "L", x: x + size, y: top }, { op: "L", x: x + size, y: bottom });
|
|
195
|
+
else
|
|
196
|
+
shape.push(
|
|
197
|
+
{ op: "L", x: x + size - r, y: top },
|
|
198
|
+
{ op: "A", cx: x + size - r, cy, r, start: -Math.PI / 2, end: Math.PI / 2 }
|
|
199
|
+
);
|
|
200
|
+
shape.push({ op: "Z" });
|
|
201
|
+
return shape;
|
|
202
|
+
};
|
|
203
|
+
var roundedRect = (x, y, width, height, radius) => {
|
|
204
|
+
const r = Math.min(radius, width / 2, height / 2);
|
|
205
|
+
return [
|
|
206
|
+
{ op: "M", x: x + r, y },
|
|
207
|
+
{ op: "L", x: x + width - r, y },
|
|
208
|
+
{ op: "A", cx: x + width - r, cy: y + r, r, start: -Math.PI / 2, end: 0 },
|
|
209
|
+
{ op: "L", x: x + width, y: y + height - r },
|
|
210
|
+
{ op: "A", cx: x + width - r, cy: y + height - r, r, start: 0, end: Math.PI / 2 },
|
|
211
|
+
{ op: "L", x: x + r, y: y + height },
|
|
212
|
+
{ op: "A", cx: x + r, cy: y + height - r, r, start: Math.PI / 2, end: Math.PI },
|
|
213
|
+
{ op: "L", x, y: y + r },
|
|
214
|
+
{ op: "A", cx: x + r, cy: y + r, r, start: Math.PI, end: 3 * Math.PI / 2 },
|
|
215
|
+
{ op: "Z" }
|
|
216
|
+
];
|
|
217
|
+
};
|
|
218
|
+
var roundedRing = (x, y, size) => {
|
|
219
|
+
const module = size / 7;
|
|
220
|
+
return roundedRect(x, y, size, size, module * 1.5).concat(
|
|
221
|
+
roundedRect(x + module, y + module, size - 2 * module, size - 2 * module, module * 0.75)
|
|
222
|
+
);
|
|
223
|
+
};
|
|
224
|
+
var roundedSquare = (x, y, size) => roundedRect(x, y, size, size, size * 0.25);
|
|
225
|
+
|
|
115
226
|
// src/figures/dot/canvas/QRDot.ts
|
|
116
227
|
var QRDot = class {
|
|
117
228
|
constructor({ context, type }) {
|
|
@@ -138,12 +249,31 @@ var QRDot = class {
|
|
|
138
249
|
case dotTypes_default.extraRounded:
|
|
139
250
|
drawFunction = this._drawExtraRounded;
|
|
140
251
|
break;
|
|
252
|
+
case dotTypes_default.diamond:
|
|
253
|
+
drawFunction = (args) => this._drawShape(diamond(args.x, args.y, args.size));
|
|
254
|
+
break;
|
|
255
|
+
case dotTypes_default.smallSquare:
|
|
256
|
+
drawFunction = (args) => this._drawShape(smallSquare(args.x, args.y, args.size));
|
|
257
|
+
break;
|
|
258
|
+
case dotTypes_default.verticalLines:
|
|
259
|
+
case dotTypes_default.horizontalLines: {
|
|
260
|
+
const vertical = type === dotTypes_default.verticalLines;
|
|
261
|
+
drawFunction = (args) => {
|
|
262
|
+
const before = args.getNeighbor ? args.getNeighbor(vertical ? 0 : -1, vertical ? -1 : 0) : false;
|
|
263
|
+
const after = args.getNeighbor ? args.getNeighbor(vertical ? 0 : 1, vertical ? 1 : 0) : false;
|
|
264
|
+
this._drawShape(line(args.x, args.y, args.size, vertical, before, after));
|
|
265
|
+
};
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
141
268
|
case dotTypes_default.square:
|
|
142
269
|
default:
|
|
143
270
|
drawFunction = this._drawSquare;
|
|
144
271
|
}
|
|
145
272
|
drawFunction.call(this, { x, y, size, context, getNeighbor });
|
|
146
273
|
}
|
|
274
|
+
_drawShape(shape) {
|
|
275
|
+
toCanvas(this._context, shape);
|
|
276
|
+
}
|
|
147
277
|
_rotateFigure({ x, y, size, context, rotation = 0, draw }) {
|
|
148
278
|
const cx = x + size / 2;
|
|
149
279
|
const cy = y + size / 2;
|
|
@@ -365,7 +495,8 @@ var QRDot = class {
|
|
|
365
495
|
var cornerSquareTypes_default = {
|
|
366
496
|
dot: "dot",
|
|
367
497
|
square: "square",
|
|
368
|
-
extraRounded: "extra-rounded"
|
|
498
|
+
extraRounded: "extra-rounded",
|
|
499
|
+
rounded: "rounded"
|
|
369
500
|
};
|
|
370
501
|
|
|
371
502
|
// src/figures/cornerSquare/canvas/QRCornerSquare.ts
|
|
@@ -385,6 +516,9 @@ var QRCornerSquare = class {
|
|
|
385
516
|
case cornerSquareTypes_default.extraRounded:
|
|
386
517
|
drawFunction = this._drawExtraRounded;
|
|
387
518
|
break;
|
|
519
|
+
case cornerSquareTypes_default.rounded:
|
|
520
|
+
drawFunction = (args) => toCanvas(context, roundedRing(args.x, args.y, args.size));
|
|
521
|
+
break;
|
|
388
522
|
case cornerSquareTypes_default.dot:
|
|
389
523
|
default:
|
|
390
524
|
drawFunction = this._drawDot;
|
|
@@ -462,7 +596,8 @@ var QRCornerSquare = class {
|
|
|
462
596
|
// src/constants/cornerDotTypes.ts
|
|
463
597
|
var cornerDotTypes_default = {
|
|
464
598
|
dot: "dot",
|
|
465
|
-
square: "square"
|
|
599
|
+
square: "square",
|
|
600
|
+
rounded: "rounded"
|
|
466
601
|
};
|
|
467
602
|
|
|
468
603
|
// src/figures/cornerDot/canvas/QRCornerDot.ts
|
|
@@ -479,6 +614,9 @@ var QRCornerDot = class {
|
|
|
479
614
|
case cornerDotTypes_default.square:
|
|
480
615
|
drawFunction = this._drawSquare;
|
|
481
616
|
break;
|
|
617
|
+
case cornerDotTypes_default.rounded:
|
|
618
|
+
drawFunction = (args) => toCanvas(context, roundedSquare(args.x, args.y, args.size));
|
|
619
|
+
break;
|
|
482
620
|
case cornerDotTypes_default.dot:
|
|
483
621
|
default:
|
|
484
622
|
drawFunction = this._drawDot;
|
|
@@ -521,6 +659,494 @@ var QRCornerDot = class {
|
|
|
521
659
|
}
|
|
522
660
|
};
|
|
523
661
|
|
|
662
|
+
// src/tools/cornerTypes.ts
|
|
663
|
+
var SOLID_CORNER_DOT_TYPES = ["diamond", "small-square", "vertical-lines", "horizontal-lines"];
|
|
664
|
+
var needsSolidAlignment = (type) => SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
|
|
665
|
+
function getCornerTypes(options) {
|
|
666
|
+
var _a, _b;
|
|
667
|
+
const solid = SOLID_CORNER_DOT_TYPES.indexOf(options.dotsOptions.type) !== -1;
|
|
668
|
+
return {
|
|
669
|
+
square: ((_a = options.cornersSquareOptions) == null ? void 0 : _a.type) || (solid ? "square" : void 0),
|
|
670
|
+
dot: ((_b = options.cornersDotOptions) == null ? void 0 : _b.type) || (solid ? "square" : void 0)
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// src/tools/frameLayout.ts
|
|
675
|
+
var hasFrame = (options) => {
|
|
676
|
+
var _a;
|
|
677
|
+
return !!(options.frameOptions || ((_a = options.labelOptions) == null ? void 0 : _a.text));
|
|
678
|
+
};
|
|
679
|
+
function getFrameLayout(options) {
|
|
680
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
681
|
+
const { width, height } = options;
|
|
682
|
+
const unit = Math.min(width, height);
|
|
683
|
+
const frame = options.frameOptions;
|
|
684
|
+
const label = ((_a = options.labelOptions) == null ? void 0 : _a.text) ? options.labelOptions : void 0;
|
|
685
|
+
const thickness = frame ? Math.max(0, (_b = frame.width) != null ? _b : Math.round(unit * 0.04)) : 0;
|
|
686
|
+
const padding = Math.max(0, (_c = frame == null ? void 0 : frame.padding) != null ? _c : frame ? Math.round(unit * 0.04) : options.margin);
|
|
687
|
+
const fontSize = label ? (_d = label.fontSize) != null ? _d : Math.max(8, Math.round(unit * 0.07)) : 0;
|
|
688
|
+
const bandHeight = label ? Math.round(fontSize * 1.8) : 0;
|
|
689
|
+
const position = (label == null ? void 0 : label.position) === "top" ? "top" : "bottom";
|
|
690
|
+
const innerX = thickness;
|
|
691
|
+
const innerY = thickness;
|
|
692
|
+
const innerWidth = width - 2 * thickness;
|
|
693
|
+
const innerHeight = height - 2 * thickness;
|
|
694
|
+
const qrRegionY = position === "top" ? innerY + bandHeight : innerY;
|
|
695
|
+
const qrRegionHeight = innerHeight - bandHeight;
|
|
696
|
+
const size = Math.floor(Math.min(innerWidth, qrRegionHeight) - 2 * padding);
|
|
697
|
+
if (size <= 0) throw new Error("frameOptions/labelOptions leave no room for the QR code; increase width/height");
|
|
698
|
+
const frameColor = (_e = frame == null ? void 0 : frame.color) != null ? _e : "#000";
|
|
699
|
+
const fontFamily = (_f = label == null ? void 0 : label.fontFamily) != null ? _f : "Helvetica, Arial, sans-serif";
|
|
700
|
+
const fontWeight = String((_g = label == null ? void 0 : label.fontWeight) != null ? _g : "bold");
|
|
701
|
+
return {
|
|
702
|
+
width,
|
|
703
|
+
height,
|
|
704
|
+
qr: {
|
|
705
|
+
x: Math.round(innerX + (innerWidth - size) / 2),
|
|
706
|
+
y: Math.round(qrRegionY + (qrRegionHeight - size) / 2),
|
|
707
|
+
size
|
|
708
|
+
},
|
|
709
|
+
frame: frame ? { thickness, radius: Math.max(0, (_h = frame.radius) != null ? _h : 0), color: frameColor } : void 0,
|
|
710
|
+
label: label ? {
|
|
711
|
+
text: label.text,
|
|
712
|
+
band: {
|
|
713
|
+
x: innerX,
|
|
714
|
+
y: position === "top" ? innerY : innerY + innerHeight - bandHeight,
|
|
715
|
+
width: innerWidth,
|
|
716
|
+
height: bandHeight
|
|
717
|
+
},
|
|
718
|
+
color: (_i = label.color) != null ? _i : frame ? "#fff" : options.dotsOptions.color || "#000",
|
|
719
|
+
font: `${fontWeight} ${fontSize}px ${fontFamily}`,
|
|
720
|
+
fontSize,
|
|
721
|
+
fontFamily,
|
|
722
|
+
fontWeight
|
|
723
|
+
} : void 0,
|
|
724
|
+
background: options.backgroundOptions.color || "#fff"
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
var innerOptions = (options, size) => ({
|
|
728
|
+
...options,
|
|
729
|
+
width: size,
|
|
730
|
+
height: size,
|
|
731
|
+
margin: 0,
|
|
732
|
+
frameOptions: void 0,
|
|
733
|
+
labelOptions: void 0
|
|
734
|
+
});
|
|
735
|
+
var getLogoPlate = (options, dx, dy, width, height) => {
|
|
736
|
+
var _a;
|
|
737
|
+
const ratio = Math.min(0.5, Math.max(0, (_a = options.imageOptions.backgroundRadius) != null ? _a : 0.2));
|
|
738
|
+
return {
|
|
739
|
+
x: dx - options.imageOptions.margin,
|
|
740
|
+
y: dy - options.imageOptions.margin,
|
|
741
|
+
width,
|
|
742
|
+
height,
|
|
743
|
+
radius: ratio * Math.min(width, height)
|
|
744
|
+
};
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
// src/art/functionModules.ts
|
|
748
|
+
var ALIGNMENT_POSITIONS = [
|
|
749
|
+
[],
|
|
750
|
+
[6, 18],
|
|
751
|
+
[6, 22],
|
|
752
|
+
[6, 26],
|
|
753
|
+
[6, 30],
|
|
754
|
+
[6, 34],
|
|
755
|
+
[6, 22, 38],
|
|
756
|
+
[6, 24, 42],
|
|
757
|
+
[6, 26, 46],
|
|
758
|
+
[6, 28, 50],
|
|
759
|
+
[6, 30, 54],
|
|
760
|
+
[6, 32, 58],
|
|
761
|
+
[6, 34, 62],
|
|
762
|
+
[6, 26, 46, 66],
|
|
763
|
+
[6, 26, 48, 70],
|
|
764
|
+
[6, 26, 50, 74],
|
|
765
|
+
[6, 30, 54, 78],
|
|
766
|
+
[6, 30, 56, 82],
|
|
767
|
+
[6, 30, 58, 86],
|
|
768
|
+
[6, 34, 62, 90],
|
|
769
|
+
[6, 28, 50, 72, 94],
|
|
770
|
+
[6, 26, 50, 74, 98],
|
|
771
|
+
[6, 30, 54, 78, 102],
|
|
772
|
+
[6, 28, 54, 80, 106],
|
|
773
|
+
[6, 32, 58, 84, 110],
|
|
774
|
+
[6, 30, 58, 86, 114],
|
|
775
|
+
[6, 34, 62, 90, 118],
|
|
776
|
+
[6, 26, 50, 74, 98, 122],
|
|
777
|
+
[6, 30, 54, 78, 102, 126],
|
|
778
|
+
[6, 26, 52, 78, 104, 130],
|
|
779
|
+
[6, 30, 56, 82, 108, 134],
|
|
780
|
+
[6, 34, 60, 86, 112, 138],
|
|
781
|
+
[6, 30, 58, 86, 114, 142],
|
|
782
|
+
[6, 34, 62, 90, 118, 146],
|
|
783
|
+
[6, 30, 54, 78, 102, 126, 150],
|
|
784
|
+
[6, 24, 50, 76, 102, 128, 154],
|
|
785
|
+
[6, 28, 54, 80, 106, 132, 158],
|
|
786
|
+
[6, 32, 58, 84, 110, 136, 162],
|
|
787
|
+
[6, 26, 54, 82, 110, 138, 166],
|
|
788
|
+
[6, 30, 58, 86, 114, 142, 170]
|
|
789
|
+
];
|
|
790
|
+
function getModuleRoles(count) {
|
|
791
|
+
const version = (count - 17) / 4;
|
|
792
|
+
const roles = new Uint8Array(count * count);
|
|
793
|
+
const mark = (row, col, role) => {
|
|
794
|
+
if (row >= 0 && col >= 0 && row < count && col < count) roles[row * count + col] = role;
|
|
795
|
+
};
|
|
796
|
+
for (let r = 0; r < 8; r++) {
|
|
797
|
+
for (let c = 0; c < 8; c++) {
|
|
798
|
+
mark(r, c, 2);
|
|
799
|
+
mark(r, count - 1 - c, 2);
|
|
800
|
+
mark(count - 1 - r, c, 2);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
for (let i = 8; i < count - 8; i++) {
|
|
804
|
+
mark(6, i, 1);
|
|
805
|
+
mark(i, 6, 1);
|
|
806
|
+
}
|
|
807
|
+
const positions = ALIGNMENT_POSITIONS[version - 1] || [];
|
|
808
|
+
positions.forEach(
|
|
809
|
+
(row) => positions.forEach((col) => {
|
|
810
|
+
if (roles[row * count + col] === 2) return;
|
|
811
|
+
for (let r = -2; r <= 2; r++) for (let c = -2; c <= 2; c++) mark(row + r, col + c, 3);
|
|
812
|
+
})
|
|
813
|
+
);
|
|
814
|
+
for (let i = 0; i < 9; i++) {
|
|
815
|
+
if (roles[8 * count + i] !== 2) mark(8, i, 1);
|
|
816
|
+
if (roles[i * count + 8] !== 2) mark(i, 8, 1);
|
|
817
|
+
}
|
|
818
|
+
for (let i = 0; i < 8; i++) {
|
|
819
|
+
mark(8, count - 1 - i, 1);
|
|
820
|
+
mark(count - 1 - i, 8, 1);
|
|
821
|
+
}
|
|
822
|
+
if (version >= 7) {
|
|
823
|
+
for (let a = 0; a < 6; a++) {
|
|
824
|
+
for (let b = 0; b < 3; b++) {
|
|
825
|
+
mark(a, count - 11 + b, 1);
|
|
826
|
+
mark(count - 11 + b, a, 1);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
return (row, col) => ["data", "function", "finder", "alignment"][roles[row * count + col]];
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// src/tools/svgDom.ts
|
|
834
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
835
|
+
var escapeAttribute = (value) => value.replace(/[&<>"\t\n\r]/g, (char) => {
|
|
836
|
+
switch (char) {
|
|
837
|
+
case "&":
|
|
838
|
+
return "&";
|
|
839
|
+
case "<":
|
|
840
|
+
return "<";
|
|
841
|
+
case ">":
|
|
842
|
+
return ">";
|
|
843
|
+
case '"':
|
|
844
|
+
return """;
|
|
845
|
+
case " ":
|
|
846
|
+
return "	";
|
|
847
|
+
case "\n":
|
|
848
|
+
return "
";
|
|
849
|
+
default:
|
|
850
|
+
return "
";
|
|
851
|
+
}
|
|
852
|
+
});
|
|
853
|
+
var VirtualSVGElement = class _VirtualSVGElement {
|
|
854
|
+
constructor(tagName) {
|
|
855
|
+
this.namespaceURI = SVG_NS;
|
|
856
|
+
this.parentNode = null;
|
|
857
|
+
this.childNodes = [];
|
|
858
|
+
this._attributes = /* @__PURE__ */ new Map();
|
|
859
|
+
this._text = "";
|
|
860
|
+
this.tagName = tagName;
|
|
861
|
+
}
|
|
862
|
+
get textContent() {
|
|
863
|
+
return this._text + this.childNodes.map((child) => child.textContent).join("");
|
|
864
|
+
}
|
|
865
|
+
set textContent(value) {
|
|
866
|
+
this.childNodes = [];
|
|
867
|
+
this._text = String(value);
|
|
868
|
+
}
|
|
869
|
+
get nodeName() {
|
|
870
|
+
return this.tagName;
|
|
871
|
+
}
|
|
872
|
+
setAttribute(name, value) {
|
|
873
|
+
this._attributes.set(name, String(value));
|
|
874
|
+
}
|
|
875
|
+
getAttribute(name) {
|
|
876
|
+
const value = this._attributes.get(name);
|
|
877
|
+
return value === void 0 ? null : value;
|
|
878
|
+
}
|
|
879
|
+
removeAttribute(name) {
|
|
880
|
+
this._attributes.delete(name);
|
|
881
|
+
}
|
|
882
|
+
appendChild(child) {
|
|
883
|
+
if (child.parentNode) child.parentNode.removeChild(child);
|
|
884
|
+
child.parentNode = this;
|
|
885
|
+
this.childNodes.push(child);
|
|
886
|
+
return child;
|
|
887
|
+
}
|
|
888
|
+
removeChild(child) {
|
|
889
|
+
const index = this.childNodes.indexOf(child);
|
|
890
|
+
if (index !== -1) this.childNodes.splice(index, 1);
|
|
891
|
+
child.parentNode = null;
|
|
892
|
+
return child;
|
|
893
|
+
}
|
|
894
|
+
replaceChild(newChild, oldChild) {
|
|
895
|
+
const index = this.childNodes.indexOf(oldChild);
|
|
896
|
+
if (index !== -1) {
|
|
897
|
+
if (newChild.parentNode) newChild.parentNode.removeChild(newChild);
|
|
898
|
+
this.childNodes[index] = newChild;
|
|
899
|
+
newChild.parentNode = this;
|
|
900
|
+
oldChild.parentNode = null;
|
|
901
|
+
}
|
|
902
|
+
return oldChild;
|
|
903
|
+
}
|
|
904
|
+
cloneNode(deep = false) {
|
|
905
|
+
const clone = new _VirtualSVGElement(this.tagName);
|
|
906
|
+
this._attributes.forEach((value, name) => clone.setAttribute(name, value));
|
|
907
|
+
if (deep) clone._text = this._text;
|
|
908
|
+
if (deep) this.childNodes.forEach((child) => clone.appendChild(child.cloneNode(true)));
|
|
909
|
+
return clone;
|
|
910
|
+
}
|
|
911
|
+
serialize(isRoot = true) {
|
|
912
|
+
let markup = `<${this.tagName}`;
|
|
913
|
+
if (isRoot) markup += ` xmlns="${SVG_NS}"`;
|
|
914
|
+
this._attributes.forEach((value, name) => {
|
|
915
|
+
markup += ` ${name}="${escapeAttribute(value)}"`;
|
|
916
|
+
});
|
|
917
|
+
if (!this.childNodes.length && !this._text) return `${markup}/>`;
|
|
918
|
+
markup += ">";
|
|
919
|
+
markup += this._text.replace(/[&<>]/g, (char) => char === "&" ? "&" : char === "<" ? "<" : ">");
|
|
920
|
+
for (const child of this.childNodes) markup += child.serialize(false);
|
|
921
|
+
return `${markup}</${this.tagName}>`;
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
var VirtualXMLSerializer = class {
|
|
925
|
+
serializeToString(node) {
|
|
926
|
+
return node.serialize(true);
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
var VirtualDocument = class {
|
|
930
|
+
createElementNS(_namespace, tagName) {
|
|
931
|
+
return new VirtualSVGElement(tagName);
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
var createVirtualWindow = () => ({
|
|
935
|
+
document: new VirtualDocument(),
|
|
936
|
+
XMLSerializer: VirtualXMLSerializer,
|
|
937
|
+
isVirtual: true
|
|
938
|
+
});
|
|
939
|
+
var hasBrowserDom = () => typeof window !== "undefined" && typeof document !== "undefined" && typeof document.createElementNS === "function";
|
|
940
|
+
|
|
941
|
+
// src/art/art.ts
|
|
942
|
+
var DEFAULT_CONTRAST = { halftone: 0.5, colorize: 0.7 };
|
|
943
|
+
var clamp01 = (value) => Math.min(1, Math.max(0, value));
|
|
944
|
+
var getContrast = (art) => clamp01(
|
|
945
|
+
typeof art.contrast === "number" && !isNaN(art.contrast) ? art.contrast : DEFAULT_CONTRAST[art.mode === "colorize" ? "colorize" : "halftone"]
|
|
946
|
+
);
|
|
947
|
+
var getLimits = (contrast) => ({
|
|
948
|
+
dark: 0.45 - 0.35 * contrast,
|
|
949
|
+
light: 0.55 + 0.35 * contrast
|
|
950
|
+
});
|
|
951
|
+
var luminance = ([r, g, b]) => (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
|
|
952
|
+
var toDark = (color, limit) => {
|
|
953
|
+
const lum = luminance(color);
|
|
954
|
+
if (lum <= limit) return color;
|
|
955
|
+
const factor = limit / lum;
|
|
956
|
+
return [color[0] * factor, color[1] * factor, color[2] * factor];
|
|
957
|
+
};
|
|
958
|
+
var toLight = (color, limit) => {
|
|
959
|
+
const lum = luminance(color);
|
|
960
|
+
if (lum >= limit) return color;
|
|
961
|
+
const t = (limit - lum) / (1 - lum);
|
|
962
|
+
return [color[0] + (255 - color[0]) * t, color[1] + (255 - color[1]) * t, color[2] + (255 - color[2]) * t];
|
|
963
|
+
};
|
|
964
|
+
var css = ([r, g, b]) => `rgb(${Math.round(r)},${Math.round(g)},${Math.round(b)})`;
|
|
965
|
+
function createCanvas(options, width, height) {
|
|
966
|
+
var _a;
|
|
967
|
+
if ((_a = options.nodeCanvas) == null ? void 0 : _a.createCanvas) {
|
|
968
|
+
return options.nodeCanvas.createCanvas(width, height);
|
|
969
|
+
}
|
|
970
|
+
if (hasBrowserDom()) {
|
|
971
|
+
const canvas = document.createElement("canvas");
|
|
972
|
+
canvas.width = width;
|
|
973
|
+
canvas.height = height;
|
|
974
|
+
return canvas;
|
|
975
|
+
}
|
|
976
|
+
throw new Error('QR art reads the picture through a canvas. On a server, pass { nodeCanvas: require("canvas") }.');
|
|
977
|
+
}
|
|
978
|
+
function loadPicture(options, art) {
|
|
979
|
+
var _a;
|
|
980
|
+
if ((_a = options.nodeCanvas) == null ? void 0 : _a.loadImage) {
|
|
981
|
+
return options.nodeCanvas.loadImage(art.image);
|
|
982
|
+
}
|
|
983
|
+
return new Promise((resolve, reject) => {
|
|
984
|
+
var _a2;
|
|
985
|
+
const image = new Image();
|
|
986
|
+
const crossOrigin = (_a2 = art.crossOrigin) != null ? _a2 : options.imageOptions.crossOrigin;
|
|
987
|
+
if (typeof crossOrigin === "string") image.crossOrigin = crossOrigin;
|
|
988
|
+
image.onload = () => resolve(image);
|
|
989
|
+
image.onerror = () => reject(new Error(`QR art: failed to load image ${art.image}`));
|
|
990
|
+
image.src = art.image;
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
function drawCover(context, image, width, height) {
|
|
994
|
+
const scale = Math.max(width / image.width, height / image.height);
|
|
995
|
+
const sourceWidth = width / scale;
|
|
996
|
+
const sourceHeight = height / scale;
|
|
997
|
+
context.drawImage(
|
|
998
|
+
image,
|
|
999
|
+
(image.width - sourceWidth) / 2,
|
|
1000
|
+
(image.height - sourceHeight) / 2,
|
|
1001
|
+
sourceWidth,
|
|
1002
|
+
sourceHeight,
|
|
1003
|
+
0,
|
|
1004
|
+
0,
|
|
1005
|
+
width,
|
|
1006
|
+
height
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
function readPixels(options, image, width, height) {
|
|
1010
|
+
const canvas = createCanvas(options, width, height);
|
|
1011
|
+
const context = canvas.getContext("2d");
|
|
1012
|
+
context.imageSmoothingEnabled = true;
|
|
1013
|
+
drawCover(context, image, width, height);
|
|
1014
|
+
return { canvas, context, data: context.getImageData(0, 0, width, height).data };
|
|
1015
|
+
}
|
|
1016
|
+
function parseColor(options, color, fallback) {
|
|
1017
|
+
if (!color) return fallback;
|
|
1018
|
+
const canvas = createCanvas(options, 1, 1);
|
|
1019
|
+
const context = canvas.getContext("2d");
|
|
1020
|
+
context.fillStyle = color;
|
|
1021
|
+
context.fillRect(0, 0, 1, 1);
|
|
1022
|
+
const pixel = context.getImageData(0, 0, 1, 1).data;
|
|
1023
|
+
return pixel[3] === 0 ? fallback : [pixel[0], pixel[1], pixel[2]];
|
|
1024
|
+
}
|
|
1025
|
+
function planHalftone(options, art, image, qr, area, filter) {
|
|
1026
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1027
|
+
const { count, dotSize } = area;
|
|
1028
|
+
const sub = 3;
|
|
1029
|
+
const cells = count * sub;
|
|
1030
|
+
const pixels = readPixels(options, image, cells, cells).data;
|
|
1031
|
+
const role = getModuleRoles(count);
|
|
1032
|
+
const contrast = getContrast(art);
|
|
1033
|
+
const limits = getLimits(contrast);
|
|
1034
|
+
const strong = getLimits(Math.max(contrast, 0.8));
|
|
1035
|
+
const darkColor = parseColor(
|
|
1036
|
+
options,
|
|
1037
|
+
(_c = (_b = (_a = options.dotsOptions.gradient) == null ? void 0 : _a.colorStops[0]) == null ? void 0 : _b.color) != null ? _c : options.dotsOptions.color,
|
|
1038
|
+
[0, 0, 0]
|
|
1039
|
+
);
|
|
1040
|
+
const lightColor = parseColor(
|
|
1041
|
+
options,
|
|
1042
|
+
(_f = (_e = (_d = options.backgroundOptions.gradient) == null ? void 0 : _d.colorStops[0]) == null ? void 0 : _e.color) != null ? _f : options.backgroundOptions.color,
|
|
1043
|
+
[255, 255, 255]
|
|
1044
|
+
);
|
|
1045
|
+
const pixelAt = (cx, cy) => {
|
|
1046
|
+
const index = (cy * cells + cx) * 4;
|
|
1047
|
+
return [pixels[index], pixels[index + 1], pixels[index + 2]];
|
|
1048
|
+
};
|
|
1049
|
+
const shade = (color, dark, limit) => {
|
|
1050
|
+
if (!art.colored) return css(dark ? darkColor : lightColor);
|
|
1051
|
+
return css(dark ? toDark(color, limit.dark) : toLight(color, limit.light));
|
|
1052
|
+
};
|
|
1053
|
+
const grid = new Array(cells * cells).fill(null);
|
|
1054
|
+
const centers = [];
|
|
1055
|
+
const bounds = [0, 1, 2, 3].map((k) => Math.round(k * dotSize / sub));
|
|
1056
|
+
const centerSize = Math.max(1, Math.round(dotSize * (1 / 3 + contrast / 3)));
|
|
1057
|
+
const centerOffset = (dotSize - centerSize) / 2;
|
|
1058
|
+
for (let i = 0; i < count; i++) {
|
|
1059
|
+
for (let j = 0; j < count; j++) {
|
|
1060
|
+
const found = role(i, j);
|
|
1061
|
+
const moduleRole = found === "alignment" ? "function" : found;
|
|
1062
|
+
if (moduleRole === "finder" || !filter(i, j)) continue;
|
|
1063
|
+
const dark = qr.isDark(i, j);
|
|
1064
|
+
for (let a = 0; a < sub; a++) {
|
|
1065
|
+
for (let b = 0; b < sub; b++) {
|
|
1066
|
+
const cx = i * sub + a;
|
|
1067
|
+
const cy = j * sub + b;
|
|
1068
|
+
const color = pixelAt(cx, cy);
|
|
1069
|
+
const cellDark = moduleRole === "function" ? dark : luminance(color) < 0.5;
|
|
1070
|
+
grid[cy * cells + cx] = shade(color, cellDark, moduleRole === "function" ? strong : limits);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
if (moduleRole === "data") {
|
|
1074
|
+
const center = pixelAt(i * sub + 1, j * sub + 1);
|
|
1075
|
+
centers.push({
|
|
1076
|
+
x: area.x + i * dotSize + centerOffset,
|
|
1077
|
+
y: area.y + j * dotSize + centerOffset,
|
|
1078
|
+
width: centerSize,
|
|
1079
|
+
height: centerSize,
|
|
1080
|
+
color: shade(center, dark, strong)
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
const rects = [];
|
|
1086
|
+
for (let cy = 0; cy < cells; cy++) {
|
|
1087
|
+
let cx = 0;
|
|
1088
|
+
while (cx < cells) {
|
|
1089
|
+
const color = grid[cy * cells + cx];
|
|
1090
|
+
let end = cx + 1;
|
|
1091
|
+
while (end < cells && grid[cy * cells + end] === color) end++;
|
|
1092
|
+
if (color) {
|
|
1093
|
+
const moduleX0 = Math.floor(cx / sub);
|
|
1094
|
+
const moduleX1 = Math.floor((end - 1) / sub);
|
|
1095
|
+
const moduleY = Math.floor(cy / sub);
|
|
1096
|
+
const x0 = area.x + moduleX0 * dotSize + bounds[cx % sub];
|
|
1097
|
+
const x1 = area.x + moduleX1 * dotSize + bounds[(end - 1) % sub + 1];
|
|
1098
|
+
const y0 = area.y + moduleY * dotSize + bounds[cy % sub];
|
|
1099
|
+
const y1 = area.y + moduleY * dotSize + bounds[cy % sub + 1];
|
|
1100
|
+
rects.push({ x: x0, y: y0, width: x1 - x0, height: y1 - y0, color });
|
|
1101
|
+
}
|
|
1102
|
+
cx = end;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
return { mode: "halftone", rects: rects.concat(centers) };
|
|
1106
|
+
}
|
|
1107
|
+
function prepareColorize(options, art, image, area) {
|
|
1108
|
+
const size = area.count * area.dotSize;
|
|
1109
|
+
const { data } = readPixels(options, image, size, size);
|
|
1110
|
+
const limits = getLimits(getContrast(art));
|
|
1111
|
+
const makeLayer = (transform) => {
|
|
1112
|
+
const layer = createCanvas(options, options.width, options.height);
|
|
1113
|
+
const context = layer.getContext("2d");
|
|
1114
|
+
const imageData = context.createImageData(size, size);
|
|
1115
|
+
for (let index = 0; index < data.length; index += 4) {
|
|
1116
|
+
const [r, g, b] = transform([data[index], data[index + 1], data[index + 2]]);
|
|
1117
|
+
imageData.data[index] = r;
|
|
1118
|
+
imageData.data[index + 1] = g;
|
|
1119
|
+
imageData.data[index + 2] = b;
|
|
1120
|
+
imageData.data[index + 3] = 255;
|
|
1121
|
+
}
|
|
1122
|
+
context.putImageData(imageData, area.x, area.y);
|
|
1123
|
+
return layer;
|
|
1124
|
+
};
|
|
1125
|
+
return {
|
|
1126
|
+
mode: "colorize",
|
|
1127
|
+
dots: makeLayer((color) => toDark(color, limits.dark)),
|
|
1128
|
+
background: art.background === false ? void 0 : makeLayer((color) => toLight(color, limits.light))
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
function getQRArea(options, count) {
|
|
1132
|
+
const minSize = Math.min(options.width, options.height) - options.margin * 2;
|
|
1133
|
+
const dotSize = Math.floor(minSize / count);
|
|
1134
|
+
return {
|
|
1135
|
+
count,
|
|
1136
|
+
dotSize,
|
|
1137
|
+
x: Math.floor((options.width - count * dotSize) / 2),
|
|
1138
|
+
y: Math.floor((options.height - count * dotSize) / 2)
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
async function prepareArt(options, qr, filter) {
|
|
1142
|
+
const art = options.art;
|
|
1143
|
+
if (!art.image) throw new Error("QR art needs art.image");
|
|
1144
|
+
createCanvas(options, 1, 1);
|
|
1145
|
+
const image = await loadPicture(options, art);
|
|
1146
|
+
const area = getQRArea(options, qr.getModuleCount());
|
|
1147
|
+
return art.mode === "colorize" ? prepareColorize(options, art, image, area) : planHalftone(options, art, image, qr, area, filter);
|
|
1148
|
+
}
|
|
1149
|
+
|
|
524
1150
|
// src/constants/gradientTypes.ts
|
|
525
1151
|
var gradientTypes_default = {
|
|
526
1152
|
radial: "radial",
|
|
@@ -546,7 +1172,7 @@ var dotMask = [
|
|
|
546
1172
|
[0, 0, 0, 0, 0, 0, 0],
|
|
547
1173
|
[0, 0, 0, 0, 0, 0, 0]
|
|
548
1174
|
];
|
|
549
|
-
var QRCanvas = class {
|
|
1175
|
+
var QRCanvas = class _QRCanvas {
|
|
550
1176
|
//TODO don't pass all options to this class
|
|
551
1177
|
constructor(options) {
|
|
552
1178
|
var _a;
|
|
@@ -577,7 +1203,57 @@ var QRCanvas = class {
|
|
|
577
1203
|
canvasContext.clearRect(0, 0, this._canvas.width, this._canvas.height);
|
|
578
1204
|
}
|
|
579
1205
|
}
|
|
1206
|
+
async _drawFramed(qr) {
|
|
1207
|
+
const layout = getFrameLayout(this._options);
|
|
1208
|
+
const inner = new _QRCanvas(innerOptions(this._options, layout.qr.size));
|
|
1209
|
+
await inner.drawQR(qr);
|
|
1210
|
+
const context = this.context;
|
|
1211
|
+
this.clear();
|
|
1212
|
+
if (layout.frame) {
|
|
1213
|
+
const { thickness, radius, color } = layout.frame;
|
|
1214
|
+
context.beginPath();
|
|
1215
|
+
toCanvas(context, roundedRect(0, 0, layout.width, layout.height, radius));
|
|
1216
|
+
context.fillStyle = color;
|
|
1217
|
+
context.fill();
|
|
1218
|
+
context.save();
|
|
1219
|
+
context.beginPath();
|
|
1220
|
+
toCanvas(
|
|
1221
|
+
context,
|
|
1222
|
+
roundedRect(
|
|
1223
|
+
thickness,
|
|
1224
|
+
thickness,
|
|
1225
|
+
layout.width - 2 * thickness,
|
|
1226
|
+
layout.height - 2 * thickness,
|
|
1227
|
+
Math.max(0, radius - thickness)
|
|
1228
|
+
)
|
|
1229
|
+
);
|
|
1230
|
+
context.clip();
|
|
1231
|
+
this.drawBackground();
|
|
1232
|
+
context.restore();
|
|
1233
|
+
if (layout.label) {
|
|
1234
|
+
const { x, y, width, height } = layout.label.band;
|
|
1235
|
+
context.fillStyle = color;
|
|
1236
|
+
context.fillRect(x, y, width, height);
|
|
1237
|
+
}
|
|
1238
|
+
} else {
|
|
1239
|
+
this.drawBackground();
|
|
1240
|
+
}
|
|
1241
|
+
context.drawImage(inner.getCanvas(), layout.qr.x, layout.qr.y);
|
|
1242
|
+
if (layout.label) {
|
|
1243
|
+
const { band, font, color, text } = layout.label;
|
|
1244
|
+
context.font = font;
|
|
1245
|
+
context.fillStyle = color;
|
|
1246
|
+
context.textAlign = "center";
|
|
1247
|
+
context.textBaseline = "middle";
|
|
1248
|
+
context.fillText(text, band.x + band.width / 2, band.y + band.height / 2);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
580
1251
|
async drawQR(qr) {
|
|
1252
|
+
var _a;
|
|
1253
|
+
if (hasFrame(this._options)) {
|
|
1254
|
+
this._qr = qr;
|
|
1255
|
+
return this._drawFramed(qr);
|
|
1256
|
+
}
|
|
581
1257
|
const count = qr.getModuleCount();
|
|
582
1258
|
const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
|
|
583
1259
|
const dotSize = Math.floor(minSize / count);
|
|
@@ -602,23 +1278,39 @@ var QRCanvas = class {
|
|
|
602
1278
|
dotSize
|
|
603
1279
|
});
|
|
604
1280
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
this.drawDots((i, j) => {
|
|
608
|
-
var _a, _b, _c, _d, _e, _f;
|
|
1281
|
+
const filter = (i, j) => {
|
|
1282
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
609
1283
|
if (this._options.imageOptions.hideBackgroundDots) {
|
|
610
1284
|
if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
|
|
611
1285
|
return false;
|
|
612
1286
|
}
|
|
613
1287
|
}
|
|
614
|
-
if (((
|
|
1288
|
+
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
1289
|
return false;
|
|
616
1290
|
}
|
|
617
1291
|
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
1292
|
return false;
|
|
619
1293
|
}
|
|
620
1294
|
return true;
|
|
621
|
-
}
|
|
1295
|
+
};
|
|
1296
|
+
let art;
|
|
1297
|
+
if (this._options.art) {
|
|
1298
|
+
art = await prepareArt(this._options, qr, filter);
|
|
1299
|
+
}
|
|
1300
|
+
this.clear();
|
|
1301
|
+
this.drawBackground();
|
|
1302
|
+
if ((art == null ? void 0 : art.mode) === "halftone") {
|
|
1303
|
+
this._drawArtRects(art.rects);
|
|
1304
|
+
this.drawDots(() => false);
|
|
1305
|
+
} else {
|
|
1306
|
+
if ((art == null ? void 0 : art.mode) === "colorize") {
|
|
1307
|
+
const context = this.context;
|
|
1308
|
+
if (art.background) context.drawImage(art.background, 0, 0);
|
|
1309
|
+
this._dotsFill = (_a = context.createPattern(art.dots, "no-repeat")) != null ? _a : void 0;
|
|
1310
|
+
}
|
|
1311
|
+
this.drawDots(filter);
|
|
1312
|
+
this._dotsFill = void 0;
|
|
1313
|
+
}
|
|
622
1314
|
this.drawCorners();
|
|
623
1315
|
if (this._options.image) {
|
|
624
1316
|
this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
|
|
@@ -665,7 +1357,10 @@ var QRCanvas = class {
|
|
|
665
1357
|
const dotSize = Math.floor(minSize / count);
|
|
666
1358
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
667
1359
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
668
|
-
const
|
|
1360
|
+
const typeDot = new QRDot({ context: canvasContext, type: options.dotsOptions.type });
|
|
1361
|
+
const squareDot = new QRDot({ context: canvasContext, type: "square" });
|
|
1362
|
+
const solidAlignment = needsSolidAlignment(options.dotsOptions.type);
|
|
1363
|
+
const role = solidAlignment ? getModuleRoles(count) : void 0;
|
|
669
1364
|
canvasContext.beginPath();
|
|
670
1365
|
for (let i = 0; i < count; i++) {
|
|
671
1366
|
for (let j = 0; j < count; j++) {
|
|
@@ -675,6 +1370,7 @@ var QRCanvas = class {
|
|
|
675
1370
|
if (!this._qr.isDark(i, j)) {
|
|
676
1371
|
continue;
|
|
677
1372
|
}
|
|
1373
|
+
const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
678
1374
|
dot.draw(
|
|
679
1375
|
xBeginning + i * dotSize,
|
|
680
1376
|
yBeginning + j * dotSize,
|
|
@@ -704,8 +1400,19 @@ var QRCanvas = class {
|
|
|
704
1400
|
} else if (options.dotsOptions.color) {
|
|
705
1401
|
canvasContext.fillStyle = canvasContext.strokeStyle = options.dotsOptions.color;
|
|
706
1402
|
}
|
|
1403
|
+
if (this._dotsFill) {
|
|
1404
|
+
canvasContext.fillStyle = canvasContext.strokeStyle = this._dotsFill;
|
|
1405
|
+
}
|
|
707
1406
|
canvasContext.fill("evenodd");
|
|
708
1407
|
}
|
|
1408
|
+
_drawArtRects(rects) {
|
|
1409
|
+
const context = this.context;
|
|
1410
|
+
if (!context) return;
|
|
1411
|
+
rects.forEach(({ x, y, width, height, color }) => {
|
|
1412
|
+
context.fillStyle = color;
|
|
1413
|
+
context.fillRect(x, y, width, height);
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
709
1416
|
drawCorners(filter) {
|
|
710
1417
|
if (!this._qr) {
|
|
711
1418
|
throw "QR code is not defined";
|
|
@@ -720,6 +1427,7 @@ var QRCanvas = class {
|
|
|
720
1427
|
const dotSize = Math.floor(minSize / count);
|
|
721
1428
|
const cornersSquareSize = dotSize * 7;
|
|
722
1429
|
const cornersDotSize = dotSize * 3;
|
|
1430
|
+
const cornerTypes = getCornerTypes(options);
|
|
723
1431
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
724
1432
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
725
1433
|
[
|
|
@@ -727,14 +1435,14 @@ var QRCanvas = class {
|
|
|
727
1435
|
[1, 0, Math.PI / 2],
|
|
728
1436
|
[0, 1, -Math.PI / 2]
|
|
729
1437
|
].forEach(([column, row, rotation]) => {
|
|
730
|
-
var _a, _b, _c, _d, _e, _f
|
|
1438
|
+
var _a, _b, _c, _d, _e, _f;
|
|
731
1439
|
if (filter && !filter(column, row)) {
|
|
732
1440
|
return;
|
|
733
1441
|
}
|
|
734
1442
|
const x = xBeginning + column * dotSize * (count - 7);
|
|
735
1443
|
const y = yBeginning + row * dotSize * (count - 7);
|
|
736
|
-
if (
|
|
737
|
-
const cornersSquare = new QRCornerSquare({ context: canvasContext, type:
|
|
1444
|
+
if (cornerTypes.square) {
|
|
1445
|
+
const cornersSquare = new QRCornerSquare({ context: canvasContext, type: cornerTypes.square });
|
|
738
1446
|
canvasContext.beginPath();
|
|
739
1447
|
cornersSquare.draw(x, y, cornersSquareSize, rotation);
|
|
740
1448
|
} else {
|
|
@@ -742,7 +1450,7 @@ var QRCanvas = class {
|
|
|
742
1450
|
canvasContext.beginPath();
|
|
743
1451
|
for (let i = 0; i < squareMask.length; i++) {
|
|
744
1452
|
for (let j = 0; j < squareMask[i].length; j++) {
|
|
745
|
-
if (!((
|
|
1453
|
+
if (!((_a = squareMask[i]) == null ? void 0 : _a[j])) {
|
|
746
1454
|
continue;
|
|
747
1455
|
}
|
|
748
1456
|
dot.draw(
|
|
@@ -757,7 +1465,7 @@ var QRCanvas = class {
|
|
|
757
1465
|
}
|
|
758
1466
|
}
|
|
759
1467
|
}
|
|
760
|
-
if ((
|
|
1468
|
+
if ((_b = options.cornersSquareOptions) == null ? void 0 : _b.gradient) {
|
|
761
1469
|
const gradientOptions = options.cornersSquareOptions.gradient;
|
|
762
1470
|
const gradient = this._createGradient({
|
|
763
1471
|
context: canvasContext,
|
|
@@ -771,12 +1479,12 @@ var QRCanvas = class {
|
|
|
771
1479
|
gradient.addColorStop(offset, color);
|
|
772
1480
|
});
|
|
773
1481
|
canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
|
|
774
|
-
} else if ((
|
|
1482
|
+
} else if ((_c = options.cornersSquareOptions) == null ? void 0 : _c.color) {
|
|
775
1483
|
canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersSquareOptions.color;
|
|
776
1484
|
}
|
|
777
1485
|
canvasContext.fill("evenodd");
|
|
778
|
-
if (
|
|
779
|
-
const cornersDot = new QRCornerDot({ context: canvasContext, type:
|
|
1486
|
+
if (cornerTypes.dot) {
|
|
1487
|
+
const cornersDot = new QRCornerDot({ context: canvasContext, type: cornerTypes.dot });
|
|
780
1488
|
canvasContext.beginPath();
|
|
781
1489
|
cornersDot.draw(x + dotSize * 2, y + dotSize * 2, cornersDotSize, rotation);
|
|
782
1490
|
} else {
|
|
@@ -784,7 +1492,7 @@ var QRCanvas = class {
|
|
|
784
1492
|
canvasContext.beginPath();
|
|
785
1493
|
for (let i = 0; i < dotMask.length; i++) {
|
|
786
1494
|
for (let j = 0; j < dotMask[i].length; j++) {
|
|
787
|
-
if (!((
|
|
1495
|
+
if (!((_d = dotMask[i]) == null ? void 0 : _d[j])) {
|
|
788
1496
|
continue;
|
|
789
1497
|
}
|
|
790
1498
|
dot.draw(
|
|
@@ -799,7 +1507,7 @@ var QRCanvas = class {
|
|
|
799
1507
|
}
|
|
800
1508
|
}
|
|
801
1509
|
}
|
|
802
|
-
if ((
|
|
1510
|
+
if ((_e = options.cornersDotOptions) == null ? void 0 : _e.gradient) {
|
|
803
1511
|
const gradientOptions = options.cornersDotOptions.gradient;
|
|
804
1512
|
const gradient = this._createGradient({
|
|
805
1513
|
context: canvasContext,
|
|
@@ -813,7 +1521,7 @@ var QRCanvas = class {
|
|
|
813
1521
|
gradient.addColorStop(offset, color);
|
|
814
1522
|
});
|
|
815
1523
|
canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
|
|
816
|
-
} else if ((
|
|
1524
|
+
} else if ((_f = options.cornersDotOptions) == null ? void 0 : _f.color) {
|
|
817
1525
|
canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersDotOptions.color;
|
|
818
1526
|
}
|
|
819
1527
|
canvasContext.fill("evenodd");
|
|
@@ -869,6 +1577,13 @@ var QRCanvas = class {
|
|
|
869
1577
|
const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
|
|
870
1578
|
const dw = width - options.imageOptions.margin * 2;
|
|
871
1579
|
const dh = height - options.imageOptions.margin * 2;
|
|
1580
|
+
if (options.imageOptions.backgroundColor) {
|
|
1581
|
+
const plate = getLogoPlate(options, dx, dy, width, height);
|
|
1582
|
+
canvasContext.beginPath();
|
|
1583
|
+
toCanvas(canvasContext, roundedRect(plate.x, plate.y, plate.width, plate.height, plate.radius));
|
|
1584
|
+
canvasContext.fillStyle = options.imageOptions.backgroundColor;
|
|
1585
|
+
canvasContext.fill();
|
|
1586
|
+
}
|
|
872
1587
|
canvasContext.drawImage(this._image, dx, dy, dw < 0 ? 0 : dw, dh < 0 ? 0 : dh);
|
|
873
1588
|
}
|
|
874
1589
|
_createGradient({
|
|
@@ -942,12 +1657,32 @@ var QRDot2 = class {
|
|
|
942
1657
|
case dotTypes_default.extraRounded:
|
|
943
1658
|
drawFunction = this._drawExtraRounded;
|
|
944
1659
|
break;
|
|
1660
|
+
case dotTypes_default.diamond:
|
|
1661
|
+
drawFunction = (args) => this._drawShape(diamond(args.x, args.y, args.size));
|
|
1662
|
+
break;
|
|
1663
|
+
case dotTypes_default.smallSquare:
|
|
1664
|
+
drawFunction = (args) => this._drawShape(smallSquare(args.x, args.y, args.size));
|
|
1665
|
+
break;
|
|
1666
|
+
case dotTypes_default.verticalLines:
|
|
1667
|
+
case dotTypes_default.horizontalLines: {
|
|
1668
|
+
const vertical = type === dotTypes_default.verticalLines;
|
|
1669
|
+
drawFunction = (args) => {
|
|
1670
|
+
const before = args.getNeighbor ? args.getNeighbor(vertical ? 0 : -1, vertical ? -1 : 0) : false;
|
|
1671
|
+
const after = args.getNeighbor ? args.getNeighbor(vertical ? 0 : 1, vertical ? 1 : 0) : false;
|
|
1672
|
+
this._drawShape(line(args.x, args.y, args.size, vertical, before, after));
|
|
1673
|
+
};
|
|
1674
|
+
break;
|
|
1675
|
+
}
|
|
945
1676
|
case dotTypes_default.square:
|
|
946
1677
|
default:
|
|
947
1678
|
drawFunction = this._drawSquare;
|
|
948
1679
|
}
|
|
949
1680
|
drawFunction.call(this, { x, y, size, getNeighbor });
|
|
950
1681
|
}
|
|
1682
|
+
_drawShape(shape) {
|
|
1683
|
+
this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
1684
|
+
this._element.setAttribute("d", toSVGPath(shape));
|
|
1685
|
+
}
|
|
951
1686
|
_rotateFigure({ x, y, size, rotation = 0, draw }) {
|
|
952
1687
|
var _a;
|
|
953
1688
|
const cx = x + size / 2;
|
|
@@ -1183,6 +1918,13 @@ var QRCornerSquare2 = class {
|
|
|
1183
1918
|
case cornerSquareTypes_default.extraRounded:
|
|
1184
1919
|
drawFunction = this._drawExtraRounded;
|
|
1185
1920
|
break;
|
|
1921
|
+
case cornerSquareTypes_default.rounded:
|
|
1922
|
+
drawFunction = (args) => {
|
|
1923
|
+
this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
1924
|
+
this._element.setAttribute("clip-rule", "evenodd");
|
|
1925
|
+
this._element.setAttribute("d", toSVGPath(roundedRing(args.x, args.y, args.size)));
|
|
1926
|
+
};
|
|
1927
|
+
break;
|
|
1186
1928
|
case cornerSquareTypes_default.dot:
|
|
1187
1929
|
default:
|
|
1188
1930
|
drawFunction = this._drawDot;
|
|
@@ -1267,6 +2009,12 @@ var QRCornerDot2 = class {
|
|
|
1267
2009
|
case cornerDotTypes_default.square:
|
|
1268
2010
|
drawFunction = this._drawSquare;
|
|
1269
2011
|
break;
|
|
2012
|
+
case cornerDotTypes_default.rounded:
|
|
2013
|
+
drawFunction = (args) => {
|
|
2014
|
+
this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
2015
|
+
this._element.setAttribute("d", toSVGPath(roundedSquare(args.x, args.y, args.size)));
|
|
2016
|
+
};
|
|
2017
|
+
break;
|
|
1270
2018
|
case cornerDotTypes_default.dot:
|
|
1271
2019
|
default:
|
|
1272
2020
|
drawFunction = this._drawDot;
|
|
@@ -1313,104 +2061,6 @@ var QRCornerDot2 = class {
|
|
|
1313
2061
|
}
|
|
1314
2062
|
};
|
|
1315
2063
|
|
|
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 "&";
|
|
1322
|
-
case "<":
|
|
1323
|
-
return "<";
|
|
1324
|
-
case ">":
|
|
1325
|
-
return ">";
|
|
1326
|
-
case '"':
|
|
1327
|
-
return """;
|
|
1328
|
-
case " ":
|
|
1329
|
-
return "	";
|
|
1330
|
-
case "\n":
|
|
1331
|
-
return "
";
|
|
1332
|
-
default:
|
|
1333
|
-
return "
";
|
|
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
2064
|
// src/tools/imageSize.ts
|
|
1415
2065
|
var decodeBase64 = (base64) => {
|
|
1416
2066
|
if (typeof atob === "function") {
|
|
@@ -1560,13 +2210,16 @@ var dotMask2 = [
|
|
|
1560
2210
|
[0, 0, 0, 0, 0, 0, 0],
|
|
1561
2211
|
[0, 0, 0, 0, 0, 0, 0]
|
|
1562
2212
|
];
|
|
1563
|
-
var QRSVG = class {
|
|
2213
|
+
var QRSVG = class _QRSVG {
|
|
1564
2214
|
//TODO don't pass all options to this class
|
|
1565
|
-
constructor(options) {
|
|
2215
|
+
constructor(options, sharedWindow) {
|
|
1566
2216
|
/** True when no browser DOM or jsdom is available and the built-in virtual DOM is used. */
|
|
1567
2217
|
this._virtual = false;
|
|
1568
2218
|
var _a;
|
|
1569
|
-
if (
|
|
2219
|
+
if (sharedWindow) {
|
|
2220
|
+
this._window = sharedWindow;
|
|
2221
|
+
this._virtual = !!sharedWindow.isVirtual;
|
|
2222
|
+
} else if (options.jsdom) {
|
|
1570
2223
|
this._window = new options.jsdom("", { resources: "usable" }).window;
|
|
1571
2224
|
} else if (hasBrowserDom()) {
|
|
1572
2225
|
this._window = window;
|
|
@@ -1608,7 +2261,67 @@ var QRSVG = class {
|
|
|
1608
2261
|
this._defs = this._window.document.createElementNS("http://www.w3.org/2000/svg", "defs");
|
|
1609
2262
|
this._element.appendChild(this._defs);
|
|
1610
2263
|
}
|
|
2264
|
+
_svgElement(name, attributes) {
|
|
2265
|
+
const element = this._window.document.createElementNS("http://www.w3.org/2000/svg", name);
|
|
2266
|
+
Object.keys(attributes).forEach((key) => element.setAttribute(key, String(attributes[key])));
|
|
2267
|
+
return element;
|
|
2268
|
+
}
|
|
2269
|
+
async _drawFramed(qr) {
|
|
2270
|
+
const layout = getFrameLayout(this._options);
|
|
2271
|
+
const inner = new _QRSVG(innerOptions(this._options, layout.qr.size), this._window);
|
|
2272
|
+
await inner.drawQR(qr);
|
|
2273
|
+
this.clear();
|
|
2274
|
+
if (layout.frame) {
|
|
2275
|
+
const { thickness, radius, color } = layout.frame;
|
|
2276
|
+
this._element.appendChild(
|
|
2277
|
+
this._svgElement("path", { d: toSVGPath(roundedRect(0, 0, layout.width, layout.height, radius)), fill: color })
|
|
2278
|
+
);
|
|
2279
|
+
this._element.appendChild(
|
|
2280
|
+
this._svgElement("path", {
|
|
2281
|
+
d: toSVGPath(
|
|
2282
|
+
roundedRect(
|
|
2283
|
+
thickness,
|
|
2284
|
+
thickness,
|
|
2285
|
+
layout.width - 2 * thickness,
|
|
2286
|
+
layout.height - 2 * thickness,
|
|
2287
|
+
Math.max(0, radius - thickness)
|
|
2288
|
+
)
|
|
2289
|
+
),
|
|
2290
|
+
fill: layout.background
|
|
2291
|
+
})
|
|
2292
|
+
);
|
|
2293
|
+
if (layout.label) {
|
|
2294
|
+
const { x, y, width, height } = layout.label.band;
|
|
2295
|
+
this._element.appendChild(this._svgElement("rect", { x, y, width, height, fill: color }));
|
|
2296
|
+
}
|
|
2297
|
+
} else {
|
|
2298
|
+
this.drawBackground();
|
|
2299
|
+
}
|
|
2300
|
+
const element = inner.getElement();
|
|
2301
|
+
element.setAttribute("x", String(layout.qr.x));
|
|
2302
|
+
element.setAttribute("y", String(layout.qr.y));
|
|
2303
|
+
this._element.appendChild(element);
|
|
2304
|
+
if (layout.label) {
|
|
2305
|
+
const { band, color, text, fontSize, fontFamily, fontWeight } = layout.label;
|
|
2306
|
+
const label = this._svgElement("text", {
|
|
2307
|
+
x: band.x + band.width / 2,
|
|
2308
|
+
y: band.y + band.height / 2,
|
|
2309
|
+
fill: color,
|
|
2310
|
+
"font-family": fontFamily,
|
|
2311
|
+
"font-size": fontSize,
|
|
2312
|
+
"font-weight": fontWeight,
|
|
2313
|
+
"text-anchor": "middle",
|
|
2314
|
+
"dominant-baseline": "central"
|
|
2315
|
+
});
|
|
2316
|
+
label.textContent = text;
|
|
2317
|
+
this._element.appendChild(label);
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
1611
2320
|
async drawQR(qr) {
|
|
2321
|
+
if (hasFrame(this._options)) {
|
|
2322
|
+
this._qr = qr;
|
|
2323
|
+
return this._drawFramed(qr);
|
|
2324
|
+
}
|
|
1612
2325
|
const count = qr.getModuleCount();
|
|
1613
2326
|
const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
|
|
1614
2327
|
const dotSize = Math.floor(minSize / count);
|
|
@@ -1634,9 +2347,7 @@ var QRSVG = class {
|
|
|
1634
2347
|
dotSize
|
|
1635
2348
|
});
|
|
1636
2349
|
}
|
|
1637
|
-
|
|
1638
|
-
this.drawBackground();
|
|
1639
|
-
this.drawDots((i, j) => {
|
|
2350
|
+
const filter = (i, j) => {
|
|
1640
2351
|
var _a, _b, _c, _d, _e, _f;
|
|
1641
2352
|
if (this._options.imageOptions.hideBackgroundDots) {
|
|
1642
2353
|
if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
|
|
@@ -1650,7 +2361,24 @@ var QRSVG = class {
|
|
|
1650
2361
|
return false;
|
|
1651
2362
|
}
|
|
1652
2363
|
return true;
|
|
1653
|
-
}
|
|
2364
|
+
};
|
|
2365
|
+
let art;
|
|
2366
|
+
if (this._options.art) {
|
|
2367
|
+
art = await prepareArt(this._options, qr, filter);
|
|
2368
|
+
}
|
|
2369
|
+
this.clear();
|
|
2370
|
+
this.drawBackground();
|
|
2371
|
+
if ((art == null ? void 0 : art.mode) === "halftone") {
|
|
2372
|
+
this._drawArtRects(art.rects);
|
|
2373
|
+
this.drawDots(() => false);
|
|
2374
|
+
} else {
|
|
2375
|
+
if ((art == null ? void 0 : art.mode) === "colorize") {
|
|
2376
|
+
if (art.background) this._appendImage(art.background.toDataURL("image/png"), 0, 0);
|
|
2377
|
+
this._dotsImageHref = art.dots.toDataURL("image/png");
|
|
2378
|
+
}
|
|
2379
|
+
this.drawDots(filter);
|
|
2380
|
+
this._dotsImageHref = void 0;
|
|
2381
|
+
}
|
|
1654
2382
|
this.drawCorners();
|
|
1655
2383
|
if (this._options.image) {
|
|
1656
2384
|
this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
|
|
@@ -1691,11 +2419,14 @@ var QRSVG = class {
|
|
|
1691
2419
|
const dotSize = Math.floor(minSize / count);
|
|
1692
2420
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1693
2421
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1694
|
-
const
|
|
2422
|
+
const typeDot = new QRDot2({
|
|
1695
2423
|
svg: this._element,
|
|
1696
2424
|
type: options.dotsOptions.type,
|
|
1697
2425
|
window: this._window
|
|
1698
2426
|
});
|
|
2427
|
+
const squareDot = new QRDot2({ svg: this._element, type: "square", window: this._window });
|
|
2428
|
+
const solidAlignment = needsSolidAlignment(options.dotsOptions.type);
|
|
2429
|
+
const role = solidAlignment ? getModuleRoles(count) : void 0;
|
|
1699
2430
|
this._dotsClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
1700
2431
|
this._dotsClipPath.setAttribute("id", "clip-path-dot-color");
|
|
1701
2432
|
this._defs.appendChild(this._dotsClipPath);
|
|
@@ -1707,7 +2438,8 @@ var QRSVG = class {
|
|
|
1707
2438
|
y: yBeginning,
|
|
1708
2439
|
height: count * dotSize,
|
|
1709
2440
|
width: count * dotSize,
|
|
1710
|
-
name: "dot-color"
|
|
2441
|
+
name: "dot-color",
|
|
2442
|
+
imageHref: this._dotsImageHref
|
|
1711
2443
|
});
|
|
1712
2444
|
for (let i = 0; i < count; i++) {
|
|
1713
2445
|
for (let j = 0; j < count; j++) {
|
|
@@ -1717,6 +2449,7 @@ var QRSVG = class {
|
|
|
1717
2449
|
if (!((_b = this._qr) == null ? void 0 : _b.isDark(i, j))) {
|
|
1718
2450
|
continue;
|
|
1719
2451
|
}
|
|
2452
|
+
const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
1720
2453
|
dot.draw(
|
|
1721
2454
|
xBeginning + i * dotSize,
|
|
1722
2455
|
yBeginning + j * dotSize,
|
|
@@ -1747,6 +2480,7 @@ var QRSVG = class {
|
|
|
1747
2480
|
const dotSize = Math.floor(minSize / count);
|
|
1748
2481
|
const cornersSquareSize = dotSize * 7;
|
|
1749
2482
|
const cornersDotSize = dotSize * 3;
|
|
2483
|
+
const cornerTypes = getCornerTypes(options);
|
|
1750
2484
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1751
2485
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1752
2486
|
[
|
|
@@ -1754,7 +2488,7 @@ var QRSVG = class {
|
|
|
1754
2488
|
[1, 0, Math.PI / 2],
|
|
1755
2489
|
[0, 1, -Math.PI / 2]
|
|
1756
2490
|
].forEach(([column, row, rotation]) => {
|
|
1757
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j
|
|
2491
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1758
2492
|
const x = xBeginning + column * dotSize * (count - 7);
|
|
1759
2493
|
const y = yBeginning + row * dotSize * (count - 7);
|
|
1760
2494
|
let cornersSquareClipPath = this._dotsClipPath;
|
|
@@ -1775,10 +2509,10 @@ var QRSVG = class {
|
|
|
1775
2509
|
name: `corners-square-color-${column}-${row}`
|
|
1776
2510
|
});
|
|
1777
2511
|
}
|
|
1778
|
-
if (
|
|
2512
|
+
if (cornerTypes.square) {
|
|
1779
2513
|
const cornersSquare = new QRCornerSquare2({
|
|
1780
2514
|
svg: this._element,
|
|
1781
|
-
type:
|
|
2515
|
+
type: cornerTypes.square,
|
|
1782
2516
|
window: this._window
|
|
1783
2517
|
});
|
|
1784
2518
|
cornersSquare.draw(x, y, cornersSquareSize, rotation);
|
|
@@ -1793,7 +2527,7 @@ var QRSVG = class {
|
|
|
1793
2527
|
});
|
|
1794
2528
|
for (let i = 0; i < squareMask2.length; i++) {
|
|
1795
2529
|
for (let j = 0; j < squareMask2[i].length; j++) {
|
|
1796
|
-
if (!((
|
|
2530
|
+
if (!((_e = squareMask2[i]) == null ? void 0 : _e[j])) {
|
|
1797
2531
|
continue;
|
|
1798
2532
|
}
|
|
1799
2533
|
dot.draw(
|
|
@@ -1811,14 +2545,14 @@ var QRSVG = class {
|
|
|
1811
2545
|
}
|
|
1812
2546
|
}
|
|
1813
2547
|
}
|
|
1814
|
-
if (((
|
|
2548
|
+
if (((_f = options.cornersDotOptions) == null ? void 0 : _f.gradient) || ((_g = options.cornersDotOptions) == null ? void 0 : _g.color)) {
|
|
1815
2549
|
cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
1816
2550
|
cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
|
|
1817
2551
|
this._defs.appendChild(cornersDotClipPath);
|
|
1818
2552
|
this._cornersDotClipPath = cornersDotClipPath;
|
|
1819
2553
|
this._createColor({
|
|
1820
|
-
options: (
|
|
1821
|
-
color: (
|
|
2554
|
+
options: (_h = options.cornersDotOptions) == null ? void 0 : _h.gradient,
|
|
2555
|
+
color: (_i = options.cornersDotOptions) == null ? void 0 : _i.color,
|
|
1822
2556
|
additionalRotation: rotation,
|
|
1823
2557
|
x: x + dotSize * 2,
|
|
1824
2558
|
y: y + dotSize * 2,
|
|
@@ -1827,10 +2561,10 @@ var QRSVG = class {
|
|
|
1827
2561
|
name: `corners-dot-color-${column}-${row}`
|
|
1828
2562
|
});
|
|
1829
2563
|
}
|
|
1830
|
-
if (
|
|
2564
|
+
if (cornerTypes.dot) {
|
|
1831
2565
|
const cornersDot = new QRCornerDot2({
|
|
1832
2566
|
svg: this._element,
|
|
1833
|
-
type:
|
|
2567
|
+
type: cornerTypes.dot,
|
|
1834
2568
|
window: this._window
|
|
1835
2569
|
});
|
|
1836
2570
|
cornersDot.draw(x + dotSize * 2, y + dotSize * 2, cornersDotSize, rotation);
|
|
@@ -1845,7 +2579,7 @@ var QRSVG = class {
|
|
|
1845
2579
|
});
|
|
1846
2580
|
for (let i = 0; i < dotMask2.length; i++) {
|
|
1847
2581
|
for (let j = 0; j < dotMask2[i].length; j++) {
|
|
1848
|
-
if (!((
|
|
2582
|
+
if (!((_j = dotMask2[i]) == null ? void 0 : _j[j])) {
|
|
1849
2583
|
continue;
|
|
1850
2584
|
}
|
|
1851
2585
|
dot.draw(
|
|
@@ -1865,6 +2599,26 @@ var QRSVG = class {
|
|
|
1865
2599
|
}
|
|
1866
2600
|
});
|
|
1867
2601
|
}
|
|
2602
|
+
_drawArtRects(rects) {
|
|
2603
|
+
rects.forEach(({ x, y, width, height, color }) => {
|
|
2604
|
+
const rect2 = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
2605
|
+
rect2.setAttribute("x", String(x));
|
|
2606
|
+
rect2.setAttribute("y", String(y));
|
|
2607
|
+
rect2.setAttribute("width", String(width));
|
|
2608
|
+
rect2.setAttribute("height", String(height));
|
|
2609
|
+
rect2.setAttribute("fill", color);
|
|
2610
|
+
this._element.appendChild(rect2);
|
|
2611
|
+
});
|
|
2612
|
+
}
|
|
2613
|
+
_appendImage(href, x, y) {
|
|
2614
|
+
const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
|
|
2615
|
+
image.setAttribute("href", href);
|
|
2616
|
+
image.setAttribute("x", String(x));
|
|
2617
|
+
image.setAttribute("y", String(y));
|
|
2618
|
+
image.setAttribute("width", String(this._options.width));
|
|
2619
|
+
image.setAttribute("height", String(this._options.height));
|
|
2620
|
+
this._element.appendChild(image);
|
|
2621
|
+
}
|
|
1868
2622
|
imageToBlob() {
|
|
1869
2623
|
var _a;
|
|
1870
2624
|
if (!this._image) return;
|
|
@@ -1931,6 +2685,15 @@ var QRSVG = class {
|
|
|
1931
2685
|
const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
|
|
1932
2686
|
const dw = width - options.imageOptions.margin * 2;
|
|
1933
2687
|
const dh = height - options.imageOptions.margin * 2;
|
|
2688
|
+
if (options.imageOptions.backgroundColor) {
|
|
2689
|
+
const plate = getLogoPlate(options, dx, dy, width, height);
|
|
2690
|
+
this._element.appendChild(
|
|
2691
|
+
this._svgElement("path", {
|
|
2692
|
+
d: toSVGPath(roundedRect(plate.x, plate.y, plate.width, plate.height, plate.radius)),
|
|
2693
|
+
fill: options.imageOptions.backgroundColor
|
|
2694
|
+
})
|
|
2695
|
+
);
|
|
2696
|
+
}
|
|
1934
2697
|
const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
|
|
1935
2698
|
image.setAttribute("href", this._imageUri || "");
|
|
1936
2699
|
image.setAttribute("x", String(dx));
|
|
@@ -1947,15 +2710,27 @@ var QRSVG = class {
|
|
|
1947
2710
|
y,
|
|
1948
2711
|
height,
|
|
1949
2712
|
width,
|
|
1950
|
-
name
|
|
2713
|
+
name,
|
|
2714
|
+
imageHref
|
|
1951
2715
|
}) {
|
|
1952
2716
|
const size = width > height ? width : height;
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
2717
|
+
if (imageHref) {
|
|
2718
|
+
const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
|
|
2719
|
+
image.setAttribute("href", imageHref);
|
|
2720
|
+
image.setAttribute("x", "0");
|
|
2721
|
+
image.setAttribute("y", "0");
|
|
2722
|
+
image.setAttribute("width", String(this._options.width));
|
|
2723
|
+
image.setAttribute("height", String(this._options.height));
|
|
2724
|
+
image.setAttribute("clip-path", `url('#clip-path-${name}')`);
|
|
2725
|
+
this._element.appendChild(image);
|
|
2726
|
+
return;
|
|
2727
|
+
}
|
|
2728
|
+
const rect2 = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
2729
|
+
rect2.setAttribute("x", String(x));
|
|
2730
|
+
rect2.setAttribute("y", String(y));
|
|
2731
|
+
rect2.setAttribute("height", String(height));
|
|
2732
|
+
rect2.setAttribute("width", String(width));
|
|
2733
|
+
rect2.setAttribute("clip-path", `url('#clip-path-${name}')`);
|
|
1959
2734
|
if (options) {
|
|
1960
2735
|
let gradient;
|
|
1961
2736
|
if (options.type === gradientTypes_default.radial) {
|
|
@@ -2009,12 +2784,12 @@ var QRSVG = class {
|
|
|
2009
2784
|
stop.setAttribute("stop-color", color2);
|
|
2010
2785
|
gradient.appendChild(stop);
|
|
2011
2786
|
});
|
|
2012
|
-
|
|
2787
|
+
rect2.setAttribute("fill", `url('#${name}')`);
|
|
2013
2788
|
this._defs.appendChild(gradient);
|
|
2014
2789
|
} else if (color) {
|
|
2015
|
-
|
|
2790
|
+
rect2.setAttribute("fill", color);
|
|
2016
2791
|
}
|
|
2017
|
-
this._element.appendChild(
|
|
2792
|
+
this._element.appendChild(rect2);
|
|
2018
2793
|
}
|
|
2019
2794
|
};
|
|
2020
2795
|
|
|
@@ -2275,6 +3050,67 @@ ${serializer.serializeToString(svg.getElement())}`;
|
|
|
2275
3050
|
const png = new Resvg(_QRCodeStyling._normalizeSVGForResvg(svgString), { fitTo: { mode: "original" } }).render().asPng();
|
|
2276
3051
|
return typeof Buffer !== "undefined" ? Buffer.from(png) : png;
|
|
2277
3052
|
}
|
|
3053
|
+
/** Draws the current QR code to RGBA pixels, using a canvas or, failing that, resvg. */
|
|
3054
|
+
async _getPixels() {
|
|
3055
|
+
if (this._canUseCanvas()) {
|
|
3056
|
+
const canvas = (await this._getQRStylingElement("png")).getCanvas();
|
|
3057
|
+
const context = canvas.getContext("2d");
|
|
3058
|
+
return {
|
|
3059
|
+
data: context.getImageData(0, 0, canvas.width, canvas.height).data,
|
|
3060
|
+
width: canvas.width,
|
|
3061
|
+
height: canvas.height
|
|
3062
|
+
};
|
|
3063
|
+
}
|
|
3064
|
+
if (this._options.resvg) {
|
|
3065
|
+
const { svgString } = await this._getSVGString();
|
|
3066
|
+
const rendered = new this._options.resvg(_QRCodeStyling._normalizeSVGForResvg(svgString), {
|
|
3067
|
+
fitTo: { mode: "original" }
|
|
3068
|
+
}).render();
|
|
3069
|
+
return { data: new Uint8ClampedArray(rendered.pixels), width: rendered.width, height: rendered.height };
|
|
3070
|
+
}
|
|
3071
|
+
throw new Error(
|
|
3072
|
+
"verify() needs to draw the QR code: in a browser this works as is; on a server pass nodeCanvas or resvg."
|
|
3073
|
+
);
|
|
3074
|
+
}
|
|
3075
|
+
async _decode(decoder) {
|
|
3076
|
+
const { data, width, height } = await this._getPixels();
|
|
3077
|
+
const pad = Math.max(16, Math.round(Math.min(width, height) / 8));
|
|
3078
|
+
const paddedWidth = width + pad * 2;
|
|
3079
|
+
const paddedHeight = height + pad * 2;
|
|
3080
|
+
const padded = new Uint8ClampedArray(paddedWidth * paddedHeight * 4).fill(255);
|
|
3081
|
+
for (let y = 0; y < height; y++) {
|
|
3082
|
+
padded.set(data.subarray(y * width * 4, (y + 1) * width * 4), ((y + pad) * paddedWidth + pad) * 4);
|
|
3083
|
+
}
|
|
3084
|
+
const result = decoder(padded, paddedWidth, paddedHeight, { inversionAttempts: "attemptBoth" });
|
|
3085
|
+
return result ? result.data : void 0;
|
|
3086
|
+
}
|
|
3087
|
+
/**
|
|
3088
|
+
* Checks that the QR code can be scanned by decoding the rendered picture.
|
|
3089
|
+
* With `adjust: true` and QR art, raises art.contrast step by step until it scans.
|
|
3090
|
+
*/
|
|
3091
|
+
async verify({ decoder, adjust = false }) {
|
|
3092
|
+
if (!this._qr) throw "QR code is empty";
|
|
3093
|
+
if (typeof decoder !== "function")
|
|
3094
|
+
throw new Error("verify() needs a decoder, for example jsQR from the jsqr package");
|
|
3095
|
+
let attempts = 1;
|
|
3096
|
+
let decoded = await this._decode(decoder);
|
|
3097
|
+
const art = this._options.art;
|
|
3098
|
+
if (decoded !== this._options.data && adjust && art) {
|
|
3099
|
+
const start = getContrast(art);
|
|
3100
|
+
for (const contrast of [0.65, 0.8, 0.9, 1].filter((value) => value > start)) {
|
|
3101
|
+
attempts++;
|
|
3102
|
+
this.update({ art: { ...art, contrast } });
|
|
3103
|
+
decoded = await this._decode(decoder);
|
|
3104
|
+
if (decoded === this._options.data) break;
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
return {
|
|
3108
|
+
readable: decoded === this._options.data,
|
|
3109
|
+
decoded,
|
|
3110
|
+
contrast: this._options.art ? getContrast(this._options.art) : void 0,
|
|
3111
|
+
attempts
|
|
3112
|
+
};
|
|
3113
|
+
}
|
|
2278
3114
|
async getRawData(extension = "png") {
|
|
2279
3115
|
if (!this._qr) throw "QR code is empty";
|
|
2280
3116
|
if (extension.toLowerCase() !== "svg" && !this._canUseCanvas() && this._options.resvg) {
|