qr-code-styling-node 1.9.0 → 1.11.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 +76 -4
- package/lib/art/art.d.ts +12 -8
- package/lib/core/QRCanvas.d.ts +2 -0
- package/lib/core/QRCodeStyling.d.ts +23 -1
- package/lib/core/QRSVG.d.ts +5 -1
- package/lib/figures/shapes.d.ts +11 -0
- package/lib/index.d.ts +3 -1
- package/lib/qr-code-styling.common.js +1 -1
- package/lib/qr-code-styling.common.js.map +1 -1
- package/lib/qr-code-styling.js +1 -1
- package/lib/qr-code-styling.js.map +1 -1
- package/lib/qr-code-styling.mjs +454 -97
- package/lib/qr-code-styling.mjs.map +4 -4
- package/lib/tools/circle.d.ts +16 -0
- package/lib/tools/cornerTypes.d.ts +2 -0
- package/lib/tools/payloads.d.ts +72 -0
- package/lib/tools/qrArea.d.ts +14 -0
- package/lib/tools/utf8.d.ts +4 -0
- package/lib/types/index.d.ts +25 -3
- package/package.json +4 -2
package/lib/qr-code-styling.mjs
CHANGED
|
@@ -113,7 +113,8 @@ var dotTypes_default = {
|
|
|
113
113
|
diamond: "diamond",
|
|
114
114
|
smallSquare: "small-square",
|
|
115
115
|
verticalLines: "vertical-lines",
|
|
116
|
-
horizontalLines: "horizontal-lines"
|
|
116
|
+
horizontalLines: "horizontal-lines",
|
|
117
|
+
fluid: "fluid"
|
|
117
118
|
};
|
|
118
119
|
|
|
119
120
|
// src/figures/shapes.ts
|
|
@@ -222,6 +223,35 @@ var roundedRing = (x, y, size) => {
|
|
|
222
223
|
);
|
|
223
224
|
};
|
|
224
225
|
var roundedSquare = (x, y, size) => roundedRect(x, y, size, size, size * 0.25);
|
|
226
|
+
var fluid = (x, y, size, neighbors) => {
|
|
227
|
+
const { left, right, top, bottom } = neighbors;
|
|
228
|
+
const inset = size * 0.1;
|
|
229
|
+
const x0 = x + inset;
|
|
230
|
+
const x1 = x + size - inset;
|
|
231
|
+
const y0 = y + inset;
|
|
232
|
+
const y1 = y + size - inset;
|
|
233
|
+
const r = (size - 2 * inset) / 2;
|
|
234
|
+
const cx = x + size / 2;
|
|
235
|
+
const cy = y + size / 2;
|
|
236
|
+
const roundTL = !left && !top;
|
|
237
|
+
const roundTR = !top && !right;
|
|
238
|
+
const roundBR = !right && !bottom;
|
|
239
|
+
const roundBL = !bottom && !left;
|
|
240
|
+
const shape = [{ op: "M", x: roundTL ? cx : x0, y: y0 }];
|
|
241
|
+
if (top) shape.push({ op: "L", x: x0, y }, { op: "L", x: x1, y }, { op: "L", x: x1, y: y0 });
|
|
242
|
+
if (roundTR) shape.push({ op: "A", cx, cy, r, start: -Math.PI / 2, end: 0 });
|
|
243
|
+
else shape.push({ op: "L", x: x1, y: y0 });
|
|
244
|
+
if (right) shape.push({ op: "L", x: x + size, y: y0 }, { op: "L", x: x + size, y: y1 }, { op: "L", x: x1, y: y1 });
|
|
245
|
+
if (roundBR) shape.push({ op: "A", cx, cy, r, start: 0, end: Math.PI / 2 });
|
|
246
|
+
else shape.push({ op: "L", x: x1, y: y1 });
|
|
247
|
+
if (bottom) shape.push({ op: "L", x: x1, y: y + size }, { op: "L", x: x0, y: y + size }, { op: "L", x: x0, y: y1 });
|
|
248
|
+
if (roundBL) shape.push({ op: "A", cx, cy, r, start: Math.PI / 2, end: Math.PI });
|
|
249
|
+
else shape.push({ op: "L", x: x0, y: y1 });
|
|
250
|
+
if (left) shape.push({ op: "L", x, y: y1 }, { op: "L", x, y: y0 }, { op: "L", x: x0, y: y0 });
|
|
251
|
+
if (roundTL) shape.push({ op: "A", cx, cy, r, start: Math.PI, end: 3 * Math.PI / 2 });
|
|
252
|
+
shape.push({ op: "Z" });
|
|
253
|
+
return shape;
|
|
254
|
+
};
|
|
225
255
|
|
|
226
256
|
// src/figures/dot/canvas/QRDot.ts
|
|
227
257
|
var QRDot = class {
|
|
@@ -265,6 +295,19 @@ var QRDot = class {
|
|
|
265
295
|
};
|
|
266
296
|
break;
|
|
267
297
|
}
|
|
298
|
+
case dotTypes_default.fluid:
|
|
299
|
+
drawFunction = (args) => {
|
|
300
|
+
const has = (dx, dy) => args.getNeighbor ? args.getNeighbor(dx, dy) : false;
|
|
301
|
+
this._drawShape(
|
|
302
|
+
fluid(args.x, args.y, args.size, {
|
|
303
|
+
left: has(-1, 0),
|
|
304
|
+
right: has(1, 0),
|
|
305
|
+
top: has(0, -1),
|
|
306
|
+
bottom: has(0, 1)
|
|
307
|
+
})
|
|
308
|
+
);
|
|
309
|
+
};
|
|
310
|
+
break;
|
|
268
311
|
case dotTypes_default.square:
|
|
269
312
|
default:
|
|
270
313
|
drawFunction = this._drawSquare;
|
|
@@ -659,15 +702,68 @@ var QRCornerDot = class {
|
|
|
659
702
|
}
|
|
660
703
|
};
|
|
661
704
|
|
|
705
|
+
// src/tools/circle.ts
|
|
706
|
+
var isCircle = (options) => options.shape === "circle";
|
|
707
|
+
function getCircleLayout(count) {
|
|
708
|
+
let size = Math.ceil((count + 2) * Math.SQRT2) + 1;
|
|
709
|
+
if ((size - count) % 2) size++;
|
|
710
|
+
const pad2 = (size - count) / 2;
|
|
711
|
+
const radius = size / 2;
|
|
712
|
+
const center = count / 2;
|
|
713
|
+
const isDecoration = (i, j) => {
|
|
714
|
+
if (i >= -1 && i <= count && j >= -1 && j <= count) return false;
|
|
715
|
+
const dx = i + 0.5 - center;
|
|
716
|
+
const dy = j + 0.5 - center;
|
|
717
|
+
if (dx * dx + dy * dy > (radius - 0.5) * (radius - 0.5)) return false;
|
|
718
|
+
let h = Math.imul(i + 1013, 668265261) ^ Math.imul(j + 7919, 374761393) ^ Math.imul(count, 2654435769);
|
|
719
|
+
h = Math.imul(h ^ h >>> 15, 2246822507);
|
|
720
|
+
h ^= h >>> 13;
|
|
721
|
+
return (h >>> 0) % 100 < 45;
|
|
722
|
+
};
|
|
723
|
+
return { size, pad: pad2, isDecoration };
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// src/tools/qrArea.ts
|
|
727
|
+
function getQRArea(options, count) {
|
|
728
|
+
var _a;
|
|
729
|
+
const placement = (_a = options.art) == null ? void 0 : _a.placement;
|
|
730
|
+
if (placement && placement.size > 0) {
|
|
731
|
+
const dotSize2 = Math.floor(placement.size / (options.shape === "circle" ? getCircleLayout(count).size : count));
|
|
732
|
+
return {
|
|
733
|
+
count,
|
|
734
|
+
dotSize: dotSize2,
|
|
735
|
+
x: Math.floor(placement.x + (placement.size - count * dotSize2) / 2),
|
|
736
|
+
y: Math.floor(placement.y + (placement.size - count * dotSize2) / 2)
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const minSize = Math.min(options.width, options.height) - options.margin * 2;
|
|
740
|
+
const dotSize = Math.floor(minSize / (options.shape === "circle" ? getCircleLayout(count).size : count));
|
|
741
|
+
return {
|
|
742
|
+
count,
|
|
743
|
+
dotSize,
|
|
744
|
+
x: Math.floor((options.width - count * dotSize) / 2),
|
|
745
|
+
y: Math.floor((options.height - count * dotSize) / 2)
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
|
|
662
749
|
// src/tools/cornerTypes.ts
|
|
663
750
|
var SOLID_CORNER_DOT_TYPES = ["diamond", "small-square", "vertical-lines", "horizontal-lines"];
|
|
664
|
-
var
|
|
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;
|
|
665
753
|
function getCornerTypes(options) {
|
|
666
|
-
var _a, _b;
|
|
667
|
-
const
|
|
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;
|
|
668
764
|
return {
|
|
669
|
-
square: ((
|
|
670
|
-
dot: ((
|
|
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)
|
|
671
767
|
};
|
|
672
768
|
}
|
|
673
769
|
|
|
@@ -939,10 +1035,10 @@ var createVirtualWindow = () => ({
|
|
|
939
1035
|
var hasBrowserDom = () => typeof window !== "undefined" && typeof document !== "undefined" && typeof document.createElementNS === "function";
|
|
940
1036
|
|
|
941
1037
|
// src/art/art.ts
|
|
942
|
-
var DEFAULT_CONTRAST = { halftone: 0.5, colorize: 0.7 };
|
|
1038
|
+
var DEFAULT_CONTRAST = { halftone: 0.5, colorize: 0.7, blend: 0.6 };
|
|
943
1039
|
var clamp01 = (value) => Math.min(1, Math.max(0, value));
|
|
944
1040
|
var getContrast = (art) => clamp01(
|
|
945
|
-
typeof art.contrast === "number" && !isNaN(art.contrast) ? art.contrast : DEFAULT_CONTRAST[art.mode === "colorize"
|
|
1041
|
+
typeof art.contrast === "number" && !isNaN(art.contrast) ? art.contrast : DEFAULT_CONTRAST[art.mode === "colorize" || art.mode === "blend" ? art.mode : "halftone"]
|
|
946
1042
|
);
|
|
947
1043
|
var getLimits = (contrast) => ({
|
|
948
1044
|
dark: 0.45 - 0.35 * contrast,
|
|
@@ -1128,15 +1224,82 @@ function prepareColorize(options, art, image, area) {
|
|
|
1128
1224
|
background: art.background === false ? void 0 : makeLayer((color) => toLight(color, limits.light))
|
|
1129
1225
|
};
|
|
1130
1226
|
}
|
|
1131
|
-
function
|
|
1132
|
-
const
|
|
1133
|
-
const
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1227
|
+
function prepareBlend(options, art, image, qr, area, filter) {
|
|
1228
|
+
const { count, dotSize } = area;
|
|
1229
|
+
const { canvas: picture, data } = readPixels(options, image, options.width, options.height);
|
|
1230
|
+
const width = options.width;
|
|
1231
|
+
const contrast = getContrast(art);
|
|
1232
|
+
const limits = getLimits(contrast);
|
|
1233
|
+
const strong = getLimits(Math.max(contrast, 0.8));
|
|
1234
|
+
const role = getModuleRoles(count);
|
|
1235
|
+
const shape = ROUND_DOT_TYPES.indexOf(options.dotsOptions.type) !== -1 ? "circle" : "rect";
|
|
1236
|
+
const average = (x0, y0, size) => {
|
|
1237
|
+
let r = 0;
|
|
1238
|
+
let g = 0;
|
|
1239
|
+
let b = 0;
|
|
1240
|
+
let n = 0;
|
|
1241
|
+
const xStart = Math.max(0, Math.floor(x0));
|
|
1242
|
+
const yStart = Math.max(0, Math.floor(y0));
|
|
1243
|
+
const xEnd = Math.min(options.width, Math.ceil(x0 + size));
|
|
1244
|
+
const yEnd = Math.min(options.height, Math.ceil(y0 + size));
|
|
1245
|
+
for (let y = yStart; y < yEnd; y++) {
|
|
1246
|
+
for (let x = xStart; x < xEnd; x++) {
|
|
1247
|
+
const index = (y * width + x) * 4;
|
|
1248
|
+
r += data[index];
|
|
1249
|
+
g += data[index + 1];
|
|
1250
|
+
b += data[index + 2];
|
|
1251
|
+
n++;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
return n ? [r / n, g / n, b / n] : [255, 255, 255];
|
|
1139
1255
|
};
|
|
1256
|
+
const rects = [];
|
|
1257
|
+
const finderColors = [];
|
|
1258
|
+
[
|
|
1259
|
+
[0, 0],
|
|
1260
|
+
[count - 8, 0],
|
|
1261
|
+
[0, count - 8]
|
|
1262
|
+
].forEach(([column, row]) => {
|
|
1263
|
+
const x = area.x + column * dotSize;
|
|
1264
|
+
const y = area.y + row * dotSize;
|
|
1265
|
+
const color = average(x, y, 8 * dotSize);
|
|
1266
|
+
const x0 = Math.max(0, x - dotSize);
|
|
1267
|
+
const y0 = Math.max(0, y - dotSize);
|
|
1268
|
+
const x1 = Math.min(options.width, x + 8 * dotSize + (column ? dotSize : 0));
|
|
1269
|
+
const y1 = Math.min(options.height, y + 8 * dotSize + (row ? dotSize : 0));
|
|
1270
|
+
rects.push({ x: x0, y: y0, width: x1 - x0, height: y1 - y0, color: css(toLight(color, strong.light)) });
|
|
1271
|
+
finderColors.push(css(toDark(color, strong.dark)));
|
|
1272
|
+
});
|
|
1273
|
+
const dot = Math.max(
|
|
1274
|
+
1,
|
|
1275
|
+
Math.min(dotSize, Math.round(dotSize * ((shape === "circle" ? 0.55 : 0.45) + contrast * 0.3)))
|
|
1276
|
+
);
|
|
1277
|
+
const offset = (dotSize - dot) / 2;
|
|
1278
|
+
for (let i = 0; i < count; i++) {
|
|
1279
|
+
for (let j = 0; j < count; j++) {
|
|
1280
|
+
const moduleRole = role(i, j);
|
|
1281
|
+
if (moduleRole === "finder" || !filter(i, j)) continue;
|
|
1282
|
+
const x = area.x + i * dotSize;
|
|
1283
|
+
const y = area.y + j * dotSize;
|
|
1284
|
+
const color = average(x + dotSize / 3, y + dotSize / 3, dotSize / 3);
|
|
1285
|
+
const lum = luminance(color);
|
|
1286
|
+
const dark = qr.isDark(i, j);
|
|
1287
|
+
const agrees = dark ? lum <= limits.dark : lum >= limits.light;
|
|
1288
|
+
if (agrees && moduleRole === "data" && !dark) continue;
|
|
1289
|
+
const full = moduleRole === "alignment";
|
|
1290
|
+
rects.push({
|
|
1291
|
+
x: full ? x : x + offset,
|
|
1292
|
+
y: full ? y : y + offset,
|
|
1293
|
+
width: full ? dotSize : dot,
|
|
1294
|
+
height: full ? dotSize : dot,
|
|
1295
|
+
shape: full ? "rect" : shape,
|
|
1296
|
+
color: css(
|
|
1297
|
+
dark ? toDark(color, agrees ? Math.min(lum, strong.dark) * 0.4 : strong.dark) : toLight(color, strong.light)
|
|
1298
|
+
)
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
return { mode: "blend", picture, rects, finderColors };
|
|
1140
1303
|
}
|
|
1141
1304
|
async function prepareArt(options, qr, filter) {
|
|
1142
1305
|
const art = options.art;
|
|
@@ -1144,7 +1307,9 @@ async function prepareArt(options, qr, filter) {
|
|
|
1144
1307
|
createCanvas(options, 1, 1);
|
|
1145
1308
|
const image = await loadPicture(options, art);
|
|
1146
1309
|
const area = getQRArea(options, qr.getModuleCount());
|
|
1147
|
-
|
|
1310
|
+
if (art.mode === "colorize") return prepareColorize(options, art, image, area);
|
|
1311
|
+
if (art.mode === "blend") return prepareBlend(options, art, image, qr, area, filter);
|
|
1312
|
+
return planHalftone(options, art, image, qr, area, filter);
|
|
1148
1313
|
}
|
|
1149
1314
|
|
|
1150
1315
|
// src/constants/gradientTypes.ts
|
|
@@ -1255,8 +1420,7 @@ var QRCanvas = class _QRCanvas {
|
|
|
1255
1420
|
return this._drawFramed(qr);
|
|
1256
1421
|
}
|
|
1257
1422
|
const count = qr.getModuleCount();
|
|
1258
|
-
const
|
|
1259
|
-
const dotSize = Math.floor(minSize / count);
|
|
1423
|
+
const { dotSize } = getQRArea(this._options, count);
|
|
1260
1424
|
let drawImageSize = {
|
|
1261
1425
|
hideXDots: 0,
|
|
1262
1426
|
hideYDots: 0,
|
|
@@ -1299,7 +1463,12 @@ var QRCanvas = class _QRCanvas {
|
|
|
1299
1463
|
}
|
|
1300
1464
|
this.clear();
|
|
1301
1465
|
this.drawBackground();
|
|
1302
|
-
if ((art == null ? void 0 : art.mode) === "
|
|
1466
|
+
if ((art == null ? void 0 : art.mode) === "blend") {
|
|
1467
|
+
this.context.drawImage(art.picture, 0, 0);
|
|
1468
|
+
this._drawArtRects(art.rects);
|
|
1469
|
+
this.drawDots(() => false);
|
|
1470
|
+
this._finderColors = art.finderColors;
|
|
1471
|
+
} else if ((art == null ? void 0 : art.mode) === "halftone") {
|
|
1303
1472
|
this._drawArtRects(art.rects);
|
|
1304
1473
|
this.drawDots(() => false);
|
|
1305
1474
|
} else {
|
|
@@ -1312,6 +1481,7 @@ var QRCanvas = class _QRCanvas {
|
|
|
1312
1481
|
this._dotsFill = void 0;
|
|
1313
1482
|
}
|
|
1314
1483
|
this.drawCorners();
|
|
1484
|
+
this._finderColors = void 0;
|
|
1315
1485
|
if (this._options.image) {
|
|
1316
1486
|
this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
|
|
1317
1487
|
}
|
|
@@ -1337,10 +1507,18 @@ var QRCanvas = class _QRCanvas {
|
|
|
1337
1507
|
} else if (options.backgroundOptions.color) {
|
|
1338
1508
|
canvasContext.fillStyle = options.backgroundOptions.color;
|
|
1339
1509
|
}
|
|
1510
|
+
if (isCircle(options)) {
|
|
1511
|
+
const size = Math.min(this._canvas.width, this._canvas.height);
|
|
1512
|
+
canvasContext.beginPath();
|
|
1513
|
+
canvasContext.arc(this._canvas.width / 2, this._canvas.height / 2, size / 2, 0, Math.PI * 2);
|
|
1514
|
+
canvasContext.fill();
|
|
1515
|
+
return;
|
|
1516
|
+
}
|
|
1340
1517
|
canvasContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
|
|
1341
1518
|
}
|
|
1342
1519
|
}
|
|
1343
1520
|
drawDots(filter) {
|
|
1521
|
+
var _a;
|
|
1344
1522
|
if (!this._qr) {
|
|
1345
1523
|
throw "QR code is not defined";
|
|
1346
1524
|
}
|
|
@@ -1353,32 +1531,39 @@ var QRCanvas = class _QRCanvas {
|
|
|
1353
1531
|
if (count > options.width || count > options.height) {
|
|
1354
1532
|
throw "The canvas is too small.";
|
|
1355
1533
|
}
|
|
1356
|
-
const
|
|
1357
|
-
const
|
|
1358
|
-
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1359
|
-
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1534
|
+
const { dotSize, x: xBeginning, y: yBeginning } = getQRArea(options, count);
|
|
1535
|
+
const colorPad = isCircle(options) ? getCircleLayout(count).pad * dotSize : 0;
|
|
1360
1536
|
const typeDot = new QRDot({ context: canvasContext, type: options.dotsOptions.type });
|
|
1361
1537
|
const squareDot = new QRDot({ context: canvasContext, type: "square" });
|
|
1362
1538
|
const solidAlignment = needsSolidAlignment(options.dotsOptions.type);
|
|
1363
1539
|
const role = solidAlignment ? getModuleRoles(count) : void 0;
|
|
1364
1540
|
canvasContext.beginPath();
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1541
|
+
const circle = isCircle(options) ? getCircleLayout(count) : void 0;
|
|
1542
|
+
const from = circle ? -circle.pad : 0;
|
|
1543
|
+
const to = circle ? count + circle.pad : count;
|
|
1544
|
+
const inCode = (i, j) => i >= 0 && j >= 0 && i < count && j < count;
|
|
1545
|
+
const isDark = (i, j) => {
|
|
1546
|
+
if (filter && !filter(i, j)) return false;
|
|
1547
|
+
if (inCode(i, j)) return !!this._qr && this._qr.isDark(i, j);
|
|
1548
|
+
return !!circle && circle.isDecoration(i, j);
|
|
1549
|
+
};
|
|
1550
|
+
for (let i = from; i < to; i++) {
|
|
1551
|
+
for (let j = from; j < to; j++) {
|
|
1552
|
+
if (inCode(i, j) ? filter && !filter(i, j) || !((_a = this._qr) == null ? void 0 : _a.isDark(i, j)) : !isDark(i, j)) {
|
|
1371
1553
|
continue;
|
|
1372
1554
|
}
|
|
1373
|
-
const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
1555
|
+
const dot = role && inCode(i, j) && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
1374
1556
|
dot.draw(
|
|
1375
1557
|
xBeginning + i * dotSize,
|
|
1376
1558
|
yBeginning + j * dotSize,
|
|
1377
1559
|
dotSize,
|
|
1378
1560
|
(xOffset, yOffset) => {
|
|
1379
|
-
if (
|
|
1380
|
-
|
|
1381
|
-
|
|
1561
|
+
if (!circle) {
|
|
1562
|
+
if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
|
|
1563
|
+
if (filter && !filter(i + xOffset, j + yOffset)) return false;
|
|
1564
|
+
return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
|
|
1565
|
+
}
|
|
1566
|
+
return isDark(i + xOffset, j + yOffset);
|
|
1382
1567
|
}
|
|
1383
1568
|
);
|
|
1384
1569
|
}
|
|
@@ -1389,9 +1574,9 @@ var QRCanvas = class _QRCanvas {
|
|
|
1389
1574
|
context: canvasContext,
|
|
1390
1575
|
options: gradientOptions,
|
|
1391
1576
|
additionalRotation: 0,
|
|
1392
|
-
x: xBeginning,
|
|
1393
|
-
y: yBeginning,
|
|
1394
|
-
size: count * dotSize
|
|
1577
|
+
x: xBeginning - colorPad,
|
|
1578
|
+
y: yBeginning - colorPad,
|
|
1579
|
+
size: count * dotSize + 2 * colorPad
|
|
1395
1580
|
});
|
|
1396
1581
|
gradientOptions.colorStops.forEach(({ offset, color }) => {
|
|
1397
1582
|
gradient.addColorStop(offset, color);
|
|
@@ -1408,9 +1593,15 @@ var QRCanvas = class _QRCanvas {
|
|
|
1408
1593
|
_drawArtRects(rects) {
|
|
1409
1594
|
const context = this.context;
|
|
1410
1595
|
if (!context) return;
|
|
1411
|
-
rects.forEach(({ x, y, width, height, color }) => {
|
|
1596
|
+
rects.forEach(({ x, y, width, height, color, shape }) => {
|
|
1412
1597
|
context.fillStyle = color;
|
|
1413
|
-
|
|
1598
|
+
if (shape === "circle") {
|
|
1599
|
+
context.beginPath();
|
|
1600
|
+
context.arc(x + width / 2, y + height / 2, width / 2, 0, Math.PI * 2);
|
|
1601
|
+
context.fill();
|
|
1602
|
+
} else {
|
|
1603
|
+
context.fillRect(x, y, width, height);
|
|
1604
|
+
}
|
|
1414
1605
|
});
|
|
1415
1606
|
}
|
|
1416
1607
|
drawCorners(filter) {
|
|
@@ -1423,18 +1614,15 @@ var QRCanvas = class _QRCanvas {
|
|
|
1423
1614
|
}
|
|
1424
1615
|
const options = this._options;
|
|
1425
1616
|
const count = this._qr.getModuleCount();
|
|
1426
|
-
const
|
|
1427
|
-
const dotSize = Math.floor(minSize / count);
|
|
1617
|
+
const { dotSize, x: xBeginning, y: yBeginning } = getQRArea(options, count);
|
|
1428
1618
|
const cornersSquareSize = dotSize * 7;
|
|
1429
1619
|
const cornersDotSize = dotSize * 3;
|
|
1430
1620
|
const cornerTypes = getCornerTypes(options);
|
|
1431
|
-
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1432
|
-
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1433
1621
|
[
|
|
1434
1622
|
[0, 0, 0],
|
|
1435
1623
|
[1, 0, Math.PI / 2],
|
|
1436
1624
|
[0, 1, -Math.PI / 2]
|
|
1437
|
-
].forEach(([column, row, rotation]) => {
|
|
1625
|
+
].forEach(([column, row, rotation], index) => {
|
|
1438
1626
|
var _a, _b, _c, _d, _e, _f;
|
|
1439
1627
|
if (filter && !filter(column, row)) {
|
|
1440
1628
|
return;
|
|
@@ -1481,6 +1669,8 @@ var QRCanvas = class _QRCanvas {
|
|
|
1481
1669
|
canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
|
|
1482
1670
|
} else if ((_c = options.cornersSquareOptions) == null ? void 0 : _c.color) {
|
|
1483
1671
|
canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersSquareOptions.color;
|
|
1672
|
+
} else if (this._finderColors) {
|
|
1673
|
+
canvasContext.fillStyle = canvasContext.strokeStyle = this._finderColors[index];
|
|
1484
1674
|
}
|
|
1485
1675
|
canvasContext.fill("evenodd");
|
|
1486
1676
|
if (cornerTypes.dot) {
|
|
@@ -1523,6 +1713,8 @@ var QRCanvas = class _QRCanvas {
|
|
|
1523
1713
|
canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
|
|
1524
1714
|
} else if ((_f = options.cornersDotOptions) == null ? void 0 : _f.color) {
|
|
1525
1715
|
canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersDotOptions.color;
|
|
1716
|
+
} else if (this._finderColors) {
|
|
1717
|
+
canvasContext.fillStyle = canvasContext.strokeStyle = this._finderColors[index];
|
|
1526
1718
|
}
|
|
1527
1719
|
canvasContext.fill("evenodd");
|
|
1528
1720
|
});
|
|
@@ -1571,8 +1763,7 @@ var QRCanvas = class _QRCanvas {
|
|
|
1571
1763
|
throw "image is not defined";
|
|
1572
1764
|
}
|
|
1573
1765
|
const options = this._options;
|
|
1574
|
-
const xBeginning =
|
|
1575
|
-
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1766
|
+
const { x: xBeginning, y: yBeginning } = getQRArea(options, count);
|
|
1576
1767
|
const dx = xBeginning + options.imageOptions.margin + (count * dotSize - width) / 2;
|
|
1577
1768
|
const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
|
|
1578
1769
|
const dw = width - options.imageOptions.margin * 2;
|
|
@@ -1673,6 +1864,19 @@ var QRDot2 = class {
|
|
|
1673
1864
|
};
|
|
1674
1865
|
break;
|
|
1675
1866
|
}
|
|
1867
|
+
case dotTypes_default.fluid:
|
|
1868
|
+
drawFunction = (args) => {
|
|
1869
|
+
const has = (dx, dy) => args.getNeighbor ? args.getNeighbor(dx, dy) : false;
|
|
1870
|
+
this._drawShape(
|
|
1871
|
+
fluid(args.x, args.y, args.size, {
|
|
1872
|
+
left: has(-1, 0),
|
|
1873
|
+
right: has(1, 0),
|
|
1874
|
+
top: has(0, -1),
|
|
1875
|
+
bottom: has(0, 1)
|
|
1876
|
+
})
|
|
1877
|
+
);
|
|
1878
|
+
};
|
|
1879
|
+
break;
|
|
1676
1880
|
case dotTypes_default.square:
|
|
1677
1881
|
default:
|
|
1678
1882
|
drawFunction = this._drawSquare;
|
|
@@ -2323,8 +2527,7 @@ var QRSVG = class _QRSVG {
|
|
|
2323
2527
|
return this._drawFramed(qr);
|
|
2324
2528
|
}
|
|
2325
2529
|
const count = qr.getModuleCount();
|
|
2326
|
-
const
|
|
2327
|
-
const dotSize = Math.floor(minSize / count);
|
|
2530
|
+
const { dotSize } = getQRArea(this._options, count);
|
|
2328
2531
|
let drawImageSize = {
|
|
2329
2532
|
hideXDots: 0,
|
|
2330
2533
|
hideYDots: 0,
|
|
@@ -2368,7 +2571,12 @@ var QRSVG = class _QRSVG {
|
|
|
2368
2571
|
}
|
|
2369
2572
|
this.clear();
|
|
2370
2573
|
this.drawBackground();
|
|
2371
|
-
if ((art == null ? void 0 : art.mode) === "
|
|
2574
|
+
if ((art == null ? void 0 : art.mode) === "blend") {
|
|
2575
|
+
this._appendImage(art.picture.toDataURL("image/png"), 0, 0);
|
|
2576
|
+
this._drawArtRects(art.rects);
|
|
2577
|
+
this.drawDots(() => false);
|
|
2578
|
+
this._finderColors = art.finderColors;
|
|
2579
|
+
} else if ((art == null ? void 0 : art.mode) === "halftone") {
|
|
2372
2580
|
this._drawArtRects(art.rects);
|
|
2373
2581
|
this.drawDots(() => false);
|
|
2374
2582
|
} else {
|
|
@@ -2380,6 +2588,7 @@ var QRSVG = class _QRSVG {
|
|
|
2380
2588
|
this._dotsImageHref = void 0;
|
|
2381
2589
|
}
|
|
2382
2590
|
this.drawCorners();
|
|
2591
|
+
this._finderColors = void 0;
|
|
2383
2592
|
if (this._options.image) {
|
|
2384
2593
|
this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
|
|
2385
2594
|
}
|
|
@@ -2392,6 +2601,21 @@ var QRSVG = class _QRSVG {
|
|
|
2392
2601
|
const gradientOptions = (_a = options.backgroundOptions) == null ? void 0 : _a.gradient;
|
|
2393
2602
|
const color = (_b = options.backgroundOptions) == null ? void 0 : _b.color;
|
|
2394
2603
|
if (gradientOptions || color) {
|
|
2604
|
+
if (isCircle(options)) {
|
|
2605
|
+
const size = Math.min(options.width, options.height);
|
|
2606
|
+
this._createColor({
|
|
2607
|
+
options: gradientOptions,
|
|
2608
|
+
color,
|
|
2609
|
+
additionalRotation: 0,
|
|
2610
|
+
x: (options.width - size) / 2,
|
|
2611
|
+
y: (options.height - size) / 2,
|
|
2612
|
+
height: size,
|
|
2613
|
+
width: size,
|
|
2614
|
+
name: "background-color",
|
|
2615
|
+
radius: size / 2
|
|
2616
|
+
});
|
|
2617
|
+
return;
|
|
2618
|
+
}
|
|
2395
2619
|
this._createColor({
|
|
2396
2620
|
options: gradientOptions,
|
|
2397
2621
|
color,
|
|
@@ -2415,10 +2639,8 @@ var QRSVG = class _QRSVG {
|
|
|
2415
2639
|
if (count > options.width || count > options.height) {
|
|
2416
2640
|
throw "The canvas is too small.";
|
|
2417
2641
|
}
|
|
2418
|
-
const
|
|
2419
|
-
const
|
|
2420
|
-
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
2421
|
-
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
2642
|
+
const { dotSize, x: xBeginning, y: yBeginning } = getQRArea(options, count);
|
|
2643
|
+
const colorPad = isCircle(options) ? getCircleLayout(count).pad * dotSize : 0;
|
|
2422
2644
|
const typeDot = new QRDot2({
|
|
2423
2645
|
svg: this._element,
|
|
2424
2646
|
type: options.dotsOptions.type,
|
|
@@ -2434,30 +2656,39 @@ var QRSVG = class _QRSVG {
|
|
|
2434
2656
|
options: (_a = options.dotsOptions) == null ? void 0 : _a.gradient,
|
|
2435
2657
|
color: options.dotsOptions.color,
|
|
2436
2658
|
additionalRotation: 0,
|
|
2437
|
-
x: xBeginning,
|
|
2438
|
-
y: yBeginning,
|
|
2439
|
-
height: count * dotSize,
|
|
2440
|
-
width: count * dotSize,
|
|
2659
|
+
x: xBeginning - colorPad,
|
|
2660
|
+
y: yBeginning - colorPad,
|
|
2661
|
+
height: count * dotSize + 2 * colorPad,
|
|
2662
|
+
width: count * dotSize + 2 * colorPad,
|
|
2441
2663
|
name: "dot-color",
|
|
2442
2664
|
imageHref: this._dotsImageHref
|
|
2443
2665
|
});
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2666
|
+
const circle = isCircle(options) ? getCircleLayout(count) : void 0;
|
|
2667
|
+
const from = circle ? -circle.pad : 0;
|
|
2668
|
+
const to = circle ? count + circle.pad : count;
|
|
2669
|
+
const inCode = (i, j) => i >= 0 && j >= 0 && i < count && j < count;
|
|
2670
|
+
const isDark = (i, j) => {
|
|
2671
|
+
if (filter && !filter(i, j)) return false;
|
|
2672
|
+
if (inCode(i, j)) return !!this._qr && this._qr.isDark(i, j);
|
|
2673
|
+
return !!circle && circle.isDecoration(i, j);
|
|
2674
|
+
};
|
|
2675
|
+
for (let i = from; i < to; i++) {
|
|
2676
|
+
for (let j = from; j < to; j++) {
|
|
2677
|
+
if (inCode(i, j) ? filter && !filter(i, j) || !((_b = this._qr) == null ? void 0 : _b.isDark(i, j)) : !isDark(i, j)) {
|
|
2450
2678
|
continue;
|
|
2451
2679
|
}
|
|
2452
|
-
const dot = role && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
2680
|
+
const dot = role && inCode(i, j) && role(i, j) === "alignment" ? squareDot : typeDot;
|
|
2453
2681
|
dot.draw(
|
|
2454
2682
|
xBeginning + i * dotSize,
|
|
2455
2683
|
yBeginning + j * dotSize,
|
|
2456
2684
|
dotSize,
|
|
2457
2685
|
(xOffset, yOffset) => {
|
|
2458
|
-
if (
|
|
2459
|
-
|
|
2460
|
-
|
|
2686
|
+
if (!circle) {
|
|
2687
|
+
if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
|
|
2688
|
+
if (filter && !filter(i + xOffset, j + yOffset)) return false;
|
|
2689
|
+
return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
|
|
2690
|
+
}
|
|
2691
|
+
return isDark(i + xOffset, j + yOffset);
|
|
2461
2692
|
}
|
|
2462
2693
|
);
|
|
2463
2694
|
if (dot._element && this._dotsClipPath) {
|
|
@@ -2476,31 +2707,28 @@ var QRSVG = class _QRSVG {
|
|
|
2476
2707
|
throw "Element code is not defined";
|
|
2477
2708
|
}
|
|
2478
2709
|
const count = this._qr.getModuleCount();
|
|
2479
|
-
const
|
|
2480
|
-
const dotSize = Math.floor(minSize / count);
|
|
2710
|
+
const { dotSize, x: xBeginning, y: yBeginning } = getQRArea(options, count);
|
|
2481
2711
|
const cornersSquareSize = dotSize * 7;
|
|
2482
2712
|
const cornersDotSize = dotSize * 3;
|
|
2483
2713
|
const cornerTypes = getCornerTypes(options);
|
|
2484
|
-
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
2485
|
-
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
2486
2714
|
[
|
|
2487
2715
|
[0, 0, 0],
|
|
2488
2716
|
[1, 0, Math.PI / 2],
|
|
2489
2717
|
[0, 1, -Math.PI / 2]
|
|
2490
|
-
].forEach(([column, row, rotation]) => {
|
|
2491
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2718
|
+
].forEach(([column, row, rotation], index) => {
|
|
2719
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
2492
2720
|
const x = xBeginning + column * dotSize * (count - 7);
|
|
2493
2721
|
const y = yBeginning + row * dotSize * (count - 7);
|
|
2494
2722
|
let cornersSquareClipPath = this._dotsClipPath;
|
|
2495
2723
|
let cornersDotClipPath = this._dotsClipPath;
|
|
2496
|
-
if (((_a = options.cornersSquareOptions) == null ? void 0 : _a.gradient) || ((_b = options.cornersSquareOptions) == null ? void 0 : _b.color)) {
|
|
2724
|
+
if (((_a = options.cornersSquareOptions) == null ? void 0 : _a.gradient) || ((_b = options.cornersSquareOptions) == null ? void 0 : _b.color) || this._finderColors) {
|
|
2497
2725
|
cornersSquareClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
2498
2726
|
cornersSquareClipPath.setAttribute("id", `clip-path-corners-square-color-${column}-${row}`);
|
|
2499
2727
|
this._defs.appendChild(cornersSquareClipPath);
|
|
2500
2728
|
this._cornersSquareClipPath = this._cornersDotClipPath = cornersDotClipPath = cornersSquareClipPath;
|
|
2501
2729
|
this._createColor({
|
|
2502
2730
|
options: (_c = options.cornersSquareOptions) == null ? void 0 : _c.gradient,
|
|
2503
|
-
color: (_d = options.cornersSquareOptions) == null ? void 0 : _d.color,
|
|
2731
|
+
color: (_f = (_d = options.cornersSquareOptions) == null ? void 0 : _d.color) != null ? _f : (_e = this._finderColors) == null ? void 0 : _e[index],
|
|
2504
2732
|
additionalRotation: rotation,
|
|
2505
2733
|
x,
|
|
2506
2734
|
y,
|
|
@@ -2527,7 +2755,7 @@ var QRSVG = class _QRSVG {
|
|
|
2527
2755
|
});
|
|
2528
2756
|
for (let i = 0; i < squareMask2.length; i++) {
|
|
2529
2757
|
for (let j = 0; j < squareMask2[i].length; j++) {
|
|
2530
|
-
if (!((
|
|
2758
|
+
if (!((_g = squareMask2[i]) == null ? void 0 : _g[j])) {
|
|
2531
2759
|
continue;
|
|
2532
2760
|
}
|
|
2533
2761
|
dot.draw(
|
|
@@ -2545,14 +2773,14 @@ var QRSVG = class _QRSVG {
|
|
|
2545
2773
|
}
|
|
2546
2774
|
}
|
|
2547
2775
|
}
|
|
2548
|
-
if (((
|
|
2776
|
+
if (((_h = options.cornersDotOptions) == null ? void 0 : _h.gradient) || ((_i = options.cornersDotOptions) == null ? void 0 : _i.color) || this._finderColors) {
|
|
2549
2777
|
cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
2550
2778
|
cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
|
|
2551
2779
|
this._defs.appendChild(cornersDotClipPath);
|
|
2552
2780
|
this._cornersDotClipPath = cornersDotClipPath;
|
|
2553
2781
|
this._createColor({
|
|
2554
|
-
options: (
|
|
2555
|
-
color: (
|
|
2782
|
+
options: (_j = options.cornersDotOptions) == null ? void 0 : _j.gradient,
|
|
2783
|
+
color: (_m = (_k = options.cornersDotOptions) == null ? void 0 : _k.color) != null ? _m : (_l = this._finderColors) == null ? void 0 : _l[index],
|
|
2556
2784
|
additionalRotation: rotation,
|
|
2557
2785
|
x: x + dotSize * 2,
|
|
2558
2786
|
y: y + dotSize * 2,
|
|
@@ -2579,7 +2807,7 @@ var QRSVG = class _QRSVG {
|
|
|
2579
2807
|
});
|
|
2580
2808
|
for (let i = 0; i < dotMask2.length; i++) {
|
|
2581
2809
|
for (let j = 0; j < dotMask2[i].length; j++) {
|
|
2582
|
-
if (!((
|
|
2810
|
+
if (!((_n = dotMask2[i]) == null ? void 0 : _n[j])) {
|
|
2583
2811
|
continue;
|
|
2584
2812
|
}
|
|
2585
2813
|
dot.draw(
|
|
@@ -2600,14 +2828,10 @@ var QRSVG = class _QRSVG {
|
|
|
2600
2828
|
});
|
|
2601
2829
|
}
|
|
2602
2830
|
_drawArtRects(rects) {
|
|
2603
|
-
rects.forEach(({ x, y, width, height, color }) => {
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
rect2.setAttribute("width", String(width));
|
|
2608
|
-
rect2.setAttribute("height", String(height));
|
|
2609
|
-
rect2.setAttribute("fill", color);
|
|
2610
|
-
this._element.appendChild(rect2);
|
|
2831
|
+
rects.forEach(({ x, y, width, height, color, shape }) => {
|
|
2832
|
+
this._element.appendChild(
|
|
2833
|
+
shape === "circle" ? this._svgElement("circle", { cx: x + width / 2, cy: y + height / 2, r: width / 2, fill: color }) : this._svgElement("rect", { x, y, width, height, fill: color })
|
|
2834
|
+
);
|
|
2611
2835
|
});
|
|
2612
2836
|
}
|
|
2613
2837
|
_appendImage(href, x, y) {
|
|
@@ -2679,8 +2903,7 @@ var QRSVG = class _QRSVG {
|
|
|
2679
2903
|
dotSize
|
|
2680
2904
|
}) {
|
|
2681
2905
|
const options = this._options;
|
|
2682
|
-
const xBeginning =
|
|
2683
|
-
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
2906
|
+
const { x: xBeginning, y: yBeginning } = getQRArea(options, count);
|
|
2684
2907
|
const dx = xBeginning + options.imageOptions.margin + (count * dotSize - width) / 2;
|
|
2685
2908
|
const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
|
|
2686
2909
|
const dw = width - options.imageOptions.margin * 2;
|
|
@@ -2711,7 +2934,8 @@ var QRSVG = class _QRSVG {
|
|
|
2711
2934
|
height,
|
|
2712
2935
|
width,
|
|
2713
2936
|
name,
|
|
2714
|
-
imageHref
|
|
2937
|
+
imageHref,
|
|
2938
|
+
radius
|
|
2715
2939
|
}) {
|
|
2716
2940
|
const size = width > height ? width : height;
|
|
2717
2941
|
if (imageHref) {
|
|
@@ -2731,6 +2955,10 @@ var QRSVG = class _QRSVG {
|
|
|
2731
2955
|
rect2.setAttribute("height", String(height));
|
|
2732
2956
|
rect2.setAttribute("width", String(width));
|
|
2733
2957
|
rect2.setAttribute("clip-path", `url('#clip-path-${name}')`);
|
|
2958
|
+
if (radius) {
|
|
2959
|
+
rect2.setAttribute("rx", String(radius));
|
|
2960
|
+
rect2.setAttribute("ry", String(radius));
|
|
2961
|
+
}
|
|
2734
2962
|
if (options) {
|
|
2735
2963
|
let gradient;
|
|
2736
2964
|
if (options.type === gradientTypes_default.radial) {
|
|
@@ -2909,7 +3137,93 @@ function sanitizeOptions(options) {
|
|
|
2909
3137
|
|
|
2910
3138
|
// src/core/QRCodeStyling.ts
|
|
2911
3139
|
import qrcode from "qrcode-generator";
|
|
2912
|
-
|
|
3140
|
+
|
|
3141
|
+
// src/tools/utf8.ts
|
|
3142
|
+
function toUTF8Bytes(text) {
|
|
3143
|
+
const bytes = [];
|
|
3144
|
+
for (let i = 0; i < text.length; i++) {
|
|
3145
|
+
let code = text.charCodeAt(i);
|
|
3146
|
+
if (code >= 55296 && code < 56320 && i + 1 < text.length) {
|
|
3147
|
+
const next = text.charCodeAt(i + 1);
|
|
3148
|
+
if (next >= 56320 && next < 57344) {
|
|
3149
|
+
code = 65536 + (code - 55296 << 10) + (next - 56320);
|
|
3150
|
+
i++;
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
if (code < 128) bytes.push(code);
|
|
3154
|
+
else if (code < 2048) bytes.push(192 | code >> 6, 128 | code & 63);
|
|
3155
|
+
else if (code < 65536) bytes.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
|
|
3156
|
+
else
|
|
3157
|
+
bytes.push(240 | code >> 18, 128 | code >> 12 & 63, 128 | code >> 6 & 63, 128 | code & 63);
|
|
3158
|
+
}
|
|
3159
|
+
return bytes;
|
|
3160
|
+
}
|
|
3161
|
+
var needsUTF8 = (text) => {
|
|
3162
|
+
for (let i = 0; i < text.length; i++) if (text.charCodeAt(i) > 255) return true;
|
|
3163
|
+
return false;
|
|
3164
|
+
};
|
|
3165
|
+
|
|
3166
|
+
// src/tools/payloads.ts
|
|
3167
|
+
var escapeWifi = (value) => value.replace(/([\\;,:"])/g, "\\$1");
|
|
3168
|
+
var escapeText = (value) => value.replace(/\\/g, "\\\\").replace(/;/g, "\\;").replace(/,/g, "\\,").replace(/\r?\n/g, "\\n");
|
|
3169
|
+
var list = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
3170
|
+
function wifi({ ssid, password, encryption, hidden }) {
|
|
3171
|
+
const type = encryption || (password ? "WPA" : "nopass");
|
|
3172
|
+
let data = `WIFI:T:${type};S:${escapeWifi(ssid)};`;
|
|
3173
|
+
if (type !== "nopass" && password) data += `P:${escapeWifi(password)};`;
|
|
3174
|
+
if (hidden) data += "H:true;";
|
|
3175
|
+
return `${data};`;
|
|
3176
|
+
}
|
|
3177
|
+
function vCard(contact) {
|
|
3178
|
+
const first = contact.firstName || "";
|
|
3179
|
+
const last = contact.lastName || "";
|
|
3180
|
+
const fullName = contact.name || [first, last].filter(Boolean).join(" ");
|
|
3181
|
+
const lines = [
|
|
3182
|
+
"BEGIN:VCARD",
|
|
3183
|
+
"VERSION:3.0",
|
|
3184
|
+
`N:${escapeText(last)};${escapeText(first)};;;`,
|
|
3185
|
+
`FN:${escapeText(fullName)}`
|
|
3186
|
+
];
|
|
3187
|
+
if (contact.organization) lines.push(`ORG:${escapeText(contact.organization)}`);
|
|
3188
|
+
if (contact.title) lines.push(`TITLE:${escapeText(contact.title)}`);
|
|
3189
|
+
list(contact.phone).forEach((phone2) => lines.push(`TEL;TYPE=CELL:${escapeText(phone2)}`));
|
|
3190
|
+
list(contact.email).forEach((email2) => lines.push(`EMAIL:${escapeText(email2)}`));
|
|
3191
|
+
if (contact.url) lines.push(`URL:${escapeText(contact.url)}`);
|
|
3192
|
+
if (contact.address) {
|
|
3193
|
+
const { street = "", city = "", region = "", postalCode = "", country = "" } = contact.address;
|
|
3194
|
+
lines.push(`ADR;TYPE=WORK:;;${[street, city, region, postalCode, country].map(escapeText).join(";")}`);
|
|
3195
|
+
}
|
|
3196
|
+
if (contact.note) lines.push(`NOTE:${escapeText(contact.note)}`);
|
|
3197
|
+
lines.push("END:VCARD");
|
|
3198
|
+
return lines.join("\r\n");
|
|
3199
|
+
}
|
|
3200
|
+
var pad = (value) => value < 10 ? `0${value}` : String(value);
|
|
3201
|
+
var formatDate = (value, allDay) => {
|
|
3202
|
+
const date = typeof value === "string" ? new Date(value) : value;
|
|
3203
|
+
if (isNaN(date.getTime())) throw new Error(`Invalid date: ${value}`);
|
|
3204
|
+
if (allDay) return `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}`;
|
|
3205
|
+
return `${date.getUTCFullYear()}${pad(date.getUTCMonth() + 1)}${pad(date.getUTCDate())}T${pad(date.getUTCHours())}${pad(date.getUTCMinutes())}${pad(date.getUTCSeconds())}Z`;
|
|
3206
|
+
};
|
|
3207
|
+
function event({ title, start, end, allDay, location, description }) {
|
|
3208
|
+
const dateType = allDay ? ";VALUE=DATE" : "";
|
|
3209
|
+
const lines = ["BEGIN:VEVENT", `SUMMARY:${escapeText(title)}`, `DTSTART${dateType}:${formatDate(start, allDay)}`];
|
|
3210
|
+
if (end) lines.push(`DTEND${dateType}:${formatDate(end, allDay)}`);
|
|
3211
|
+
if (location) lines.push(`LOCATION:${escapeText(location)}`);
|
|
3212
|
+
if (description) lines.push(`DESCRIPTION:${escapeText(description)}`);
|
|
3213
|
+
lines.push("END:VEVENT");
|
|
3214
|
+
return lines.join("\r\n");
|
|
3215
|
+
}
|
|
3216
|
+
function email({ to, subject, body }) {
|
|
3217
|
+
const query = [subject && `subject=${encodeURIComponent(subject)}`, body && `body=${encodeURIComponent(body)}`].filter(Boolean).join("&");
|
|
3218
|
+
return `mailto:${to}${query ? `?${query}` : ""}`;
|
|
3219
|
+
}
|
|
3220
|
+
var phone = (number) => `tel:${number.replace(/[^\d+*#]/g, "")}`;
|
|
3221
|
+
var sms = ({ number, message }) => `SMSTO:${number.replace(/[^\d+]/g, "")}:${message || ""}`;
|
|
3222
|
+
var geo = ({ latitude, longitude }) => `geo:${latitude},${longitude}`;
|
|
3223
|
+
var payloads_default = { wifi, vCard, event, email, phone, sms, geo };
|
|
3224
|
+
|
|
3225
|
+
// src/core/QRCodeStyling.ts
|
|
3226
|
+
var _QRCodeStyling = class _QRCodeStyling {
|
|
2913
3227
|
constructor(options) {
|
|
2914
3228
|
this._options = options ? sanitizeOptions(mergeDeep(QROptions_default, options)) : QROptions_default;
|
|
2915
3229
|
this.update();
|
|
@@ -2962,7 +3276,19 @@ var QRCodeStyling = class _QRCodeStyling {
|
|
|
2962
3276
|
return;
|
|
2963
3277
|
}
|
|
2964
3278
|
this._qr = qrcode(this._options.qrOptions.typeNumber, this._options.qrOptions.errorCorrectionLevel);
|
|
2965
|
-
|
|
3279
|
+
const data = this._options.data;
|
|
3280
|
+
if (needsUTF8(data)) {
|
|
3281
|
+
const generator = qrcode;
|
|
3282
|
+
const latin1 = generator.stringToBytes;
|
|
3283
|
+
generator.stringToBytes = toUTF8Bytes;
|
|
3284
|
+
try {
|
|
3285
|
+
this._qr.addData(data, this._options.qrOptions.mode || getMode(data));
|
|
3286
|
+
} finally {
|
|
3287
|
+
generator.stringToBytes = latin1;
|
|
3288
|
+
}
|
|
3289
|
+
} else {
|
|
3290
|
+
this._qr.addData(data, this._options.qrOptions.mode || getMode(data));
|
|
3291
|
+
}
|
|
2966
3292
|
this._qr.make();
|
|
2967
3293
|
if (this._options.type === drawTypes_default.canvas && !this._canUseCanvas()) {
|
|
2968
3294
|
this._canvas = void 0;
|
|
@@ -3074,12 +3400,12 @@ ${serializer.serializeToString(svg.getElement())}`;
|
|
|
3074
3400
|
}
|
|
3075
3401
|
async _decode(decoder) {
|
|
3076
3402
|
const { data, width, height } = await this._getPixels();
|
|
3077
|
-
const
|
|
3078
|
-
const paddedWidth = width +
|
|
3079
|
-
const paddedHeight = height +
|
|
3403
|
+
const pad2 = Math.max(16, Math.round(Math.min(width, height) / 8));
|
|
3404
|
+
const paddedWidth = width + pad2 * 2;
|
|
3405
|
+
const paddedHeight = height + pad2 * 2;
|
|
3080
3406
|
const padded = new Uint8ClampedArray(paddedWidth * paddedHeight * 4).fill(255);
|
|
3081
3407
|
for (let y = 0; y < height; y++) {
|
|
3082
|
-
padded.set(data.subarray(y * width * 4, (y + 1) * width * 4), ((y +
|
|
3408
|
+
padded.set(data.subarray(y * width * 4, (y + 1) * width * 4), ((y + pad2) * paddedWidth + pad2) * 4);
|
|
3083
3409
|
}
|
|
3084
3410
|
const result = decoder(padded, paddedWidth, paddedHeight, { inversionAttempts: "attemptBoth" });
|
|
3085
3411
|
return result ? result.data : void 0;
|
|
@@ -3172,7 +3498,37 @@ ${source}`;
|
|
|
3172
3498
|
downloadURI(url, `${name}.${extension}`);
|
|
3173
3499
|
}
|
|
3174
3500
|
}
|
|
3501
|
+
/**
|
|
3502
|
+
* Opens the phone's share sheet with the QR code as a file (Web Share API). Where sharing files is not
|
|
3503
|
+
* supported (most desktop browsers), downloads it instead. Resolves to "shared", "downloaded", or
|
|
3504
|
+
* "cancelled" when the user closes the share sheet.
|
|
3505
|
+
*/
|
|
3506
|
+
async share(shareOptions = {}) {
|
|
3507
|
+
if (!this._qr) throw "QR code is empty";
|
|
3508
|
+
const extension = shareOptions.extension || "png";
|
|
3509
|
+
const name = shareOptions.name || "qr";
|
|
3510
|
+
const nav = typeof navigator !== "undefined" ? navigator : void 0;
|
|
3511
|
+
if ((nav == null ? void 0 : nav.share) && typeof File !== "undefined") {
|
|
3512
|
+
const blob = await this.getRawData(extension);
|
|
3513
|
+
const type = extension === "svg" ? "image/svg+xml" : `image/${extension}`;
|
|
3514
|
+
const file = new File([blob], `${name}.${extension}`, { type });
|
|
3515
|
+
const data = { files: [file], title: shareOptions.title, text: shareOptions.text };
|
|
3516
|
+
if (!nav.canShare || nav.canShare(data)) {
|
|
3517
|
+
try {
|
|
3518
|
+
await nav.share(data);
|
|
3519
|
+
return "shared";
|
|
3520
|
+
} catch (error) {
|
|
3521
|
+
if ((error == null ? void 0 : error.name) === "AbortError") return "cancelled";
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
}
|
|
3525
|
+
await this.download({ name, extension });
|
|
3526
|
+
return "downloaded";
|
|
3527
|
+
}
|
|
3175
3528
|
};
|
|
3529
|
+
/** Builders for Wi-Fi, contact, event, e-mail, phone, SMS and location codes: pass the result as `data`. */
|
|
3530
|
+
_QRCodeStyling.payload = payloads_default;
|
|
3531
|
+
var QRCodeStyling = _QRCodeStyling;
|
|
3176
3532
|
|
|
3177
3533
|
// src/types/index.ts
|
|
3178
3534
|
var JSDom = class {
|
|
@@ -3195,6 +3551,7 @@ export {
|
|
|
3195
3551
|
errorCorrectionLevels_default as errorCorrectionLevels,
|
|
3196
3552
|
errorCorrectionPercents_default as errorCorrectionPercents,
|
|
3197
3553
|
modes_default as modes,
|
|
3554
|
+
payloads_default as payload,
|
|
3198
3555
|
qrTypes_default as qrTypes
|
|
3199
3556
|
};
|
|
3200
3557
|
//# sourceMappingURL=qr-code-styling.mjs.map
|