qr-code-styling-node 1.8.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 +49 -4
- package/lib/art/functionModules.d.ts +1 -1
- package/lib/core/QRCanvas.d.ts +1 -0
- package/lib/core/QROptions.d.ts +2 -0
- package/lib/core/QRSVG.d.ts +3 -1
- 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 +539 -134
- 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 +31 -3
- package/package.json +3 -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,177 @@ 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
|
+
|
|
524
833
|
// src/tools/svgDom.ts
|
|
525
834
|
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
526
835
|
var escapeAttribute = (value) => value.replace(/[&<>"\t\n\r]/g, (char) => {
|
|
@@ -547,8 +856,16 @@ var VirtualSVGElement = class _VirtualSVGElement {
|
|
|
547
856
|
this.parentNode = null;
|
|
548
857
|
this.childNodes = [];
|
|
549
858
|
this._attributes = /* @__PURE__ */ new Map();
|
|
859
|
+
this._text = "";
|
|
550
860
|
this.tagName = tagName;
|
|
551
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
|
+
}
|
|
552
869
|
get nodeName() {
|
|
553
870
|
return this.tagName;
|
|
554
871
|
}
|
|
@@ -587,6 +904,7 @@ var VirtualSVGElement = class _VirtualSVGElement {
|
|
|
587
904
|
cloneNode(deep = false) {
|
|
588
905
|
const clone = new _VirtualSVGElement(this.tagName);
|
|
589
906
|
this._attributes.forEach((value, name) => clone.setAttribute(name, value));
|
|
907
|
+
if (deep) clone._text = this._text;
|
|
590
908
|
if (deep) this.childNodes.forEach((child) => clone.appendChild(child.cloneNode(true)));
|
|
591
909
|
return clone;
|
|
592
910
|
}
|
|
@@ -596,8 +914,9 @@ var VirtualSVGElement = class _VirtualSVGElement {
|
|
|
596
914
|
this._attributes.forEach((value, name) => {
|
|
597
915
|
markup += ` ${name}="${escapeAttribute(value)}"`;
|
|
598
916
|
});
|
|
599
|
-
if (!this.childNodes.length) return `${markup}/>`;
|
|
917
|
+
if (!this.childNodes.length && !this._text) return `${markup}/>`;
|
|
600
918
|
markup += ">";
|
|
919
|
+
markup += this._text.replace(/[&<>]/g, (char) => char === "&" ? "&" : char === "<" ? "<" : ">");
|
|
601
920
|
for (const child of this.childNodes) markup += child.serialize(false);
|
|
602
921
|
return `${markup}</${this.tagName}>`;
|
|
603
922
|
}
|
|
@@ -619,92 +938,6 @@ var createVirtualWindow = () => ({
|
|
|
619
938
|
});
|
|
620
939
|
var hasBrowserDom = () => typeof window !== "undefined" && typeof document !== "undefined" && typeof document.createElementNS === "function";
|
|
621
940
|
|
|
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
941
|
// src/art/art.ts
|
|
709
942
|
var DEFAULT_CONTRAST = { halftone: 0.5, colorize: 0.7 };
|
|
710
943
|
var clamp01 = (value) => Math.min(1, Math.max(0, value));
|
|
@@ -824,7 +1057,8 @@ function planHalftone(options, art, image, qr, area, filter) {
|
|
|
824
1057
|
const centerOffset = (dotSize - centerSize) / 2;
|
|
825
1058
|
for (let i = 0; i < count; i++) {
|
|
826
1059
|
for (let j = 0; j < count; j++) {
|
|
827
|
-
const
|
|
1060
|
+
const found = role(i, j);
|
|
1061
|
+
const moduleRole = found === "alignment" ? "function" : found;
|
|
828
1062
|
if (moduleRole === "finder" || !filter(i, j)) continue;
|
|
829
1063
|
const dark = qr.isDark(i, j);
|
|
830
1064
|
for (let a = 0; a < sub; a++) {
|
|
@@ -938,7 +1172,7 @@ var dotMask = [
|
|
|
938
1172
|
[0, 0, 0, 0, 0, 0, 0],
|
|
939
1173
|
[0, 0, 0, 0, 0, 0, 0]
|
|
940
1174
|
];
|
|
941
|
-
var QRCanvas = class {
|
|
1175
|
+
var QRCanvas = class _QRCanvas {
|
|
942
1176
|
//TODO don't pass all options to this class
|
|
943
1177
|
constructor(options) {
|
|
944
1178
|
var _a;
|
|
@@ -969,8 +1203,57 @@ var QRCanvas = class {
|
|
|
969
1203
|
canvasContext.clearRect(0, 0, this._canvas.width, this._canvas.height);
|
|
970
1204
|
}
|
|
971
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
|
+
}
|
|
972
1251
|
async drawQR(qr) {
|
|
973
1252
|
var _a;
|
|
1253
|
+
if (hasFrame(this._options)) {
|
|
1254
|
+
this._qr = qr;
|
|
1255
|
+
return this._drawFramed(qr);
|
|
1256
|
+
}
|
|
974
1257
|
const count = qr.getModuleCount();
|
|
975
1258
|
const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
|
|
976
1259
|
const dotSize = Math.floor(minSize / count);
|
|
@@ -1074,7 +1357,10 @@ var QRCanvas = class {
|
|
|
1074
1357
|
const dotSize = Math.floor(minSize / count);
|
|
1075
1358
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1076
1359
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1077
|
-
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;
|
|
1078
1364
|
canvasContext.beginPath();
|
|
1079
1365
|
for (let i = 0; i < count; i++) {
|
|
1080
1366
|
for (let j = 0; j < count; j++) {
|
|
@@ -1084,6 +1370,7 @@ var QRCanvas = class {
|
|
|
1084
1370
|
if (!this._qr.isDark(i, j)) {
|
|
1085
1371
|
continue;
|
|
1086
1372
|
}
|
|
1373
|
+
const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
1087
1374
|
dot.draw(
|
|
1088
1375
|
xBeginning + i * dotSize,
|
|
1089
1376
|
yBeginning + j * dotSize,
|
|
@@ -1140,6 +1427,7 @@ var QRCanvas = class {
|
|
|
1140
1427
|
const dotSize = Math.floor(minSize / count);
|
|
1141
1428
|
const cornersSquareSize = dotSize * 7;
|
|
1142
1429
|
const cornersDotSize = dotSize * 3;
|
|
1430
|
+
const cornerTypes = getCornerTypes(options);
|
|
1143
1431
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1144
1432
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1145
1433
|
[
|
|
@@ -1147,14 +1435,14 @@ var QRCanvas = class {
|
|
|
1147
1435
|
[1, 0, Math.PI / 2],
|
|
1148
1436
|
[0, 1, -Math.PI / 2]
|
|
1149
1437
|
].forEach(([column, row, rotation]) => {
|
|
1150
|
-
var _a, _b, _c, _d, _e, _f
|
|
1438
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1151
1439
|
if (filter && !filter(column, row)) {
|
|
1152
1440
|
return;
|
|
1153
1441
|
}
|
|
1154
1442
|
const x = xBeginning + column * dotSize * (count - 7);
|
|
1155
1443
|
const y = yBeginning + row * dotSize * (count - 7);
|
|
1156
|
-
if (
|
|
1157
|
-
const cornersSquare = new QRCornerSquare({ context: canvasContext, type:
|
|
1444
|
+
if (cornerTypes.square) {
|
|
1445
|
+
const cornersSquare = new QRCornerSquare({ context: canvasContext, type: cornerTypes.square });
|
|
1158
1446
|
canvasContext.beginPath();
|
|
1159
1447
|
cornersSquare.draw(x, y, cornersSquareSize, rotation);
|
|
1160
1448
|
} else {
|
|
@@ -1162,7 +1450,7 @@ var QRCanvas = class {
|
|
|
1162
1450
|
canvasContext.beginPath();
|
|
1163
1451
|
for (let i = 0; i < squareMask.length; i++) {
|
|
1164
1452
|
for (let j = 0; j < squareMask[i].length; j++) {
|
|
1165
|
-
if (!((
|
|
1453
|
+
if (!((_a = squareMask[i]) == null ? void 0 : _a[j])) {
|
|
1166
1454
|
continue;
|
|
1167
1455
|
}
|
|
1168
1456
|
dot.draw(
|
|
@@ -1177,7 +1465,7 @@ var QRCanvas = class {
|
|
|
1177
1465
|
}
|
|
1178
1466
|
}
|
|
1179
1467
|
}
|
|
1180
|
-
if ((
|
|
1468
|
+
if ((_b = options.cornersSquareOptions) == null ? void 0 : _b.gradient) {
|
|
1181
1469
|
const gradientOptions = options.cornersSquareOptions.gradient;
|
|
1182
1470
|
const gradient = this._createGradient({
|
|
1183
1471
|
context: canvasContext,
|
|
@@ -1191,12 +1479,12 @@ var QRCanvas = class {
|
|
|
1191
1479
|
gradient.addColorStop(offset, color);
|
|
1192
1480
|
});
|
|
1193
1481
|
canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
|
|
1194
|
-
} else if ((
|
|
1482
|
+
} else if ((_c = options.cornersSquareOptions) == null ? void 0 : _c.color) {
|
|
1195
1483
|
canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersSquareOptions.color;
|
|
1196
1484
|
}
|
|
1197
1485
|
canvasContext.fill("evenodd");
|
|
1198
|
-
if (
|
|
1199
|
-
const cornersDot = new QRCornerDot({ context: canvasContext, type:
|
|
1486
|
+
if (cornerTypes.dot) {
|
|
1487
|
+
const cornersDot = new QRCornerDot({ context: canvasContext, type: cornerTypes.dot });
|
|
1200
1488
|
canvasContext.beginPath();
|
|
1201
1489
|
cornersDot.draw(x + dotSize * 2, y + dotSize * 2, cornersDotSize, rotation);
|
|
1202
1490
|
} else {
|
|
@@ -1204,7 +1492,7 @@ var QRCanvas = class {
|
|
|
1204
1492
|
canvasContext.beginPath();
|
|
1205
1493
|
for (let i = 0; i < dotMask.length; i++) {
|
|
1206
1494
|
for (let j = 0; j < dotMask[i].length; j++) {
|
|
1207
|
-
if (!((
|
|
1495
|
+
if (!((_d = dotMask[i]) == null ? void 0 : _d[j])) {
|
|
1208
1496
|
continue;
|
|
1209
1497
|
}
|
|
1210
1498
|
dot.draw(
|
|
@@ -1219,7 +1507,7 @@ var QRCanvas = class {
|
|
|
1219
1507
|
}
|
|
1220
1508
|
}
|
|
1221
1509
|
}
|
|
1222
|
-
if ((
|
|
1510
|
+
if ((_e = options.cornersDotOptions) == null ? void 0 : _e.gradient) {
|
|
1223
1511
|
const gradientOptions = options.cornersDotOptions.gradient;
|
|
1224
1512
|
const gradient = this._createGradient({
|
|
1225
1513
|
context: canvasContext,
|
|
@@ -1233,7 +1521,7 @@ var QRCanvas = class {
|
|
|
1233
1521
|
gradient.addColorStop(offset, color);
|
|
1234
1522
|
});
|
|
1235
1523
|
canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
|
|
1236
|
-
} else if ((
|
|
1524
|
+
} else if ((_f = options.cornersDotOptions) == null ? void 0 : _f.color) {
|
|
1237
1525
|
canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersDotOptions.color;
|
|
1238
1526
|
}
|
|
1239
1527
|
canvasContext.fill("evenodd");
|
|
@@ -1289,6 +1577,13 @@ var QRCanvas = class {
|
|
|
1289
1577
|
const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
|
|
1290
1578
|
const dw = width - options.imageOptions.margin * 2;
|
|
1291
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
|
+
}
|
|
1292
1587
|
canvasContext.drawImage(this._image, dx, dy, dw < 0 ? 0 : dw, dh < 0 ? 0 : dh);
|
|
1293
1588
|
}
|
|
1294
1589
|
_createGradient({
|
|
@@ -1362,12 +1657,32 @@ var QRDot2 = class {
|
|
|
1362
1657
|
case dotTypes_default.extraRounded:
|
|
1363
1658
|
drawFunction = this._drawExtraRounded;
|
|
1364
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
|
+
}
|
|
1365
1676
|
case dotTypes_default.square:
|
|
1366
1677
|
default:
|
|
1367
1678
|
drawFunction = this._drawSquare;
|
|
1368
1679
|
}
|
|
1369
1680
|
drawFunction.call(this, { x, y, size, getNeighbor });
|
|
1370
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
|
+
}
|
|
1371
1686
|
_rotateFigure({ x, y, size, rotation = 0, draw }) {
|
|
1372
1687
|
var _a;
|
|
1373
1688
|
const cx = x + size / 2;
|
|
@@ -1603,6 +1918,13 @@ var QRCornerSquare2 = class {
|
|
|
1603
1918
|
case cornerSquareTypes_default.extraRounded:
|
|
1604
1919
|
drawFunction = this._drawExtraRounded;
|
|
1605
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;
|
|
1606
1928
|
case cornerSquareTypes_default.dot:
|
|
1607
1929
|
default:
|
|
1608
1930
|
drawFunction = this._drawDot;
|
|
@@ -1687,6 +2009,12 @@ var QRCornerDot2 = class {
|
|
|
1687
2009
|
case cornerDotTypes_default.square:
|
|
1688
2010
|
drawFunction = this._drawSquare;
|
|
1689
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;
|
|
1690
2018
|
case cornerDotTypes_default.dot:
|
|
1691
2019
|
default:
|
|
1692
2020
|
drawFunction = this._drawDot;
|
|
@@ -1882,13 +2210,16 @@ var dotMask2 = [
|
|
|
1882
2210
|
[0, 0, 0, 0, 0, 0, 0],
|
|
1883
2211
|
[0, 0, 0, 0, 0, 0, 0]
|
|
1884
2212
|
];
|
|
1885
|
-
var QRSVG = class {
|
|
2213
|
+
var QRSVG = class _QRSVG {
|
|
1886
2214
|
//TODO don't pass all options to this class
|
|
1887
|
-
constructor(options) {
|
|
2215
|
+
constructor(options, sharedWindow) {
|
|
1888
2216
|
/** True when no browser DOM or jsdom is available and the built-in virtual DOM is used. */
|
|
1889
2217
|
this._virtual = false;
|
|
1890
2218
|
var _a;
|
|
1891
|
-
if (
|
|
2219
|
+
if (sharedWindow) {
|
|
2220
|
+
this._window = sharedWindow;
|
|
2221
|
+
this._virtual = !!sharedWindow.isVirtual;
|
|
2222
|
+
} else if (options.jsdom) {
|
|
1892
2223
|
this._window = new options.jsdom("", { resources: "usable" }).window;
|
|
1893
2224
|
} else if (hasBrowserDom()) {
|
|
1894
2225
|
this._window = window;
|
|
@@ -1930,7 +2261,67 @@ var QRSVG = class {
|
|
|
1930
2261
|
this._defs = this._window.document.createElementNS("http://www.w3.org/2000/svg", "defs");
|
|
1931
2262
|
this._element.appendChild(this._defs);
|
|
1932
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
|
+
}
|
|
1933
2320
|
async drawQR(qr) {
|
|
2321
|
+
if (hasFrame(this._options)) {
|
|
2322
|
+
this._qr = qr;
|
|
2323
|
+
return this._drawFramed(qr);
|
|
2324
|
+
}
|
|
1934
2325
|
const count = qr.getModuleCount();
|
|
1935
2326
|
const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
|
|
1936
2327
|
const dotSize = Math.floor(minSize / count);
|
|
@@ -2028,11 +2419,14 @@ var QRSVG = class {
|
|
|
2028
2419
|
const dotSize = Math.floor(minSize / count);
|
|
2029
2420
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
2030
2421
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
2031
|
-
const
|
|
2422
|
+
const typeDot = new QRDot2({
|
|
2032
2423
|
svg: this._element,
|
|
2033
2424
|
type: options.dotsOptions.type,
|
|
2034
2425
|
window: this._window
|
|
2035
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;
|
|
2036
2430
|
this._dotsClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
2037
2431
|
this._dotsClipPath.setAttribute("id", "clip-path-dot-color");
|
|
2038
2432
|
this._defs.appendChild(this._dotsClipPath);
|
|
@@ -2055,6 +2449,7 @@ var QRSVG = class {
|
|
|
2055
2449
|
if (!((_b = this._qr) == null ? void 0 : _b.isDark(i, j))) {
|
|
2056
2450
|
continue;
|
|
2057
2451
|
}
|
|
2452
|
+
const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
2058
2453
|
dot.draw(
|
|
2059
2454
|
xBeginning + i * dotSize,
|
|
2060
2455
|
yBeginning + j * dotSize,
|
|
@@ -2085,6 +2480,7 @@ var QRSVG = class {
|
|
|
2085
2480
|
const dotSize = Math.floor(minSize / count);
|
|
2086
2481
|
const cornersSquareSize = dotSize * 7;
|
|
2087
2482
|
const cornersDotSize = dotSize * 3;
|
|
2483
|
+
const cornerTypes = getCornerTypes(options);
|
|
2088
2484
|
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
2089
2485
|
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
2090
2486
|
[
|
|
@@ -2092,7 +2488,7 @@ var QRSVG = class {
|
|
|
2092
2488
|
[1, 0, Math.PI / 2],
|
|
2093
2489
|
[0, 1, -Math.PI / 2]
|
|
2094
2490
|
].forEach(([column, row, rotation]) => {
|
|
2095
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j
|
|
2491
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2096
2492
|
const x = xBeginning + column * dotSize * (count - 7);
|
|
2097
2493
|
const y = yBeginning + row * dotSize * (count - 7);
|
|
2098
2494
|
let cornersSquareClipPath = this._dotsClipPath;
|
|
@@ -2113,10 +2509,10 @@ var QRSVG = class {
|
|
|
2113
2509
|
name: `corners-square-color-${column}-${row}`
|
|
2114
2510
|
});
|
|
2115
2511
|
}
|
|
2116
|
-
if (
|
|
2512
|
+
if (cornerTypes.square) {
|
|
2117
2513
|
const cornersSquare = new QRCornerSquare2({
|
|
2118
2514
|
svg: this._element,
|
|
2119
|
-
type:
|
|
2515
|
+
type: cornerTypes.square,
|
|
2120
2516
|
window: this._window
|
|
2121
2517
|
});
|
|
2122
2518
|
cornersSquare.draw(x, y, cornersSquareSize, rotation);
|
|
@@ -2131,7 +2527,7 @@ var QRSVG = class {
|
|
|
2131
2527
|
});
|
|
2132
2528
|
for (let i = 0; i < squareMask2.length; i++) {
|
|
2133
2529
|
for (let j = 0; j < squareMask2[i].length; j++) {
|
|
2134
|
-
if (!((
|
|
2530
|
+
if (!((_e = squareMask2[i]) == null ? void 0 : _e[j])) {
|
|
2135
2531
|
continue;
|
|
2136
2532
|
}
|
|
2137
2533
|
dot.draw(
|
|
@@ -2149,14 +2545,14 @@ var QRSVG = class {
|
|
|
2149
2545
|
}
|
|
2150
2546
|
}
|
|
2151
2547
|
}
|
|
2152
|
-
if (((
|
|
2548
|
+
if (((_f = options.cornersDotOptions) == null ? void 0 : _f.gradient) || ((_g = options.cornersDotOptions) == null ? void 0 : _g.color)) {
|
|
2153
2549
|
cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
2154
2550
|
cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
|
|
2155
2551
|
this._defs.appendChild(cornersDotClipPath);
|
|
2156
2552
|
this._cornersDotClipPath = cornersDotClipPath;
|
|
2157
2553
|
this._createColor({
|
|
2158
|
-
options: (
|
|
2159
|
-
color: (
|
|
2554
|
+
options: (_h = options.cornersDotOptions) == null ? void 0 : _h.gradient,
|
|
2555
|
+
color: (_i = options.cornersDotOptions) == null ? void 0 : _i.color,
|
|
2160
2556
|
additionalRotation: rotation,
|
|
2161
2557
|
x: x + dotSize * 2,
|
|
2162
2558
|
y: y + dotSize * 2,
|
|
@@ -2165,10 +2561,10 @@ var QRSVG = class {
|
|
|
2165
2561
|
name: `corners-dot-color-${column}-${row}`
|
|
2166
2562
|
});
|
|
2167
2563
|
}
|
|
2168
|
-
if (
|
|
2564
|
+
if (cornerTypes.dot) {
|
|
2169
2565
|
const cornersDot = new QRCornerDot2({
|
|
2170
2566
|
svg: this._element,
|
|
2171
|
-
type:
|
|
2567
|
+
type: cornerTypes.dot,
|
|
2172
2568
|
window: this._window
|
|
2173
2569
|
});
|
|
2174
2570
|
cornersDot.draw(x + dotSize * 2, y + dotSize * 2, cornersDotSize, rotation);
|
|
@@ -2183,7 +2579,7 @@ var QRSVG = class {
|
|
|
2183
2579
|
});
|
|
2184
2580
|
for (let i = 0; i < dotMask2.length; i++) {
|
|
2185
2581
|
for (let j = 0; j < dotMask2[i].length; j++) {
|
|
2186
|
-
if (!((
|
|
2582
|
+
if (!((_j = dotMask2[i]) == null ? void 0 : _j[j])) {
|
|
2187
2583
|
continue;
|
|
2188
2584
|
}
|
|
2189
2585
|
dot.draw(
|
|
@@ -2205,13 +2601,13 @@ var QRSVG = class {
|
|
|
2205
2601
|
}
|
|
2206
2602
|
_drawArtRects(rects) {
|
|
2207
2603
|
rects.forEach(({ x, y, width, height, color }) => {
|
|
2208
|
-
const
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
this._element.appendChild(
|
|
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);
|
|
2215
2611
|
});
|
|
2216
2612
|
}
|
|
2217
2613
|
_appendImage(href, x, y) {
|
|
@@ -2289,6 +2685,15 @@ var QRSVG = class {
|
|
|
2289
2685
|
const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
|
|
2290
2686
|
const dw = width - options.imageOptions.margin * 2;
|
|
2291
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
|
+
}
|
|
2292
2697
|
const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
|
|
2293
2698
|
image.setAttribute("href", this._imageUri || "");
|
|
2294
2699
|
image.setAttribute("x", String(dx));
|
|
@@ -2320,12 +2725,12 @@ var QRSVG = class {
|
|
|
2320
2725
|
this._element.appendChild(image);
|
|
2321
2726
|
return;
|
|
2322
2727
|
}
|
|
2323
|
-
const
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
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}')`);
|
|
2329
2734
|
if (options) {
|
|
2330
2735
|
let gradient;
|
|
2331
2736
|
if (options.type === gradientTypes_default.radial) {
|
|
@@ -2379,12 +2784,12 @@ var QRSVG = class {
|
|
|
2379
2784
|
stop.setAttribute("stop-color", color2);
|
|
2380
2785
|
gradient.appendChild(stop);
|
|
2381
2786
|
});
|
|
2382
|
-
|
|
2787
|
+
rect2.setAttribute("fill", `url('#${name}')`);
|
|
2383
2788
|
this._defs.appendChild(gradient);
|
|
2384
2789
|
} else if (color) {
|
|
2385
|
-
|
|
2790
|
+
rect2.setAttribute("fill", color);
|
|
2386
2791
|
}
|
|
2387
|
-
this._element.appendChild(
|
|
2792
|
+
this._element.appendChild(rect2);
|
|
2388
2793
|
}
|
|
2389
2794
|
};
|
|
2390
2795
|
|