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

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:  * Encode.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\Encode
 23:  *
 24:  * Datamatrix Barcode type class
 25:  * DATAMATRIX (ISO/IEC 16022)
 26:  *
 27:  * @since       2015-02-21
 28:  * @category    Library
 29:  * @package     Barcode
 30:  * @author      Nicola Asuni <info@tecnick.com>
 31:  * @copyright   2015-2016 Nicola Asuni - Tecnick.com LTD
 32:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 33:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 34:  */
 35: class Encode extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\EncodeTxt
 36: {
 37:     /**
 38:      * Store last used encoding for data codewords.
 39:      *
 40:      * @var int
 41:      */
 42:     public $last_enc;
 43: 
 44:     /**
 45:      * Encode ASCII
 46:      *
 47:      * @param int    $cdw
 48:      * @param int    $cdw_num
 49:      * @param int    $pos
 50:      * @param int    $data_length
 51:      * @param string $data
 52:      * @param int    $enc
 53:      */
 54:     public function encodeASCII(&$cdw, &$cdw_num, &$pos, &$data_length, &$data, &$enc)
 55:     {
 56:         if (($data_length > 1)
 57:             && ($pos < ($data_length - 1))
 58:             && ($this->isCharMode(ord($data[$pos]), Data::ENC_ASCII_NUM)
 59:                 && $this->isCharMode(ord($data[$pos + 1]), Data::ENC_ASCII_NUM)
 60:             )
 61:         ) {
 62:             // 1. If the next data sequence is at least 2 consecutive digits,
 63:             // encode the next two digits as a double digit in ASCII mode.
 64:             $cdw[] = (intval(substr($data, $pos, 2)) + 130);
 65:             ++$cdw_num;
 66:             $pos += 2;
 67:         } else {
 68:             // 2. If the look-ahead test (starting at step J) indicates another mode, switch to that mode.
 69:             $newenc = $this->lookAheadTest($data, $pos, $enc);
 70:             if ($newenc != $enc) {
 71:                 // switch to new encoding
 72:                 $enc = $newenc;
 73:                 $cdw[] = $this->getSwitchEncodingCodeword($enc);
 74:                 ++$cdw_num;
 75:             } else {
 76:                 // get new byte
 77:                 $chr = ord($data[$pos]);
 78:                 ++$pos;
 79:                 if ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
 80:                     // 3. If the next data character is extended ASCII (greater than 127)
 81:                     // encode it in ASCII mode first using the Upper Shift (value 235) character.
 82:                     $cdw[] = 235;
 83:                     $cdw[] = ($chr - 127);
 84:                     $cdw_num += 2;
 85:                 } else {
 86:                     // 4. Otherwise process the next data character in ASCII encodation.
 87:                     $cdw[] = ($chr + 1);
 88:                     ++$cdw_num;
 89:                 }
 90:             }
 91:         }
 92:     }
 93: 
 94:     /**
 95:      * Encode EDF4
 96:      *
 97:      * @param int    $epos
 98:      * @param int    $cdw
 99:      * @param int    $cdw_num
100:      * @param int    $pos
101:      * @param int    $data_length
102:      * @param int    $field_length
103:      * @param int    $enc
104:      * @param array  $temp_cw
105:      *
106:      * @return boolean true to break the loop
107:      */
108:     public function encodeEDFfour($epos, &$cdw, &$cdw_num, &$pos, &$data_length, &$field_length, &$enc, &$temp_cw)
109:     {
110:         if (($epos == $data_length) && ($field_length < 3)) {
111:             $enc = Data::ENC_ASCII;
112:             $cdw[] = $this->getSwitchEncodingCodeword($enc);
113:             ++$cdw_num;
114:             return true;
115:         }
116:         if ($field_length < 4) {
117:             // set unlatch character
118:             $temp_cw[] = 0x1f;
119:             ++$field_length;
120:             // fill empty characters
121:             for ($i = $field_length; $i < 4; ++$i) {
122:                 $temp_cw[] = 0;
123:             }
124:             $enc = Data::ENC_ASCII;
125:             $this->last_enc = $enc;
126:         }
127:         // encodes four data characters in three codewords
128:         $tcw = (($temp_cw[0] & 0x3F) << 2) + (($temp_cw[1] & 0x30) >> 4);
129:         if ($tcw > 0) {
130:             $cdw[] = $tcw;
131:             $cdw_num++;
132:         }
133:         $tcw = (($temp_cw[1] & 0x0F) << 4) + (($temp_cw[2] & 0x3C) >> 2);
134:         if ($tcw > 0) {
135:             $cdw[] = $tcw;
136:             $cdw_num++;
137:         }
138:         $tcw = (($temp_cw[2] & 0x03) << 6) + ($temp_cw[3] & 0x3F);
139:         if ($tcw > 0) {
140:             $cdw[] = $tcw;
141:             $cdw_num++;
142:         }
143:         $temp_cw = array();
144:         $pos = $epos;
145:         $field_length = 0;
146:         if ($enc == Data::ENC_ASCII) {
147:             return true; // exit from EDIFACT mode
148:         }
149:         return false;
150:     }
151: 
152:     /**
153:      * Encode EDF
154:      *
155:      * @param int    $cdw
156:      * @param int    $cdw_num
157:      * @param int    $pos
158:      * @param int    $data_length
159:      * @param int    $field_length
160:      * @param string $data
161:      * @param int    $enc
162:      */
163:     public function encodeEDF(&$cdw, &$cdw_num, &$pos, &$data_length, &$field_length, &$data, &$enc)
164:     {
165:         // initialize temporary array with 0 length
166:         $temp_cw = array();
167:         $epos = $pos;
168:         $field_length = 0;
169:         do {
170:             // 2. process the next character in EDIFACT encodation.
171:             $chr = ord($data[$epos]);
172:             if ($this->isCharMode($chr, Data::ENC_EDF)) {
173:                 ++$epos;
174:                 $temp_cw[] = $chr;
175:                 ++$field_length;
176:             }
177:             if (($field_length == 4)
178:                 || ($epos == $data_length)
179:                 || !$this->isCharMode($chr, Data::ENC_EDF)
180:             ) {
181:                 if ($this->encodeEDFfour($epos, $cdw, $cdw_num, $pos, $data_length, $field_length, $enc, $temp_cw)) {
182:                     break;
183:                 }
184:             }
185:         } while ($epos < $data_length);
186:     }
187: 
188:     /**
189:      * Encode Base256
190:      *
191:      * @param int    $cdw
192:      * @param int    $cdw_num
193:      * @param int    $pos
194:      * @param int    $data_length
195:      * @param int    $field_length
196:      * @param string $data
197:      * @param int    $enc
198:      */
199:     public function encodeBase256(&$cdw, &$cdw_num, &$pos, &$data_length, &$field_length, &$data, &$enc)
200:     {
201:         // initialize temporary array with 0 length
202:         $temp_cw = array();
203:         $field_length = 0;
204:         while (($pos < $data_length) && ($field_length <= 1555)) {
205:             $newenc = $this->lookAheadTest($data, $pos, $enc);
206:             if ($newenc != $enc) {
207:                 // 1. If the look-ahead test (starting at step J)
208:                 // indicates another mode, switch to that mode.
209:                 $enc = $newenc;
210:                 break; // exit from B256 mode
211:             } else {
212:                 // 2. Otherwise, process the next character in Base 256 encodation.
213:                 $chr = ord($data[$pos]);
214:                 ++$pos;
215:                 $temp_cw[] = $chr;
216:                 ++$field_length;
217:             }
218:         }
219:         // set field length
220:         if ($field_length <= 249) {
221:             $cdw[] = $this->get255StateCodeword($field_length, ($cdw_num + 1));
222:             ++$cdw_num;
223:         } else {
224:             $cdw[] = $this->get255StateCodeword((floor($field_length / 250) + 249), ($cdw_num + 1));
225:             $cdw[] = $this->get255StateCodeword(($field_length % 250), ($cdw_num + 2));
226:             $cdw_num += 2;
227:         }
228:         if (!empty($temp_cw)) {
229:             // add B256 field
230:             foreach ($temp_cw as $chp => $cht) {
231:                 $cdw[] = $this->get255StateCodeword($cht, ($cdw_num + $chp + 1));
232:             }
233:         }
234:     }
235: }
236: 
 

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