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

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:  * Datamatrix.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;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: use \Com\Tecnick\Barcode\Type\Square\Datamatrix\Data;
 20: use \Com\Tecnick\Barcode\Type\Square\Datamatrix\Encode;
 21: 
 22: /**
 23:  * Com\Tecnick\Barcode\Type\Square\Datamatrix
 24:  *
 25:  * Datamatrix Barcode type class
 26:  * DATAMATRIX (ISO/IEC 16022)
 27:  *
 28:  * @since       2015-02-21
 29:  * @category    Library
 30:  * @package     Barcode
 31:  * @author      Nicola Asuni <info@tecnick.com>
 32:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 33:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 34:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 35:  */
 36: class Datamatrix extends \Com\Tecnick\Barcode\Type\Square
 37: {
 38:     /**
 39:      * Barcode format
 40:      *
 41:      * @var string
 42:      */
 43:     protected $format = 'DATAMATRIX';
 44: 
 45:     /**
 46:      * Array of codewords.
 47:      *
 48:      * @var array
 49:      */
 50:     protected $cdw = array();
 51: 
 52:     /**
 53:      * Binary grid
 54:      *
 55:      * @var array
 56:      */
 57:     protected $grid = array();
 58: 
 59:     /**
 60:      * Datamatrix Encoding object
 61:      *
 62:      * @var \Com\Tecnick\Barcode\Type\Square\Datamatrix\Encode
 63:      */
 64:     protected $dmx;
 65: 
 66:     /**
 67:      * Add padding codewords
 68:      *
 69:      * @param int $size Max barcode size in codewords
 70:      * @param int $ncw  Number of codewords
 71:      *
 72:      * @throws BarcodeException in case of error
 73:      */
 74:     protected function addPadding($size, $ncw)
 75:     {
 76:         // add padding
 77:         if ((($size - $ncw) > 1) && ($this->cdw[($ncw - 1)] != 254)) {
 78:             if ($this->dmx->last_enc == Data::ENC_EDF) {
 79:                 // switch to ASCII encoding
 80:                 $this->cdw[] = 124;
 81:             } elseif (($this->dmx->last_enc != Data::ENC_ASCII) && ($this->dmx->last_enc != Data::ENC_BASE256)) {
 82:                 // switch to ASCII encoding
 83:                 $this->cdw[] = 254;
 84:             }
 85:         }
 86:         if ($size > $ncw) {
 87:             // add first pad
 88:             $this->cdw[] = 129;
 89:             // add remaining pads
 90:             for ($i = $ncw; $i < $size; ++$i) {
 91:                 $this->cdw[] = $this->dmx->get253StateCodeword(129, $i);
 92:             }
 93:         }
 94:     }
 95: 
 96:     /**
 97:      * Get the codewords
 98:      *
 99:      * @return array params
100:      *
101:      * @throws BarcodeException in case of error
102:      */
103:     protected function getCodewords()
104:     {
105:         if (strlen((string)$this->code) == 0) {
106:             throw new BarcodeException('Empty input');
107:         }
108: 
109:         // get data codewords
110:         $this->cdw = $this->getHighLevelEncoding($this->code);
111:         
112:         // number of data codewords
113:         $ncw = count($this->cdw);
114:         
115:         // check size
116:         if ($ncw > 1560) {
117:             throw new BarcodeException('the input is too large to fit the barcode');
118:         }
119:         
120:         // get minimum required matrix size.
121:         foreach (Data::$symbattr as $params) {
122:             if ($params[11] >= $ncw) {
123:                 break;
124:             }
125:         }
126:         if ($params[11] > $ncw) {
127:             $this->addPadding($params[11], $ncw);
128:         }
129: 
130:         $errorCorrection = new \Com\Tecnick\Barcode\Type\Square\Datamatrix\ErrorCorrection;
131:         $this->cdw = $errorCorrection->getErrorCorrection($this->cdw, $params[13], $params[14], $params[15]);
132: 
133:         return $params;
134:     }
135: 
136:     /**
137:      * Set the grid
138:      *
139:      * @param int $idx
140:      * @param array $places
141:      * @param int $row
142:      * @param int $col
143:      * @param int $rdx
144:      * @param int $cdx
145:      * @param int $rdri
146:      * @param int $rdci
147:      */
148:     protected function setGrid(&$idx, &$places, &$row, &$col, &$rdx, &$cdx, &$rdri, &$rdci)
149:     {
150:         // braw bits by case
151:         if ($rdx == 0) {
152:             // top finder pattern
153:             $this->grid[$row][$col] = intval(($cdx % 2) == 0);
154:         } elseif ($rdx == $rdri) {
155:             // bottom finder pattern
156:             $this->grid[$row][$col] = 1;
157:         } elseif ($cdx == 0) {
158:             // left finder pattern
159:             $this->grid[$row][$col] = 1;
160:         } elseif ($cdx == $rdci) {
161:             // right finder pattern
162:             $this->grid[$row][$col] = intval(($rdx % 2) > 0);
163:         } else {
164:             // data bit
165:             if ($places[$idx] < 2) {
166:                 $this->grid[$row][$col] = $places[$idx];
167:             } else {
168:                 // codeword ID
169:                 $cdw_id = (floor($places[$idx] / 10) - 1);
170:                 // codeword BIT mask
171:                 $cdw_bit = pow(2, (8 - ($places[$idx] % 10)));
172:                 $this->grid[$row][$col] = (($this->cdw[$cdw_id] & $cdw_bit) == 0) ? 0 : 1;
173:             }
174:             ++$idx;
175:         }
176:     }
177: 
178:     /**
179:      * Get high level encoding using the minimum symbol data characters for ECC 200
180:      *
181:      * @param $data (string) data to encode
182:      *
183:      * @return array of codewords
184:      */
185:     protected function getHighLevelEncoding($data)
186:     {
187:         // STEP A. Start in ASCII encodation.
188:         $enc = Data::ENC_ASCII; // current encoding mode
189:         $pos = 0; // current position
190:         $cdw = array(); // array of codewords to be returned
191:         $cdw_num = 0; // number of data codewords
192:         $data_length = strlen($data); // number of chars
193:         while ($pos < $data_length) {
194:             // set last used encoding
195:             $this->dmx->last_enc = $enc;
196:             switch ($enc) {
197:                 case Data::ENC_ASCII:
198:                     // STEP B. While in ASCII encodation
199:                     $this->dmx->encodeASCII($cdw, $cdw_num, $pos, $data_length, $data, $enc);
200:                     break;
201:                 case Data::ENC_C40:
202:                     // Upper-case alphanumeric
203:                 case Data::ENC_TXT:
204:                     // Lower-case alphanumeric
205:                 case Data::ENC_X12:
206:                     // ANSI X12
207:                     $this->dmx->encodeTXT($cdw, $cdw_num, $pos, $data_length, $data, $enc);
208:                     break;
209:                 case Data::ENC_EDF:
210:                     // F. While in EDIFACT (EDF) encodation
211:                     $this->dmx->encodeEDF($cdw, $cdw_num, $pos, $data_length, $field_length, $data, $enc);
212:                     break;
213:                 case Data::ENC_BASE256:
214:                     // G. While in Base 256 (B256) encodation
215:                     $this->dmx->encodeBase256($cdw, $cdw_num, $pos, $data_length, $field_length, $data, $enc);
216:                     break;
217:             }
218:         }
219:         return $cdw;
220:     }
221: 
222:     /**
223:      * Get the bars array
224:      *
225:      * @throws BarcodeException in case of error
226:      */
227:     protected function setBars()
228:     {
229:         $this->dmx = new Encode;
230:         $params = $this->getCodewords();
231:         // initialize empty arrays
232:         $this->grid = array_fill(0, ($params[2] * $params[3]), 0);
233:         // get placement map
234:         $places = $this->dmx->getPlacementMap($params[2], $params[3]);
235:         // fill the grid with data
236:         $this->grid = array();
237:         $idx = 0;
238:         // region data row max index
239:         $rdri = ($params[4] - 1);
240:         // region data column max index
241:         $rdci = ($params[5] - 1);
242:         // for each vertical region
243:         for ($vr = 0; $vr < $params[9]; ++$vr) {
244:             // for each row on region
245:             for ($rdx = 0; $rdx < $params[4]; ++$rdx) {
246:                 // get row
247:                 $row = (($vr * $params[4]) + $rdx);
248:                 // for each horizontal region
249:                 for ($hr = 0; $hr < $params[8]; ++$hr) {
250:                     // for each column on region
251:                     for ($cdx = 0; $cdx < $params[5]; ++$cdx) {
252:                         // get column
253:                         $col = (($hr * $params[5]) + $cdx);
254:                         $this->setGrid($idx, $places, $row, $col, $rdx, $cdx, $rdri, $rdci);
255:                     }
256:                 }
257:             }
258:         }
259:         $this->processBinarySequence($this->grid);
260:     }
261: }
262: 
 

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