@node-projects/web-component-designer-zpl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +15 -0
  2. package/dist/extensions/ZplLayoutResizeExtensionProvider.d.ts +4 -0
  3. package/dist/extensions/ZplLayoutResizeExtensionProvider.js +16 -0
  4. package/dist/index.d.ts +15 -0
  5. package/dist/index.js +15 -0
  6. package/dist/jsBarcodeOptions.d.ts +27 -0
  7. package/dist/jsBarcodeOptions.js +14 -0
  8. package/dist/qr.d.ts +11 -0
  9. package/dist/qr.js +759 -0
  10. package/dist/services/ZplImageDrop.d.ts +10 -0
  11. package/dist/services/ZplImageDrop.js +220 -0
  12. package/dist/services/ZplLayoutCopyPasteService.d.ts +6 -0
  13. package/dist/services/ZplLayoutCopyPasteService.js +19 -0
  14. package/dist/services/ZplLayoutPlacementService.d.ts +10 -0
  15. package/dist/services/ZplLayoutPlacementService.js +25 -0
  16. package/dist/services/ZplParserService.d.ts +6 -0
  17. package/dist/services/ZplParserService.js +213 -0
  18. package/dist/setupZplServiceContainer.d.ts +2 -0
  19. package/dist/setupZplServiceContainer.js +66 -0
  20. package/dist/widgets/zpl-barcode.d.ts +25 -0
  21. package/dist/widgets/zpl-barcode.js +92 -0
  22. package/dist/widgets/zpl-graphic-box.d.ts +25 -0
  23. package/dist/widgets/zpl-graphic-box.js +89 -0
  24. package/dist/widgets/zpl-graphic-circle.d.ts +23 -0
  25. package/dist/widgets/zpl-graphic-circle.js +71 -0
  26. package/dist/widgets/zpl-graphic-diagonal-line.d.ts +29 -0
  27. package/dist/widgets/zpl-graphic-diagonal-line.js +87 -0
  28. package/dist/widgets/zpl-image.d.ts +27 -0
  29. package/dist/widgets/zpl-image.js +120 -0
  30. package/dist/widgets/zpl-text.d.ts +25 -0
  31. package/dist/widgets/zpl-text.js +69 -0
  32. package/dist/zplHelper.d.ts +1 -0
  33. package/dist/zplHelper.js +5 -0
  34. package/package.json +23 -0
