source-class-Com.Tecnick.Barcode.Type.Linear.CodeOneTwoEight.Process

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\Linear\CodeOneTwoEight;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: 
 20: /**
 21:  * Com\Tecnick\Barcode\Type\Linear\CodeOneTwoEight\Process;
 22:  *
 23:  * Process methods for CodeOneTwoEight Barcode type class
 24:  * CODE 128
 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 Process extends \Com\Tecnick\Barcode\Type\Linear
 35: {
 36:     /**
 37:      * Get the numeric sequence (if any)
 38:      *
 39:      * @param string $code Code to parse
 40:      *
 41:      * @return array
 42:      *
 43:      * @throws BarcodeException in case of error
 44:      */
 45:     protected function getNumericSequence($code)
 46:     {
 47:         $sequence = array();
 48:         $len = strlen($code);
 49:         // get numeric sequences (if any)
 50:         $numseq = array();
 51:         preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE);
 52:         if (!empty($numseq[1])) {
 53:             $end_offset = 0;
 54:             foreach ($numseq[1] as $val) {
 55:                 $offset = $val[1];
 56:                 if ($offset > $end_offset) {
 57:                     // non numeric sequence
 58:                     $sequence = array_merge(
 59:                         $sequence,
 60:                         $this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset)))
 61:                     );
 62:                 }
 63:                 // numeric sequence
 64:                 $slen = strlen($val[0]);
 65:                 if (($slen % 2) != 0) {
 66:                     // the length must be even
 67:                     --$slen;
 68:                 }
 69:                 $sequence[] = array('C', substr($code, $offset, $slen), $slen);
 70:                 $end_offset = $offset + $slen;
 71:             }
 72:             if ($end_offset < $len) {
 73:                 $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset)));
 74:             }
 75:         } else {
 76:             // text code (non C mode)
 77:             $sequence = array_merge($sequence, $this->get128ABsequence($code));
 78:         }
 79:         return $sequence;
 80:     }
 81: 
 82:     /**
 83:      * Split text code in A/B sequence for 128 code
 84:      *
 85:      * @param string $code Code to split
 86:      *
 87:      * @return array sequence
 88:      */
 89:     protected function get128ABsequence($code)
 90:     {
 91:         $len = strlen($code);
 92:         $sequence = array();
 93:         // get A sequences (if any)
 94:         $aseq = array();
 95:         preg_match_all('/([\x00-\x1f])/', $code, $aseq, PREG_OFFSET_CAPTURE);
 96:         if (!empty($aseq[1])) {
 97:             // get the entire A sequence (excluding FNC1-FNC4)
 98:             preg_match_all('/([\x00-\x5f]+)/', $code, $aseq, PREG_OFFSET_CAPTURE);
 99:             $end_offset = 0;
100:             foreach ($aseq[1] as $val) {
101:                 $offset = $val[1];
102:                 if ($offset > $end_offset) {
103:                     // B sequence
104:                     $sequence[] = array(
105:                         'B',
106:                         substr($code, $end_offset, ($offset - $end_offset)),
107:                         ($offset - $end_offset)
108:                     );
109:                 }
110:                 // A sequence
111:                 $slen = strlen($val[0]);
112:                 $sequence[] = array('A', substr($code, $offset, $slen), $slen);
113:                 $end_offset = $offset + $slen;
114:             }
115:             if ($end_offset < $len) {
116:                 $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset));
117:             }
118:         } else {
119:             // only B sequence
120:             $sequence[] = array('B', $code, $len);
121:         }
122:         return $sequence;
123:     }
124: 
125: 
126:     /**
127:      * Get the A code point array
128:      *
129:      * @param array  $code_data  Array of codepoints to alter
130:      * @param string $code       Code to process
131:      * @param int    $len        Number of characters to process
132:      *
133:      * @retun array
134:      *
135:      * @throws BarcodeException in case of error
136:      */
137:     protected function getCodeDataA(&$code_data, $code, $len)
138:     {
139:         for ($pos = 0; $pos < $len; ++$pos) {
140:             $char = $code[$pos];
141:             $char_id = ord($char);
142:             if (($char_id >= 241) && ($char_id <= 244)) {
143:                 $code_data[] = $this->fnc_a[$char_id];
144:             } elseif (($char_id >= 0) && ($char_id <= 95)) {
145:                 $code_data[] = strpos($this->keys_a, $char);
146:             } else {
147:                 throw new BarcodeException('Invalid character sequence');
148:             }
149:         }
150:     }
151: 
152:     /**
153:      * Get the B code point array
154:      *
155:      * @param array  $code_data  Array of codepoints to alter
156:      * @param string $code       Code to process
157:      * @param int    $len        Number of characters to process
158:      *
159:      * @retun array
160:      *
161:      * @throws BarcodeException in case of error
162:      */
163:     protected function getCodeDataB(&$code_data, $code, $len)
164:     {
165:         for ($pos = 0; $pos < $len; ++$pos) {
166:             $char = $code[$pos];
167:             $char_id = ord($char);
168:             if (($char_id >= 241) && ($char_id <= 244)) {
169:                 $code_data[] = $this->fnc_b[$char_id];
170:             } elseif (($char_id >= 32) && ($char_id <= 127)) {
171:                 $code_data[] = strpos($this->keys_b, $char);
172:             } else {
173:                 throw new BarcodeException('Invalid character sequence: '.$char_id);
174:             }
175:         }
176:     }
177: 
178:     /**
179:      * Get the C code point array
180:      *
181:      * @param array  $code_data  Array of codepoints to alter
182:      * @param string $code       Code to process
183:      *
184:      * @retun array
185:      *
186:      * @throws BarcodeException in case of error
187:      */
188:     protected function getCodeDataC(&$code_data, $code)
189:     {
190:         // code blocks separated by FNC1 (chr 241)
191:         $blocks = explode(chr(241), $code);
192: 
193:         foreach ($blocks as $blk) {
194:             $len = strlen($blk);
195:   
196:             if (($len % 2) != 0) {
197:                 throw new BarcodeException('The length of each FNC1-separated code block must be even');
198:             }
199: 
200:             for ($pos = 0; $pos < $len; $pos += 2) {
201:                 $chrnum = $blk[$pos].$blk[($pos + 1)];
202:                 if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
203:                     $code_data[] = intval($chrnum);
204:                 } else {
205:                     throw new BarcodeException('Invalid character sequence');
206:                 }
207:             }
208: 
209:             $code_data[] = 102;
210:         }
211: 
212:         // remove last 102 code
213:         array_pop($code_data);
214:     }
215: 
216:     /**
217:      * Finalize code data
218:      *
219:      * @param array  $code_data  Array of codepoints to alter
220:      * @param int    $startid    Start ID code
221:      *
222:      * @return array
223:      */
224:     protected function finalizeCodeData($code_data, $startid)
225:     {
226:         // calculate check character
227:         $sum = $startid;
228:         foreach ($code_data as $key => $val) {
229:             $sum += ($val * ($key + 1));
230:         }
231:         // add check character
232:         $code_data[] = ($sum % 103);
233: 
234:         // add stop sequence
235:         $code_data[] = 106;
236:         $code_data[] = 107;
237:         // add start code at the beginning
238:         array_unshift($code_data, $startid);
239: 
240:         return $code_data;
241:     }
242: }
243: 
 

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