source-class-Com.Tecnick.Barcode.Type.Square.QrCode.EncodingMode

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:  * EncodingMode.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\QrCode;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: use \Com\Tecnick\Barcode\Type\Square\QrCode\Data;
 20: 
 21: /**
 22:  * Com\Tecnick\Barcode\Type\Square\QrCode\EncodingMode
 23:  *
 24:  * @since       2015-02-21
 25:  * @category    Library
 26:  * @package     Barcode
 27:  * @author      Nicola Asuni <info@tecnick.com>
 28:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 29:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 30:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 31:  */
 32: abstract class EncodingMode extends \Com\Tecnick\Barcode\Type\Square\QrCode\InputItem
 33: {
 34:     /**
 35:      * Get the encoding mode to use
 36:      *
 37:      * @param string $data Data
 38:      * @param int    $pos  Position
 39:      *
 40:      * @return int mode
 41:      */
 42:     public function getEncodingMode($data, $pos)
 43:     {
 44:         if (!isset($data[$pos])) {
 45:             return Data::$encodingModes['NL'];
 46:         }
 47:         if ($this->isDigitAt($data, $pos)) {
 48:             return Data::$encodingModes['NM'];
 49:         }
 50:         if ($this->isAlphanumericAt($data, $pos)) {
 51:             return Data::$encodingModes['AN'];
 52:         }
 53:         return $this->getEncodingModeKj($data, $pos);
 54:     }
 55: 
 56:     /**
 57:      * Get the encoding mode for KJ or 8B
 58:      *
 59:      * @param string $data Data
 60:      * @param int    $pos  Position
 61:      *
 62:      * @return int mode
 63:      */
 64:     protected function getEncodingModeKj($data, $pos)
 65:     {
 66:         if (($this->hint == Data::$encodingModes['KJ']) && isset($data[($pos + 1)])) {
 67:             $word = ((ord($data[$pos]) << 8) | ord($data[($pos + 1)]));
 68:             if ((($word >= 0x8140) && ($word <= 0x9ffc)) || (($word >= 0xe040) && ($word <= 0xebbf))) {
 69:                 return Data::$encodingModes['KJ'];
 70:             }
 71:         }
 72:         return Data::$encodingModes['8B'];
 73:     }
 74: 
 75:     /**
 76:      * Return true if the character at specified position is a number
 77:      *
 78:      * @param string $str Data
 79:      * @param int    $pos Character position
 80:      *
 81:      * @return boolean
 82:      */
 83:     public function isDigitAt($str, $pos)
 84:     {
 85:         if (!isset($str[$pos])) {
 86:             return false;
 87:         }
 88:         return ((ord($str[$pos]) >= ord('0')) && (ord($str[$pos]) <= ord('9')));
 89:     }
 90: 
 91:     /**
 92:      * Return true if the character at specified position is an alphanumeric character
 93:      *
 94:      * @param string $str Data
 95:      * @param int    $pos Character position
 96:      *
 97:      * @return boolean
 98:      */
 99:     public function isAlphanumericAt($str, $pos)
100:     {
101:         if (!isset($str[$pos])) {
102:             return false;
103:         }
104:         return ($this->lookAnTable(ord($str[$pos])) >= 0);
105:     }
106: 
107:     /**
108:      * Look up the alphabet-numeric conversion table (see JIS X0510:2004, pp.19)
109:      *
110:      * @param int $chr Character value
111:      *
112:      * @return value
113:      */
114:     public function lookAnTable($chr)
115:     {
116:         return (($chr > 127) ? -1 : Data::$anTable[$chr]);
117:     }
118: 
119:     /**
120:      * Return the size of length indicator for the mode and version
121:      *
122:      * @param int $mode Encoding mode
123:      * @param int $version Version
124:      *
125:      * @return int the size of the appropriate length indicator (bits).
126:      */
127:     public function getLengthIndicator($mode)
128:     {
129:         if ($mode == Data::$encodingModes['ST']) {
130:             return 0;
131:         }
132:         if ($this->version <= 9) {
133:             $len = 0;
134:         } elseif ($this->version <= 26) {
135:             $len = 1;
136:         } else {
137:             $len = 2;
138:         }
139:         return Data::$lengthTableBits[$mode][$len];
140:     }
141: 
142:     /**
143:      * Append one bitstream to another
144:      *
145:      * @param array $bitstream Original bitstream
146:      * @param array $append    Bitstream to append
147:      *
148:      * @return array bitstream
149:      */
150:     protected function appendBitstream($bitstream, $append)
151:     {
152:         if ((!is_array($append)) || (count($append) == 0)) {
153:             return $bitstream;
154:         }
155:         if (count($bitstream) == 0) {
156:             return $append;
157:         }
158:         return array_values(array_merge($bitstream, $append));
159:     }
160: 
161:     /**
162:      * Append one bitstream created from number to another
163:      *
164:      * @param array $bitstream Original bitstream
165:      * @param int   $bits      Number of bits
166:      * @param int   $num       Number
167:      *
168:      * @return array bitstream
169:      */
170:     protected function appendNum($bitstream, $bits, $num)
171:     {
172:         if ($bits == 0) {
173:             return 0;
174:         }
175:         return $this->appendBitstream($bitstream, $this->newFromNum($bits, $num));
176:     }
177: 
178:     /**
179:      * Append one bitstream created from bytes to another
180:      *
181:      * @param array $bitstream Original bitstream
182:      * @param int   $size      Size
183:      * @param array $data      Bytes
184:      *
185:      * @return array bitstream
186:      */
187:     protected function appendBytes($bitstream, $size, $data)
188:     {
189:         if ($size == 0) {
190:             return 0;
191:         }
192:         return $this->appendBitstream($bitstream, $this->newFromBytes($size, $data));
193:     }
194: 
195:     /**
196:      * Return new bitstream from number
197:      *
198:      * @param int $bits Number of bits
199:      * @param int $num  Number
200:      *
201:      * @return array bitstream
202:      */
203:     protected function newFromNum($bits, $num)
204:     {
205:         $bstream = $this->allocate($bits);
206:         $mask = 1 << ($bits - 1);
207:         for ($idx = 0; $idx < $bits; ++$idx) {
208:             if ($num & $mask) {
209:                 $bstream[$idx] = 1;
210:             } else {
211:                 $bstream[$idx] = 0;
212:             }
213:             $mask = $mask >> 1;
214:         }
215:         return $bstream;
216:     }
217: 
218:     /**
219:      * Return new bitstream from bytes
220:      *
221:      * @param int   $size Size
222:      * @param array $data Bytes
223:      *
224:      * @return array bitstream
225:      */
226:     protected function newFromBytes($size, $data)
227:     {
228:         $bstream = $this->allocate($size * 8);
229:         $pval = 0;
230:         for ($idx = 0; $idx < $size; ++$idx) {
231:             $mask = 0x80;
232:             for ($jdx = 0; $jdx < 8; ++$jdx) {
233:                 if ($data[$idx] & $mask) {
234:                     $bstream[$pval] = 1;
235:                 } else {
236:                     $bstream[$pval] = 0;
237:                 }
238:                 $pval++;
239:                 $mask = $mask >> 1;
240:             }
241:         }
242:         return $bstream;
243:     }
244: 
245:     /**
246:      * Return an array with zeros
247:      *
248:      * @param int $setLength Array size
249:      *
250:      * @return array
251:      */
252:     protected function allocate($setLength)
253:     {
254:         return array_fill(0, $setLength, 0);
255:     }
256: }
257: 
 

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