@twin.org/qr 0.0.2-next.9 → 0.0.3-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 (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 +276 -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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IRendererOptions.js","sourceRoot":"","sources":["../../../src/models/IRendererOptions.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Options for rendering.\n */\nexport interface IRendererOptions {\n\t/**\n\t * The cell size.\n\t */\n\tcellSize?: number;\n\n\t/**\n\t * The margin size.\n\t */\n\tmarginSize?: number;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ITextRendererOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ITextRendererOptions.js","sourceRoot":"","sources":["../../../src/models/ITextRendererOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IRendererOptions } from \"./IRendererOptions.js\";\n\n/**\n * Options for rendering as text.\n */\nexport interface ITextRendererOptions extends IRendererOptions {\n\t/**\n\t * The character to use for on pixels.\n\t */\n\tonChar?: string;\n\n\t/**\n\t * The character to use for off pixels.\n\t */\n\toffChar?: string;\n}\n"]}
@@ -0,0 +1,26 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /**
4
+ * Error correction level to use for the QR Code.
5
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
6
+ */
7
+ // eslint-disable-next-line @typescript-eslint/naming-convention
8
+ export const ErrorCorrectLevel = {
9
+ /**
10
+ * 7% Error correction.
11
+ */
12
+ L: 1,
13
+ /**
14
+ * 15% Error correction.
15
+ */
16
+ M: 0,
17
+ /**
18
+ * 25% Error correction.
19
+ */
20
+ Q: 3,
21
+ /**
22
+ * 30% Error correction.
23
+ */
24
+ H: 2
25
+ };
26
+ //# sourceMappingURL=errorCorrectLevel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorCorrectLevel.js","sourceRoot":"","sources":["../../../src/models/errorCorrectLevel.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC;;;GAGG;AACH,gEAAgE;AAChE,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAChC;;OAEG;IACH,CAAC,EAAE,CAAC;IAEJ;;OAEG;IACH,CAAC,EAAE,CAAC;IAEJ;;OAEG;IACH,CAAC,EAAE,CAAC;IAEJ;;OAEG;IACH,CAAC,EAAE,CAAC;CACK,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/**\n * Error correction level to use for the QR Code.\n * Based on https://github.com/kazuhikoarase/qrcode-generator/ .\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const ErrorCorrectLevel = {\n\t/**\n\t * 7% Error correction.\n\t */\n\tL: 1,\n\n\t/**\n\t * 15% Error correction.\n\t */\n\tM: 0,\n\n\t/**\n\t * 25% Error correction.\n\t */\n\tQ: 3,\n\n\t/**\n\t * 30% Error correction.\n\t */\n\tH: 2\n} as const;\n\n/**\n * Error correction level to use for the QR Code.\n * Based on https://github.com/kazuhikoarase/qrcode-generator/ .\n */\nexport type ErrorCorrectLevel = (typeof ErrorCorrectLevel)[keyof typeof ErrorCorrectLevel];\n"]}
@@ -0,0 +1,42 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /**
4
+ * Mask patterns for QR codes.
5
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
6
+ */
7
+ // eslint-disable-next-line @typescript-eslint/naming-convention
8
+ export const MaskPattern = {
9
+ /**
10
+ * Mask pattern 000.
11
+ */
12
+ PATTERN000: 0b000,
13
+ /**
14
+ * Mask pattern 001.
15
+ */
16
+ PATTERN001: 0b001,
17
+ /**
18
+ * Mask pattern 010.
19
+ */
20
+ PATTERN010: 0b010,
21
+ /**
22
+ * Mask pattern 011.
23
+ */
24
+ PATTERN011: 0b011,
25
+ /**
26
+ * Mask pattern 100.
27
+ */
28
+ PATTERN100: 0b100,
29
+ /**
30
+ * Mask pattern 101.
31
+ */
32
+ PATTERN101: 0b101,
33
+ /**
34
+ * Mask pattern 110.
35
+ */
36
+ PATTERN110: 0b110,
37
+ /**
38
+ * Mask pattern 111.
39
+ */
40
+ PATTERN111: 0b111
41
+ };
42
+ //# sourceMappingURL=maskPattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"maskPattern.js","sourceRoot":"","sources":["../../../src/models/maskPattern.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AAEvC;;;GAGG;AACH,gEAAgE;AAChE,MAAM,CAAC,MAAM,WAAW,GAAG;IAC1B;;OAEG;IACH,UAAU,EAAE,KAAK;IAEjB;;OAEG;IACH,UAAU,EAAE,KAAK;IAEjB;;OAEG;IACH,UAAU,EAAE,KAAK;IAEjB;;OAEG;IACH,UAAU,EAAE,KAAK;IAEjB;;OAEG;IACH,UAAU,EAAE,KAAK;IAEjB;;OAEG;IACH,UAAU,EAAE,KAAK;IAEjB;;OAEG;IACH,UAAU,EAAE,KAAK;IAEjB;;OAEG;IACH,UAAU,EAAE,KAAK;CACR,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Mask patterns for QR codes.\n * Based on https://github.com/kazuhikoarase/qrcode-generator/ .\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const MaskPattern = {\n\t/**\n\t * Mask pattern 000.\n\t */\n\tPATTERN000: 0b000,\n\n\t/**\n\t * Mask pattern 001.\n\t */\n\tPATTERN001: 0b001,\n\n\t/**\n\t * Mask pattern 010.\n\t */\n\tPATTERN010: 0b010,\n\n\t/**\n\t * Mask pattern 011.\n\t */\n\tPATTERN011: 0b011,\n\n\t/**\n\t * Mask pattern 100.\n\t */\n\tPATTERN100: 0b100,\n\n\t/**\n\t * Mask pattern 101.\n\t */\n\tPATTERN101: 0b101,\n\n\t/**\n\t * Mask pattern 110.\n\t */\n\tPATTERN110: 0b110,\n\n\t/**\n\t * Mask pattern 111.\n\t */\n\tPATTERN111: 0b111\n} as const;\n\n/**\n * Mask patterns for QR codes.\n * Based on https://github.com/kazuhikoarase/qrcode-generator/ .\n */\nexport type MaskPattern = (typeof MaskPattern)[keyof typeof MaskPattern];\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=qrCellData.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"qrCellData.js","sourceRoot":"","sources":["../../../src/models/qrCellData.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/**\n * Definition of type for QR Code Cell data.\n */\nexport type QRCellData = (boolean | null)[][];\n"]}
@@ -0,0 +1,22 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /**
4
+ * The mode for the qr data.
5
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
6
+ */
7
+ // eslint-disable-next-line @typescript-eslint/naming-convention
8
+ export const QRDataMode = {
9
+ /**
10
+ * Number.
11
+ */
12
+ Number: 1,
13
+ /**
14
+ * Alphabet and number.
15
+ */
16
+ AlphaNumeric: 2,
17
+ /**
18
+ * 8bit byte.
19
+ */
20
+ Byte8: 4
21
+ };
22
+ //# sourceMappingURL=qrDataMode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"qrDataMode.js","sourceRoot":"","sources":["../../../src/models/qrDataMode.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC;;;GAGG;AACH,gEAAgE;AAChE,MAAM,CAAC,MAAM,UAAU,GAAG;IACzB;;OAEG;IACH,MAAM,EAAE,CAAC;IAET;;OAEG;IACH,YAAY,EAAE,CAAC;IAEf;;OAEG;IACH,KAAK,EAAE,CAAC;CACC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/**\n * The mode for the qr data.\n * Based on https://github.com/kazuhikoarase/qrcode-generator/ .\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const QRDataMode = {\n\t/**\n\t * Number.\n\t */\n\tNumber: 1,\n\n\t/**\n\t * Alphabet and number.\n\t */\n\tAlphaNumeric: 2,\n\n\t/**\n\t * 8bit byte.\n\t */\n\tByte8: 4\n} as const;\n\n/**\n * The mode for the qr data.\n * Based on https://github.com/kazuhikoarase/qrcode-generator/ .\n */\nexport type QRDataMode = (typeof QRDataMode)[keyof typeof QRDataMode];\n"]}
package/dist/es/qr.js ADDED
@@ -0,0 +1,551 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /* eslint-disable no-bitwise */
4
+ /* eslint-disable no-continue */
5
+ /* eslint-disable no-mixed-operators */
6
+ import { GeneralError, Is } from "@twin.org/core";
7
+ import { QRAlphaNumeric } from "./data/qrAlphaNumeric.js";
8
+ import { QRByte8 } from "./data/qrByte8.js";
9
+ import { QRNumber } from "./data/qrNumber.js";
10
+ import { BitBuffer } from "./helpers/bitBuffer.js";
11
+ import { MathHelper } from "./helpers/mathHelper.js";
12
+ import { Polynomial } from "./helpers/polynomial.js";
13
+ import { QRHelper } from "./helpers/qrHelper.js";
14
+ import { RSBlock } from "./helpers/rsBlock.js";
15
+ import { ErrorCorrectLevel } from "./models/errorCorrectLevel.js";
16
+ /**
17
+ * Class to generates QR codes from data.
18
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
19
+ */
20
+ export class QR {
21
+ /**
22
+ * Runtime name for the class.
23
+ */
24
+ static CLASS_NAME = "QR";
25
+ /**
26
+ * @internal
27
+ */
28
+ static _PAD0 = 0xec;
29
+ /**
30
+ * @internal
31
+ */
32
+ static _PAD1 = 0x11;
33
+ /**
34
+ * @internal
35
+ */
36
+ _typeNumber;
37
+ /**
38
+ * @internal
39
+ */
40
+ _errorCorrectLevel;
41
+ /**
42
+ * @internal
43
+ */
44
+ _qrData;
45
+ /**
46
+ * @internal
47
+ */
48
+ _modules;
49
+ /**
50
+ * @internal
51
+ */
52
+ _moduleCount;
53
+ /**
54
+ * Create a new instance of QR.
55
+ * @param typeNumber 0 to 40, 0 means autodetect.
56
+ * @param errorCorrectLevel 'L','M','Q','H'.
57
+ * @throws Error if the typeNumber is invalid.
58
+ */
59
+ constructor(typeNumber = 6, errorCorrectLevel = ErrorCorrectLevel.L) {
60
+ if (!Is.integer(typeNumber) || typeNumber < 0 || typeNumber > 40) {
61
+ throw new GeneralError(QR.CLASS_NAME, "typeNumberRange", { typeNumber });
62
+ }
63
+ this._typeNumber = typeNumber;
64
+ this._errorCorrectLevel = errorCorrectLevel;
65
+ this._qrData = [];
66
+ this._moduleCount = 0;
67
+ this._modules = [];
68
+ MathHelper.initialize();
69
+ }
70
+ /**
71
+ * Add text data to the QR Code.
72
+ * @param qrData The data to add.
73
+ */
74
+ addText(qrData) {
75
+ this._qrData.push(new QRByte8(qrData));
76
+ }
77
+ /**
78
+ * Add number to the QR Code.
79
+ * @param qrData The data to add.
80
+ */
81
+ addNumber(qrData) {
82
+ this._qrData.push(new QRNumber(qrData));
83
+ }
84
+ /**
85
+ * Add alpha numeric to the QR Code.
86
+ * @param qrData The data to add.
87
+ */
88
+ addAlphaNumeric(qrData) {
89
+ this._qrData.push(new QRAlphaNumeric(qrData));
90
+ }
91
+ /**
92
+ * Generate the display buffer.
93
+ * @returns Boolean buffer of pixels.
94
+ */
95
+ generate() {
96
+ this.autoDetectTypeNumber();
97
+ this.makeImpl(false, this.getBestMaskPattern());
98
+ const pixels = [];
99
+ for (let y = 0; y < this._moduleCount; y++) {
100
+ for (let x = 0; x < this._moduleCount; x++) {
101
+ pixels[x] = pixels[x] || [];
102
+ pixels[x][y] = this.isDark(y, x);
103
+ }
104
+ }
105
+ return pixels;
106
+ }
107
+ /**
108
+ * @internal
109
+ */
110
+ isDark(row, col) {
111
+ if (this._modules[row][col] !== null) {
112
+ return this._modules[row][col];
113
+ }
114
+ return false;
115
+ }
116
+ /**
117
+ * @internal
118
+ */
119
+ getBestMaskPattern() {
120
+ let minLostPoint = 0;
121
+ let pattern = 0;
122
+ for (let i = 0; i < 8; i++) {
123
+ this.makeImpl(true, i);
124
+ const lostPoint = this.getLostPoint();
125
+ if (i === 0 || minLostPoint > lostPoint) {
126
+ minLostPoint = lostPoint;
127
+ pattern = i;
128
+ }
129
+ }
130
+ return pattern;
131
+ }
132
+ /**
133
+ * @internal
134
+ */
135
+ makeImpl(test, maskPattern) {
136
+ this._moduleCount = this._typeNumber * 4 + 17;
137
+ this._modules = [];
138
+ for (let i = 0; i < this._moduleCount; i++) {
139
+ this._modules.push([]);
140
+ for (let j = 0; j < this._moduleCount; j++) {
141
+ this._modules[i].push(null);
142
+ }
143
+ }
144
+ this.setupPositionProbePattern(0, 0);
145
+ this.setupPositionProbePattern(this._moduleCount - 7, 0);
146
+ this.setupPositionProbePattern(0, this._moduleCount - 7);
147
+ this.setupPositionAdjustPattern();
148
+ this.setupTimingPattern();
149
+ this.setupTypeInfo(test, maskPattern);
150
+ if (this._typeNumber >= 7) {
151
+ this.setupTypeNumber(test);
152
+ }
153
+ const data = this.createData();
154
+ this.mapData(data, maskPattern);
155
+ }
156
+ /**
157
+ * @internal
158
+ */
159
+ mapData(data, maskPattern) {
160
+ let inc = -1;
161
+ let row = this._moduleCount - 1;
162
+ let bitIndex = 7;
163
+ let byteIndex = 0;
164
+ const maskFunc = QRHelper.getMaskMethod(maskPattern);
165
+ for (let col = this._moduleCount - 1; col > 0; col -= 2) {
166
+ if (col === 6) {
167
+ col -= 1;
168
+ }
169
+ let flag = true;
170
+ while (flag) {
171
+ for (let c = 0; c < 2; c++) {
172
+ if (this._modules[row][col - c] === null) {
173
+ let dark = false;
174
+ if (byteIndex < data.length) {
175
+ dark = ((data[byteIndex] >>> bitIndex) & 1) === 1;
176
+ }
177
+ const mask = maskFunc(row, col - c);
178
+ if (mask) {
179
+ dark = !dark;
180
+ }
181
+ this._modules[row][col - c] = dark;
182
+ bitIndex -= 1;
183
+ if (bitIndex === -1) {
184
+ byteIndex++;
185
+ bitIndex = 7;
186
+ }
187
+ }
188
+ }
189
+ row += inc;
190
+ if (row < 0 || this._moduleCount <= row) {
191
+ row -= inc;
192
+ inc = -inc;
193
+ flag = false;
194
+ }
195
+ }
196
+ }
197
+ }
198
+ /**
199
+ * @internal
200
+ */
201
+ setupPositionAdjustPattern() {
202
+ const pos = QRHelper.getPatternPosition(this._typeNumber);
203
+ for (let i = 0; i < pos.length; i++) {
204
+ for (let j = 0; j < pos.length; j++) {
205
+ const row = pos[i];
206
+ const col = pos[j];
207
+ if (this._modules[row][col] !== null) {
208
+ continue;
209
+ }
210
+ for (let r = -2; r <= 2; r++) {
211
+ for (let c = -2; c <= 2; c++) {
212
+ if (r === -2 || r === 2 || c === -2 || c === 2 || (r === 0 && c === 0)) {
213
+ this._modules[row + r][col + c] = true;
214
+ }
215
+ else {
216
+ this._modules[row + r][col + c] = false;
217
+ }
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+ /**
224
+ * @internal
225
+ */
226
+ setupPositionProbePattern(row, col) {
227
+ for (let r = -1; r <= 7; r++) {
228
+ for (let c = -1; c <= 7; c++) {
229
+ if (row + r <= -1 ||
230
+ this._moduleCount <= row + r ||
231
+ col + c <= -1 ||
232
+ this._moduleCount <= col + c) {
233
+ continue;
234
+ }
235
+ if ((r >= 0 && r <= 6 && (c === 0 || c === 6)) ||
236
+ (c >= 0 && c <= 6 && (r === 0 || r === 6)) ||
237
+ (r >= 2 && r <= 4 && c >= 2 && c <= 4)) {
238
+ this._modules[row + r][col + c] = true;
239
+ }
240
+ else {
241
+ this._modules[row + r][col + c] = false;
242
+ }
243
+ }
244
+ }
245
+ }
246
+ /**
247
+ * @internal
248
+ */
249
+ setupTimingPattern() {
250
+ for (let r = 8; r < this._moduleCount - 8; r++) {
251
+ if (this._modules[r][6] !== null) {
252
+ continue;
253
+ }
254
+ this._modules[r][6] = r % 2 === 0;
255
+ }
256
+ for (let c = 8; c < this._moduleCount - 8; c++) {
257
+ if (this._modules[6][c] !== null) {
258
+ continue;
259
+ }
260
+ this._modules[6][c] = c % 2 === 0;
261
+ }
262
+ }
263
+ /**
264
+ * @internal
265
+ */
266
+ setupTypeNumber(test) {
267
+ const bits = QRHelper.getBCHTypeNumber(this._typeNumber);
268
+ for (let i = 0; i < 18; i++) {
269
+ this._modules[~~(i / 3)][(i % 3) + this._moduleCount - 8 - 3] =
270
+ !test && ((bits >> i) & 1) === 1;
271
+ }
272
+ for (let i = 0; i < 18; i++) {
273
+ this._modules[(i % 3) + this._moduleCount - 8 - 3][~~(i / 3)] =
274
+ !test && ((bits >> i) & 1) === 1;
275
+ }
276
+ }
277
+ /**
278
+ * @internal
279
+ */
280
+ setupTypeInfo(test, maskPattern) {
281
+ const data = (this._errorCorrectLevel << 3) | maskPattern;
282
+ const bits = QRHelper.getBCHTypeInfo(data);
283
+ // vertical
284
+ for (let i = 0; i < 15; i++) {
285
+ const mod = !test && ((bits >> i) & 1) === 1;
286
+ if (i < 6) {
287
+ this._modules[i][8] = mod;
288
+ }
289
+ else if (i < 8) {
290
+ this._modules[i + 1][8] = mod;
291
+ }
292
+ else {
293
+ this._modules[this._moduleCount - 15 + i][8] = mod;
294
+ }
295
+ }
296
+ // horizontal
297
+ for (let i = 0; i < 15; i++) {
298
+ const mod = !test && ((bits >> i) & 1) === 1;
299
+ if (i < 8) {
300
+ this._modules[8][this._moduleCount - i - 1] = mod;
301
+ }
302
+ else if (i < 9) {
303
+ this._modules[8][15 - i - 1 + 1] = mod;
304
+ }
305
+ else {
306
+ this._modules[8][15 - i - 1] = mod;
307
+ }
308
+ }
309
+ // fixed
310
+ this._modules[this._moduleCount - 8][8] = !test;
311
+ }
312
+ /**
313
+ * @internal
314
+ */
315
+ getLostPoint() {
316
+ const moduleCount = this._moduleCount;
317
+ let lostPoint = 0;
318
+ // LEVEL1
319
+ for (let row = 0; row < moduleCount; row++) {
320
+ for (let col = 0; col < moduleCount; col++) {
321
+ let sameCount = 0;
322
+ const dark = this.isDark(row, col);
323
+ for (let r = -1; r <= 1; r++) {
324
+ if (row + r < 0 || moduleCount <= row + r) {
325
+ continue;
326
+ }
327
+ for (let c = -1; c <= 1; c++) {
328
+ if (col + c < 0 || moduleCount <= col + c) {
329
+ continue;
330
+ }
331
+ if (r === 0 && c === 0) {
332
+ continue;
333
+ }
334
+ if (dark === this.isDark(row + r, col + c)) {
335
+ sameCount++;
336
+ }
337
+ }
338
+ }
339
+ if (sameCount > 5) {
340
+ lostPoint += 3 + sameCount - 5;
341
+ }
342
+ }
343
+ }
344
+ // LEVEL2
345
+ for (let row = 0; row < moduleCount - 1; row++) {
346
+ for (let col = 0; col < moduleCount - 1; col++) {
347
+ let count = 0;
348
+ if (this.isDark(row, col)) {
349
+ count++;
350
+ }
351
+ if (this.isDark(row + 1, col)) {
352
+ count++;
353
+ }
354
+ if (this.isDark(row, col + 1)) {
355
+ count++;
356
+ }
357
+ if (this.isDark(row + 1, col + 1)) {
358
+ count++;
359
+ }
360
+ if (count === 0 || count === 4) {
361
+ lostPoint += 3;
362
+ }
363
+ }
364
+ }
365
+ // LEVEL3
366
+ for (let row = 0; row < moduleCount; row++) {
367
+ for (let col = 0; col < moduleCount - 6; col++) {
368
+ if (this.isDark(row, col) &&
369
+ !this.isDark(row, col + 1) &&
370
+ this.isDark(row, col + 2) &&
371
+ this.isDark(row, col + 3) &&
372
+ this.isDark(row, col + 4) &&
373
+ !this.isDark(row, col + 5) &&
374
+ this.isDark(row, col + 6)) {
375
+ lostPoint += 40;
376
+ }
377
+ }
378
+ }
379
+ for (let col = 0; col < moduleCount; col++) {
380
+ for (let row = 0; row < moduleCount - 6; row++) {
381
+ if (this.isDark(row, col) &&
382
+ !this.isDark(row + 1, col) &&
383
+ this.isDark(row + 2, col) &&
384
+ this.isDark(row + 3, col) &&
385
+ this.isDark(row + 4, col) &&
386
+ !this.isDark(row + 5, col) &&
387
+ this.isDark(row + 6, col)) {
388
+ lostPoint += 40;
389
+ }
390
+ }
391
+ }
392
+ // LEVEL4
393
+ let darkCount = 0;
394
+ for (let col = 0; col < moduleCount; col++) {
395
+ for (let row = 0; row < moduleCount; row++) {
396
+ if (this.isDark(row, col)) {
397
+ darkCount++;
398
+ }
399
+ }
400
+ }
401
+ const ratio = Math.abs((100 * darkCount) / moduleCount / moduleCount - 50) / 5;
402
+ lostPoint += ratio * 10;
403
+ return lostPoint;
404
+ }
405
+ /**
406
+ * @internal
407
+ */
408
+ createData() {
409
+ const rsBlocks = RSBlock.getRSBlocks(this._typeNumber, this._errorCorrectLevel);
410
+ const buffer = new BitBuffer();
411
+ for (let i = 0; i < this._qrData.length; i++) {
412
+ const data = this._qrData[i];
413
+ buffer.put(data.getMode(), 4);
414
+ buffer.put(data.getLength(), data.getLengthInBits(this._typeNumber));
415
+ data.write(buffer);
416
+ }
417
+ // calc max data count
418
+ let totalDataCount = 0;
419
+ for (let i = 0; i < rsBlocks.length; i++) {
420
+ totalDataCount += rsBlocks[i].getDataCount();
421
+ }
422
+ if (buffer.getLengthInBits() > totalDataCount * 8) {
423
+ throw new GeneralError(QR.CLASS_NAME, "dataOverflow", {
424
+ lengthInBits: buffer.getLengthInBits(),
425
+ totalDataCount,
426
+ typeNumber: this._typeNumber
427
+ });
428
+ }
429
+ // end
430
+ if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
431
+ buffer.put(0, 4);
432
+ }
433
+ // padding
434
+ while (buffer.getLengthInBits() % 8 !== 0) {
435
+ buffer.putBit(false);
436
+ }
437
+ // padding
438
+ let flag = true;
439
+ while (flag) {
440
+ if (buffer.getLengthInBits() >= totalDataCount * 8) {
441
+ break;
442
+ }
443
+ buffer.put(QR._PAD0, 8);
444
+ if (buffer.getLengthInBits() >= totalDataCount * 8) {
445
+ flag = false;
446
+ }
447
+ else {
448
+ buffer.put(QR._PAD1, 8);
449
+ }
450
+ }
451
+ return this.createBytes(buffer, rsBlocks);
452
+ }
453
+ /**
454
+ * @internal
455
+ */
456
+ createBytes(buffer, rsBlocks) {
457
+ let offset = 0;
458
+ let maxDcCount = 0;
459
+ let maxEcCount = 0;
460
+ const dcData = [];
461
+ const ecData = [];
462
+ for (let r = 0; r < rsBlocks.length; r++) {
463
+ dcData.push([]);
464
+ ecData.push([]);
465
+ }
466
+ for (let r = 0; r < rsBlocks.length; r++) {
467
+ const dcCount = rsBlocks[r].getDataCount();
468
+ const ecCount = rsBlocks[r].getTotalCount() - dcCount;
469
+ maxDcCount = Math.max(maxDcCount, dcCount);
470
+ maxEcCount = Math.max(maxEcCount, ecCount);
471
+ dcData[r] = this.createNumArray(dcCount);
472
+ for (let i = 0; i < dcData[r].length; i++) {
473
+ dcData[r][i] = 0xff & buffer.getBuffer()[i + offset];
474
+ }
475
+ offset += dcCount;
476
+ const rsPoly = QRHelper.getErrorCorrectPolynomial(ecCount);
477
+ const rawPoly = new Polynomial(dcData[r], rsPoly.getLength() - 1);
478
+ const modPoly = rawPoly.mod(rsPoly);
479
+ ecData[r] = this.createNumArray(rsPoly.getLength() - 1);
480
+ for (let i = 0; i < ecData[r].length; i++) {
481
+ const modIndex = i + modPoly.getLength() - ecData[r].length;
482
+ ecData[r][i] = modIndex >= 0 ? modPoly.getAt(modIndex) : 0;
483
+ }
484
+ }
485
+ let totalCodeCount = 0;
486
+ for (let i = 0; i < rsBlocks.length; i++) {
487
+ totalCodeCount += rsBlocks[i].getTotalCount();
488
+ }
489
+ const data = this.createNumArray(totalCodeCount);
490
+ let index = 0;
491
+ for (let i = 0; i < maxDcCount; i++) {
492
+ for (let r = 0; r < rsBlocks.length; r++) {
493
+ if (i < dcData[r].length) {
494
+ data[index] = dcData[r][i];
495
+ index++;
496
+ }
497
+ }
498
+ }
499
+ for (let i = 0; i < maxEcCount; i++) {
500
+ for (let r = 0; r < rsBlocks.length; r++) {
501
+ if (i < ecData[r].length) {
502
+ data[index] = ecData[r][i];
503
+ index++;
504
+ }
505
+ }
506
+ }
507
+ return data;
508
+ }
509
+ /**
510
+ * @internal
511
+ */
512
+ createNumArray(len) {
513
+ const a = [];
514
+ for (let i = 0; i < len; i++) {
515
+ a.push(0);
516
+ }
517
+ return a;
518
+ }
519
+ /**
520
+ * @internal
521
+ */
522
+ autoDetectTypeNumber() {
523
+ if (this._typeNumber === 0) {
524
+ for (let typeNumber = 1; typeNumber <= 40; typeNumber++) {
525
+ const rsBlocks = RSBlock.getRSBlocks(typeNumber, this._errorCorrectLevel);
526
+ const buffer = new BitBuffer();
527
+ for (let i = 0; i < this._qrData.length; i++) {
528
+ const data = this._qrData[i];
529
+ buffer.put(data.getMode(), 4);
530
+ buffer.put(data.getLength(), data.getLengthInBits(typeNumber));
531
+ data.write(buffer);
532
+ }
533
+ let totalDataCount = 0;
534
+ for (let i = 0; i < rsBlocks.length; i++) {
535
+ totalDataCount += rsBlocks[i].getDataCount();
536
+ }
537
+ if (buffer.getLengthInBits() <= totalDataCount * 8) {
538
+ this._typeNumber = typeNumber;
539
+ break;
540
+ }
541
+ if (typeNumber === 40) {
542
+ throw new GeneralError(QR.CLASS_NAME, "typeNumberOverflow", {
543
+ lengthInBits: buffer.getLengthInBits(),
544
+ totalDataCount
545
+ });
546
+ }
547
+ }
548
+ }
549
+ }
550
+ }
551
+ //# sourceMappingURL=qr.js.map