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

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:  * EncodeTxt.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\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\Encodetxt
 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   2010-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 EncodeTxt extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\Steps
 36: {
 37:     /**
 38:      * Encode TXTC40 shift
 39:      *
 40:      * @param int $chr
 41:      * @param int $enc
 42:      * @param int $temp_cw
 43:      * @param int $ptr
 44:      */
 45:     public function encodeTXTC40shift(&$chr, &$enc, &$temp_cw, &$ptr)
 46:     {
 47:         if (isset(Data::$chset['SH1'][$chr])) {
 48:             $temp_cw[] = 0; // shift 1
 49:             $shiftset = Data::$chset['SH1'];
 50:         } elseif (isset($chr, Data::$chset['SH2'][$chr])) {
 51:             $temp_cw[] = 1; // shift 2
 52:             $shiftset = Data::$chset['SH2'];
 53:         } elseif (($enc == Data::ENC_C40) && isset(Data::$chset['S3C'][$chr])) {
 54:             $temp_cw[] = 2; // shift 3
 55:             $shiftset = Data::$chset['S3C'];
 56:         } elseif (($enc == Data::ENC_TXT) && isset(Data::$chset['S3T'][$chr])) {
 57:             $temp_cw[] = 2; // shift 3
 58:             $shiftset = Data::$chset['S3T'];
 59:         } else {
 60:             throw new BarcodeException('Error');
 61:         }
 62:         $temp_cw[] = $shiftset[$chr];
 63:         $ptr += 2;
 64:     }
 65: 
 66:     /**
 67:      * Encode TXTC40
 68:      *
 69:      * @param string $data
 70:      * @param int    $enc
 71:      * @param int    $temp_cw
 72:      * @param int    $ptr
 73:      * @param int    $epos
 74:      * @param array  $charset
 75:      *
 76:      * @return int   Curent character code
 77:      */
 78:     public function encodeTXTC40(&$data, &$enc, &$temp_cw, &$ptr, &$epos, &$charset)
 79:     {
 80:         // 2. process the next character in C40 encodation.
 81:         $chr = ord($data[$epos]);
 82:         ++$epos;
 83:         // check for extended character
 84:         if ($chr & 0x80) {
 85:             if ($enc == Data::ENC_X12) {
 86:                 throw new BarcodeException('TXTC40 Error');
 87:             }
 88:             $chr = ($chr & 0x7f);
 89:             $temp_cw[] = 1; // shift 2
 90:             $temp_cw[] = 30; // upper shift
 91:             $ptr += 2;
 92:         }
 93:         if (isset($charset[$chr])) {
 94:             $temp_cw[] = $charset[$chr];
 95:             ++$ptr;
 96:         } else {
 97:             $this->encodeTXTC40shift($chr, $enc, $temp_cw, $ptr);
 98:         }
 99:         return $chr;
100:     }
101: 
102:     /**
103:      * Encode TXTC40 last
104:      *
105:      * @param int $chr
106:      * @param int    $cdw
107:      * @param int    $cdw_num
108:      * @param int    $enc
109:      * @param int    $temp_cw
110:      * @param int    $ptr
111:      * @param int    $epos
112:      */
113:     public function encodeTXTC40last($chr, &$cdw, &$cdw_num, &$enc, &$temp_cw, &$ptr, &$epos)
114:     {
115:         // get remaining number of data symbols
116:         $cdwr = ($this->getMaxDataCodewords($cdw_num) - $cdw_num);
117:         if (($cdwr == 1) && ($ptr == 1)) {
118:             // d. If one symbol character remains and one
119:             // C40 value (data character) remains to be encoded
120:             $ch1 = array_shift($temp_cw);
121:             --$ptr;
122:             $cdw[] = ($chr + 1);
123:             ++$cdw_num;
124:             $pos = $epos;
125:             $enc = Data::ENC_ASCII;
126:             $this->last_enc = $enc;
127:         } elseif (($cdwr == 2) && ($ptr == 1)) {
128:             // c. If two symbol characters remain and only one
129:             // C40 value (data character) remains to be encoded
130:             $ch1 = array_shift($temp_cw);
131:             --$ptr;
132:             $cdw[] = 254;
133:             $cdw[] = ($chr + 1);
134:             $cdw_num += 2;
135:             $pos = $epos;
136:             $enc = Data::ENC_ASCII;
137:             $this->last_enc = $enc;
138:         } elseif (($cdwr == 2) && ($ptr == 2)) {
139:             // b. If two symbol characters remain and two C40 values remain to be encoded
140:             $ch1 = array_shift($temp_cw);
141:             $ch2 = array_shift($temp_cw);
142:             $ptr -= 2;
143:             $tmp = ((1600 * $ch1) + (40 * $ch2) + 1);
144:             $cdw[] = ($tmp >> 8);
145:             $cdw[] = ($tmp % 256);
146:             $cdw_num += 2;
147:             $pos = $epos;
148:             $enc = Data::ENC_ASCII;
149:             $this->last_enc = $enc;
150:         } else {
151:             // switch to ASCII encoding
152:             if ($enc != Data::ENC_ASCII) {
153:                 $enc = Data::ENC_ASCII;
154:                 $this->last_enc = $enc;
155:                 $cdw[] = $this->getSwitchEncodingCodeword($enc);
156:                 ++$cdw_num;
157:                 $pos = ($epos - $ptr);
158:             }
159:         }
160:     }
161: 
162:     /**
163:      * Encode TXT
164:      *
165:      * @param int    $cdw
166:      * @param int    $cdw_num
167:      * @param int    $pos
168:      * @param int    $data_length
169:      * @param string $data
170:      * @param int    $enc
171:      */
172:     public function encodeTXT(&$cdw, &$cdw_num, &$pos, &$data_length, &$data, &$enc)
173:     {
174:         $temp_cw = array();
175:         $ptr = 0;
176:         $epos = $pos;
177:         // get charset ID
178:         $set_id = Data::$chset_id[$enc];
179:         // get basic charset for current encoding
180:         $charset = Data::$chset[$set_id];
181:         do {
182:             $chr = $this->encodeTXTC40($data, $enc, $temp_cw, $ptr, $epos, $charset);
183:             if ($ptr >= 3) {
184:                 $ch1 = array_shift($temp_cw);
185:                 $ch2 = array_shift($temp_cw);
186:                 $ch3 = array_shift($temp_cw);
187:                 $ptr -= 3;
188:                 $tmp = ((1600 * $ch1) + (40 * $ch2) + $ch3 + 1);
189:                 $cdw[] = ($tmp >> 8);
190:                 $cdw[] = ($tmp % 256);
191:                 $cdw_num += 2;
192:                 $pos = $epos;
193:                 // 1. If the C40 encoding is at the point of starting a new double symbol character and
194:                 // if the look-ahead test (starting at step J) indicates another mode, switch to that mode.
195:                 $newenc = $this->lookAheadTest($data, $pos, $enc);
196:                 if ($newenc != $enc) {
197:                     // switch to new encoding
198:                     $enc = $newenc;
199:                     if ($enc != Data::ENC_ASCII) {
200:                         // set unlatch character
201:                         $cdw[] = $this->getSwitchEncodingCodeword(Data::ENC_ASCII);
202:                         ++$cdw_num;
203:                     }
204:                     $cdw[] = $this->getSwitchEncodingCodeword($enc);
205:                     ++$cdw_num;
206:                     $pos -= $ptr;
207:                     $ptr = 0;
208:                     break;
209:                 }
210:             }
211:         } while (($ptr > 0) && ($epos < $data_length));
212:         // process last data (if any)
213:         if ($ptr > 0) {
214:             $this->encodeTXTC40last($chr, $cdw, $cdw_num, $enc, $temp_cw, $ptr, $epos);
215:         }
216:     }
217: }
218: 
 

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