package/dist/qr.js ADDED
@@ -0,0 +1,759 @@
1
+ /* qr.js -- QR code generator in Javascript (revision 2011-01-19)
2
+ * Written by Kang Seonghoon <public+qrjs@mearie.org>.
3
+ *
4
+ * This source code is in the public domain; if your jurisdiction does not
5
+ * recognize the public domain the terms of Creative Commons CC0 license
6
+ * apply. In the other words, you can always do what you want.
7
+ */
8
+ /* Quick overview: QR code composed of 2D array of modules (a rectangular
9
+ * area that conveys one bit of information); some modules are fixed to help
10
+ * the recognition of the code, and remaining data modules are further divided
11
+ * into 8-bit code words which are augumented by Reed-Solomon error correcting
12
+ * codes (ECC). There could be multiple ECCs, in the case the code is so large
13
+ * that it is helpful to split the raw data into several chunks.
14
+ *
15
+ * The number of modules is determined by the code's "version", ranging from 1
16
+ * (21x21) to 40 (177x177). How many ECC bits are used is determined by the
17
+ * ECC level (L/M/Q/H). The number and size (and thus the order of generator
18
+ * polynomial) of ECCs depend to the version and ECC level.
19
+ */
20
+ // per-version information (cf. JIS X 0510:2004 pp. 30--36, 71)
21
+ //
22
+ // [0]: the degree of generator polynomial by ECC levels
23
+ // [1]: # of code blocks by ECC levels
24
+ // [2]: left-top positions of alignment patterns
25
+ //
26
+ // the number in this table (in particular, [0]) does not exactly match with
27
+ // the numbers in the specficiation. see augumenteccs below for the reason.
28
+ var VERSIONS = [
29
+ null,
30
+ [[10, 7, 17, 13], [1, 1, 1, 1], []],
31
+ [[16, 10, 28, 22], [1, 1, 1, 1], [4, 16]],
32
+ [[26, 15, 22, 18], [1, 1, 2, 2], [4, 20]],
33
+ [[18, 20, 16, 26], [2, 1, 4, 2], [4, 24]],
34
+ [[24, 26, 22, 18], [2, 1, 4, 4], [4, 28]],
35
+ [[16, 18, 28, 24], [4, 2, 4, 4], [4, 32]],
36
+ [[18, 20, 26, 18], [4, 2, 5, 6], [4, 20, 36]],
37
+ [[22, 24, 26, 22], [4, 2, 6, 6], [4, 22, 40]],
38
+ [[22, 30, 24, 20], [5, 2, 8, 8], [4, 24, 44]],
39
+ [[26, 18, 28, 24], [5, 4, 8, 8], [4, 26, 48]],
40
+ [[30, 20, 24, 28], [5, 4, 11, 8], [4, 28, 52]],
41
+ [[22, 24, 28, 26], [8, 4, 11, 10], [4, 30, 56]],
42
+ [[22, 26, 22, 24], [9, 4, 16, 12], [4, 32, 60]],
43
+ [[24, 30, 24, 20], [9, 4, 16, 16], [4, 24, 44, 64]],
44
+ [[24, 22, 24, 30], [10, 6, 18, 12], [4, 24, 46, 68]],
45
+ [[28, 24, 30, 24], [10, 6, 16, 17], [4, 24, 48, 72]],
46
+ [[28, 28, 28, 28], [11, 6, 19, 16], [4, 28, 52, 76]],
47
+ [[26, 30, 28, 28], [13, 6, 21, 18], [4, 28, 54, 80]],
48
+ [[26, 28, 26, 26], [14, 7, 25, 21], [4, 28, 56, 84]],
49
+ [[26, 28, 28, 30], [16, 8, 25, 20], [4, 32, 60, 88]],
50
+ [[26, 28, 30, 28], [17, 8, 25, 23], [4, 26, 48, 70, 92]],
51
+ [[28, 28, 24, 30], [17, 9, 34, 23], [4, 24, 48, 72, 96]],
52
+ [[28, 30, 30, 30], [18, 9, 30, 25], [4, 28, 52, 76, 100]],
53
+ [[28, 30, 30, 30], [20, 10, 32, 27], [4, 26, 52, 78, 104]],
54
+ [[28, 26, 30, 30], [21, 12, 35, 29], [4, 30, 56, 82, 108]],
55
+ [[28, 28, 30, 28], [23, 12, 37, 34], [4, 28, 56, 84, 112]],
56
+ [[28, 30, 30, 30], [25, 12, 40, 34], [4, 32, 60, 88, 116]],
57
+ [[28, 30, 30, 30], [26, 13, 42, 35], [4, 24, 48, 72, 96, 120]],
58
+ [[28, 30, 30, 30], [28, 14, 45, 38], [4, 28, 52, 76, 100, 124]],
59
+ [[28, 30, 30, 30], [29, 15, 48, 40], [4, 24, 50, 76, 102, 128]],
60
+ [[28, 30, 30, 30], [31, 16, 51, 43], [4, 28, 54, 80, 106, 132]],
61
+ [[28, 30, 30, 30], [33, 17, 54, 45], [4, 32, 58, 84, 110, 136]],
62
+ [[28, 30, 30, 30], [35, 18, 57, 48], [4, 28, 56, 84, 112, 140]],
63
+ [[28, 30, 30, 30], [37, 19, 60, 51], [4, 32, 60, 88, 116, 144]],
64
+ [[28, 30, 30, 30], [38, 19, 63, 53], [4, 28, 52, 76, 100, 124, 148]],
65
+ [[28, 30, 30, 30], [40, 20, 66, 56], [4, 22, 48, 74, 100, 126, 152]],
66
+ [[28, 30, 30, 30], [43, 21, 70, 59], [4, 26, 52, 78, 104, 130, 156]],
67
+ [[28, 30, 30, 30], [45, 22, 74, 62], [4, 30, 56, 82, 108, 134, 160]],
68
+ [[28, 30, 30, 30], [47, 24, 77, 65], [4, 24, 52, 80, 108, 136, 164]],
69
+ [[28, 30, 30, 30], [49, 25, 81, 68], [4, 28, 56, 84, 112, 140, 168]]
70
+ ];
71
+ // mode constants (cf. Table 2 in JIS X 0510:2004 p. 16)
72
+ var MODE_TERMINATOR = 0;
73
+ var MODE_NUMERIC = 1, MODE_ALPHANUMERIC = 2, MODE_OCTET = 4, MODE_KANJI = 8;
74
+ // validation regexps
75
+ var NUMERIC_REGEXP = /^\d*$/;
76
+ var ALPHANUMERIC_REGEXP = /^[A-Za-z0-9 $%*+\-./:]*$/;
77
+ var ALPHANUMERIC_OUT_REGEXP = /^[A-Z0-9 $%*+\-./:]*$/;
78
+ // ECC levels (cf. Table 22 in JIS X 0510:2004 p. 45)
79
+ var ECCLEVEL_L = 1, ECCLEVEL_M = 0, ECCLEVEL_Q = 3, ECCLEVEL_H = 2;
80
+ // GF(2^8)-to-integer mapping with a reducing polynomial x^8+x^4+x^3+x^2+1
81
+ // invariant: GF256_MAP[GF256_INVMAP[i]] == i for all i in [1,256)
82
+ var GF256_MAP = [], GF256_INVMAP = [-1];
83
+ for (var i = 0, v = 1; i < 255; ++i) {
84
+ GF256_MAP.push(v);
85
+ GF256_INVMAP[v] = i;
86
+ v = (v * 2) ^ (v >= 128 ? 0x11d : 0);
87
+ }
88
+ // generator polynomials up to degree 30
89
+ // (should match with polynomials in JIS X 0510:2004 Appendix A)
90
+ //
91
+ // generator polynomial of degree K is product of (x-\alpha^0), (x-\alpha^1),
92
+ // ..., (x-\alpha^(K-1)). by convention, we omit the K-th coefficient (always 1)
93
+ // from the result; also other coefficients are written in terms of the exponent
94
+ // to \alpha to avoid the redundant calculation. (see also calculateecc below.)
95
+ var GF256_GENPOLY = [[]];
96
+ for (var i = 0; i < 30; ++i) {
97
+ var prevpoly = GF256_GENPOLY[i], poly = [];
98
+ for (var j = 0; j <= i; ++j) {
99
+ var a = (j < i ? GF256_MAP[prevpoly[j]] : 0);
100
+ var b = GF256_MAP[(i + (prevpoly[j - 1] || 0)) % 255];
101
+ poly.push(GF256_INVMAP[a ^ b]);
102
+ }
103
+ GF256_GENPOLY.push(poly);
104
+ }
105
+ // alphanumeric character mapping (cf. Table 5 in JIS X 0510:2004 p. 19)
106
+ var ALPHANUMERIC_MAP = {};
107
+ for (var i = 0; i < 45; ++i) {
108
+ ALPHANUMERIC_MAP['0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'.charAt(i)] = i;
109
+ }
110
+ // mask functions in terms of row # and column #
111
+ // (cf. Table 20 in JIS X 0510:2004 p. 42)
112
+ var MASKFUNCS = [
113
+ function (i, j) { return (i + j) % 2 == 0; },
114
+ function (i, j) { return i % 2 == 0; },
115
+ function (i, j) { return j % 3 == 0; },
116
+ function (i, j) { return (i + j) % 3 == 0; },
117
+ function (i, j) { return (((i / 2) | 0) + ((j / 3) | 0)) % 2 == 0; },
118
+ function (i, j) { return (i * j) % 2 + (i * j) % 3 == 0; },
119
+ function (i, j) { return ((i * j) % 2 + (i * j) % 3) % 2 == 0; },
120
+ function (i, j) { return ((i + j) % 2 + (i * j) % 3) % 2 == 0; }
121
+ ];
122
+ // returns true when the version information has to be embeded.
123
+ var needsverinfo = function (ver) { return ver > 6; };
124
+ // returns the size of entire QR code for given version.
125
+ var getsizebyver = function (ver) { return 4 * ver + 17; };
126
+ // returns the number of bits available for code words in this version.
127
+ var nfullbits = function (ver) {
128
+ /*
129
+ * |<--------------- n --------------->|
130
+ * | |<----- n-17 ---->| |
131
+ * +-------+ ///+-------+ ----
132
+ * | | ///| | ^
133
+ * | 9x9 | @@@@@ ///| 9x8 | |
134
+ * | | # # # @5x5@ # # # | | |
135
+ * +-------+ @@@@@ +-------+ |
136
+ * # ---|
137
+ * ^ |
138
+ * # |
139
+ * @@@@@ @@@@@ @@@@@ | n
140
+ * @5x5@ @5x5@ @5x5@ n-17
141
+ * @@@@@ @@@@@ @@@@@ | |
142
+ * # | |
143
+ * ////// v |
144
+ * //////# ---|
145
+ * +-------+ @@@@@ @@@@@ |
146
+ * | | @5x5@ @5x5@ |
147
+ * | 8x9 | @@@@@ @@@@@ |
148
+ * | | v
149
+ * +-------+ ----
150
+ *
151
+ * when the entire code has n^2 modules and there are m^2-3 alignment
152
+ * patterns, we have:
153
+ * - 225 (= 9x9 + 9x8 + 8x9) modules for finder patterns and
154
+ * format information;
155
+ * - 2n-34 (= 2(n-17)) modules for timing patterns;
156
+ * - 36 (= 3x6 + 6x3) modules for version information, if any;
157
+ * - 25m^2-75 (= (m^2-3)(5x5)) modules for alignment patterns
158
+ * if any, but 10m-20 (= 2(m-2)x5) of them overlaps with
159
+ * timing patterns.
160
+ */
161
+ var v = VERSIONS[ver];
162
+ var nbits = 16 * ver * ver + 128 * ver + 64; // finder, timing and format info.
163
+ if (needsverinfo(ver))
164
+ nbits -= 36; // version information
165
+ if (v[2].length) { // alignment patterns
166
+ nbits -= 25 * v[2].length * v[2].length - 10 * v[2].length - 55;
167
+ }
168
+ return nbits;
169
+ };
170
+ // returns the number of bits available for data portions (i.e. excludes ECC
171
+ // bits but includes mode and length bits) in this version and ECC level.
172
+ var ndatabits = function (ver, ecclevel) {
173
+ var nbits = nfullbits(ver) & ~7; // no sub-octet code words
174
+ var v = VERSIONS[ver];
175
+ nbits -= 8 * v[0][ecclevel] * v[1][ecclevel]; // ecc bits
176
+ return nbits;
177
+ };
178
+ // returns the number of bits required for the length of data.
179
+ // (cf. Table 3 in JIS X 0510:2004 p. 16)
180
+ var ndatalenbits = function (ver, mode) {
181
+ switch (mode) {
182
+ case MODE_NUMERIC: return (ver < 10 ? 10 : ver < 27 ? 12 : 14);
183
+ case MODE_ALPHANUMERIC: return (ver < 10 ? 9 : ver < 27 ? 11 : 13);
184
+ case MODE_OCTET: return (ver < 10 ? 8 : 16);
185
+ case MODE_KANJI: return (ver < 10 ? 8 : ver < 27 ? 10 : 12);
186
+ }
187
+ return undefined;
188
+ };
189
+ // returns the maximum length of data possible in given configuration.
190
+ var getmaxdatalen = function (ver, mode, ecclevel) {
191
+ var nbits = ndatabits(ver, ecclevel) - 4 - ndatalenbits(ver, mode); // 4 for mode bits
192
+ switch (mode) {
193
+ case MODE_NUMERIC:
194
+ return ((nbits / 10) | 0) * 3 + (nbits % 10 < 4 ? 0 : nbits % 10 < 7 ? 1 : 2);
195
+ case MODE_ALPHANUMERIC:
196
+ return ((nbits / 11) | 0) * 2 + (nbits % 11 < 6 ? 0 : 1);
197
+ case MODE_OCTET:
198
+ return (nbits / 8) | 0;
199
+ case MODE_KANJI:
200
+ return (nbits / 13) | 0;
201
+ }
202
+ return undefined;
203
+ };
204
+ // checks if the given data can be encoded in given mode, and returns
205
+ // the converted data for the further processing if possible. otherwise
206
+ // returns null.
207
+ //
208
+ // this function does not check the length of data; it is a duty of
209
+ // encode function below (as it depends on the version and ECC level too).
210
+ var validatedata = function (mode, data) {
211
+ switch (mode) {
212
+ case MODE_NUMERIC:
213
+ if (!data.match(NUMERIC_REGEXP))
214
+ return null;
215
+ return data;
216
+ case MODE_ALPHANUMERIC:
217
+ if (!data.match(ALPHANUMERIC_REGEXP))
218
+ return null;
219
+ return data.toUpperCase();
220
+ case MODE_OCTET:
221
+ if (typeof data === 'string') { // encode as utf-8 string
222
+ var newdata = [];
223
+ for (var i = 0; i < data.length; ++i) {
224
+ var ch = data.charCodeAt(i);
225
+ if (ch < 0x80) {
226
+ newdata.push(ch);
227
+ }
228
+ else if (ch < 0x800) {
229
+ newdata.push(0xc0 | (ch >> 6), 0x80 | (ch & 0x3f));
230
+ }
231
+ else if (ch < 0x10000) {
232
+ newdata.push(0xe0 | (ch >> 12), 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
233
+ }
234
+ else {
235
+ newdata.push(0xf0 | (ch >> 18), 0x80 | ((ch >> 12) & 0x3f), 0x80 | ((ch >> 6) & 0x3f), 0x80 | (ch & 0x3f));
236
+ }
237
+ }
238
+ return newdata;
239
+ }
240
+ else {
241
+ return data;
242
+ }
243
+ }
244
+ };
245
+ // returns the code words (sans ECC bits) for given data and configurations.
246
+ // requires data to be preprocessed by validatedata. no length check is
247
+ // performed, and everything has to be checked before calling this function.
248
+ var encode = function (ver, mode, data, maxbuflen) {
249
+ var buf = [];
250
+ var bits = 0, remaining = 8;
251
+ var datalen = data.length;
252
+ // this function is intentionally no-op when n=0.
253
+ var pack = function (x, n) {
254
+ if (n >= remaining) {
255
+ buf.push(bits | (x >> (n -= remaining)));
256
+ while (n >= 8)
257
+ buf.push((x >> (n -= 8)) & 255);
258
+ bits = 0;
259
+ remaining = 8;
260
+ }
261
+ if (n > 0)
262
+ bits |= (x & ((1 << n) - 1)) << (remaining -= n);
263
+ };
264
+ var nlenbits = ndatalenbits(ver, mode);
265
+ pack(mode, 4);
266
+ pack(datalen, nlenbits);
267
+ switch (mode) {
268
+ case MODE_NUMERIC:
269
+ for (var i = 2; i < datalen; i += 3) {
270
+ pack(parseInt(data.substring(i - 2, i + 1), 10), 10);
271
+ }
272
+ pack(parseInt(data.substring(i - 2), 10), [0, 4, 7][datalen % 3]);
273
+ break;
274
+ case MODE_ALPHANUMERIC:
275
+ for (var i = 1; i < datalen; i += 2) {
276
+ pack(ALPHANUMERIC_MAP[data.charAt(i - 1)] * 45 +
277
+ ALPHANUMERIC_MAP[data.charAt(i)], 11);
278
+ }
279
+ if (datalen % 2 == 1) {
280
+ pack(ALPHANUMERIC_MAP[data.charAt(i - 1)], 6);
281
+ }
282
+ break;
283
+ case MODE_OCTET:
284
+ for (var i = 0; i < datalen; ++i) {
285
+ pack(data[i], 8);
286
+ }
287
+ break;
288
+ }
289
+ ;
290
+ // final bits. it is possible that adding terminator causes the buffer
291
+ // to overflow, but then the buffer truncated to the maximum size will
292
+ // be valid as the truncated terminator mode bits and padding is
293
+ // identical in appearance (cf. JIS X 0510:2004 sec 8.4.8).
294
+ pack(MODE_TERMINATOR, 4);
295
+ if (remaining < 8)
296
+ buf.push(bits);
297
+ // the padding to fill up the remaining space. we should not add any
298
+ // words when the overflow already occurred.
299
+ while (buf.length + 1 < maxbuflen)
300
+ buf.push(0xec, 0x11);
301
+ if (buf.length < maxbuflen)
302
+ buf.push(0xec);
303
+ return buf;
304
+ };
305
+ // calculates ECC code words for given code words and generator polynomial.
306
+ //
307
+ // this is quite similar to CRC calculation as both Reed-Solomon and CRC use
308
+ // the certain kind of cyclic codes, which is effectively the division of
309
+ // zero-augumented polynomial by the generator polynomial. the only difference
310
+ // is that Reed-Solomon uses GF(2^8), instead of CRC's GF(2), and Reed-Solomon
311
+ // uses the different generator polynomial than CRC's.
312
+ var calculateecc = function (poly, genpoly) {
313
+ var modulus = poly.slice(0);
314
+ var polylen = poly.length, genpolylen = genpoly.length;
315
+ for (var i = 0; i < genpolylen; ++i)
316
+ modulus.push(0);
317
+ for (var i = 0; i < polylen;) {
318
+ var quotient = GF256_INVMAP[modulus[i++]];
319
+ if (quotient >= 0) {
320
+ for (var j = 0; j < genpolylen; ++j) {
321
+ modulus[i + j] ^= GF256_MAP[(quotient + genpoly[j]) % 255];
322
+ }
323
+ }
324
+ }
325
+ return modulus.slice(polylen);
326
+ };
327
+ // auguments ECC code words to given code words. the resulting words are
328
+ // ready to be encoded in the matrix.
329
+ //
330
+ // the much of actual augumenting procedure follows JIS X 0510:2004 sec 8.7.
331
+ // the code is simplified using the fact that the size of each code & ECC
332
+ // blocks is almost same; for example, when we have 4 blocks and 46 data words
333
+ // the number of code words in those blocks are 11, 11, 12, 12 respectively.
334
+ var augumenteccs = function (poly, nblocks, genpoly) {
335
+ var subsizes = [];
336
+ var subsize = (poly.length / nblocks) | 0, subsize0 = 0;
337
+ var pivot = nblocks - poly.length % nblocks;
338
+ for (var i = 0; i < pivot; ++i) {
339
+ subsizes.push(subsize0);
340
+ subsize0 += subsize;
341
+ }
342
+ for (var i = pivot; i < nblocks; ++i) {
343
+ subsizes.push(subsize0);
344
+ subsize0 += subsize + 1;
345
+ }
346
+ subsizes.push(subsize0);
347
+ var eccs = [];
348
+ for (var i = 0; i < nblocks; ++i) {
349
+ eccs.push(calculateecc(poly.slice(subsizes[i], subsizes[i + 1]), genpoly));
350
+ }
351
+ var result = [];
352
+ var nitemsperblock = (poly.length / nblocks) | 0;
353
+ for (var i = 0; i < nitemsperblock; ++i) {
354
+ for (var j = 0; j < nblocks; ++j) {
355
+ result.push(poly[subsizes[j] + i]);
356
+ }
357
+ }
358
+ for (var j = pivot; j < nblocks; ++j) {
359
+ result.push(poly[subsizes[j + 1] - 1]);
360
+ }
361
+ for (var i = 0; i < genpoly.length; ++i) {
362
+ for (var j = 0; j < nblocks; ++j) {
363
+ result.push(eccs[j][i]);
364
+ }
365
+ }
366
+ return result;
367
+ };
368
+ // auguments BCH(p+q,q) code to the polynomial over GF(2), given the proper
369
+ // genpoly. the both input and output are in binary numbers, and unlike
370
+ // calculateecc genpoly should include the 1 bit for the highest degree.
371
+ //
372
+ // actual polynomials used for this procedure are as follows:
373
+ // - p=10, q=5, genpoly=x^10+x^8+x^5+x^4+x^2+x+1 (JIS X 0510:2004 Appendix C)
374
+ // - p=18, q=6, genpoly=x^12+x^11+x^10+x^9+x^8+x^5+x^2+1 (ibid. Appendix D)
375
+ var augumentbch = function (poly, p, genpoly, q) {
376
+ var modulus = poly << q;
377
+ for (var i = p - 1; i >= 0; --i) {
378
+ if ((modulus >> (q + i)) & 1)
379
+ modulus ^= genpoly << i;
380
+ }
381
+ return (poly << q) | modulus;
382
+ };
383
+ // creates the base matrix for given version. it returns two matrices, one of
384
+ // them is the actual one and the another represents the "reserved" portion
385
+ // (e.g. finder and timing patterns) of the matrix.
386
+ //
387
+ // some entries in the matrix may be undefined, rather than 0 or 1. this is
388
+ // intentional (no initialization needed!), and putdata below will fill
389
+ // the remaining ones.
390
+ var makebasematrix = function (ver) {
391
+ var v = VERSIONS[ver], n = getsizebyver(ver);
392
+ var matrix = [], reserved = [];
393
+ for (var i = 0; i < n; ++i) {
394
+ matrix.push([]);
395
+ reserved.push([]);
396
+ }
397
+ var blit = function (y, x, h, w, bits) {
398
+ for (var i = 0; i < h; ++i) {
399
+ for (var j = 0; j < w; ++j) {
400
+ matrix[y + i][x + j] = (bits[i] >> j) & 1;
401
+ reserved[y + i][x + j] = 1;
402
+ }
403
+ }
404
+ };
405
+ // finder patterns and a part of timing patterns
406
+ // will also mark the format information area (not yet written) as reserved.
407
+ blit(0, 0, 9, 9, [0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x17f, 0x00, 0x40]);
408
+ blit(n - 8, 0, 8, 9, [0x100, 0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x7f]);
409
+ blit(0, n - 8, 9, 8, [0xfe, 0x82, 0xba, 0xba, 0xba, 0x82, 0xfe, 0x00, 0x00]);
410
+ // the rest of timing patterns
411
+ for (var i = 9; i < n - 8; ++i) {
412
+ matrix[6][i] = matrix[i][6] = ~i & 1;
413
+ reserved[6][i] = reserved[i][6] = 1;
414
+ }
415
+ // alignment patterns
416
+ var aligns = v[2], m = aligns.length;
417
+ for (var i = 0; i < m; ++i) {
418
+ var minj = (i == 0 || i == m - 1 ? 1 : 0), maxj = (i == 0 ? m - 1 : m);
419
+ for (var j = minj; j < maxj; ++j) {
420
+ blit(aligns[i], aligns[j], 5, 5, [0x1f, 0x11, 0x15, 0x11, 0x1f]);
421
+ }
422
+ }
423
+ // version information
424
+ if (needsverinfo(ver)) {
425
+ var code = augumentbch(ver, 6, 0x1f25, 12);
426
+ var k = 0;
427
+ for (var i = 0; i < 6; ++i) {
428
+ for (var j = 0; j < 3; ++j) {
429
+ matrix[i][(n - 11) + j] = matrix[(n - 11) + j][i] = (code >> k++) & 1;
430
+ reserved[i][(n - 11) + j] = reserved[(n - 11) + j][i] = 1;
431
+ }
432
+ }
433
+ }
434
+ return { matrix: matrix, reserved: reserved };
435
+ };
436
+ // fills the data portion (i.e. unmarked in reserved) of the matrix with given
437
+ // code words. the size of code words should be no more than available bits,
438
+ // and remaining bits are padded to 0 (cf. JIS X 0510:2004 sec 8.7.3).
439
+ var putdata = function (matrix, reserved, buf) {
440
+ var n = matrix.length;
441
+ var k = 0, dir = -1;
442
+ for (var i = n - 1; i >= 0; i -= 2) {
443
+ if (i == 6)
444
+ --i; // skip the entire timing pattern column
445
+ var jj = (dir < 0 ? n - 1 : 0);
446
+ for (var j = 0; j < n; ++j) {
447
+ for (var ii = i; ii > i - 2; --ii) {
448
+ if (!reserved[jj][ii]) {
449
+ // may overflow, but (undefined >> x)
450
+ // is 0 so it will auto-pad to zero.
451
+ matrix[jj][ii] = (buf[k >> 3] >> (~k & 7)) & 1;
452
+ ++k;
453
+ }
454
+ }
455
+ jj += dir;
456
+ }
457
+ dir = -dir;
458
+ }
459
+ return matrix;
460
+ };
461
+ // XOR-masks the data portion of the matrix. repeating the call with the same
462
+ // arguments will revert the prior call (convenient in the matrix evaluation).
463
+ var maskdata = function (matrix, reserved, mask) {
464
+ var maskf = MASKFUNCS[mask];
465
+ var n = matrix.length;
466
+ for (var i = 0; i < n; ++i) {
467
+ for (var j = 0; j < n; ++j) {
468
+ //@ts-ignore
469
+ if (!reserved[i][j])
470
+ matrix[i][j] ^= maskf(i, j);
471
+ }
472
+ }
473
+ return matrix;
474
+ };
475
+ // puts the format information.
476
+ var putformatinfo = function (matrix, reserved, ecclevel, mask) {
477
+ var n = matrix.length;
478
+ var code = augumentbch((ecclevel << 3) | mask, 5, 0x537, 10) ^ 0x5412;
479
+ for (var i = 0; i < 15; ++i) {
480
+ var r = [0, 1, 2, 3, 4, 5, 7, 8, n - 7, n - 6, n - 5, n - 4, n - 3, n - 2, n - 1][i];
481
+ var c = [n - 1, n - 2, n - 3, n - 4, n - 5, n - 6, n - 7, n - 8, 7, 5, 4, 3, 2, 1, 0][i];
482
+ matrix[r][8] = matrix[8][c] = (code >> i) & 1;
483
+ // we don't have to mark those bits reserved; always done
484
+ // in makebasematrix above.
485
+ }
486
+ return matrix;
487
+ };
488
+ // evaluates the resulting matrix and returns the score (lower is better).
489
+ // (cf. JIS X 0510:2004 sec 8.8.2)
490
+ //
491
+ // the evaluation procedure tries to avoid the problematic patterns naturally
492
+ // occuring from the original matrix. for example, it penaltizes the patterns
493
+ // which just look like the finder pattern which will confuse the decoder.
494
+ // we choose the mask which results in the lowest score among 8 possible ones.
495
+ //
496
+ // note: zxing seems to use the same procedure and in many cases its choice
497
+ // agrees to ours, but sometimes it does not. practically it doesn't matter.
498
+ var evaluatematrix = function (matrix) {
499
+ // N1+(k-5) points for each consecutive row of k same-colored modules,
500
+ // where k >= 5. no overlapping row counts.
501
+ var PENALTY_CONSECUTIVE = 3;
502
+ // N2 points for each 2x2 block of same-colored modules.
503
+ // overlapping block does count.
504
+ var PENALTY_TWOBYTWO = 3;
505
+ // N3 points for each pattern with >4W:1B:1W:3B:1W:1B or
506
+ // 1B:1W:3B:1W:1B:>4W, or their multiples (e.g. highly unlikely,
507
+ // but 13W:3B:3W:9B:3W:3B counts).
508
+ var PENALTY_FINDERLIKE = 40;
509
+ // N4*k points for every (5*k)% deviation from 50% black density.
510
+ // i.e. k=1 for 55~60% and 40~45%, k=2 for 60~65% and 35~40%, etc.
511
+ var PENALTY_DENSITY = 10;
512
+ var evaluategroup = function (groups) {
513
+ var score = 0;
514
+ for (var i = 0; i < groups.length; ++i) {
515
+ if (groups[i] >= 5)
516
+ score += PENALTY_CONSECUTIVE + (groups[i] - 5);
517
+ }
518
+ for (var i = 5; i < groups.length; i += 2) {
519
+ var p = groups[i];
520
+ if (groups[i - 1] == p && groups[i - 2] == 3 * p && groups[i - 3] == p &&
521
+ groups[i - 4] == p && (groups[i - 5] >= 4 * p || groups[i + 1] >= 4 * p)) {
522
+ // this part differs from zxing...
523
+ score += PENALTY_FINDERLIKE;
524
+ }
525
+ }
526
+ return score;
527
+ };
528
+ var n = matrix.length;
529
+ var score = 0, nblacks = 0;
530
+ for (var i = 0; i < n; ++i) {
531
+ var row = matrix[i];
532
+ var groups;
533
+ // evaluate the current row
534
+ groups = [0]; // the first empty group of white
535
+ for (var j = 0; j < n;) {
536
+ var k;
537
+ for (k = 0; j < n && row[j]; ++k)
538
+ ++j;
539
+ groups.push(k);
540
+ for (k = 0; j < n && !row[j]; ++k)
541
+ ++j;
542
+ groups.push(k);
543
+ }
544
+ score += evaluategroup(groups);
545
+ // evaluate the current column
546
+ groups = [0];
547
+ for (var j = 0; j < n;) {
548
+ var k;
549
+ for (k = 0; j < n && matrix[j][i]; ++k)
550
+ ++j;
551
+ groups.push(k);
552
+ for (k = 0; j < n && !matrix[j][i]; ++k)
553
+ ++j;
554
+ groups.push(k);
555
+ }
556
+ score += evaluategroup(groups);
557
+ // check the 2x2 box and calculate the density
558
+ var nextrow = matrix[i + 1] || [];
559
+ nblacks += row[0];
560
+ for (var j = 1; j < n; ++j) {
561
+ var p = row[j];
562
+ nblacks += p;
563
+ // at least comparison with next row should be strict...
564
+ if (row[j - 1] == p && nextrow[j] === p && nextrow[j - 1] === p) {
565
+ score += PENALTY_TWOBYTWO;
566
+ }
567
+ }
568
+ }
569
+ score += PENALTY_DENSITY * ((Math.abs(nblacks / n / n - 0.5) / 0.05) | 0);
570
+ return score;
571
+ };
572
+ // returns the fully encoded QR code matrix which contains given data.
573
+ // it also chooses the best mask automatically when mask is -1.
574
+ var generate = function (data, ver, mode, ecclevel, mask) {
575
+ var v = VERSIONS[ver];
576
+ var buf = encode(ver, mode, data, ndatabits(ver, ecclevel) >> 3);
577
+ buf = augumenteccs(buf, v[1][ecclevel], GF256_GENPOLY[v[0][ecclevel]]);
578
+ var result = makebasematrix(ver);
579
+ var matrix = result.matrix, reserved = result.reserved;
580
+ putdata(matrix, reserved, buf);
581
+ if (mask < 0) {
582
+ // find the best mask
583
+ maskdata(matrix, reserved, 0);
584
+ putformatinfo(matrix, reserved, ecclevel, 0);
585
+ var bestmask = 0, bestscore = evaluatematrix(matrix);
586
+ maskdata(matrix, reserved, 0);
587
+ for (mask = 1; mask < 8; ++mask) {
588
+ maskdata(matrix, reserved, mask);
589
+ putformatinfo(matrix, reserved, ecclevel, mask);
590
+ var score = evaluatematrix(matrix);
591
+ if (bestscore > score) {
592
+ bestscore = score;
593
+ bestmask = mask;
594
+ }
595
+ maskdata(matrix, reserved, mask);
596
+ }
597
+ mask = bestmask;
598
+ }
599
+ maskdata(matrix, reserved, mask);
600
+ putformatinfo(matrix, reserved, ecclevel, mask);
601
+ return matrix;
602
+ };
603
+ // the public interface is trivial; the options available are as follows:
604
+ //
605
+ // - version: an integer in [1,40]. when omitted (or -1) the smallest possible
606
+ // version is chosen.
607
+ // - mode: one of 'numeric', 'alphanumeric', 'octet'. when omitted the smallest
608
+ // possible mode is chosen.
609
+ // - ecclevel: one of 'L', 'M', 'Q', 'H'. defaults to 'L'.
610
+ // - mask: an integer in [0,7]. when omitted (or -1) the best mask is chosen.
611
+ //
612
+ // for generate{HTML,PNG}:
613
+ //
614
+ // - modulesize: a number. this is a size of each modules in pixels, and
615
+ // defaults to 5px.
616
+ // - margin: a number. this is a size of margin in *modules*, and defaults to
617
+ // 4 (white modules). the specficiation mandates the margin no less than 4
618
+ // modules, so it is better not to alter this value unless you know what
619
+ // you're doing.
620
+ var QRCode = {
621
+ 'generate': function (data, options) {
622
+ var MODES = {
623
+ 'numeric': MODE_NUMERIC, 'alphanumeric': MODE_ALPHANUMERIC,
624
+ 'octet': MODE_OCTET
625
+ };
626
+ var ECCLEVELS = {
627
+ 'L': ECCLEVEL_L, 'M': ECCLEVEL_M, 'Q': ECCLEVEL_Q,
628
+ 'H': ECCLEVEL_H
629
+ };
630
+ options = options || {};
631
+ var ver = options.version || -1;
632
+ var ecclevel = ECCLEVELS[(options.ecclevel || 'L').toUpperCase()];
633
+ var mode = options.mode ? MODES[options.mode.toLowerCase()] : -1;
634
+ var mask = 'mask' in options ? options.mask : -1;
635
+ if (mode < 0) {
636
+ if (typeof data === 'string') {
637
+ if (data.match(NUMERIC_REGEXP)) {
638
+ mode = MODE_NUMERIC;
639
+ }
640
+ else if (data.match(ALPHANUMERIC_OUT_REGEXP)) {
641
+ // while encode supports case-insensitive
642
+ // encoding, we restrict the data to be
643
+ // uppercased when auto-selecting the mode.
644
+ mode = MODE_ALPHANUMERIC;
645
+ }
646
+ else {
647
+ mode = MODE_OCTET;
648
+ }
649
+ }
650
+ else {
651
+ mode = MODE_OCTET;
652
+ }
653
+ }
654
+ else if (!(mode == MODE_NUMERIC || mode == MODE_ALPHANUMERIC ||
655
+ mode == MODE_OCTET)) {
656
+ throw 'invalid or unsupported mode';
657
+ }
658
+ data = validatedata(mode, data);
659
+ if (data === null)
660
+ throw 'invalid data format';
661
+ if (ecclevel < 0 || ecclevel > 3)
662
+ throw 'invalid ECC level';
663
+ if (ver < 0) {
664
+ for (ver = 1; ver <= 40; ++ver) {
665
+ if (data.length <= getmaxdatalen(ver, mode, ecclevel))
666
+ break;
667
+ }
668
+ if (ver > 40)
669
+ throw 'too large data';
670
+ }
671
+ else if (ver < 1 || ver > 40) {
672
+ throw 'invalid version';
673
+ }
674
+ if (mask != -1 && (mask < 0 || mask > 8))
675
+ throw 'invalid mask';
676
+ return generate(data, ver, mode, ecclevel, mask);
677
+ },
678
+ 'generateHTML': function (data, options) {
679
+ options = options || {};
680
+ var matrix = QRCode['generate'](data, options);
681
+ var modsize = Math.max(options.modulesize || 5, 0.5);
682
+ var margin = Math.max(options.margin !== null ? options.margin : 4, 0.0);
683
+ var e = document.createElement('div');
684
+ var n = matrix.length;
685
+ var html = ['<table border="0" cellspacing="0" cellpadding="0" style="border:' +
686
+ modsize * margin + 'px solid #fff;background:#fff">'];
687
+ for (var i = 0; i < n; ++i) {
688
+ html.push('<tr>');
689
+ for (var j = 0; j < n; ++j) {
690
+ html.push('<td style="width:' + modsize + 'px;height:' + modsize + 'px' +
691
+ (matrix[i][j] ? ';background:#000' : '') + '" ' +
692
+ 'part="' + (matrix[i][j] ? 'module-fg' : 'module-bg') + '" ' + '></td>');
693
+ }
694
+ html.push('</tr>');
695
+ }
696
+ e.className = 'qrcode';
697
+ e.innerHTML = html.join('') + '</table>';
698
+ return e;
699
+ },
700
+ 'generateSVG': function (data, options) {
701
+ options = options || {};
702
+ var matrix = QRCode['generate'](data, options);
703
+ var n = matrix.length;
704
+ var modsize = Math.max(options.modulesize || 5, 0.5);
705
+ var margin = Math.max(options.margin !== null ? options.margin : 4, 0.0);
706
+ var size = modsize * (n + 2 * margin);
707
+ var common = ' class= "fg"' + ' width="' + modsize + '" height="' + modsize + '"/>';
708
+ var e = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
709
+ e.setAttribute('viewBox', '0 0 ' + size + ' ' + size);
710
+ e.setAttribute('style', 'shape-rendering:crispEdges');
711
+ if (options.modulesize) {
712
+ e.setAttribute('width', size.toString());
713
+ e.setAttribute('height', size.toString());
714
+ }
715
+ var svg = [
716
+ '<style scoped>.bg{fill:' + (options?.background ?? '#FFF') + '}.fg{fill:#000}</style>',
717
+ '<rect class="bg" x="0" y="0"',
718
+ 'width="' + size + '" height="' + size + '"/>',
719
+ ];
720
+ var yo = margin * modsize;
721
+ for (var y = 0; y < n; ++y) {
722
+ var xo = margin * modsize;
723
+ for (var x = 0; x < n; ++x) {
724
+ if (matrix[y][x])
725
+ svg.push('<rect x="' + xo + '" y="' + yo + '"', common);
726
+ xo += modsize;
727
+ }
728
+ yo += modsize;
729
+ }
730
+ e.innerHTML = svg.join('');
731
+ return e;
732
+ },
733
+ 'generatePNG': function (data, options) {
734
+ options = options || {};
735
+ var matrix = QRCode['generate'](data, options);
736
+ var modsize = Math.max(options.modulesize || 5, 0.5);
737
+ var margin = Math.max(options.margin !== null ? options.margin : 4, 0.0);
738
+ var n = matrix.length;
739
+ var size = modsize * (n + 2 * margin);
740
+ var canvas = document.createElement('canvas'), context;
741
+ canvas.width = canvas.height = size;
742
+ context = canvas.getContext('2d');
743
+ if (!context)
744
+ throw 'canvas support is needed for PNG output';
745
+ context.fillStyle = '#fff';
746
+ context.fillRect(0, 0, size, size);
747
+ context.fillStyle = '#000';
748
+ for (var i = 0; i < n; ++i) {
749
+ for (var j = 0; j < n; ++j) {
750
+ if (matrix[i][j]) {
751
+ context.fillRect(modsize * (margin + j), modsize * (margin + i), modsize, modsize);
752
+ }
753
+ }
754
+ }
755
+ //context.fillText('evaluation: ' + evaluatematrix(matrix), 10, 10);
756
+ return canvas.toDataURL();
757
+ }
758
+ };
759
+ export default QRCode;