@twin.org/qr 0.0.2-next.9 → 0.0.3-next.2

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.
Files changed (66) hide show
  1. package/dist/es/data/qrAlphaNumeric.js +79 -0
  2. package/dist/es/data/qrAlphaNumeric.js.map +1 -0
  3. package/dist/es/data/qrByte8.js +63 -0
  4. package/dist/es/data/qrByte8.js.map +1 -0
  5. package/dist/es/data/qrDataBase.js +102 -0
  6. package/dist/es/data/qrDataBase.js.map +1 -0
  7. package/dist/es/data/qrNumber.js +73 -0
  8. package/dist/es/data/qrNumber.js.map +1 -0
  9. package/dist/es/helpers/bitBuffer.js +79 -0
  10. package/dist/es/helpers/bitBuffer.js.map +1 -0
  11. package/dist/es/helpers/mathHelper.js +71 -0
  12. package/dist/es/helpers/mathHelper.js.map +1 -0
  13. package/dist/es/helpers/polynomial.js +117 -0
  14. package/dist/es/helpers/polynomial.js.map +1 -0
  15. package/dist/es/helpers/qrHelper.js +269 -0
  16. package/dist/es/helpers/qrHelper.js.map +1 -0
  17. package/dist/es/helpers/rsBlock.js +293 -0
  18. package/dist/es/helpers/rsBlock.js.map +1 -0
  19. package/dist/es/index.js +12 -0
  20. package/dist/es/index.js.map +1 -0
  21. package/dist/es/models/IBitmapRendererOptions.js +2 -0
  22. package/dist/es/models/IBitmapRendererOptions.js.map +1 -0
  23. package/dist/es/models/IRendererOptions.js +4 -0
  24. package/dist/es/models/IRendererOptions.js.map +1 -0
  25. package/dist/es/models/ITextRendererOptions.js +2 -0
  26. package/dist/es/models/ITextRendererOptions.js.map +1 -0
  27. package/dist/es/models/errorCorrectLevel.js +26 -0
  28. package/dist/es/models/errorCorrectLevel.js.map +1 -0
  29. package/dist/es/models/maskPattern.js +42 -0
  30. package/dist/es/models/maskPattern.js.map +1 -0
  31. package/dist/es/models/qrCellData.js +2 -0
  32. package/dist/es/models/qrCellData.js.map +1 -0
  33. package/dist/es/models/qrDataMode.js +22 -0
  34. package/dist/es/models/qrDataMode.js.map +1 -0
  35. package/dist/es/qr.js +551 -0
  36. package/dist/es/qr.js.map +1 -0
  37. package/dist/es/renderers/jpegRenderer.js +80 -0
  38. package/dist/es/renderers/jpegRenderer.js.map +1 -0
  39. package/dist/es/renderers/pngRenderer.js +80 -0
  40. package/dist/es/renderers/pngRenderer.js.map +1 -0
  41. package/dist/es/renderers/textRenderer.js +65 -0
  42. package/dist/es/renderers/textRenderer.js.map +1 -0
  43. package/dist/types/data/qrAlphaNumeric.d.ts +6 -2
  44. package/dist/types/data/qrByte8.d.ts +2 -2
  45. package/dist/types/{models → data}/qrDataBase.d.ts +7 -4
  46. package/dist/types/data/qrNumber.d.ts +6 -2
  47. package/dist/types/helpers/mathHelper.d.ts +4 -0
  48. package/dist/types/helpers/polynomial.d.ts +1 -1
  49. package/dist/types/helpers/qrHelper.d.ts +7 -3
  50. package/dist/types/helpers/rsBlock.d.ts +5 -1
  51. package/dist/types/index.d.ts +9 -9
  52. package/dist/types/models/IBitmapRendererOptions.d.ts +1 -1
  53. package/dist/types/models/ITextRendererOptions.d.ts +1 -1
  54. package/dist/types/qr.d.ts +6 -2
  55. package/dist/types/renderers/jpegRenderer.d.ts +6 -2
  56. package/dist/types/renderers/pngRenderer.d.ts +6 -2
  57. package/dist/types/renderers/textRenderer.d.ts +6 -2
  58. package/docs/changelog.md +296 -0
  59. package/docs/reference/classes/JpegRenderer.md +8 -0
  60. package/docs/reference/classes/PngRenderer.md +8 -0
  61. package/docs/reference/classes/QR.md +8 -0
  62. package/docs/reference/classes/TextRenderer.md +8 -0
  63. package/locales/en.json +9 -9
  64. package/package.json +20 -11
  65. package/dist/cjs/index.cjs +0 -1999
  66. package/dist/esm/index.mjs +0 -1993
