source-class-Com.Tecnick.Barcode.Type.Square.Datamatrix.Steps

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:  * Steps.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Barcode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2015-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\Datamatrix;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: use \Com\Tecnick\Barcode\Type\Square\Datamatrix\Data;
 20: 
 21: /**
 22:  * Com\Tecnick\Barcode\Type\Square\Datamatrix\Steps
 23:  *
 24:  * Steps methods for Datamatrix Barcode type class
 25:  *
 26:  * @since       2015-02-21
 27:  * @category    Library
 28:  * @package     Barcode
 29:  * @author      Nicola Asuni <info@tecnick.com>
 30:  * @copyright   2015-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 Steps extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\Modes
 35: {
 36:     /**
 37:      * The look-ahead test scans the data to be encoded to find the best mode (Annex P - steps from J to S).
 38:      *
 39:      * @param string $data Data to encode
 40:      * @param int    $pos  Current position
 41:      * @param int    $mode Current encoding mode
 42:      *
 43:      * @return int encoding mode
 44:      */
 45:     public function lookAheadTest($data, $pos, $mode)
 46:     {
 47:         $data_length = strlen($data);
 48:         if ($pos >= $data_length) {
 49:             return $mode;
 50:         }
 51:         $charscount = 0; // count processed chars
 52:         // STEP J
 53:         if ($mode == Data::ENC_ASCII) {
 54:             $numch = array(0, 1, 1, 1, 1, 1.25);
 55:         } else {
 56:             $numch = array(1, 2, 2, 2, 2, 2.25);
 57:             $numch[$mode] = 0;
 58:         }
 59:         while (true) {
 60:             if (($pos + $charscount) == $data_length) {
 61:                 return $this->stepK($numch);
 62:             }
 63:             $chr = ord($data[$pos + $charscount]);
 64:             $charscount++;
 65:             $this->stepL($chr, $numch);
 66:             $this->stepM($chr, $numch);
 67:             $this->stepN($chr, $numch);
 68:             $this->stepO($chr, $numch);
 69:             $this->stepP($chr, $numch);
 70:             $this->stepQ($chr, $numch);
 71:             if ($charscount >= 4) {
 72:                 $ret = $this->stepR($numch, $pos, $data_length, $charscount, $data);
 73:                 if ($ret !== null) {
 74:                     return $ret;
 75:                 }
 76:             }
 77:         }
 78:         throw new BarcodeException('LookAhead Error');
 79:     }
 80: 
 81:     /**
 82:      * Step K
 83:      *
 84:      * @param int $numch
 85:      *
 86:      * @return int
 87:      */
 88:     protected function stepK($numch)
 89:     {
 90:         if ($numch[Data::ENC_ASCII] <= ceil(min(
 91:             $numch[Data::ENC_C40],
 92:             $numch[Data::ENC_TXT],
 93:             $numch[Data::ENC_X12],
 94:             $numch[Data::ENC_EDF],
 95:             $numch[Data::ENC_BASE256]
 96:         ))) {
 97:             return Data::ENC_ASCII;
 98:         }
 99:         if ($numch[Data::ENC_BASE256] < ceil(min(
100:             $numch[Data::ENC_ASCII],
101:             $numch[Data::ENC_C40],
102:             $numch[Data::ENC_TXT],
103:             $numch[Data::ENC_X12],
104:             $numch[Data::ENC_EDF]
105:         ))) {
106:             return Data::ENC_BASE256;
107:         }
108:         if ($numch[Data::ENC_EDF] < ceil(min(
109:             $numch[Data::ENC_ASCII],
110:             $numch[Data::ENC_C40],
111:             $numch[Data::ENC_TXT],
112:             $numch[Data::ENC_X12],
113:             $numch[Data::ENC_BASE256]
114:         ))) {
115:             return Data::ENC_EDF;
116:         }
117:         if ($numch[Data::ENC_TXT] < ceil(min(
118:             $numch[Data::ENC_ASCII],
119:             $numch[Data::ENC_C40],
120:             $numch[Data::ENC_X12],
121:             $numch[Data::ENC_EDF],
122:             $numch[Data::ENC_BASE256]
123:         ))) {
124:             return Data::ENC_TXT;
125:         }
126:         if ($numch[Data::ENC_X12] < ceil(min(
127:             $numch[Data::ENC_ASCII],
128:             $numch[Data::ENC_C40],
129:             $numch[Data::ENC_TXT],
130:             $numch[Data::ENC_EDF],
131:             $numch[Data::ENC_BASE256]
132:         ))) {
133:             return Data::ENC_X12;
134:         }
135:         return Data::ENC_C40;
136:     }
137: 
138:     /**
139:      * Step L
140:      *
141:      * @param int $chr
142:      * @param int $numch
143:      */
144:     protected function stepL($chr, &$numch)
145:     {
146:         if ($this->isCharMode($chr, Data::ENC_ASCII_NUM)) {
147:             $numch[Data::ENC_ASCII] += (1 / 2);
148:         } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
149:             $numch[Data::ENC_ASCII] = ceil($numch[Data::ENC_ASCII]);
150:             $numch[Data::ENC_ASCII] += 2;
151:         } else {
152:             $numch[Data::ENC_ASCII] = ceil($numch[Data::ENC_ASCII]);
153:             $numch[Data::ENC_ASCII] += 1;
154:         }
155:     }
156: 
157:     /**
158:      * Step M
159:      *
160:      * @param int $chr
161:      * @param int $numch
162:      */
163:     protected function stepM($chr, &$numch)
164:     {
165:         if ($this->isCharMode($chr, Data::ENC_C40)) {
166:             $numch[Data::ENC_C40] += (2 / 3);
167:         } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
168:             $numch[Data::ENC_C40] += (8 / 3);
169:         } else {
170:             $numch[Data::ENC_C40] += (4 / 3);
171:         }
172:     }
173: 
174:     /**
175:      * Step N
176:      *
177:      * @param int $chr
178:      * @param int $numch
179:      */
180:     protected function stepN($chr, &$numch)
181:     {
182:         if ($this->isCharMode($chr, Data::ENC_TXT)) {
183:             $numch[Data::ENC_TXT] += (2 / 3);
184:         } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
185:             $numch[Data::ENC_TXT] += (8 / 3);
186:         } else {
187:             $numch[Data::ENC_TXT] += (4 / 3);
188:         }
189:     }
190: 
191:     /**
192:      * Step O
193:      *
194:      * @param int $chr
195:      * @param int $numch
196:      */
197:     protected function stepO($chr, &$numch)
198:     {
199:         if ($this->isCharMode($chr, Data::ENC_X12) || $this->isCharMode($chr, Data::ENC_C40)) {
200:             $numch[Data::ENC_X12] += (2 / 3);
201:         } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
202:             $numch[Data::ENC_X12] += (13 / 3);
203:         } else {
204:             $numch[Data::ENC_X12] += (10 / 3);
205:         }
206:     }
207: 
208:     /**
209:      * Step P
210:      *
211:      * @param int $chr
212:      * @param int $numch
213:      */
214:     protected function stepP($chr, &$numch)
215:     {
216:         if ($this->isCharMode($chr, Data::ENC_EDF)) {
217:             $numch[Data::ENC_EDF] += (3 / 4);
218:         } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
219:             $numch[Data::ENC_EDF] += (17 / 4);
220:         } else {
221:             $numch[Data::ENC_EDF] += (13 / 4);
222:         }
223:     }
224: 
225:     /**
226:      * Step Q
227:      *
228:      * @param int $chr
229:      * @param int $numch
230:      */
231:     protected function stepQ($chr, &$numch)
232:     {
233:         if ($this->isCharMode($chr, Data::ENC_BASE256)) {
234:             $numch[Data::ENC_BASE256] += 4;
235:         } else {
236:             $numch[Data::ENC_BASE256] += 1;
237:         }
238:     }
239: 
240:     /**
241:      * Step R-f
242:      *
243:      * @param int    $numch
244:      * @param int    $pos
245:      * @param int    $data_length
246:      * @param int    $charscount
247:      * @param string $data
248:      *
249:      * @return int   Encoding mode
250:      */
251:     protected function stepRf($numch, $pos, $data_length, $charscount, $data)
252:     {
253:         if (($numch[Data::ENC_C40] + 1) < min(
254:             $numch[Data::ENC_ASCII],
255:             $numch[Data::ENC_TXT],
256:             $numch[Data::ENC_EDF],
257:             $numch[Data::ENC_BASE256]
258:         )) {
259:             if ($numch[Data::ENC_C40] < $numch[Data::ENC_X12]) {
260:                 return Data::ENC_C40;
261:             }
262:             if ($numch[Data::ENC_C40] == $numch[Data::ENC_X12]) {
263:                 $ker = ($pos + $charscount + 1);
264:                 while ($ker < $data_length) {
265:                     $tmpchr = ord($data[$ker]);
266:                     if ($this->isCharMode($tmpchr, Data::ENC_X12)) {
267:                         return Data::ENC_X12;
268:                     } elseif (!($this->isCharMode($tmpchr, Data::ENC_X12)
269:                         || $this->isCharMode($tmpchr, Data::ENC_C40))) {
270:                         break;
271:                     }
272:                     ++$ker;
273:                 }
274:                 return Data::ENC_C40;
275:             }
276:         }
277:         return null;
278:     }
279: 
280:     /**
281:      * Step R
282:      *
283:      * @param int    $numch
284:      * @param int    $pos
285:      * @param int    $data_length
286:      * @param int    $charscount
287:      * @param string $data
288:      */
289:     protected function stepR($numch, $pos, $data_length, $charscount, $data)
290:     {
291:         if (($numch[Data::ENC_ASCII] + 1) <= min(
292:             $numch[Data::ENC_C40],
293:             $numch[Data::ENC_TXT],
294:             $numch[Data::ENC_X12],
295:             $numch[Data::ENC_EDF],
296:             $numch[Data::ENC_BASE256]
297:         )) {
298:             return Data::ENC_ASCII;
299:         }
300:         if ((($numch[Data::ENC_BASE256] + 1) <= $numch[Data::ENC_ASCII])
301:             || (($numch[Data::ENC_BASE256] + 1) < min(
302:                 $numch[Data::ENC_C40],
303:                 $numch[Data::ENC_TXT],
304:                 $numch[Data::ENC_X12],
305:                 $numch[Data::ENC_EDF]
306:             ))) {
307:             return Data::ENC_BASE256;
308:         }
309:         if (($numch[Data::ENC_EDF] + 1) < min(
310:             $numch[Data::ENC_ASCII],
311:             $numch[Data::ENC_C40],
312:             $numch[Data::ENC_TXT],
313:             $numch[Data::ENC_X12],
314:             $numch[Data::ENC_BASE256]
315:         )) {
316:             return Data::ENC_EDF;
317:         }
318:         if (($numch[Data::ENC_TXT] + 1) < min(
319:             $numch[Data::ENC_ASCII],
320:             $numch[Data::ENC_C40],
321:             $numch[Data::ENC_X12],
322:             $numch[Data::ENC_EDF],
323:             $numch[Data::ENC_BASE256]
324:         )) {
325:             return Data::ENC_TXT;
326:         }
327:         if (($numch[Data::ENC_X12] + 1) < min(
328:             $numch[Data::ENC_ASCII],
329:             $numch[Data::ENC_C40],
330:             $numch[Data::ENC_TXT],
331:             $numch[Data::ENC_EDF],
332:             $numch[Data::ENC_BASE256]
333:         )) {
334:             return Data::ENC_X12;
335:         }
336:         return $this->stepRf($numch, $pos, $data_length, $charscount, $data);
337:     }
338: }
339: 
 

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