Debian Codegraph @

addons/point_of_sale/static/lib/qrcode.js

raw
1 /**
2 * @fileoverview
3 * - Using the 'QRCode for Javascript library'
4 * - Fixed dataset of 'QRCode for Javascript library' for support full-spec.
5 * - this library has no dependencies.
6 *
7 * @author davidshimjs
8 * @see <a href="http://www.d-project.com/" target="_blank">http://www.d-project.com/</a>
9 * @see <a href="http://jeromeetienne.github.com/jquery-qrcode/" target="_blank">http://jeromeetienne.github.com/jquery-qrcode/</a>
10 */
11 let QRCode;
12
13 (() => {
14 //---------------------------------------------------------------------
15 // QRCode for JavaScript
16 //
17 // Copyright (c) 2009 Kazuhiko Arase
18 //
19 // URL: http://www.d-project.com/
20 // Github: https://github.com/davidshimjs/qrcodejs
21 //
22 // Licensed under the MIT license:
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 // The MIT License (MIT)
26 // ---------------------
27 // Copyright (c) 2012 davidshimjs
28 //
29 // Permission is hereby granted, free of charge,
30 // to any person obtaining a copy of this software and associated documentation files (the "Software"),
31 // to deal in the Software without restriction,
32 // including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
33 // and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
34 // subject to the following conditions:
35 //
36 // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
37 //
38 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 //
40 //
41 // The word "QR Code" is registered trademark of
42 // DENSO WAVE INCORPORATED
43 // http://www.denso-wave.com/qrcode/faqpatent-e.html
44 //
45 //---------------------------------------------------------------------
46 class QR8bitByte {
47 constructor(data) {
48 this.mode = QRMode.MODE_8BIT_BYTE;
49 this.data = data;
50 this.parsedData = [];
51
52 // Added to support UTF-8 Characters
53 for (let i = 0, l = this.data.length; i < l; i++) {
54 const byteArray = [];
55 const code = this.data.charCodeAt(i);
56
57 if (code > 0x10000) {
58 byteArray[0] = 0xf0 | ((code & 0x1c0000) >>> 18);
59 byteArray[1] = 0x80 | ((code & 0x3f000) >>> 12);
60 byteArray[2] = 0x80 | ((code & 0xfc0) >>> 6);
61 byteArray[3] = 0x80 | (code & 0x3f);
62 } else if (code > 0x800) {
63 byteArray[0] = 0xe0 | ((code & 0xf000) >>> 12);
64 byteArray[1] = 0x80 | ((code & 0xfc0) >>> 6);
65 byteArray[2] = 0x80 | (code & 0x3f);
66 } else if (code > 0x80) {
67 byteArray[0] = 0xc0 | ((code & 0x7c0) >>> 6);
68 byteArray[1] = 0x80 | (code & 0x3f);
69 } else {
70 byteArray[0] = code;
71 }
72
73 this.parsedData.push(byteArray);
74 }
75
76 this.parsedData = Array.prototype.concat.apply([], this.parsedData);
77
78 if (this.parsedData.length != this.data.length) {
79 this.parsedData.unshift(191);
80 this.parsedData.unshift(187);
81 this.parsedData.unshift(239);
82 }
83 }
84
85 getLength(buffer) {
86 return this.parsedData.length;
87 }
88
89 write(buffer) {
90 for (let i = 0, l = this.parsedData.length; i < l; i++) {
91 buffer.put(this.parsedData[i], 8);
92 }
93 }
94 }
95
96 class QRCodeModel {
97 constructor(typeNumber, errorCorrectLevel) {
98 this.typeNumber = typeNumber;
99 this.errorCorrectLevel = errorCorrectLevel;
100 this.modules = null;
101 this.moduleCount = 0;
102 this.dataCache = null;
103 this.dataList = [];
104 }
105
106 addData(data) {
107 const newData = new QR8bitByte(data);
108 this.dataList.push(newData);
109 this.dataCache = null;
110 }
111
112 isDark(row, col) {
113 if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) {
114 throw new Error(`${row},${col}`);
115 }
116 return this.modules[row][col];
117 }
118
119 getModuleCount() {
120 return this.moduleCount;
121 }
122 make() {
123 this.makeImpl(false, this.getBestMaskPattern());
124 }
125
126 makeImpl(test, maskPattern) {
127 this.moduleCount = this.typeNumber * 4 + 17;
128 this.modules = new Array(this.moduleCount);
129 for (let row = 0; row < this.moduleCount; row++) {
130 this.modules[row] = new Array(this.moduleCount);
131 for (let col = 0; col < this.moduleCount; col++) {
132 this.modules[row][col] = null;
133 }
134 }
135 this.setupPositionProbePattern(0, 0);
136 this.setupPositionProbePattern(this.moduleCount - 7, 0);
137 this.setupPositionProbePattern(0, this.moduleCount - 7);
138 this.setupPositionAdjustPattern();
139 this.setupTimingPattern();
140 this.setupTypeInfo(test, maskPattern);
141 if (this.typeNumber >= 7) {
142 this.setupTypeNumber(test);
143 }
144 if (this.dataCache == null) {
145 this.dataCache = QRCodeModel.createData(
146 this.typeNumber,
147 this.errorCorrectLevel,
148 this.dataList
149 );
150 }
151 this.mapData(this.dataCache, maskPattern);
152 }
153
154 setupPositionProbePattern(row, col) {
155 for (let r = -1; r <= 7; r++) {
156 if (row + r <= -1 || this.moduleCount <= row + r) {
157 continue;
158 }
159 for (let c = -1; c <= 7; c++) {
160 if (col + c <= -1 || this.moduleCount <= col + c) {
161 continue;
162 }
163 if (
164 (0 <= r && r <= 6 && (c == 0 || c == 6)) ||
165 (0 <= c && c <= 6 && (r == 0 || r == 6)) ||
166 (2 <= r && r <= 4 && 2 <= c && c <= 4)
167 ) {
168 this.modules[row + r][col + c] = true;
169 } else {
170 this.modules[row + r][col + c] = false;
171 }
172 }
173 }
174 }
175
176 getBestMaskPattern() {
177 let minLostPoint = 0;
178 let pattern = 0;
179 for (let i = 0; i < 8; i++) {
180 this.makeImpl(true, i);
181 const lostPoint = QRUtil.getLostPoint(this);
182 if (i == 0 || minLostPoint > lostPoint) {
183 minLostPoint = lostPoint;
184 pattern = i;
185 }
186 }
187 return pattern;
188 }
189
190 createMovieClip(target_mc, instance_name, depth) {
191 const qr_mc = target_mc.createEmptyMovieClip(instance_name, depth);
192 const cs = 1;
193 this.make();
194 for (let row = 0; row < this.modules.length; row++) {
195 const y = row * cs;
196 for (let col = 0; col < this.modules[row].length; col++) {
197 const x = col * cs;
198 const dark = this.modules[row][col];
199 if (dark) {
200 qr_mc.beginFill(0, 100);
201 qr_mc.moveTo(x, y);
202 qr_mc.lineTo(x + cs, y);
203 qr_mc.lineTo(x + cs, y + cs);
204 qr_mc.lineTo(x, y + cs);
205 qr_mc.endFill();
206 }
207 }
208 }
209 return qr_mc;
210 }
211
212 setupTimingPattern() {
213 for (let r = 8; r < this.moduleCount - 8; r++) {
214 if (this.modules[r][6] != null) {
215 continue;
216 }
217 this.modules[r][6] = r % 2 == 0;
218 }
219 for (let c = 8; c < this.moduleCount - 8; c++) {
220 if (this.modules[6][c] != null) {
221 continue;
222 }
223 this.modules[6][c] = c % 2 == 0;
224 }
225 }
226
227 setupPositionAdjustPattern() {
228 const pos = QRUtil.getPatternPosition(this.typeNumber);
229 for (let i = 0; i < pos.length; i++) {
230 for (let j = 0; j < pos.length; j++) {
231 const row = pos[i];
232 const col = pos[j];
233 if (this.modules[row][col] != null) {
234 continue;
235 }
236 for (let r = -2; r <= 2; r++) {
237 for (let c = -2; c <= 2; c++) {
238 if (r == -2 || r == 2 || c == -2 || c == 2 || (r == 0 && c == 0)) {
239 this.modules[row + r][col + c] = true;
240 } else {
241 this.modules[row + r][col + c] = false;
242 }
243 }
244 }
245 }
246 }
247 }
248
249 setupTypeNumber(test) {
250 const bits = QRUtil.getBCHTypeNumber(this.typeNumber);
251 for (let i = 0; i < 18; i++) {
252 const mod = !test && ((bits >> i) & 1) == 1;
253 this.modules[Math.floor(i / 3)][(i % 3) + this.moduleCount - 8 - 3] = mod;
254 }
255 for (let i = 0; i < 18; i++) {
256 const mod = !test && ((bits >> i) & 1) == 1;
257 this.modules[(i % 3) + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
258 }
259 }
260
261 setupTypeInfo(test, maskPattern) {
262 const data = (this.errorCorrectLevel << 3) | maskPattern;
263 const bits = QRUtil.getBCHTypeInfo(data);
264 for (let i = 0; i < 15; i++) {
265 const mod = !test && ((bits >> i) & 1) == 1;
266 if (i < 6) {
267 this.modules[i][8] = mod;
268 } else if (i < 8) {
269 this.modules[i + 1][8] = mod;
270 } else {
271 this.modules[this.moduleCount - 15 + i][8] = mod;
272 }
273 }
274 for (let i = 0; i < 15; i++) {
275 const mod = !test && ((bits >> i) & 1) == 1;
276 if (i < 8) {
277 this.modules[8][this.moduleCount - i - 1] = mod;
278 } else if (i < 9) {
279 this.modules[8][15 - i - 1 + 1] = mod;
280 } else {
281 this.modules[8][15 - i - 1] = mod;
282 }
283 }
284 this.modules[this.moduleCount - 8][8] = !test;
285 }
286
287 mapData(data, maskPattern) {
288 let inc = -1;
289 let row = this.moduleCount - 1;
290 let bitIndex = 7;
291 let byteIndex = 0;
292 for (let col = this.moduleCount - 1; col > 0; col -= 2) {
293 if (col == 6) col--;
294 while (true) {
295 for (let c = 0; c < 2; c++) {
296 if (this.modules[row][col - c] == null) {
297 let dark = false;
298 if (byteIndex < data.length) {
299 dark = ((data[byteIndex] >>> bitIndex) & 1) == 1;
300 }
301 const mask = QRUtil.getMask(maskPattern, row, col - c);
302 if (mask) {
303 dark = !dark;
304 }
305 this.modules[row][col - c] = dark;
306 bitIndex--;
307 if (bitIndex == -1) {
308 byteIndex++;
309 bitIndex = 7;
310 }
311 }
312 }
313 row += inc;
314 if (row < 0 || this.moduleCount <= row) {
315 row -= inc;
316 inc = -inc;
317 break;
318 }
319 }
320 }
321 }
322
323 static createData(typeNumber, errorCorrectLevel, dataList) {
324 const rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectLevel);
325 const buffer = new QRBitBuffer();
326 for (let i = 0; i < dataList.length; i++) {
327 const data = dataList[i];
328 buffer.put(data.mode, 4);
329 buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber));
330 data.write(buffer);
331 }
332 let totalDataCount = 0;
333 for (let i = 0; i < rsBlocks.length; i++) {
334 totalDataCount += rsBlocks[i].dataCount;
335 }
336 if (buffer.getLengthInBits() > totalDataCount * 8) {
337 throw new Error(
338 `code length overflow. (${buffer.getLengthInBits()}>${totalDataCount * 8})`
339 );
340 }
341 if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
342 buffer.put(0, 4);
343 }
344 while (buffer.getLengthInBits() % 8 != 0) {
345 buffer.putBit(false);
346 }
347 while (true) {
348 if (buffer.getLengthInBits() >= totalDataCount * 8) {
349 break;
350 }
351 buffer.put(QRCodeModel.PAD0, 8);
352 if (buffer.getLengthInBits() >= totalDataCount * 8) {
353 break;
354 }
355 buffer.put(QRCodeModel.PAD1, 8);
356 }
357 return QRCodeModel.createBytes(buffer, rsBlocks);
358 }
359
360 static createBytes(buffer, rsBlocks) {
361 let offset = 0;
362 let maxDcCount = 0;
363 let maxEcCount = 0;
364 const dcdata = new Array(rsBlocks.length);
365 const ecdata = new Array(rsBlocks.length);
366 for (let r = 0; r < rsBlocks.length; r++) {
367 const dcCount = rsBlocks[r].dataCount;
368 const ecCount = rsBlocks[r].totalCount - dcCount;
369 maxDcCount = Math.max(maxDcCount, dcCount);
370 maxEcCount = Math.max(maxEcCount, ecCount);
371 dcdata[r] = new Array(dcCount);
372 for (let i = 0; i < dcdata[r].length; i++) {
373 dcdata[r][i] = 0xff & buffer.buffer[i + offset];
374 }
375 offset += dcCount;
376 const rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
377 const rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1);
378 const modPoly = rawPoly.mod(rsPoly);
379 ecdata[r] = new Array(rsPoly.getLength() - 1);
380 for (let i = 0; i < ecdata[r].length; i++) {
381 const modIndex = i + modPoly.getLength() - ecdata[r].length;
382 ecdata[r][i] = modIndex >= 0 ? modPoly.get(modIndex) : 0;
383 }
384 }
385 let totalCodeCount = 0;
386 for (let i = 0; i < rsBlocks.length; i++) {
387 totalCodeCount += rsBlocks[i].totalCount;
388 }
389 const data = new Array(totalCodeCount);
390 let index = 0;
391 for (let i = 0; i < maxDcCount; i++) {
392 for (let r = 0; r < rsBlocks.length; r++) {
393 if (i < dcdata[r].length) {
394 data[index++] = dcdata[r][i];
395 }
396 }
397 }
398 for (let i = 0; i < maxEcCount; i++) {
399 for (let r = 0; r < rsBlocks.length; r++) {
400 if (i < ecdata[r].length) {
401 data[index++] = ecdata[r][i];
402 }
403 }
404 }
405 return data;
406 }
407 }
408
409 QRCodeModel.PAD0 = 0xec;
410 QRCodeModel.PAD1 = 0x11;
411 const QRMode = {
412 MODE_NUMBER: 1 << 0,
413 MODE_ALPHA_NUM: 1 << 1,
414 MODE_8BIT_BYTE: 1 << 2,
415 MODE_KANJI: 1 << 3,
416 };
417 const QRErrorCorrectLevel = { L: 1, M: 0, Q: 3, H: 2 };
418 const QRMaskPattern = {
419 PATTERN000: 0,
420 PATTERN001: 1,
421 PATTERN010: 2,
422 PATTERN011: 3,
423 PATTERN100: 4,
424 PATTERN101: 5,
425 PATTERN110: 6,
426 PATTERN111: 7,
427 };
428 const QRUtil = {
429 PATTERN_POSITION_TABLE: [
430 [],
431 [6, 18],
432 [6, 22],
433 [6, 26],
434 [6, 30],
435 [6, 34],
436 [6, 22, 38],
437 [6, 24, 42],
438 [6, 26, 46],
439 [6, 28, 50],
440 [6, 30, 54],
441 [6, 32, 58],
442 [6, 34, 62],
443 [6, 26, 46, 66],
444 [6, 26, 48, 70],
445 [6, 26, 50, 74],
446 [6, 30, 54, 78],
447 [6, 30, 56, 82],
448 [6, 30, 58, 86],
449 [6, 34, 62, 90],
450 [6, 28, 50, 72, 94],
451 [6, 26, 50, 74, 98],
452 [6, 30, 54, 78, 102],
453 [6, 28, 54, 80, 106],
454 [6, 32, 58, 84, 110],
455 [6, 30, 58, 86, 114],
456 [6, 34, 62, 90, 118],
457 [6, 26, 50, 74, 98, 122],
458 [6, 30, 54, 78, 102, 126],
459 [6, 26, 52, 78, 104, 130],
460 [6, 30, 56, 82, 108, 134],
461 [6, 34, 60, 86, 112, 138],
462 [6, 30, 58, 86, 114, 142],
463 [6, 34, 62, 90, 118, 146],
464 [6, 30, 54, 78, 102, 126, 150],
465 [6, 24, 50, 76, 102, 128, 154],
466 [6, 28, 54, 80, 106, 132, 158],
467 [6, 32, 58, 84, 110, 136, 162],
468 [6, 26, 54, 82, 110, 138, 166],
469 [6, 30, 58, 86, 114, 142, 170],
470 ],
471 G15: (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0),
472 G18:
473 (1 << 12) |
474 (1 << 11) |
475 (1 << 10) |
476 (1 << 9) |
477 (1 << 8) |
478 (1 << 5) |
479 (1 << 2) |
480 (1 << 0),
481 G15_MASK: (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1),
482 getBCHTypeInfo(data) {
483 let d = data << 10;
484 while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) {
485 d ^= QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15));
486 }
487 return ((data << 10) | d) ^ QRUtil.G15_MASK;
488 },
489 getBCHTypeNumber(data) {
490 let d = data << 12;
491 while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) {
492 d ^= QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18));
493 }
494 return (data << 12) | d;
495 },
496 getBCHDigit(data) {
497 let digit = 0;
498 while (data != 0) {
499 digit++;
500 data >>>= 1;
501 }
502 return digit;
503 },
504 getPatternPosition(typeNumber) {
505 return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
506 },
507 getMask(maskPattern, i, j) {
508 switch (maskPattern) {
509 case QRMaskPattern.PATTERN000:
510 return (i + j) % 2 == 0;
511 case QRMaskPattern.PATTERN001:
512 return i % 2 == 0;
513 case QRMaskPattern.PATTERN010:
514 return j % 3 == 0;
515 case QRMaskPattern.PATTERN011:
516 return (i + j) % 3 == 0;
517 case QRMaskPattern.PATTERN100:
518 return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
519 case QRMaskPattern.PATTERN101:
520 return ((i * j) % 2) + ((i * j) % 3) == 0;
521 case QRMaskPattern.PATTERN110:
522 return (((i * j) % 2) + ((i * j) % 3)) % 2 == 0;
523 case QRMaskPattern.PATTERN111:
524 return (((i * j) % 3) + ((i + j) % 2)) % 2 == 0;
525 default:
526 throw new Error(`bad maskPattern:${maskPattern}`);
527 }
528 },
529 getErrorCorrectPolynomial(errorCorrectLength) {
530 let a = new QRPolynomial([1], 0);
531 for (let i = 0; i < errorCorrectLength; i++) {
532 a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0));
533 }
534 return a;
535 },
536 getLengthInBits(mode, type) {
537 if (1 <= type && type < 10) {
538 switch (mode) {
539 case QRMode.MODE_NUMBER:
540 return 10;
541 case QRMode.MODE_ALPHA_NUM:
542 return 9;
543 case QRMode.MODE_8BIT_BYTE:
544 return 8;
545 case QRMode.MODE_KANJI:
546 return 8;
547 default:
548 throw new Error(`mode:${mode}`);
549 }
550 } else if (type < 27) {
551 switch (mode) {
552 case QRMode.MODE_NUMBER:
553 return 12;
554 case QRMode.MODE_ALPHA_NUM:
555 return 11;
556 case QRMode.MODE_8BIT_BYTE:
557 return 16;
558 case QRMode.MODE_KANJI:
559 return 10;
560 default:
561 throw new Error(`mode:${mode}`);
562 }
563 } else if (type < 41) {
564 switch (mode) {
565 case QRMode.MODE_NUMBER:
566 return 14;
567 case QRMode.MODE_ALPHA_NUM:
568 return 13;
569 case QRMode.MODE_8BIT_BYTE:
570 return 16;
571 case QRMode.MODE_KANJI:
572 return 12;
573 default:
574 throw new Error(`mode:${mode}`);
575 }
576 } else {
577 throw new Error(`type:${type}`);
578 }
579 },
580 getLostPoint(qrCode) {
581 const moduleCount = qrCode.getModuleCount();
582 let lostPoint = 0;
583 for (let row = 0; row < moduleCount; row++) {
584 for (let col = 0; col < moduleCount; col++) {
585 let sameCount = 0;
586 const dark = qrCode.isDark(row, col);
587 for (let r = -1; r <= 1; r++) {
588 if (row + r < 0 || moduleCount <= row + r) {
589 continue;
590 }
591 for (let c = -1; c <= 1; c++) {
592 if (col + c < 0 || moduleCount <= col + c) {
593 continue;
594 }
595 if (r == 0 && c == 0) {
596 continue;
597 }
598 if (dark == qrCode.isDark(row + r, col + c)) {
599 sameCount++;
600 }
601 }
602 }
603 if (sameCount > 5) {
604 lostPoint += 3 + sameCount - 5;
605 }
606 }
607 }
608 for (let row = 0; row < moduleCount - 1; row++) {
609 for (let col = 0; col < moduleCount - 1; col++) {
610 let count = 0;
611 if (qrCode.isDark(row, col)) count++;
612 if (qrCode.isDark(row + 1, col)) count++;
613 if (qrCode.isDark(row, col + 1)) count++;
614 if (qrCode.isDark(row + 1, col + 1)) count++;
615 if (count == 0 || count == 4) {
616 lostPoint += 3;
617 }
618 }
619 }
620 for (let row = 0; row < moduleCount; row++) {
621 for (let col = 0; col < moduleCount - 6; col++) {
622 if (
623 qrCode.isDark(row, col) &&
624 !qrCode.isDark(row, col + 1) &&
625 qrCode.isDark(row, col + 2) &&
626 qrCode.isDark(row, col + 3) &&
627 qrCode.isDark(row, col + 4) &&
628 !qrCode.isDark(row, col + 5) &&
629 qrCode.isDark(row, col + 6)
630 ) {
631 lostPoint += 40;
632 }
633 }
634 }
635 for (let col = 0; col < moduleCount; col++) {
636 for (let row = 0; row < moduleCount - 6; row++) {
637 if (
638 qrCode.isDark(row, col) &&
639 !qrCode.isDark(row + 1, col) &&
640 qrCode.isDark(row + 2, col) &&
641 qrCode.isDark(row + 3, col) &&
642 qrCode.isDark(row + 4, col) &&
643 !qrCode.isDark(row + 5, col) &&
644 qrCode.isDark(row + 6, col)
645 ) {
646 lostPoint += 40;
647 }
648 }
649 }
650 let darkCount = 0;
651 for (let col = 0; col < moduleCount; col++) {
652 for (let row = 0; row < moduleCount; row++) {
653 if (qrCode.isDark(row, col)) {
654 darkCount++;
655 }
656 }
657 }
658 const ratio = Math.abs((100 * darkCount) / moduleCount / moduleCount - 50) / 5;
659 lostPoint += ratio * 10;
660 return lostPoint;
661 },
662 };
663 const QRMath = {
664 glog(n) {
665 if (n < 1) {
666 throw new Error(`glog(${n})`);
667 }
668 return QRMath.LOG_TABLE[n];
669 },
670 gexp(n) {
671 while (n < 0) {
672 n += 255;
673 }
674 while (n >= 256) {
675 n -= 255;
676 }
677 return QRMath.EXP_TABLE[n];
678 },
679 EXP_TABLE: new Array(256),
680 LOG_TABLE: new Array(256),
681 };
682 for (let i = 0; i < 8; i++) {
683 QRMath.EXP_TABLE[i] = 1 << i;
684 }
685 for (let i = 8; i < 256; i++) {
686 QRMath.EXP_TABLE[i] =
687 QRMath.EXP_TABLE[i - 4] ^
688 QRMath.EXP_TABLE[i - 5] ^
689 QRMath.EXP_TABLE[i - 6] ^
690 QRMath.EXP_TABLE[i - 8];
691 }
692 for (let i = 0; i < 255; i++) {
693 QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
694 }
695
696 class QRPolynomial {
697 constructor(num, shift) {
698 if (num.length == undefined) {
699 throw new Error(`${num.length}/${shift}`);
700 }
701 let offset = 0;
702 while (offset < num.length && num[offset] == 0) {
703 offset++;
704 }
705 this.num = new Array(num.length - offset + shift);
706 for (let i = 0; i < num.length - offset; i++) {
707 this.num[i] = num[i + offset];
708 }
709 }
710
711 get(index) {
712 return this.num[index];
713 }
714 getLength() {
715 return this.num.length;
716 }
717
718 multiply(e) {
719 const num = new Array(this.getLength() + e.getLength() - 1);
720 for (let i = 0; i < this.getLength(); i++) {
721 for (let j = 0; j < e.getLength(); j++) {
722 num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
723 }
724 }
725 return new QRPolynomial(num, 0);
726 }
727
728 mod(e) {
729 if (this.getLength() - e.getLength() < 0) {
730 return this;
731 }
732 const ratio = QRMath.glog(this.get(0)) - QRMath.glog(e.get(0));
733 const num = new Array(this.getLength());
734 for (let i = 0; i < this.getLength(); i++) {
735 num[i] = this.get(i);
736 }
737 for (let i = 0; i < e.getLength(); i++) {
738 num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio);
739 }
740 return new QRPolynomial(num, 0).mod(e);
741 }
742 }
743
744 class QRRSBlock {
745 constructor(totalCount, dataCount) {
746 this.totalCount = totalCount;
747 this.dataCount = dataCount;
748 }
749
750 static getRSBlocks(typeNumber, errorCorrectLevel) {
751 const rsBlock = QRRSBlock.getRsBlockTable(typeNumber, errorCorrectLevel);
752 if (rsBlock == undefined) {
753 throw new Error(
754 `bad rs block @ typeNumber:${typeNumber}/errorCorrectLevel:${errorCorrectLevel}`
755 );
756 }
757 const length = rsBlock.length / 3;
758 const list = [];
759 for (let i = 0; i < length; i++) {
760 const count = rsBlock[i * 3 + 0];
761 const totalCount = rsBlock[i * 3 + 1];
762 const dataCount = rsBlock[i * 3 + 2];
763 for (let j = 0; j < count; j++) {
764 list.push(new QRRSBlock(totalCount, dataCount));
765 }
766 }
767 return list;
768 }
769
770 static getRsBlockTable(typeNumber, errorCorrectLevel) {
771 switch (errorCorrectLevel) {
772 case QRErrorCorrectLevel.L:
773 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
774 case QRErrorCorrectLevel.M:
775 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
776 case QRErrorCorrectLevel.Q:
777 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
778 case QRErrorCorrectLevel.H:
779 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
780 default:
781 return undefined;
782 }
783 }
784 }
785
786 QRRSBlock.RS_BLOCK_TABLE = [
787 [1, 26, 19],
788 [1, 26, 16],
789 [1, 26, 13],
790 [1, 26, 9],
791 [1, 44, 34],
792 [1, 44, 28],
793 [1, 44, 22],
794 [1, 44, 16],
795 [1, 70, 55],
796 [1, 70, 44],
797 [2, 35, 17],
798 [2, 35, 13],
799 [1, 100, 80],
800 [2, 50, 32],
801 [2, 50, 24],
802 [4, 25, 9],
803 [1, 134, 108],
804 [2, 67, 43],
805 [2, 33, 15, 2, 34, 16],
806 [2, 33, 11, 2, 34, 12],
807 [2, 86, 68],
808 [4, 43, 27],
809 [4, 43, 19],
810 [4, 43, 15],
811 [2, 98, 78],
812 [4, 49, 31],
813 [2, 32, 14, 4, 33, 15],
814 [4, 39, 13, 1, 40, 14],
815 [2, 121, 97],
816 [2, 60, 38, 2, 61, 39],
817 [4, 40, 18, 2, 41, 19],
818 [4, 40, 14, 2, 41, 15],
819 [2, 146, 116],
820 [3, 58, 36, 2, 59, 37],
821 [4, 36, 16, 4, 37, 17],
822 [4, 36, 12, 4, 37, 13],
823 [2, 86, 68, 2, 87, 69],
824 [4, 69, 43, 1, 70, 44],
825 [6, 43, 19, 2, 44, 20],
826 [6, 43, 15, 2, 44, 16],
827 [4, 101, 81],
828 [1, 80, 50, 4, 81, 51],
829 [4, 50, 22, 4, 51, 23],
830 [3, 36, 12, 8, 37, 13],
831 [2, 116, 92, 2, 117, 93],
832 [6, 58, 36, 2, 59, 37],
833 [4, 46, 20, 6, 47, 21],
834 [7, 42, 14, 4, 43, 15],
835 [4, 133, 107],
836 [8, 59, 37, 1, 60, 38],
837 [8, 44, 20, 4, 45, 21],
838 [12, 33, 11, 4, 34, 12],
839 [3, 145, 115, 1, 146, 116],
840 [4, 64, 40, 5, 65, 41],
841 [11, 36, 16, 5, 37, 17],
842 [11, 36, 12, 5, 37, 13],
843 [5, 109, 87, 1, 110, 88],
844 [5, 65, 41, 5, 66, 42],
845 [5, 54, 24, 7, 55, 25],
846 [11, 36, 12],
847 [5, 122, 98, 1, 123, 99],
848 [7, 73, 45, 3, 74, 46],
849 [15, 43, 19, 2, 44, 20],
850 [3, 45, 15, 13, 46, 16],
851 [1, 135, 107, 5, 136, 108],
852 [10, 74, 46, 1, 75, 47],
853 [1, 50, 22, 15, 51, 23],
854 [2, 42, 14, 17, 43, 15],
855 [5, 150, 120, 1, 151, 121],
856 [9, 69, 43, 4, 70, 44],
857 [17, 50, 22, 1, 51, 23],
858 [2, 42, 14, 19, 43, 15],
859 [3, 141, 113, 4, 142, 114],
860 [3, 70, 44, 11, 71, 45],
861 [17, 47, 21, 4, 48, 22],
862 [9, 39, 13, 16, 40, 14],
863 [3, 135, 107, 5, 136, 108],
864 [3, 67, 41, 13, 68, 42],
865 [15, 54, 24, 5, 55, 25],
866 [15, 43, 15, 10, 44, 16],
867 [4, 144, 116, 4, 145, 117],
868 [17, 68, 42],
869 [17, 50, 22, 6, 51, 23],
870 [19, 46, 16, 6, 47, 17],
871 [2, 139, 111, 7, 140, 112],
872 [17, 74, 46],
873 [7, 54, 24, 16, 55, 25],
874 [34, 37, 13],
875 [4, 151, 121, 5, 152, 122],
876 [4, 75, 47, 14, 76, 48],
877 [11, 54, 24, 14, 55, 25],
878 [16, 45, 15, 14, 46, 16],
879 [6, 147, 117, 4, 148, 118],
880 [6, 73, 45, 14, 74, 46],
881 [11, 54, 24, 16, 55, 25],
882 [30, 46, 16, 2, 47, 17],
883 [8, 132, 106, 4, 133, 107],
884 [8, 75, 47, 13, 76, 48],
885 [7, 54, 24, 22, 55, 25],
886 [22, 45, 15, 13, 46, 16],
887 [10, 142, 114, 2, 143, 115],
888 [19, 74, 46, 4, 75, 47],
889 [28, 50, 22, 6, 51, 23],
890 [33, 46, 16, 4, 47, 17],
891 [8, 152, 122, 4, 153, 123],
892 [22, 73, 45, 3, 74, 46],
893 [8, 53, 23, 26, 54, 24],
894 [12, 45, 15, 28, 46, 16],
895 [3, 147, 117, 10, 148, 118],
896 [3, 73, 45, 23, 74, 46],
897 [4, 54, 24, 31, 55, 25],
898 [11, 45, 15, 31, 46, 16],
899 [7, 146, 116, 7, 147, 117],
900 [21, 73, 45, 7, 74, 46],
901 [1, 53, 23, 37, 54, 24],
902 [19, 45, 15, 26, 46, 16],
903 [5, 145, 115, 10, 146, 116],
904 [19, 75, 47, 10, 76, 48],
905 [15, 54, 24, 25, 55, 25],
906 [23, 45, 15, 25, 46, 16],
907 [13, 145, 115, 3, 146, 116],
908 [2, 74, 46, 29, 75, 47],
909 [42, 54, 24, 1, 55, 25],
910 [23, 45, 15, 28, 46, 16],
911 [17, 145, 115],
912 [10, 74, 46, 23, 75, 47],
913 [10, 54, 24, 35, 55, 25],
914 [19, 45, 15, 35, 46, 16],
915 [17, 145, 115, 1, 146, 116],
916 [14, 74, 46, 21, 75, 47],
917 [29, 54, 24, 19, 55, 25],
918 [11, 45, 15, 46, 46, 16],
919 [13, 145, 115, 6, 146, 116],
920 [14, 74, 46, 23, 75, 47],
921 [44, 54, 24, 7, 55, 25],
922 [59, 46, 16, 1, 47, 17],
923 [12, 151, 121, 7, 152, 122],
924 [12, 75, 47, 26, 76, 48],
925 [39, 54, 24, 14, 55, 25],
926 [22, 45, 15, 41, 46, 16],
927 [6, 151, 121, 14, 152, 122],
928 [6, 75, 47, 34, 76, 48],
929 [46, 54, 24, 10, 55, 25],
930 [2, 45, 15, 64, 46, 16],
931 [17, 152, 122, 4, 153, 123],
932 [29, 74, 46, 14, 75, 47],
933 [49, 54, 24, 10, 55, 25],
934 [24, 45, 15, 46, 46, 16],
935 [4, 152, 122, 18, 153, 123],
936 [13, 74, 46, 32, 75, 47],
937 [48, 54, 24, 14, 55, 25],
938 [42, 45, 15, 32, 46, 16],
939 [20, 147, 117, 4, 148, 118],
940 [40, 75, 47, 7, 76, 48],
941 [43, 54, 24, 22, 55, 25],
942 [10, 45, 15, 67, 46, 16],
943 [19, 148, 118, 6, 149, 119],
944 [18, 75, 47, 31, 76, 48],
945 [34, 54, 24, 34, 55, 25],
946 [20, 45, 15, 61, 46, 16],
947 ];
948
949 class QRBitBuffer {
950 constructor() {
951 this.buffer = [];
952 this.length = 0;
953 }
954 get(index) {
955 const bufIndex = Math.floor(index / 8);
956 return ((this.buffer[bufIndex] >>> (7 - (index % 8))) & 1) == 1;
957 }
958 put(num, length) {
959 for (let i = 0; i < length; i++) {
960 this.putBit(((num >>> (length - i - 1)) & 1) == 1);
961 }
962 }
963 getLengthInBits() {
964 return this.length;
965 }
966
967 putBit(bit) {
968 const bufIndex = Math.floor(this.length / 8);
969 if (this.buffer.length <= bufIndex) {
970 this.buffer.push(0);
971 }
972 if (bit) {
973 this.buffer[bufIndex] |= 0x80 >>> this.length % 8;
974 }
975 this.length++;
976 }
977 }
978
979 const QRCodeLimitLength = [
980 [17, 14, 11, 7],
981 [32, 26, 20, 14],
982 [53, 42, 32, 24],
983 [78, 62, 46, 34],
984 [106, 84, 60, 44],
985 [134, 106, 74, 58],
986 [154, 122, 86, 64],
987 [192, 152, 108, 84],
988 [230, 180, 130, 98],
989 [271, 213, 151, 119],
990 [321, 251, 177, 137],
991 [367, 287, 203, 155],
992 [425, 331, 241, 177],
993 [458, 362, 258, 194],
994 [520, 412, 292, 220],
995 [586, 450, 322, 250],
996 [644, 504, 364, 280],
997 [718, 560, 394, 310],
998 [792, 624, 442, 338],
999 [858, 666, 482, 382],
1000 [929, 711, 509, 403],
1001 [1003, 779, 565, 439],
1002 [1091, 857, 611, 461],
1003 [1171, 911, 661, 511],
1004 [1273, 997, 715, 535],
1005 [1367, 1059, 751, 593],
1006 [1465, 1125, 805, 625],
1007 [1528, 1190, 868, 658],
1008 [1628, 1264, 908, 698],
1009 [1732, 1370, 982, 742],
1010 [1840, 1452, 1030, 790],
1011 [1952, 1538, 1112, 842],
1012 [2068, 1628, 1168, 898],
1013 [2188, 1722, 1228, 958],
1014 [2303, 1809, 1283, 983],
1015 [2431, 1911, 1351, 1051],
1016 [2563, 1989, 1423, 1093],
1017 [2699, 2099, 1499, 1139],
1018 [2809, 2213, 1579, 1219],
1019 [2953, 2331, 1663, 1273],
1020 ];
1021
1022 // android 2.x doesn't support Data-URI spec
1023 function _getAndroid() {
1024 let android = false;
1025 const sAgent = navigator.userAgent;
1026
1027 if (/android/i.test(sAgent)) {
1028 // android
1029 android = true;
1030 const aMat = sAgent.toString().match(/android ([0-9]\.[0-9])/i);
1031
1032 if (aMat && aMat[1]) {
1033 android = parseFloat(aMat[1]);
1034 }
1035 }
1036
1037 return android;
1038 }
1039
1040 const svgDrawer = (() => {
1041 const Drawing = function (el, htOption) {
1042 this._el = el;
1043 this._htOption = htOption;
1044 };
1045
1046 Drawing.prototype.draw = function (oQRCode) {
1047 const _htOption = this._htOption;
1048 const _el = this._el;
1049 const nCount = oQRCode.getModuleCount();
1050 const nWidth = Math.floor(_htOption.width / nCount);
1051 const nHeight = Math.floor(_htOption.height / nCount);
1052
1053 this.clear();
1054
1055 function makeSVG(tag, attrs) {
1056 const el = document.createElementNS("http://www.w3.org/2000/svg", tag);
1057 for (const k in attrs) if (attrs.hasOwnProperty(k)) el.setAttribute(k, attrs[k]);
1058 return el;
1059 }
1060
1061 const svg = makeSVG("svg", {
1062 viewBox: `0 0 ${String(nCount)} ${String(nCount)}`,
1063 width: "100%",
1064 height: "100%",
1065 fill: _htOption.colorLight,
1066 });
1067 svg.setAttributeNS(
1068 "http://www.w3.org/2000/xmlns/",
1069 "xmlns:xlink",
1070 "http://www.w3.org/1999/xlink"
1071 );
1072 _el.appendChild(svg);
1073
1074 svg.appendChild(
1075 makeSVG("rect", { fill: _htOption.colorLight, width: "100%", height: "100%" })
1076 );
1077 svg.appendChild(
1078 makeSVG("rect", {
1079 fill: _htOption.colorDark,
1080 width: "1",
1081 height: "1",
1082 id: "template",
1083 })
1084 );
1085
1086 for (let row = 0; row < nCount; row++) {
1087 for (let col = 0; col < nCount; col++) {
1088 if (oQRCode.isDark(row, col)) {
1089 const child = makeSVG("use", { x: String(col), y: String(row) });
1090 child.setAttributeNS("http://www.w3.org/1999/xlink", "href", "#template");
1091 svg.appendChild(child);
1092 }
1093 }
1094 }
1095 };
1096 Drawing.prototype.clear = function () {
1097 while (this._el.hasChildNodes()) this._el.removeChild(this._el.lastChild);
1098 };
1099 return Drawing;
1100 })();
1101
1102 const useSVG = document.documentElement.tagName.toLowerCase() === "svg";
1103
1104 // Drawing in DOM by using Table tag
1105 let Drawing = useSVG
1106 ? svgDrawer
1107 : (function () {
1108 // Drawing in Canvas
1109 function _onMakeImage() {
1110 this._elImage.src = this._elCanvas.toDataURL("image/png");
1111 this._elImage.style.display = "block";
1112 this._elCanvas.style.display = "none";
1113 }
1114
1115 // Android 2.1 bug workaround
1116 // http://code.google.com/p/android/issues/detail?id=5141
1117 if (this._android && this._android <= 2.1) {
1118 const factor = 1 / window.devicePixelRatio;
1119 const drawImage = CanvasRenderingContext2D.prototype.drawImage;
1120 CanvasRenderingContext2D.prototype.drawImage = function (
1121 image,
1122 sx,
1123 sy,
1124 sw,
1125 sh,
1126 dx,
1127 dy,
1128 dw,
1129 dh
1130 ) {
1131 if ("nodeName" in image && /img/i.test(image.nodeName)) {
1132 for (let i = arguments.length - 1; i >= 1; i--) {
1133 arguments[i] = arguments[i] * factor;
1134 }
1135 } else if (typeof dw == "undefined") {
1136 arguments[1] *= factor;
1137 arguments[2] *= factor;
1138 arguments[3] *= factor;
1139 arguments[4] *= factor;
1140 }
1141
1142 drawImage.apply(this, arguments);
1143 };
1144 }
1145
1146 /**
1147 * Check whether the user's browser supports Data URI or not
1148 *
1149 * @private
1150 * @param {Function} fSuccess Occurs if it supports Data URI
1151 * @param {Function} fFail Occurs if it doesn't support Data URI
1152 */
1153 function _safeSetDataURI(fSuccess, fFail) {
1154 const self = this;
1155 self._fFail = fFail;
1156 self._fSuccess = fSuccess;
1157
1158 // Check it just once
1159 if (self._bSupportDataURI === null) {
1160 const el = document.createElement("img");
1161 const fOnError = () => {
1162 self._bSupportDataURI = false;
1163
1164 if (self._fFail) {
1165 self._fFail.call(self);
1166 }
1167 };
1168 const fOnSuccess = () => {
1169 self._bSupportDataURI = true;
1170
1171 if (self._fSuccess) {
1172 self._fSuccess.call(self);
1173 }
1174 };
1175
1176 el.onabort = fOnError;
1177 el.onerror = fOnError;
1178 el.onload = fOnSuccess;
1179 el.src =
1180 "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // the Image contains 1px data.
1181 return;
1182 } else if (self._bSupportDataURI === true && self._fSuccess) {
1183 self._fSuccess.call(self);
1184 } else if (self._bSupportDataURI === false && self._fFail) {
1185 self._fFail.call(self);
1186 }
1187 }
1188
1189 /**
1190 * Drawing QRCode by using canvas
1191 *
1192 * @constructor
1193 * @param {HTMLElement} el
1194 * @param {Object} htOption QRCode Options
1195 */
1196 class Drawing {
1197 constructor(el, htOption) {
1198 this._bIsPainted = false;
1199 this._android = _getAndroid();
1200
1201 this._htOption = htOption;
1202 this._elCanvas = document.createElement("canvas");
1203 this._elCanvas.width = htOption.width;
1204 this._elCanvas.height = htOption.height;
1205 el.appendChild(this._elCanvas);
1206 this._el = el;
1207 this._oContext = this._elCanvas.getContext("2d");
1208 this._bIsPainted = false;
1209 this._elImage = document.createElement("img");
1210 this._elImage.alt = "Scan me!";
1211 this._elImage.style.display = "none";
1212 this._el.appendChild(this._elImage);
1213 this._bSupportDataURI = null;
1214 }
1215
1216 /**
1217 * Draw the QRCode
1218 *
1219 * @param {QRCode} oQRCode
1220 */
1221 draw(oQRCode) {
1222 const _elImage = this._elImage;
1223 const _oContext = this._oContext;
1224 const _htOption = this._htOption;
1225
1226 const nCount = oQRCode.getModuleCount();
1227 const nWidth = _htOption.width / nCount;
1228 const nHeight = _htOption.height / nCount;
1229 const nRoundedWidth = Math.round(nWidth);
1230 const nRoundedHeight = Math.round(nHeight);
1231
1232 _elImage.style.display = "none";
1233 this.clear();
1234
1235 for (let row = 0; row < nCount; row++) {
1236 for (let col = 0; col < nCount; col++) {
1237 const bIsDark = oQRCode.isDark(row, col);
1238 const nLeft = col * nWidth;
1239 const nTop = row * nHeight;
1240 _oContext.strokeStyle = bIsDark
1241 ? _htOption.colorDark
1242 : _htOption.colorLight;
1243 _oContext.lineWidth = 1;
1244 _oContext.fillStyle = bIsDark
1245 ? _htOption.colorDark
1246 : _htOption.colorLight;
1247 _oContext.fillRect(nLeft, nTop, nWidth, nHeight);
1248
1249 // 안티 앨리어싱 방지 처리
1250 _oContext.strokeRect(
1251 Math.floor(nLeft) + 0.5,
1252 Math.floor(nTop) + 0.5,
1253 nRoundedWidth,
1254 nRoundedHeight
1255 );
1256
1257 _oContext.strokeRect(
1258 Math.ceil(nLeft) - 0.5,
1259 Math.ceil(nTop) - 0.5,
1260 nRoundedWidth,
1261 nRoundedHeight
1262 );
1263 }
1264 }
1265
1266 this._bIsPainted = true;
1267 }
1268
1269 /**
1270 * Make the image from Canvas if the browser supports Data URI.
1271 */
1272 makeImage() {
1273 if (this._bIsPainted) {
1274 _safeSetDataURI.call(this, _onMakeImage);
1275 }
1276 }
1277
1278 /**
1279 * Return whether the QRCode is painted or not
1280 *
1281 * @return {Boolean}
1282 */
1283 isPainted() {
1284 return this._bIsPainted;
1285 }
1286
1287 /**
1288 * Clear the QRCode
1289 */
1290 clear() {
1291 this._oContext.clearRect(0, 0, this._elCanvas.width, this._elCanvas.height);
1292 this._bIsPainted = false;
1293 }
1294
1295 /**
1296 * @private
1297 * @param {Number} nNumber
1298 */
1299 round(nNumber) {
1300 if (!nNumber) {
1301 return nNumber;
1302 }
1303
1304 return Math.floor(nNumber * 1000) / 1000;
1305 }
1306 }
1307
1308 return Drawing;
1309 })();
1310
1311 /**
1312 * Get the type by string length
1313 *
1314 * @private
1315 * @param {String} sText
1316 * @param {Number} nCorrectLevel
1317 * @return {Number} type
1318 */
1319 function _getTypeNumber(sText, nCorrectLevel) {
1320 let nType = 1;
1321 const length = _getUTF8Length(sText);
1322
1323 for (let i = 0, len = QRCodeLimitLength.length; i <= len; i++) {
1324 let nLimit = 0;
1325
1326 switch (nCorrectLevel) {
1327 case QRErrorCorrectLevel.L:
1328 nLimit = QRCodeLimitLength[i][0];
1329 break;
1330 case QRErrorCorrectLevel.M:
1331 nLimit = QRCodeLimitLength[i][1];
1332 break;
1333 case QRErrorCorrectLevel.Q:
1334 nLimit = QRCodeLimitLength[i][2];
1335 break;
1336 case QRErrorCorrectLevel.H:
1337 nLimit = QRCodeLimitLength[i][3];
1338 break;
1339 }
1340
1341 if (length <= nLimit) {
1342 break;
1343 } else {
1344 nType++;
1345 }
1346 }
1347
1348 if (nType > QRCodeLimitLength.length) {
1349 throw new Error("Too long data");
1350 }
1351
1352 return nType;
1353 }
1354
1355 function _getUTF8Length(sText) {
1356 const replacedText = encodeURI(sText)
1357 .toString()
1358 .replace(/\%[0-9a-fA-F]{2}/g, "a");
1359 return replacedText.length + (replacedText.length != sText ? 3 : 0);
1360 }
1361
1362 /**
1363 * @class QRCode
1364 * @constructor
1365 * @example
1366 * new QRCode(document.getElementById("test"), "http://jindo.dev.naver.com/collie");
1367 *
1368 * @example
1369 * const oQRCode = new QRCode("test", {
1370 * text : "http://naver.com",
1371 * width : 128,
1372 * height : 128
1373 * });
1374 *
1375 * oQRCode.clear(); // Clear the QRCode.
1376 * oQRCode.makeCode("http://map.naver.com"); // Re-create the QRCode.
1377 *
1378 * @param {HTMLElement|String} el target element or 'id' attribute of element.
1379 * @param {Object|String} vOption
1380 * @param {String} vOption.text QRCode link data
1381 * @param {Number} [vOption.width=256]
1382 * @param {Number} [vOption.height=256]
1383 * @param {String} [vOption.colorDark="#000000"]
1384 * @param {String} [vOption.colorLight="#ffffff"]
1385 * @param {QRCode.CorrectLevel} [vOption.correctLevel=QRCode.CorrectLevel.H] [L|M|Q|H]
1386 */
1387 QRCode = function (el, vOption) {
1388 this._htOption = {
1389 width: 256,
1390 height: 256,
1391 typeNumber: 4,
1392 colorDark: "#000000",
1393 colorLight: "#ffffff",
1394 correctLevel: QRErrorCorrectLevel.H,
1395 };
1396
1397 if (typeof vOption === "string") {
1398 vOption = {
1399 text: vOption,
1400 };
1401 }
1402
1403 // Overwrites options
1404 if (vOption) {
1405 for (const i in vOption) {
1406 this._htOption[i] = vOption[i];
1407 }
1408 }
1409
1410 if (typeof el == "string") {
1411 el = document.getElementById(el);
1412 }
1413
1414 if (this._htOption.useSVG) {
1415 Drawing = svgDrawer;
1416 }
1417
1418 this._android = _getAndroid();
1419 this._el = el;
1420 this._oQRCode = null;
1421 this._oDrawing = new Drawing(this._el, this._htOption);
1422
1423 if (this._htOption.text) {
1424 this.makeCode(this._htOption.text);
1425 }
1426 };
1427
1428 /**
1429 * Make the QRCode
1430 *
1431 * @param {String} sText link data
1432 */
1433 QRCode.prototype.makeCode = function (sText) {
1434 this._oQRCode = new QRCodeModel(
1435 _getTypeNumber(sText, this._htOption.correctLevel),
1436 this._htOption.correctLevel
1437 );
1438 this._oQRCode.addData(sText);
1439 this._oQRCode.make();
1440 this._el.title = sText;
1441 this._oDrawing.draw(this._oQRCode);
1442 this.makeImage();
1443 };
1444
1445 /**
1446 * Make the Image from Canvas element
1447 * - It occurs automatically
1448 * - Android below 3 doesn't support Data-URI spec.
1449 *
1450 * @private
1451 */
1452 QRCode.prototype.makeImage = function () {
1453 if (
1454 typeof this._oDrawing.makeImage == "function" &&
1455 (!this._android || this._android >= 3)
1456 ) {
1457 this._oDrawing.makeImage();
1458 }
1459 };
1460
1461 /**
1462 * Clear the QRCode
1463 */
1464 QRCode.prototype.clear = function () {
1465 this._oDrawing.clear();
1466 };
1467
1468 /**
1469 * @name QRCode.CorrectLevel
1470 */
1471 QRCode.CorrectLevel = QRErrorCorrectLevel;
1472 })();