@@ -1,1993 +0,0 @@
1
- import { GeneralError, Is, Guards } from '@twin.org/core';
2
- import { Color, JpegEncoder, PngEncoder } from '@twin.org/image';
3
-
4
- // Copyright 2024 IOTA Stiftung.
5
- // SPDX-License-Identifier: Apache-2.0.
6
- /**
7
- * Error correction level to use for the QR Code.
8
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
9
- */
10
- // eslint-disable-next-line @typescript-eslint/naming-convention
11
- const ErrorCorrectLevel = {
12
- /**
13
- * 7% Error correction.
14
- */
15
- L: 1,
16
- /**
17
- * 15% Error correction.
18
- */
19
- M: 0,
20
- /**
21
- * 25% Error correction.
22
- */
23
- Q: 3,
24
- /**
25
- * 30% Error correction.
26
- */
27
- H: 2
28
- };
29
-
30
- // Copyright 2024 IOTA Stiftung.
31
- // SPDX-License-Identifier: Apache-2.0.
32
- /**
33
- * The mode for the qr data.
34
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
35
- */
36
- // eslint-disable-next-line @typescript-eslint/naming-convention
37
- const QRDataMode = {
38
- /**
39
- * Number.
40
- */
41
- Number: 1,
42
- /**
43
- * Alphabet and number.
44
- */
45
- AlphaNumeric: 2,
46
- /**
47
- * 8bit byte.
48
- */
49
- Byte8: 4
50
- };
51
-
52
- // Copyright 2024 IOTA Stiftung.
53
- // SPDX-License-Identifier: Apache-2.0.
54
- /**
55
- * Base class for storing QR Data.
56
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
57
- */
58
- class QRDataBase {
59
- /**
60
- * @internal
61
- */
62
- _className;
63
- /**
64
- * @internal
65
- */
66
- _mode;
67
- /**
68
- * @internal
69
- */
70
- _data;
71
- /**
72
- * Create a new instance of QRDataBase.
73
- * @param className The class name for the derived class.
74
- * @param mode The mode for the data.
75
- * @param data The data.
76
- */
77
- constructor(className, mode, data) {
78
- this._className = className;
79
- this._mode = mode;
80
- this._data = data;
81
- }
82
- /**
83
- * Get the data mode.
84
- * @returns The data mode.
85
- */
86
- getMode() {
87
- return this._mode;
88
- }
89
- /**
90
- * Get the data for the qr.
91
- * @returns The data.
92
- */
93
- getData() {
94
- return this._data;
95
- }
96
- /**
97
- * Get the length in bits of the data.
98
- * @param typeNumber The type number to get the length for.
99
- * @returns The length in bits.
100
- * @throws Error if the typeNumber if invalid.
101
- */
102
- getLengthInBits(typeNumber) {
103
- if (typeNumber >= 1 && typeNumber < 10) {
104
- switch (this._mode) {
105
- case QRDataMode.Number:
106
- return 10;
107
- case QRDataMode.AlphaNumeric:
108
- return 9;
109
- case QRDataMode.Byte8:
110
- return 8;
111
- default:
112
- throw new GeneralError(this._className, "invalidMode", {
113
- typeNumber,
114
- mode: this._mode
115
- });
116
- }
117
- }
118
- else if (typeNumber < 27) {
119
- switch (this._mode) {
120
- case QRDataMode.Number:
121
- return 12;
122
- case QRDataMode.AlphaNumeric:
123
- return 11;
124
- case QRDataMode.Byte8:
125
- return 16;
126
- default:
127
- throw new GeneralError(this._className, "invalidMode", {
128
- typeNumber,
129
- mode: this._mode
130
- });
131
- }
132
- }
133
- else if (typeNumber < 41) {
134
- switch (this._mode) {
135
- case QRDataMode.Number:
136
- return 14;
137
- case QRDataMode.AlphaNumeric:
138
- return 13;
139
- case QRDataMode.Byte8:
140
- return 16;
141
- default:
142
- throw new GeneralError(this._className, "invalidMode", {
143
- typeNumber,
144
- mode: this._mode
145
- });
146
- }
147
- }
148
- else {
149
- throw new GeneralError(this._className, "invalidTypeNumber", { typeNumber });
150
- }
151
- }
152
- }
153
-
154
- // Copyright 2024 IOTA Stiftung.
155
- // SPDX-License-Identifier: Apache-2.0.
156
- /* eslint-disable no-mixed-operators */
157
- /**
158
- * QR Data for representing a alpha numeric.
159
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
160
- */
161
- class QRAlphaNumeric extends QRDataBase {
162
- /**
163
- * Runtime name for the class.
164
- * @internal
165
- */
166
- static _CLASS_NAME = "QRAlphaNumeric";
167
- /**
168
- * Create a new instance of QRAlphaNumeric.
169
- * @param data The data for the qr alpha numeric.
170
- */
171
- constructor(data) {
172
- super(QRAlphaNumeric._CLASS_NAME, QRDataMode.AlphaNumeric, data);
173
- }
174
- /**
175
- * Get the length of the data.
176
- * @returns The length of the data.
177
- */
178
- getLength() {
179
- return this.getData().length;
180
- }
181
- /**
182
- * Write data into the buffer.
183
- * @param buffer The buffer to write into.
184
- */
185
- write(buffer) {
186
- const s = this.getData();
187
- let i = 0;
188
- while (i + 1 < s.length) {
189
- buffer.put(this.getCode(s.charAt(i)) * 45 + this.getCode(s.charAt(i + 1)), 11);
190
- i += 2;
191
- }
192
- if (i < s.length) {
193
- buffer.put(this.getCode(s.charAt(i)), 6);
194
- }
195
- }
196
- /**
197
- * @internal
198
- */
199
- getCode(c) {
200
- if (c >= "0" && c <= "9") {
201
- return c.charCodeAt(0) - "0".charCodeAt(0);
202
- }
203
- if (c >= "A" && c <= "Z") {
204
- return c.charCodeAt(0) - "A".charCodeAt(0) + 10;
205
- }
206
- switch (c) {
207
- case " ":
208
- return 36;
209
- case "$":
210
- return 37;
211
- case "%":
212
- return 38;
213
- case "*":
214
- return 39;
215
- case "+":
216
- return 40;
217
- case "-":
218
- return 41;
219
- case ".":
220
- return 42;
221
- case "/":
222
- return 43;
223
- case ":":
224
- return 44;
225
- default:
226
- throw new GeneralError(QRAlphaNumeric._CLASS_NAME, "illegalCharacter", { value: c });
227
- }
228
- }
229
- }
230
-
231
- /**
232
- * QR Data for representing a 8 bit data.
233
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
234
- */
235
- class QRByte8 extends QRDataBase {
236
- /**
237
- * Runtime name for the class.
238
- * @internal
239
- */
240
- static _CLASS_NAME = "QRByte8";
241
- /**
242
- * Create a new instance of QRByte8.
243
- * @param data The data for the qr 8 bit data.
244
- */
245
- constructor(data) {
246
- super(QRByte8._CLASS_NAME, QRDataMode.Byte8, data);
247
- }
248
- /**
249
- * Get the length of the data.
250
- * @returns The length of the data.
251
- */
252
- getLength() {
253
- return this.stringToBytes(this.getData()).length;
254
- }
255
- /**
256
- * Write data into the buffer.
257
- * @param buffer The buffer to write into.
258
- */
259
- write(buffer) {
260
- const data = this.stringToBytes(this.getData());
261
- for (let i = 0; i < data.length; i++) {
262
- buffer.put(data[i], 8);
263
- }
264
- }
265
- /**
266
- * @internal
267
- */
268
- stringToBytes(str) {
269
- const utf8 = [];
270
- for (let i = 0; i < str.length; i++) {
271
- let charCode = str.charCodeAt(i);
272
- if (charCode < 0x80) {
273
- utf8.push(charCode);
274
- }
275
- else if (charCode < 0x800) {
276
- utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
277
- }
278
- else if (charCode < 0xd800 || charCode >= 0xe000) {
279
- utf8.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
280
- }
281
- else {
282
- i++;
283
- // UTF-16 encodes 0x10000-0x10FFFF by
284
- // subtracting 0x10000 and splitting the
285
- // 20 bits of 0x0-0xFFFFF into two halves
286
- charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
287
- utf8.push(0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
288
- }
289
- }
290
- return utf8;
291
- }
292
- }
293
-
294
- // Copyright 2024 IOTA Stiftung.
295
- // SPDX-License-Identifier: Apache-2.0.
296
- /* eslint-disable no-mixed-operators */
297
- /**
298
- * QR Data for representing a number.
299
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
300
- */
301
- class QRNumber extends QRDataBase {
302
- /**
303
- * Runtime name for the class.
304
- * @internal
305
- */
306
- static _CLASS_NAME = "QRNumber";
307
- /**
308
- * Create a new instance of QRNumber.
309
- * @param data The data for the qr number.
310
- */
311
- constructor(data) {
312
- super(QRNumber._CLASS_NAME, QRDataMode.Number, data);
313
- }
314
- /**
315
- * Get the length of the data.
316
- * @returns The length of the data.
317
- */
318
- getLength() {
319
- return this.getData().length;
320
- }
321
- /**
322
- * Write data into the buffer.
323
- * @param buffer The buffer to write into.
324
- */
325
- write(buffer) {
326
- const data = this.getData();
327
- let i = 0;
328
- while (i + 2 < data.length) {
329
- // eslint-disable-next-line unicorn/prefer-string-slice
330
- buffer.put(this.strToNum(data.substring(i, i + 3)), 10);
331
- i += 3;
332
- }
333
- if (i < data.length) {
334
- if (data.length - i === 1) {
335
- // eslint-disable-next-line unicorn/prefer-string-slice
336
- buffer.put(this.strToNum(data.substring(i, i + 1)), 4);
337
- }
338
- else if (data.length - i === 2) {
339
- // eslint-disable-next-line unicorn/prefer-string-slice
340
- buffer.put(this.strToNum(data.substring(i, i + 2)), 7);
341
- }
342
- }
343
- }
344
- /**
345
- * @internal
346
- */
347
- strToNum(s) {
348
- let num = 0;
349
- for (let i = 0; i < s.length; i++) {
350
- num = num * 10 + this.charToNum(s.charAt(i));
351
- }
352
- return num;
353
- }
354
- /**
355
- * @internal
356
- */
357
- charToNum(c) {
358
- if (c >= "0" && c <= "9") {
359
- return c.charCodeAt(0) - "0".charCodeAt(0);
360
- }
361
- throw new GeneralError(QRNumber._CLASS_NAME, "illegalCharacter", { value: c });
362
- }
363
- }
364
-
365
- // Copyright 2024 IOTA Stiftung.
366
- // SPDX-License-Identifier: Apache-2.0.
367
- /* eslint-disable no-bitwise */
368
- /**
369
- * Class for maintaining data bits.
370
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
371
- */
372
- class BitBuffer {
373
- /**
374
- * @internal
375
- */
376
- _buffer;
377
- /**
378
- * @internal
379
- */
380
- _length;
381
- /**
382
- * Create a new instance of BitBuffer.
383
- */
384
- constructor() {
385
- this._buffer = [];
386
- this._length = 0;
387
- }
388
- /**
389
- * Get the buffer.
390
- * @returns The buffer.
391
- */
392
- getBuffer() {
393
- return this._buffer;
394
- }
395
- /**
396
- * Get the length in bits.
397
- * @returns The length.
398
- */
399
- getLengthInBits() {
400
- return this._length;
401
- }
402
- /**
403
- * Put a value in to the bit buffer.
404
- * @param num The value.
405
- * @param length The length.
406
- */
407
- put(num, length) {
408
- for (let i = 0; i < length; i++) {
409
- this.putBit(((num >>> (length - i - 1)) & 1) === 1);
410
- }
411
- }
412
- /**
413
- * Put a bit.
414
- * @param bit The bit to put.
415
- */
416
- putBit(bit) {
417
- if (this._length === this._buffer.length * 8) {
418
- this._buffer.push(0);
419
- }
420
- if (bit) {
421
- this._buffer[~~(this._length / 8)] |= 0x80 >>> this._length % 8;
422
- }
423
- this._length++;
424
- }
425
- /**
426
- * Convert to string.
427
- * @returns The buffer as string.
428
- */
429
- toString() {
430
- let buffer = "";
431
- for (let i = 0; i < this.getLengthInBits(); i++) {
432
- buffer += this.getBit(i) ? "1" : "0";
433
- }
434
- return buffer;
435
- }
436
- /**
437
- * @internal
438
- */
439
- getBit(index) {
440
- return ((this._buffer[~~(index / 8)] >>> (7 - (index % 8))) & 1) === 1;
441
- }
442
- }
443
-
444
- // Copyright 2024 IOTA Stiftung.
445
- // SPDX-License-Identifier: Apache-2.0.
446
- /* eslint-disable no-bitwise */
447
- /**
448
- * Class to helper with math.
449
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
450
- */
451
- class MathHelper {
452
- /**
453
- * Runtime name for the class.
454
- * @internal
455
- */
456
- static _CLASS_NAME = "MathHelper";
457
- /**
458
- * @internal
459
- */
460
- static _EXP_TABLE;
461
- /**
462
- * @internal
463
- */
464
- static _LOG_TABLE;
465
- /**
466
- * Initialize the math helper.
467
- */
468
- static initialize() {
469
- if (!MathHelper._EXP_TABLE) {
470
- MathHelper._EXP_TABLE = [];
471
- MathHelper._LOG_TABLE = [];
472
- for (let i = 0; i < 256; i++) {
473
- MathHelper._EXP_TABLE.push(i < 8
474
- ? 1 << i
475
- : MathHelper._EXP_TABLE[i - 4] ^
476
- MathHelper._EXP_TABLE[i - 5] ^
477
- MathHelper._EXP_TABLE[i - 6] ^
478
- MathHelper._EXP_TABLE[i - 8]);
479
- MathHelper._LOG_TABLE.push(0);
480
- }
481
- for (let i = 0; i < 255; i++) {
482
- MathHelper._LOG_TABLE[MathHelper._EXP_TABLE[i]] = i;
483
- }
484
- }
485
- }
486
- /**
487
- * Get the log of the value.
488
- * @param value The value to get the log of.
489
- * @returns The log of the value.
490
- * @throws Error if value < 1.
491
- */
492
- static gLog(value) {
493
- if (value < 1) {
494
- throw new GeneralError(MathHelper._CLASS_NAME, "lessThanOne", { value });
495
- }
496
- return MathHelper._LOG_TABLE[value];
497
- }
498
- /**
499
- * Get the exponent of the value.
500
- * @param value The value to get the exponent of.
501
- * @returns The exponent of the value.
502
- */
503
- static gExp(value) {
504
- let localValue = value;
505
- while (localValue < 0) {
506
- localValue += 255;
507
- }
508
- while (localValue >= 256) {
509
- localValue -= 255;
510
- }
511
- return MathHelper._EXP_TABLE[localValue];
512
- }
513
- }
514
-
515
- // Copyright 2024 IOTA Stiftung.
516
- // SPDX-License-Identifier: Apache-2.0.
517
- /* eslint-disable no-bitwise */
518
- /**
519
- * Class to represent a polynomial.
520
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
521
- */
522
- class Polynomial {
523
- /**
524
- * @internal
525
- */
526
- _num;
527
- /**
528
- * Create a new instance of Polynomial.
529
- * @param num The num of the polynomial.
530
- * @param shift The shift for the polynomial.
531
- */
532
- constructor(num, shift = 0) {
533
- let offset = 0;
534
- while (offset < num.length && num[offset] === 0) {
535
- offset++;
536
- }
537
- this._num = [];
538
- const len = num.length - offset;
539
- for (let i = 0; i < len; i++) {
540
- this._num.push(num[offset + i]);
541
- }
542
- for (let i = 0; i < shift; i++) {
543
- this._num.push(0);
544
- }
545
- }
546
- /**
547
- * The the value of the polynomial at given index.
548
- * @param index The index.
549
- * @returns The value of the polynomial.
550
- */
551
- getAt(index) {
552
- return this._num[index];
553
- }
554
- /**
555
- * Get the length of the polynomial.
556
- * @returns The polynomial.
557
- */
558
- getLength() {
559
- return this._num.length;
560
- }
561
- /**
562
- * Convert the polynomial to a string.
563
- * @returns The string representation of the polynomial.
564
- */
565
- toString() {
566
- let buffer = "";
567
- for (let i = 0; i < this.getLength(); i++) {
568
- if (i > 0) {
569
- buffer += ",";
570
- }
571
- buffer += this.getAt(i);
572
- }
573
- return buffer.toString();
574
- }
575
- /**
576
- * Get the log representation of the polynomial.
577
- * @returns The log representation of the polynomial.
578
- */
579
- toLogString() {
580
- let buffer = "";
581
- for (let i = 0; i < this.getLength(); i++) {
582
- if (i > 0) {
583
- buffer += ",";
584
- }
585
- buffer += MathHelper.gLog(this.getAt(i));
586
- }
587
- return buffer.toString();
588
- }
589
- /**
590
- * Multiply the polynomial with another one.
591
- * @param e The polynomial to multiply.
592
- * @returns The multiplication of the polynomials.
593
- */
594
- multiply(e) {
595
- const num = [];
596
- const len = this.getLength() + e.getLength() - 1;
597
- for (let i = 0; i < len; i++) {
598
- num.push(0);
599
- }
600
- for (let i = 0; i < this.getLength(); i++) {
601
- for (let j = 0; j < e.getLength(); j++) {
602
- num[i + j] ^= MathHelper.gExp(MathHelper.gLog(this.getAt(i)) + MathHelper.gLog(e.getAt(j)));
603
- }
604
- }
605
- return new Polynomial(num);
606
- }
607
- /**
608
- * Get the modulus of the polynomial from another.
609
- * @param e The polynomial.
610
- * @returns The modules of the polynomials.
611
- */
612
- mod(e) {
613
- if (this.getLength() - e.getLength() < 0) {
614
- return this;
615
- }
616
- const ratio = MathHelper.gLog(this.getAt(0)) - MathHelper.gLog(e.getAt(0));
617
- // create copy
618
- const num = [];
619
- for (let i = 0; i < this.getLength(); i++) {
620
- num.push(this.getAt(i));
621
- }
622
- // subtract and calc rest.
623
- for (let i = 0; i < e.getLength(); i++) {
624
- num[i] ^= MathHelper.gExp(MathHelper.gLog(e.getAt(i)) + ratio);
625
- }
626
- // call recursively
627
- return new Polynomial(num).mod(e);
628
- }
629
- }
630
-
631
- // Copyright 2024 IOTA Stiftung.
632
- // SPDX-License-Identifier: Apache-2.0.
633
- /**
634
- * Mask patterns for QR codes.
635
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
636
- */
637
- // eslint-disable-next-line @typescript-eslint/naming-convention
638
- const MaskPattern = {
639
- /**
640
- * Mask pattern 000.
641
- */
642
- PATTERN000: 0b000,
643
- /**
644
- * Mask pattern 001.
645
- */
646
- PATTERN001: 0b001,
647
- /**
648
- * Mask pattern 010.
649
- */
650
- PATTERN010: 0b010,
651
- /**
652
- * Mask pattern 011.
653
- */
654
- PATTERN011: 0b011,
655
- /**
656
- * Mask pattern 100.
657
- */
658
- PATTERN100: 0b100,
659
- /**
660
- * Mask pattern 101.
661
- */
662
- PATTERN101: 0b101,
663
- /**
664
- * Mask pattern 110.
665
- */
666
- PATTERN110: 0b110,
667
- /**
668
- * Mask pattern 111.
669
- */
670
- PATTERN111: 0b111
671
- };
672
-
673
- // Copyright 2024 IOTA Stiftung.
674
- // SPDX-License-Identifier: Apache-2.0.
675
- /* eslint-disable no-bitwise */
676
- /**
677
- * Helper methods for QR generation.
678
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
679
- */
680
- class QRHelper {
681
- /**
682
- * Runtime name for the class.
683
- * @internal
684
- */
685
- static _CLASS_NAME = "QRHelper";
686
- /**
687
- * @internal
688
- */
689
- static _PATTERN_POSITION_TABLE = [
690
- [],
691
- [6, 18],
692
- [6, 22],
693
- [6, 26],
694
- [6, 30],
695
- [6, 34],
696
- [6, 22, 38],
697
- [6, 24, 42],
698
- [6, 26, 46],
699
- [6, 28, 50],
700
- [6, 30, 54],
701
- [6, 32, 58],
702
- [6, 34, 62],
703
- [6, 26, 46, 66],
704
- [6, 26, 48, 70],
705
- [6, 26, 50, 74],
706
- [6, 30, 54, 78],
707
- [6, 30, 56, 82],
708
- [6, 30, 58, 86],
709
- [6, 34, 62, 90],
710
- [6, 28, 50, 72, 94],
711
- [6, 26, 50, 74, 98],
712
- [6, 30, 54, 78, 102],
713
- [6, 28, 54, 80, 106],
714
- [6, 32, 58, 84, 110],
715
- [6, 30, 58, 86, 114],
716
- [6, 34, 62, 90, 118],
717
- [6, 26, 50, 74, 98, 122],
718
- [6, 30, 54, 78, 102, 126],
719
- [6, 26, 52, 78, 104, 130],
720
- [6, 30, 56, 82, 108, 134],
721
- [6, 34, 60, 86, 112, 138],
722
- [6, 30, 58, 86, 114, 142],
723
- [6, 34, 62, 90, 118, 146],
724
- [6, 30, 54, 78, 102, 126, 150],
725
- [6, 24, 50, 76, 102, 128, 154],
726
- [6, 28, 54, 80, 106, 132, 158],
727
- [6, 32, 58, 84, 110, 136, 162],
728
- [6, 26, 54, 82, 110, 138, 166],
729
- [6, 30, 58, 86, 114, 142, 170]
730
- ];
731
- /**
732
- * @internal
733
- */
734
- static _MAX_LENGTH = [
735
- [
736
- [41, 25, 17, 10],
737
- [34, 20, 14, 8],
738
- [27, 16, 11, 7],
739
- [17, 10, 7, 4]
740
- ],
741
- [
742
- [77, 47, 32, 20],
743
- [63, 38, 26, 16],
744
- [48, 29, 20, 12],
745
- [34, 20, 14, 8]
746
- ],
747
- [
748
- [127, 77, 53, 32],
749
- [101, 61, 42, 26],
750
- [77, 47, 32, 20],
751
- [58, 35, 24, 15]
752
- ],
753
- [
754
- [187, 114, 78, 48],
755
- [149, 90, 62, 38],
756
- [111, 67, 46, 28],
757
- [82, 50, 34, 21]
758
- ],
759
- [
760
- [255, 154, 106, 65],
761
- [202, 122, 84, 52],
762
- [144, 87, 60, 37],
763
- [106, 64, 44, 27]
764
- ],
765
- [
766
- [322, 195, 134, 82],
767
- [255, 154, 106, 65],
768
- [178, 108, 74, 45],
769
- [139, 84, 58, 36]
770
- ],
771
- [
772
- [370, 224, 154, 95],
773
- [293, 178, 122, 75],
774
- [207, 125, 86, 53],
775
- [154, 93, 64, 39]
776
- ],
777
- [
778
- [461, 279, 192, 118],
779
- [365, 221, 152, 93],
780
- [259, 157, 108, 66],
781
- [202, 122, 84, 52]
782
- ],
783
- [
784
- [552, 335, 230, 141],
785
- [432, 262, 180, 111],
786
- [312, 189, 130, 80],
787
- [235, 143, 98, 60]
788
- ],
789
- [
790
- [652, 395, 271, 167],
791
- [513, 311, 213, 131],
792
- [364, 221, 151, 93],
793
- [288, 174, 119, 74]
794
- ]
795
- ];
796
- /**
797
- * @internal
798
- */
799
- static _G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
800
- /**
801
- * @internal
802
- */
803
- static _G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
804
- /**
805
- * @internal
806
- */
807
- static _G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
808
- /**
809
- * Get the pattern position for the given type number.
810
- * @param typeNumber The type number to get the pattern for.
811
- * @returns The pattern for the type number.
812
- */
813
- static getPatternPosition(typeNumber) {
814
- return QRHelper._PATTERN_POSITION_TABLE[typeNumber - 1];
815
- }
816
- /**
817
- * Get the max length of the data.
818
- * @param typeNumber The type number to get the max length for.
819
- * @param mode The data mode to get data max length for.
820
- * @param errorCorrectLevel The error correction to get the max length for.
821
- * @returns The max length.
822
- * @throws Error with unknown correction level.
823
- */
824
- static getMaxLength(typeNumber, mode, errorCorrectLevel) {
825
- const t = typeNumber - 1;
826
- let e = 0;
827
- let m = 0;
828
- switch (errorCorrectLevel) {
829
- case ErrorCorrectLevel.L:
830
- e = 0;
831
- break;
832
- case ErrorCorrectLevel.M:
833
- e = 1;
834
- break;
835
- case ErrorCorrectLevel.Q:
836
- e = 2;
837
- break;
838
- case ErrorCorrectLevel.H:
839
- e = 3;
840
- break;
841
- default:
842
- throw new GeneralError(QRHelper._CLASS_NAME, "correctionLevelRange", { errorCorrectLevel });
843
- }
844
- switch (mode) {
845
- case QRDataMode.Number:
846
- m = 0;
847
- break;
848
- case QRDataMode.AlphaNumeric:
849
- m = 1;
850
- break;
851
- case QRDataMode.Byte8:
852
- m = 2;
853
- break;
854
- default:
855
- throw new GeneralError(QRHelper._CLASS_NAME, "modeRange", { mode });
856
- }
857
- return QRHelper._MAX_LENGTH[t][e][m];
858
- }
859
- /**
860
- * Get the error correction polynomial for the error correct to length.
861
- * @param errorCorrectLength The error correction length to get the polynomial for.
862
- * @returns The polynomial for the error correction length.
863
- */
864
- static getErrorCorrectPolynomial(errorCorrectLength) {
865
- let a = new Polynomial([1]);
866
- for (let i = 0; i < errorCorrectLength; i++) {
867
- a = a.multiply(new Polynomial([1, MathHelper.gExp(i)]));
868
- }
869
- return a;
870
- }
871
- /**
872
- * Get the mask method for the given pattern.
873
- * @param maskPattern The pattern to get the mask for.
874
- * @returns The mask method for the pattern.
875
- * @throws Error with invalid mask.
876
- */
877
- static getMaskMethod(maskPattern) {
878
- switch (maskPattern) {
879
- case MaskPattern.PATTERN000:
880
- return (i, j) => (i + j) % 2 === 0;
881
- case MaskPattern.PATTERN001:
882
- return (i, j) => i % 2 === 0;
883
- case MaskPattern.PATTERN010:
884
- return (i, j) => j % 3 === 0;
885
- case MaskPattern.PATTERN011:
886
- return (i, j) => (i + j) % 3 === 0;
887
- case MaskPattern.PATTERN100:
888
- return (i, j) => (~~(i / 2) + ~~(j / 3)) % 2 === 0;
889
- case MaskPattern.PATTERN101:
890
- return (i, j) => ((i * j) % 2) + ((i * j) % 3) === 0;
891
- case MaskPattern.PATTERN110:
892
- return (i, j) => (((i * j) % 2) + ((i * j) % 3)) % 2 === 0;
893
- case MaskPattern.PATTERN111:
894
- return (i, j) => (((i * j) % 3) + ((i + j) % 2)) % 2 === 0;
895
- default:
896
- throw new GeneralError(QRHelper._CLASS_NAME, "maskPatternRange", { maskPattern });
897
- }
898
- }
899
- /**
900
- * Get the BCH type info.
901
- * @param data The data to get the BCH type info for.
902
- * @returns The type info.
903
- */
904
- static getBCHTypeInfo(data) {
905
- let d = data << 10;
906
- while (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G15) >= 0) {
907
- d ^= QRHelper._G15 << (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G15));
908
- }
909
- return ((data << 10) | d) ^ QRHelper._G15_MASK;
910
- }
911
- /**
912
- * Get the BCH type number.
913
- * @param data The data to get the BCH type number for.
914
- * @returns The type number.
915
- */
916
- static getBCHTypeNumber(data) {
917
- let d = data << 12;
918
- while (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G18) >= 0) {
919
- d ^= QRHelper._G18 << (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G18));
920
- }
921
- return (data << 12) | d;
922
- }
923
- /**
924
- * @internal
925
- */
926
- static getBCHDigit(data) {
927
- let localData = data;
928
- let digit = 0;
929
- while (localData !== 0) {
930
- digit++;
931
- localData >>>= 1;
932
- }
933
- return digit;
934
- }
935
- }
936
-
937
- // Copyright 2024 IOTA Stiftung.
938
- // SPDX-License-Identifier: Apache-2.0.
939
- /* eslint-disable no-mixed-operators */
940
- /**
941
- * Class to represent a RS Block.
942
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
943
- */
944
- class RSBlock {
945
- /**
946
- * Runtime name for the class.
947
- * @internal
948
- */
949
- static _CLASS_NAME = "RSBlock";
950
- /**
951
- * @internal
952
- */
953
- static _RS_BLOCK_TABLE = [
954
- // L
955
- // M
956
- // Q
957
- // H
958
- // 1
959
- [1, 26, 19],
960
- [1, 26, 16],
961
- [1, 26, 13],
962
- [1, 26, 9],
963
- // 2
964
- [1, 44, 34],
965
- [1, 44, 28],
966
- [1, 44, 22],
967
- [1, 44, 16],
968
- // 3
969
- [1, 70, 55],
970
- [1, 70, 44],
971
- [2, 35, 17],
972
- [2, 35, 13],
973
- // 4
974
- [1, 100, 80],
975
- [2, 50, 32],
976
- [2, 50, 24],
977
- [4, 25, 9],
978
- // 5
979
- [1, 134, 108],
980
- [2, 67, 43],
981
- [2, 33, 15, 2, 34, 16],
982
- [2, 33, 11, 2, 34, 12],
983
- // 6
984
- [2, 86, 68],
985
- [4, 43, 27],
986
- [4, 43, 19],
987
- [4, 43, 15],
988
- // 7
989
- [2, 98, 78],
990
- [4, 49, 31],
991
- [2, 32, 14, 4, 33, 15],
992
- [4, 39, 13, 1, 40, 14],
993
- // 8
994
- [2, 121, 97],
995
- [2, 60, 38, 2, 61, 39],
996
- [4, 40, 18, 2, 41, 19],
997
- [4, 40, 14, 2, 41, 15],
998
- // 9
999
- [2, 146, 116],
1000
- [3, 58, 36, 2, 59, 37],
1001
- [4, 36, 16, 4, 37, 17],
1002
- [4, 36, 12, 4, 37, 13],
1003
- // 10
1004
- [2, 86, 68, 2, 87, 69],
1005
- [4, 69, 43, 1, 70, 44],
1006
- [6, 43, 19, 2, 44, 20],
1007
- [6, 43, 15, 2, 44, 16],
1008
- // 11
1009
- [4, 101, 81],
1010
- [1, 80, 50, 4, 81, 51],
1011
- [4, 50, 22, 4, 51, 23],
1012
- [3, 36, 12, 8, 37, 13],
1013
- // 12
1014
- [2, 116, 92, 2, 117, 93],
1015
- [6, 58, 36, 2, 59, 37],
1016
- [4, 46, 20, 6, 47, 21],
1017
- [7, 42, 14, 4, 43, 15],
1018
- // 13
1019
- [4, 133, 107],
1020
- [8, 59, 37, 1, 60, 38],
1021
- [8, 44, 20, 4, 45, 21],
1022
- [12, 33, 11, 4, 34, 12],
1023
- // 14
1024
- [3, 145, 115, 1, 146, 116],
1025
- [4, 64, 40, 5, 65, 41],
1026
- [11, 36, 16, 5, 37, 17],
1027
- [11, 36, 12, 5, 37, 13],
1028
- // 15
1029
- [5, 109, 87, 1, 110, 88],
1030
- [5, 65, 41, 5, 66, 42],
1031
- [5, 54, 24, 7, 55, 25],
1032
- [11, 36, 12, 7, 37, 13],
1033
- // 16
1034
- [5, 122, 98, 1, 123, 99],
1035
- [7, 73, 45, 3, 74, 46],
1036
- [15, 43, 19, 2, 44, 20],
1037
- [3, 45, 15, 13, 46, 16],
1038
- // 17
1039
- [1, 135, 107, 5, 136, 108],
1040
- [10, 74, 46, 1, 75, 47],
1041
- [1, 50, 22, 15, 51, 23],
1042
- [2, 42, 14, 17, 43, 15],
1043
- // 18
1044
- [5, 150, 120, 1, 151, 121],
1045
- [9, 69, 43, 4, 70, 44],
1046
- [17, 50, 22, 1, 51, 23],
1047
- [2, 42, 14, 19, 43, 15],
1048
- // 19
1049
- [3, 141, 113, 4, 142, 114],
1050
- [3, 70, 44, 11, 71, 45],
1051
- [17, 47, 21, 4, 48, 22],
1052
- [9, 39, 13, 16, 40, 14],
1053
- // 20
1054
- [3, 135, 107, 5, 136, 108],
1055
- [3, 67, 41, 13, 68, 42],
1056
- [15, 54, 24, 5, 55, 25],
1057
- [15, 43, 15, 10, 44, 16],
1058
- // 21
1059
- [4, 144, 116, 4, 145, 117],
1060
- [17, 68, 42],
1061
- [17, 50, 22, 6, 51, 23],
1062
- [19, 46, 16, 6, 47, 17],
1063
- // 22
1064
- [2, 139, 111, 7, 140, 112],
1065
- [17, 74, 46],
1066
- [7, 54, 24, 16, 55, 25],
1067
- [34, 37, 13],
1068
- // 23
1069
- [4, 151, 121, 5, 152, 122],
1070
- [4, 75, 47, 14, 76, 48],
1071
- [11, 54, 24, 14, 55, 25],
1072
- [16, 45, 15, 14, 46, 16],
1073
- // 24
1074
- [6, 147, 117, 4, 148, 118],
1075
- [6, 73, 45, 14, 74, 46],
1076
- [11, 54, 24, 16, 55, 25],
1077
- [30, 46, 16, 2, 47, 17],
1078
- // 25
1079
- [8, 132, 106, 4, 133, 107],
1080
- [8, 75, 47, 13, 76, 48],
1081
- [7, 54, 24, 22, 55, 25],
1082
- [22, 45, 15, 13, 46, 16],
1083
- // 26
1084
- [10, 142, 114, 2, 143, 115],
1085
- [19, 74, 46, 4, 75, 47],
1086
- [28, 50, 22, 6, 51, 23],
1087
- [33, 46, 16, 4, 47, 17],
1088
- // 27
1089
- [8, 152, 122, 4, 153, 123],
1090
- [22, 73, 45, 3, 74, 46],
1091
- [8, 53, 23, 26, 54, 24],
1092
- [12, 45, 15, 28, 46, 16],
1093
- // 28
1094
- [3, 147, 117, 10, 148, 118],
1095
- [3, 73, 45, 23, 74, 46],
1096
- [4, 54, 24, 31, 55, 25],
1097
- [11, 45, 15, 31, 46, 16],
1098
- // 29
1099
- [7, 146, 116, 7, 147, 117],
1100
- [21, 73, 45, 7, 74, 46],
1101
- [1, 53, 23, 37, 54, 24],
1102
- [19, 45, 15, 26, 46, 16],
1103
- // 30
1104
- [5, 145, 115, 10, 146, 116],
1105
- [19, 75, 47, 10, 76, 48],
1106
- [15, 54, 24, 25, 55, 25],
1107
- [23, 45, 15, 25, 46, 16],
1108
- // 31
1109
- [13, 145, 115, 3, 146, 116],
1110
- [2, 74, 46, 29, 75, 47],
1111
- [42, 54, 24, 1, 55, 25],
1112
- [23, 45, 15, 28, 46, 16],
1113
- // 32
1114
- [17, 145, 115],
1115
- [10, 74, 46, 23, 75, 47],
1116
- [10, 54, 24, 35, 55, 25],
1117
- [19, 45, 15, 35, 46, 16],
1118
- // 33
1119
- [17, 145, 115, 1, 146, 116],
1120
- [14, 74, 46, 21, 75, 47],
1121
- [29, 54, 24, 19, 55, 25],
1122
- [11, 45, 15, 46, 46, 16],
1123
- // 34
1124
- [13, 145, 115, 6, 146, 116],
1125
- [14, 74, 46, 23, 75, 47],
1126
- [44, 54, 24, 7, 55, 25],
1127
- [59, 46, 16, 1, 47, 17],
1128
- // 35
1129
- [12, 151, 121, 7, 152, 122],
1130
- [12, 75, 47, 26, 76, 48],
1131
- [39, 54, 24, 14, 55, 25],
1132
- [22, 45, 15, 41, 46, 16],
1133
- // 36
1134
- [6, 151, 121, 14, 152, 122],
1135
- [6, 75, 47, 34, 76, 48],
1136
- [46, 54, 24, 10, 55, 25],
1137
- [2, 45, 15, 64, 46, 16],
1138
- // 37
1139
- [17, 152, 122, 4, 153, 123],
1140
- [29, 74, 46, 14, 75, 47],
1141
- [49, 54, 24, 10, 55, 25],
1142
- [24, 45, 15, 46, 46, 16],
1143
- // 38
1144
- [4, 152, 122, 18, 153, 123],
1145
- [13, 74, 46, 32, 75, 47],
1146
- [48, 54, 24, 14, 55, 25],
1147
- [42, 45, 15, 32, 46, 16],
1148
- // 39
1149
- [20, 147, 117, 4, 148, 118],
1150
- [40, 75, 47, 7, 76, 48],
1151
- [43, 54, 24, 22, 55, 25],
1152
- [10, 45, 15, 67, 46, 16],
1153
- // 40
1154
- [19, 148, 118, 6, 149, 119],
1155
- [18, 75, 47, 31, 76, 48],
1156
- [34, 54, 24, 34, 55, 25],
1157
- [20, 45, 15, 61, 46, 16]
1158
- ];
1159
- /**
1160
- * @internal
1161
- */
1162
- _totalCount;
1163
- /**
1164
- * @internal
1165
- */
1166
- _dataCount;
1167
- /**
1168
- * Create a new instance of RSBlock.
1169
- * @param totalCount The total count for the block.
1170
- * @param dataCount The data count for the block.
1171
- */
1172
- constructor(totalCount, dataCount) {
1173
- this._totalCount = totalCount;
1174
- this._dataCount = dataCount;
1175
- }
1176
- /**
1177
- * Get RS Blocks for the type number and error correct level.
1178
- * @param typeNumber The type number.
1179
- * @param errorCorrectLevel The error correct level.
1180
- * @returns The RS Blocks.
1181
- */
1182
- static getRSBlocks(typeNumber, errorCorrectLevel) {
1183
- const rsBlock = RSBlock.getRsBlockTable(typeNumber, errorCorrectLevel);
1184
- const length = rsBlock.length / 3;
1185
- const list = [];
1186
- for (let i = 0; i < length; i++) {
1187
- const count = rsBlock[i * 3 + 0];
1188
- const totalCount = rsBlock[i * 3 + 1];
1189
- const dataCount = rsBlock[i * 3 + 2];
1190
- for (let j = 0; j < count; j++) {
1191
- list.push(new RSBlock(totalCount, dataCount));
1192
- }
1193
- }
1194
- return list;
1195
- }
1196
- /**
1197
- * @internal
1198
- */
1199
- static getRsBlockTable(typeNumber, errorCorrectLevel) {
1200
- switch (errorCorrectLevel) {
1201
- case ErrorCorrectLevel.L:
1202
- return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
1203
- case ErrorCorrectLevel.M:
1204
- return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
1205
- case ErrorCorrectLevel.Q:
1206
- return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
1207
- case ErrorCorrectLevel.H:
1208
- return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
1209
- }
1210
- throw new GeneralError(RSBlock._CLASS_NAME, "correctionLevelRange", { errorCorrectLevel });
1211
- }
1212
- /**
1213
- * Get the data count of the block.
1214
- * @returns The data count.
1215
- */
1216
- getDataCount() {
1217
- return this._dataCount;
1218
- }
1219
- /**
1220
- * Get the total count of blocks.
1221
- * @returns The total count.
1222
- */
1223
- getTotalCount() {
1224
- return this._totalCount;
1225
- }
1226
- }
1227
-
1228
- // Copyright 2024 IOTA Stiftung.
1229
- // SPDX-License-Identifier: Apache-2.0.
1230
- /* eslint-disable no-bitwise */
1231
- /* eslint-disable no-continue */
1232
- /* eslint-disable no-mixed-operators */
1233
- /**
1234
- * Class to generates QR codes from data.
1235
- * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
1236
- */
1237
- class QR {
1238
- /**
1239
- * Runtime name for the class.
1240
- * @internal
1241
- */
1242
- static _CLASS_NAME = "QR";
1243
- /**
1244
- * @internal
1245
- */
1246
- static _PAD0 = 0xec;
1247
- /**
1248
- * @internal
1249
- */
1250
- static _PAD1 = 0x11;
1251
- /**
1252
- * @internal
1253
- */
1254
- _typeNumber;
1255
- /**
1256
- * @internal
1257
- */
1258
- _errorCorrectLevel;
1259
- /**
1260
- * @internal
1261
- */
1262
- _qrData;
1263
- /**
1264
- * @internal
1265
- */
1266
- _modules;
1267
- /**
1268
- * @internal
1269
- */
1270
- _moduleCount;
1271
- /**
1272
- * Create a new instance of QR.
1273
- * @param typeNumber 0 to 40, 0 means autodetect.
1274
- * @param errorCorrectLevel 'L','M','Q','H'.
1275
- * @throws Error if the typeNumber is invalid.
1276
- */
1277
- constructor(typeNumber = 6, errorCorrectLevel = ErrorCorrectLevel.L) {
1278
- if (!Is.integer(typeNumber) || typeNumber < 0 || typeNumber > 40) {
1279
- throw new GeneralError(QR._CLASS_NAME, "typeNumberRange", { typeNumber });
1280
- }
1281
- this._typeNumber = typeNumber;
1282
- this._errorCorrectLevel = errorCorrectLevel;
1283
- this._qrData = [];
1284
- this._moduleCount = 0;
1285
- this._modules = [];
1286
- MathHelper.initialize();
1287
- }
1288
- /**
1289
- * Add text data to the QR Code.
1290
- * @param qrData The data to add.
1291
- */
1292
- addText(qrData) {
1293
- this._qrData.push(new QRByte8(qrData));
1294
- }
1295
- /**
1296
- * Add number to the QR Code.
1297
- * @param qrData The data to add.
1298
- */
1299
- addNumber(qrData) {
1300
- this._qrData.push(new QRNumber(qrData));
1301
- }
1302
- /**
1303
- * Add alpha numeric to the QR Code.
1304
- * @param qrData The data to add.
1305
- */
1306
- addAlphaNumeric(qrData) {
1307
- this._qrData.push(new QRAlphaNumeric(qrData));
1308
- }
1309
- /**
1310
- * Generate the display buffer.
1311
- * @returns Boolean buffer of pixels.
1312
- */
1313
- generate() {
1314
- this.autoDetectTypeNumber();
1315
- this.makeImpl(false, this.getBestMaskPattern());
1316
- const pixels = [];
1317
- for (let y = 0; y < this._moduleCount; y++) {
1318
- for (let x = 0; x < this._moduleCount; x++) {
1319
- pixels[x] = pixels[x] || [];
1320
- pixels[x][y] = this.isDark(y, x);
1321
- }
1322
- }
1323
- return pixels;
1324
- }
1325
- /**
1326
- * @internal
1327
- */
1328
- isDark(row, col) {
1329
- if (this._modules[row][col] !== null) {
1330
- return this._modules[row][col];
1331
- }
1332
- return false;
1333
- }
1334
- /**
1335
- * @internal
1336
- */
1337
- getBestMaskPattern() {
1338
- let minLostPoint = 0;
1339
- let pattern = 0;
1340
- for (let i = 0; i < 8; i++) {
1341
- this.makeImpl(true, i);
1342
- const lostPoint = this.getLostPoint();
1343
- if (i === 0 || minLostPoint > lostPoint) {
1344
- minLostPoint = lostPoint;
1345
- pattern = i;
1346
- }
1347
- }
1348
- return pattern;
1349
- }
1350
- /**
1351
- * @internal
1352
- */
1353
- makeImpl(test, maskPattern) {
1354
- this._moduleCount = this._typeNumber * 4 + 17;
1355
- this._modules = [];
1356
- for (let i = 0; i < this._moduleCount; i++) {
1357
- this._modules.push([]);
1358
- for (let j = 0; j < this._moduleCount; j++) {
1359
- this._modules[i].push(null);
1360
- }
1361
- }
1362
- this.setupPositionProbePattern(0, 0);
1363
- this.setupPositionProbePattern(this._moduleCount - 7, 0);
1364
- this.setupPositionProbePattern(0, this._moduleCount - 7);
1365
- this.setupPositionAdjustPattern();
1366
- this.setupTimingPattern();
1367
- this.setupTypeInfo(test, maskPattern);
1368
- if (this._typeNumber >= 7) {
1369
- this.setupTypeNumber(test);
1370
- }
1371
- const data = this.createData();
1372
- this.mapData(data, maskPattern);
1373
- }
1374
- /**
1375
- * @internal
1376
- */
1377
- mapData(data, maskPattern) {
1378
- let inc = -1;
1379
- let row = this._moduleCount - 1;
1380
- let bitIndex = 7;
1381
- let byteIndex = 0;
1382
- const maskFunc = QRHelper.getMaskMethod(maskPattern);
1383
- for (let col = this._moduleCount - 1; col > 0; col -= 2) {
1384
- if (col === 6) {
1385
- col -= 1;
1386
- }
1387
- let flag = true;
1388
- while (flag) {
1389
- for (let c = 0; c < 2; c++) {
1390
- if (this._modules[row][col - c] === null) {
1391
- let dark = false;
1392
- if (byteIndex < data.length) {
1393
- dark = ((data[byteIndex] >>> bitIndex) & 1) === 1;
1394
- }
1395
- const mask = maskFunc(row, col - c);
1396
- if (mask) {
1397
- dark = !dark;
1398
- }
1399
- this._modules[row][col - c] = dark;
1400
- bitIndex -= 1;
1401
- if (bitIndex === -1) {
1402
- byteIndex++;
1403
- bitIndex = 7;
1404
- }
1405
- }
1406
- }
1407
- row += inc;
1408
- if (row < 0 || this._moduleCount <= row) {
1409
- row -= inc;
1410
- inc = -inc;
1411
- flag = false;
1412
- }
1413
- }
1414
- }
1415
- }
1416
- /**
1417
- * @internal
1418
- */
1419
- setupPositionAdjustPattern() {
1420
- const pos = QRHelper.getPatternPosition(this._typeNumber);
1421
- for (let i = 0; i < pos.length; i++) {
1422
- for (let j = 0; j < pos.length; j++) {
1423
- const row = pos[i];
1424
- const col = pos[j];
1425
- if (this._modules[row][col] !== null) {
1426
- continue;
1427
- }
1428
- for (let r = -2; r <= 2; r++) {
1429
- for (let c = -2; c <= 2; c++) {
1430
- if (r === -2 || r === 2 || c === -2 || c === 2 || (r === 0 && c === 0)) {
1431
- this._modules[row + r][col + c] = true;
1432
- }
1433
- else {
1434
- this._modules[row + r][col + c] = false;
1435
- }
1436
- }
1437
- }
1438
- }
1439
- }
1440
- }
1441
- /**
1442
- * @internal
1443
- */
1444
- setupPositionProbePattern(row, col) {
1445
- for (let r = -1; r <= 7; r++) {
1446
- for (let c = -1; c <= 7; c++) {
1447
- if (row + r <= -1 ||
1448
- this._moduleCount <= row + r ||
1449
- col + c <= -1 ||
1450
- this._moduleCount <= col + c) {
1451
- continue;
1452
- }
1453
- if ((r >= 0 && r <= 6 && (c === 0 || c === 6)) ||
1454
- (c >= 0 && c <= 6 && (r === 0 || r === 6)) ||
1455
- (r >= 2 && r <= 4 && c >= 2 && c <= 4)) {
1456
- this._modules[row + r][col + c] = true;
1457
- }
1458
- else {
1459
- this._modules[row + r][col + c] = false;
1460
- }
1461
- }
1462
- }
1463
- }
1464
- /**
1465
- * @internal
1466
- */
1467
- setupTimingPattern() {
1468
- for (let r = 8; r < this._moduleCount - 8; r++) {
1469
- if (this._modules[r][6] !== null) {
1470
- continue;
1471
- }
1472
- this._modules[r][6] = r % 2 === 0;
1473
- }
1474
- for (let c = 8; c < this._moduleCount - 8; c++) {
1475
- if (this._modules[6][c] !== null) {
1476
- continue;
1477
- }
1478
- this._modules[6][c] = c % 2 === 0;
1479
- }
1480
- }
1481
- /**
1482
- * @internal
1483
- */
1484
- setupTypeNumber(test) {
1485
- const bits = QRHelper.getBCHTypeNumber(this._typeNumber);
1486
- for (let i = 0; i < 18; i++) {
1487
- this._modules[~~(i / 3)][(i % 3) + this._moduleCount - 8 - 3] =
1488
- !test && ((bits >> i) & 1) === 1;
1489
- }
1490
- for (let i = 0; i < 18; i++) {
1491
- this._modules[(i % 3) + this._moduleCount - 8 - 3][~~(i / 3)] =
1492
- !test && ((bits >> i) & 1) === 1;
1493
- }
1494
- }
1495
- /**
1496
- * @internal
1497
- */
1498
- setupTypeInfo(test, maskPattern) {
1499
- const data = (this._errorCorrectLevel << 3) | maskPattern;
1500
- const bits = QRHelper.getBCHTypeInfo(data);
1501
- // vertical
1502
- for (let i = 0; i < 15; i++) {
1503
- const mod = !test && ((bits >> i) & 1) === 1;
1504
- if (i < 6) {
1505
- this._modules[i][8] = mod;
1506
- }
1507
- else if (i < 8) {
1508
- this._modules[i + 1][8] = mod;
1509
- }
1510
- else {
1511
- this._modules[this._moduleCount - 15 + i][8] = mod;
1512
- }
1513
- }
1514
- // horizontal
1515
- for (let i = 0; i < 15; i++) {
1516
- const mod = !test && ((bits >> i) & 1) === 1;
1517
- if (i < 8) {
1518
- this._modules[8][this._moduleCount - i - 1] = mod;
1519
- }
1520
- else if (i < 9) {
1521
- this._modules[8][15 - i - 1 + 1] = mod;
1522
- }
1523
- else {
1524
- this._modules[8][15 - i - 1] = mod;
1525
- }
1526
- }
1527
- // fixed
1528
- this._modules[this._moduleCount - 8][8] = !test;
1529
- }
1530
- /**
1531
- * @internal
1532
- */
1533
- getLostPoint() {
1534
- const moduleCount = this._moduleCount;
1535
- let lostPoint = 0;
1536
- // LEVEL1
1537
- for (let row = 0; row < moduleCount; row++) {
1538
- for (let col = 0; col < moduleCount; col++) {
1539
- let sameCount = 0;
1540
- const dark = this.isDark(row, col);
1541
- for (let r = -1; r <= 1; r++) {
1542
- if (row + r < 0 || moduleCount <= row + r) {
1543
- continue;
1544
- }
1545
- for (let c = -1; c <= 1; c++) {
1546
- if (col + c < 0 || moduleCount <= col + c) {
1547
- continue;
1548
- }
1549
- if (r === 0 && c === 0) {
1550
- continue;
1551
- }
1552
- if (dark === this.isDark(row + r, col + c)) {
1553
- sameCount++;
1554
- }
1555
- }
1556
- }
1557
- if (sameCount > 5) {
1558
- lostPoint += 3 + sameCount - 5;
1559
- }
1560
- }
1561
- }
1562
- // LEVEL2
1563
- for (let row = 0; row < moduleCount - 1; row++) {
1564
- for (let col = 0; col < moduleCount - 1; col++) {
1565
- let count = 0;
1566
- if (this.isDark(row, col)) {
1567
- count++;
1568
- }
1569
- if (this.isDark(row + 1, col)) {
1570
- count++;
1571
- }
1572
- if (this.isDark(row, col + 1)) {
1573
- count++;
1574
- }
1575
- if (this.isDark(row + 1, col + 1)) {
1576
- count++;
1577
- }
1578
- if (count === 0 || count === 4) {
1579
- lostPoint += 3;
1580
- }
1581
- }
1582
- }
1583
- // LEVEL3
1584
- for (let row = 0; row < moduleCount; row++) {
1585
- for (let col = 0; col < moduleCount - 6; col++) {
1586
- if (this.isDark(row, col) &&
1587
- !this.isDark(row, col + 1) &&
1588
- this.isDark(row, col + 2) &&
1589
- this.isDark(row, col + 3) &&
1590
- this.isDark(row, col + 4) &&
1591
- !this.isDark(row, col + 5) &&
1592
- this.isDark(row, col + 6)) {
1593
- lostPoint += 40;
1594
- }
1595
- }
1596
- }
1597
- for (let col = 0; col < moduleCount; col++) {
1598
- for (let row = 0; row < moduleCount - 6; row++) {
1599
- if (this.isDark(row, col) &&
1600
- !this.isDark(row + 1, col) &&
1601
- this.isDark(row + 2, col) &&
1602
- this.isDark(row + 3, col) &&
1603
- this.isDark(row + 4, col) &&
1604
- !this.isDark(row + 5, col) &&
1605
- this.isDark(row + 6, col)) {
1606
- lostPoint += 40;
1607
- }
1608
- }
1609
- }
1610
- // LEVEL4
1611
- let darkCount = 0;
1612
- for (let col = 0; col < moduleCount; col++) {
1613
- for (let row = 0; row < moduleCount; row++) {
1614
- if (this.isDark(row, col)) {
1615
- darkCount++;
1616
- }
1617
- }
1618
- }
1619
- const ratio = Math.abs((100 * darkCount) / moduleCount / moduleCount - 50) / 5;
1620
- lostPoint += ratio * 10;
1621
- return lostPoint;
1622
- }
1623
- /**
1624
- * @internal
1625
- */
1626
- createData() {
1627
- const rsBlocks = RSBlock.getRSBlocks(this._typeNumber, this._errorCorrectLevel);
1628
- const buffer = new BitBuffer();
1629
- for (let i = 0; i < this._qrData.length; i++) {
1630
- const data = this._qrData[i];
1631
- buffer.put(data.getMode(), 4);
1632
- buffer.put(data.getLength(), data.getLengthInBits(this._typeNumber));
1633
- data.write(buffer);
1634
- }
1635
- // calc max data count
1636
- let totalDataCount = 0;
1637
- for (let i = 0; i < rsBlocks.length; i++) {
1638
- totalDataCount += rsBlocks[i].getDataCount();
1639
- }
1640
- if (buffer.getLengthInBits() > totalDataCount * 8) {
1641
- throw new GeneralError(QR._CLASS_NAME, "dataOverflow", {
1642
- lengthInBits: buffer.getLengthInBits(),
1643
- totalDataCount,
1644
- typeNumber: this._typeNumber
1645
- });
1646
- }
1647
- // end
1648
- if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
1649
- buffer.put(0, 4);
1650
- }
1651
- // padding
1652
- while (buffer.getLengthInBits() % 8 !== 0) {
1653
- buffer.putBit(false);
1654
- }
1655
- // padding
1656
- let flag = true;
1657
- while (flag) {
1658
- if (buffer.getLengthInBits() >= totalDataCount * 8) {
1659
- break;
1660
- }
1661
- buffer.put(QR._PAD0, 8);
1662
- if (buffer.getLengthInBits() >= totalDataCount * 8) {
1663
- flag = false;
1664
- }
1665
- else {
1666
- buffer.put(QR._PAD1, 8);
1667
- }
1668
- }
1669
- return this.createBytes(buffer, rsBlocks);
1670
- }
1671
- /**
1672
- * @internal
1673
- */
1674
- createBytes(buffer, rsBlocks) {
1675
- let offset = 0;
1676
- let maxDcCount = 0;
1677
- let maxEcCount = 0;
1678
- const dcData = [];
1679
- const ecData = [];
1680
- for (let r = 0; r < rsBlocks.length; r++) {
1681
- dcData.push([]);
1682
- ecData.push([]);
1683
- }
1684
- for (let r = 0; r < rsBlocks.length; r++) {
1685
- const dcCount = rsBlocks[r].getDataCount();
1686
- const ecCount = rsBlocks[r].getTotalCount() - dcCount;
1687
- maxDcCount = Math.max(maxDcCount, dcCount);
1688
- maxEcCount = Math.max(maxEcCount, ecCount);
1689
- dcData[r] = this.createNumArray(dcCount);
1690
- for (let i = 0; i < dcData[r].length; i++) {
1691
- dcData[r][i] = 0xff & buffer.getBuffer()[i + offset];
1692
- }
1693
- offset += dcCount;
1694
- const rsPoly = QRHelper.getErrorCorrectPolynomial(ecCount);
1695
- const rawPoly = new Polynomial(dcData[r], rsPoly.getLength() - 1);
1696
- const modPoly = rawPoly.mod(rsPoly);
1697
- ecData[r] = this.createNumArray(rsPoly.getLength() - 1);
1698
- for (let i = 0; i < ecData[r].length; i++) {
1699
- const modIndex = i + modPoly.getLength() - ecData[r].length;
1700
- ecData[r][i] = modIndex >= 0 ? modPoly.getAt(modIndex) : 0;
1701
- }
1702
- }
1703
- let totalCodeCount = 0;
1704
- for (let i = 0; i < rsBlocks.length; i++) {
1705
- totalCodeCount += rsBlocks[i].getTotalCount();
1706
- }
1707
- const data = this.createNumArray(totalCodeCount);
1708
- let index = 0;
1709
- for (let i = 0; i < maxDcCount; i++) {
1710
- for (let r = 0; r < rsBlocks.length; r++) {
1711
- if (i < dcData[r].length) {
1712
- data[index] = dcData[r][i];
1713
- index++;
1714
- }
1715
- }
1716
- }
1717
- for (let i = 0; i < maxEcCount; i++) {
1718
- for (let r = 0; r < rsBlocks.length; r++) {
1719
- if (i < ecData[r].length) {
1720
- data[index] = ecData[r][i];
1721
- index++;
1722
- }
1723
- }
1724
- }
1725
- return data;
1726
- }
1727
- /**
1728
- * @internal
1729
- */
1730
- createNumArray(len) {
1731
- const a = [];
1732
- for (let i = 0; i < len; i++) {
1733
- a.push(0);
1734
- }
1735
- return a;
1736
- }
1737
- /**
1738
- * @internal
1739
- */
1740
- autoDetectTypeNumber() {
1741
- if (this._typeNumber === 0) {
1742
- for (let typeNumber = 1; typeNumber <= 40; typeNumber++) {
1743
- const rsBlocks = RSBlock.getRSBlocks(typeNumber, this._errorCorrectLevel);
1744
- const buffer = new BitBuffer();
1745
- for (let i = 0; i < this._qrData.length; i++) {
1746
- const data = this._qrData[i];
1747
- buffer.put(data.getMode(), 4);
1748
- buffer.put(data.getLength(), data.getLengthInBits(typeNumber));
1749
- data.write(buffer);
1750
- }
1751
- let totalDataCount = 0;
1752
- for (let i = 0; i < rsBlocks.length; i++) {
1753
- totalDataCount += rsBlocks[i].getDataCount();
1754
- }
1755
- if (buffer.getLengthInBits() <= totalDataCount * 8) {
1756
- this._typeNumber = typeNumber;
1757
- break;
1758
- }
1759
- if (typeNumber === 40) {
1760
- throw new GeneralError(QR._CLASS_NAME, "typeNumberOverflow", {
1761
- lengthInBits: buffer.getLengthInBits(),
1762
- totalDataCount
1763
- });
1764
- }
1765
- }
1766
- }
1767
- }
1768
- }
1769
-
1770
- // Copyright 2024 IOTA Stiftung.
1771
- // SPDX-License-Identifier: Apache-2.0.
1772
- /* eslint-disable no-mixed-operators */
1773
- /**
1774
- * Class to render qr data as jpeg.
1775
- */
1776
- class JpegRenderer {
1777
- /**
1778
- * Runtime name for the class.
1779
- * @internal
1780
- */
1781
- static _CLASS_NAME = "JpegRenderer";
1782
- /**
1783
- * Render the QR code data as a bitmap.
1784
- * @param cellData The cell data for the QR code.
1785
- * @param options The options for rendering.
1786
- * @returns The bitmap content.
1787
- */
1788
- static async render(cellData, options) {
1789
- Guards.array(JpegRenderer._CLASS_NAME, "cellData", cellData);
1790
- options = options ?? {};
1791
- options.cellSize = options.cellSize ?? 5;
1792
- options.marginSize = options.marginSize ?? 10;
1793
- Guards.number("IRendererOptions", "options.cellSize", options?.cellSize);
1794
- Guards.number("IRendererOptions", "options.marginSize", options?.marginSize);
1795
- if (options?.cellSize <= 0) {
1796
- throw new GeneralError("IRendererOptions", "cellSizeZero", {
1797
- cellSize: options?.cellSize
1798
- });
1799
- }
1800
- if (options?.marginSize <= 0) {
1801
- throw new GeneralError("IRendererOptions", "marginSizeZero", {
1802
- marginSize: options?.marginSize
1803
- });
1804
- }
1805
- const background = Color.coerce(options?.background) ?? Color.fromHex("#FFFFFF");
1806
- const foreground = Color.coerce(options?.foreground) ?? Color.fromHex("#000000");
1807
- const dimensions = cellData.length * options?.cellSize + 2 * options?.marginSize;
1808
- const data = new Uint8Array(dimensions * dimensions * 4);
1809
- for (let i = 0; i < data.length; i += 4) {
1810
- data[i] = background.red();
1811
- data[i + 1] = background.green();
1812
- data[i + 2] = background.blue();
1813
- data[i + 3] = 0xff;
1814
- }
1815
- let dc = options?.marginSize * dimensions * 4;
1816
- for (let x = 0; x < cellData.length; x++) {
1817
- const row = new Uint8Array(dimensions * 4);
1818
- let r = 0;
1819
- for (let i = 0; i < options?.marginSize; i++) {
1820
- row[r++] = background.red();
1821
- row[r++] = background.green();
1822
- row[r++] = background.blue();
1823
- row[r++] = 0xff;
1824
- }
1825
- for (let y = 0; y < cellData[x].length; y++) {
1826
- const colour = cellData[y][x] ? foreground : background;
1827
- for (let c = 0; c < options.cellSize; c++) {
1828
- row[r++] = colour.red();
1829
- row[r++] = colour.green();
1830
- row[r++] = colour.blue();
1831
- row[r++] = 0xff;
1832
- }
1833
- }
1834
- for (let i = 0; i < options.marginSize; i++) {
1835
- row[r++] = background.red();
1836
- row[r++] = background.green();
1837
- row[r++] = background.blue();
1838
- row[r++] = 0xff;
1839
- }
1840
- for (let c = 0; c < options.cellSize; c++) {
1841
- data.set(row, dc);
1842
- dc += row.length;
1843
- }
1844
- }
1845
- return new JpegEncoder().encode(dimensions, dimensions, data, 75);
1846
- }
1847
- }
1848
-
1849
- // Copyright 2024 IOTA Stiftung.
1850
- // SPDX-License-Identifier: Apache-2.0.
1851
- /* eslint-disable no-mixed-operators */
1852
- /**
1853
- * Class to render qr data as png.
1854
- */
1855
- class PngRenderer {
1856
- /**
1857
- * Runtime name for the class.
1858
- * @internal
1859
- */
1860
- static _CLASS_NAME = "PngRenderer";
1861
- /**
1862
- * Render the QR code data as a bitmap.
1863
- * @param cellData The cell data for the QR code.
1864
- * @param options The options for rendering.
1865
- * @returns The bitmap content.
1866
- */
1867
- static async render(cellData, options) {
1868
- Guards.array(PngRenderer._CLASS_NAME, "cellData", cellData);
1869
- options = options ?? {};
1870
- options.cellSize = options.cellSize ?? 5;
1871
- options.marginSize = options.marginSize ?? 10;
1872
- Guards.number("IRendererOptions", "options.cellSize", options?.cellSize);
1873
- Guards.number("IRendererOptions", "options.marginSize", options?.marginSize);
1874
- if (options?.cellSize <= 0) {
1875
- throw new GeneralError("IRendererOptions", "cellSizeZero", {
1876
- cellSize: options?.cellSize
1877
- });
1878
- }
1879
- if (options?.marginSize <= 0) {
1880
- throw new GeneralError("IRendererOptions", "marginSizeZero", {
1881
- marginSize: options?.marginSize
1882
- });
1883
- }
1884
- const background = Color.coerce(options?.background) ?? Color.fromHex("#FFFFFF");
1885
- const foreground = Color.coerce(options?.foreground) ?? Color.fromHex("#000000");
1886
- const dimensions = cellData.length * options?.cellSize + 2 * options?.marginSize;
1887
- const data = new Uint8Array(dimensions * dimensions * 4);
1888
- for (let i = 0; i < data.length; i += 4) {
1889
- data[i] = background.red();
1890
- data[i + 1] = background.green();
1891
- data[i + 2] = background.blue();
1892
- data[i + 3] = background.alpha();
1893
- }
1894
- let dc = options?.marginSize * dimensions * 4;
1895
- for (let x = 0; x < cellData.length; x++) {
1896
- const row = new Uint8Array(dimensions * 4);
1897
- let r = 0;
1898
- for (let i = 0; i < options?.marginSize; i++) {
1899
- row[r++] = background.red();
1900
- row[r++] = background.green();
1901
- row[r++] = background.blue();
1902
- row[r++] = background.alpha();
1903
- }
1904
- for (let y = 0; y < cellData[x].length; y++) {
1905
- const colour = cellData[y][x] ? foreground : background;
1906
- for (let c = 0; c < options?.cellSize; c++) {
1907
- row[r++] = colour.red();
1908
- row[r++] = colour.green();
1909
- row[r++] = colour.blue();
1910
- row[r++] = colour.alpha();
1911
- }
1912
- }
1913
- for (let i = 0; i < options?.marginSize; i++) {
1914
- row[r++] = background.red();
1915
- row[r++] = background.green();
1916
- row[r++] = background.blue();
1917
- row[r++] = background.alpha();
1918
- }
1919
- for (let c = 0; c < options?.cellSize; c++) {
1920
- data.set(row, dc);
1921
- dc += row.length;
1922
- }
1923
- }
1924
- return new PngEncoder().encode([data.buffer], dimensions, dimensions);
1925
- }
1926
- }
1927
-
1928
- // Copyright 2024 IOTA Stiftung.
1929
- // SPDX-License-Identifier: Apache-2.0.
1930
- /**
1931
- * Class to render qr data as text.
1932
- */
1933
- class TextRenderer {
1934
- /**
1935
- * Runtime name for the class.
1936
- * @internal
1937
- */
1938
- static _CLASS_NAME = "TextRenderer";
1939
- /**
1940
- * Render the QR code data as text.
1941
- * @param cellData The cell data for the QR code.
1942
- * @param options The options for rendering.
1943
- * @returns The text content.
1944
- */
1945
- static async render(cellData, options) {
1946
- Guards.array(TextRenderer._CLASS_NAME, "cellData", cellData);
1947
- options = options ?? {};
1948
- options.cellSize = options.cellSize ?? 1;
1949
- options.marginSize = options.marginSize ?? 2;
1950
- Guards.number("IRendererOptions", "options.cellSize", options?.cellSize);
1951
- Guards.number("IRendererOptions", "options.marginSize", options?.marginSize);
1952
- if (options?.cellSize <= 0) {
1953
- throw new GeneralError("IRendererOptions", "cellSizeZero", {
1954
- cellSize: options?.cellSize
1955
- });
1956
- }
1957
- if (options?.marginSize <= 0) {
1958
- throw new GeneralError("IRendererOptions", "marginSizeZero", {
1959
- marginSize: options?.marginSize
1960
- });
1961
- }
1962
- options.onChar = options.onChar ?? "██";
1963
- options.offChar = options.offChar ?? " ";
1964
- const marginSize = options?.marginSize;
1965
- const cellSize = options?.cellSize;
1966
- let text = "";
1967
- for (let m = 0; m < marginSize; m++) {
1968
- text += `${options.offChar.repeat(cellSize * cellData.length)}\r\n`;
1969
- }
1970
- for (let x = 0; x < cellData.length; x++) {
1971
- let line = options.offChar.repeat(marginSize);
1972
- for (let y = 0; y < cellData[x].length; y++) {
1973
- if (cellData[y][x]) {
1974
- line += options.onChar.repeat(cellSize);
1975
- }
1976
- else {
1977
- line += options.offChar.repeat(cellSize);
1978
- }
1979
- }
1980
- line += options.offChar.repeat(marginSize);
1981
- line += "\r\n";
1982
- for (let c = 0; c < cellSize; c++) {
1983
- text += line;
1984
- }
1985
- }
1986
- for (let m = 0; m < marginSize; m++) {
1987
- text += `${options.offChar.repeat(cellSize * cellData.length)}\r\n`;
1988
- }
1989
- return text;
1990
- }
1991
- }
1992
-
1993
- export { ErrorCorrectLevel, JpegRenderer, PngRenderer, QR, TextRenderer };