Total Complexity | 389 |
Total Lines | 2872 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like qrcode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use qrcode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
318 | class qrcode |
||
319 | { |
||
320 | /** |
||
321 | * @var barcode array to be returned which is readable by TCPDF |
||
322 | */ |
||
323 | protected $barcode_array = []; |
||
324 | |||
325 | /** |
||
326 | * @var QR code version. Size of QRcode is defined as version. Version is from 1 to 40. Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases. So version 40 is 177*177 matrix. |
||
327 | */ |
||
328 | protected $version = 0; |
||
329 | |||
330 | /** |
||
331 | * @var Levels of error correction. See definitions for possible values. |
||
332 | */ |
||
333 | protected $level = QR_ECLEVEL_L; |
||
334 | |||
335 | /** |
||
336 | * @var Encoding mode |
||
337 | */ |
||
338 | protected $hint = QR_MODE_8B; |
||
339 | |||
340 | /** |
||
341 | * @var if true the input string will be converted to uppercase |
||
342 | */ |
||
343 | protected $casesensitive = true; |
||
344 | |||
345 | /** |
||
346 | * @var structured QR code (not supported yet) |
||
347 | */ |
||
348 | protected $structured = 0; |
||
349 | |||
350 | /** |
||
351 | * @var mask data |
||
352 | */ |
||
353 | protected $data; |
||
354 | |||
355 | // FrameFiller |
||
356 | |||
357 | /** |
||
358 | * @var width |
||
359 | */ |
||
360 | protected $width; |
||
361 | |||
362 | /** |
||
363 | * @var frame |
||
364 | */ |
||
365 | protected $frame; |
||
366 | |||
367 | /** |
||
368 | * @var X position of bit |
||
369 | */ |
||
370 | protected $x; |
||
371 | |||
372 | /** |
||
373 | * @var Y position of bit |
||
374 | */ |
||
375 | protected $y; |
||
376 | |||
377 | /** |
||
378 | * @var direction |
||
379 | */ |
||
380 | protected $dir; |
||
381 | |||
382 | /** |
||
383 | * @var single bit |
||
384 | */ |
||
385 | protected $bit; |
||
386 | |||
387 | // ---- QRrawcode ---- |
||
388 | |||
389 | /** |
||
390 | * @var data code |
||
391 | */ |
||
392 | protected $datacode = []; |
||
393 | |||
394 | /** |
||
395 | * @var error correction code |
||
396 | */ |
||
397 | protected $ecccode = []; |
||
398 | |||
399 | /** |
||
400 | * @var blocks |
||
401 | */ |
||
402 | protected $blocks; |
||
403 | |||
404 | /** |
||
405 | * @var Reed-Solomon blocks |
||
406 | */ |
||
407 | protected $rsblocks = []; //of RSblock |
||
408 | |||
409 | /** |
||
410 | * @var counter |
||
411 | */ |
||
412 | protected $count; |
||
413 | |||
414 | /** |
||
415 | * @var data length |
||
416 | */ |
||
417 | protected $dataLength; |
||
418 | |||
419 | /** |
||
420 | * @var error correction length |
||
421 | */ |
||
422 | protected $eccLength; |
||
423 | |||
424 | /** |
||
425 | * @var b1 |
||
426 | */ |
||
427 | protected $b1; |
||
428 | |||
429 | // ---- QRmask ---- |
||
430 | |||
431 | /** |
||
432 | * @var run length |
||
433 | */ |
||
434 | protected $runLength = []; |
||
435 | |||
436 | // ---- QRsplit ---- |
||
437 | |||
438 | /** |
||
439 | * @var input data string |
||
440 | */ |
||
441 | protected $dataStr = ''; |
||
442 | |||
443 | /** |
||
444 | * @var input items |
||
445 | */ |
||
446 | protected $items; |
||
447 | |||
448 | // Reed-Solomon items |
||
449 | |||
450 | /** |
||
451 | * @var Reed-Solomon items |
||
452 | */ |
||
453 | protected $rsitems = []; |
||
454 | |||
455 | /** |
||
456 | * @var array of frames |
||
457 | */ |
||
458 | protected $frames = []; |
||
459 | |||
460 | /** |
||
461 | * @var alphabet-numeric convesion table |
||
462 | */ |
||
463 | protected $anTable = [ |
||
464 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // |
||
465 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // |
||
466 | 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, // |
||
467 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, // |
||
468 | -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // |
||
469 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // |
||
470 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // |
||
471 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // |
||
472 | ]; |
||
473 | |||
474 | /** |
||
475 | * @var array Table of the capacity of symbols |
||
476 | * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004. |
||
477 | */ |
||
478 | protected $capacity = [ |
||
479 | [0, 0, 0, [0, 0, 0, 0]], // |
||
480 | [21, 26, 0, [7, 10, 13, 17]], // 1 |
||
481 | [25, 44, 7, [10, 16, 22, 28]], // |
||
482 | [29, 70, 7, [15, 26, 36, 44]], // |
||
483 | [33, 100, 7, [20, 36, 52, 64]], // |
||
484 | [37, 134, 7, [26, 48, 72, 88]], // 5 |
||
485 | [41, 172, 7, [36, 64, 96, 112]], // |
||
486 | [45, 196, 0, [40, 72, 108, 130]], // |
||
487 | [49, 242, 0, [48, 88, 132, 156]], // |
||
488 | [53, 292, 0, [60, 110, 160, 192]], // |
||
489 | [57, 346, 0, [72, 130, 192, 224]], // 10 |
||
490 | [61, 404, 0, [80, 150, 224, 264]], // |
||
491 | [65, 466, 0, [96, 176, 260, 308]], // |
||
492 | [69, 532, 0, [104, 198, 288, 352]], // |
||
493 | [73, 581, 3, [120, 216, 320, 384]], // |
||
494 | [77, 655, 3, [132, 240, 360, 432]], // 15 |
||
495 | [81, 733, 3, [144, 280, 408, 480]], // |
||
496 | [85, 815, 3, [168, 308, 448, 532]], // |
||
497 | [89, 901, 3, [180, 338, 504, 588]], // |
||
498 | [93, 991, 3, [196, 364, 546, 650]], // |
||
499 | [97, 1085, 3, [224, 416, 600, 700]], // 20 |
||
500 | [101, 1156, 4, [224, 442, 644, 750]], // |
||
501 | [105, 1258, 4, [252, 476, 690, 816]], // |
||
502 | [109, 1364, 4, [270, 504, 750, 900]], // |
||
503 | [113, 1474, 4, [300, 560, 810, 960]], // |
||
504 | [117, 1588, 4, [312, 588, 870, 1050]], // 25 |
||
505 | [121, 1706, 4, [336, 644, 952, 1110]], // |
||
506 | [125, 1828, 4, [360, 700, 1020, 1200]], // |
||
507 | [129, 1921, 3, [390, 728, 1050, 1260]], // |
||
508 | [133, 2051, 3, [420, 784, 1140, 1350]], // |
||
509 | [137, 2185, 3, [450, 812, 1200, 1440]], // 30 |
||
510 | [141, 2323, 3, [480, 868, 1290, 1530]], // |
||
511 | [145, 2465, 3, [510, 924, 1350, 1620]], // |
||
512 | [149, 2611, 3, [540, 980, 1440, 1710]], // |
||
513 | [153, 2761, 3, [570, 1036, 1530, 1800]], // |
||
514 | [157, 2876, 0, [570, 1064, 1590, 1890]], // 35 |
||
515 | [161, 3034, 0, [600, 1120, 1680, 1980]], // |
||
516 | [165, 3196, 0, [630, 1204, 1770, 2100]], // |
||
517 | [169, 3362, 0, [660, 1260, 1860, 2220]], // |
||
518 | [173, 3532, 0, [720, 1316, 1950, 2310]], // |
||
519 | [177, 3706, 0, [750, 1372, 2040, 2430]], // 40 |
||
520 | ]; |
||
521 | |||
522 | /** |
||
523 | * @var array Length indicator |
||
524 | */ |
||
525 | protected $lengthTableBits = [ |
||
526 | [10, 12, 14], |
||
527 | [9, 11, 13], |
||
528 | [8, 16, 16], |
||
529 | [8, 10, 12], |
||
530 | ]; |
||
531 | |||
532 | /** |
||
533 | * @var array Table of the error correction code (Reed-Solomon block) |
||
534 | * See Table 12-16 (pp.30-36), JIS X0510:2004. |
||
535 | */ |
||
536 | protected $eccTable = [ |
||
537 | [[0, 0], [0, 0], [0, 0], [0, 0]], // |
||
538 | [[1, 0], [1, 0], [1, 0], [1, 0]], // 1 |
||
539 | [[1, 0], [1, 0], [1, 0], [1, 0]], // |
||
540 | [[1, 0], [1, 0], [2, 0], [2, 0]], // |
||
541 | [[1, 0], [2, 0], [2, 0], [4, 0]], // |
||
542 | [[1, 0], [2, 0], [2, 2], [2, 2]], // 5 |
||
543 | [[2, 0], [4, 0], [4, 0], [4, 0]], // |
||
544 | [[2, 0], [4, 0], [2, 4], [4, 1]], // |
||
545 | [[2, 0], [2, 2], [4, 2], [4, 2]], // |
||
546 | [[2, 0], [3, 2], [4, 4], [4, 4]], // |
||
547 | [[2, 2], [4, 1], [6, 2], [6, 2]], // 10 |
||
548 | [[4, 0], [1, 4], [4, 4], [3, 8]], // |
||
549 | [[2, 2], [6, 2], [4, 6], [7, 4]], // |
||
550 | [[4, 0], [8, 1], [8, 4], [12, 4]], // |
||
551 | [[3, 1], [4, 5], [11, 5], [11, 5]], // |
||
552 | [[5, 1], [5, 5], [5, 7], [11, 7]], // 15 |
||
553 | [[5, 1], [7, 3], [15, 2], [3, 13]], // |
||
554 | [[1, 5], [10, 1], [1, 15], [2, 17]], // |
||
555 | [[5, 1], [9, 4], [17, 1], [2, 19]], // |
||
556 | [[3, 4], [3, 11], [17, 4], [9, 16]], // |
||
557 | [[3, 5], [3, 13], [15, 5], [15, 10]], // 20 |
||
558 | [[4, 4], [17, 0], [17, 6], [19, 6]], // |
||
559 | [[2, 7], [17, 0], [7, 16], [34, 0]], // |
||
560 | [[4, 5], [4, 14], [11, 14], [16, 14]], // |
||
561 | [[6, 4], [6, 14], [11, 16], [30, 2]], // |
||
562 | [[8, 4], [8, 13], [7, 22], [22, 13]], // 25 |
||
563 | [[10, 2], [19, 4], [28, 6], [33, 4]], // |
||
564 | [[8, 4], [22, 3], [8, 26], [12, 28]], // |
||
565 | [[3, 10], [3, 23], [4, 31], [11, 31]], // |
||
566 | [[7, 7], [21, 7], [1, 37], [19, 26]], // |
||
567 | [[5, 10], [19, 10], [15, 25], [23, 25]], // 30 |
||
568 | [[13, 3], [2, 29], [42, 1], [23, 28]], // |
||
569 | [[17, 0], [10, 23], [10, 35], [19, 35]], // |
||
570 | [[17, 1], [14, 21], [29, 19], [11, 46]], // |
||
571 | [[13, 6], [14, 23], [44, 7], [59, 1]], // |
||
572 | [[12, 7], [12, 26], [39, 14], [22, 41]], // 35 |
||
573 | [[6, 14], [6, 34], [46, 10], [2, 64]], // |
||
574 | [[17, 4], [29, 14], [49, 10], [24, 46]], // |
||
575 | [[4, 18], [13, 32], [48, 14], [42, 32]], // |
||
576 | [[20, 4], [40, 7], [43, 22], [10, 67]], // |
||
577 | [[19, 6], [18, 31], [34, 34], [20, 61]], // 40 |
||
578 | ]; |
||
579 | |||
580 | /** |
||
581 | * @var array Positions of alignment patterns. |
||
582 | * This array includes only the second and the third position of the alignment patterns. Rest of them can be calculated from the distance between them. |
||
583 | * See Table 1 in Appendix E (pp.71) of JIS X0510:2004. |
||
584 | */ |
||
585 | protected $alignmentPattern = [ |
||
586 | [0, 0], |
||
587 | [0, 0], [18, 0], [22, 0], [26, 0], [30, 0], // 1- 5 |
||
588 | [34, 0], [22, 38], [24, 42], [26, 46], [28, 50], // 6-10 |
||
589 | [30, 54], [32, 58], [34, 62], [26, 46], [26, 48], // 11-15 |
||
590 | [26, 50], [30, 54], [30, 56], [30, 58], [34, 62], // 16-20 |
||
591 | [28, 50], [26, 50], [30, 54], [28, 54], [32, 58], // 21-25 |
||
592 | [30, 58], [34, 62], [26, 50], [30, 54], [26, 52], // 26-30 |
||
593 | [30, 56], [34, 60], [30, 58], [34, 62], [30, 54], // 31-35 |
||
594 | [24, 50], [28, 54], [32, 58], [26, 54], [30, 58], // 35-40 |
||
595 | ]; |
||
596 | |||
597 | /** |
||
598 | * @var array Version information pattern (BCH coded). |
||
599 | * See Table 1 in Appendix D (pp.68) of JIS X0510:2004. |
||
600 | * size: [QRSPEC_VERSION_MAX - 6] |
||
601 | */ |
||
602 | protected $versionPattern = [ |
||
603 | 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, // |
||
604 | 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, // |
||
605 | 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, // |
||
606 | 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, // |
||
607 | 0x27541, 0x28c69, |
||
608 | ]; |
||
609 | |||
610 | /** |
||
611 | * @var array Format information |
||
612 | */ |
||
613 | protected $formatInfo = [ |
||
614 | [0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976], // |
||
615 | [0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0], // |
||
616 | [0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed], // |
||
617 | [0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b], // |
||
618 | ]; |
||
619 | |||
620 | // ------------------------------------------------- |
||
621 | // ------------------------------------------------- |
||
622 | |||
623 | /** |
||
624 | * This is the class constructor. |
||
625 | * Creates a QRcode object. |
||
626 | * |
||
627 | * @param string $code code to represent using QRcode |
||
628 | * @param string $eclevel error level: <ul><li>L : About 7% or less errors can be corrected.</li><li>M : About 15% or less errors can be corrected.</li><li>Q : About 25% or less errors can be corrected.</li><li>H : About 30% or less errors can be corrected.</li></ul> |
||
629 | * |
||
630 | * @since 1.0.000 |
||
631 | */ |
||
632 | public function __construct($code, $eclevel = 'L') |
||
633 | { |
||
634 | $barcode_array = []; |
||
635 | if ((is_null($code)) or ($code == '\0') or ($code == '')) { |
||
636 | return false; |
||
637 | } |
||
638 | // set error correction level |
||
639 | $this->level = array_search($eclevel, ['L', 'M', 'Q', 'H']); |
||
640 | if ($this->level === false) { |
||
641 | $this->level = QR_ECLEVEL_L; |
||
642 | } |
||
643 | if (($this->hint != QR_MODE_8B) and ($this->hint != QR_MODE_KJ)) { |
||
644 | return false; |
||
645 | } |
||
646 | if (($this->version < 0) or ($this->version > QRSPEC_VERSION_MAX)) { |
||
647 | return false; |
||
648 | } |
||
649 | $this->items = []; |
||
650 | $this->encodeString($code); |
||
651 | $qrTab = $this->binarize($this->data); |
||
652 | $size = count($qrTab); |
||
653 | $barcode_array['num_rows'] = $size; |
||
654 | $barcode_array['num_cols'] = $size; |
||
655 | $barcode_array['bcode'] = []; |
||
656 | foreach ($qrTab as $line) { |
||
657 | $arrAdd = []; |
||
658 | foreach (str_split($line) as $char) { |
||
659 | $arrAdd[] = ($char == '1') ? 1 : 0; |
||
660 | } |
||
661 | $barcode_array['bcode'][] = $arrAdd; |
||
662 | } |
||
663 | $this->barcode_array = $barcode_array; |
||
664 | } |
||
665 | |||
666 | /** |
||
667 | * Returns a barcode array which is readable by TCPDF. |
||
668 | * |
||
669 | * @return array barcode array readable by TCPDF; |
||
670 | */ |
||
671 | public function getBarcodeArray() |
||
672 | { |
||
673 | return $this->barcode_array; |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * Convert the frame in binary form. |
||
678 | * |
||
679 | * @param array $frame array to binarize |
||
680 | * |
||
681 | * @return array frame in binary form |
||
682 | */ |
||
683 | protected function binarize($frame) |
||
684 | { |
||
685 | $len = count($frame); |
||
686 | // the frame is square (width = height) |
||
687 | foreach ($frame as &$frameLine) { |
||
688 | for ($i = 0; $i < $len; $i++) { |
||
689 | $frameLine[$i] = (ord($frameLine[$i]) & 1) ? '1' : '0'; |
||
690 | } |
||
691 | } |
||
692 | |||
693 | return $frame; |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * Encode the input string to QR code. |
||
698 | * |
||
699 | * @param string $string input string to encode |
||
700 | */ |
||
701 | protected function encodeString($string) |
||
702 | { |
||
703 | $this->dataStr = $string; |
||
704 | if (!$this->casesensitive) { |
||
705 | $this->toUpper(); |
||
706 | } |
||
707 | $ret = $this->splitString(); |
||
708 | if ($ret < 0) { |
||
709 | return; |
||
710 | } |
||
711 | $this->encodeMask(-1); |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * Encode mask. |
||
716 | * |
||
717 | * @param int $mask masking mode |
||
718 | */ |
||
719 | protected function encodeMask($mask) |
||
720 | { |
||
721 | $spec = [0, 0, 0, 0, 0]; |
||
722 | $this->datacode = $this->getByteStream($this->items); |
||
723 | if (is_null($this->datacode)) { |
||
724 | return; |
||
725 | } |
||
726 | $spec = $this->getEccSpec($this->version, $this->level, $spec); |
||
727 | $this->b1 = $this->rsBlockNum1($spec); |
||
728 | $this->dataLength = $this->rsDataLength($spec); |
||
729 | $this->eccLength = $this->rsEccLength($spec); |
||
730 | $this->ecccode = array_fill(0, $this->eccLength, 0); |
||
731 | $this->blocks = $this->rsBlockNum($spec); |
||
732 | $ret = $this->init($spec); |
||
733 | if ($ret < 0) { |
||
734 | return; |
||
735 | } |
||
736 | $this->count = 0; |
||
737 | $this->width = $this->getWidth($this->version); |
||
738 | $this->frame = $this->newFrame($this->version); |
||
739 | $this->x = $this->width - 1; |
||
740 | $this->y = $this->width - 1; |
||
741 | $this->dir = -1; |
||
742 | $this->bit = -1; |
||
743 | // inteleaved data and ecc codes |
||
744 | for ($i = 0; $i < ($this->dataLength + $this->eccLength); $i++) { |
||
745 | $code = $this->getCode(); |
||
746 | $bit = 0x80; |
||
747 | for ($j = 0; $j < 8; $j++) { |
||
748 | $addr = $this->getNextPosition(); |
||
749 | $this->setFrameAt($addr, 0x02 | (($bit & $code) != 0)); |
||
750 | $bit = $bit >> 1; |
||
751 | } |
||
752 | } |
||
753 | // remainder bits |
||
754 | $j = $this->getRemainder($this->version); |
||
755 | for ($i = 0; $i < $j; $i++) { |
||
756 | $addr = $this->getNextPosition(); |
||
757 | $this->setFrameAt($addr, 0x02); |
||
758 | } |
||
759 | // masking |
||
760 | $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0); |
||
761 | if ($mask < 0) { |
||
762 | if (QR_FIND_BEST_MASK) { |
||
763 | $masked = $this->mask($this->width, $this->frame, $this->level); |
||
764 | } else { |
||
765 | $masked = $this->makeMask($this->width, $this->frame, (intval(QR_DEFAULT_MASK) % 8), $this->level); |
||
766 | } |
||
767 | } else { |
||
768 | $masked = $this->makeMask($this->width, $this->frame, $mask, $this->level); |
||
769 | } |
||
770 | if ($masked == null) { |
||
771 | return; |
||
772 | } |
||
773 | $this->data = $masked; |
||
774 | } |
||
775 | |||
776 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
777 | |||
778 | // FrameFiller |
||
779 | |||
780 | /** |
||
781 | * Set frame value at specified position. |
||
782 | * |
||
783 | * @param array $at x,y position |
||
784 | * @param int $val value of the character to set |
||
785 | */ |
||
786 | protected function setFrameAt($at, $val) |
||
787 | { |
||
788 | $this->frame[$at['y']][$at['x']] = chr($val); |
||
789 | } |
||
790 | |||
791 | /** |
||
792 | * Get frame value at specified position. |
||
793 | * |
||
794 | * @param array $at x,y position |
||
795 | * |
||
796 | * @return value at specified position |
||
797 | */ |
||
798 | protected function getFrameAt($at) |
||
799 | { |
||
800 | return ord($this->frame[$at['y']][$at['x']]); |
||
801 | } |
||
802 | |||
803 | /** |
||
804 | * Return the next frame position. |
||
805 | * |
||
806 | * @return array of x,y coordinates |
||
807 | */ |
||
808 | protected function getNextPosition() |
||
809 | { |
||
810 | do { |
||
811 | if ($this->bit == -1) { |
||
812 | $this->bit = 0; |
||
813 | |||
814 | return ['x'=>$this->x, 'y'=>$this->y]; |
||
815 | } |
||
816 | $x = $this->x; |
||
817 | $y = $this->y; |
||
818 | $w = $this->width; |
||
819 | if ($this->bit == 0) { |
||
820 | $x--; |
||
821 | $this->bit++; |
||
822 | } else { |
||
823 | $x++; |
||
824 | $y += $this->dir; |
||
825 | $this->bit--; |
||
826 | } |
||
827 | if ($this->dir < 0) { |
||
828 | if ($y < 0) { |
||
829 | $y = 0; |
||
830 | $x -= 2; |
||
831 | $this->dir = 1; |
||
832 | if ($x == 6) { |
||
833 | $x--; |
||
834 | $y = 9; |
||
835 | } |
||
836 | } |
||
837 | } else { |
||
838 | if ($y == $w) { |
||
839 | $y = $w - 1; |
||
840 | $x -= 2; |
||
841 | $this->dir = -1; |
||
842 | if ($x == 6) { |
||
843 | $x--; |
||
844 | $y -= 8; |
||
845 | } |
||
846 | } |
||
847 | } |
||
848 | if (($x < 0) or ($y < 0)) { |
||
849 | return; |
||
850 | } |
||
851 | $this->x = $x; |
||
852 | $this->y = $y; |
||
853 | } while (ord($this->frame[$y][$x]) & 0x80); |
||
854 | |||
855 | return ['x'=>$x, 'y'=>$y]; |
||
856 | } |
||
857 | |||
858 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
859 | |||
860 | // QRrawcode |
||
861 | |||
862 | /** |
||
863 | * Initialize code. |
||
864 | * |
||
865 | * @param array $spec array of ECC specification |
||
866 | * |
||
867 | * @return 0 in case of success, -1 in case of error |
||
868 | */ |
||
869 | protected function init($spec) |
||
870 | { |
||
871 | $dl = $this->rsDataCodes1($spec); |
||
872 | $el = $this->rsEccCodes1($spec); |
||
873 | $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el); |
||
874 | $blockNo = 0; |
||
875 | $dataPos = 0; |
||
876 | $eccPos = 0; |
||
877 | $endfor = $this->rsBlockNum1($spec); |
||
878 | for ($i = 0; $i < $endfor; $i++) { |
||
879 | $ecc = array_slice($this->ecccode, $eccPos); |
||
880 | $this->rsblocks[$blockNo] = []; |
||
881 | $this->rsblocks[$blockNo]['dataLength'] = $dl; |
||
882 | $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos); |
||
883 | $this->rsblocks[$blockNo]['eccLength'] = $el; |
||
884 | $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc); |
||
885 | $this->rsblocks[$blockNo]['ecc'] = $ecc; |
||
886 | $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc); |
||
887 | $dataPos += $dl; |
||
888 | $eccPos += $el; |
||
889 | $blockNo++; |
||
890 | } |
||
891 | if ($this->rsBlockNum2($spec) == 0) { |
||
892 | return 0; |
||
893 | } |
||
894 | $dl = $this->rsDataCodes2($spec); |
||
895 | $el = $this->rsEccCodes2($spec); |
||
896 | $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el); |
||
897 | if ($rs == null) { |
||
898 | return -1; |
||
899 | } |
||
900 | $endfor = $this->rsBlockNum2($spec); |
||
901 | for ($i = 0; $i < $endfor; $i++) { |
||
902 | $ecc = array_slice($this->ecccode, $eccPos); |
||
903 | $this->rsblocks[$blockNo] = []; |
||
904 | $this->rsblocks[$blockNo]['dataLength'] = $dl; |
||
905 | $this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos); |
||
906 | $this->rsblocks[$blockNo]['eccLength'] = $el; |
||
907 | $ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc); |
||
908 | $this->rsblocks[$blockNo]['ecc'] = $ecc; |
||
909 | $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc); |
||
910 | $dataPos += $dl; |
||
911 | $eccPos += $el; |
||
912 | $blockNo++; |
||
913 | } |
||
914 | |||
915 | return 0; |
||
916 | } |
||
917 | |||
918 | /** |
||
919 | * Return Reed-Solomon block code. |
||
920 | * |
||
921 | * @return array rsblocks |
||
922 | */ |
||
923 | protected function getCode() |
||
924 | { |
||
925 | if ($this->count < $this->dataLength) { |
||
926 | $row = $this->count % $this->blocks; |
||
927 | $col = $this->count / $this->blocks; |
||
928 | if ($col >= $this->rsblocks[0]['dataLength']) { |
||
929 | $row += $this->b1; |
||
930 | } |
||
931 | $ret = $this->rsblocks[$row]['data'][$col]; |
||
932 | } elseif ($this->count < $this->dataLength + $this->eccLength) { |
||
933 | $row = ($this->count - $this->dataLength) % $this->blocks; |
||
934 | $col = ($this->count - $this->dataLength) / $this->blocks; |
||
935 | $ret = $this->rsblocks[$row]['ecc'][$col]; |
||
936 | } else { |
||
937 | return 0; |
||
938 | } |
||
939 | $this->count++; |
||
940 | |||
941 | return $ret; |
||
942 | } |
||
943 | |||
944 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
945 | |||
946 | // QRmask |
||
947 | |||
948 | /** |
||
949 | * Write Format Information on frame and returns the number of black bits. |
||
950 | * |
||
951 | * @param int $width frame width |
||
952 | * @param array $frame frame |
||
953 | * @param array $mask masking mode |
||
954 | * @param int $level error correction level |
||
955 | * |
||
956 | * @return int blacks |
||
957 | */ |
||
958 | protected function writeFormatInformation($width, &$frame, $mask, $level) |
||
959 | { |
||
960 | $blacks = 0; |
||
961 | $format = $this->getFormatInfo($mask, $level); |
||
962 | for ($i = 0; $i < 8; $i++) { |
||
963 | if ($format & 1) { |
||
964 | $blacks += 2; |
||
965 | $v = 0x85; |
||
966 | } else { |
||
967 | $v = 0x84; |
||
968 | } |
||
969 | $frame[8][$width - 1 - $i] = chr($v); |
||
970 | if ($i < 6) { |
||
971 | $frame[$i][8] = chr($v); |
||
972 | } else { |
||
973 | $frame[$i + 1][8] = chr($v); |
||
974 | } |
||
975 | $format = $format >> 1; |
||
976 | } |
||
977 | for ($i = 0; $i < 7; $i++) { |
||
978 | if ($format & 1) { |
||
979 | $blacks += 2; |
||
980 | $v = 0x85; |
||
981 | } else { |
||
982 | $v = 0x84; |
||
983 | } |
||
984 | $frame[$width - 7 + $i][8] = chr($v); |
||
985 | if ($i == 0) { |
||
986 | $frame[8][7] = chr($v); |
||
987 | } else { |
||
988 | $frame[8][6 - $i] = chr($v); |
||
989 | } |
||
990 | $format = $format >> 1; |
||
991 | } |
||
992 | |||
993 | return $blacks; |
||
994 | } |
||
995 | |||
996 | /** |
||
997 | * mask0. |
||
998 | * |
||
999 | * @param int $x X position |
||
1000 | * @param int $y Y position |
||
1001 | * |
||
1002 | * @return int mask |
||
1003 | */ |
||
1004 | protected function mask0($x, $y) |
||
1005 | { |
||
1006 | return ($x + $y) & 1; |
||
1007 | } |
||
1008 | |||
1009 | /** |
||
1010 | * mask1. |
||
1011 | * |
||
1012 | * @param int $x X position |
||
1013 | * @param int $y Y position |
||
1014 | * |
||
1015 | * @return int mask |
||
1016 | */ |
||
1017 | protected function mask1($x, $y) |
||
1018 | { |
||
1019 | return $y & 1; |
||
1020 | } |
||
1021 | |||
1022 | /** |
||
1023 | * mask2. |
||
1024 | * |
||
1025 | * @param int $x X position |
||
1026 | * @param int $y Y position |
||
1027 | * |
||
1028 | * @return int mask |
||
1029 | */ |
||
1030 | protected function mask2($x, $y) |
||
1031 | { |
||
1032 | return $x % 3; |
||
1033 | } |
||
1034 | |||
1035 | /** |
||
1036 | * mask3. |
||
1037 | * |
||
1038 | * @param int $x X position |
||
1039 | * @param int $y Y position |
||
1040 | * |
||
1041 | * @return int mask |
||
1042 | */ |
||
1043 | protected function mask3($x, $y) |
||
1044 | { |
||
1045 | return ($x + $y) % 3; |
||
1046 | } |
||
1047 | |||
1048 | /** |
||
1049 | * mask4. |
||
1050 | * |
||
1051 | * @param int $x X position |
||
1052 | * @param int $y Y position |
||
1053 | * |
||
1054 | * @return int mask |
||
1055 | */ |
||
1056 | protected function mask4($x, $y) |
||
1057 | { |
||
1058 | return (((int) ($y / 2)) + ((int) ($x / 3))) & 1; |
||
1059 | } |
||
1060 | |||
1061 | /** |
||
1062 | * mask5. |
||
1063 | * |
||
1064 | * @param int $x X position |
||
1065 | * @param int $y Y position |
||
1066 | * |
||
1067 | * @return int mask |
||
1068 | */ |
||
1069 | protected function mask5($x, $y) |
||
1070 | { |
||
1071 | return (($x * $y) & 1) + ($x * $y) % 3; |
||
1072 | } |
||
1073 | |||
1074 | /** |
||
1075 | * mask6. |
||
1076 | * |
||
1077 | * @param int $x X position |
||
1078 | * @param int $y Y position |
||
1079 | * |
||
1080 | * @return int mask |
||
1081 | */ |
||
1082 | protected function mask6($x, $y) |
||
1083 | { |
||
1084 | return ((($x * $y) & 1) + ($x * $y) % 3) & 1; |
||
1085 | } |
||
1086 | |||
1087 | /** |
||
1088 | * mask7. |
||
1089 | * |
||
1090 | * @param int $x X position |
||
1091 | * @param int $y Y position |
||
1092 | * |
||
1093 | * @return int mask |
||
1094 | */ |
||
1095 | protected function mask7($x, $y) |
||
1096 | { |
||
1097 | return ((($x * $y) % 3) + (($x + $y) & 1)) & 1; |
||
1098 | } |
||
1099 | |||
1100 | /** |
||
1101 | * Return bitmask. |
||
1102 | * |
||
1103 | * @param int $maskNo mask number |
||
1104 | * @param int $width width |
||
1105 | * @param array $frame frame |
||
1106 | * |
||
1107 | * @return array bitmask |
||
1108 | */ |
||
1109 | protected function generateMaskNo($maskNo, $width, $frame) |
||
1110 | { |
||
1111 | $bitMask = array_fill(0, $width, array_fill(0, $width, 0)); |
||
1112 | for ($y = 0; $y < $width; $y++) { |
||
1113 | for ($x = 0; $x < $width; $x++) { |
||
1114 | if (ord($frame[$y][$x]) & 0x80) { |
||
1115 | $bitMask[$y][$x] = 0; |
||
1116 | } else { |
||
1117 | $maskFunc = call_user_func([$this, 'mask'.$maskNo], $x, $y); |
||
1118 | $bitMask[$y][$x] = ($maskFunc == 0) ? 1 : 0; |
||
1119 | } |
||
1120 | } |
||
1121 | } |
||
1122 | |||
1123 | return $bitMask; |
||
1124 | } |
||
1125 | |||
1126 | /** |
||
1127 | * makeMaskNo. |
||
1128 | * |
||
1129 | * @param int $maskNo |
||
1130 | * @param int $width |
||
1131 | * @param int $s |
||
1132 | * @param int $d |
||
1133 | * @param bool $maskGenOnly |
||
1134 | * |
||
1135 | * @return int b |
||
1136 | */ |
||
1137 | protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) |
||
1138 | { |
||
1139 | $b = 0; |
||
1140 | $bitMask = []; |
||
1141 | $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d); |
||
1142 | if ($maskGenOnly) { |
||
1143 | return; |
||
1144 | } |
||
1145 | $d = $s; |
||
1146 | for ($y = 0; $y < $width; $y++) { |
||
1147 | for ($x = 0; $x < $width; $x++) { |
||
1148 | if ($bitMask[$y][$x] == 1) { |
||
1149 | $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int) $bitMask[$y][$x]); |
||
1150 | } |
||
1151 | $b += (int) (ord($d[$y][$x]) & 1); |
||
1152 | } |
||
1153 | } |
||
1154 | |||
1155 | return $b; |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * makeMask. |
||
1160 | * |
||
1161 | * @param int $width |
||
1162 | * @param array $frame |
||
1163 | * @param int $maskNo |
||
1164 | * @param int $level |
||
1165 | * |
||
1166 | * @return array mask |
||
1167 | */ |
||
1168 | protected function makeMask($width, $frame, $maskNo, $level) |
||
1169 | { |
||
1170 | $masked = array_fill(0, $width, str_repeat("\0", $width)); |
||
1171 | $this->makeMaskNo($maskNo, $width, $frame, $masked); |
||
1172 | $this->writeFormatInformation($width, $masked, $maskNo, $level); |
||
1173 | |||
1174 | return $masked; |
||
1175 | } |
||
1176 | |||
1177 | /** |
||
1178 | * calcN1N3. |
||
1179 | * |
||
1180 | * @param int $length |
||
1181 | * |
||
1182 | * @return int demerit |
||
1183 | */ |
||
1184 | protected function calcN1N3($length) |
||
1185 | { |
||
1186 | $demerit = 0; |
||
1187 | for ($i = 0; $i < $length; $i++) { |
||
1188 | if ($this->runLength[$i] >= 5) { |
||
1189 | $demerit += (N1 + ($this->runLength[$i] - 5)); |
||
1190 | } |
||
1191 | if ($i & 1) { |
||
1192 | if (($i >= 3) and ($i < ($length - 2)) and ($this->runLength[$i] % 3 == 0)) { |
||
1193 | $fact = (int) ($this->runLength[$i] / 3); |
||
1194 | if (($this->runLength[$i - 2] == $fact) |
||
1195 | and ($this->runLength[$i - 1] == $fact) |
||
1196 | and ($this->runLength[$i + 1] == $fact) |
||
1197 | and ($this->runLength[$i + 2] == $fact)) { |
||
1198 | if (($this->runLength[$i - 3] < 0) or ($this->runLength[$i - 3] >= (4 * $fact))) { |
||
1199 | $demerit += N3; |
||
1200 | } elseif ((($i + 3) >= $length) or ($this->runLength[$i + 3] >= (4 * $fact))) { |
||
1201 | $demerit += N3; |
||
1202 | } |
||
1203 | } |
||
1204 | } |
||
1205 | } |
||
1206 | } |
||
1207 | |||
1208 | return $demerit; |
||
1209 | } |
||
1210 | |||
1211 | /** |
||
1212 | * evaluateSymbol. |
||
1213 | * |
||
1214 | * @param int $width |
||
1215 | * @param array $frame |
||
1216 | * |
||
1217 | * @return int demerit |
||
1218 | */ |
||
1219 | protected function evaluateSymbol($width, $frame) |
||
1220 | { |
||
1221 | $head = 0; |
||
1222 | $demerit = 0; |
||
1223 | for ($y = 0; $y < $width; $y++) { |
||
1224 | $head = 0; |
||
1225 | $this->runLength[0] = 1; |
||
1226 | $frameY = $frame[$y]; |
||
1227 | if ($y > 0) { |
||
1228 | $frameYM = $frame[$y - 1]; |
||
1229 | } |
||
1230 | for ($x = 0; $x < $width; $x++) { |
||
1231 | if (($x > 0) and ($y > 0)) { |
||
1232 | $b22 = ord($frameY[$x]) & ord($frameY[$x - 1]) & ord($frameYM[$x]) & ord($frameYM[$x - 1]); |
||
1233 | $w22 = ord($frameY[$x]) | ord($frameY[$x - 1]) | ord($frameYM[$x]) | ord($frameYM[$x - 1]); |
||
1234 | if (($b22 | ($w22 ^ 1)) & 1) { |
||
1235 | $demerit += N2; |
||
1236 | } |
||
1237 | } |
||
1238 | if (($x == 0) and (ord($frameY[$x]) & 1)) { |
||
1239 | $this->runLength[0] = -1; |
||
1240 | $head = 1; |
||
1241 | $this->runLength[$head] = 1; |
||
1242 | } elseif ($x > 0) { |
||
1243 | if ((ord($frameY[$x]) ^ ord($frameY[$x - 1])) & 1) { |
||
1244 | $head++; |
||
1245 | $this->runLength[$head] = 1; |
||
1246 | } else { |
||
1247 | $this->runLength[$head]++; |
||
1248 | } |
||
1249 | } |
||
1250 | } |
||
1251 | $demerit += $this->calcN1N3($head + 1); |
||
1252 | } |
||
1253 | for ($x = 0; $x < $width; $x++) { |
||
1254 | $head = 0; |
||
1255 | $this->runLength[0] = 1; |
||
1256 | for ($y = 0; $y < $width; $y++) { |
||
1257 | if (($y == 0) and (ord($frame[$y][$x]) & 1)) { |
||
1258 | $this->runLength[0] = -1; |
||
1259 | $head = 1; |
||
1260 | $this->runLength[$head] = 1; |
||
1261 | } elseif ($y > 0) { |
||
1262 | if ((ord($frame[$y][$x]) ^ ord($frame[$y - 1][$x])) & 1) { |
||
1263 | $head++; |
||
1264 | $this->runLength[$head] = 1; |
||
1265 | } else { |
||
1266 | $this->runLength[$head]++; |
||
1267 | } |
||
1268 | } |
||
1269 | } |
||
1270 | $demerit += $this->calcN1N3($head + 1); |
||
1271 | } |
||
1272 | |||
1273 | return $demerit; |
||
1274 | } |
||
1275 | |||
1276 | /** |
||
1277 | * mask. |
||
1278 | * |
||
1279 | * @param int $width |
||
1280 | * @param array $frame |
||
1281 | * @param int $level |
||
1282 | * |
||
1283 | * @return array best mask |
||
1284 | */ |
||
1285 | protected function mask($width, $frame, $level) |
||
1286 | { |
||
1287 | $minDemerit = PHP_INT_MAX; |
||
1288 | $bestMaskNum = 0; |
||
1289 | $bestMask = []; |
||
1290 | $checked_masks = [0, 1, 2, 3, 4, 5, 6, 7]; |
||
1291 | if (QR_FIND_FROM_RANDOM !== false) { |
||
1292 | $howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9); |
||
1293 | for ($i = 0; $i < $howManuOut; $i++) { |
||
1294 | $remPos = rand(0, count($checked_masks) - 1); |
||
1295 | unset($checked_masks[$remPos]); |
||
1296 | $checked_masks = array_values($checked_masks); |
||
1297 | } |
||
1298 | } |
||
1299 | $bestMask = $frame; |
||
1300 | foreach ($checked_masks as $i) { |
||
1301 | $mask = array_fill(0, $width, str_repeat("\0", $width)); |
||
1302 | $demerit = 0; |
||
1303 | $blacks = 0; |
||
1304 | $blacks = $this->makeMaskNo($i, $width, $frame, $mask); |
||
1305 | $blacks += $this->writeFormatInformation($width, $mask, $i, $level); |
||
1306 | $blacks = (int) (100 * $blacks / ($width * $width)); |
||
1307 | $demerit = (int) ((int) (abs($blacks - 50) / 5) * N4); |
||
1308 | $demerit += $this->evaluateSymbol($width, $mask); |
||
1309 | if ($demerit < $minDemerit) { |
||
1310 | $minDemerit = $demerit; |
||
1311 | $bestMask = $mask; |
||
1312 | $bestMaskNum = $i; |
||
1313 | } |
||
1314 | } |
||
1315 | |||
1316 | return $bestMask; |
||
1317 | } |
||
1318 | |||
1319 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
1320 | |||
1321 | // QRsplit |
||
1322 | |||
1323 | /** |
||
1324 | * Return true if the character at specified position is a number. |
||
1325 | * |
||
1326 | * @param string $str string |
||
1327 | * @param int $pos characted position |
||
1328 | * |
||
1329 | * @return bool true of false |
||
1330 | */ |
||
1331 | protected function isdigitat($str, $pos) |
||
1332 | { |
||
1333 | if ($pos >= strlen($str)) { |
||
1334 | return false; |
||
1335 | } |
||
1336 | |||
1337 | return (ord($str[$pos]) >= ord('0')) && (ord($str[$pos]) <= ord('9')); |
||
1338 | } |
||
1339 | |||
1340 | /** |
||
1341 | * Return true if the character at specified position is an alphanumeric character. |
||
1342 | * |
||
1343 | * @param string $str string |
||
1344 | * @param int $pos characted position |
||
1345 | * |
||
1346 | * @return bool true of false |
||
1347 | */ |
||
1348 | protected function isalnumat($str, $pos) |
||
1349 | { |
||
1350 | if ($pos >= strlen($str)) { |
||
1351 | return false; |
||
1352 | } |
||
1353 | |||
1354 | return $this->lookAnTable(ord($str[$pos])) >= 0; |
||
1355 | } |
||
1356 | |||
1357 | /** |
||
1358 | * identifyMode. |
||
1359 | * |
||
1360 | * @param int $pos |
||
1361 | * |
||
1362 | * @return int mode |
||
1363 | */ |
||
1364 | protected function identifyMode($pos) |
||
1365 | { |
||
1366 | if ($pos >= strlen($this->dataStr)) { |
||
1367 | return QR_MODE_NL; |
||
1368 | } |
||
1369 | $c = $this->dataStr[$pos]; |
||
1370 | if ($this->isdigitat($this->dataStr, $pos)) { |
||
1371 | return QR_MODE_NM; |
||
1372 | } elseif ($this->isalnumat($this->dataStr, $pos)) { |
||
1373 | return QR_MODE_AN; |
||
1374 | } elseif ($this->hint == QR_MODE_KJ) { |
||
1375 | if ($pos + 1 < strlen($this->dataStr)) { |
||
1376 | $d = $this->dataStr[$pos + 1]; |
||
1377 | $word = (ord($c) << 8) | ord($d); |
||
1378 | if (($word >= 0x8140 && $word <= 0x9ffc) or ($word >= 0xe040 && $word <= 0xebbf)) { |
||
1379 | return QR_MODE_KJ; |
||
1380 | } |
||
1381 | } |
||
1382 | } |
||
1383 | |||
1384 | return QR_MODE_8B; |
||
1385 | } |
||
1386 | |||
1387 | /** |
||
1388 | * eatNum. |
||
1389 | * |
||
1390 | * @return int run |
||
1391 | */ |
||
1392 | protected function eatNum() |
||
1393 | { |
||
1394 | $ln = $this->lengthIndicator(QR_MODE_NM, $this->version); |
||
1395 | $p = 0; |
||
1396 | while ($this->isdigitat($this->dataStr, $p)) { |
||
1397 | $p++; |
||
1398 | } |
||
1399 | $run = $p; |
||
1400 | $mode = $this->identifyMode($p); |
||
1401 | if ($mode == QR_MODE_8B) { |
||
1402 | $dif = $this->estimateBitsModeNum($run) + 4 + $ln |
||
1403 | + $this->estimateBitsMode8(1) // + 4 + l8 |
||
1404 | - $this->estimateBitsMode8($run + 1); // - 4 - l8 |
||
1405 | if ($dif > 0) { |
||
1406 | return $this->eat8(); |
||
1407 | } |
||
1408 | } |
||
1409 | if ($mode == QR_MODE_AN) { |
||
1410 | $dif = $this->estimateBitsModeNum($run) + 4 + $ln |
||
1411 | + $this->estimateBitsModeAn(1) // + 4 + la |
||
1412 | - $this->estimateBitsModeAn($run + 1); // - 4 - la |
||
1413 | if ($dif > 0) { |
||
1414 | return $this->eatAn(); |
||
1415 | } |
||
1416 | } |
||
1417 | $this->items = $this->appendNewInputItem($this->items, QR_MODE_NM, $run, str_split($this->dataStr)); |
||
1418 | |||
1419 | return $run; |
||
1420 | } |
||
1421 | |||
1422 | /** |
||
1423 | * eatAn. |
||
1424 | * |
||
1425 | * @return int run |
||
1426 | */ |
||
1427 | protected function eatAn() |
||
1428 | { |
||
1429 | $la = $this->lengthIndicator(QR_MODE_AN, $this->version); |
||
1430 | $ln = $this->lengthIndicator(QR_MODE_NM, $this->version); |
||
1431 | $p = 0; |
||
1432 | while ($this->isalnumat($this->dataStr, $p)) { |
||
1433 | if ($this->isdigitat($this->dataStr, $p)) { |
||
1434 | $q = $p; |
||
1435 | while ($this->isdigitat($this->dataStr, $q)) { |
||
1436 | $q++; |
||
1437 | } |
||
1438 | $dif = $this->estimateBitsModeAn($p) // + 4 + la |
||
1439 | + $this->estimateBitsModeNum($q - $p) + 4 + $ln |
||
1440 | - $this->estimateBitsModeAn($q); // - 4 - la |
||
1441 | if ($dif < 0) { |
||
1442 | break; |
||
1443 | } else { |
||
1444 | $p = $q; |
||
1445 | } |
||
1446 | } else { |
||
1447 | $p++; |
||
1448 | } |
||
1449 | } |
||
1450 | $run = $p; |
||
1451 | if (!$this->isalnumat($this->dataStr, $p)) { |
||
1452 | $dif = $this->estimateBitsModeAn($run) + 4 + $la |
||
1453 | + $this->estimateBitsMode8(1) // + 4 + l8 |
||
1454 | - $this->estimateBitsMode8($run + 1); // - 4 - l8 |
||
1455 | if ($dif > 0) { |
||
1456 | return $this->eat8(); |
||
1457 | } |
||
1458 | } |
||
1459 | $this->items = $this->appendNewInputItem($this->items, QR_MODE_AN, $run, str_split($this->dataStr)); |
||
1460 | |||
1461 | return $run; |
||
1462 | } |
||
1463 | |||
1464 | /** |
||
1465 | * eatKanji. |
||
1466 | * |
||
1467 | * @return int run |
||
1468 | */ |
||
1469 | protected function eatKanji() |
||
1470 | { |
||
1471 | $p = 0; |
||
1472 | while ($this->identifyMode($p) == QR_MODE_KJ) { |
||
1473 | $p += 2; |
||
1474 | } |
||
1475 | $this->items = $this->appendNewInputItem($this->items, QR_MODE_KJ, $p, str_split($this->dataStr)); |
||
1476 | |||
1477 | return $run; |
||
1478 | } |
||
1479 | |||
1480 | /** |
||
1481 | * eat8. |
||
1482 | * |
||
1483 | * @return int run |
||
1484 | */ |
||
1485 | protected function eat8() |
||
1486 | { |
||
1487 | $la = $this->lengthIndicator(QR_MODE_AN, $this->version); |
||
1488 | $ln = $this->lengthIndicator(QR_MODE_NM, $this->version); |
||
1489 | $p = 1; |
||
1490 | $dataStrLen = strlen($this->dataStr); |
||
1491 | while ($p < $dataStrLen) { |
||
1492 | $mode = $this->identifyMode($p); |
||
1493 | if ($mode == QR_MODE_KJ) { |
||
1494 | break; |
||
1495 | } |
||
1496 | if ($mode == QR_MODE_NM) { |
||
1497 | $q = $p; |
||
1498 | while ($this->isdigitat($this->dataStr, $q)) { |
||
1499 | $q++; |
||
1500 | } |
||
1501 | $dif = $this->estimateBitsMode8($p) // + 4 + l8 |
||
1502 | + $this->estimateBitsModeNum($q - $p) + 4 + $ln |
||
1503 | - $this->estimateBitsMode8($q); // - 4 - l8 |
||
1504 | if ($dif < 0) { |
||
1505 | break; |
||
1506 | } else { |
||
1507 | $p = $q; |
||
1508 | } |
||
1509 | } elseif ($mode == QR_MODE_AN) { |
||
1510 | $q = $p; |
||
1511 | while ($this->isalnumat($this->dataStr, $q)) { |
||
1512 | $q++; |
||
1513 | } |
||
1514 | $dif = $this->estimateBitsMode8($p) // + 4 + l8 |
||
1515 | + $this->estimateBitsModeAn($q - $p) + 4 + $la |
||
1516 | - $this->estimateBitsMode8($q); // - 4 - l8 |
||
1517 | if ($dif < 0) { |
||
1518 | break; |
||
1519 | } else { |
||
1520 | $p = $q; |
||
1521 | } |
||
1522 | } else { |
||
1523 | $p++; |
||
1524 | } |
||
1525 | } |
||
1526 | $run = $p; |
||
1527 | $this->items = $this->appendNewInputItem($this->items, QR_MODE_8B, $run, str_split($this->dataStr)); |
||
1528 | |||
1529 | return $run; |
||
1530 | } |
||
1531 | |||
1532 | /** |
||
1533 | * splitString. |
||
1534 | */ |
||
1535 | protected function splitString() |
||
1536 | { |
||
1537 | while (strlen($this->dataStr) > 0) { |
||
1538 | if ($this->dataStr == '') { |
||
1539 | return 0; |
||
1540 | } |
||
1541 | $mode = $this->identifyMode(0); |
||
1542 | switch ($mode) { |
||
1543 | case QR_MODE_NM: { |
||
1544 | $length = $this->eatNum(); |
||
1545 | break; |
||
1546 | } |
||
1547 | case QR_MODE_AN: { |
||
1548 | $length = $this->eatAn(); |
||
1549 | break; |
||
1550 | } |
||
1551 | case QR_MODE_KJ: { |
||
1552 | if ($hint == QR_MODE_KJ) { |
||
1553 | $length = $this->eatKanji(); |
||
1554 | } else { |
||
1555 | $length = $this->eat8(); |
||
1556 | } |
||
1557 | break; |
||
1558 | } |
||
1559 | default: { |
||
1560 | $length = $this->eat8(); |
||
1561 | break; |
||
1562 | } |
||
1563 | } |
||
1564 | if ($length == 0) { |
||
1565 | return 0; |
||
1566 | } |
||
1567 | if ($length < 0) { |
||
1568 | return -1; |
||
1569 | } |
||
1570 | $this->dataStr = substr($this->dataStr, $length); |
||
1571 | } |
||
1572 | } |
||
1573 | |||
1574 | /** |
||
1575 | * toUpper. |
||
1576 | */ |
||
1577 | protected function toUpper() |
||
1578 | { |
||
1579 | $stringLen = strlen($this->dataStr); |
||
1580 | $p = 0; |
||
1581 | while ($p < $stringLen) { |
||
1582 | $mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint); |
||
1583 | if ($mode == QR_MODE_KJ) { |
||
1584 | $p += 2; |
||
1585 | } else { |
||
1586 | if ((ord($this->dataStr[$p]) >= ord('a')) and (ord($this->dataStr[$p]) <= ord('z'))) { |
||
1587 | $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32); |
||
1588 | } |
||
1589 | $p++; |
||
1590 | } |
||
1591 | } |
||
1592 | |||
1593 | return $this->dataStr; |
||
1594 | } |
||
1595 | |||
1596 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
1597 | |||
1598 | // QRinputItem |
||
1599 | |||
1600 | /** |
||
1601 | * newInputItem. |
||
1602 | * |
||
1603 | * @param int $mode |
||
1604 | * @param int $size |
||
1605 | * @param array $data |
||
1606 | * @param array $bstream |
||
1607 | * |
||
1608 | * @return array input item |
||
1609 | */ |
||
1610 | protected function newInputItem($mode, $size, $data, $bstream = null) |
||
1611 | { |
||
1612 | $setData = array_slice($data, 0, $size); |
||
1613 | if (count($setData) < $size) { |
||
1614 | $setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0)); |
||
1615 | } |
||
1616 | if (!$this->check($mode, $size, $setData)) { |
||
1617 | return; |
||
1618 | } |
||
1619 | $inputitem = []; |
||
1620 | $inputitem['mode'] = $mode; |
||
1621 | $inputitem['size'] = $size; |
||
1622 | $inputitem['data'] = $setData; |
||
1623 | $inputitem['bstream'] = $bstream; |
||
1624 | |||
1625 | return $inputitem; |
||
1626 | } |
||
1627 | |||
1628 | /** |
||
1629 | * encodeModeNum. |
||
1630 | * |
||
1631 | * @param array $inputitem |
||
1632 | * @param int $version |
||
1633 | * |
||
1634 | * @return array input item |
||
1635 | */ |
||
1636 | protected function encodeModeNum($inputitem, $version) |
||
1637 | { |
||
1638 | $words = (int) ($inputitem['size'] / 3); |
||
1639 | $inputitem['bstream'] = []; |
||
1640 | $val = 0x1; |
||
1641 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val); |
||
1642 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']); |
||
1643 | for ($i = 0; $i < $words; $i++) { |
||
1644 | $val = (ord($inputitem['data'][$i * 3]) - ord('0')) * 100; |
||
1645 | $val += (ord($inputitem['data'][$i * 3 + 1]) - ord('0')) * 10; |
||
1646 | $val += (ord($inputitem['data'][$i * 3 + 2]) - ord('0')); |
||
1647 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val); |
||
1648 | } |
||
1649 | if ($inputitem['size'] - $words * 3 == 1) { |
||
1650 | $val = ord($inputitem['data'][$words * 3]) - ord('0'); |
||
1651 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val); |
||
1652 | } elseif (($inputitem['size'] - ($words * 3)) == 2) { |
||
1653 | $val = (ord($inputitem['data'][$words * 3]) - ord('0')) * 10; |
||
1654 | $val += (ord($inputitem['data'][$words * 3 + 1]) - ord('0')); |
||
1655 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val); |
||
1656 | } |
||
1657 | |||
1658 | return $inputitem; |
||
1659 | } |
||
1660 | |||
1661 | /** |
||
1662 | * encodeModeAn. |
||
1663 | * |
||
1664 | * @param array $inputitem |
||
1665 | * @param int $version |
||
1666 | * |
||
1667 | * @return array input item |
||
1668 | */ |
||
1669 | protected function encodeModeAn($inputitem, $version) |
||
1670 | { |
||
1671 | $words = (int) ($inputitem['size'] / 2); |
||
1672 | $inputitem['bstream'] = []; |
||
1673 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02); |
||
1674 | $inputitem['bstream'] = $this->appendNum(v, $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']); |
||
1675 | for ($i = 0; $i < $words; $i++) { |
||
1676 | $val = (int) $this->lookAnTable(ord($inputitem['data'][$i * 2])) * 45; |
||
1677 | $val += (int) $this->lookAnTable(ord($inputitem['data'][$i * 2 + 1])); |
||
1678 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val); |
||
1679 | } |
||
1680 | if ($inputitem['size'] & 1) { |
||
1681 | $val = $this->lookAnTable(ord($inputitem['data'][($words * 2)])); |
||
1682 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val); |
||
1683 | } |
||
1684 | |||
1685 | return $inputitem; |
||
1686 | } |
||
1687 | |||
1688 | /** |
||
1689 | * encodeMode8. |
||
1690 | * |
||
1691 | * @param array $inputitem |
||
1692 | * @param int $version |
||
1693 | * |
||
1694 | * @return array input item |
||
1695 | */ |
||
1696 | protected function encodeMode8($inputitem, $version) |
||
1697 | { |
||
1698 | $inputitem['bstream'] = []; |
||
1699 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4); |
||
1700 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']); |
||
1701 | for ($i = 0; $i < $inputitem['size']; $i++) { |
||
1702 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i])); |
||
1703 | } |
||
1704 | |||
1705 | return $inputitem; |
||
1706 | } |
||
1707 | |||
1708 | /** |
||
1709 | * encodeModeKanji. |
||
1710 | * |
||
1711 | * @param array $inputitem |
||
1712 | * @param int $version |
||
1713 | * |
||
1714 | * @return array input item |
||
1715 | */ |
||
1716 | protected function encodeModeKanji($inputitem, $version) |
||
1717 | { |
||
1718 | $inputitem['bstream'] = []; |
||
1719 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8); |
||
1720 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_KJ, $version), (int) ($inputitem['size'] / 2)); |
||
1721 | for ($i = 0; $i < $inputitem['size']; $i += 2) { |
||
1722 | $val = (ord($inputitem['data'][$i]) << 8) | ord($inputitem['data'][$i + 1]); |
||
1723 | if ($val <= 0x9ffc) { |
||
1724 | $val -= 0x8140; |
||
1725 | } else { |
||
1726 | $val -= 0xc140; |
||
1727 | } |
||
1728 | $h = ($val >> 8) * 0xc0; |
||
1729 | $val = ($val & 0xff) + $h; |
||
1730 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val); |
||
1731 | } |
||
1732 | |||
1733 | return $inputitem; |
||
1734 | } |
||
1735 | |||
1736 | /** |
||
1737 | * encodeModeStructure. |
||
1738 | * |
||
1739 | * @param array $inputitem |
||
1740 | * |
||
1741 | * @return array input item |
||
1742 | */ |
||
1743 | protected function encodeModeStructure($inputitem) |
||
1744 | { |
||
1745 | $inputitem['bstream'] = []; |
||
1746 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03); |
||
1747 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][1]) - 1); |
||
1748 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][0]) - 1); |
||
1749 | $inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][2])); |
||
1750 | |||
1751 | return $inputitem; |
||
1752 | } |
||
1753 | |||
1754 | /** |
||
1755 | * encodeBitStream. |
||
1756 | * |
||
1757 | * @param array $inputitem |
||
1758 | * @param int $version |
||
1759 | * |
||
1760 | * @return array input item |
||
1761 | */ |
||
1762 | protected function encodeBitStream($inputitem, $version) |
||
1763 | { |
||
1764 | $inputitem['bstream'] = []; |
||
1765 | $words = $this->maximumWords($inputitem['mode'], $version); |
||
1766 | if ($inputitem['size'] > $words) { |
||
1767 | $st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']); |
||
1768 | $st2 = $this->newInputItem($inputitem['mode'], $inputitem['size'] - $words, array_slice($inputitem['data'], $words)); |
||
1769 | $st1 = $this->encodeBitStream($st1, $version); |
||
1770 | $st2 = $this->encodeBitStream($st2, $version); |
||
1771 | $inputitem['bstream'] = []; |
||
1772 | $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']); |
||
1773 | $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']); |
||
1774 | } else { |
||
1775 | switch ($inputitem['mode']) { |
||
1776 | case QR_MODE_NM: { |
||
1777 | $inputitem = $this->encodeModeNum($inputitem, $version); |
||
1778 | break; |
||
1779 | } |
||
1780 | case QR_MODE_AN: { |
||
1781 | $inputitem = $this->encodeModeAn($inputitem, $version); |
||
1782 | break; |
||
1783 | } |
||
1784 | case QR_MODE_8B: { |
||
1785 | $inputitem = $this->encodeMode8($inputitem, $version); |
||
1786 | break; |
||
1787 | } |
||
1788 | case QR_MODE_KJ: { |
||
1789 | $inputitem = $this->encodeModeKanji($inputitem, $version); |
||
1790 | break; |
||
1791 | } |
||
1792 | case QR_MODE_ST: { |
||
1793 | $inputitem = $this->encodeModeStructure($inputitem); |
||
1794 | break; |
||
1795 | } |
||
1796 | default: { |
||
1797 | break; |
||
1798 | } |
||
1799 | } |
||
1800 | } |
||
1801 | |||
1802 | return $inputitem; |
||
1803 | } |
||
1804 | |||
1805 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
1806 | |||
1807 | // QRinput |
||
1808 | |||
1809 | /** |
||
1810 | * Append data to an input object. |
||
1811 | * The data is copied and appended to the input object. |
||
1812 | * |
||
1813 | * @param array items input items |
||
1814 | * @param int $mode encoding mode. |
||
1815 | * @param int $size size of data (byte). |
||
1816 | * @param array $data array of input data. |
||
1817 | * |
||
1818 | * @return items |
||
1819 | */ |
||
1820 | protected function appendNewInputItem($items, $mode, $size, $data) |
||
1821 | { |
||
1822 | $items[] = $this->newInputItem($mode, $size, $data); |
||
1823 | |||
1824 | return $items; |
||
1825 | } |
||
1826 | |||
1827 | /** |
||
1828 | * insertStructuredAppendHeader. |
||
1829 | * |
||
1830 | * @param array $items |
||
1831 | * @param int $size |
||
1832 | * @param int $index |
||
1833 | * @param int $parity |
||
1834 | * |
||
1835 | * @return array items |
||
1836 | */ |
||
1837 | protected function insertStructuredAppendHeader($items, $size, $index, $parity) |
||
1838 | { |
||
1839 | if ($size > MAX_STRUCTURED_SYMBOLS) { |
||
1840 | return -1; |
||
1841 | } |
||
1842 | if (($index <= 0) or ($index > MAX_STRUCTURED_SYMBOLS)) { |
||
1843 | return -1; |
||
1844 | } |
||
1845 | $buf = [$size, $index, $parity]; |
||
1846 | $entry = $this->newInputItem(QR_MODE_ST, 3, buf); |
||
1847 | array_unshift($items, $entry); |
||
1848 | |||
1849 | return $items; |
||
1850 | } |
||
1851 | |||
1852 | /** |
||
1853 | * calcParity. |
||
1854 | * |
||
1855 | * @param array $items |
||
1856 | * |
||
1857 | * @return int parity |
||
1858 | */ |
||
1859 | protected function calcParity($items) |
||
1860 | { |
||
1861 | $parity = 0; |
||
1862 | foreach ($items as $item) { |
||
1863 | if ($item['mode'] != QR_MODE_ST) { |
||
1864 | for ($i = $item['size'] - 1; $i >= 0; $i--) { |
||
1865 | $parity ^= $item['data'][$i]; |
||
1866 | } |
||
1867 | } |
||
1868 | } |
||
1869 | |||
1870 | return $parity; |
||
1871 | } |
||
1872 | |||
1873 | /** |
||
1874 | * checkModeNum. |
||
1875 | * |
||
1876 | * @param int $size |
||
1877 | * @param array $data |
||
1878 | * |
||
1879 | * @return bool true or false |
||
1880 | */ |
||
1881 | protected function checkModeNum($size, $data) |
||
1882 | { |
||
1883 | for ($i = 0; $i < $size; $i++) { |
||
1884 | if ((ord($data[$i]) < ord('0')) or (ord($data[$i]) > ord('9'))) { |
||
1885 | return false; |
||
1886 | } |
||
1887 | } |
||
1888 | |||
1889 | return true; |
||
1890 | } |
||
1891 | |||
1892 | /** |
||
1893 | * estimateBitsModeNum. |
||
1894 | * |
||
1895 | * @param int $size |
||
1896 | * |
||
1897 | * @return int number of bits |
||
1898 | */ |
||
1899 | protected function estimateBitsModeNum($size) |
||
1900 | { |
||
1901 | $w = (int) $size / 3; |
||
1902 | $bits = $w * 10; |
||
1903 | switch ($size - $w * 3) { |
||
1904 | case 1: { |
||
1905 | $bits += 4; |
||
1906 | break; |
||
1907 | } |
||
1908 | case 2: { |
||
1909 | $bits += 7; |
||
1910 | break; |
||
1911 | } |
||
1912 | default: { |
||
1913 | break; |
||
1914 | } |
||
1915 | } |
||
1916 | |||
1917 | return $bits; |
||
1918 | } |
||
1919 | |||
1920 | /** |
||
1921 | * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19). |
||
1922 | * |
||
1923 | * @param int $c character value |
||
1924 | * |
||
1925 | * @return value |
||
1926 | */ |
||
1927 | protected function lookAnTable($c) |
||
1928 | { |
||
1929 | return ($c > 127) ? -1 : $this->anTable[$c]; |
||
1930 | } |
||
1931 | |||
1932 | /** |
||
1933 | * checkModeAn. |
||
1934 | * |
||
1935 | * @param int $size |
||
1936 | * @param array $data |
||
1937 | * |
||
1938 | * @return bool true or false |
||
1939 | */ |
||
1940 | protected function checkModeAn($size, $data) |
||
1941 | { |
||
1942 | for ($i = 0; $i < $size; $i++) { |
||
1943 | if ($this->lookAnTable(ord($data[$i])) == -1) { |
||
1944 | return false; |
||
1945 | } |
||
1946 | } |
||
1947 | |||
1948 | return true; |
||
1949 | } |
||
1950 | |||
1951 | /** |
||
1952 | * estimateBitsModeAn. |
||
1953 | * |
||
1954 | * @param int $size |
||
1955 | * |
||
1956 | * @return int number of bits |
||
1957 | */ |
||
1958 | protected function estimateBitsModeAn($size) |
||
1959 | { |
||
1960 | $w = (int) ($size / 2); |
||
1961 | $bits = $w * 11; |
||
1962 | if ($size & 1) { |
||
1963 | $bits += 6; |
||
1964 | } |
||
1965 | |||
1966 | return $bits; |
||
1967 | } |
||
1968 | |||
1969 | /** |
||
1970 | * estimateBitsMode8. |
||
1971 | * |
||
1972 | * @param int $size |
||
1973 | * |
||
1974 | * @return int number of bits |
||
1975 | */ |
||
1976 | protected function estimateBitsMode8($size) |
||
1977 | { |
||
1978 | return $size * 8; |
||
1979 | } |
||
1980 | |||
1981 | /** |
||
1982 | * estimateBitsModeKanji. |
||
1983 | * |
||
1984 | * @param int $size |
||
1985 | * |
||
1986 | * @return int number of bits |
||
1987 | */ |
||
1988 | protected function estimateBitsModeKanji($size) |
||
1989 | { |
||
1990 | return (int) (($size / 2) * 13); |
||
1991 | } |
||
1992 | |||
1993 | /** |
||
1994 | * checkModeKanji. |
||
1995 | * |
||
1996 | * @param int $size |
||
1997 | * @param array $data |
||
1998 | * |
||
1999 | * @return bool true or false |
||
2000 | */ |
||
2001 | protected function checkModeKanji($size, $data) |
||
2002 | { |
||
2003 | if ($size & 1) { |
||
2004 | return false; |
||
2005 | } |
||
2006 | for ($i = 0; $i < $size; $i += 2) { |
||
2007 | $val = (ord($data[$i]) << 8) | ord($data[$i + 1]); |
||
2008 | if (($val < 0x8140) or (($val > 0x9ffc) and ($val < 0xe040)) or ($val > 0xebbf)) { |
||
2009 | return false; |
||
2010 | } |
||
2011 | } |
||
2012 | |||
2013 | return true; |
||
2014 | } |
||
2015 | |||
2016 | /** |
||
2017 | * Validate the input data. |
||
2018 | * |
||
2019 | * @param int $mode encoding mode. |
||
2020 | * @param int $size size of data (byte). |
||
2021 | * @param array data data to validate |
||
2022 | * |
||
2023 | * @return bool true in case of valid data, false otherwise |
||
2024 | */ |
||
2025 | protected function check($mode, $size, $data) |
||
2026 | { |
||
2027 | if ($size <= 0) { |
||
2028 | return false; |
||
2029 | } |
||
2030 | switch ($mode) { |
||
2031 | case QR_MODE_NM: { |
||
2032 | return $this->checkModeNum($size, $data); |
||
2033 | } |
||
2034 | case QR_MODE_AN: { |
||
2035 | return $this->checkModeAn($size, $data); |
||
2036 | } |
||
2037 | case QR_MODE_KJ: { |
||
2038 | return $this->checkModeKanji($size, $data); |
||
2039 | } |
||
2040 | case QR_MODE_8B: { |
||
2041 | return true; |
||
2042 | } |
||
2043 | case QR_MODE_ST: { |
||
2044 | return true; |
||
2045 | } |
||
2046 | default: { |
||
2047 | break; |
||
2048 | } |
||
2049 | } |
||
2050 | |||
2051 | return false; |
||
2052 | } |
||
2053 | |||
2054 | /** |
||
2055 | * estimateBitStreamSize. |
||
2056 | * |
||
2057 | * @param array $items |
||
2058 | * @param int $version |
||
2059 | * |
||
2060 | * @return int bits |
||
2061 | */ |
||
2062 | protected function estimateBitStreamSize($items, $version) |
||
2063 | { |
||
2064 | $bits = 0; |
||
2065 | if ($version == 0) { |
||
2066 | $version = 1; |
||
2067 | } |
||
2068 | foreach ($items as $item) { |
||
2069 | switch ($item['mode']) { |
||
2070 | case QR_MODE_NM: { |
||
2071 | $bits = $this->estimateBitsModeNum($item['size']); |
||
2072 | break; |
||
2073 | } |
||
2074 | case QR_MODE_AN: { |
||
2075 | $bits = $this->estimateBitsModeAn($item['size']); |
||
2076 | break; |
||
2077 | } |
||
2078 | case QR_MODE_8B: { |
||
2079 | $bits = $this->estimateBitsMode8($item['size']); |
||
2080 | break; |
||
2081 | } |
||
2082 | case QR_MODE_KJ: { |
||
2083 | $bits = $this->estimateBitsModeKanji($item['size']); |
||
2084 | break; |
||
2085 | } |
||
2086 | case QR_MODE_ST: { |
||
2087 | return STRUCTURE_HEADER_BITS; |
||
2088 | } |
||
2089 | default: { |
||
2090 | return 0; |
||
2091 | } |
||
2092 | } |
||
2093 | $l = $this->lengthIndicator($item['mode'], $version); |
||
2094 | $m = 1 << $l; |
||
2095 | $num = (int) (($item['size'] + $m - 1) / $m); |
||
2096 | $bits += $num * (4 + $l); |
||
2097 | } |
||
2098 | |||
2099 | return $bits; |
||
2100 | } |
||
2101 | |||
2102 | /** |
||
2103 | * estimateVersion. |
||
2104 | * |
||
2105 | * @param array $items |
||
2106 | * |
||
2107 | * @return int version |
||
2108 | */ |
||
2109 | protected function estimateVersion($items) |
||
2110 | { |
||
2111 | $version = 0; |
||
2112 | $prev = 0; |
||
2113 | do { |
||
2114 | $prev = $version; |
||
2115 | $bits = $this->estimateBitStreamSize($items, $prev); |
||
2116 | $version = $this->getMinimumVersion((int) (($bits + 7) / 8), $this->level); |
||
2117 | if ($version < 0) { |
||
2118 | return -1; |
||
2119 | } |
||
2120 | } while ($version > $prev); |
||
2121 | |||
2122 | return $version; |
||
2123 | } |
||
2124 | |||
2125 | /** |
||
2126 | * lengthOfCode. |
||
2127 | * |
||
2128 | * @param int $mode |
||
2129 | * @param int $version |
||
2130 | * @param int $bits |
||
2131 | * |
||
2132 | * @return int size |
||
2133 | */ |
||
2134 | protected function lengthOfCode($mode, $version, $bits) |
||
2135 | { |
||
2136 | $payload = $bits - 4 - $this->lengthIndicator($mode, $version); |
||
2137 | switch ($mode) { |
||
2138 | case QR_MODE_NM: { |
||
2139 | $chunks = (int) ($payload / 10); |
||
2140 | $remain = $payload - $chunks * 10; |
||
2141 | $size = $chunks * 3; |
||
2142 | if ($remain >= 7) { |
||
2143 | $size += 2; |
||
2144 | } elseif ($remain >= 4) { |
||
2145 | $size += 1; |
||
2146 | } |
||
2147 | break; |
||
2148 | } |
||
2149 | case QR_MODE_AN: { |
||
2150 | $chunks = (int) ($payload / 11); |
||
2151 | $remain = $payload - $chunks * 11; |
||
2152 | $size = $chunks * 2; |
||
2153 | if ($remain >= 6) { |
||
2154 | $size++; |
||
2155 | } |
||
2156 | break; |
||
2157 | } |
||
2158 | case QR_MODE_8B: { |
||
2159 | $size = (int) ($payload / 8); |
||
2160 | break; |
||
2161 | } |
||
2162 | case QR_MODE_KJ: { |
||
2163 | $size = (int) (($payload / 13) * 2); |
||
2164 | break; |
||
2165 | } |
||
2166 | case QR_MODE_ST: { |
||
2167 | $size = (int) ($payload / 8); |
||
2168 | break; |
||
2169 | } |
||
2170 | default: { |
||
2171 | $size = 0; |
||
2172 | break; |
||
2173 | } |
||
2174 | } |
||
2175 | $maxsize = $this->maximumWords($mode, $version); |
||
2176 | if ($size < 0) { |
||
2177 | $size = 0; |
||
2178 | } |
||
2179 | if ($size > $maxsize) { |
||
2180 | $size = $maxsize; |
||
2181 | } |
||
2182 | |||
2183 | return $size; |
||
2184 | } |
||
2185 | |||
2186 | /** |
||
2187 | * createBitStream. |
||
2188 | * |
||
2189 | * @param array $items |
||
2190 | * |
||
2191 | * @return array of items and total bits |
||
2192 | */ |
||
2193 | protected function createBitStream($items) |
||
2194 | { |
||
2195 | $total = 0; |
||
2196 | foreach ($items as $key => $item) { |
||
2197 | $items[$key] = $this->encodeBitStream($item, $this->version); |
||
2198 | $bits = count($items[$key]['bstream']); |
||
2199 | $total += $bits; |
||
2200 | } |
||
2201 | |||
2202 | return [$items, $total]; |
||
2203 | } |
||
2204 | |||
2205 | /** |
||
2206 | * convertData. |
||
2207 | * |
||
2208 | * @param array $items |
||
2209 | * |
||
2210 | * @return array items |
||
2211 | */ |
||
2212 | protected function convertData($items) |
||
2213 | { |
||
2214 | $ver = $this->estimateVersion($items); |
||
2215 | if ($ver > $this->version) { |
||
2216 | $this->version = $ver; |
||
2217 | } |
||
2218 | for (; ;) { |
||
2219 | $cbs = $this->createBitStream($items); |
||
2220 | $items = $cbs[0]; |
||
2221 | $bits = $cbs[1]; |
||
2222 | if ($bits < 0) { |
||
2223 | return -1; |
||
2224 | } |
||
2225 | $ver = $this->getMinimumVersion((int) (($bits + 7) / 8), $this->level); |
||
2226 | if ($ver < 0) { |
||
2227 | return -1; |
||
2228 | } elseif ($ver > $this->version) { |
||
2229 | $this->version = $ver; |
||
2230 | } else { |
||
2231 | break; |
||
2232 | } |
||
2233 | } |
||
2234 | |||
2235 | return $items; |
||
2236 | } |
||
2237 | |||
2238 | /** |
||
2239 | * Append Padding Bit to bitstream. |
||
2240 | * |
||
2241 | * @param array $bstream |
||
2242 | * |
||
2243 | * @return array bitstream |
||
2244 | */ |
||
2245 | protected function appendPaddingBit($bstream) |
||
2246 | { |
||
2247 | $bits = count($bstream); |
||
2248 | $maxwords = $this->getDataLength($this->version, $this->level); |
||
2249 | $maxbits = $maxwords * 8; |
||
2250 | if ($maxbits == $bits) { |
||
2251 | return 0; |
||
2252 | } |
||
2253 | if ($maxbits - $bits < 5) { |
||
2254 | return $this->appendNum($bstream, $maxbits - $bits, 0); |
||
2255 | } |
||
2256 | $bits += 4; |
||
2257 | $words = (int) (($bits + 7) / 8); |
||
2258 | $padding = []; |
||
2259 | $padding = $this->appendNum($padding, $words * 8 - $bits + 4, 0); |
||
2260 | $padlen = $maxwords - $words; |
||
2261 | if ($padlen > 0) { |
||
2262 | $padbuf = []; |
||
2263 | for ($i = 0; $i < $padlen; $i++) { |
||
2264 | $padbuf[$i] = ($i & 1) ? 0x11 : 0xec; |
||
2265 | } |
||
2266 | $padding = $this->appendBytes($padding, $padlen, $padbuf); |
||
2267 | } |
||
2268 | |||
2269 | return $this->appendBitstream($bstream, $padding); |
||
2270 | } |
||
2271 | |||
2272 | /** |
||
2273 | * mergeBitStream. |
||
2274 | * |
||
2275 | * @param array $bstream |
||
2276 | * |
||
2277 | * @return array bitstream |
||
2278 | */ |
||
2279 | protected function mergeBitStream($items) |
||
2280 | { |
||
2281 | $items = $this->convertData($items); |
||
2282 | $bstream = []; |
||
2283 | foreach ($items as $item) { |
||
2284 | $bstream = $this->appendBitstream($bstream, $item['bstream']); |
||
2285 | } |
||
2286 | |||
2287 | return $bstream; |
||
2288 | } |
||
2289 | |||
2290 | /** |
||
2291 | * Returns a stream of bits. |
||
2292 | * |
||
2293 | * @param int $items |
||
2294 | * |
||
2295 | * @return array padded merged byte stream |
||
2296 | */ |
||
2297 | protected function getBitStream($items) |
||
2298 | { |
||
2299 | $bstream = $this->mergeBitStream($items); |
||
2300 | |||
2301 | return $this->appendPaddingBit($bstream); |
||
2302 | } |
||
2303 | |||
2304 | /** |
||
2305 | * Pack all bit streams padding bits into a byte array. |
||
2306 | * |
||
2307 | * @param int $items |
||
2308 | * |
||
2309 | * @return array padded merged byte stream |
||
2310 | */ |
||
2311 | protected function getByteStream($items) |
||
2312 | { |
||
2313 | $bstream = $this->getBitStream($items); |
||
2314 | |||
2315 | return $this->bitstreamToByte($bstream); |
||
2316 | } |
||
2317 | |||
2318 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
2319 | |||
2320 | // QRbitstream |
||
2321 | |||
2322 | /** |
||
2323 | * Return an array with zeros. |
||
2324 | * |
||
2325 | * @param int $setLength array size |
||
2326 | * |
||
2327 | * @return array |
||
2328 | */ |
||
2329 | protected function allocate($setLength) |
||
2330 | { |
||
2331 | return array_fill(0, $setLength, 0); |
||
2332 | } |
||
2333 | |||
2334 | /** |
||
2335 | * Return new bitstream from number. |
||
2336 | * |
||
2337 | * @param int $bits number of bits |
||
2338 | * @param int $num number |
||
2339 | * |
||
2340 | * @return array bitstream |
||
2341 | */ |
||
2342 | protected function newFromNum($bits, $num) |
||
2343 | { |
||
2344 | $bstream = $this->allocate($bits); |
||
2345 | $mask = 1 << ($bits - 1); |
||
2346 | for ($i = 0; $i < $bits; $i++) { |
||
2347 | if ($num & $mask) { |
||
2348 | $bstream[$i] = 1; |
||
2349 | } else { |
||
2350 | $bstream[$i] = 0; |
||
2351 | } |
||
2352 | $mask = $mask >> 1; |
||
2353 | } |
||
2354 | |||
2355 | return $bstream; |
||
2356 | } |
||
2357 | |||
2358 | /** |
||
2359 | * Return new bitstream from bytes. |
||
2360 | * |
||
2361 | * @param int $size size |
||
2362 | * @param array $data bytes |
||
2363 | * |
||
2364 | * @return array bitstream |
||
2365 | */ |
||
2366 | protected function newFromBytes($size, $data) |
||
2367 | { |
||
2368 | $bstream = $this->allocate($size * 8); |
||
2369 | $p = 0; |
||
2370 | for ($i = 0; $i < $size; $i++) { |
||
2371 | $mask = 0x80; |
||
2372 | for ($j = 0; $j < 8; $j++) { |
||
2373 | if ($data[$i] & $mask) { |
||
2374 | $bstream[$p] = 1; |
||
2375 | } else { |
||
2376 | $bstream[$p] = 0; |
||
2377 | } |
||
2378 | $p++; |
||
2379 | $mask = $mask >> 1; |
||
2380 | } |
||
2381 | } |
||
2382 | |||
2383 | return $bstream; |
||
2384 | } |
||
2385 | |||
2386 | /** |
||
2387 | * Append one bitstream to another. |
||
2388 | * |
||
2389 | * @param array $bitstream original bitstream |
||
2390 | * @param array $append bitstream to append |
||
2391 | * |
||
2392 | * @return array bitstream |
||
2393 | */ |
||
2394 | protected function appendBitstream($bitstream, $append) |
||
2395 | { |
||
2396 | if ((!is_array($append)) or (count($append) == 0)) { |
||
2397 | return $bitstream; |
||
2398 | } |
||
2399 | if (count($bitstream) == 0) { |
||
2400 | return $append; |
||
2401 | } |
||
2402 | |||
2403 | return array_values(array_merge($bitstream, $append)); |
||
2404 | } |
||
2405 | |||
2406 | /** |
||
2407 | * Append one bitstream created from number to another. |
||
2408 | * |
||
2409 | * @param array $bitstream original bitstream |
||
2410 | * @param int $bits number of bits |
||
2411 | * @param int $num number |
||
2412 | * |
||
2413 | * @return array bitstream |
||
2414 | */ |
||
2415 | protected function appendNum($bitstream, $bits, $num) |
||
2416 | { |
||
2417 | if ($bits == 0) { |
||
2418 | return 0; |
||
2419 | } |
||
2420 | $b = $this->newFromNum($bits, $num); |
||
2421 | |||
2422 | return $this->appendBitstream($bitstream, $b); |
||
2423 | } |
||
2424 | |||
2425 | /** |
||
2426 | * Append one bitstream created from bytes to another. |
||
2427 | * |
||
2428 | * @param array $bitstream original bitstream |
||
2429 | * @param int $size size |
||
2430 | * @param array $data bytes |
||
2431 | * |
||
2432 | * @return array bitstream |
||
2433 | */ |
||
2434 | protected function appendBytes($bitstream, $size, $data) |
||
2435 | { |
||
2436 | if ($size == 0) { |
||
2437 | return 0; |
||
2438 | } |
||
2439 | $b = $this->newFromBytes($size, $data); |
||
2440 | |||
2441 | return $this->appendBitstream($bitstream, $b); |
||
2442 | } |
||
2443 | |||
2444 | /** |
||
2445 | * Convert bitstream to bytes. |
||
2446 | * |
||
2447 | * @param array $bitstream original bitstream |
||
2448 | * |
||
2449 | * @return array of bytes |
||
2450 | */ |
||
2451 | protected function bitstreamToByte($bstream) |
||
2452 | { |
||
2453 | $size = count($bstream); |
||
2454 | if ($size == 0) { |
||
2455 | return []; |
||
2456 | } |
||
2457 | $data = array_fill(0, (int) (($size + 7) / 8), 0); |
||
2458 | $bytes = (int) ($size / 8); |
||
2459 | $p = 0; |
||
2460 | for ($i = 0; $i < $bytes; $i++) { |
||
2461 | $v = 0; |
||
2462 | for ($j = 0; $j < 8; $j++) { |
||
2463 | $v = $v << 1; |
||
2464 | $v |= $bstream[$p]; |
||
2465 | $p++; |
||
2466 | } |
||
2467 | $data[$i] = $v; |
||
2468 | } |
||
2469 | if ($size & 7) { |
||
2470 | $v = 0; |
||
2471 | for ($j = 0; $j < ($size & 7); $j++) { |
||
2472 | $v = $v << 1; |
||
2473 | $v |= $bstream[$p]; |
||
2474 | $p++; |
||
2475 | } |
||
2476 | $data[$bytes] = $v; |
||
2477 | } |
||
2478 | |||
2479 | return $data; |
||
2480 | } |
||
2481 | |||
2482 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
2483 | |||
2484 | // QRspec |
||
2485 | |||
2486 | /** |
||
2487 | * Replace a value on the array at the specified position. |
||
2488 | * |
||
2489 | * @param array $srctab |
||
2490 | * @param int $x X position |
||
2491 | * @param int $y Y position |
||
2492 | * @param string $repl value to replace |
||
2493 | * @param int $replLen length of the repl string |
||
2494 | * |
||
2495 | * @return array srctab |
||
2496 | */ |
||
2497 | protected function qrstrset($srctab, $x, $y, $repl, $replLen = false) |
||
2498 | { |
||
2499 | $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false) ? substr($repl, 0, $replLen) : $repl, $x, ($replLen !== false) ? $replLen : strlen($repl)); |
||
2500 | |||
2501 | return $srctab; |
||
2502 | } |
||
2503 | |||
2504 | /** |
||
2505 | * Return maximum data code length (bytes) for the version. |
||
2506 | * |
||
2507 | * @param int $version version |
||
2508 | * @param int $level error correction level |
||
2509 | * |
||
2510 | * @return int maximum size (bytes) |
||
2511 | */ |
||
2512 | protected function getDataLength($version, $level) |
||
2513 | { |
||
2514 | return $this->capacity[$version][QRCAP_WORDS] - $this->capacity[$version][QRCAP_EC][$level]; |
||
2515 | } |
||
2516 | |||
2517 | /** |
||
2518 | * Return maximum error correction code length (bytes) for the version. |
||
2519 | * |
||
2520 | * @param int $version version |
||
2521 | * @param int $level error correction level |
||
2522 | * |
||
2523 | * @return int ECC size (bytes) |
||
2524 | */ |
||
2525 | protected function getECCLength($version, $level) |
||
2526 | { |
||
2527 | return $this->capacity[$version][QRCAP_EC][$level]; |
||
2528 | } |
||
2529 | |||
2530 | /** |
||
2531 | * Return the width of the symbol for the version. |
||
2532 | * |
||
2533 | * @param int $version version |
||
2534 | * |
||
2535 | * @return int width |
||
2536 | */ |
||
2537 | protected function getWidth($version) |
||
2538 | { |
||
2539 | return $this->capacity[$version][QRCAP_WIDTH]; |
||
2540 | } |
||
2541 | |||
2542 | /** |
||
2543 | * Return the numer of remainder bits. |
||
2544 | * |
||
2545 | * @param int $version version |
||
2546 | * |
||
2547 | * @return int number of remainder bits |
||
2548 | */ |
||
2549 | protected function getRemainder($version) |
||
2550 | { |
||
2551 | return $this->capacity[$version][QRCAP_REMINDER]; |
||
2552 | } |
||
2553 | |||
2554 | /** |
||
2555 | * Return a version number that satisfies the input code length. |
||
2556 | * |
||
2557 | * @param int $size input code length (byte) |
||
2558 | * @param int $level error correction level |
||
2559 | * |
||
2560 | * @return int version number |
||
2561 | */ |
||
2562 | protected function getMinimumVersion($size, $level) |
||
2563 | { |
||
2564 | for ($i = 1; $i <= QRSPEC_VERSION_MAX; $i++) { |
||
2565 | $words = $this->capacity[$i][QRCAP_WORDS] - $this->capacity[$i][QRCAP_EC][$level]; |
||
2566 | if ($words >= $size) { |
||
2567 | return $i; |
||
2568 | } |
||
2569 | } |
||
2570 | |||
2571 | return -1; |
||
2572 | } |
||
2573 | |||
2574 | /** |
||
2575 | * Return the size of length indicator for the mode and version. |
||
2576 | * |
||
2577 | * @param int $mode encoding mode |
||
2578 | * @param int $version version |
||
2579 | * |
||
2580 | * @return int the size of the appropriate length indicator (bits). |
||
2581 | */ |
||
2582 | protected function lengthIndicator($mode, $version) |
||
2583 | { |
||
2584 | if ($mode == QR_MODE_ST) { |
||
2585 | return 0; |
||
2586 | } |
||
2587 | if ($version <= 9) { |
||
2588 | $l = 0; |
||
2589 | } elseif ($version <= 26) { |
||
2590 | $l = 1; |
||
2591 | } else { |
||
2592 | $l = 2; |
||
2593 | } |
||
2594 | |||
2595 | return $this->lengthTableBits[$mode][$l]; |
||
2596 | } |
||
2597 | |||
2598 | /** |
||
2599 | * Return the maximum length for the mode and version. |
||
2600 | * |
||
2601 | * @param int $mode encoding mode |
||
2602 | * @param int $version version |
||
2603 | * |
||
2604 | * @return int the maximum length (bytes) |
||
2605 | */ |
||
2606 | protected function maximumWords($mode, $version) |
||
2607 | { |
||
2608 | if ($mode == QR_MODE_ST) { |
||
2609 | return 3; |
||
2610 | } |
||
2611 | if ($version <= 9) { |
||
2612 | $l = 0; |
||
2613 | } elseif ($version <= 26) { |
||
2614 | $l = 1; |
||
2615 | } else { |
||
2616 | $l = 2; |
||
2617 | } |
||
2618 | $bits = $this->lengthTableBits[$mode][$l]; |
||
2619 | $words = (1 << $bits) - 1; |
||
2620 | if ($mode == QR_MODE_KJ) { |
||
2621 | $words *= 2; // the number of bytes is required |
||
2622 | } |
||
2623 | |||
2624 | return $words; |
||
2625 | } |
||
2626 | |||
2627 | /** |
||
2628 | * Return an array of ECC specification. |
||
2629 | * |
||
2630 | * @param int $version version |
||
2631 | * @param int $level error correction level |
||
2632 | * @param array $spec an array of ECC specification contains as following: {# of type1 blocks, # of data code, # of ecc code, # of type2 blocks, # of data code} |
||
2633 | * |
||
2634 | * @return array spec |
||
2635 | */ |
||
2636 | protected function getEccSpec($version, $level, $spec) |
||
2637 | { |
||
2638 | if (count($spec) < 5) { |
||
2639 | $spec = [0, 0, 0, 0, 0]; |
||
2640 | } |
||
2641 | $b1 = $this->eccTable[$version][$level][0]; |
||
2642 | $b2 = $this->eccTable[$version][$level][1]; |
||
2643 | $data = $this->getDataLength($version, $level); |
||
2644 | $ecc = $this->getECCLength($version, $level); |
||
2645 | if ($b2 == 0) { |
||
2646 | $spec[0] = $b1; |
||
2647 | $spec[1] = (int) ($data / $b1); |
||
2648 | $spec[2] = (int) ($ecc / $b1); |
||
2649 | $spec[3] = 0; |
||
2650 | $spec[4] = 0; |
||
2651 | } else { |
||
2652 | $spec[0] = $b1; |
||
2653 | $spec[1] = (int) ($data / ($b1 + $b2)); |
||
2654 | $spec[2] = (int) ($ecc / ($b1 + $b2)); |
||
2655 | $spec[3] = $b2; |
||
2656 | $spec[4] = $spec[1] + 1; |
||
2657 | } |
||
2658 | |||
2659 | return $spec; |
||
2660 | } |
||
2661 | |||
2662 | /** |
||
2663 | * Put an alignment marker. |
||
2664 | * |
||
2665 | * @param array $frame frame |
||
2666 | * @param int $width width |
||
2667 | * @param int $ox X center coordinate of the pattern |
||
2668 | * @param int $oy Y center coordinate of the pattern |
||
2669 | * |
||
2670 | * @return array frame |
||
2671 | */ |
||
2672 | protected function putAlignmentMarker($frame, $ox, $oy) |
||
2673 | { |
||
2674 | $finder = [ |
||
2675 | "\xa1\xa1\xa1\xa1\xa1", |
||
2676 | "\xa1\xa0\xa0\xa0\xa1", |
||
2677 | "\xa1\xa0\xa1\xa0\xa1", |
||
2678 | "\xa1\xa0\xa0\xa0\xa1", |
||
2679 | "\xa1\xa1\xa1\xa1\xa1", |
||
2680 | ]; |
||
2681 | $yStart = $oy - 2; |
||
2682 | $xStart = $ox - 2; |
||
2683 | for ($y = 0; $y < 5; $y++) { |
||
2684 | $frame = $this->qrstrset($frame, $xStart, $yStart + $y, $finder[$y]); |
||
2685 | } |
||
2686 | |||
2687 | return $frame; |
||
2688 | } |
||
2689 | |||
2690 | /** |
||
2691 | * Put an alignment pattern. |
||
2692 | * |
||
2693 | * @param int $version version |
||
2694 | * @param array $fram frame |
||
2695 | * @param int $width width |
||
2696 | * |
||
2697 | * @return array frame |
||
2698 | */ |
||
2699 | protected function putAlignmentPattern($version, $frame, $width) |
||
2700 | { |
||
2701 | if ($version < 2) { |
||
2702 | return $frame; |
||
2703 | } |
||
2704 | $d = $this->alignmentPattern[$version][1] - $this->alignmentPattern[$version][0]; |
||
2705 | if ($d < 0) { |
||
2706 | $w = 2; |
||
2707 | } else { |
||
2708 | $w = (int) (($width - $this->alignmentPattern[$version][0]) / $d + 2); |
||
2709 | } |
||
2710 | if ($w * $w - 3 == 1) { |
||
2711 | $x = $this->alignmentPattern[$version][0]; |
||
2712 | $y = $this->alignmentPattern[$version][0]; |
||
2713 | $frame = $this->putAlignmentMarker($frame, $x, $y); |
||
2714 | |||
2715 | return $frame; |
||
2716 | } |
||
2717 | $cx = $this->alignmentPattern[$version][0]; |
||
2718 | $wo = $w - 1; |
||
2719 | for ($x = 1; $x < $wo; $x++) { |
||
2720 | $frame = $this->putAlignmentMarker($frame, 6, $cx); |
||
2721 | $frame = $this->putAlignmentMarker($frame, $cx, 6); |
||
2722 | $cx += $d; |
||
2723 | } |
||
2724 | $cy = $this->alignmentPattern[$version][0]; |
||
2725 | for ($y = 0; $y < $wo; $y++) { |
||
2726 | $cx = $this->alignmentPattern[$version][0]; |
||
2727 | for ($x = 0; $x < $wo; $x++) { |
||
2728 | $frame = $this->putAlignmentMarker($frame, $cx, $cy); |
||
2729 | $cx += $d; |
||
2730 | } |
||
2731 | $cy += $d; |
||
2732 | } |
||
2733 | |||
2734 | return $frame; |
||
2735 | } |
||
2736 | |||
2737 | /** |
||
2738 | * Return BCH encoded version information pattern that is used for the symbol of version 7 or greater. Use lower 18 bits. |
||
2739 | * |
||
2740 | * @param int $version version |
||
2741 | * |
||
2742 | * @return BCH encoded version information pattern |
||
2743 | */ |
||
2744 | protected function getVersionPattern($version) |
||
2745 | { |
||
2746 | if (($version < 7) or ($version > QRSPEC_VERSION_MAX)) { |
||
2747 | return 0; |
||
2748 | } |
||
2749 | |||
2750 | return $this->versionPattern[($version - 7)]; |
||
2751 | } |
||
2752 | |||
2753 | /** |
||
2754 | * Return BCH encoded format information pattern. |
||
2755 | * |
||
2756 | * @param array $mask |
||
2757 | * @param int $level error correction level |
||
2758 | * |
||
2759 | * @return BCH encoded format information pattern |
||
2760 | */ |
||
2761 | protected function getFormatInfo($mask, $level) |
||
2762 | { |
||
2763 | if (($mask < 0) or ($mask > 7)) { |
||
2764 | return 0; |
||
2765 | } |
||
2766 | if (($level < 0) or ($level > 3)) { |
||
2767 | return 0; |
||
2768 | } |
||
2769 | |||
2770 | return $this->formatInfo[$level][$mask]; |
||
2771 | } |
||
2772 | |||
2773 | /** |
||
2774 | * Put a finder pattern. |
||
2775 | * |
||
2776 | * @param array $frame frame |
||
2777 | * @param int $width width |
||
2778 | * @param int $ox X center coordinate of the pattern |
||
2779 | * @param int $oy Y center coordinate of the pattern |
||
2780 | * |
||
2781 | * @return array frame |
||
2782 | */ |
||
2783 | protected function putFinderPattern($frame, $ox, $oy) |
||
2784 | { |
||
2785 | $finder = [ |
||
2786 | "\xc1\xc1\xc1\xc1\xc1\xc1\xc1", |
||
2787 | "\xc1\xc0\xc0\xc0\xc0\xc0\xc1", |
||
2788 | "\xc1\xc0\xc1\xc1\xc1\xc0\xc1", |
||
2789 | "\xc1\xc0\xc1\xc1\xc1\xc0\xc1", |
||
2790 | "\xc1\xc0\xc1\xc1\xc1\xc0\xc1", |
||
2791 | "\xc1\xc0\xc0\xc0\xc0\xc0\xc1", |
||
2792 | "\xc1\xc1\xc1\xc1\xc1\xc1\xc1", |
||
2793 | ]; |
||
2794 | for ($y = 0; $y < 7; $y++) { |
||
2795 | $frame = $this->qrstrset($frame, $ox, ($oy + $y), $finder[$y]); |
||
2796 | } |
||
2797 | |||
2798 | return $frame; |
||
2799 | } |
||
2800 | |||
2801 | /** |
||
2802 | * Return a copy of initialized frame. |
||
2803 | * |
||
2804 | * @param int $version version |
||
2805 | * |
||
2806 | * @return array of unsigned char. |
||
2807 | */ |
||
2808 | protected function createFrame($version) |
||
2809 | { |
||
2810 | $width = $this->capacity[$version][QRCAP_WIDTH]; |
||
2811 | $frameLine = str_repeat("\0", $width); |
||
2812 | $frame = array_fill(0, $width, $frameLine); |
||
2813 | // Finder pattern |
||
2814 | $frame = $this->putFinderPattern($frame, 0, 0); |
||
2815 | $frame = $this->putFinderPattern($frame, $width - 7, 0); |
||
2816 | $frame = $this->putFinderPattern($frame, 0, $width - 7); |
||
2817 | // Separator |
||
2818 | $yOffset = $width - 7; |
||
2819 | for ($y = 0; $y < 7; $y++) { |
||
2820 | $frame[$y][7] = "\xc0"; |
||
2821 | $frame[$y][$width - 8] = "\xc0"; |
||
2822 | $frame[$yOffset][7] = "\xc0"; |
||
2823 | $yOffset++; |
||
2824 | } |
||
2825 | $setPattern = str_repeat("\xc0", 8); |
||
2826 | $frame = $this->qrstrset($frame, 0, 7, $setPattern); |
||
2827 | $frame = $this->qrstrset($frame, $width - 8, 7, $setPattern); |
||
2828 | $frame = $this->qrstrset($frame, 0, $width - 8, $setPattern); |
||
2829 | // Format info |
||
2830 | $setPattern = str_repeat("\x84", 9); |
||
2831 | $frame = $this->qrstrset($frame, 0, 8, $setPattern); |
||
2832 | $frame = $this->qrstrset($frame, $width - 8, 8, $setPattern, 8); |
||
2833 | $yOffset = $width - 8; |
||
2834 | for ($y = 0; $y < 8; ++$y, ++$yOffset) { |
||
2835 | $frame[$y][8] = "\x84"; |
||
2836 | $frame[$yOffset][8] = "\x84"; |
||
2837 | } |
||
2838 | // Timing pattern |
||
2839 | $wo = $width - 15; |
||
2840 | for ($i = 1; $i < $wo; $i++) { |
||
2841 | $frame[6][7 + $i] = chr(0x90 | ($i & 1)); |
||
2842 | $frame[7 + $i][6] = chr(0x90 | ($i & 1)); |
||
2843 | } |
||
2844 | // Alignment pattern |
||
2845 | $frame = $this->putAlignmentPattern($version, $frame, $width); |
||
2846 | // Version information |
||
2847 | if ($version >= 7) { |
||
2848 | $vinf = $this->getVersionPattern($version); |
||
2849 | $v = $vinf; |
||
2850 | for ($x = 0; $x < 6; $x++) { |
||
2851 | for ($y = 0; $y < 3; $y++) { |
||
2852 | $frame[($width - 11) + $y][$x] = chr(0x88 | ($v & 1)); |
||
2853 | $v = $v >> 1; |
||
2854 | } |
||
2855 | } |
||
2856 | $v = $vinf; |
||
2857 | for ($y = 0; $y < 6; $y++) { |
||
2858 | for ($x = 0; $x < 3; $x++) { |
||
2859 | $frame[$y][$x + ($width - 11)] = chr(0x88 | ($v & 1)); |
||
2860 | $v = $v >> 1; |
||
2861 | } |
||
2862 | } |
||
2863 | } |
||
2864 | // and a little bit... |
||
2865 | $frame[$width - 8][8] = "\x81"; |
||
2866 | |||
2867 | return $frame; |
||
2868 | } |
||
2869 | |||
2870 | /** |
||
2871 | * Set new frame for the specified version. |
||
2872 | * |
||
2873 | * @param int $version version |
||
2874 | * |
||
2875 | * @return array of unsigned char. |
||
2876 | */ |
||
2877 | protected function newFrame($version) |
||
2878 | { |
||
2879 | if (($version < 1) or ($version > QRSPEC_VERSION_MAX)) { |
||
2880 | return; |
||
2881 | } |
||
2882 | if (!isset($this->frames[$version])) { |
||
2883 | $this->frames[$version] = $this->createFrame($version); |
||
2884 | } |
||
2885 | if (is_null($this->frames[$version])) { |
||
2886 | return; |
||
2887 | } |
||
2888 | |||
2889 | return $this->frames[$version]; |
||
2890 | } |
||
2891 | |||
2892 | /** |
||
2893 | * Return block number 0. |
||
2894 | * |
||
2895 | * @param array $spec |
||
2896 | * |
||
2897 | * @return int value |
||
2898 | */ |
||
2899 | protected function rsBlockNum($spec) |
||
2900 | { |
||
2901 | return $spec[0] + $spec[3]; |
||
2902 | } |
||
2903 | |||
2904 | /** |
||
2905 | * Return block number 1. |
||
2906 | * |
||
2907 | * @param array $spec |
||
2908 | * |
||
2909 | * @return int value |
||
2910 | */ |
||
2911 | protected function rsBlockNum1($spec) |
||
2912 | { |
||
2913 | return $spec[0]; |
||
2914 | } |
||
2915 | |||
2916 | /** |
||
2917 | * Return data codes 1. |
||
2918 | * |
||
2919 | * @param array $spec |
||
2920 | * |
||
2921 | * @return int value |
||
2922 | */ |
||
2923 | protected function rsDataCodes1($spec) |
||
2924 | { |
||
2925 | return $spec[1]; |
||
2926 | } |
||
2927 | |||
2928 | /** |
||
2929 | * Return ecc codes 1. |
||
2930 | * |
||
2931 | * @param array $spec |
||
2932 | * |
||
2933 | * @return int value |
||
2934 | */ |
||
2935 | protected function rsEccCodes1($spec) |
||
2936 | { |
||
2937 | return $spec[2]; |
||
2938 | } |
||
2939 | |||
2940 | /** |
||
2941 | * Return block number 2. |
||
2942 | * |
||
2943 | * @param array $spec |
||
2944 | * |
||
2945 | * @return int value |
||
2946 | */ |
||
2947 | protected function rsBlockNum2($spec) |
||
2950 | } |
||
2951 | |||
2952 | /** |
||
2953 | * Return data codes 2. |
||
2954 | * |
||
2955 | * @param array $spec |
||
2956 | * |
||
2957 | * @return int value |
||
2958 | */ |
||
2959 | protected function rsDataCodes2($spec) |
||
2960 | { |
||
2961 | return $spec[4]; |
||
2962 | } |
||
2963 | |||
2964 | /** |
||
2965 | * Return ecc codes 2. |
||
2966 | * |
||
2967 | * @param array $spec |
||
2968 | * |
||
2969 | * @return int value |
||
2970 | */ |
||
2971 | protected function rsEccCodes2($spec) |
||
2972 | { |
||
2973 | return $spec[2]; |
||
2974 | } |
||
2975 | |||
2976 | /** |
||
2977 | * Return data length. |
||
2978 | * |
||
2979 | * @param array $spec |
||
2980 | * |
||
2981 | * @return int value |
||
2982 | */ |
||
2983 | protected function rsDataLength($spec) |
||
2984 | { |
||
2985 | return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]); |
||
2986 | } |
||
2987 | |||
2988 | /** |
||
2989 | * Return ecc length. |
||
2990 | * |
||
2991 | * @param array $spec |
||
2992 | * |
||
2993 | * @return int value |
||
2994 | */ |
||
2995 | protected function rsEccLength($spec) |
||
2996 | { |
||
2997 | return ($spec[0] + $spec[3]) * $spec[2]; |
||
2998 | } |
||
2999 | |||
3000 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
3001 | |||
3002 | // QRrs |
||
3003 | |||
3004 | /** |
||
3005 | * Initialize a Reed-Solomon codec and add it to existing rsitems. |
||
3006 | * |
||
3007 | * @param int $symsize symbol size, bits |
||
3008 | * @param int $gfpoly Field generator polynomial coefficients |
||
3009 | * @param int $fcr first root of RS code generator polynomial, index form |
||
3010 | * @param int $prim primitive element to generate polynomial roots |
||
3011 | * @param int $nroots RS code generator polynomial degree (number of roots) |
||
3012 | * @param int $pad padding bytes at front of shortened block |
||
3013 | * |
||
3014 | * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>. |
||
3015 | */ |
||
3016 | protected function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) |
||
3017 | { |
||
3018 | foreach ($this->rsitems as $rs) { |
||
3019 | if (($rs['pad'] != $pad) or ($rs['nroots'] != $nroots) or ($rs['mm'] != $symsize) |
||
3020 | or ($rs['gfpoly'] != $gfpoly) or ($rs['fcr'] != $fcr) or ($rs['prim'] != $prim)) { |
||
3021 | continue; |
||
3022 | } |
||
3023 | |||
3024 | return $rs; |
||
3025 | } |
||
3026 | $rs = $this->init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad); |
||
3027 | array_unshift($this->rsitems, $rs); |
||
3028 | |||
3029 | return $rs; |
||
3030 | } |
||
3031 | |||
3032 | // - - - - - - - - - - - - - - - - - - - - - - - - - |
||
3033 | |||
3034 | // QRrsItem |
||
3035 | |||
3036 | /** |
||
3037 | * modnn. |
||
3038 | * |
||
3039 | * @param array RS values |
||
3040 | * @param int $x X position |
||
3041 | * |
||
3042 | * @return int X osition |
||
3043 | */ |
||
3044 | protected function modnn($rs, $x) |
||
3045 | { |
||
3046 | while ($x >= $rs['nn']) { |
||
3047 | $x -= $rs['nn']; |
||
3048 | $x = ($x >> $rs['mm']) + ($x & $rs['nn']); |
||
3049 | } |
||
3050 | |||
3051 | return $x; |
||
3052 | } |
||
3053 | |||
3054 | /** |
||
3055 | * Initialize a Reed-Solomon codec and returns an array of values. |
||
3056 | * |
||
3057 | * @param int $symsize symbol size, bits |
||
3058 | * @param int $gfpoly Field generator polynomial coefficients |
||
3059 | * @param int $fcr first root of RS code generator polynomial, index form |
||
3060 | * @param int $prim primitive element to generate polynomial roots |
||
3061 | * @param int $nroots RS code generator polynomial degree (number of roots) |
||
3062 | * @param int $pad padding bytes at front of shortened block |
||
3063 | * |
||
3064 | * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>. |
||
3065 | */ |
||
3066 | protected function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) |
||
3067 | { |
||
3068 | // Based on Reed solomon encoder by Phil Karn, KA9Q (GNU-LGPLv2) |
||
3069 | $rs = null; |
||
3070 | // Check parameter ranges |
||
3071 | if (($symsize < 0) or ($symsize > 8)) { |
||
3072 | return $rs; |
||
3073 | } |
||
3074 | if (($fcr < 0) or ($fcr >= (1 << $symsize))) { |
||
3075 | return $rs; |
||
3076 | } |
||
3077 | if (($prim <= 0) or ($prim >= (1 << $symsize))) { |
||
3078 | return $rs; |
||
3079 | } |
||
3080 | if (($nroots < 0) or ($nroots >= (1 << $symsize))) { |
||
3081 | return $rs; |
||
3082 | } |
||
3083 | if (($pad < 0) or ($pad >= ((1 << $symsize) - 1 - $nroots))) { |
||
3084 | return $rs; |
||
3085 | } |
||
3086 | $rs = []; |
||
3087 | $rs['mm'] = $symsize; |
||
3088 | $rs['nn'] = (1 << $symsize) - 1; |
||
3089 | $rs['pad'] = $pad; |
||
3090 | $rs['alpha_to'] = array_fill(0, ($rs['nn'] + 1), 0); |
||
3091 | $rs['index_of'] = array_fill(0, ($rs['nn'] + 1), 0); |
||
3092 | // PHP style macro replacement ;) |
||
3093 | $NN = &$rs['nn']; |
||
3094 | $A0 = &$NN; |
||
3095 | // Generate Galois field lookup tables |
||
3096 | $rs['index_of'][0] = $A0; // log(zero) = -inf |
||
3097 | $rs['alpha_to'][$A0] = 0; // alpha**-inf = 0 |
||
3098 | $sr = 1; |
||
3099 | for ($i = 0; $i < $rs['nn']; $i++) { |
||
3100 | $rs['index_of'][$sr] = $i; |
||
3101 | $rs['alpha_to'][$i] = $sr; |
||
3102 | $sr <<= 1; |
||
3103 | if ($sr & (1 << $symsize)) { |
||
3104 | $sr ^= $gfpoly; |
||
3105 | } |
||
3106 | $sr &= $rs['nn']; |
||
3107 | } |
||
3108 | if ($sr != 1) { |
||
3109 | // field generator polynomial is not primitive! |
||
3110 | return; |
||
3111 | } |
||
3112 | // Form RS code generator polynomial from its roots |
||
3113 | $rs['genpoly'] = array_fill(0, ($nroots + 1), 0); |
||
3114 | $rs['fcr'] = $fcr; |
||
3115 | $rs['prim'] = $prim; |
||
3116 | $rs['nroots'] = $nroots; |
||
3117 | $rs['gfpoly'] = $gfpoly; |
||
3118 | // Find prim-th root of 1, used in decoding |
||
3119 | for ($iprim = 1; ($iprim % $prim) != 0; $iprim += $rs['nn']) { |
||
3120 | // intentional empty-body loop! |
||
3121 | } |
||
3122 | $rs['iprim'] = (int) ($iprim / $prim); |
||
3123 | $rs['genpoly'][0] = 1; |
||
3124 | |||
3125 | for ($i = 0, $root = $fcr * $prim; $i < $nroots; $i++, $root += $prim) { |
||
3126 | $rs['genpoly'][$i + 1] = 1; |
||
3127 | // Multiply rs->genpoly[] by @**(root + x) |
||
3128 | for ($j = $i; $j > 0; $j--) { |
||
3129 | if ($rs['genpoly'][$j] != 0) { |
||
3130 | $rs['genpoly'][$j] = $rs['genpoly'][$j - 1] ^ $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][$j]] + $root)]; |
||
3131 | } else { |
||
3132 | $rs['genpoly'][$j] = $rs['genpoly'][$j - 1]; |
||
3133 | } |
||
3134 | } |
||
3135 | // rs->genpoly[0] can never be zero |
||
3136 | $rs['genpoly'][0] = $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][0]] + $root)]; |
||
3137 | } |
||
3138 | // convert rs->genpoly[] to index form for quicker encoding |
||
3139 | for ($i = 0; $i <= $nroots; $i++) { |
||
3140 | $rs['genpoly'][$i] = $rs['index_of'][$rs['genpoly'][$i]]; |
||
3141 | } |
||
3142 | |||
3143 | return $rs; |
||
3144 | } |
||
3145 | |||
3146 | /** |
||
3147 | * Encode a Reed-Solomon codec and returns the parity array. |
||
3148 | * |
||
3149 | * @param array $rs RS values |
||
3150 | * @param array $data data |
||
3151 | * @param array $parity parity |
||
3152 | * |
||
3153 | * @return parity array |
||
3154 | */ |
||
3155 | protected function encode_rs_char($rs, $data, $parity) |
||
3190 | } |
||
3191 | } // end QRcode class |
||
3192 | } // END OF "class_exists QRcode" |
||
3193 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths