qr-code-styling-node 1.5.0 → 1.7.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/LICENSE +1 -0
- package/README.md +70 -24
- package/lib/core/QRCodeStyling.d.ts +14 -1
- package/lib/core/QRSVG.d.ts +3 -0
- package/lib/qr-code-styling.common.js +2 -1
- package/lib/qr-code-styling.common.js.map +1 -0
- package/lib/qr-code-styling.js +2 -1
- package/lib/qr-code-styling.js.map +1 -0
- package/lib/qr-code-styling.mjs +2364 -0
- package/lib/qr-code-styling.mjs.map +7 -0
- package/lib/tools/imageSize.d.ts +17 -0
- package/lib/tools/svgDom.d.ts +37 -0
- package/lib/types/index.d.ts +40 -21
- package/package.json +81 -49
- package/lib/core/QRCornerDot.d.ts +0 -38
- package/lib/core/QRCornerSquare.d.ts +0 -40
- package/lib/core/QRDot.d.ts +0 -48
|
@@ -0,0 +1,2364 @@
|
|
|
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/tools/svgDom.ts
|
|
1317
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
1318
|
+
var escapeAttribute = (value) => value.replace(/[&<>"\t\n\r]/g, (char) => {
|
|
1319
|
+
switch (char) {
|
|
1320
|
+
case "&":
|
|
1321
|
+
return "&";
|
|
1322
|
+
case "<":
|
|
1323
|
+
return "<";
|
|
1324
|
+
case ">":
|
|
1325
|
+
return ">";
|
|
1326
|
+
case '"':
|
|
1327
|
+
return """;
|
|
1328
|
+
case " ":
|
|
1329
|
+
return "	";
|
|
1330
|
+
case "\n":
|
|
1331
|
+
return "
";
|
|
1332
|
+
default:
|
|
1333
|
+
return "
";
|
|
1334
|
+
}
|
|
1335
|
+
});
|
|
1336
|
+
var VirtualSVGElement = class _VirtualSVGElement {
|
|
1337
|
+
constructor(tagName) {
|
|
1338
|
+
this.namespaceURI = SVG_NS;
|
|
1339
|
+
this.parentNode = null;
|
|
1340
|
+
this.childNodes = [];
|
|
1341
|
+
this._attributes = /* @__PURE__ */ new Map();
|
|
1342
|
+
this.tagName = tagName;
|
|
1343
|
+
}
|
|
1344
|
+
get nodeName() {
|
|
1345
|
+
return this.tagName;
|
|
1346
|
+
}
|
|
1347
|
+
setAttribute(name, value) {
|
|
1348
|
+
this._attributes.set(name, String(value));
|
|
1349
|
+
}
|
|
1350
|
+
getAttribute(name) {
|
|
1351
|
+
const value = this._attributes.get(name);
|
|
1352
|
+
return value === void 0 ? null : value;
|
|
1353
|
+
}
|
|
1354
|
+
removeAttribute(name) {
|
|
1355
|
+
this._attributes.delete(name);
|
|
1356
|
+
}
|
|
1357
|
+
appendChild(child) {
|
|
1358
|
+
if (child.parentNode) child.parentNode.removeChild(child);
|
|
1359
|
+
child.parentNode = this;
|
|
1360
|
+
this.childNodes.push(child);
|
|
1361
|
+
return child;
|
|
1362
|
+
}
|
|
1363
|
+
removeChild(child) {
|
|
1364
|
+
const index = this.childNodes.indexOf(child);
|
|
1365
|
+
if (index !== -1) this.childNodes.splice(index, 1);
|
|
1366
|
+
child.parentNode = null;
|
|
1367
|
+
return child;
|
|
1368
|
+
}
|
|
1369
|
+
replaceChild(newChild, oldChild) {
|
|
1370
|
+
const index = this.childNodes.indexOf(oldChild);
|
|
1371
|
+
if (index !== -1) {
|
|
1372
|
+
if (newChild.parentNode) newChild.parentNode.removeChild(newChild);
|
|
1373
|
+
this.childNodes[index] = newChild;
|
|
1374
|
+
newChild.parentNode = this;
|
|
1375
|
+
oldChild.parentNode = null;
|
|
1376
|
+
}
|
|
1377
|
+
return oldChild;
|
|
1378
|
+
}
|
|
1379
|
+
cloneNode(deep = false) {
|
|
1380
|
+
const clone = new _VirtualSVGElement(this.tagName);
|
|
1381
|
+
this._attributes.forEach((value, name) => clone.setAttribute(name, value));
|
|
1382
|
+
if (deep) this.childNodes.forEach((child) => clone.appendChild(child.cloneNode(true)));
|
|
1383
|
+
return clone;
|
|
1384
|
+
}
|
|
1385
|
+
serialize(isRoot = true) {
|
|
1386
|
+
let markup = `<${this.tagName}`;
|
|
1387
|
+
if (isRoot) markup += ` xmlns="${SVG_NS}"`;
|
|
1388
|
+
this._attributes.forEach((value, name) => {
|
|
1389
|
+
markup += ` ${name}="${escapeAttribute(value)}"`;
|
|
1390
|
+
});
|
|
1391
|
+
if (!this.childNodes.length) return `${markup}/>`;
|
|
1392
|
+
markup += ">";
|
|
1393
|
+
for (const child of this.childNodes) markup += child.serialize(false);
|
|
1394
|
+
return `${markup}</${this.tagName}>`;
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
var VirtualXMLSerializer = class {
|
|
1398
|
+
serializeToString(node) {
|
|
1399
|
+
return node.serialize(true);
|
|
1400
|
+
}
|
|
1401
|
+
};
|
|
1402
|
+
var VirtualDocument = class {
|
|
1403
|
+
createElementNS(_namespace, tagName) {
|
|
1404
|
+
return new VirtualSVGElement(tagName);
|
|
1405
|
+
}
|
|
1406
|
+
};
|
|
1407
|
+
var createVirtualWindow = () => ({
|
|
1408
|
+
document: new VirtualDocument(),
|
|
1409
|
+
XMLSerializer: VirtualXMLSerializer,
|
|
1410
|
+
isVirtual: true
|
|
1411
|
+
});
|
|
1412
|
+
var hasBrowserDom = () => typeof window !== "undefined" && typeof document !== "undefined" && typeof document.createElementNS === "function";
|
|
1413
|
+
|
|
1414
|
+
// src/tools/imageSize.ts
|
|
1415
|
+
var decodeBase64 = (base64) => {
|
|
1416
|
+
if (typeof atob === "function") {
|
|
1417
|
+
const binary = atob(base64);
|
|
1418
|
+
const bytes = new Uint8Array(binary.length);
|
|
1419
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
1420
|
+
return bytes;
|
|
1421
|
+
}
|
|
1422
|
+
return new Uint8Array(Buffer.from(base64, "base64"));
|
|
1423
|
+
};
|
|
1424
|
+
var encodeBase64 = (bytes) => {
|
|
1425
|
+
if (typeof btoa === "function") {
|
|
1426
|
+
let binary = "";
|
|
1427
|
+
const chunk = 32768;
|
|
1428
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
1429
|
+
binary += String.fromCharCode.apply(null, Array.from(bytes.subarray(i, i + chunk)));
|
|
1430
|
+
}
|
|
1431
|
+
return btoa(binary);
|
|
1432
|
+
}
|
|
1433
|
+
return Buffer.from(bytes).toString("base64");
|
|
1434
|
+
};
|
|
1435
|
+
var sniffMimeType = (bytes) => {
|
|
1436
|
+
if (bytes[0] === 137 && bytes[1] === 80 && bytes[2] === 78 && bytes[3] === 71) return "image/png";
|
|
1437
|
+
if (bytes[0] === 255 && bytes[1] === 216) return "image/jpeg";
|
|
1438
|
+
if (bytes[0] === 71 && bytes[1] === 73 && bytes[2] === 70) return "image/gif";
|
|
1439
|
+
if (ascii(bytes, 0, 4) === "RIFF" && ascii(bytes, 8, 12) === "WEBP") return "image/webp";
|
|
1440
|
+
const head = ascii(bytes, 0, Math.min(bytes.length, 1024)).toLowerCase();
|
|
1441
|
+
if (head.includes("<svg")) return "image/svg+xml";
|
|
1442
|
+
return "application/octet-stream";
|
|
1443
|
+
};
|
|
1444
|
+
var loadImageData = async (source) => {
|
|
1445
|
+
var _a;
|
|
1446
|
+
const dataUrl = /^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,([\s\S]*)$/.exec(source);
|
|
1447
|
+
if (dataUrl) {
|
|
1448
|
+
const [, mimeType, , base64, payload] = dataUrl;
|
|
1449
|
+
const bytes2 = base64 ? decodeBase64(payload) : new TextEncoder().encode(decodeURIComponent(payload));
|
|
1450
|
+
return { bytes: bytes2, mimeType: mimeType || sniffMimeType(bytes2) };
|
|
1451
|
+
}
|
|
1452
|
+
if (typeof fetch !== "function") {
|
|
1453
|
+
throw new Error("Loading an image URL needs a global fetch (Node.js 18+), or pass the image as a data URL.");
|
|
1454
|
+
}
|
|
1455
|
+
const response = await fetch(source);
|
|
1456
|
+
if (!response.ok) throw new Error(`Failed to load image ${source}: HTTP ${response.status}`);
|
|
1457
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
1458
|
+
const header = (_a = response.headers.get("content-type")) == null ? void 0 : _a.split(";")[0].trim();
|
|
1459
|
+
return { bytes, mimeType: header && header.startsWith("image/") ? header : sniffMimeType(bytes) };
|
|
1460
|
+
};
|
|
1461
|
+
var toDataUrl = ({ bytes, mimeType }) => `data:${mimeType};base64,${encodeBase64(bytes)}`;
|
|
1462
|
+
function ascii(bytes, start, end) {
|
|
1463
|
+
let result = "";
|
|
1464
|
+
for (let i = start; i < end && i < bytes.length; i++) result += String.fromCharCode(bytes[i]);
|
|
1465
|
+
return result;
|
|
1466
|
+
}
|
|
1467
|
+
var uint16BE = (b, i) => b[i] << 8 | b[i + 1];
|
|
1468
|
+
var uint16LE = (b, i) => b[i] | b[i + 1] << 8;
|
|
1469
|
+
var uint24LE = (b, i) => b[i] | b[i + 1] << 8 | b[i + 2] << 16;
|
|
1470
|
+
var uint32BE = (b, i) => (b[i] << 24 | b[i + 1] << 16 | b[i + 2] << 8 | b[i + 3]) >>> 0;
|
|
1471
|
+
var svgLength = (value) => {
|
|
1472
|
+
if (!value) return void 0;
|
|
1473
|
+
const match = /^\s*([\d.]+)\s*(px)?\s*$/.exec(value);
|
|
1474
|
+
return match ? parseFloat(match[1]) : void 0;
|
|
1475
|
+
};
|
|
1476
|
+
var svgSize = (bytes) => {
|
|
1477
|
+
var _a, _b;
|
|
1478
|
+
const text = new TextDecoder().decode(bytes);
|
|
1479
|
+
const tag = (_a = /<svg\b[^>]*>/i.exec(text)) == null ? void 0 : _a[0];
|
|
1480
|
+
if (!tag) return null;
|
|
1481
|
+
const attr = (name) => {
|
|
1482
|
+
var _a2;
|
|
1483
|
+
return (_a2 = new RegExp(`\\s${name}\\s*=\\s*(["'])([^"']*)\\1`, "i").exec(tag)) == null ? void 0 : _a2[2];
|
|
1484
|
+
};
|
|
1485
|
+
let width = svgLength(attr("width"));
|
|
1486
|
+
let height = svgLength(attr("height"));
|
|
1487
|
+
const viewBox = (_b = attr("viewBox")) == null ? void 0 : _b.trim().split(/[\s,]+/).map(Number);
|
|
1488
|
+
if (viewBox && viewBox.length === 4 && viewBox[2] > 0 && viewBox[3] > 0) {
|
|
1489
|
+
const ratio = viewBox[2] / viewBox[3];
|
|
1490
|
+
if (width === void 0 && height === void 0) {
|
|
1491
|
+
width = viewBox[2];
|
|
1492
|
+
height = viewBox[3];
|
|
1493
|
+
} else if (width === void 0 && height !== void 0) {
|
|
1494
|
+
width = height * ratio;
|
|
1495
|
+
} else if (height === void 0 && width !== void 0) {
|
|
1496
|
+
height = width / ratio;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
return { width: width != null ? width : 300, height: height != null ? height : 150 };
|
|
1500
|
+
};
|
|
1501
|
+
var jpegSize = (b) => {
|
|
1502
|
+
let i = 2;
|
|
1503
|
+
while (i + 9 < b.length) {
|
|
1504
|
+
if (b[i] !== 255) {
|
|
1505
|
+
i++;
|
|
1506
|
+
continue;
|
|
1507
|
+
}
|
|
1508
|
+
const marker = b[i + 1];
|
|
1509
|
+
if (marker === 216 || marker === 1 || marker >= 208 && marker <= 215) {
|
|
1510
|
+
i += 2;
|
|
1511
|
+
continue;
|
|
1512
|
+
}
|
|
1513
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
1514
|
+
return { height: uint16BE(b, i + 5), width: uint16BE(b, i + 7) };
|
|
1515
|
+
}
|
|
1516
|
+
i += 2 + uint16BE(b, i + 2);
|
|
1517
|
+
}
|
|
1518
|
+
return null;
|
|
1519
|
+
};
|
|
1520
|
+
var webpSize = (b) => {
|
|
1521
|
+
const chunk = ascii(b, 12, 16);
|
|
1522
|
+
if (chunk === "VP8 ") return { width: uint16LE(b, 26) & 16383, height: uint16LE(b, 28) & 16383 };
|
|
1523
|
+
if (chunk === "VP8L") {
|
|
1524
|
+
const bits = b[21] | b[22] << 8 | b[23] << 16 | b[24] << 24;
|
|
1525
|
+
return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
|
|
1526
|
+
}
|
|
1527
|
+
if (chunk === "VP8X") return { width: uint24LE(b, 24) + 1, height: uint24LE(b, 27) + 1 };
|
|
1528
|
+
return null;
|
|
1529
|
+
};
|
|
1530
|
+
var getImageSize = ({ bytes, mimeType }) => {
|
|
1531
|
+
const type = mimeType === "application/octet-stream" ? sniffMimeType(bytes) : mimeType;
|
|
1532
|
+
let size = null;
|
|
1533
|
+
if (type === "image/png") size = { width: uint32BE(bytes, 16), height: uint32BE(bytes, 20) };
|
|
1534
|
+
else if (type === "image/gif") size = { width: uint16LE(bytes, 6), height: uint16LE(bytes, 8) };
|
|
1535
|
+
else if (type === "image/jpeg" || type === "image/jpg") size = jpegSize(bytes);
|
|
1536
|
+
else if (type === "image/webp") size = webpSize(bytes);
|
|
1537
|
+
else if (type === "image/svg+xml") size = svgSize(bytes);
|
|
1538
|
+
if (!size || !size.width || !size.height) {
|
|
1539
|
+
throw new Error(`Cannot read the size of the image (${type}). Supported: PNG, JPEG, GIF, WebP, SVG.`);
|
|
1540
|
+
}
|
|
1541
|
+
return size;
|
|
1542
|
+
};
|
|
1543
|
+
|
|
1544
|
+
// src/core/QRSVG.ts
|
|
1545
|
+
var squareMask2 = [
|
|
1546
|
+
[1, 1, 1, 1, 1, 1, 1],
|
|
1547
|
+
[1, 0, 0, 0, 0, 0, 1],
|
|
1548
|
+
[1, 0, 0, 0, 0, 0, 1],
|
|
1549
|
+
[1, 0, 0, 0, 0, 0, 1],
|
|
1550
|
+
[1, 0, 0, 0, 0, 0, 1],
|
|
1551
|
+
[1, 0, 0, 0, 0, 0, 1],
|
|
1552
|
+
[1, 1, 1, 1, 1, 1, 1]
|
|
1553
|
+
];
|
|
1554
|
+
var dotMask2 = [
|
|
1555
|
+
[0, 0, 0, 0, 0, 0, 0],
|
|
1556
|
+
[0, 0, 0, 0, 0, 0, 0],
|
|
1557
|
+
[0, 0, 1, 1, 1, 0, 0],
|
|
1558
|
+
[0, 0, 1, 1, 1, 0, 0],
|
|
1559
|
+
[0, 0, 1, 1, 1, 0, 0],
|
|
1560
|
+
[0, 0, 0, 0, 0, 0, 0],
|
|
1561
|
+
[0, 0, 0, 0, 0, 0, 0]
|
|
1562
|
+
];
|
|
1563
|
+
var QRSVG = class {
|
|
1564
|
+
//TODO don't pass all options to this class
|
|
1565
|
+
constructor(options) {
|
|
1566
|
+
/** True when no browser DOM or jsdom is available and the built-in virtual DOM is used. */
|
|
1567
|
+
this._virtual = false;
|
|
1568
|
+
var _a;
|
|
1569
|
+
if (options.jsdom) {
|
|
1570
|
+
this._window = new options.jsdom("", { resources: "usable" }).window;
|
|
1571
|
+
} else if (hasBrowserDom()) {
|
|
1572
|
+
this._window = window;
|
|
1573
|
+
} else {
|
|
1574
|
+
this._window = createVirtualWindow();
|
|
1575
|
+
this._virtual = true;
|
|
1576
|
+
}
|
|
1577
|
+
this._element = this._window.document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
1578
|
+
this._element.setAttribute("width", String(options.width));
|
|
1579
|
+
this._element.setAttribute("height", String(options.height));
|
|
1580
|
+
this._defs = this._window.document.createElementNS("http://www.w3.org/2000/svg", "defs");
|
|
1581
|
+
this._element.appendChild(this._defs);
|
|
1582
|
+
if (options.imageOptions.saveAsBlob && !this._virtual) {
|
|
1583
|
+
if ((_a = options.nodeCanvas) == null ? void 0 : _a.createCanvas) {
|
|
1584
|
+
this._canvas = options.nodeCanvas.createCanvas(options.width, options.height);
|
|
1585
|
+
} else {
|
|
1586
|
+
this._canvas = document.createElement("canvas");
|
|
1587
|
+
}
|
|
1588
|
+
this._canvas.width = options.width;
|
|
1589
|
+
this._canvas.height = options.height;
|
|
1590
|
+
}
|
|
1591
|
+
this._imageUri = options.image;
|
|
1592
|
+
this._options = options;
|
|
1593
|
+
}
|
|
1594
|
+
get width() {
|
|
1595
|
+
return this._options.width;
|
|
1596
|
+
}
|
|
1597
|
+
get height() {
|
|
1598
|
+
return this._options.height;
|
|
1599
|
+
}
|
|
1600
|
+
getElement() {
|
|
1601
|
+
return this._element;
|
|
1602
|
+
}
|
|
1603
|
+
clear() {
|
|
1604
|
+
var _a;
|
|
1605
|
+
const oldElement = this._element;
|
|
1606
|
+
this._element = oldElement.cloneNode(false);
|
|
1607
|
+
(_a = oldElement == null ? void 0 : oldElement.parentNode) == null ? void 0 : _a.replaceChild(this._element, oldElement);
|
|
1608
|
+
this._defs = this._window.document.createElementNS("http://www.w3.org/2000/svg", "defs");
|
|
1609
|
+
this._element.appendChild(this._defs);
|
|
1610
|
+
}
|
|
1611
|
+
async drawQR(qr) {
|
|
1612
|
+
const count = qr.getModuleCount();
|
|
1613
|
+
const minSize = Math.min(this._options.width, this._options.height) - this._options.margin * 2;
|
|
1614
|
+
const dotSize = Math.floor(minSize / count);
|
|
1615
|
+
let drawImageSize = {
|
|
1616
|
+
hideXDots: 0,
|
|
1617
|
+
hideYDots: 0,
|
|
1618
|
+
width: 0,
|
|
1619
|
+
height: 0
|
|
1620
|
+
};
|
|
1621
|
+
this._qr = qr;
|
|
1622
|
+
if (this._options.image) {
|
|
1623
|
+
await this.loadImage();
|
|
1624
|
+
if (!this._image) return;
|
|
1625
|
+
this.imageToBlob();
|
|
1626
|
+
const { imageOptions, qrOptions } = this._options;
|
|
1627
|
+
const coverLevel = imageOptions.imageSize * errorCorrectionPercents_default[qrOptions.errorCorrectionLevel];
|
|
1628
|
+
const maxHiddenDots = Math.floor(coverLevel * count * count);
|
|
1629
|
+
drawImageSize = calculateImageSize({
|
|
1630
|
+
originalWidth: this._image.width,
|
|
1631
|
+
originalHeight: this._image.height,
|
|
1632
|
+
maxHiddenDots,
|
|
1633
|
+
maxHiddenAxisDots: count - 14,
|
|
1634
|
+
dotSize
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
this.clear();
|
|
1638
|
+
this.drawBackground();
|
|
1639
|
+
this.drawDots((i, j) => {
|
|
1640
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1641
|
+
if (this._options.imageOptions.hideBackgroundDots) {
|
|
1642
|
+
if (i >= (count - drawImageSize.hideXDots) / 2 && i < (count + drawImageSize.hideXDots) / 2 && j >= (count - drawImageSize.hideYDots) / 2 && j < (count + drawImageSize.hideYDots) / 2) {
|
|
1643
|
+
return false;
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
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])) {
|
|
1647
|
+
return false;
|
|
1648
|
+
}
|
|
1649
|
+
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])) {
|
|
1650
|
+
return false;
|
|
1651
|
+
}
|
|
1652
|
+
return true;
|
|
1653
|
+
});
|
|
1654
|
+
this.drawCorners();
|
|
1655
|
+
if (this._options.image) {
|
|
1656
|
+
this.drawImage({ width: drawImageSize.width, height: drawImageSize.height, count, dotSize });
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
drawBackground() {
|
|
1660
|
+
var _a, _b;
|
|
1661
|
+
const element = this._element;
|
|
1662
|
+
const options = this._options;
|
|
1663
|
+
if (element) {
|
|
1664
|
+
const gradientOptions = (_a = options.backgroundOptions) == null ? void 0 : _a.gradient;
|
|
1665
|
+
const color = (_b = options.backgroundOptions) == null ? void 0 : _b.color;
|
|
1666
|
+
if (gradientOptions || color) {
|
|
1667
|
+
this._createColor({
|
|
1668
|
+
options: gradientOptions,
|
|
1669
|
+
color,
|
|
1670
|
+
additionalRotation: 0,
|
|
1671
|
+
x: 0,
|
|
1672
|
+
y: 0,
|
|
1673
|
+
height: options.height,
|
|
1674
|
+
width: options.width,
|
|
1675
|
+
name: "background-color"
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
drawDots(filter) {
|
|
1681
|
+
var _a, _b;
|
|
1682
|
+
if (!this._qr) {
|
|
1683
|
+
throw "QR code is not defined";
|
|
1684
|
+
}
|
|
1685
|
+
const options = this._options;
|
|
1686
|
+
const count = this._qr.getModuleCount();
|
|
1687
|
+
if (count > options.width || count > options.height) {
|
|
1688
|
+
throw "The canvas is too small.";
|
|
1689
|
+
}
|
|
1690
|
+
const minSize = Math.min(options.width, options.height) - options.margin * 2;
|
|
1691
|
+
const dotSize = Math.floor(minSize / count);
|
|
1692
|
+
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1693
|
+
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1694
|
+
const dot = new QRDot2({
|
|
1695
|
+
svg: this._element,
|
|
1696
|
+
type: options.dotsOptions.type,
|
|
1697
|
+
window: this._window
|
|
1698
|
+
});
|
|
1699
|
+
this._dotsClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
1700
|
+
this._dotsClipPath.setAttribute("id", "clip-path-dot-color");
|
|
1701
|
+
this._defs.appendChild(this._dotsClipPath);
|
|
1702
|
+
this._createColor({
|
|
1703
|
+
options: (_a = options.dotsOptions) == null ? void 0 : _a.gradient,
|
|
1704
|
+
color: options.dotsOptions.color,
|
|
1705
|
+
additionalRotation: 0,
|
|
1706
|
+
x: xBeginning,
|
|
1707
|
+
y: yBeginning,
|
|
1708
|
+
height: count * dotSize,
|
|
1709
|
+
width: count * dotSize,
|
|
1710
|
+
name: "dot-color"
|
|
1711
|
+
});
|
|
1712
|
+
for (let i = 0; i < count; i++) {
|
|
1713
|
+
for (let j = 0; j < count; j++) {
|
|
1714
|
+
if (filter && !filter(i, j)) {
|
|
1715
|
+
continue;
|
|
1716
|
+
}
|
|
1717
|
+
if (!((_b = this._qr) == null ? void 0 : _b.isDark(i, j))) {
|
|
1718
|
+
continue;
|
|
1719
|
+
}
|
|
1720
|
+
dot.draw(
|
|
1721
|
+
xBeginning + i * dotSize,
|
|
1722
|
+
yBeginning + j * dotSize,
|
|
1723
|
+
dotSize,
|
|
1724
|
+
(xOffset, yOffset) => {
|
|
1725
|
+
if (i + xOffset < 0 || j + yOffset < 0 || i + xOffset >= count || j + yOffset >= count) return false;
|
|
1726
|
+
if (filter && !filter(i + xOffset, j + yOffset)) return false;
|
|
1727
|
+
return !!this._qr && this._qr.isDark(i + xOffset, j + yOffset);
|
|
1728
|
+
}
|
|
1729
|
+
);
|
|
1730
|
+
if (dot._element && this._dotsClipPath) {
|
|
1731
|
+
this._dotsClipPath.appendChild(dot._element);
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
drawCorners() {
|
|
1737
|
+
if (!this._qr) {
|
|
1738
|
+
throw "QR code is not defined";
|
|
1739
|
+
}
|
|
1740
|
+
const element = this._element;
|
|
1741
|
+
const options = this._options;
|
|
1742
|
+
if (!element) {
|
|
1743
|
+
throw "Element code is not defined";
|
|
1744
|
+
}
|
|
1745
|
+
const count = this._qr.getModuleCount();
|
|
1746
|
+
const minSize = Math.min(options.width, options.height) - options.margin * 2;
|
|
1747
|
+
const dotSize = Math.floor(minSize / count);
|
|
1748
|
+
const cornersSquareSize = dotSize * 7;
|
|
1749
|
+
const cornersDotSize = dotSize * 3;
|
|
1750
|
+
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1751
|
+
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1752
|
+
[
|
|
1753
|
+
[0, 0, 0],
|
|
1754
|
+
[1, 0, Math.PI / 2],
|
|
1755
|
+
[0, 1, -Math.PI / 2]
|
|
1756
|
+
].forEach(([column, row, rotation]) => {
|
|
1757
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
1758
|
+
const x = xBeginning + column * dotSize * (count - 7);
|
|
1759
|
+
const y = yBeginning + row * dotSize * (count - 7);
|
|
1760
|
+
let cornersSquareClipPath = this._dotsClipPath;
|
|
1761
|
+
let cornersDotClipPath = this._dotsClipPath;
|
|
1762
|
+
if (((_a = options.cornersSquareOptions) == null ? void 0 : _a.gradient) || ((_b = options.cornersSquareOptions) == null ? void 0 : _b.color)) {
|
|
1763
|
+
cornersSquareClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
1764
|
+
cornersSquareClipPath.setAttribute("id", `clip-path-corners-square-color-${column}-${row}`);
|
|
1765
|
+
this._defs.appendChild(cornersSquareClipPath);
|
|
1766
|
+
this._cornersSquareClipPath = this._cornersDotClipPath = cornersDotClipPath = cornersSquareClipPath;
|
|
1767
|
+
this._createColor({
|
|
1768
|
+
options: (_c = options.cornersSquareOptions) == null ? void 0 : _c.gradient,
|
|
1769
|
+
color: (_d = options.cornersSquareOptions) == null ? void 0 : _d.color,
|
|
1770
|
+
additionalRotation: rotation,
|
|
1771
|
+
x,
|
|
1772
|
+
y,
|
|
1773
|
+
height: cornersSquareSize,
|
|
1774
|
+
width: cornersSquareSize,
|
|
1775
|
+
name: `corners-square-color-${column}-${row}`
|
|
1776
|
+
});
|
|
1777
|
+
}
|
|
1778
|
+
if ((_e = options.cornersSquareOptions) == null ? void 0 : _e.type) {
|
|
1779
|
+
const cornersSquare = new QRCornerSquare2({
|
|
1780
|
+
svg: this._element,
|
|
1781
|
+
type: options.cornersSquareOptions.type,
|
|
1782
|
+
window: this._window
|
|
1783
|
+
});
|
|
1784
|
+
cornersSquare.draw(x, y, cornersSquareSize, rotation);
|
|
1785
|
+
if (cornersSquare._element && cornersSquareClipPath) {
|
|
1786
|
+
cornersSquareClipPath.appendChild(cornersSquare._element);
|
|
1787
|
+
}
|
|
1788
|
+
} else {
|
|
1789
|
+
const dot = new QRDot2({
|
|
1790
|
+
svg: this._element,
|
|
1791
|
+
type: options.dotsOptions.type,
|
|
1792
|
+
window: this._window
|
|
1793
|
+
});
|
|
1794
|
+
for (let i = 0; i < squareMask2.length; i++) {
|
|
1795
|
+
for (let j = 0; j < squareMask2[i].length; j++) {
|
|
1796
|
+
if (!((_f = squareMask2[i]) == null ? void 0 : _f[j])) {
|
|
1797
|
+
continue;
|
|
1798
|
+
}
|
|
1799
|
+
dot.draw(
|
|
1800
|
+
x + i * dotSize,
|
|
1801
|
+
y + j * dotSize,
|
|
1802
|
+
dotSize,
|
|
1803
|
+
(xOffset, yOffset) => {
|
|
1804
|
+
var _a2;
|
|
1805
|
+
return !!((_a2 = squareMask2[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
|
|
1806
|
+
}
|
|
1807
|
+
);
|
|
1808
|
+
if (dot._element && cornersSquareClipPath) {
|
|
1809
|
+
cornersSquareClipPath.appendChild(dot._element);
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
if (((_g = options.cornersDotOptions) == null ? void 0 : _g.gradient) || ((_h = options.cornersDotOptions) == null ? void 0 : _h.color)) {
|
|
1815
|
+
cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
|
|
1816
|
+
cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
|
|
1817
|
+
this._defs.appendChild(cornersDotClipPath);
|
|
1818
|
+
this._cornersDotClipPath = cornersDotClipPath;
|
|
1819
|
+
this._createColor({
|
|
1820
|
+
options: (_i = options.cornersDotOptions) == null ? void 0 : _i.gradient,
|
|
1821
|
+
color: (_j = options.cornersDotOptions) == null ? void 0 : _j.color,
|
|
1822
|
+
additionalRotation: rotation,
|
|
1823
|
+
x: x + dotSize * 2,
|
|
1824
|
+
y: y + dotSize * 2,
|
|
1825
|
+
height: cornersDotSize,
|
|
1826
|
+
width: cornersDotSize,
|
|
1827
|
+
name: `corners-dot-color-${column}-${row}`
|
|
1828
|
+
});
|
|
1829
|
+
}
|
|
1830
|
+
if ((_k = options.cornersDotOptions) == null ? void 0 : _k.type) {
|
|
1831
|
+
const cornersDot = new QRCornerDot2({
|
|
1832
|
+
svg: this._element,
|
|
1833
|
+
type: options.cornersDotOptions.type,
|
|
1834
|
+
window: this._window
|
|
1835
|
+
});
|
|
1836
|
+
cornersDot.draw(x + dotSize * 2, y + dotSize * 2, cornersDotSize, rotation);
|
|
1837
|
+
if (cornersDot._element && cornersDotClipPath) {
|
|
1838
|
+
cornersDotClipPath.appendChild(cornersDot._element);
|
|
1839
|
+
}
|
|
1840
|
+
} else {
|
|
1841
|
+
const dot = new QRDot2({
|
|
1842
|
+
svg: this._element,
|
|
1843
|
+
type: options.dotsOptions.type,
|
|
1844
|
+
window: this._window
|
|
1845
|
+
});
|
|
1846
|
+
for (let i = 0; i < dotMask2.length; i++) {
|
|
1847
|
+
for (let j = 0; j < dotMask2[i].length; j++) {
|
|
1848
|
+
if (!((_l = dotMask2[i]) == null ? void 0 : _l[j])) {
|
|
1849
|
+
continue;
|
|
1850
|
+
}
|
|
1851
|
+
dot.draw(
|
|
1852
|
+
x + i * dotSize,
|
|
1853
|
+
y + j * dotSize,
|
|
1854
|
+
dotSize,
|
|
1855
|
+
(xOffset, yOffset) => {
|
|
1856
|
+
var _a2;
|
|
1857
|
+
return !!((_a2 = dotMask2[i + xOffset]) == null ? void 0 : _a2[j + yOffset]);
|
|
1858
|
+
}
|
|
1859
|
+
);
|
|
1860
|
+
if (dot._element && cornersDotClipPath) {
|
|
1861
|
+
cornersDotClipPath.appendChild(dot._element);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
imageToBlob() {
|
|
1869
|
+
var _a;
|
|
1870
|
+
if (!this._image) return;
|
|
1871
|
+
if (/(\.svg$)|(^data:image\/svg)/.test((_a = this._options.image) != null ? _a : "")) {
|
|
1872
|
+
this._image.width = this._options.width;
|
|
1873
|
+
this._image.height = this._options.height;
|
|
1874
|
+
}
|
|
1875
|
+
if (this._options.imageOptions.saveAsBlob && this._canvas) {
|
|
1876
|
+
const ctx = this._canvas.getContext("2d");
|
|
1877
|
+
if (ctx) {
|
|
1878
|
+
ctx.drawImage(this._image, 0, 0, this._canvas.width, this._canvas.height);
|
|
1879
|
+
this._imageUri = this._canvas.toDataURL("image/png");
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
async _loadImageWithoutDom() {
|
|
1884
|
+
const source = this._options.image;
|
|
1885
|
+
if (!source) throw "Image is not defined";
|
|
1886
|
+
const data = await loadImageData(source);
|
|
1887
|
+
const { width, height } = getImageSize(data);
|
|
1888
|
+
this._image = { width, height };
|
|
1889
|
+
if (this._options.imageOptions.saveAsBlob && !source.startsWith("data:")) {
|
|
1890
|
+
this._imageUri = toDataUrl(data);
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
loadImage() {
|
|
1894
|
+
if (this._virtual) {
|
|
1895
|
+
return this._loadImageWithoutDom();
|
|
1896
|
+
}
|
|
1897
|
+
return new Promise((resolve, reject) => {
|
|
1898
|
+
var _a;
|
|
1899
|
+
const options = this._options;
|
|
1900
|
+
if (((_a = options.nodeCanvas) == null ? void 0 : _a.loadImage) && options.image) {
|
|
1901
|
+
options.nodeCanvas.loadImage(options.image).then((image2) => {
|
|
1902
|
+
this._image = image2;
|
|
1903
|
+
resolve();
|
|
1904
|
+
}).catch(reject);
|
|
1905
|
+
return;
|
|
1906
|
+
}
|
|
1907
|
+
const image = new this._window.Image();
|
|
1908
|
+
if (!options.image) {
|
|
1909
|
+
return reject("Image is not defined");
|
|
1910
|
+
}
|
|
1911
|
+
if (typeof options.imageOptions.crossOrigin === "string") {
|
|
1912
|
+
image.crossOrigin = options.imageOptions.crossOrigin;
|
|
1913
|
+
}
|
|
1914
|
+
this._image = image;
|
|
1915
|
+
image.onload = () => {
|
|
1916
|
+
resolve();
|
|
1917
|
+
};
|
|
1918
|
+
image.src = options.image;
|
|
1919
|
+
});
|
|
1920
|
+
}
|
|
1921
|
+
drawImage({
|
|
1922
|
+
width,
|
|
1923
|
+
height,
|
|
1924
|
+
count,
|
|
1925
|
+
dotSize
|
|
1926
|
+
}) {
|
|
1927
|
+
const options = this._options;
|
|
1928
|
+
const xBeginning = Math.floor((options.width - count * dotSize) / 2);
|
|
1929
|
+
const yBeginning = Math.floor((options.height - count * dotSize) / 2);
|
|
1930
|
+
const dx = xBeginning + options.imageOptions.margin + (count * dotSize - width) / 2;
|
|
1931
|
+
const dy = yBeginning + options.imageOptions.margin + (count * dotSize - height) / 2;
|
|
1932
|
+
const dw = width - options.imageOptions.margin * 2;
|
|
1933
|
+
const dh = height - options.imageOptions.margin * 2;
|
|
1934
|
+
const image = this._window.document.createElementNS("http://www.w3.org/2000/svg", "image");
|
|
1935
|
+
image.setAttribute("href", this._imageUri || "");
|
|
1936
|
+
image.setAttribute("x", String(dx));
|
|
1937
|
+
image.setAttribute("y", String(dy));
|
|
1938
|
+
image.setAttribute("width", `${dw}px`);
|
|
1939
|
+
image.setAttribute("height", `${dh}px`);
|
|
1940
|
+
this._element.appendChild(image);
|
|
1941
|
+
}
|
|
1942
|
+
_createColor({
|
|
1943
|
+
options,
|
|
1944
|
+
color,
|
|
1945
|
+
additionalRotation,
|
|
1946
|
+
x,
|
|
1947
|
+
y,
|
|
1948
|
+
height,
|
|
1949
|
+
width,
|
|
1950
|
+
name
|
|
1951
|
+
}) {
|
|
1952
|
+
const size = width > height ? width : height;
|
|
1953
|
+
const rect = this._window.document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
1954
|
+
rect.setAttribute("x", String(x));
|
|
1955
|
+
rect.setAttribute("y", String(y));
|
|
1956
|
+
rect.setAttribute("height", String(height));
|
|
1957
|
+
rect.setAttribute("width", String(width));
|
|
1958
|
+
rect.setAttribute("clip-path", `url('#clip-path-${name}')`);
|
|
1959
|
+
if (options) {
|
|
1960
|
+
let gradient;
|
|
1961
|
+
if (options.type === gradientTypes_default.radial) {
|
|
1962
|
+
gradient = this._window.document.createElementNS("http://www.w3.org/2000/svg", "radialGradient");
|
|
1963
|
+
gradient.setAttribute("id", name);
|
|
1964
|
+
gradient.setAttribute("gradientUnits", "userSpaceOnUse");
|
|
1965
|
+
gradient.setAttribute("fx", String(x + width / 2));
|
|
1966
|
+
gradient.setAttribute("fy", String(y + height / 2));
|
|
1967
|
+
gradient.setAttribute("cx", String(x + width / 2));
|
|
1968
|
+
gradient.setAttribute("cy", String(y + height / 2));
|
|
1969
|
+
gradient.setAttribute("r", String(size / 2));
|
|
1970
|
+
} else {
|
|
1971
|
+
const rotation = ((options.rotation || 0) + additionalRotation) % (2 * Math.PI);
|
|
1972
|
+
const positiveRotation = (rotation + 2 * Math.PI) % (2 * Math.PI);
|
|
1973
|
+
let x0 = x + width / 2;
|
|
1974
|
+
let y0 = y + height / 2;
|
|
1975
|
+
let x1 = x + width / 2;
|
|
1976
|
+
let y1 = y + height / 2;
|
|
1977
|
+
if (positiveRotation >= 0 && positiveRotation <= 0.25 * Math.PI || positiveRotation > 1.75 * Math.PI && positiveRotation <= 2 * Math.PI) {
|
|
1978
|
+
x0 = x0 - width / 2;
|
|
1979
|
+
y0 = y0 - height / 2 * Math.tan(rotation);
|
|
1980
|
+
x1 = x1 + width / 2;
|
|
1981
|
+
y1 = y1 + height / 2 * Math.tan(rotation);
|
|
1982
|
+
} else if (positiveRotation > 0.25 * Math.PI && positiveRotation <= 0.75 * Math.PI) {
|
|
1983
|
+
y0 = y0 - height / 2;
|
|
1984
|
+
x0 = x0 - width / 2 / Math.tan(rotation);
|
|
1985
|
+
y1 = y1 + height / 2;
|
|
1986
|
+
x1 = x1 + width / 2 / Math.tan(rotation);
|
|
1987
|
+
} else if (positiveRotation > 0.75 * Math.PI && positiveRotation <= 1.25 * Math.PI) {
|
|
1988
|
+
x0 = x0 + width / 2;
|
|
1989
|
+
y0 = y0 + height / 2 * Math.tan(rotation);
|
|
1990
|
+
x1 = x1 - width / 2;
|
|
1991
|
+
y1 = y1 - height / 2 * Math.tan(rotation);
|
|
1992
|
+
} else if (positiveRotation > 1.25 * Math.PI && positiveRotation <= 1.75 * Math.PI) {
|
|
1993
|
+
y0 = y0 + height / 2;
|
|
1994
|
+
x0 = x0 + width / 2 / Math.tan(rotation);
|
|
1995
|
+
y1 = y1 - height / 2;
|
|
1996
|
+
x1 = x1 - width / 2 / Math.tan(rotation);
|
|
1997
|
+
}
|
|
1998
|
+
gradient = this._window.document.createElementNS("http://www.w3.org/2000/svg", "linearGradient");
|
|
1999
|
+
gradient.setAttribute("id", name);
|
|
2000
|
+
gradient.setAttribute("gradientUnits", "userSpaceOnUse");
|
|
2001
|
+
gradient.setAttribute("x1", String(Math.round(x0)));
|
|
2002
|
+
gradient.setAttribute("y1", String(Math.round(y0)));
|
|
2003
|
+
gradient.setAttribute("x2", String(Math.round(x1)));
|
|
2004
|
+
gradient.setAttribute("y2", String(Math.round(y1)));
|
|
2005
|
+
}
|
|
2006
|
+
options.colorStops.forEach(({ offset, color: color2 }) => {
|
|
2007
|
+
const stop = this._window.document.createElementNS("http://www.w3.org/2000/svg", "stop");
|
|
2008
|
+
stop.setAttribute("offset", `${100 * offset}%`);
|
|
2009
|
+
stop.setAttribute("stop-color", color2);
|
|
2010
|
+
gradient.appendChild(stop);
|
|
2011
|
+
});
|
|
2012
|
+
rect.setAttribute("fill", `url('#${name}')`);
|
|
2013
|
+
this._defs.appendChild(gradient);
|
|
2014
|
+
} else if (color) {
|
|
2015
|
+
rect.setAttribute("fill", color);
|
|
2016
|
+
}
|
|
2017
|
+
this._element.appendChild(rect);
|
|
2018
|
+
}
|
|
2019
|
+
};
|
|
2020
|
+
|
|
2021
|
+
// src/constants/drawTypes.ts
|
|
2022
|
+
var drawTypes_default = {
|
|
2023
|
+
canvas: "canvas",
|
|
2024
|
+
svg: "svg"
|
|
2025
|
+
};
|
|
2026
|
+
|
|
2027
|
+
// src/constants/qrTypes.ts
|
|
2028
|
+
var qrTypes = {};
|
|
2029
|
+
for (let type = 0; type <= 40; type++) {
|
|
2030
|
+
qrTypes[type] = type;
|
|
2031
|
+
}
|
|
2032
|
+
var qrTypes_default = qrTypes;
|
|
2033
|
+
|
|
2034
|
+
// src/constants/errorCorrectionLevels.ts
|
|
2035
|
+
var errorCorrectionLevels_default = {
|
|
2036
|
+
L: "L",
|
|
2037
|
+
M: "M",
|
|
2038
|
+
Q: "Q",
|
|
2039
|
+
H: "H"
|
|
2040
|
+
};
|
|
2041
|
+
|
|
2042
|
+
// src/core/QROptions.ts
|
|
2043
|
+
var defaultOptions = {
|
|
2044
|
+
type: drawTypes_default.canvas,
|
|
2045
|
+
width: 300,
|
|
2046
|
+
height: 300,
|
|
2047
|
+
data: "",
|
|
2048
|
+
margin: 0,
|
|
2049
|
+
qrOptions: {
|
|
2050
|
+
typeNumber: qrTypes_default[0],
|
|
2051
|
+
mode: void 0,
|
|
2052
|
+
errorCorrectionLevel: errorCorrectionLevels_default.Q
|
|
2053
|
+
},
|
|
2054
|
+
imageOptions: {
|
|
2055
|
+
saveAsBlob: false,
|
|
2056
|
+
hideBackgroundDots: true,
|
|
2057
|
+
imageSize: 0.4,
|
|
2058
|
+
crossOrigin: void 0,
|
|
2059
|
+
margin: 0
|
|
2060
|
+
},
|
|
2061
|
+
dotsOptions: {
|
|
2062
|
+
type: "square",
|
|
2063
|
+
color: "#000"
|
|
2064
|
+
},
|
|
2065
|
+
backgroundOptions: {
|
|
2066
|
+
color: "#fff"
|
|
2067
|
+
}
|
|
2068
|
+
};
|
|
2069
|
+
var QROptions_default = defaultOptions;
|
|
2070
|
+
|
|
2071
|
+
// src/tools/sanitizeOptions.ts
|
|
2072
|
+
function sanitizeGradient(gradient) {
|
|
2073
|
+
const newGradient = { ...gradient };
|
|
2074
|
+
if (!newGradient.colorStops || !newGradient.colorStops.length) {
|
|
2075
|
+
throw "Field 'colorStops' is required in gradient";
|
|
2076
|
+
}
|
|
2077
|
+
if (newGradient.rotation) {
|
|
2078
|
+
newGradient.rotation = Number(newGradient.rotation);
|
|
2079
|
+
} else {
|
|
2080
|
+
newGradient.rotation = 0;
|
|
2081
|
+
}
|
|
2082
|
+
newGradient.colorStops = newGradient.colorStops.map((colorStop) => ({
|
|
2083
|
+
...colorStop,
|
|
2084
|
+
offset: Number(colorStop.offset)
|
|
2085
|
+
}));
|
|
2086
|
+
return newGradient;
|
|
2087
|
+
}
|
|
2088
|
+
function sanitizeOptions(options) {
|
|
2089
|
+
const newOptions = { ...options };
|
|
2090
|
+
newOptions.width = Number(newOptions.width);
|
|
2091
|
+
newOptions.height = Number(newOptions.height);
|
|
2092
|
+
newOptions.margin = Number(newOptions.margin);
|
|
2093
|
+
newOptions.imageOptions = {
|
|
2094
|
+
...newOptions.imageOptions,
|
|
2095
|
+
hideBackgroundDots: Boolean(newOptions.imageOptions.hideBackgroundDots),
|
|
2096
|
+
imageSize: Number(newOptions.imageOptions.imageSize),
|
|
2097
|
+
margin: Number(newOptions.imageOptions.margin)
|
|
2098
|
+
};
|
|
2099
|
+
if (newOptions.margin > Math.min(newOptions.width, newOptions.height)) {
|
|
2100
|
+
newOptions.margin = Math.min(newOptions.width, newOptions.height);
|
|
2101
|
+
}
|
|
2102
|
+
newOptions.dotsOptions = {
|
|
2103
|
+
...newOptions.dotsOptions
|
|
2104
|
+
};
|
|
2105
|
+
if (newOptions.dotsOptions.gradient) {
|
|
2106
|
+
newOptions.dotsOptions.gradient = sanitizeGradient(newOptions.dotsOptions.gradient);
|
|
2107
|
+
}
|
|
2108
|
+
if (newOptions.cornersSquareOptions) {
|
|
2109
|
+
newOptions.cornersSquareOptions = {
|
|
2110
|
+
...newOptions.cornersSquareOptions
|
|
2111
|
+
};
|
|
2112
|
+
if (newOptions.cornersSquareOptions.gradient) {
|
|
2113
|
+
newOptions.cornersSquareOptions.gradient = sanitizeGradient(newOptions.cornersSquareOptions.gradient);
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
if (newOptions.cornersDotOptions) {
|
|
2117
|
+
newOptions.cornersDotOptions = {
|
|
2118
|
+
...newOptions.cornersDotOptions
|
|
2119
|
+
};
|
|
2120
|
+
if (newOptions.cornersDotOptions.gradient) {
|
|
2121
|
+
newOptions.cornersDotOptions.gradient = sanitizeGradient(newOptions.cornersDotOptions.gradient);
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
if (newOptions.backgroundOptions) {
|
|
2125
|
+
newOptions.backgroundOptions = {
|
|
2126
|
+
...newOptions.backgroundOptions
|
|
2127
|
+
};
|
|
2128
|
+
if (newOptions.backgroundOptions.gradient) {
|
|
2129
|
+
newOptions.backgroundOptions.gradient = sanitizeGradient(newOptions.backgroundOptions.gradient);
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
return newOptions;
|
|
2133
|
+
}
|
|
2134
|
+
|
|
2135
|
+
// src/core/QRCodeStyling.ts
|
|
2136
|
+
import qrcode from "qrcode-generator";
|
|
2137
|
+
var QRCodeStyling = class _QRCodeStyling {
|
|
2138
|
+
constructor(options) {
|
|
2139
|
+
this._options = options ? sanitizeOptions(mergeDeep(QROptions_default, options)) : QROptions_default;
|
|
2140
|
+
this.update();
|
|
2141
|
+
}
|
|
2142
|
+
/** Canvas output needs a browser canvas or node-canvas. */
|
|
2143
|
+
_canUseCanvas() {
|
|
2144
|
+
var _a;
|
|
2145
|
+
return !!((_a = this._options.nodeCanvas) == null ? void 0 : _a.createCanvas) || hasBrowserDom();
|
|
2146
|
+
}
|
|
2147
|
+
static _clearContainer(container) {
|
|
2148
|
+
if (container) {
|
|
2149
|
+
container.innerHTML = "";
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
async _getQRStylingElement(extension = "png") {
|
|
2153
|
+
if (!this._qr) throw "QR code is empty";
|
|
2154
|
+
if (extension.toLowerCase() === "svg") {
|
|
2155
|
+
let promise, svg;
|
|
2156
|
+
if (this._svg && this._svgDrawingPromise) {
|
|
2157
|
+
svg = this._svg;
|
|
2158
|
+
promise = this._svgDrawingPromise;
|
|
2159
|
+
} else {
|
|
2160
|
+
svg = new QRSVG(this._options);
|
|
2161
|
+
promise = svg.drawQR(this._qr);
|
|
2162
|
+
}
|
|
2163
|
+
await promise;
|
|
2164
|
+
return svg;
|
|
2165
|
+
} else {
|
|
2166
|
+
if (!this._canUseCanvas()) {
|
|
2167
|
+
throw new Error(
|
|
2168
|
+
`Rendering "${extension}" outside the browser needs node-canvas (pass { nodeCanvas: require("canvas") }) or, for png, resvg (pass { resvg: require("@resvg/resvg-js").Resvg }). SVG output needs neither.`
|
|
2169
|
+
);
|
|
2170
|
+
}
|
|
2171
|
+
let promise, canvas;
|
|
2172
|
+
if (this._canvas && this._canvasDrawingPromise) {
|
|
2173
|
+
canvas = this._canvas;
|
|
2174
|
+
promise = this._canvasDrawingPromise;
|
|
2175
|
+
} else {
|
|
2176
|
+
canvas = new QRCanvas(this._options);
|
|
2177
|
+
promise = canvas.drawQR(this._qr);
|
|
2178
|
+
}
|
|
2179
|
+
await promise;
|
|
2180
|
+
return canvas;
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
update(options) {
|
|
2184
|
+
_QRCodeStyling._clearContainer(this._container);
|
|
2185
|
+
this._options = options ? sanitizeOptions(mergeDeep(this._options, options)) : this._options;
|
|
2186
|
+
if (!this._options.data) {
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
this._qr = qrcode(this._options.qrOptions.typeNumber, this._options.qrOptions.errorCorrectionLevel);
|
|
2190
|
+
this._qr.addData(this._options.data, this._options.qrOptions.mode || getMode(this._options.data));
|
|
2191
|
+
this._qr.make();
|
|
2192
|
+
if (this._options.type === drawTypes_default.canvas && !this._canUseCanvas()) {
|
|
2193
|
+
this._canvas = void 0;
|
|
2194
|
+
this._canvasDrawingPromise = void 0;
|
|
2195
|
+
this._svg = void 0;
|
|
2196
|
+
this._svgDrawingPromise = void 0;
|
|
2197
|
+
} else if (this._options.type === drawTypes_default.canvas) {
|
|
2198
|
+
this._canvas = new QRCanvas(this._options);
|
|
2199
|
+
this._canvasDrawingPromise = this._canvas.drawQR(this._qr);
|
|
2200
|
+
this._svgDrawingPromise = void 0;
|
|
2201
|
+
this._svg = void 0;
|
|
2202
|
+
} else {
|
|
2203
|
+
this._svg = new QRSVG(this._options);
|
|
2204
|
+
this._svgDrawingPromise = this._svg.drawQR(this._qr);
|
|
2205
|
+
this._canvasDrawingPromise = void 0;
|
|
2206
|
+
this._canvas = void 0;
|
|
2207
|
+
}
|
|
2208
|
+
this.append(this._container);
|
|
2209
|
+
}
|
|
2210
|
+
append(container) {
|
|
2211
|
+
if (!container) {
|
|
2212
|
+
return;
|
|
2213
|
+
}
|
|
2214
|
+
if (typeof container.appendChild !== "function") {
|
|
2215
|
+
throw "Container should be a single DOM node";
|
|
2216
|
+
}
|
|
2217
|
+
if (this._options.type === drawTypes_default.canvas) {
|
|
2218
|
+
if (this._canvas) {
|
|
2219
|
+
container.appendChild(this._canvas.getCanvas());
|
|
2220
|
+
}
|
|
2221
|
+
} else {
|
|
2222
|
+
if (this._svg) {
|
|
2223
|
+
container.appendChild(this._svg.getElement());
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
this._container = container;
|
|
2227
|
+
}
|
|
2228
|
+
static _serializeSVG(svg) {
|
|
2229
|
+
const serializer = new svg._window.XMLSerializer();
|
|
2230
|
+
return `<?xml version="1.0" standalone="no"?>\r
|
|
2231
|
+
${serializer.serializeToString(svg.getElement())}`;
|
|
2232
|
+
}
|
|
2233
|
+
async _getSVGString() {
|
|
2234
|
+
const svg = await this._getQRStylingElement("svg");
|
|
2235
|
+
return { svgString: _QRCodeStyling._serializeSVG(svg), svg };
|
|
2236
|
+
}
|
|
2237
|
+
/**
|
|
2238
|
+
* resvg follows the SVG spec more strictly than browsers: it does not accept quoted url('#id')
|
|
2239
|
+
* references, and it hides elements whose clip-path points to a missing id. Browsers ignore both,
|
|
2240
|
+
* so normalize them to get the picture a browser would show. The public SVG output is unchanged.
|
|
2241
|
+
*/
|
|
2242
|
+
static _normalizeSVGForResvg(svgString) {
|
|
2243
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2244
|
+
svgString.replace(/\sid="([^"]+)"/g, (match, id) => {
|
|
2245
|
+
ids.add(id);
|
|
2246
|
+
return match;
|
|
2247
|
+
});
|
|
2248
|
+
return svgString.replace(
|
|
2249
|
+
/\sclip-path="url\('#([^']+)'\)"/g,
|
|
2250
|
+
(_match, id) => ids.has(id) ? ` clip-path="url(#${id})"` : ""
|
|
2251
|
+
).replace(/url\('#([^']+)'\)/g, "url(#$1)");
|
|
2252
|
+
}
|
|
2253
|
+
async _rasterizeWithResvg(extension) {
|
|
2254
|
+
const Resvg = this._options.resvg;
|
|
2255
|
+
if (!Resvg) throw new Error("resvg is not configured");
|
|
2256
|
+
if (extension.toLowerCase() !== "png") {
|
|
2257
|
+
throw new Error(`resvg can only produce png, not "${extension}". Use extension "png" or pass nodeCanvas.`);
|
|
2258
|
+
}
|
|
2259
|
+
const previous = this._options.imageOptions.saveAsBlob;
|
|
2260
|
+
const previousSvg = this._svg;
|
|
2261
|
+
const previousPromise = this._svgDrawingPromise;
|
|
2262
|
+
let svgString;
|
|
2263
|
+
try {
|
|
2264
|
+
if (this._options.image && !previous && !this._options.image.startsWith("data:")) {
|
|
2265
|
+
this._options.imageOptions.saveAsBlob = true;
|
|
2266
|
+
this._svg = void 0;
|
|
2267
|
+
this._svgDrawingPromise = void 0;
|
|
2268
|
+
}
|
|
2269
|
+
({ svgString } = await this._getSVGString());
|
|
2270
|
+
} finally {
|
|
2271
|
+
this._options.imageOptions.saveAsBlob = previous;
|
|
2272
|
+
this._svg = previousSvg;
|
|
2273
|
+
this._svgDrawingPromise = previousPromise;
|
|
2274
|
+
}
|
|
2275
|
+
const png = new Resvg(_QRCodeStyling._normalizeSVGForResvg(svgString), { fitTo: { mode: "original" } }).render().asPng();
|
|
2276
|
+
return typeof Buffer !== "undefined" ? Buffer.from(png) : png;
|
|
2277
|
+
}
|
|
2278
|
+
async getRawData(extension = "png") {
|
|
2279
|
+
if (!this._qr) throw "QR code is empty";
|
|
2280
|
+
if (extension.toLowerCase() !== "svg" && !this._canUseCanvas() && this._options.resvg) {
|
|
2281
|
+
return await this._rasterizeWithResvg(extension);
|
|
2282
|
+
}
|
|
2283
|
+
const element = await this._getQRStylingElement(extension);
|
|
2284
|
+
if (extension.toLowerCase() === "svg" && element._virtual) {
|
|
2285
|
+
const svgString = _QRCodeStyling._serializeSVG(element);
|
|
2286
|
+
return typeof Buffer !== "undefined" ? Buffer.from(svgString) : new Blob([svgString], { type: "image/svg+xml" });
|
|
2287
|
+
}
|
|
2288
|
+
if (extension.toLowerCase() === "svg") {
|
|
2289
|
+
const serializer = new element._window.XMLSerializer();
|
|
2290
|
+
const source = serializer.serializeToString(element.getElement());
|
|
2291
|
+
const svgString = `<?xml version="1.0" standalone="no"?>\r
|
|
2292
|
+
${source}`;
|
|
2293
|
+
if (typeof Blob !== "undefined" && !this._options.jsdom) {
|
|
2294
|
+
return new Blob([svgString], { type: "image/svg+xml" });
|
|
2295
|
+
} else {
|
|
2296
|
+
return Buffer.from(svgString);
|
|
2297
|
+
}
|
|
2298
|
+
} else {
|
|
2299
|
+
return new Promise((resolve) => {
|
|
2300
|
+
const canvas = element.getCanvas();
|
|
2301
|
+
if (canvas.toBuffer) {
|
|
2302
|
+
resolve(canvas.toBuffer(`image/${extension}`));
|
|
2303
|
+
} else {
|
|
2304
|
+
canvas.toBlob(resolve, `image/${extension}`, 1);
|
|
2305
|
+
}
|
|
2306
|
+
});
|
|
2307
|
+
}
|
|
2308
|
+
}
|
|
2309
|
+
async download(downloadOptions) {
|
|
2310
|
+
if (!this._qr) throw "QR code is empty";
|
|
2311
|
+
if (typeof Blob === "undefined") throw "Cannot download in Node.js, call getRawData instead.";
|
|
2312
|
+
let extension = "png";
|
|
2313
|
+
let name = "qr";
|
|
2314
|
+
if (typeof downloadOptions === "string") {
|
|
2315
|
+
extension = downloadOptions;
|
|
2316
|
+
console.warn(
|
|
2317
|
+
"Extension is deprecated as argument for 'download' method, please pass object { name: '...', extension: '...' } as argument"
|
|
2318
|
+
);
|
|
2319
|
+
} else if (typeof downloadOptions === "object" && downloadOptions !== null) {
|
|
2320
|
+
if (downloadOptions.name) {
|
|
2321
|
+
name = downloadOptions.name;
|
|
2322
|
+
}
|
|
2323
|
+
if (downloadOptions.extension) {
|
|
2324
|
+
extension = downloadOptions.extension;
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
const element = await this._getQRStylingElement(extension);
|
|
2328
|
+
if (extension.toLowerCase() === "svg") {
|
|
2329
|
+
const serializer = new XMLSerializer();
|
|
2330
|
+
let source = serializer.serializeToString(element.getElement());
|
|
2331
|
+
source = '<?xml version="1.0" standalone="no"?>\r\n' + source;
|
|
2332
|
+
const url = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source);
|
|
2333
|
+
downloadURI(url, `${name}.svg`);
|
|
2334
|
+
} else {
|
|
2335
|
+
const url = element.getCanvas().toDataURL(`image/${extension}`);
|
|
2336
|
+
downloadURI(url, `${name}.${extension}`);
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
};
|
|
2340
|
+
|
|
2341
|
+
// src/types/index.ts
|
|
2342
|
+
var JSDom = class {
|
|
2343
|
+
constructor(input, options) {
|
|
2344
|
+
this._options = options;
|
|
2345
|
+
this._input = input;
|
|
2346
|
+
this.window = window;
|
|
2347
|
+
}
|
|
2348
|
+
};
|
|
2349
|
+
|
|
2350
|
+
// src/index.ts
|
|
2351
|
+
var index_default = QRCodeStyling;
|
|
2352
|
+
export {
|
|
2353
|
+
JSDom,
|
|
2354
|
+
cornerDotTypes_default as cornerDotTypes,
|
|
2355
|
+
cornerSquareTypes_default as cornerSquareTypes,
|
|
2356
|
+
index_default as default,
|
|
2357
|
+
dotTypes_default as dotTypes,
|
|
2358
|
+
drawTypes_default as drawTypes,
|
|
2359
|
+
errorCorrectionLevels_default as errorCorrectionLevels,
|
|
2360
|
+
errorCorrectionPercents_default as errorCorrectionPercents,
|
|
2361
|
+
modes_default as modes,
|
|
2362
|
+
qrTypes_default as qrTypes
|
|
2363
|
+
};
|
|
2364
|
+
//# sourceMappingURL=qr-code-styling.mjs.map
|