qr-code-styling-node 1.5.0 → 1.6.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.
@@ -0,0 +1,2047 @@
1
+ // src/constants/modes.ts
2
+ var modes_default = {
3
+ numeric: "Numeric",
4
+ alphanumeric: "Alphanumeric",
5
+ byte: "Byte",
6
+ kanji: "Kanji"
7
+ };
8
+
9
+ // src/tools/getMode.ts
10
+ function getMode(data) {
11
+ switch (true) {
12
+ case /^[0-9]*$/.test(data):
13
+ return modes_default.numeric;
14
+ case /^[0-9A-Z $%*+\-./:]*$/.test(data):
15
+ return modes_default.alphanumeric;
16
+ default:
17
+ return modes_default.byte;
18
+ }
19
+ }
20
+
21
+ // src/tools/merge.ts
22
+ var isObject = (obj) => !!obj && typeof obj === "object" && !Array.isArray(obj);
23
+ function mergeDeep(target, ...sources) {
24
+ if (!sources.length) return target;
25
+ const source = sources.shift();
26
+ if (source === void 0 || !isObject(target) || !isObject(source)) return target;
27
+ target = { ...target };
28
+ Object.keys(source).forEach((key) => {
29
+ const targetValue = target[key];
30
+ const sourceValue = source[key];
31
+ if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
32
+ target[key] = sourceValue;
33
+ } else if (isObject(targetValue) && isObject(sourceValue)) {
34
+ target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue);
35
+ } else {
36
+ target[key] = sourceValue;
37
+ }
38
+ });
39
+ return mergeDeep(target, ...sources);
40
+ }
41
+
42
+ // src/tools/downloadURI.ts
43
+ function downloadURI(uri, name) {
44
+ const link = document.createElement("a");
45
+ link.download = name;
46
+ link.href = uri;
47
+ document.body.appendChild(link);
48
+ link.click();
49
+ document.body.removeChild(link);
50
+ }
51
+
52
+ // src/tools/calculateImageSize.ts
53
+ function calculateImageSize({
54
+ originalHeight,
55
+ originalWidth,
56
+ maxHiddenDots,
57
+ maxHiddenAxisDots,
58
+ dotSize
59
+ }) {
60
+ const hideDots = { x: 0, y: 0 };
61
+ const imageSize = { x: 0, y: 0 };
62
+ if (originalHeight <= 0 || originalWidth <= 0 || maxHiddenDots <= 0 || dotSize <= 0) {
63
+ return {
64
+ height: 0,
65
+ width: 0,
66
+ hideYDots: 0,
67
+ hideXDots: 0
68
+ };
69
+ }
70
+ const k = originalHeight / originalWidth;
71
+ hideDots.x = Math.floor(Math.sqrt(maxHiddenDots / k));
72
+ if (hideDots.x <= 0) hideDots.x = 1;
73
+ if (maxHiddenAxisDots && maxHiddenAxisDots < hideDots.x) hideDots.x = maxHiddenAxisDots;
74
+ if (hideDots.x % 2 === 0) hideDots.x--;
75
+ imageSize.x = hideDots.x * dotSize;
76
+ hideDots.y = 1 + 2 * Math.ceil((hideDots.x * k - 1) / 2);
77
+ imageSize.y = Math.round(imageSize.x * k);
78
+ if (hideDots.y * hideDots.x > maxHiddenDots || maxHiddenAxisDots && maxHiddenAxisDots < hideDots.y) {
79
+ if (maxHiddenAxisDots && maxHiddenAxisDots < hideDots.y) {
80
+ hideDots.y = maxHiddenAxisDots;
81
+ if (hideDots.y % 2 === 0) hideDots.x--;
82
+ } else {
83
+ hideDots.y -= 2;
84
+ }
85
+ imageSize.y = hideDots.y * dotSize;
86
+ hideDots.x = 1 + 2 * Math.ceil((hideDots.y / k - 1) / 2);
87
+ imageSize.x = Math.round(imageSize.y / k);
88
+ }
89
+ return {
90
+ height: imageSize.y,
91
+ width: imageSize.x,
92
+ hideYDots: hideDots.y,
93
+ hideXDots: hideDots.x
94
+ };
95
+ }
96
+
97
+ // src/constants/errorCorrectionPercents.ts
98
+ var errorCorrectionPercents_default = {
99
+ L: 0.07,
100
+ M: 0.15,
101
+ Q: 0.25,
102
+ H: 0.3
103
+ };
104
+
105
+ // src/constants/dotTypes.ts
106
+ var dotTypes_default = {
107
+ dots: "dots",
108
+ rounded: "rounded",
109
+ classy: "classy",
110
+ classyRounded: "classy-rounded",
111
+ square: "square",
112
+ extraRounded: "extra-rounded"
113
+ };
114
+
115
+ // src/figures/dot/canvas/QRDot.ts
116
+ var QRDot = class {
117
+ constructor({ context, type }) {
118
+ this._context = context;
119
+ this._type = type;
120
+ }
121
+ draw(x, y, size, getNeighbor) {
122
+ const context = this._context;
123
+ const type = this._type;
124
+ let drawFunction;
125
+ switch (type) {
126
+ case dotTypes_default.dots:
127
+ drawFunction = this._drawDot;
128
+ break;
129
+ case dotTypes_default.classy:
130
+ drawFunction = this._drawClassy;
131
+ break;
132
+ case dotTypes_default.classyRounded:
133
+ drawFunction = this._drawClassyRounded;
134
+ break;
135
+ case dotTypes_default.rounded:
136
+ drawFunction = this._drawRounded;
137
+ break;
138
+ case dotTypes_default.extraRounded:
139
+ drawFunction = this._drawExtraRounded;
140
+ break;
141
+ case dotTypes_default.square:
142
+ default:
143
+ drawFunction = this._drawSquare;
144
+ }
145
+ drawFunction.call(this, { x, y, size, context, getNeighbor });
146
+ }
147
+ _rotateFigure({ x, y, size, context, rotation = 0, draw }) {
148
+ const cx = x + size / 2;
149
+ const cy = y + size / 2;
150
+ context.translate(cx, cy);
151
+ rotation && context.rotate(rotation);
152
+ draw();
153
+ context.closePath();
154
+ rotation && context.rotate(-rotation);
155
+ context.translate(-cx, -cy);
156
+ }
157
+ _basicDot(args) {
158
+ const { size, context } = args;
159
+ this._rotateFigure({
160
+ ...args,
161
+ draw: () => {
162
+ context.arc(0, 0, size / 2, 0, Math.PI * 2);
163
+ }
164
+ });
165
+ }
166
+ _basicSquare(args) {
167
+ const { size, context } = args;
168
+ this._rotateFigure({
169
+ ...args,
170
+ draw: () => {
171
+ context.rect(-size / 2, -size / 2, size, size);
172
+ }
173
+ });
174
+ }
175
+ //if rotation === 0 - right side is rounded
176
+ _basicSideRounded(args) {
177
+ const { size, context } = args;
178
+ this._rotateFigure({
179
+ ...args,
180
+ draw: () => {
181
+ context.arc(0, 0, size / 2, -Math.PI / 2, Math.PI / 2);
182
+ context.lineTo(-size / 2, size / 2);
183
+ context.lineTo(-size / 2, -size / 2);
184
+ context.lineTo(0, -size / 2);
185
+ }
186
+ });
187
+ }
188
+ //if rotation === 0 - top right corner is rounded
189
+ _basicCornerRounded(args) {
190
+ const { size, context } = args;
191
+ this._rotateFigure({
192
+ ...args,
193
+ draw: () => {
194
+ context.arc(0, 0, size / 2, -Math.PI / 2, 0);
195
+ context.lineTo(size / 2, size / 2);
196
+ context.lineTo(-size / 2, size / 2);
197
+ context.lineTo(-size / 2, -size / 2);
198
+ context.lineTo(0, -size / 2);
199
+ }
200
+ });
201
+ }
202
+ //if rotation === 0 - top right corner is rounded
203
+ _basicCornerExtraRounded(args) {
204
+ const { size, context } = args;
205
+ this._rotateFigure({
206
+ ...args,
207
+ draw: () => {
208
+ context.arc(-size / 2, size / 2, size, -Math.PI / 2, 0);
209
+ context.lineTo(-size / 2, size / 2);
210
+ context.lineTo(-size / 2, -size / 2);
211
+ }
212
+ });
213
+ }
214
+ _basicCornersRounded(args) {
215
+ const { size, context } = args;
216
+ this._rotateFigure({
217
+ ...args,
218
+ draw: () => {
219
+ context.arc(0, 0, size / 2, -Math.PI / 2, 0);
220
+ context.lineTo(size / 2, size / 2);
221
+ context.lineTo(0, size / 2);
222
+ context.arc(0, 0, size / 2, Math.PI / 2, Math.PI);
223
+ context.lineTo(-size / 2, -size / 2);
224
+ context.lineTo(0, -size / 2);
225
+ }
226
+ });
227
+ }
228
+ _basicCornersExtraRounded(args) {
229
+ const { size, context } = args;
230
+ this._rotateFigure({
231
+ ...args,
232
+ draw: () => {
233
+ context.arc(-size / 2, size / 2, size, -Math.PI / 2, 0);
234
+ context.arc(size / 2, -size / 2, size, Math.PI / 2, Math.PI);
235
+ }
236
+ });
237
+ }
238
+ _drawDot({ x, y, size, context }) {
239
+ this._basicDot({ x, y, size, context, rotation: 0 });
240
+ }
241
+ _drawSquare({ x, y, size, context }) {
242
+ this._basicSquare({ x, y, size, context, rotation: 0 });
243
+ }
244
+ _drawRounded({ x, y, size, context, getNeighbor }) {
245
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
246
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
247
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
248
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
249
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
250
+ if (neighborsCount === 0) {
251
+ this._basicDot({ x, y, size, context, rotation: 0 });
252
+ return;
253
+ }
254
+ if (neighborsCount > 2 || leftNeighbor && rightNeighbor || topNeighbor && bottomNeighbor) {
255
+ this._basicSquare({ x, y, size, context, rotation: 0 });
256
+ return;
257
+ }
258
+ if (neighborsCount === 2) {
259
+ let rotation = 0;
260
+ if (leftNeighbor && topNeighbor) {
261
+ rotation = Math.PI / 2;
262
+ } else if (topNeighbor && rightNeighbor) {
263
+ rotation = Math.PI;
264
+ } else if (rightNeighbor && bottomNeighbor) {
265
+ rotation = -Math.PI / 2;
266
+ }
267
+ this._basicCornerRounded({ x, y, size, context, rotation });
268
+ return;
269
+ }
270
+ if (neighborsCount === 1) {
271
+ let rotation = 0;
272
+ if (topNeighbor) {
273
+ rotation = Math.PI / 2;
274
+ } else if (rightNeighbor) {
275
+ rotation = Math.PI;
276
+ } else if (bottomNeighbor) {
277
+ rotation = -Math.PI / 2;
278
+ }
279
+ this._basicSideRounded({ x, y, size, context, rotation });
280
+ return;
281
+ }
282
+ }
283
+ _drawExtraRounded({ x, y, size, context, getNeighbor }) {
284
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
285
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
286
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
287
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
288
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
289
+ if (neighborsCount === 0) {
290
+ this._basicDot({ x, y, size, context, rotation: 0 });
291
+ return;
292
+ }
293
+ if (neighborsCount > 2 || leftNeighbor && rightNeighbor || topNeighbor && bottomNeighbor) {
294
+ this._basicSquare({ x, y, size, context, rotation: 0 });
295
+ return;
296
+ }
297
+ if (neighborsCount === 2) {
298
+ let rotation = 0;
299
+ if (leftNeighbor && topNeighbor) {
300
+ rotation = Math.PI / 2;
301
+ } else if (topNeighbor && rightNeighbor) {
302
+ rotation = Math.PI;
303
+ } else if (rightNeighbor && bottomNeighbor) {
304
+ rotation = -Math.PI / 2;
305
+ }
306
+ this._basicCornerExtraRounded({ x, y, size, context, rotation });
307
+ return;
308
+ }
309
+ if (neighborsCount === 1) {
310
+ let rotation = 0;
311
+ if (topNeighbor) {
312
+ rotation = Math.PI / 2;
313
+ } else if (rightNeighbor) {
314
+ rotation = Math.PI;
315
+ } else if (bottomNeighbor) {
316
+ rotation = -Math.PI / 2;
317
+ }
318
+ this._basicSideRounded({ x, y, size, context, rotation });
319
+ return;
320
+ }
321
+ }
322
+ _drawClassy({ x, y, size, context, getNeighbor }) {
323
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
324
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
325
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
326
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
327
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
328
+ if (neighborsCount === 0) {
329
+ this._basicCornersRounded({ x, y, size, context, rotation: Math.PI / 2 });
330
+ return;
331
+ }
332
+ if (!leftNeighbor && !topNeighbor) {
333
+ this._basicCornerRounded({ x, y, size, context, rotation: -Math.PI / 2 });
334
+ return;
335
+ }
336
+ if (!rightNeighbor && !bottomNeighbor) {
337
+ this._basicCornerRounded({ x, y, size, context, rotation: Math.PI / 2 });
338
+ return;
339
+ }
340
+ this._basicSquare({ x, y, size, context, rotation: 0 });
341
+ }
342
+ _drawClassyRounded({ x, y, size, context, getNeighbor }) {
343
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
344
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
345
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
346
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
347
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
348
+ if (neighborsCount === 0) {
349
+ this._basicCornersRounded({ x, y, size, context, rotation: Math.PI / 2 });
350
+ return;
351
+ }
352
+ if (!leftNeighbor && !topNeighbor) {
353
+ this._basicCornerExtraRounded({ x, y, size, context, rotation: -Math.PI / 2 });
354
+ return;
355
+ }
356
+ if (!rightNeighbor && !bottomNeighbor) {
357
+ this._basicCornerExtraRounded({ x, y, size, context, rotation: Math.PI / 2 });
358
+ return;
359
+ }
360
+ this._basicSquare({ x, y, size, context, rotation: 0 });
361
+ }
362
+ };
363
+
364
+ // src/constants/cornerSquareTypes.ts
365
+ var cornerSquareTypes_default = {
366
+ dot: "dot",
367
+ square: "square",
368
+ extraRounded: "extra-rounded"
369
+ };
370
+
371
+ // src/figures/cornerSquare/canvas/QRCornerSquare.ts
372
+ var QRCornerSquare = class {
373
+ constructor({ context, type }) {
374
+ this._context = context;
375
+ this._type = type;
376
+ }
377
+ draw(x, y, size, rotation) {
378
+ const context = this._context;
379
+ const type = this._type;
380
+ let drawFunction;
381
+ switch (type) {
382
+ case cornerSquareTypes_default.square:
383
+ drawFunction = this._drawSquare;
384
+ break;
385
+ case cornerSquareTypes_default.extraRounded:
386
+ drawFunction = this._drawExtraRounded;
387
+ break;
388
+ case cornerSquareTypes_default.dot:
389
+ default:
390
+ drawFunction = this._drawDot;
391
+ }
392
+ drawFunction.call(this, { x, y, size, context, rotation });
393
+ }
394
+ _rotateFigure({ x, y, size, context, rotation = 0, draw }) {
395
+ const cx = x + size / 2;
396
+ const cy = y + size / 2;
397
+ context.translate(cx, cy);
398
+ rotation && context.rotate(rotation);
399
+ draw();
400
+ context.closePath();
401
+ rotation && context.rotate(-rotation);
402
+ context.translate(-cx, -cy);
403
+ }
404
+ _basicDot(args) {
405
+ const { size, context } = args;
406
+ const dotSize = size / 7;
407
+ this._rotateFigure({
408
+ ...args,
409
+ draw: () => {
410
+ context.arc(0, 0, size / 2, 0, Math.PI * 2);
411
+ context.arc(0, 0, size / 2 - dotSize, 0, Math.PI * 2);
412
+ }
413
+ });
414
+ }
415
+ _basicSquare(args) {
416
+ const { size, context } = args;
417
+ const dotSize = size / 7;
418
+ this._rotateFigure({
419
+ ...args,
420
+ draw: () => {
421
+ context.rect(-size / 2, -size / 2, size, size);
422
+ context.rect(-size / 2 + dotSize, -size / 2 + dotSize, size - 2 * dotSize, size - 2 * dotSize);
423
+ }
424
+ });
425
+ }
426
+ _basicExtraRounded(args) {
427
+ const { size, context } = args;
428
+ const dotSize = size / 7;
429
+ this._rotateFigure({
430
+ ...args,
431
+ draw: () => {
432
+ context.arc(-dotSize, -dotSize, 2.5 * dotSize, Math.PI, -Math.PI / 2);
433
+ context.lineTo(dotSize, -3.5 * dotSize);
434
+ context.arc(dotSize, -dotSize, 2.5 * dotSize, -Math.PI / 2, 0);
435
+ context.lineTo(3.5 * dotSize, -dotSize);
436
+ context.arc(dotSize, dotSize, 2.5 * dotSize, 0, Math.PI / 2);
437
+ context.lineTo(-dotSize, 3.5 * dotSize);
438
+ context.arc(-dotSize, dotSize, 2.5 * dotSize, Math.PI / 2, Math.PI);
439
+ context.lineTo(-3.5 * dotSize, -dotSize);
440
+ context.arc(-dotSize, -dotSize, 1.5 * dotSize, Math.PI, -Math.PI / 2);
441
+ context.lineTo(dotSize, -2.5 * dotSize);
442
+ context.arc(dotSize, -dotSize, 1.5 * dotSize, -Math.PI / 2, 0);
443
+ context.lineTo(2.5 * dotSize, -dotSize);
444
+ context.arc(dotSize, dotSize, 1.5 * dotSize, 0, Math.PI / 2);
445
+ context.lineTo(-dotSize, 2.5 * dotSize);
446
+ context.arc(-dotSize, dotSize, 1.5 * dotSize, Math.PI / 2, Math.PI);
447
+ context.lineTo(-2.5 * dotSize, -dotSize);
448
+ }
449
+ });
450
+ }
451
+ _drawDot({ x, y, size, context, rotation }) {
452
+ this._basicDot({ x, y, size, context, rotation });
453
+ }
454
+ _drawSquare({ x, y, size, context, rotation }) {
455
+ this._basicSquare({ x, y, size, context, rotation });
456
+ }
457
+ _drawExtraRounded({ x, y, size, context, rotation }) {
458
+ this._basicExtraRounded({ x, y, size, context, rotation });
459
+ }
460
+ };
461
+
462
+ // src/constants/cornerDotTypes.ts
463
+ var cornerDotTypes_default = {
464
+ dot: "dot",
465
+ square: "square"
466
+ };
467
+
468
+ // src/figures/cornerDot/canvas/QRCornerDot.ts
469
+ var QRCornerDot = class {
470
+ constructor({ context, type }) {
471
+ this._context = context;
472
+ this._type = type;
473
+ }
474
+ draw(x, y, size, rotation) {
475
+ const context = this._context;
476
+ const type = this._type;
477
+ let drawFunction;
478
+ switch (type) {
479
+ case cornerDotTypes_default.square:
480
+ drawFunction = this._drawSquare;
481
+ break;
482
+ case cornerDotTypes_default.dot:
483
+ default:
484
+ drawFunction = this._drawDot;
485
+ }
486
+ drawFunction.call(this, { x, y, size, context, rotation });
487
+ }
488
+ _rotateFigure({ x, y, size, context, rotation = 0, draw }) {
489
+ const cx = x + size / 2;
490
+ const cy = y + size / 2;
491
+ context.translate(cx, cy);
492
+ rotation && context.rotate(rotation);
493
+ draw();
494
+ context.closePath();
495
+ rotation && context.rotate(-rotation);
496
+ context.translate(-cx, -cy);
497
+ }
498
+ _basicDot(args) {
499
+ const { size, context } = args;
500
+ this._rotateFigure({
501
+ ...args,
502
+ draw: () => {
503
+ context.arc(0, 0, size / 2, 0, Math.PI * 2);
504
+ }
505
+ });
506
+ }
507
+ _basicSquare(args) {
508
+ const { size, context } = args;
509
+ this._rotateFigure({
510
+ ...args,
511
+ draw: () => {
512
+ context.rect(-size / 2, -size / 2, size, size);
513
+ }
514
+ });
515
+ }
516
+ _drawDot({ x, y, size, context, rotation }) {
517
+ this._basicDot({ x, y, size, context, rotation });
518
+ }
519
+ _drawSquare({ x, y, size, context, rotation }) {
520
+ this._basicSquare({ x, y, size, context, rotation });
521
+ }
522
+ };
523
+
524
+ // src/constants/gradientTypes.ts
525
+ var gradientTypes_default = {
526
+ radial: "radial",
527
+ linear: "linear"
528
+ };
529
+
530
+ // src/core/QRCanvas.ts
531
+ var squareMask = [
532
+ [1, 1, 1, 1, 1, 1, 1],
533
+ [1, 0, 0, 0, 0, 0, 1],
534
+ [1, 0, 0, 0, 0, 0, 1],
535
+ [1, 0, 0, 0, 0, 0, 1],
536
+ [1, 0, 0, 0, 0, 0, 1],
537
+ [1, 0, 0, 0, 0, 0, 1],
538
+ [1, 1, 1, 1, 1, 1, 1]
539
+ ];
540
+ var dotMask = [
541
+ [0, 0, 0, 0, 0, 0, 0],
542
+ [0, 0, 0, 0, 0, 0, 0],
543
+ [0, 0, 1, 1, 1, 0, 0],
544
+ [0, 0, 1, 1, 1, 0, 0],
545
+ [0, 0, 1, 1, 1, 0, 0],
546
+ [0, 0, 0, 0, 0, 0, 0],
547
+ [0, 0, 0, 0, 0, 0, 0]
548
+ ];
549
+ var QRCanvas = class {
550
+ //TODO don't pass all options to this class
551
+ constructor(options) {
552
+ var _a;
553
+ if ((_a = options.nodeCanvas) == null ? void 0 : _a.createCanvas) {
554
+ this._canvas = options.nodeCanvas.createCanvas(options.width, options.height);
555
+ } else {
556
+ this._canvas = document.createElement("canvas");
557
+ }
558
+ this._canvas.width = options.width;
559
+ this._canvas.height = options.height;
560
+ this._options = options;
561
+ }
562
+ get context() {
563
+ return this._canvas.getContext("2d");
564
+ }
565
+ get width() {
566
+ return this._canvas.width;
567
+ }
568
+ get height() {
569
+ return this._canvas.height;
570
+ }
571
+ getCanvas() {
572
+ return this._canvas;
573
+ }
574
+ clear() {
575
+ const canvasContext = this.context;
576
+ if (canvasContext) {
577
+ canvasContext.clearRect(0, 0, this._canvas.width, this._canvas.height);
578
+ }
579
+ }
580
+ async drawQR(qr) {
581
+ const count = qr.getModuleCount();
582
+ const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
583
+ const dotSize = Math.floor(minSize / count);
584
+ let drawImageSize = {
585
+ hideXDots: 0,
586
+ hideYDots: 0,
587
+ width: 0,
588
+ height: 0
589
+ };
590
+ this._qr = qr;
591
+ if (this._options.image) {
592
+ await this.loadImage();
593
+ if (!this._image) return;
594
+ const { imageOptions, qrOptions } = this._options;
595
+ const coverLevel = imageOptions.imageSize * errorCorrectionPercents_default[qrOptions.errorCorrectionLevel];
596
+ const maxHiddenDots = Math.floor(coverLevel * count * count);
597
+ drawImageSize = calculateImageSize({
598
+ originalWidth: this._image.width,
599
+ originalHeight: this._image.height,
600
+ maxHiddenDots,
601
+ maxHiddenAxisDots: count - 14,
602
+ dotSize
603
+ });
604
+ }
605
+ this.clear();
606
+ this.drawBackground();
607
+ this.drawDots((i, j) => {
608
+ var _a, _b, _c, _d, _e, _f;
609
+ if (this._options.imageOptions.hideBackgroundDots) {
610
+ if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
611
+ return false;
612
+ }
613
+ }
614
+ if (((_a = squareMask[i]) == null ? void 0 : _a[j]) || ((_b = squareMask[i - count + 7]) == null ? void 0 : _b[j]) || ((_c = squareMask[i]) == null ? void 0 : _c[j - count + 7])) {
615
+ return false;
616
+ }
617
+ if (((_d = dotMask[i]) == null ? void 0 : _d[j]) || ((_e = dotMask[i - count + 7]) == null ? void 0 : _e[j]) || ((_f = dotMask[i]) == null ? void 0 : _f[j - count + 7])) {
618
+ return false;
619
+ }
620
+ return true;
621
+ });
622
+ this.drawCorners();
623
+ if (this._options.image) {
624
+ this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
625
+ }
626
+ }
627
+ drawBackground() {
628
+ const canvasContext = this.context;
629
+ const options = this._options;
630
+ if (canvasContext) {
631
+ if (options.backgroundOptions.gradient) {
632
+ const gradientOptions = options.backgroundOptions.gradient;
633
+ const gradient = this._createGradient({
634
+ context: canvasContext,
635
+ options: gradientOptions,
636
+ additionalRotation: 0,
637
+ x: 0,
638
+ y: 0,
639
+ size: this._canvas.width > this._canvas.height ? this._canvas.width : this._canvas.height
640
+ });
641
+ gradientOptions.colorStops.forEach(({ offset, color }) => {
642
+ gradient.addColorStop(offset, color);
643
+ });
644
+ canvasContext.fillStyle = gradient;
645
+ } else if (options.backgroundOptions.color) {
646
+ canvasContext.fillStyle = options.backgroundOptions.color;
647
+ }
648
+ canvasContext.fillRect(0, 0, this._canvas.width, this._canvas.height);
649
+ }
650
+ }
651
+ drawDots(filter) {
652
+ if (!this._qr) {
653
+ throw "QR code is not defined";
654
+ }
655
+ const canvasContext = this.context;
656
+ if (!canvasContext) {
657
+ throw "QR code is not defined";
658
+ }
659
+ const options = this._options;
660
+ const count = this._qr.getModuleCount();
661
+ if (count > options.width || count > options.height) {
662
+ throw "The canvas is too small.";
663
+ }
664
+ const minSize = Math.min(options.width, options.height) - options.margin * 2;
665
+ const dotSize = Math.floor(minSize / count);
666
+ const xBeginning = Math.floor((options.width - count * dotSize) / 2);
667
+ const yBeginning = Math.floor((options.height - count * dotSize) / 2);
668
+ const dot = new QRDot({ context: canvasContext, type: options.dotsOptions.type });
669
+ canvasContext.beginPath();
670
+ for (let i = 0; i < count; i++) {
671
+ for (let j = 0; j < count; j++) {
672
+ if (filter && !filter(i, j)) {
673
+ continue;
674
+ }
675
+ if (!this._qr.isDark(i, j)) {
676
+ continue;
677
+ }
678
+ dot.draw(
679
+ xBeginning + i * dotSize,
680
+ yBeginning + j * dotSize,
681
+ dotSize,
682
+ (xOffset, yOffset) => {
683
+ if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
684
+ if (filter && !filter(i + xOffset, j + yOffset)) return false;
685
+ return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
686
+ }
687
+ );
688
+ }
689
+ }
690
+ if (options.dotsOptions.gradient) {
691
+ const gradientOptions = options.dotsOptions.gradient;
692
+ const gradient = this._createGradient({
693
+ context: canvasContext,
694
+ options: gradientOptions,
695
+ additionalRotation: 0,
696
+ x: xBeginning,
697
+ y: yBeginning,
698
+ size: count * dotSize
699
+ });
700
+ gradientOptions.colorStops.forEach(({ offset, color }) => {
701
+ gradient.addColorStop(offset, color);
702
+ });
703
+ canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
704
+ } else if (options.dotsOptions.color) {
705
+ canvasContext.fillStyle = canvasContext.strokeStyle = options.dotsOptions.color;
706
+ }
707
+ canvasContext.fill("evenodd");
708
+ }
709
+ drawCorners(filter) {
710
+ if (!this._qr) {
711
+ throw "QR code is not defined";
712
+ }
713
+ const canvasContext = this.context;
714
+ if (!canvasContext) {
715
+ throw "QR code is not defined";
716
+ }
717
+ const options = this._options;
718
+ const count = this._qr.getModuleCount();
719
+ const minSize = Math.min(options.width, options.height) - options.margin * 2;
720
+ const dotSize = Math.floor(minSize / count);
721
+ const cornersSquareSize = dotSize * 7;
722
+ const cornersDotSize = dotSize * 3;
723
+ const xBeginning = Math.floor((options.width - count * dotSize) / 2);
724
+ const yBeginning = Math.floor((options.height - count * dotSize) / 2);
725
+ [
726
+ [0, 0, 0],
727
+ [1, 0, Math.PI / 2],
728
+ [0, 1, -Math.PI / 2]
729
+ ].forEach(([column, row, rotation]) => {
730
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
731
+ if (filter && !filter(column, row)) {
732
+ return;
733
+ }
734
+ const x = xBeginning + column * dotSize * (count - 7);
735
+ const y = yBeginning + row * dotSize * (count - 7);
736
+ if ((_a = options.cornersSquareOptions) == null ? void 0 : _a.type) {
737
+ const cornersSquare = new QRCornerSquare({ context: canvasContext, type: (_b = options.cornersSquareOptions) == null ? void 0 : _b.type });
738
+ canvasContext.beginPath();
739
+ cornersSquare.draw(x, y, cornersSquareSize, rotation);
740
+ } else {
741
+ const dot = new QRDot({ context: canvasContext, type: options.dotsOptions.type });
742
+ canvasContext.beginPath();
743
+ for (let i = 0; i < squareMask.length; i++) {
744
+ for (let j = 0; j < squareMask[i].length; j++) {
745
+ if (!((_c = squareMask[i]) == null ? void 0 : _c[j])) {
746
+ continue;
747
+ }
748
+ dot.draw(
749
+ x + i * dotSize,
750
+ y + j * dotSize,
751
+ dotSize,
752
+ (xOffset, yOffset) => {
753
+ var _a2;
754
+ return !!((_a2 = squareMask[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
755
+ }
756
+ );
757
+ }
758
+ }
759
+ }
760
+ if ((_d = options.cornersSquareOptions) == null ? void 0 : _d.gradient) {
761
+ const gradientOptions = options.cornersSquareOptions.gradient;
762
+ const gradient = this._createGradient({
763
+ context: canvasContext,
764
+ options: gradientOptions,
765
+ additionalRotation: rotation,
766
+ x,
767
+ y,
768
+ size: cornersSquareSize
769
+ });
770
+ gradientOptions.colorStops.forEach(({ offset, color }) => {
771
+ gradient.addColorStop(offset, color);
772
+ });
773
+ canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
774
+ } else if ((_e = options.cornersSquareOptions) == null ? void 0 : _e.color) {
775
+ canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersSquareOptions.color;
776
+ }
777
+ canvasContext.fill("evenodd");
778
+ if ((_f = options.cornersDotOptions) == null ? void 0 : _f.type) {
779
+ const cornersDot = new QRCornerDot({ context: canvasContext, type: (_g = options.cornersDotOptions) == null ? void 0 : _g.type });
780
+ canvasContext.beginPath();
781
+ cornersDot.draw(x + dotSize * 2, y + dotSize * 2, cornersDotSize, rotation);
782
+ } else {
783
+ const dot = new QRDot({ context: canvasContext, type: options.dotsOptions.type });
784
+ canvasContext.beginPath();
785
+ for (let i = 0; i < dotMask.length; i++) {
786
+ for (let j = 0; j < dotMask[i].length; j++) {
787
+ if (!((_h = dotMask[i]) == null ? void 0 : _h[j])) {
788
+ continue;
789
+ }
790
+ dot.draw(
791
+ x + i * dotSize,
792
+ y + j * dotSize,
793
+ dotSize,
794
+ (xOffset, yOffset) => {
795
+ var _a2;
796
+ return !!((_a2 = dotMask[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
797
+ }
798
+ );
799
+ }
800
+ }
801
+ }
802
+ if ((_i = options.cornersDotOptions) == null ? void 0 : _i.gradient) {
803
+ const gradientOptions = options.cornersDotOptions.gradient;
804
+ const gradient = this._createGradient({
805
+ context: canvasContext,
806
+ options: gradientOptions,
807
+ additionalRotation: rotation,
808
+ x: x + dotSize * 2,
809
+ y: y + dotSize * 2,
810
+ size: cornersDotSize
811
+ });
812
+ gradientOptions.colorStops.forEach(({ offset, color }) => {
813
+ gradient.addColorStop(offset, color);
814
+ });
815
+ canvasContext.fillStyle = canvasContext.strokeStyle = gradient;
816
+ } else if ((_j = options.cornersDotOptions) == null ? void 0 : _j.color) {
817
+ canvasContext.fillStyle = canvasContext.strokeStyle = options.cornersDotOptions.color;
818
+ }
819
+ canvasContext.fill("evenodd");
820
+ });
821
+ }
822
+ loadImage() {
823
+ return new Promise((resolve, reject) => {
824
+ var _a;
825
+ const options = this._options;
826
+ if (!options.image) {
827
+ return reject("Image is not defined");
828
+ }
829
+ if ((_a = options.nodeCanvas) == null ? void 0 : _a.loadImage) {
830
+ options.nodeCanvas.loadImage(options.image).then((image) => {
831
+ var _a2;
832
+ if (/(\.svg$)|(^data:image\/svg)/.test((_a2 = options.image) != null ? _a2 : "")) {
833
+ image.width = this._options.width;
834
+ image.height = this._options.height;
835
+ }
836
+ this._image = image;
837
+ resolve();
838
+ }).catch(reject);
839
+ } else {
840
+ const image = new Image();
841
+ if (typeof options.imageOptions.crossOrigin === "string") {
842
+ image.crossOrigin = options.imageOptions.crossOrigin;
843
+ }
844
+ this._image = image;
845
+ image.onload = () => {
846
+ resolve();
847
+ };
848
+ image.src = options.image;
849
+ }
850
+ });
851
+ }
852
+ drawImage({
853
+ width,
854
+ height,
855
+ count,
856
+ dotSize
857
+ }) {
858
+ const canvasContext = this.context;
859
+ if (!canvasContext) {
860
+ throw "canvasContext is not defined";
861
+ }
862
+ if (!this._image) {
863
+ throw "image is not defined";
864
+ }
865
+ const options = this._options;
866
+ const xBeginning = Math.floor((options.width - count * dotSize) / 2);
867
+ const yBeginning = Math.floor((options.height - count * dotSize) / 2);
868
+ const dx = xBeginning + options.imageOptions.margin + (count * dotSize - width) / 2;
869
+ const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
870
+ const dw = width - options.imageOptions.margin * 2;
871
+ const dh = height - options.imageOptions.margin * 2;
872
+ canvasContext.drawImage(this._image, dx, dy, dw < 0 ? 0 : dw, dh < 0 ? 0 : dh);
873
+ }
874
+ _createGradient({
875
+ context,
876
+ options,
877
+ additionalRotation,
878
+ x,
879
+ y,
880
+ size
881
+ }) {
882
+ let gradient;
883
+ if (options.type === gradientTypes_default.radial) {
884
+ gradient = context.createRadialGradient(x + size / 2, y + size / 2, 0, x + size / 2, y + size / 2, size / 2);
885
+ } else {
886
+ const rotation = ((options.rotation || 0) + additionalRotation) % (2 * Math.PI);
887
+ const positiveRotation = (rotation + 2 * Math.PI) % (2 * Math.PI);
888
+ let x0 = x + size / 2;
889
+ let y0 = y + size / 2;
890
+ let x1 = x + size / 2;
891
+ let y1 = y + size / 2;
892
+ if (positiveRotation >= 0 && positiveRotation <= 0.25 * Math.PI || positiveRotation > 1.75 * Math.PI && positiveRotation <= 2 * Math.PI) {
893
+ x0 = x0 - size / 2;
894
+ y0 = y0 - size / 2 * Math.tan(rotation);
895
+ x1 = x1 + size / 2;
896
+ y1 = y1 + size / 2 * Math.tan(rotation);
897
+ } else if (positiveRotation > 0.25 * Math.PI && positiveRotation <= 0.75 * Math.PI) {
898
+ y0 = y0 - size / 2;
899
+ x0 = x0 - size / 2 / Math.tan(rotation);
900
+ y1 = y1 + size / 2;
901
+ x1 = x1 + size / 2 / Math.tan(rotation);
902
+ } else if (positiveRotation > 0.75 * Math.PI && positiveRotation <= 1.25 * Math.PI) {
903
+ x0 = x0 + size / 2;
904
+ y0 = y0 + size / 2 * Math.tan(rotation);
905
+ x1 = x1 - size / 2;
906
+ y1 = y1 - size / 2 * Math.tan(rotation);
907
+ } else if (positiveRotation > 1.25 * Math.PI && positiveRotation <= 1.75 * Math.PI) {
908
+ y0 = y0 + size / 2;
909
+ x0 = x0 + size / 2 / Math.tan(rotation);
910
+ y1 = y1 - size / 2;
911
+ x1 = x1 - size / 2 / Math.tan(rotation);
912
+ }
913
+ gradient = context.createLinearGradient(Math.round(x0), Math.round(y0), Math.round(x1), Math.round(y1));
914
+ }
915
+ return gradient;
916
+ }
917
+ };
918
+
919
+ // src/figures/dot/svg/QRDot.ts
920
+ var QRDot2 = class {
921
+ constructor({ svg, type, window: window2 }) {
922
+ this._svg = svg;
923
+ this._type = type;
924
+ this._window = window2;
925
+ }
926
+ draw(x, y, size, getNeighbor) {
927
+ const type = this._type;
928
+ let drawFunction;
929
+ switch (type) {
930
+ case dotTypes_default.dots:
931
+ drawFunction = this._drawDot;
932
+ break;
933
+ case dotTypes_default.classy:
934
+ drawFunction = this._drawClassy;
935
+ break;
936
+ case dotTypes_default.classyRounded:
937
+ drawFunction = this._drawClassyRounded;
938
+ break;
939
+ case dotTypes_default.rounded:
940
+ drawFunction = this._drawRounded;
941
+ break;
942
+ case dotTypes_default.extraRounded:
943
+ drawFunction = this._drawExtraRounded;
944
+ break;
945
+ case dotTypes_default.square:
946
+ default:
947
+ drawFunction = this._drawSquare;
948
+ }
949
+ drawFunction.call(this, { x, y, size, getNeighbor });
950
+ }
951
+ _rotateFigure({ x, y, size, rotation = 0, draw }) {
952
+ var _a;
953
+ const cx = x + size / 2;
954
+ const cy = y + size / 2;
955
+ draw();
956
+ (_a = this._element) == null ? void 0 : _a.setAttribute("transform", `rotate(${180 * rotation / Math.PI},${cx},${cy})`);
957
+ }
958
+ _basicDot(args) {
959
+ const { size, x, y } = args;
960
+ this._rotateFigure({
961
+ ...args,
962
+ draw: () => {
963
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "circle");
964
+ this._element.setAttribute("cx", String(x + size / 2));
965
+ this._element.setAttribute("cy", String(y + size / 2));
966
+ this._element.setAttribute("r", String(size / 2));
967
+ }
968
+ });
969
+ }
970
+ _basicSquare(args) {
971
+ const { size, x, y } = args;
972
+ this._rotateFigure({
973
+ ...args,
974
+ draw: () => {
975
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
976
+ this._element.setAttribute("x", String(x));
977
+ this._element.setAttribute("y", String(y));
978
+ this._element.setAttribute("width", String(size));
979
+ this._element.setAttribute("height", String(size));
980
+ }
981
+ });
982
+ }
983
+ //if rotation === 0 - right side is rounded
984
+ _basicSideRounded(args) {
985
+ const { size, x, y } = args;
986
+ this._rotateFigure({
987
+ ...args,
988
+ draw: () => {
989
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
990
+ this._element.setAttribute(
991
+ "d",
992
+ `M ${x} ${y}v ${size}h ${size / 2}a ${size / 2} ${size / 2}, 0, 0, 0, 0 ${-size}`
993
+ // draw rounded corner
994
+ );
995
+ }
996
+ });
997
+ }
998
+ //if rotation === 0 - top right corner is rounded
999
+ _basicCornerRounded(args) {
1000
+ const { size, x, y } = args;
1001
+ this._rotateFigure({
1002
+ ...args,
1003
+ draw: () => {
1004
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
1005
+ this._element.setAttribute(
1006
+ "d",
1007
+ `M ${x} ${y}v ${size}h ${size}v ${-size / 2}a ${size / 2} ${size / 2}, 0, 0, 0, ${-size / 2} ${-size / 2}`
1008
+ // draw rounded corner
1009
+ );
1010
+ }
1011
+ });
1012
+ }
1013
+ //if rotation === 0 - top right corner is rounded
1014
+ _basicCornerExtraRounded(args) {
1015
+ const { size, x, y } = args;
1016
+ this._rotateFigure({
1017
+ ...args,
1018
+ draw: () => {
1019
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
1020
+ this._element.setAttribute(
1021
+ "d",
1022
+ `M ${x} ${y}v ${size}h ${size}a ${size} ${size}, 0, 0, 0, ${-size} ${-size}`
1023
+ // draw rounded top right corner
1024
+ );
1025
+ }
1026
+ });
1027
+ }
1028
+ //if rotation === 0 - left bottom and right top corners are rounded
1029
+ _basicCornersRounded(args) {
1030
+ const { size, x, y } = args;
1031
+ this._rotateFigure({
1032
+ ...args,
1033
+ draw: () => {
1034
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
1035
+ this._element.setAttribute(
1036
+ "d",
1037
+ `M ${x} ${y}v ${size / 2}a ${size / 2} ${size / 2}, 0, 0, 0, ${size / 2} ${size / 2}h ${size / 2}v ${-size / 2}a ${size / 2} ${size / 2}, 0, 0, 0, ${-size / 2} ${-size / 2}`
1038
+ // draw rounded right top corner
1039
+ );
1040
+ }
1041
+ });
1042
+ }
1043
+ _drawDot({ x, y, size }) {
1044
+ this._basicDot({ x, y, size, rotation: 0 });
1045
+ }
1046
+ _drawSquare({ x, y, size }) {
1047
+ this._basicSquare({ x, y, size, rotation: 0 });
1048
+ }
1049
+ _drawRounded({ x, y, size, getNeighbor }) {
1050
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
1051
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
1052
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
1053
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
1054
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
1055
+ if (neighborsCount === 0) {
1056
+ this._basicDot({ x, y, size, rotation: 0 });
1057
+ return;
1058
+ }
1059
+ if (neighborsCount > 2 || leftNeighbor && rightNeighbor || topNeighbor && bottomNeighbor) {
1060
+ this._basicSquare({ x, y, size, rotation: 0 });
1061
+ return;
1062
+ }
1063
+ if (neighborsCount === 2) {
1064
+ let rotation = 0;
1065
+ if (leftNeighbor && topNeighbor) {
1066
+ rotation = Math.PI / 2;
1067
+ } else if (topNeighbor && rightNeighbor) {
1068
+ rotation = Math.PI;
1069
+ } else if (rightNeighbor && bottomNeighbor) {
1070
+ rotation = -Math.PI / 2;
1071
+ }
1072
+ this._basicCornerRounded({ x, y, size, rotation });
1073
+ return;
1074
+ }
1075
+ if (neighborsCount === 1) {
1076
+ let rotation = 0;
1077
+ if (topNeighbor) {
1078
+ rotation = Math.PI / 2;
1079
+ } else if (rightNeighbor) {
1080
+ rotation = Math.PI;
1081
+ } else if (bottomNeighbor) {
1082
+ rotation = -Math.PI / 2;
1083
+ }
1084
+ this._basicSideRounded({ x, y, size, rotation });
1085
+ return;
1086
+ }
1087
+ }
1088
+ _drawExtraRounded({ x, y, size, getNeighbor }) {
1089
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
1090
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
1091
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
1092
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
1093
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
1094
+ if (neighborsCount === 0) {
1095
+ this._basicDot({ x, y, size, rotation: 0 });
1096
+ return;
1097
+ }
1098
+ if (neighborsCount > 2 || leftNeighbor && rightNeighbor || topNeighbor && bottomNeighbor) {
1099
+ this._basicSquare({ x, y, size, rotation: 0 });
1100
+ return;
1101
+ }
1102
+ if (neighborsCount === 2) {
1103
+ let rotation = 0;
1104
+ if (leftNeighbor && topNeighbor) {
1105
+ rotation = Math.PI / 2;
1106
+ } else if (topNeighbor && rightNeighbor) {
1107
+ rotation = Math.PI;
1108
+ } else if (rightNeighbor && bottomNeighbor) {
1109
+ rotation = -Math.PI / 2;
1110
+ }
1111
+ this._basicCornerExtraRounded({ x, y, size, rotation });
1112
+ return;
1113
+ }
1114
+ if (neighborsCount === 1) {
1115
+ let rotation = 0;
1116
+ if (topNeighbor) {
1117
+ rotation = Math.PI / 2;
1118
+ } else if (rightNeighbor) {
1119
+ rotation = Math.PI;
1120
+ } else if (bottomNeighbor) {
1121
+ rotation = -Math.PI / 2;
1122
+ }
1123
+ this._basicSideRounded({ x, y, size, rotation });
1124
+ return;
1125
+ }
1126
+ }
1127
+ _drawClassy({ x, y, size, getNeighbor }) {
1128
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
1129
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
1130
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
1131
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
1132
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
1133
+ if (neighborsCount === 0) {
1134
+ this._basicCornersRounded({ x, y, size, rotation: Math.PI / 2 });
1135
+ return;
1136
+ }
1137
+ if (!leftNeighbor && !topNeighbor) {
1138
+ this._basicCornerRounded({ x, y, size, rotation: -Math.PI / 2 });
1139
+ return;
1140
+ }
1141
+ if (!rightNeighbor && !bottomNeighbor) {
1142
+ this._basicCornerRounded({ x, y, size, rotation: Math.PI / 2 });
1143
+ return;
1144
+ }
1145
+ this._basicSquare({ x, y, size, rotation: 0 });
1146
+ }
1147
+ _drawClassyRounded({ x, y, size, getNeighbor }) {
1148
+ const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
1149
+ const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
1150
+ const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
1151
+ const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;
1152
+ const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;
1153
+ if (neighborsCount === 0) {
1154
+ this._basicCornersRounded({ x, y, size, rotation: Math.PI / 2 });
1155
+ return;
1156
+ }
1157
+ if (!leftNeighbor && !topNeighbor) {
1158
+ this._basicCornerExtraRounded({ x, y, size, rotation: -Math.PI / 2 });
1159
+ return;
1160
+ }
1161
+ if (!rightNeighbor && !bottomNeighbor) {
1162
+ this._basicCornerExtraRounded({ x, y, size, rotation: Math.PI / 2 });
1163
+ return;
1164
+ }
1165
+ this._basicSquare({ x, y, size, rotation: 0 });
1166
+ }
1167
+ };
1168
+
1169
+ // src/figures/cornerSquare/svg/QRCornerSquare.ts
1170
+ var QRCornerSquare2 = class {
1171
+ constructor({ svg, type, window: window2 }) {
1172
+ this._svg = svg;
1173
+ this._type = type;
1174
+ this._window = window2;
1175
+ }
1176
+ draw(x, y, size, rotation) {
1177
+ const type = this._type;
1178
+ let drawFunction;
1179
+ switch (type) {
1180
+ case cornerSquareTypes_default.square:
1181
+ drawFunction = this._drawSquare;
1182
+ break;
1183
+ case cornerSquareTypes_default.extraRounded:
1184
+ drawFunction = this._drawExtraRounded;
1185
+ break;
1186
+ case cornerSquareTypes_default.dot:
1187
+ default:
1188
+ drawFunction = this._drawDot;
1189
+ }
1190
+ drawFunction.call(this, { x, y, size, rotation });
1191
+ }
1192
+ _rotateFigure({ x, y, size, rotation = 0, draw }) {
1193
+ var _a;
1194
+ const cx = x + size / 2;
1195
+ const cy = y + size / 2;
1196
+ draw();
1197
+ (_a = this._element) == null ? void 0 : _a.setAttribute("transform", `rotate(${180 * rotation / Math.PI},${cx},${cy})`);
1198
+ }
1199
+ _basicDot(args) {
1200
+ const { size, x, y } = args;
1201
+ const dotSize = size / 7;
1202
+ this._rotateFigure({
1203
+ ...args,
1204
+ draw: () => {
1205
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
1206
+ this._element.setAttribute("clip-rule", "evenodd");
1207
+ this._element.setAttribute(
1208
+ "d",
1209
+ `M ${x + size / 2} ${y}a ${size / 2} ${size / 2} 0 1 0 0.1 0zm 0 ${dotSize}a ${size / 2 - dotSize} ${size / 2 - dotSize} 0 1 1 -0.1 0Z`
1210
+ // Z // Close the inner ring. Actually will still work without, but inner ring will have one unit missing in stroke
1211
+ );
1212
+ }
1213
+ });
1214
+ }
1215
+ _basicSquare(args) {
1216
+ const { size, x, y } = args;
1217
+ const dotSize = size / 7;
1218
+ this._rotateFigure({
1219
+ ...args,
1220
+ draw: () => {
1221
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
1222
+ this._element.setAttribute("clip-rule", "evenodd");
1223
+ this._element.setAttribute(
1224
+ "d",
1225
+ `M ${x} ${y}v ${size}h ${size}v ${-size}zM ${x + dotSize} ${y + dotSize}h ${size - 2 * dotSize}v ${size - 2 * dotSize}h ${-size + 2 * dotSize}z`
1226
+ );
1227
+ }
1228
+ });
1229
+ }
1230
+ _basicExtraRounded(args) {
1231
+ const { size, x, y } = args;
1232
+ const dotSize = size / 7;
1233
+ this._rotateFigure({
1234
+ ...args,
1235
+ draw: () => {
1236
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "path");
1237
+ this._element.setAttribute("clip-rule", "evenodd");
1238
+ this._element.setAttribute(
1239
+ "d",
1240
+ `M ${x} ${y + 2.5 * dotSize}v ${2 * dotSize}a ${2.5 * dotSize} ${2.5 * dotSize}, 0, 0, 0, ${dotSize * 2.5} ${dotSize * 2.5}h ${2 * dotSize}a ${2.5 * dotSize} ${2.5 * dotSize}, 0, 0, 0, ${dotSize * 2.5} ${-dotSize * 2.5}v ${-2 * dotSize}a ${2.5 * dotSize} ${2.5 * dotSize}, 0, 0, 0, ${-dotSize * 2.5} ${-dotSize * 2.5}h ${-2 * dotSize}a ${2.5 * dotSize} ${2.5 * dotSize}, 0, 0, 0, ${-dotSize * 2.5} ${dotSize * 2.5}M ${x + 2.5 * dotSize} ${y + dotSize}h ${2 * dotSize}a ${1.5 * dotSize} ${1.5 * dotSize}, 0, 0, 1, ${dotSize * 1.5} ${dotSize * 1.5}v ${2 * dotSize}a ${1.5 * dotSize} ${1.5 * dotSize}, 0, 0, 1, ${-dotSize * 1.5} ${dotSize * 1.5}h ${-2 * dotSize}a ${1.5 * dotSize} ${1.5 * dotSize}, 0, 0, 1, ${-dotSize * 1.5} ${-dotSize * 1.5}v ${-2 * dotSize}a ${1.5 * dotSize} ${1.5 * dotSize}, 0, 0, 1, ${dotSize * 1.5} ${-dotSize * 1.5}`
1241
+ );
1242
+ }
1243
+ });
1244
+ }
1245
+ _drawDot({ x, y, size, rotation }) {
1246
+ this._basicDot({ x, y, size, rotation });
1247
+ }
1248
+ _drawSquare({ x, y, size, rotation }) {
1249
+ this._basicSquare({ x, y, size, rotation });
1250
+ }
1251
+ _drawExtraRounded({ x, y, size, rotation }) {
1252
+ this._basicExtraRounded({ x, y, size, rotation });
1253
+ }
1254
+ };
1255
+
1256
+ // src/figures/cornerDot/svg/QRCornerDot.ts
1257
+ var QRCornerDot2 = class {
1258
+ constructor({ svg, type, window: window2 }) {
1259
+ this._svg = svg;
1260
+ this._type = type;
1261
+ this._window = window2;
1262
+ }
1263
+ draw(x, y, size, rotation) {
1264
+ const type = this._type;
1265
+ let drawFunction;
1266
+ switch (type) {
1267
+ case cornerDotTypes_default.square:
1268
+ drawFunction = this._drawSquare;
1269
+ break;
1270
+ case cornerDotTypes_default.dot:
1271
+ default:
1272
+ drawFunction = this._drawDot;
1273
+ }
1274
+ drawFunction.call(this, { x, y, size, rotation });
1275
+ }
1276
+ _rotateFigure({ x, y, size, rotation = 0, draw }) {
1277
+ var _a;
1278
+ const cx = x + size / 2;
1279
+ const cy = y + size / 2;
1280
+ draw();
1281
+ (_a = this._element) == null ? void 0 : _a.setAttribute("transform", `rotate(${180 * rotation / Math.PI},${cx},${cy})`);
1282
+ }
1283
+ _basicDot(args) {
1284
+ const { size, x, y } = args;
1285
+ this._rotateFigure({
1286
+ ...args,
1287
+ draw: () => {
1288
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "circle");
1289
+ this._element.setAttribute("cx", String(x + size / 2));
1290
+ this._element.setAttribute("cy", String(y + size / 2));
1291
+ this._element.setAttribute("r", String(size / 2));
1292
+ }
1293
+ });
1294
+ }
1295
+ _basicSquare(args) {
1296
+ const { size, x, y } = args;
1297
+ this._rotateFigure({
1298
+ ...args,
1299
+ draw: () => {
1300
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
1301
+ this._element.setAttribute("x", String(x));
1302
+ this._element.setAttribute("y", String(y));
1303
+ this._element.setAttribute("width", String(size));
1304
+ this._element.setAttribute("height", String(size));
1305
+ }
1306
+ });
1307
+ }
1308
+ _drawDot({ x, y, size, rotation }) {
1309
+ this._basicDot({ x, y, size, rotation });
1310
+ }
1311
+ _drawSquare({ x, y, size, rotation }) {
1312
+ this._basicSquare({ x, y, size, rotation });
1313
+ }
1314
+ };
1315
+
1316
+ // src/core/QRSVG.ts
1317
+ var squareMask2 = [
1318
+ [1, 1, 1, 1, 1, 1, 1],
1319
+ [1, 0, 0, 0, 0, 0, 1],
1320
+ [1, 0, 0, 0, 0, 0, 1],
1321
+ [1, 0, 0, 0, 0, 0, 1],
1322
+ [1, 0, 0, 0, 0, 0, 1],
1323
+ [1, 0, 0, 0, 0, 0, 1],
1324
+ [1, 1, 1, 1, 1, 1, 1]
1325
+ ];
1326
+ var dotMask2 = [
1327
+ [0, 0, 0, 0, 0, 0, 0],
1328
+ [0, 0, 0, 0, 0, 0, 0],
1329
+ [0, 0, 1, 1, 1, 0, 0],
1330
+ [0, 0, 1, 1, 1, 0, 0],
1331
+ [0, 0, 1, 1, 1, 0, 0],
1332
+ [0, 0, 0, 0, 0, 0, 0],
1333
+ [0, 0, 0, 0, 0, 0, 0]
1334
+ ];
1335
+ var QRSVG = class {
1336
+ //TODO don't pass all options to this class
1337
+ constructor(options) {
1338
+ var _a;
1339
+ if (options.jsdom) {
1340
+ this._window = new options.jsdom("", { resources: "usable" }).window;
1341
+ } else {
1342
+ this._window = window;
1343
+ }
1344
+ this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "svg");
1345
+ this._element.setAttribute("width", String(options.width));
1346
+ this._element.setAttribute("height", String(options.height));
1347
+ this._defs = this._window.document.createElementNS("http://www.w3.org/2000/svg", "defs");
1348
+ this._element.appendChild(this._defs);
1349
+ if (options.imageOptions.saveAsBlob) {
1350
+ if ((_a = options.nodeCanvas) == null ? void 0 : _a.createCanvas) {
1351
+ this._canvas = options.nodeCanvas.createCanvas(options.width, options.height);
1352
+ } else {
1353
+ this._canvas = document.createElement("canvas");
1354
+ }
1355
+ this._canvas.width = options.width;
1356
+ this._canvas.height = options.height;
1357
+ }
1358
+ this._imageUri = options.image;
1359
+ this._options = options;
1360
+ }
1361
+ get width() {
1362
+ return this._options.width;
1363
+ }
1364
+ get height() {
1365
+ return this._options.height;
1366
+ }
1367
+ getElement() {
1368
+ return this._element;
1369
+ }
1370
+ clear() {
1371
+ var _a;
1372
+ const oldElement = this._element;
1373
+ this._element = oldElement.cloneNode(false);
1374
+ (_a = oldElement == null ? void 0 : oldElement.parentNode) == null ? void 0 : _a.replaceChild(this._element, oldElement);
1375
+ this._defs = this._window.document.createElementNS("http://www.w3.org/2000/svg", "defs");
1376
+ this._element.appendChild(this._defs);
1377
+ }
1378
+ async drawQR(qr) {
1379
+ const count = qr.getModuleCount();
1380
+ const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
1381
+ const dotSize = Math.floor(minSize / count);
1382
+ let drawImageSize = {
1383
+ hideXDots: 0,
1384
+ hideYDots: 0,
1385
+ width: 0,
1386
+ height: 0
1387
+ };
1388
+ this._qr = qr;
1389
+ if (this._options.image) {
1390
+ await this.loadImage();
1391
+ if (!this._image) return;
1392
+ this.imageToBlob();
1393
+ const { imageOptions, qrOptions } = this._options;
1394
+ const coverLevel = imageOptions.imageSize * errorCorrectionPercents_default[qrOptions.errorCorrectionLevel];
1395
+ const maxHiddenDots = Math.floor(coverLevel * count * count);
1396
+ drawImageSize = calculateImageSize({
1397
+ originalWidth: this._image.width,
1398
+ originalHeight: this._image.height,
1399
+ maxHiddenDots,
1400
+ maxHiddenAxisDots: count - 14,
1401
+ dotSize
1402
+ });
1403
+ }
1404
+ this.clear();
1405
+ this.drawBackground();
1406
+ this.drawDots((i, j) => {
1407
+ var _a, _b, _c, _d, _e, _f;
1408
+ if (this._options.imageOptions.hideBackgroundDots) {
1409
+ if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
1410
+ return false;
1411
+ }
1412
+ }
1413
+ if (((_a = squareMask2[i]) == null ? void 0 : _a[j]) || ((_b = squareMask2[i - count + 7]) == null ? void 0 : _b[j]) || ((_c = squareMask2[i]) == null ? void 0 : _c[j - count + 7])) {
1414
+ return false;
1415
+ }
1416
+ if (((_d = dotMask2[i]) == null ? void 0 : _d[j]) || ((_e = dotMask2[i - count + 7]) == null ? void 0 : _e[j]) || ((_f = dotMask2[i]) == null ? void 0 : _f[j - count + 7])) {
1417
+ return false;
1418
+ }
1419
+ return true;
1420
+ });
1421
+ this.drawCorners();
1422
+ if (this._options.image) {
1423
+ this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
1424
+ }
1425
+ }
1426
+ drawBackground() {
1427
+ var _a, _b;
1428
+ const element = this._element;
1429
+ const options = this._options;
1430
+ if (element) {
1431
+ const gradientOptions = (_a = options.backgroundOptions) == null ? void 0 : _a.gradient;
1432
+ const color = (_b = options.backgroundOptions) == null ? void 0 : _b.color;
1433
+ if (gradientOptions || color) {
1434
+ this._createColor({
1435
+ options: gradientOptions,
1436
+ color,
1437
+ additionalRotation: 0,
1438
+ x: 0,
1439
+ y: 0,
1440
+ height: options.height,
1441
+ width: options.width,
1442
+ name: "background-color"
1443
+ });
1444
+ }
1445
+ }
1446
+ }
1447
+ drawDots(filter) {
1448
+ var _a, _b;
1449
+ if (!this._qr) {
1450
+ throw "QR code is not defined";
1451
+ }
1452
+ const options = this._options;
1453
+ const count = this._qr.getModuleCount();
1454
+ if (count > options.width || count > options.height) {
1455
+ throw "The canvas is too small.";
1456
+ }
1457
+ const minSize = Math.min(options.width, options.height) - options.margin * 2;
1458
+ const dotSize = Math.floor(minSize / count);
1459
+ const xBeginning = Math.floor((options.width - count * dotSize) / 2);
1460
+ const yBeginning = Math.floor((options.height - count * dotSize) / 2);
1461
+ const dot = new QRDot2({
1462
+ svg: this._element,
1463
+ type: options.dotsOptions.type,
1464
+ window: this._window
1465
+ });
1466
+ this._dotsClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
1467
+ this._dotsClipPath.setAttribute("id", "clip-path-dot-color");
1468
+ this._defs.appendChild(this._dotsClipPath);
1469
+ this._createColor({
1470
+ options: (_a = options.dotsOptions) == null ? void 0 : _a.gradient,
1471
+ color: options.dotsOptions.color,
1472
+ additionalRotation: 0,
1473
+ x: xBeginning,
1474
+ y: yBeginning,
1475
+ height: count * dotSize,
1476
+ width: count * dotSize,
1477
+ name: "dot-color"
1478
+ });
1479
+ for (let i = 0; i < count; i++) {
1480
+ for (let j = 0; j < count; j++) {
1481
+ if (filter && !filter(i, j)) {
1482
+ continue;
1483
+ }
1484
+ if (!((_b = this._qr) == null ? void 0 : _b.isDark(i, j))) {
1485
+ continue;
1486
+ }
1487
+ dot.draw(
1488
+ xBeginning + i * dotSize,
1489
+ yBeginning + j * dotSize,
1490
+ dotSize,
1491
+ (xOffset, yOffset) => {
1492
+ if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
1493
+ if (filter && !filter(i + xOffset, j + yOffset)) return false;
1494
+ return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
1495
+ }
1496
+ );
1497
+ if (dot._element && this._dotsClipPath) {
1498
+ this._dotsClipPath.appendChild(dot._element);
1499
+ }
1500
+ }
1501
+ }
1502
+ }
1503
+ drawCorners() {
1504
+ if (!this._qr) {
1505
+ throw "QR code is not defined";
1506
+ }
1507
+ const element = this._element;
1508
+ const options = this._options;
1509
+ if (!element) {
1510
+ throw "Element code is not defined";
1511
+ }
1512
+ const count = this._qr.getModuleCount();
1513
+ const minSize = Math.min(options.width, options.height) - options.margin * 2;
1514
+ const dotSize = Math.floor(minSize / count);
1515
+ const cornersSquareSize = dotSize * 7;
1516
+ const cornersDotSize = dotSize * 3;
1517
+ const xBeginning = Math.floor((options.width - count * dotSize) / 2);
1518
+ const yBeginning = Math.floor((options.height - count * dotSize) / 2);
1519
+ [
1520
+ [0, 0, 0],
1521
+ [1, 0, Math.PI / 2],
1522
+ [0, 1, -Math.PI / 2]
1523
+ ].forEach(([column, row, rotation]) => {
1524
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1525
+ const x = xBeginning + column * dotSize * (count - 7);
1526
+ const y = yBeginning + row * dotSize * (count - 7);
1527
+ let cornersSquareClipPath = this._dotsClipPath;
1528
+ let cornersDotClipPath = this._dotsClipPath;
1529
+ if (((_a = options.cornersSquareOptions) == null ? void 0 : _a.gradient) || ((_b = options.cornersSquareOptions) == null ? void 0 : _b.color)) {
1530
+ cornersSquareClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
1531
+ cornersSquareClipPath.setAttribute("id", `clip-path-corners-square-color-${column}-${row}`);
1532
+ this._defs.appendChild(cornersSquareClipPath);
1533
+ this._cornersSquareClipPath = this._cornersDotClipPath = cornersDotClipPath = cornersSquareClipPath;
1534
+ this._createColor({
1535
+ options: (_c = options.cornersSquareOptions) == null ? void 0 : _c.gradient,
1536
+ color: (_d = options.cornersSquareOptions) == null ? void 0 : _d.color,
1537
+ additionalRotation: rotation,
1538
+ x,
1539
+ y,
1540
+ height: cornersSquareSize,
1541
+ width: cornersSquareSize,
1542
+ name: `corners-square-color-${column}-${row}`
1543
+ });
1544
+ }
1545
+ if ((_e = options.cornersSquareOptions) == null ? void 0 : _e.type) {
1546
+ const cornersSquare = new QRCornerSquare2({
1547
+ svg: this._element,
1548
+ type: options.cornersSquareOptions.type,
1549
+ window: this._window
1550
+ });
1551
+ cornersSquare.draw(x, y, cornersSquareSize, rotation);
1552
+ if (cornersSquare._element && cornersSquareClipPath) {
1553
+ cornersSquareClipPath.appendChild(cornersSquare._element);
1554
+ }
1555
+ } else {
1556
+ const dot = new QRDot2({
1557
+ svg: this._element,
1558
+ type: options.dotsOptions.type,
1559
+ window: this._window
1560
+ });
1561
+ for (let i = 0; i < squareMask2.length; i++) {
1562
+ for (let j = 0; j < squareMask2[i].length; j++) {
1563
+ if (!((_f = squareMask2[i]) == null ? void 0 : _f[j])) {
1564
+ continue;
1565
+ }
1566
+ dot.draw(
1567
+ x + i * dotSize,
1568
+ y + j * dotSize,
1569
+ dotSize,
1570
+ (xOffset, yOffset) => {
1571
+ var _a2;
1572
+ return !!((_a2 = squareMask2[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
1573
+ }
1574
+ );
1575
+ if (dot._element && cornersSquareClipPath) {
1576
+ cornersSquareClipPath.appendChild(dot._element);
1577
+ }
1578
+ }
1579
+ }
1580
+ }
1581
+ if (((_g = options.cornersDotOptions) == null ? void 0 : _g.gradient) || ((_h = options.cornersDotOptions) == null ? void 0 : _h.color)) {
1582
+ cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
1583
+ cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
1584
+ this._defs.appendChild(cornersDotClipPath);
1585
+ this._cornersDotClipPath = cornersDotClipPath;
1586
+ this._createColor({
1587
+ options: (_i = options.cornersDotOptions) == null ? void 0 : _i.gradient,
1588
+ color: (_j = options.cornersDotOptions) == null ? void 0 : _j.color,
1589
+ additionalRotation: rotation,
1590
+ x: x + dotSize * 2,
1591
+ y: y + dotSize * 2,
1592
+ height: cornersDotSize,
1593
+ width: cornersDotSize,
1594
+ name: `corners-dot-color-${column}-${row}`
1595
+ });
1596
+ }
1597
+ if ((_k = options.cornersDotOptions) == null ? void 0 : _k.type) {
1598
+ const cornersDot = new QRCornerDot2({
1599
+ svg: this._element,
1600
+ type: options.cornersDotOptions.type,
1601
+ window: this._window
1602
+ });
1603
+ cornersDot.draw(x + dotSize * 2, y + dotSize * 2, cornersDotSize, rotation);
1604
+ if (cornersDot._element && cornersDotClipPath) {
1605
+ cornersDotClipPath.appendChild(cornersDot._element);
1606
+ }
1607
+ } else {
1608
+ const dot = new QRDot2({
1609
+ svg: this._element,
1610
+ type: options.dotsOptions.type,
1611
+ window: this._window
1612
+ });
1613
+ for (let i = 0; i < dotMask2.length; i++) {
1614
+ for (let j = 0; j < dotMask2[i].length; j++) {
1615
+ if (!((_l = dotMask2[i]) == null ? void 0 : _l[j])) {
1616
+ continue;
1617
+ }
1618
+ dot.draw(
1619
+ x + i * dotSize,
1620
+ y + j * dotSize,
1621
+ dotSize,
1622
+ (xOffset, yOffset) => {
1623
+ var _a2;
1624
+ return !!((_a2 = dotMask2[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
1625
+ }
1626
+ );
1627
+ if (dot._element && cornersDotClipPath) {
1628
+ cornersDotClipPath.appendChild(dot._element);
1629
+ }
1630
+ }
1631
+ }
1632
+ }
1633
+ });
1634
+ }
1635
+ imageToBlob() {
1636
+ var _a;
1637
+ if (!this._image) return;
1638
+ if (/(\.svg$)|(^data:image\/svg)/.test((_a = this._options.image) != null ? _a : "")) {
1639
+ this._image.width = this._options.width;
1640
+ this._image.height = this._options.height;
1641
+ }
1642
+ if (this._options.imageOptions.saveAsBlob && this._canvas) {
1643
+ const ctx = this._canvas.getContext("2d");
1644
+ if (ctx) {
1645
+ ctx.drawImage(this._image, 0, 0, this._canvas.width, this._canvas.height);
1646
+ this._imageUri = this._canvas.toDataURL("image/png");
1647
+ console.log(this._imageUri);
1648
+ }
1649
+ }
1650
+ }
1651
+ loadImage() {
1652
+ return new Promise((resolve, reject) => {
1653
+ var _a;
1654
+ const options = this._options;
1655
+ if (((_a = options.nodeCanvas) == null ? void 0 : _a.loadImage) && options.image) {
1656
+ options.nodeCanvas.loadImage(options.image).then((image2) => {
1657
+ this._image = image2;
1658
+ resolve();
1659
+ }).catch(reject);
1660
+ return;
1661
+ }
1662
+ const image = new this._window.Image();
1663
+ if (!options.image) {
1664
+ return reject("Image is not defined");
1665
+ }
1666
+ if (typeof options.imageOptions.crossOrigin === "string") {
1667
+ image.crossOrigin = options.imageOptions.crossOrigin;
1668
+ }
1669
+ this._image = image;
1670
+ image.onload = () => {
1671
+ resolve();
1672
+ };
1673
+ image.src = options.image;
1674
+ });
1675
+ }
1676
+ drawImage({
1677
+ width,
1678
+ height,
1679
+ count,
1680
+ dotSize
1681
+ }) {
1682
+ const options = this._options;
1683
+ const xBeginning = Math.floor((options.width - count * dotSize) / 2);
1684
+ const yBeginning = Math.floor((options.height - count * dotSize) / 2);
1685
+ const dx = xBeginning + options.imageOptions.margin + (count * dotSize - width) / 2;
1686
+ const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
1687
+ const dw = width - options.imageOptions.margin * 2;
1688
+ const dh = height - options.imageOptions.margin * 2;
1689
+ const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
1690
+ image.setAttribute("href", this._imageUri || "");
1691
+ image.setAttribute("x", String(dx));
1692
+ image.setAttribute("y", String(dy));
1693
+ image.setAttribute("width", `${dw}px`);
1694
+ image.setAttribute("height", `${dh}px`);
1695
+ this._element.appendChild(image);
1696
+ }
1697
+ _createColor({
1698
+ options,
1699
+ color,
1700
+ additionalRotation,
1701
+ x,
1702
+ y,
1703
+ height,
1704
+ width,
1705
+ name
1706
+ }) {
1707
+ const size = width > height ? width : height;
1708
+ const rect = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
1709
+ rect.setAttribute("x", String(x));
1710
+ rect.setAttribute("y", String(y));
1711
+ rect.setAttribute("height", String(height));
1712
+ rect.setAttribute("width", String(width));
1713
+ rect.setAttribute("clip-path", `url('#clip-path-${name}')`);
1714
+ if (options) {
1715
+ let gradient;
1716
+ if (options.type === gradientTypes_default.radial) {
1717
+ gradient = this._window.document.createElementNS("http://www.w3.org/2000/svg", "radialGradient");
1718
+ gradient.setAttribute("id", name);
1719
+ gradient.setAttribute("gradientUnits", "userSpaceOnUse");
1720
+ gradient.setAttribute("fx", String(x + width / 2));
1721
+ gradient.setAttribute("fy", String(y + height / 2));
1722
+ gradient.setAttribute("cx", String(x + width / 2));
1723
+ gradient.setAttribute("cy", String(y + height / 2));
1724
+ gradient.setAttribute("r", String(size / 2));
1725
+ } else {
1726
+ const rotation = ((options.rotation || 0) + additionalRotation) % (2 * Math.PI);
1727
+ const positiveRotation = (rotation + 2 * Math.PI) % (2 * Math.PI);
1728
+ let x0 = x + width / 2;
1729
+ let y0 = y + height / 2;
1730
+ let x1 = x + width / 2;
1731
+ let y1 = y + height / 2;
1732
+ if (positiveRotation >= 0 && positiveRotation <= 0.25 * Math.PI || positiveRotation > 1.75 * Math.PI && positiveRotation <= 2 * Math.PI) {
1733
+ x0 = x0 - width / 2;
1734
+ y0 = y0 - height / 2 * Math.tan(rotation);
1735
+ x1 = x1 + width / 2;
1736
+ y1 = y1 + height / 2 * Math.tan(rotation);
1737
+ } else if (positiveRotation > 0.25 * Math.PI && positiveRotation <= 0.75 * Math.PI) {
1738
+ y0 = y0 - height / 2;
1739
+ x0 = x0 - width / 2 / Math.tan(rotation);
1740
+ y1 = y1 + height / 2;
1741
+ x1 = x1 + width / 2 / Math.tan(rotation);
1742
+ } else if (positiveRotation > 0.75 * Math.PI && positiveRotation <= 1.25 * Math.PI) {
1743
+ x0 = x0 + width / 2;
1744
+ y0 = y0 + height / 2 * Math.tan(rotation);
1745
+ x1 = x1 - width / 2;
1746
+ y1 = y1 - height / 2 * Math.tan(rotation);
1747
+ } else if (positiveRotation > 1.25 * Math.PI && positiveRotation <= 1.75 * Math.PI) {
1748
+ y0 = y0 + height / 2;
1749
+ x0 = x0 + width / 2 / Math.tan(rotation);
1750
+ y1 = y1 - height / 2;
1751
+ x1 = x1 - width / 2 / Math.tan(rotation);
1752
+ }
1753
+ gradient = this._window.document.createElementNS("http://www.w3.org/2000/svg", "linearGradient");
1754
+ gradient.setAttribute("id", name);
1755
+ gradient.setAttribute("gradientUnits", "userSpaceOnUse");
1756
+ gradient.setAttribute("x1", String(Math.round(x0)));
1757
+ gradient.setAttribute("y1", String(Math.round(y0)));
1758
+ gradient.setAttribute("x2", String(Math.round(x1)));
1759
+ gradient.setAttribute("y2", String(Math.round(y1)));
1760
+ }
1761
+ options.colorStops.forEach(({ offset, color: color2 }) => {
1762
+ const stop = this._window.document.createElementNS("http://www.w3.org/2000/svg", "stop");
1763
+ stop.setAttribute("offset", `${100 * offset}%`);
1764
+ stop.setAttribute("stop-color", color2);
1765
+ gradient.appendChild(stop);
1766
+ });
1767
+ rect.setAttribute("fill", `url('#${name}')`);
1768
+ this._defs.appendChild(gradient);
1769
+ } else if (color) {
1770
+ rect.setAttribute("fill", color);
1771
+ }
1772
+ this._element.appendChild(rect);
1773
+ }
1774
+ };
1775
+
1776
+ // src/constants/drawTypes.ts
1777
+ var drawTypes_default = {
1778
+ canvas: "canvas",
1779
+ svg: "svg"
1780
+ };
1781
+
1782
+ // src/constants/qrTypes.ts
1783
+ var qrTypes = {};
1784
+ for (let type = 0; type <= 40; type++) {
1785
+ qrTypes[type] = type;
1786
+ }
1787
+ var qrTypes_default = qrTypes;
1788
+
1789
+ // src/constants/errorCorrectionLevels.ts
1790
+ var errorCorrectionLevels_default = {
1791
+ L: "L",
1792
+ M: "M",
1793
+ Q: "Q",
1794
+ H: "H"
1795
+ };
1796
+
1797
+ // src/core/QROptions.ts
1798
+ var defaultOptions = {
1799
+ type: drawTypes_default.canvas,
1800
+ width: 300,
1801
+ height: 300,
1802
+ data: "",
1803
+ margin: 0,
1804
+ qrOptions: {
1805
+ typeNumber: qrTypes_default[0],
1806
+ mode: void 0,
1807
+ errorCorrectionLevel: errorCorrectionLevels_default.Q
1808
+ },
1809
+ imageOptions: {
1810
+ saveAsBlob: false,
1811
+ hideBackgroundDots: true,
1812
+ imageSize: 0.4,
1813
+ crossOrigin: void 0,
1814
+ margin: 0
1815
+ },
1816
+ dotsOptions: {
1817
+ type: "square",
1818
+ color: "#000"
1819
+ },
1820
+ backgroundOptions: {
1821
+ color: "#fff"
1822
+ }
1823
+ };
1824
+ var QROptions_default = defaultOptions;
1825
+
1826
+ // src/tools/sanitizeOptions.ts
1827
+ function sanitizeGradient(gradient) {
1828
+ const newGradient = { ...gradient };
1829
+ if (!newGradient.colorStops || !newGradient.colorStops.length) {
1830
+ throw "Field 'colorStops' is required in gradient";
1831
+ }
1832
+ if (newGradient.rotation) {
1833
+ newGradient.rotation = Number(newGradient.rotation);
1834
+ } else {
1835
+ newGradient.rotation = 0;
1836
+ }
1837
+ newGradient.colorStops = newGradient.colorStops.map((colorStop) => ({
1838
+ ...colorStop,
1839
+ offset: Number(colorStop.offset)
1840
+ }));
1841
+ return newGradient;
1842
+ }
1843
+ function sanitizeOptions(options) {
1844
+ const newOptions = { ...options };
1845
+ newOptions.width = Number(newOptions.width);
1846
+ newOptions.height = Number(newOptions.height);
1847
+ newOptions.margin = Number(newOptions.margin);
1848
+ newOptions.imageOptions = {
1849
+ ...newOptions.imageOptions,
1850
+ hideBackgroundDots: Boolean(newOptions.imageOptions.hideBackgroundDots),
1851
+ imageSize: Number(newOptions.imageOptions.imageSize),
1852
+ margin: Number(newOptions.imageOptions.margin)
1853
+ };
1854
+ if (newOptions.margin > Math.min(newOptions.width, newOptions.height)) {
1855
+ newOptions.margin = Math.min(newOptions.width, newOptions.height);
1856
+ }
1857
+ newOptions.dotsOptions = {
1858
+ ...newOptions.dotsOptions
1859
+ };
1860
+ if (newOptions.dotsOptions.gradient) {
1861
+ newOptions.dotsOptions.gradient = sanitizeGradient(newOptions.dotsOptions.gradient);
1862
+ }
1863
+ if (newOptions.cornersSquareOptions) {
1864
+ newOptions.cornersSquareOptions = {
1865
+ ...newOptions.cornersSquareOptions
1866
+ };
1867
+ if (newOptions.cornersSquareOptions.gradient) {
1868
+ newOptions.cornersSquareOptions.gradient = sanitizeGradient(newOptions.cornersSquareOptions.gradient);
1869
+ }
1870
+ }
1871
+ if (newOptions.cornersDotOptions) {
1872
+ newOptions.cornersDotOptions = {
1873
+ ...newOptions.cornersDotOptions
1874
+ };
1875
+ if (newOptions.cornersDotOptions.gradient) {
1876
+ newOptions.cornersDotOptions.gradient = sanitizeGradient(newOptions.cornersDotOptions.gradient);
1877
+ }
1878
+ }
1879
+ if (newOptions.backgroundOptions) {
1880
+ newOptions.backgroundOptions = {
1881
+ ...newOptions.backgroundOptions
1882
+ };
1883
+ if (newOptions.backgroundOptions.gradient) {
1884
+ newOptions.backgroundOptions.gradient = sanitizeGradient(newOptions.backgroundOptions.gradient);
1885
+ }
1886
+ }
1887
+ return newOptions;
1888
+ }
1889
+
1890
+ // src/core/QRCodeStyling.ts
1891
+ import qrcode from "qrcode-generator";
1892
+ var QRCodeStyling = class _QRCodeStyling {
1893
+ constructor(options) {
1894
+ this._options = options ? sanitizeOptions(mergeDeep(QROptions_default, options)) : QROptions_default;
1895
+ this.update();
1896
+ }
1897
+ static _clearContainer(container) {
1898
+ if (container) {
1899
+ container.innerHTML = "";
1900
+ }
1901
+ }
1902
+ async _getQRStylingElement(extension = "png") {
1903
+ if (!this._qr) throw "QR code is empty";
1904
+ if (extension.toLowerCase() === "svg") {
1905
+ let promise, svg;
1906
+ if (this._svg && this._svgDrawingPromise) {
1907
+ svg = this._svg;
1908
+ promise = this._svgDrawingPromise;
1909
+ } else {
1910
+ svg = new QRSVG(this._options);
1911
+ promise = svg.drawQR(this._qr);
1912
+ }
1913
+ await promise;
1914
+ return svg;
1915
+ } else {
1916
+ let promise, canvas;
1917
+ if (this._canvas && this._canvasDrawingPromise) {
1918
+ canvas = this._canvas;
1919
+ promise = this._canvasDrawingPromise;
1920
+ } else {
1921
+ canvas = new QRCanvas(this._options);
1922
+ promise = canvas.drawQR(this._qr);
1923
+ }
1924
+ await promise;
1925
+ return canvas;
1926
+ }
1927
+ }
1928
+ update(options) {
1929
+ _QRCodeStyling._clearContainer(this._container);
1930
+ this._options = options ? sanitizeOptions(mergeDeep(this._options, options)) : this._options;
1931
+ if (!this._options.data) {
1932
+ return;
1933
+ }
1934
+ this._qr = qrcode(this._options.qrOptions.typeNumber, this._options.qrOptions.errorCorrectionLevel);
1935
+ this._qr.addData(this._options.data, this._options.qrOptions.mode || getMode(this._options.data));
1936
+ this._qr.make();
1937
+ if (this._options.type === drawTypes_default.canvas) {
1938
+ this._canvas = new QRCanvas(this._options);
1939
+ this._canvasDrawingPromise = this._canvas.drawQR(this._qr);
1940
+ this._svgDrawingPromise = void 0;
1941
+ this._svg = void 0;
1942
+ } else {
1943
+ this._svg = new QRSVG(this._options);
1944
+ this._svgDrawingPromise = this._svg.drawQR(this._qr);
1945
+ this._canvasDrawingPromise = void 0;
1946
+ this._canvas = void 0;
1947
+ }
1948
+ this.append(this._container);
1949
+ }
1950
+ append(container) {
1951
+ if (!container) {
1952
+ return;
1953
+ }
1954
+ if (typeof container.appendChild !== "function") {
1955
+ throw "Container should be a single DOM node";
1956
+ }
1957
+ if (this._options.type === drawTypes_default.canvas) {
1958
+ if (this._canvas) {
1959
+ container.appendChild(this._canvas.getCanvas());
1960
+ }
1961
+ } else {
1962
+ if (this._svg) {
1963
+ container.appendChild(this._svg.getElement());
1964
+ }
1965
+ }
1966
+ this._container = container;
1967
+ }
1968
+ async getRawData(extension = "png") {
1969
+ if (!this._qr) throw "QR code is empty";
1970
+ const element = await this._getQRStylingElement(extension);
1971
+ if (extension.toLowerCase() === "svg") {
1972
+ const serializer = new element._window.XMLSerializer();
1973
+ const source = serializer.serializeToString(element.getElement());
1974
+ const svgString = `<?xml version="1.0" standalone="no"?>\r
1975
+ ${source}`;
1976
+ if (typeof Blob !== "undefined" && !this._options.jsdom) {
1977
+ return new Blob([svgString], { type: "image/svg+xml" });
1978
+ } else {
1979
+ return Buffer.from(svgString);
1980
+ }
1981
+ } else {
1982
+ return new Promise((resolve) => {
1983
+ const canvas = element.getCanvas();
1984
+ if (canvas.toBuffer) {
1985
+ resolve(canvas.toBuffer(`image/${extension}`));
1986
+ } else {
1987
+ canvas.toBlob(resolve, `image/${extension}`, 1);
1988
+ }
1989
+ });
1990
+ }
1991
+ }
1992
+ async download(downloadOptions) {
1993
+ if (!this._qr) throw "QR code is empty";
1994
+ if (typeof Blob === "undefined") throw "Cannot download in Node.js, call getRawData instead.";
1995
+ let extension = "png";
1996
+ let name = "qr";
1997
+ if (typeof downloadOptions === "string") {
1998
+ extension = downloadOptions;
1999
+ console.warn(
2000
+ "Extension is deprecated as argument for 'download' method, please pass object { name: '...', extension: '...' } as argument"
2001
+ );
2002
+ } else if (typeof downloadOptions === "object" && downloadOptions !== null) {
2003
+ if (downloadOptions.name) {
2004
+ name = downloadOptions.name;
2005
+ }
2006
+ if (downloadOptions.extension) {
2007
+ extension = downloadOptions.extension;
2008
+ }
2009
+ }
2010
+ const element = await this._getQRStylingElement(extension);
2011
+ if (extension.toLowerCase() === "svg") {
2012
+ const serializer = new XMLSerializer();
2013
+ let source = serializer.serializeToString(element.getElement());
2014
+ source = '<?xml version="1.0" standalone="no"?>\r\n' + source;
2015
+ const url = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source);
2016
+ downloadURI(url, `${name}.svg`);
2017
+ } else {
2018
+ const url = element.getCanvas().toDataURL(`image/${extension}`);
2019
+ downloadURI(url, `${name}.${extension}`);
2020
+ }
2021
+ }
2022
+ };
2023
+
2024
+ // src/types/index.ts
2025
+ var JSDom = class {
2026
+ constructor(input, options) {
2027
+ this._options = options;
2028
+ this._input = input;
2029
+ this.window = window;
2030
+ }
2031
+ };
2032
+
2033
+ // src/index.ts
2034
+ var index_default = QRCodeStyling;
2035
+ export {
2036
+ JSDom,
2037
+ cornerDotTypes_default as cornerDotTypes,
2038
+ cornerSquareTypes_default as cornerSquareTypes,
2039
+ index_default as default,
2040
+ dotTypes_default as dotTypes,
2041
+ drawTypes_default as drawTypes,
2042
+ errorCorrectionLevels_default as errorCorrectionLevels,
2043
+ errorCorrectionPercents_default as errorCorrectionPercents,
2044
+ modes_default as modes,
2045
+ qrTypes_default as qrTypes
2046
+ };
2047
+ //# sourceMappingURL=qr-code-styling.mjs.map