source-class-Com.Tecnick.Barcode.Type.Square.PdfFourOneSeven.Sequence

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Exceptions

  1: <?php
  2: /**
  3:  * Process.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Barcode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 12:  *
 13:  * This file is part of tc-lib-barcode software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: use \Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven\Data;
 20: 
 21: /**
 22:  * Com\Tecnick\Barcode\Type\Square\PdfFourOneSeven\Sequence
 23:  *
 24:  * Process for PdfFourOneSeven Barcode type class
 25:  *
 26:  * @since       2015-02-21
 27:  * @category    Library
 28:  * @package     Barcode
 29:  * @author      Nicola Asuni <info@tecnick.com>
 30:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 31:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 32:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 33:  */
 34: abstract class Sequence extends \Com\Tecnick\Barcode\Type\Square
 35: {
 36:     /**
 37:      * Get the error correction level (0-8) to be used
 38:      *
 39:      * @param int $ecl   Error correction level
 40:      * @param int $numcw Number of data codewords
 41:      *
 42:      * @return int error correction level
 43:      */
 44:     protected function getErrorCorrectionLevel($ecl, $numcw)
 45:     {
 46:         $maxecl = 8; // maximum error level
 47:         $maxerrsize = (928 - $numcw); // available codewords for error
 48:         while (($maxecl > 0) && ($maxerrsize < (2 << $maxecl))) {
 49:             --$maxecl;
 50:         }
 51:         if (($ecl < 0) || ($ecl > 8)) {
 52:             if ($numcw < 41) {
 53:                 $ecl = 2;
 54:             } elseif ($numcw < 161) {
 55:                 $ecl = 3;
 56:             } elseif ($numcw < 321) {
 57:                 $ecl = 4;
 58:             } elseif ($numcw < 864) {
 59:                 $ecl = 5;
 60:             } else {
 61:                 $ecl = $maxecl;
 62:             }
 63:         }
 64:         return min($maxecl, $ecl);
 65:     }
 66: 
 67:     /**
 68:      * Get the error correction codewords
 69:      *
 70:      * @param array $codewords  Array of codewords including Symbol Length Descriptor and pad
 71:      * @param int   $ecl        Error correction level 0-8
 72:      *
 73:      * @return array of error correction codewords
 74:      */
 75:     protected function getErrorCorrection($codewords, $ecl)
 76:     {
 77:         // get error correction coefficients
 78:         $ecc = Data::$rsfactors[$ecl];
 79:         // number of error correction factors
 80:         $eclsize = (2 << $ecl);
 81:         // maximum index for $rsfactors[$ecl]
 82:         $eclmaxid = ($eclsize - 1);
 83:         // initialize array of error correction codewords
 84:         $ecw = array_fill(0, $eclsize, 0);
 85:         // for each data codeword
 86:         foreach ($codewords as $cdw) {
 87:             $tk1 = ($cdw + $ecw[$eclmaxid]) % 929;
 88:             for ($idx = $eclmaxid; $idx > 0; --$idx) {
 89:                 $tk2 = ($tk1 * $ecc[$idx]) % 929;
 90:                 $tk3 = 929 - $tk2;
 91:                 $ecw[$idx] = ($ecw[($idx - 1)] + $tk3) % 929;
 92:             }
 93:             $tk2 = ($tk1 * $ecc[0]) % 929;
 94:             $tk3 = 929 - $tk2;
 95:             $ecw[0] = $tk3 % 929;
 96:         }
 97:         foreach ($ecw as $idx => $err) {
 98:             if ($err != 0) {
 99:                 $ecw[$idx] = 929 - $err;
100:             }
101:         }
102:         return array_reverse($ecw);
103:     }
104: 
105:     /**
106:      * Process a single sequence
107:      *
108:      * @param array  $sequence_array  Sequence to process
109:      * @param string $code            Data to process
110:      * @param int    $seq             Current sequence
111:      * @param int    $offset          Current code offset
112:      */
113:     protected function processSequence(&$sequence_array, $code, $seq, $offset)
114:     {
115:         // extract text sequence before the number sequence
116:         $prevseq = substr($code, $offset, ($seq - $offset));
117:         $textseq = array();
118:         // get text sequences
119:         preg_match_all('/([\x09\x0a\x0d\x20-\x7e]{5,})/', $prevseq, $textseq, PREG_OFFSET_CAPTURE);
120:         $textseq[1][] = array('', strlen($prevseq));
121:         $txtoffset = 0;
122:         foreach ($textseq[1] as $txtseq) {
123:             $txtseqlen = strlen($txtseq[0]);
124:             if ($txtseq[1] > 0) {
125:                 // extract byte sequence before the text sequence
126:                 $prevtxtseq = substr($prevseq, $txtoffset, ($txtseq[1] - $txtoffset));
127:                 if (strlen($prevtxtseq) > 0) {
128:                     // add BYTE sequence
129:                     if ((strlen($prevtxtseq) == 1)
130:                         && ((count($sequence_array) > 0)
131:                         && ($sequence_array[(count($sequence_array) - 1)][0] == 900))
132:                     ) {
133:                         $sequence_array[] = array(913, $prevtxtseq);
134:                     } elseif ((strlen($prevtxtseq) % 6) == 0) {
135:                         $sequence_array[] = array(924, $prevtxtseq);
136:                     } else {
137:                         $sequence_array[] = array(901, $prevtxtseq);
138:                     }
139:                 }
140:             }
141:             if ($txtseqlen > 0) {
142:                 // add numeric sequence
143:                 $sequence_array[] = array(900, $txtseq[0]);
144:             }
145:             $txtoffset = ($txtseq[1] + $txtseqlen);
146:         }
147:     }
148: 
149:     /**
150:      * Get an array of sequences from input
151:      *
152:      * @param string $code Data to process
153:      *
154:      * @return bidimensional array containing characters and classification
155:      */
156:     protected function getInputSequences($code)
157:     {
158:         $sequence_array = array(); // array to be returned
159:         $numseq = array();
160:         // get numeric sequences
161:         preg_match_all('/([0-9]{13,})/', $code, $numseq, PREG_OFFSET_CAPTURE);
162:         $numseq[1][] = array('', strlen($code));
163:         $offset = 0;
164:         foreach ($numseq[1] as $seq) {
165:             $seqlen = strlen($seq[0]);
166:             if ($seq[1] > 0) {
167:                 $this->processSequence($sequence_array, $code, $seq[1], $offset);
168:             }
169:             if ($seqlen > 0) {
170:                 // add numeric sequence
171:                 $sequence_array[] = array(902, $seq[0]);
172:             }
173:             $offset = ($seq[1] + $seqlen);
174:         }
175:         return $sequence_array;
176:     }
177: }
178: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy