qr-code-styling-node 1.11.0 → 1.13.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 +54 -1
- package/lib/core/QRCanvas.d.ts +1 -0
- package/lib/core/QRSVG.d.ts +3 -0
- package/lib/figures/shapes.d.ts +19 -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 +399 -104
- package/lib/qr-code-styling.mjs.map +4 -4
- package/lib/tools/cornerTypes.d.ts +0 -1
- package/lib/tools/pathRecorder.d.ts +15 -0
- package/lib/tools/plugins.d.ts +7 -0
- package/lib/types/index.d.ts +42 -1
- package/package.json +3 -2
package/lib/qr-code-styling.mjs
CHANGED
|
@@ -114,7 +114,10 @@ var dotTypes_default = {
|
|
|
114
114
|
smallSquare: "small-square",
|
|
115
115
|
verticalLines: "vertical-lines",
|
|
116
116
|
horizontalLines: "horizontal-lines",
|
|
117
|
-
fluid: "fluid"
|
|
117
|
+
fluid: "fluid",
|
|
118
|
+
wave: "wave",
|
|
119
|
+
blob: "blob",
|
|
120
|
+
circuit: "circuit"
|
|
118
121
|
};
|
|
119
122
|
|
|
120
123
|
// src/figures/shapes.ts
|
|
@@ -252,6 +255,102 @@ var fluid = (x, y, size, neighbors) => {
|
|
|
252
255
|
shape.push({ op: "Z" });
|
|
253
256
|
return shape;
|
|
254
257
|
};
|
|
258
|
+
var bulgeArc = (x0, y0, x1, y1, sagitta) => {
|
|
259
|
+
const chord = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
|
|
260
|
+
const r = chord * chord / (8 * sagitta) + sagitta / 2;
|
|
261
|
+
const nx = (y1 - y0) / chord;
|
|
262
|
+
const ny = -(x1 - x0) / chord;
|
|
263
|
+
const cx = (x0 + x1) / 2 - nx * (r - sagitta);
|
|
264
|
+
const cy = (y0 + y1) / 2 - ny * (r - sagitta);
|
|
265
|
+
return { op: "A", cx, cy, r, start: Math.atan2(y0 - cy, x0 - cx), end: Math.atan2(y1 - cy, x1 - cx) };
|
|
266
|
+
};
|
|
267
|
+
var wave = (x, y, size, { left, right }) => {
|
|
268
|
+
const half = size * 0.3;
|
|
269
|
+
const bulge = size * 0.1;
|
|
270
|
+
const cy = y + size / 2;
|
|
271
|
+
const x0 = left ? x : x + half;
|
|
272
|
+
const x1 = right ? x + size : x + size - half;
|
|
273
|
+
const shape = [{ op: "M", x: x0, y: cy - half }];
|
|
274
|
+
shape.push(bulgeArc(x0, cy - half, x1, cy - half, bulge));
|
|
275
|
+
if (right) shape.push({ op: "L", x: x1, y: cy + half });
|
|
276
|
+
else shape.push({ op: "A", cx: x1, cy, r: half, start: -Math.PI / 2, end: Math.PI / 2 });
|
|
277
|
+
shape.push(bulgeArc(x1, cy + half, x0, cy + half, bulge));
|
|
278
|
+
if (left) shape.push({ op: "L", x: x0, y: cy - half });
|
|
279
|
+
else shape.push({ op: "A", cx: x0, cy, r: half, start: Math.PI / 2, end: 3 * Math.PI / 2 });
|
|
280
|
+
shape.push({ op: "Z" });
|
|
281
|
+
return shape;
|
|
282
|
+
};
|
|
283
|
+
var noise = (a, b, salt) => {
|
|
284
|
+
let h = Math.imul(Math.round(a * 16) + 40503, 668265261) ^ Math.imul(Math.round(b * 16) + salt, 374761393);
|
|
285
|
+
h = Math.imul(h ^ h >>> 15, 2246822507);
|
|
286
|
+
h ^= h >>> 13;
|
|
287
|
+
return (h >>> 0) / 4294967296;
|
|
288
|
+
};
|
|
289
|
+
var blob = (x, y, size) => {
|
|
290
|
+
const inset = size * (0.03 + 0.07 * noise(x, y, 1));
|
|
291
|
+
const left = x + inset;
|
|
292
|
+
const top = y + inset;
|
|
293
|
+
const side = size - 2 * inset;
|
|
294
|
+
const radius = (k) => side * (0.25 + 0.25 * noise(x, y, k + 2));
|
|
295
|
+
const [tl, tr, br, bl] = [radius(0), radius(1), radius(2), radius(3)];
|
|
296
|
+
return [
|
|
297
|
+
{ op: "M", x: left + tl, y: top },
|
|
298
|
+
{ op: "L", x: left + side - tr, y: top },
|
|
299
|
+
{ op: "A", cx: left + side - tr, cy: top + tr, r: tr, start: -Math.PI / 2, end: 0 },
|
|
300
|
+
{ op: "L", x: left + side, y: top + side - br },
|
|
301
|
+
{ op: "A", cx: left + side - br, cy: top + side - br, r: br, start: 0, end: Math.PI / 2 },
|
|
302
|
+
{ op: "L", x: left + bl, y: top + side },
|
|
303
|
+
{ op: "A", cx: left + bl, cy: top + side - bl, r: bl, start: Math.PI / 2, end: Math.PI },
|
|
304
|
+
{ op: "L", x: left, y: top + tl },
|
|
305
|
+
{ op: "A", cx: left + tl, cy: top + tl, r: tl, start: Math.PI, end: 3 * Math.PI / 2 },
|
|
306
|
+
{ op: "Z" }
|
|
307
|
+
];
|
|
308
|
+
};
|
|
309
|
+
var circuit = (x, y, size, neighbors) => {
|
|
310
|
+
const cx = x + size / 2;
|
|
311
|
+
const cy = y + size / 2;
|
|
312
|
+
const r = size * 0.36;
|
|
313
|
+
const w = size * 0.17;
|
|
314
|
+
const alpha = Math.asin(w / r);
|
|
315
|
+
const arms = [
|
|
316
|
+
{ on: neighbors.top, angle: -Math.PI / 2 },
|
|
317
|
+
{ on: neighbors.right, angle: 0 },
|
|
318
|
+
{ on: neighbors.bottom, angle: Math.PI / 2 },
|
|
319
|
+
{ on: neighbors.left, angle: Math.PI }
|
|
320
|
+
].filter((arm) => arm.on);
|
|
321
|
+
if (!arms.length) {
|
|
322
|
+
return [
|
|
323
|
+
{ op: "M", x: cx + r, y: cy },
|
|
324
|
+
{ op: "A", cx, cy, r, start: 0, end: Math.PI },
|
|
325
|
+
{ op: "A", cx, cy, r, start: Math.PI, end: 2 * Math.PI },
|
|
326
|
+
{ op: "Z" }
|
|
327
|
+
];
|
|
328
|
+
}
|
|
329
|
+
const point = (angle) => ({
|
|
330
|
+
x: cx + r * Math.cos(angle),
|
|
331
|
+
y: cy + r * Math.sin(angle)
|
|
332
|
+
});
|
|
333
|
+
const toEdge = (angle, at) => {
|
|
334
|
+
const reach = size / 2 - r * Math.cos(alpha);
|
|
335
|
+
return { x: at.x + reach * Math.cos(angle), y: at.y + reach * Math.sin(angle) };
|
|
336
|
+
};
|
|
337
|
+
const first = point(arms[0].angle + alpha);
|
|
338
|
+
const shape = [{ op: "M", x: first.x, y: first.y }];
|
|
339
|
+
for (let k = 0; k < arms.length; k++) {
|
|
340
|
+
const from = arms[k].angle + alpha;
|
|
341
|
+
const next = arms[(k + 1) % arms.length];
|
|
342
|
+
let to = next.angle - alpha;
|
|
343
|
+
if (k === arms.length - 1 || to <= from) to += 2 * Math.PI;
|
|
344
|
+
if (to - from > 1e-6) shape.push({ op: "A", cx, cy, r, start: from, end: to });
|
|
345
|
+
const a = point(next.angle - alpha);
|
|
346
|
+
const b = point(next.angle + alpha);
|
|
347
|
+
const ea = toEdge(next.angle, a);
|
|
348
|
+
const eb = toEdge(next.angle, b);
|
|
349
|
+
shape.push({ op: "L", x: ea.x, y: ea.y }, { op: "L", x: eb.x, y: eb.y }, { op: "L", x: b.x, y: b.y });
|
|
350
|
+
}
|
|
351
|
+
shape.push({ op: "Z" });
|
|
352
|
+
return shape;
|
|
353
|
+
};
|
|
255
354
|
|
|
256
355
|
// src/figures/dot/canvas/QRDot.ts
|
|
257
356
|
var QRDot = class {
|
|
@@ -295,11 +394,17 @@ var QRDot = class {
|
|
|
295
394
|
};
|
|
296
395
|
break;
|
|
297
396
|
}
|
|
397
|
+
case dotTypes_default.blob:
|
|
398
|
+
drawFunction = (args) => this._drawShape(blob(args.x, args.y, args.size));
|
|
399
|
+
break;
|
|
400
|
+
case dotTypes_default.wave:
|
|
401
|
+
case dotTypes_default.circuit:
|
|
298
402
|
case dotTypes_default.fluid:
|
|
299
403
|
drawFunction = (args) => {
|
|
300
404
|
const has = (dx, dy) => args.getNeighbor ? args.getNeighbor(dx, dy) : false;
|
|
405
|
+
const shapeOf = type === dotTypes_default.wave ? wave : type === dotTypes_default.circuit ? circuit : fluid;
|
|
301
406
|
this._drawShape(
|
|
302
|
-
|
|
407
|
+
shapeOf(args.x, args.y, args.size, {
|
|
303
408
|
left: has(-1, 0),
|
|
304
409
|
right: has(1, 0),
|
|
305
410
|
top: has(0, -1),
|
|
@@ -746,27 +851,6 @@ function getQRArea(options, count) {
|
|
|
746
851
|
};
|
|
747
852
|
}
|
|
748
853
|
|
|
749
|
-
// src/tools/cornerTypes.ts
|
|
750
|
-
var SOLID_CORNER_DOT_TYPES = ["diamond", "small-square", "vertical-lines", "horizontal-lines"];
|
|
751
|
-
var ROUND_DOT_TYPES = ["dots", "rounded", "extra-rounded", "classy-rounded", "fluid"];
|
|
752
|
-
var needsSolidAlignment = (type) => type === "fluid" || SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
|
|
753
|
-
function getCornerTypes(options) {
|
|
754
|
-
var _a, _b, _c, _d, _e;
|
|
755
|
-
const type = options.dotsOptions.type;
|
|
756
|
-
if (((_a = options.art) == null ? void 0 : _a.mode) === "blend" || options.shape === "circle" || type === "fluid") {
|
|
757
|
-
const round2 = ROUND_DOT_TYPES.indexOf(type) !== -1;
|
|
758
|
-
return {
|
|
759
|
-
square: ((_b = options.cornersSquareOptions) == null ? void 0 : _b.type) || (round2 ? "rounded" : "square"),
|
|
760
|
-
dot: ((_c = options.cornersDotOptions) == null ? void 0 : _c.type) || (round2 ? "rounded" : "square")
|
|
761
|
-
};
|
|
762
|
-
}
|
|
763
|
-
const solid = SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
|
|
764
|
-
return {
|
|
765
|
-
square: ((_d = options.cornersSquareOptions) == null ? void 0 : _d.type) || (solid ? "square" : void 0),
|
|
766
|
-
dot: ((_e = options.cornersDotOptions) == null ? void 0 : _e.type) || (solid ? "square" : void 0)
|
|
767
|
-
};
|
|
768
|
-
}
|
|
769
|
-
|
|
770
854
|
// src/tools/frameLayout.ts
|
|
771
855
|
var hasFrame = (options) => {
|
|
772
856
|
var _a;
|
|
@@ -826,7 +910,8 @@ var innerOptions = (options, size) => ({
|
|
|
826
910
|
height: size,
|
|
827
911
|
margin: 0,
|
|
828
912
|
frameOptions: void 0,
|
|
829
|
-
labelOptions: void 0
|
|
913
|
+
labelOptions: void 0,
|
|
914
|
+
plugins: void 0
|
|
830
915
|
});
|
|
831
916
|
var getLogoPlate = (options, dx, dy, width, height) => {
|
|
832
917
|
var _a;
|
|
@@ -840,6 +925,136 @@ var getLogoPlate = (options, dx, dy, width, height) => {
|
|
|
840
925
|
};
|
|
841
926
|
};
|
|
842
927
|
|
|
928
|
+
// src/tools/sanitizeOptions.ts
|
|
929
|
+
function sanitizeGradient(gradient) {
|
|
930
|
+
const newGradient = { ...gradient };
|
|
931
|
+
if (!newGradient.colorStops || !newGradient.colorStops.length) {
|
|
932
|
+
throw "Field 'colorStops' is required in gradient";
|
|
933
|
+
}
|
|
934
|
+
if (newGradient.rotation) {
|
|
935
|
+
newGradient.rotation = Number(newGradient.rotation);
|
|
936
|
+
} else {
|
|
937
|
+
newGradient.rotation = 0;
|
|
938
|
+
}
|
|
939
|
+
newGradient.colorStops = newGradient.colorStops.map((colorStop) => ({
|
|
940
|
+
...colorStop,
|
|
941
|
+
offset: Number(colorStop.offset)
|
|
942
|
+
}));
|
|
943
|
+
return newGradient;
|
|
944
|
+
}
|
|
945
|
+
function sanitizeOptions(options) {
|
|
946
|
+
const newOptions = { ...options };
|
|
947
|
+
newOptions.width = Number(newOptions.width);
|
|
948
|
+
newOptions.height = Number(newOptions.height);
|
|
949
|
+
newOptions.margin = Number(newOptions.margin);
|
|
950
|
+
newOptions.imageOptions = {
|
|
951
|
+
...newOptions.imageOptions,
|
|
952
|
+
hideBackgroundDots: Boolean(newOptions.imageOptions.hideBackgroundDots),
|
|
953
|
+
imageSize: Number(newOptions.imageOptions.imageSize),
|
|
954
|
+
margin: Number(newOptions.imageOptions.margin)
|
|
955
|
+
};
|
|
956
|
+
if (newOptions.margin > Math.min(newOptions.width, newOptions.height)) {
|
|
957
|
+
newOptions.margin = Math.min(newOptions.width, newOptions.height);
|
|
958
|
+
}
|
|
959
|
+
newOptions.dotsOptions = {
|
|
960
|
+
...newOptions.dotsOptions
|
|
961
|
+
};
|
|
962
|
+
if (newOptions.dotsOptions.gradient) {
|
|
963
|
+
newOptions.dotsOptions.gradient = sanitizeGradient(newOptions.dotsOptions.gradient);
|
|
964
|
+
}
|
|
965
|
+
if (newOptions.cornersSquareOptions) {
|
|
966
|
+
newOptions.cornersSquareOptions = {
|
|
967
|
+
...newOptions.cornersSquareOptions
|
|
968
|
+
};
|
|
969
|
+
if (newOptions.cornersSquareOptions.gradient) {
|
|
970
|
+
newOptions.cornersSquareOptions.gradient = sanitizeGradient(newOptions.cornersSquareOptions.gradient);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
if (newOptions.cornersDotOptions) {
|
|
974
|
+
newOptions.cornersDotOptions = {
|
|
975
|
+
...newOptions.cornersDotOptions
|
|
976
|
+
};
|
|
977
|
+
if (newOptions.cornersDotOptions.gradient) {
|
|
978
|
+
newOptions.cornersDotOptions.gradient = sanitizeGradient(newOptions.cornersDotOptions.gradient);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
if (newOptions.backgroundOptions) {
|
|
982
|
+
newOptions.backgroundOptions = {
|
|
983
|
+
...newOptions.backgroundOptions
|
|
984
|
+
};
|
|
985
|
+
if (newOptions.backgroundOptions.gradient) {
|
|
986
|
+
newOptions.backgroundOptions.gradient = sanitizeGradient(newOptions.backgroundOptions.gradient);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
return newOptions;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
// src/tools/plugins.ts
|
|
993
|
+
function configurePlugins(options) {
|
|
994
|
+
let result = options;
|
|
995
|
+
(options.plugins || []).forEach((plugin) => {
|
|
996
|
+
var _a;
|
|
997
|
+
const changes = (_a = plugin.configure) == null ? void 0 : _a.call(plugin, result);
|
|
998
|
+
if (changes) result = sanitizeOptions(mergeDeep(result, changes));
|
|
999
|
+
});
|
|
1000
|
+
return result;
|
|
1001
|
+
}
|
|
1002
|
+
function getPluginArea(options, count) {
|
|
1003
|
+
let offsetX = 0;
|
|
1004
|
+
let offsetY = 0;
|
|
1005
|
+
let codeOptions = options;
|
|
1006
|
+
if (hasFrame(options)) {
|
|
1007
|
+
const layout = getFrameLayout(options);
|
|
1008
|
+
offsetX = layout.qr.x;
|
|
1009
|
+
offsetY = layout.qr.y;
|
|
1010
|
+
codeOptions = innerOptions(options, layout.qr.size);
|
|
1011
|
+
}
|
|
1012
|
+
const area = getQRArea(codeOptions, count);
|
|
1013
|
+
return {
|
|
1014
|
+
x: area.x + offsetX,
|
|
1015
|
+
y: area.y + offsetY,
|
|
1016
|
+
size: count * area.dotSize,
|
|
1017
|
+
dotSize: area.dotSize,
|
|
1018
|
+
count
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
1021
|
+
async function runAfterDraw(options, draw) {
|
|
1022
|
+
for (const plugin of options.plugins || []) {
|
|
1023
|
+
if (plugin.afterDraw) await plugin.afterDraw({ ...draw, options });
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// src/tools/cornerTypes.ts
|
|
1028
|
+
var SOLID_CORNER_DOT_TYPES = ["diamond", "small-square", "vertical-lines", "horizontal-lines"];
|
|
1029
|
+
var ROUND_DOT_TYPES = [
|
|
1030
|
+
"dots",
|
|
1031
|
+
"rounded",
|
|
1032
|
+
"extra-rounded",
|
|
1033
|
+
"classy-rounded",
|
|
1034
|
+
"fluid",
|
|
1035
|
+
"wave",
|
|
1036
|
+
"blob",
|
|
1037
|
+
"circuit"
|
|
1038
|
+
];
|
|
1039
|
+
var SOLID_ROUND_DOT_TYPES = ["fluid", "wave", "blob", "circuit"];
|
|
1040
|
+
var needsSolidAlignment = (type) => SOLID_ROUND_DOT_TYPES.indexOf(type) !== -1 || SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
|
|
1041
|
+
function getCornerTypes(options) {
|
|
1042
|
+
var _a, _b, _c, _d, _e;
|
|
1043
|
+
const type = options.dotsOptions.type;
|
|
1044
|
+
if (((_a = options.art) == null ? void 0 : _a.mode) === "blend" || options.shape === "circle" || SOLID_ROUND_DOT_TYPES.indexOf(type) !== -1) {
|
|
1045
|
+
const round2 = ROUND_DOT_TYPES.indexOf(type) !== -1;
|
|
1046
|
+
return {
|
|
1047
|
+
square: ((_b = options.cornersSquareOptions) == null ? void 0 : _b.type) || (round2 ? "rounded" : "square"),
|
|
1048
|
+
dot: ((_c = options.cornersDotOptions) == null ? void 0 : _c.type) || (round2 ? "rounded" : "square")
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
const solid = SOLID_CORNER_DOT_TYPES.indexOf(type) !== -1;
|
|
1052
|
+
return {
|
|
1053
|
+
square: ((_d = options.cornersSquareOptions) == null ? void 0 : _d.type) || (solid ? "square" : void 0),
|
|
1054
|
+
dot: ((_e = options.cornersDotOptions) == null ? void 0 : _e.type) || (solid ? "square" : void 0)
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
1057
|
+
|
|
843
1058
|
// src/art/functionModules.ts
|
|
844
1059
|
var ALIGNMENT_POSITIONS = [
|
|
845
1060
|
[],
|
|
@@ -1414,6 +1629,19 @@ var QRCanvas = class _QRCanvas {
|
|
|
1414
1629
|
}
|
|
1415
1630
|
}
|
|
1416
1631
|
async drawQR(qr) {
|
|
1632
|
+
var _a;
|
|
1633
|
+
await this._drawCode(qr);
|
|
1634
|
+
if ((_a = this._options.plugins) == null ? void 0 : _a.length) {
|
|
1635
|
+
await runAfterDraw(this._options, {
|
|
1636
|
+
type: "canvas",
|
|
1637
|
+
area: getPluginArea(this._options, qr.getModuleCount()),
|
|
1638
|
+
width: this._options.width,
|
|
1639
|
+
height: this._options.height,
|
|
1640
|
+
context: this.context
|
|
1641
|
+
});
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
async _drawCode(qr) {
|
|
1417
1645
|
var _a;
|
|
1418
1646
|
if (hasFrame(this._options)) {
|
|
1419
1647
|
this._qr = qr;
|
|
@@ -1864,11 +2092,17 @@ var QRDot2 = class {
|
|
|
1864
2092
|
};
|
|
1865
2093
|
break;
|
|
1866
2094
|
}
|
|
2095
|
+
case dotTypes_default.blob:
|
|
2096
|
+
drawFunction = (args) => this._drawShape(blob(args.x, args.y, args.size));
|
|
2097
|
+
break;
|
|
2098
|
+
case dotTypes_default.wave:
|
|
2099
|
+
case dotTypes_default.circuit:
|
|
1867
2100
|
case dotTypes_default.fluid:
|
|
1868
2101
|
drawFunction = (args) => {
|
|
1869
2102
|
const has = (dx, dy) => args.getNeighbor ? args.getNeighbor(dx, dy) : false;
|
|
2103
|
+
const shapeOf = type === dotTypes_default.wave ? wave : type === dotTypes_default.circuit ? circuit : fluid;
|
|
1870
2104
|
this._drawShape(
|
|
1871
|
-
|
|
2105
|
+
shapeOf(args.x, args.y, args.size, {
|
|
1872
2106
|
left: has(-1, 0),
|
|
1873
2107
|
right: has(1, 0),
|
|
1874
2108
|
top: has(0, -1),
|
|
@@ -2105,6 +2339,85 @@ var QRDot2 = class {
|
|
|
2105
2339
|
}
|
|
2106
2340
|
};
|
|
2107
2341
|
|
|
2342
|
+
// src/tools/pathRecorder.ts
|
|
2343
|
+
var fmt = (value) => String(Math.round(value * 1e3) / 1e3);
|
|
2344
|
+
var PathRecorder = class {
|
|
2345
|
+
constructor() {
|
|
2346
|
+
this._m = [1, 0, 0, 1, 0, 0];
|
|
2347
|
+
this._parts = [];
|
|
2348
|
+
this._open = false;
|
|
2349
|
+
}
|
|
2350
|
+
get d() {
|
|
2351
|
+
return this._parts.join("");
|
|
2352
|
+
}
|
|
2353
|
+
beginPath() {
|
|
2354
|
+
}
|
|
2355
|
+
translate(x, y) {
|
|
2356
|
+
const [a, b, c, d, e, f] = this._m;
|
|
2357
|
+
this._m = [a, b, c, d, e + a * x + c * y, f + b * x + d * y];
|
|
2358
|
+
}
|
|
2359
|
+
rotate(angle) {
|
|
2360
|
+
const [a, b, c, d, e, f] = this._m;
|
|
2361
|
+
const cos = Math.cos(angle);
|
|
2362
|
+
const sin = Math.sin(angle);
|
|
2363
|
+
this._m = [a * cos + c * sin, b * cos + d * sin, c * cos - a * sin, d * cos - b * sin, e, f];
|
|
2364
|
+
}
|
|
2365
|
+
_point(x, y) {
|
|
2366
|
+
const [a, b, c, d, e, f] = this._m;
|
|
2367
|
+
return [a * x + c * y + e, b * x + d * y + f];
|
|
2368
|
+
}
|
|
2369
|
+
moveTo(x, y) {
|
|
2370
|
+
const [px, py] = this._point(x, y);
|
|
2371
|
+
this._parts.push(`M${fmt(px)} ${fmt(py)}`);
|
|
2372
|
+
this._open = true;
|
|
2373
|
+
}
|
|
2374
|
+
lineTo(x, y) {
|
|
2375
|
+
if (!this._open) return this.moveTo(x, y);
|
|
2376
|
+
const [px, py] = this._point(x, y);
|
|
2377
|
+
this._parts.push(`L${fmt(px)} ${fmt(py)}`);
|
|
2378
|
+
}
|
|
2379
|
+
closePath() {
|
|
2380
|
+
if (!this._open) return;
|
|
2381
|
+
this._parts.push("Z");
|
|
2382
|
+
this._open = false;
|
|
2383
|
+
}
|
|
2384
|
+
rect(x, y, width, height) {
|
|
2385
|
+
this.moveTo(x, y);
|
|
2386
|
+
this.lineTo(x + width, y);
|
|
2387
|
+
this.lineTo(x + width, y + height);
|
|
2388
|
+
this.lineTo(x, y + height);
|
|
2389
|
+
this.closePath();
|
|
2390
|
+
}
|
|
2391
|
+
arc(cx, cy, r, start, end, anticlockwise = false) {
|
|
2392
|
+
let sweep = anticlockwise ? start - end : end - start;
|
|
2393
|
+
const full = sweep >= 2 * Math.PI - 1e-9;
|
|
2394
|
+
if (!full) {
|
|
2395
|
+
sweep %= 2 * Math.PI;
|
|
2396
|
+
if (sweep < 0) sweep += 2 * Math.PI;
|
|
2397
|
+
}
|
|
2398
|
+
const direction = anticlockwise ? -1 : 1;
|
|
2399
|
+
const at = (angle) => this._point(cx + r * Math.cos(angle), cy + r * Math.sin(angle));
|
|
2400
|
+
const [sx, sy] = at(start);
|
|
2401
|
+
if (this._open) this._parts.push(`L${fmt(sx)} ${fmt(sy)}`);
|
|
2402
|
+
else {
|
|
2403
|
+
this._parts.push(`M${fmt(sx)} ${fmt(sy)}`);
|
|
2404
|
+
this._open = true;
|
|
2405
|
+
}
|
|
2406
|
+
const segment = (to, length) => {
|
|
2407
|
+
const [ex, ey] = at(to);
|
|
2408
|
+
this._parts.push(
|
|
2409
|
+
`A${fmt(r)} ${fmt(r)} 0 ${length > Math.PI ? 1 : 0} ${anticlockwise ? 0 : 1} ${fmt(ex)} ${fmt(ey)}`
|
|
2410
|
+
);
|
|
2411
|
+
};
|
|
2412
|
+
if (full) {
|
|
2413
|
+
segment(start + direction * Math.PI, Math.PI);
|
|
2414
|
+
segment(start + direction * 2 * Math.PI, Math.PI);
|
|
2415
|
+
} else if (sweep > 1e-9) {
|
|
2416
|
+
segment(start + direction * sweep, sweep);
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
};
|
|
2420
|
+
|
|
2108
2421
|
// src/figures/cornerSquare/svg/QRCornerSquare.ts
|
|
2109
2422
|
var QRCornerSquare2 = class {
|
|
2110
2423
|
constructor({ svg, type, window: window2 }) {
|
|
@@ -2522,6 +2835,20 @@ var QRSVG = class _QRSVG {
|
|
|
2522
2835
|
}
|
|
2523
2836
|
}
|
|
2524
2837
|
async drawQR(qr) {
|
|
2838
|
+
var _a;
|
|
2839
|
+
await this._drawCode(qr);
|
|
2840
|
+
if ((_a = this._options.plugins) == null ? void 0 : _a.length) {
|
|
2841
|
+
await runAfterDraw(this._options, {
|
|
2842
|
+
type: "svg",
|
|
2843
|
+
area: getPluginArea(this._options, qr.getModuleCount()),
|
|
2844
|
+
width: this._options.width,
|
|
2845
|
+
height: this._options.height,
|
|
2846
|
+
svg: this._element,
|
|
2847
|
+
document: this._window.document
|
|
2848
|
+
});
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
async _drawCode(qr) {
|
|
2525
2852
|
if (hasFrame(this._options)) {
|
|
2526
2853
|
this._qr = qr;
|
|
2527
2854
|
return this._drawFramed(qr);
|
|
@@ -2630,7 +2957,7 @@ var QRSVG = class _QRSVG {
|
|
|
2630
2957
|
}
|
|
2631
2958
|
}
|
|
2632
2959
|
drawDots(filter) {
|
|
2633
|
-
var _a, _b;
|
|
2960
|
+
var _a, _b, _c;
|
|
2634
2961
|
if (!this._qr) {
|
|
2635
2962
|
throw "QR code is not defined";
|
|
2636
2963
|
}
|
|
@@ -2647,13 +2974,17 @@ var QRSVG = class _QRSVG {
|
|
|
2647
2974
|
window: this._window
|
|
2648
2975
|
});
|
|
2649
2976
|
const squareDot = new QRDot2({ svg: this._element, type: "square", window: this._window });
|
|
2977
|
+
const recorder = ((_a = options.svgOptions) == null ? void 0 : _a.mergePaths) ? new PathRecorder() : void 0;
|
|
2978
|
+
const recorded = recorder ? recorder : void 0;
|
|
2979
|
+
const typeDotMerged = recorded ? new QRDot({ context: recorded, type: options.dotsOptions.type }) : void 0;
|
|
2980
|
+
const squareDotMerged = recorded ? new QRDot({ context: recorded, type: "square" }) : void 0;
|
|
2650
2981
|
const solidAlignment = needsSolidAlignment(options.dotsOptions.type);
|
|
2651
2982
|
const role = solidAlignment ? getModuleRoles(count) : void 0;
|
|
2652
2983
|
this._dotsClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
2653
2984
|
this._dotsClipPath.setAttribute("id", "clip-path-dot-color");
|
|
2654
2985
|
this._defs.appendChild(this._dotsClipPath);
|
|
2655
2986
|
this._createColor({
|
|
2656
|
-
options: (
|
|
2987
|
+
options: (_b = options.dotsOptions) == null ? void 0 : _b.gradient,
|
|
2657
2988
|
color: options.dotsOptions.color,
|
|
2658
2989
|
additionalRotation: 0,
|
|
2659
2990
|
x: xBeginning - colorPad,
|
|
@@ -2674,10 +3005,11 @@ var QRSVG = class _QRSVG {
|
|
|
2674
3005
|
};
|
|
2675
3006
|
for (let i = from; i < to; i++) {
|
|
2676
3007
|
for (let j = from; j < to; j++) {
|
|
2677
|
-
if (inCode(i, j) ? filter && !filter(i, j) || !((
|
|
3008
|
+
if (inCode(i, j) ? filter && !filter(i, j) || !((_c = this._qr) == null ? void 0 : _c.isDark(i, j)) : !isDark(i, j)) {
|
|
2678
3009
|
continue;
|
|
2679
3010
|
}
|
|
2680
|
-
const
|
|
3011
|
+
const alignment = role && inCode(i, j) && role(i, j) === "alignment";
|
|
3012
|
+
const dot = recorded ? alignment ? squareDotMerged : typeDotMerged : alignment ? squareDot : typeDot;
|
|
2681
3013
|
dot.draw(
|
|
2682
3014
|
xBeginning + i * dotSize,
|
|
2683
3015
|
yBeginning + j * dotSize,
|
|
@@ -2691,11 +3023,14 @@ var QRSVG = class _QRSVG {
|
|
|
2691
3023
|
return isDark(i + xOffset, j + yOffset);
|
|
2692
3024
|
}
|
|
2693
3025
|
);
|
|
2694
|
-
if (dot._element && this._dotsClipPath) {
|
|
3026
|
+
if (!recorded && dot._element && this._dotsClipPath) {
|
|
2695
3027
|
this._dotsClipPath.appendChild(dot._element);
|
|
2696
3028
|
}
|
|
2697
3029
|
}
|
|
2698
3030
|
}
|
|
3031
|
+
if (recorder && recorder.d && this._dotsClipPath) {
|
|
3032
|
+
this._dotsClipPath.appendChild(this._svgElement("path", { d: recorder.d, "clip-rule": "evenodd" }));
|
|
3033
|
+
}
|
|
2699
3034
|
}
|
|
2700
3035
|
drawCorners() {
|
|
2701
3036
|
if (!this._qr) {
|
|
@@ -2716,7 +3051,7 @@ var QRSVG = class _QRSVG {
|
|
|
2716
3051
|
[1, 0, Math.PI / 2],
|
|
2717
3052
|
[0, 1, -Math.PI / 2]
|
|
2718
3053
|
].forEach(([column, row, rotation], index) => {
|
|
2719
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
3054
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
2720
3055
|
const x = xBeginning + column * dotSize * (count - 7);
|
|
2721
3056
|
const y = yBeginning + row * dotSize * (count - 7);
|
|
2722
3057
|
let cornersSquareClipPath = this._dotsClipPath;
|
|
@@ -2747,6 +3082,8 @@ var QRSVG = class _QRSVG {
|
|
|
2747
3082
|
if (cornersSquare._element && cornersSquareClipPath) {
|
|
2748
3083
|
cornersSquareClipPath.appendChild(cornersSquare._element);
|
|
2749
3084
|
}
|
|
3085
|
+
} else if ((_g = options.svgOptions) == null ? void 0 : _g.mergePaths) {
|
|
3086
|
+
this._drawMergedMask(squareMask2, x, y, dotSize, cornersSquareClipPath);
|
|
2750
3087
|
} else {
|
|
2751
3088
|
const dot = new QRDot2({
|
|
2752
3089
|
svg: this._element,
|
|
@@ -2755,7 +3092,7 @@ var QRSVG = class _QRSVG {
|
|
|
2755
3092
|
});
|
|
2756
3093
|
for (let i = 0; i < squareMask2.length; i++) {
|
|
2757
3094
|
for (let j = 0; j < squareMask2[i].length; j++) {
|
|
2758
|
-
if (!((
|
|
3095
|
+
if (!((_h = squareMask2[i]) == null ? void 0 : _h[j])) {
|
|
2759
3096
|
continue;
|
|
2760
3097
|
}
|
|
2761
3098
|
dot.draw(
|
|
@@ -2773,14 +3110,14 @@ var QRSVG = class _QRSVG {
|
|
|
2773
3110
|
}
|
|
2774
3111
|
}
|
|
2775
3112
|
}
|
|
2776
|
-
if (((
|
|
3113
|
+
if (((_i = options.cornersDotOptions) == null ? void 0 : _i.gradient) || ((_j = options.cornersDotOptions) == null ? void 0 : _j.color) || this._finderColors) {
|
|
2777
3114
|
cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
2778
3115
|
cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
|
|
2779
3116
|
this._defs.appendChild(cornersDotClipPath);
|
|
2780
3117
|
this._cornersDotClipPath = cornersDotClipPath;
|
|
2781
3118
|
this._createColor({
|
|
2782
|
-
options: (
|
|
2783
|
-
color: (
|
|
3119
|
+
options: (_k = options.cornersDotOptions) == null ? void 0 : _k.gradient,
|
|
3120
|
+
color: (_n = (_l = options.cornersDotOptions) == null ? void 0 : _l.color) != null ? _n : (_m = this._finderColors) == null ? void 0 : _m[index],
|
|
2784
3121
|
additionalRotation: rotation,
|
|
2785
3122
|
x: x + dotSize * 2,
|
|
2786
3123
|
y: y + dotSize * 2,
|
|
@@ -2799,6 +3136,8 @@ var QRSVG = class _QRSVG {
|
|
|
2799
3136
|
if (cornersDot._element && cornersDotClipPath) {
|
|
2800
3137
|
cornersDotClipPath.appendChild(cornersDot._element);
|
|
2801
3138
|
}
|
|
3139
|
+
} else if ((_o = options.svgOptions) == null ? void 0 : _o.mergePaths) {
|
|
3140
|
+
this._drawMergedMask(dotMask2, x, y, dotSize, cornersDotClipPath);
|
|
2802
3141
|
} else {
|
|
2803
3142
|
const dot = new QRDot2({
|
|
2804
3143
|
svg: this._element,
|
|
@@ -2807,7 +3146,7 @@ var QRSVG = class _QRSVG {
|
|
|
2807
3146
|
});
|
|
2808
3147
|
for (let i = 0; i < dotMask2.length; i++) {
|
|
2809
3148
|
for (let j = 0; j < dotMask2[i].length; j++) {
|
|
2810
|
-
if (!((
|
|
3149
|
+
if (!((_p = dotMask2[i]) == null ? void 0 : _p[j])) {
|
|
2811
3150
|
continue;
|
|
2812
3151
|
}
|
|
2813
3152
|
dot.draw(
|
|
@@ -2827,6 +3166,26 @@ var QRSVG = class _QRSVG {
|
|
|
2827
3166
|
}
|
|
2828
3167
|
});
|
|
2829
3168
|
}
|
|
3169
|
+
/** Finder modules drawn with the dots' own figure, as one path (svgOptions.mergePaths). */
|
|
3170
|
+
_drawMergedMask(mask, x, y, dotSize, clipPath) {
|
|
3171
|
+
var _a;
|
|
3172
|
+
const recorder = new PathRecorder();
|
|
3173
|
+
const dot = new QRDot({
|
|
3174
|
+
context: recorder,
|
|
3175
|
+
type: this._options.dotsOptions.type
|
|
3176
|
+
});
|
|
3177
|
+
for (let i = 0; i < mask.length; i++) {
|
|
3178
|
+
for (let j = 0; j < mask[i].length; j++) {
|
|
3179
|
+
if (!((_a = mask[i]) == null ? void 0 : _a[j])) continue;
|
|
3180
|
+
dot.draw(x + i * dotSize, y + j * dotSize, dotSize, (xOffset, yOffset) => {
|
|
3181
|
+
var _a2;
|
|
3182
|
+
return !!((_a2 = mask[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
|
|
3183
|
+
});
|
|
3184
|
+
}
|
|
3185
|
+
}
|
|
3186
|
+
if (recorder.d && clipPath)
|
|
3187
|
+
clipPath.appendChild(this._svgElement("path", { d: recorder.d, "clip-rule": "evenodd" }));
|
|
3188
|
+
}
|
|
2830
3189
|
_drawArtRects(rects) {
|
|
2831
3190
|
rects.forEach(({ x, y, width, height, color, shape }) => {
|
|
2832
3191
|
this._element.appendChild(
|
|
@@ -3071,70 +3430,6 @@ var defaultOptions = {
|
|
|
3071
3430
|
};
|
|
3072
3431
|
var QROptions_default = defaultOptions;
|
|
3073
3432
|
|
|
3074
|
-
// src/tools/sanitizeOptions.ts
|
|
3075
|
-
function sanitizeGradient(gradient) {
|
|
3076
|
-
const newGradient = { ...gradient };
|
|
3077
|
-
if (!newGradient.colorStops || !newGradient.colorStops.length) {
|
|
3078
|
-
throw "Field 'colorStops' is required in gradient";
|
|
3079
|
-
}
|
|
3080
|
-
if (newGradient.rotation) {
|
|
3081
|
-
newGradient.rotation = Number(newGradient.rotation);
|
|
3082
|
-
} else {
|
|
3083
|
-
newGradient.rotation = 0;
|
|
3084
|
-
}
|
|
3085
|
-
newGradient.colorStops = newGradient.colorStops.map((colorStop) => ({
|
|
3086
|
-
...colorStop,
|
|
3087
|
-
offset: Number(colorStop.offset)
|
|
3088
|
-
}));
|
|
3089
|
-
return newGradient;
|
|
3090
|
-
}
|
|
3091
|
-
function sanitizeOptions(options) {
|
|
3092
|
-
const newOptions = { ...options };
|
|
3093
|
-
newOptions.width = Number(newOptions.width);
|
|
3094
|
-
newOptions.height = Number(newOptions.height);
|
|
3095
|
-
newOptions.margin = Number(newOptions.margin);
|
|
3096
|
-
newOptions.imageOptions = {
|
|
3097
|
-
...newOptions.imageOptions,
|
|
3098
|
-
hideBackgroundDots: Boolean(newOptions.imageOptions.hideBackgroundDots),
|
|
3099
|
-
imageSize: Number(newOptions.imageOptions.imageSize),
|
|
3100
|
-
margin: Number(newOptions.imageOptions.margin)
|
|
3101
|
-
};
|
|
3102
|
-
if (newOptions.margin > Math.min(newOptions.width, newOptions.height)) {
|
|
3103
|
-
newOptions.margin = Math.min(newOptions.width, newOptions.height);
|
|
3104
|
-
}
|
|
3105
|
-
newOptions.dotsOptions = {
|
|
3106
|
-
...newOptions.dotsOptions
|
|
3107
|
-
};
|
|
3108
|
-
if (newOptions.dotsOptions.gradient) {
|
|
3109
|
-
newOptions.dotsOptions.gradient = sanitizeGradient(newOptions.dotsOptions.gradient);
|
|
3110
|
-
}
|
|
3111
|
-
if (newOptions.cornersSquareOptions) {
|
|
3112
|
-
newOptions.cornersSquareOptions = {
|
|
3113
|
-
...newOptions.cornersSquareOptions
|
|
3114
|
-
};
|
|
3115
|
-
if (newOptions.cornersSquareOptions.gradient) {
|
|
3116
|
-
newOptions.cornersSquareOptions.gradient = sanitizeGradient(newOptions.cornersSquareOptions.gradient);
|
|
3117
|
-
}
|
|
3118
|
-
}
|
|
3119
|
-
if (newOptions.cornersDotOptions) {
|
|
3120
|
-
newOptions.cornersDotOptions = {
|
|
3121
|
-
...newOptions.cornersDotOptions
|
|
3122
|
-
};
|
|
3123
|
-
if (newOptions.cornersDotOptions.gradient) {
|
|
3124
|
-
newOptions.cornersDotOptions.gradient = sanitizeGradient(newOptions.cornersDotOptions.gradient);
|
|
3125
|
-
}
|
|
3126
|
-
}
|
|
3127
|
-
if (newOptions.backgroundOptions) {
|
|
3128
|
-
newOptions.backgroundOptions = {
|
|
3129
|
-
...newOptions.backgroundOptions
|
|
3130
|
-
};
|
|
3131
|
-
if (newOptions.backgroundOptions.gradient) {
|
|
3132
|
-
newOptions.backgroundOptions.gradient = sanitizeGradient(newOptions.backgroundOptions.gradient);
|
|
3133
|
-
}
|
|
3134
|
-
}
|
|
3135
|
-
return newOptions;
|
|
3136
|
-
}
|
|
3137
|
-
|
|
3138
3433
|
// src/core/QRCodeStyling.ts
|
|
3139
3434
|
import qrcode from "qrcode-generator";
|
|
3140
3435
|
|
|
@@ -3225,7 +3520,7 @@ var payloads_default = { wifi, vCard, event, email, phone, sms, geo };
|
|
|
3225
3520
|
// src/core/QRCodeStyling.ts
|
|
3226
3521
|
var _QRCodeStyling = class _QRCodeStyling {
|
|
3227
3522
|
constructor(options) {
|
|
3228
|
-
this._options = options ? sanitizeOptions(mergeDeep(QROptions_default, options)) : QROptions_default;
|
|
3523
|
+
this._options = options ? configurePlugins(sanitizeOptions(mergeDeep(QROptions_default, options))) : QROptions_default;
|
|
3229
3524
|
this.update();
|
|
3230
3525
|
}
|
|
3231
3526
|
/** Canvas output needs a browser canvas or node-canvas. */
|
|
@@ -3271,7 +3566,7 @@ var _QRCodeStyling = class _QRCodeStyling {
|
|
|
3271
3566
|
}
|
|
3272
3567
|
update(options) {
|
|
3273
3568
|
_QRCodeStyling._clearContainer(this._container);
|
|
3274
|
-
this._options = options ? sanitizeOptions(mergeDeep(this._options, options)) : this._options;
|
|
3569
|
+
this._options = options ? configurePlugins(sanitizeOptions(mergeDeep(this._options, options))) : this._options;
|
|
3275
3570
|
if (!this._options.data) {
|
|
3276
3571
|
return;
|
|
3277
3572
|
}
|
|
@@ -3509,9 +3804,9 @@ ${source}`;
|
|
|
3509
3804
|
const name = shareOptions.name || "qr";
|
|
3510
3805
|
const nav = typeof navigator !== "undefined" ? navigator : void 0;
|
|
3511
3806
|
if ((nav == null ? void 0 : nav.share) && typeof File !== "undefined") {
|
|
3512
|
-
const
|
|
3807
|
+
const blob2 = await this.getRawData(extension);
|
|
3513
3808
|
const type = extension === "svg" ? "image/svg+xml" : `image/${extension}`;
|
|
3514
|
-
const file = new File([
|
|
3809
|
+
const file = new File([blob2], `${name}.${extension}`, { type });
|
|
3515
3810
|
const data = { files: [file], title: shareOptions.title, text: shareOptions.text };
|
|
3516
3811
|
if (!nav.canShare || nav.canShare(data)) {
|
|
3517
3812
|
try {
|