source-class-Com.Tecnick.Barcode.Type.Linear.CodeThreeNineExtCheck

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:  * CodeThreeNineExtCheck.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;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: 
 20: /**
 21:  * Com\Tecnick\Barcode\Type\Linear\CodeThreeNineExtCheck
 22:  *
 23:  * CodeThreeNineExtCheck Barcode type class
 24:  * CODE 39 EXTENDED + CHECKSUM
 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: class CodeThreeNineExtCheck extends \Com\Tecnick\Barcode\Type\Linear
 35: {
 36:     /**
 37:      * Barcode format
 38:      *
 39:      * @var string
 40:      */
 41:     protected $format = 'C39E+';
 42: 
 43:     /**
 44:      * Map characters to barcodes
 45:      *
 46:      * @var array
 47:      */
 48:     protected $chbar = array(
 49:         '0' => '111331311',
 50:         '1' => '311311113',
 51:         '2' => '113311113',
 52:         '3' => '313311111',
 53:         '4' => '111331113',
 54:         '5' => '311331111',
 55:         '6' => '113331111',
 56:         '7' => '111311313',
 57:         '8' => '311311311',
 58:         '9' => '113311311',
 59:         'A' => '311113113',
 60:         'B' => '113113113',
 61:         'C' => '313113111',
 62:         'D' => '111133113',
 63:         'E' => '311133111',
 64:         'F' => '113133111',
 65:         'G' => '111113313',
 66:         'H' => '311113311',
 67:         'I' => '113113311',
 68:         'J' => '111133311',
 69:         'K' => '311111133',
 70:         'L' => '113111133',
 71:         'M' => '313111131',
 72:         'N' => '111131133',
 73:         'O' => '311131131',
 74:         'P' => '113131131',
 75:         'Q' => '111111333',
 76:         'R' => '311111331',
 77:         'S' => '113111331',
 78:         'T' => '111131331',
 79:         'U' => '331111113',
 80:         'V' => '133111113',
 81:         'W' => '333111111',
 82:         'X' => '131131113',
 83:         'Y' => '331131111',
 84:         'Z' => '133131111',
 85:         '-' => '131111313',
 86:         '.' => '331111311',
 87:         ' ' => '133111311',
 88:         '$' => '131313111',
 89:         '/' => '131311131',
 90:         '+' => '131113131',
 91:         '%' => '111313131',
 92:         '*' => '131131311'
 93:     );
 94: 
 95:     /**
 96:      * Map for extended characters
 97:      *
 98:      * @var array
 99:      */
100:     protected $extcodes = array(
101:         '%U', '$A', '$B', '$C',
102:         '$D', '$E', '$F', '$G',
103:         '$H', '$I', '$J', '$K',
104:         '$L', '$M', '$N', '$O',
105:         '$P', '$Q', '$R', '$S',
106:         '$T', '$U', '$V', '$W',
107:         '$X', '$Y', '$Z', '%A',
108:         '%B', '%C', '%D', '%E',
109:         ' ',  '/A', '/B', '/C',
110:         '/D', '/E', '/F', '/G',
111:         '/H', '/I', '/J', '/K',
112:         '/L', '-',  '.',  '/O',
113:         '0',  '1',  '2',  '3',
114:         '4',  '5',  '6',  '7',
115:         '8',  '9',  '/Z', '%F',
116:         '%G', '%H', '%I', '%J',
117:         '%V', 'A',  'B',  'C',
118:         'D',  'E',  'F',  'G',
119:         'H',  'I',  'J',  'K',
120:         'L',  'M',  'N',  'O',
121:         'P',  'Q',  'R',  'S',
122:         'T',  'U',  'V',  'W',
123:         'X',  'Y',  'Z',  '%K',
124:         '%L', '%M', '%N', '%O',
125:         '%W', '+A', '+B', '+C',
126:         '+D', '+E', '+F', '+G',
127:         '+H', '+I', '+J', '+K',
128:         '+L', '+M', '+N', '+O',
129:         '+P', '+Q', '+R', '+S',
130:         '+T', '+U', '+V', '+W',
131:         '+X', '+Y', '+Z', '%P',
132:         '%Q', '%R', '%S', '%T'
133:     );
134: 
135:     /**
136:      * Characters used for checksum
137:      *
138:      * @var array
139:      */
140:     protected $chksum = array(
141:         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
142:         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
143:         'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
144:         'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%'
145:     );
146: 
147:     /**
148:      * Encode a string to be used for CODE 39 Extended mode.
149:      *
150:      * @param string $code Code to extend
151:      *
152:      * @return encoded string.
153:      *
154:      * @throws BarcodeException in case of error
155:      */
156:     protected function getExtendCode($code)
157:     {
158:         $ext = '';
159:         $clen = strlen($code);
160:         for ($chr = 0; $chr < $clen; ++$chr) {
161:             $item = ord($code[$chr]);
162:             if ($item > 127) {
163:                 throw new BarcodeException('Invalid character: chr('.$item.')');
164:             }
165:             $ext .= $this->extcodes[$item];
166:         }
167:         return $ext;
168:     }
169: 
170:     /**
171:      * Calculate CODE 39 checksum (modulo 43).
172:      *
173:      * @param $code (string) code to represent.
174:      *
175:      * @return char checksum.
176:      */
177:     protected function getChecksum($code)
178:     {
179:         $sum = 0;
180:         $clen = strlen($code);
181:         for ($chr = 0; $chr < $clen; ++$chr) {
182:             $key = array_keys($this->chksum, $code[$chr]);
183:             $sum += $key[0];
184:         }
185:         $idx = ($sum % 43);
186:         return $this->chksum[$idx];
187:     }
188: 
189:     /**
190:      * Format code
191:      */
192:     protected function formatCode()
193:     {
194:         $code = $this->getExtendCode(strtoupper($this->code));
195:         $this->extcode = '*'.$code.$this->getChecksum($code).'*';
196:     }
197: 
198:     /**
199:      * Get the bars array
200:      *
201:      * @return array
202:      *
203:      * @throws BarcodeException in case of error
204:      */
205:     protected function setBars()
206:     {
207:         $this->ncols = 0;
208:         $this->nrows = 1;
209:         $this->bars = array();
210:         $this->formatCode();
211:         $clen = strlen($this->extcode);
212:         for ($chr = 0; $chr < $clen; ++$chr) {
213:             $char = $this->extcode[$chr];
214:             if (!isset($this->chbar[$char])) {
215:                 throw new BarcodeException('Invalid character: chr('.ord($char).')');
216:             }
217:             for ($pos = 0; $pos < 9; ++$pos) {
218:                 $bar_width = intval($this->chbar[$char][$pos]);
219:                 if ((($pos % 2) == 0) && ($bar_width > 0)) {
220:                     $this->bars[] = array($this->ncols, 0, $bar_width, 1);
221:                 }
222:                 $this->ncols += $bar_width;
223:             }
224:             // intercharacter gap
225:             $this->ncols += 1;
226:         }
227:         --$this->ncols;
228:     }
229: }
230: 
 

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