@twin.org/qr 0.0.1-next.1

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