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

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:  * Spec.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\Spec
 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: class Spec extends \Com\Tecnick\Barcode\Type\Square\QrCode\SpecRs
 33: {
 34:     /**
 35:      * Replace a value on the array at the specified position
 36:      *
 37:      * @param array $srctab
 38:      * @param int    $xpos       X position
 39:      * @param int    $ypos       Y position
 40:      * @param string $repl    Value to replace
 41:      * @param int    $replLen Length of the repl string
 42:      *
 43:      * @return array srctab
 44:      */
 45:     public function qrstrset($srctab, $xpos, $ypos, $repl, $replLen = false)
 46:     {
 47:         $srctab[$ypos] = substr_replace(
 48:             $srctab[$ypos],
 49:             ($replLen !== false) ? substr($repl, 0, $replLen) : $repl,
 50:             $xpos,
 51:             ($replLen !== false) ? $replLen : strlen($repl)
 52:         );
 53:         return $srctab;
 54:     }
 55: 
 56:     /**
 57:      * Return maximum data code length (bytes) for the version.
 58:      *
 59:      * @param int $version Version
 60:      * @param int $level   Error correction level
 61:      *
 62:      * @return int maximum size (bytes)
 63:      */
 64:     public function getDataLength($version, $level)
 65:     {
 66:         return (Data::$capacity[$version][Data::QRCAP_WORDS] - Data::$capacity[$version][Data::QRCAP_EC][$level]);
 67:     }
 68: 
 69:     /**
 70:      * Return maximum error correction code length (bytes) for the version.
 71:      *
 72:      * @param int $version Version
 73:      * @param int $level   Error correction level
 74:      *
 75:      * @return int ECC size (bytes)
 76:      */
 77:     public function getECCLength($version, $level)
 78:     {
 79:         return Data::$capacity[$version][Data::QRCAP_EC][$level];
 80:     }
 81: 
 82:     /**
 83:      * Return the width of the symbol for the version.
 84:      *
 85:      * @param int $version Version
 86:      *
 87:      * @return int width
 88:      */
 89:     public function getWidth($version)
 90:     {
 91:         return Data::$capacity[$version][Data::QRCAP_WIDTH];
 92:     }
 93: 
 94:     /**
 95:      * Return the numer of remainder bits.
 96:      *
 97:      * @param int $version Version
 98:      *
 99:      * @return int number of remainder bits
100:      */
101:     public function getRemainder($version)
102:     {
103:         return Data::$capacity[$version][Data::QRCAP_REMINDER];
104:     }
105: 
106:     /**
107:      * Return the maximum length for the mode and version.
108:      *
109:      * @param int $mode    Encoding mode
110:      * @param int $version Version
111:      *
112:      * @return int the maximum length (bytes)
113:      */
114:     public function maximumWords($mode, $version)
115:     {
116:         if ($mode == Data::$encodingModes['ST']) {
117:             return 3;
118:         }
119:         if ($version <= 9) {
120:             $lval = 0;
121:         } elseif ($version <= 26) {
122:             $lval = 1;
123:         } else {
124:             $lval = 2;
125:         }
126:         $bits = Data::$lengthTableBits[$mode][$lval];
127:         $words = (1 << $bits) - 1;
128:         if ($mode == Data::$encodingModes['KJ']) {
129:             $words *= 2; // the number of bytes is required
130:         }
131:         return $words;
132:     }
133: 
134:     /**
135:      * Return an array of ECC specification.
136:      *
137:      * @param int   $version Version
138:      * @param int   $level   Error correction level
139:      * @param array $spec    Array of ECC specification contains as following:
140:      *                       {# of type1 blocks, # of data code, # of ecc code, # of type2 blocks, # of data code}
141:      *
142:      * @return array spec
143:      */
144:     public function getEccSpec($version, $level, $spec)
145:     {
146:         if (count($spec) < 5) {
147:             $spec = array(0, 0, 0, 0, 0);
148:         }
149:         $bv1 = Data::$eccTable[$version][$level][0];
150:         $bv2 = Data::$eccTable[$version][$level][1];
151:         $data = $this->getDataLength($version, $level);
152:         $ecc = $this->getECCLength($version, $level);
153:         if ($bv2 == 0) {
154:             $spec[0] = $bv1;
155:             $spec[1] = (int)($data / $bv1);
156:             $spec[2] = (int)($ecc / $bv1);
157:             $spec[3] = 0;
158:             $spec[4] = 0;
159:         } else {
160:             $spec[0] = $bv1;
161:             $spec[1] = (int)($data / ($bv1 + $bv2));
162:             $spec[2] = (int)($ecc  / ($bv1 + $bv2));
163:             $spec[3] = $bv2;
164:             $spec[4] = $spec[1] + 1;
165:         }
166:         return $spec;
167:     }
168: 
169:     /**
170:      * Put an alignment marker.
171:      *
172:      * @param array $frame Frame
173:      * @param int   $pox   X center coordinate of the pattern
174:      * @param int   $poy   Y center coordinate of the pattern
175:      *
176:      * @return array frame
177:      */
178:     public function putAlignmentMarker($frame, $pox, $poy)
179:     {
180:         $finder = array(
181:             "\xa1\xa1\xa1\xa1\xa1",
182:             "\xa1\xa0\xa0\xa0\xa1",
183:             "\xa1\xa0\xa1\xa0\xa1",
184:             "\xa1\xa0\xa0\xa0\xa1",
185:             "\xa1\xa1\xa1\xa1\xa1"
186:         );
187:         $yStart = $poy - 2;
188:         $xStart = $pox - 2;
189:         for ($ydx = 0; $ydx < 5; ++$ydx) {
190:             $frame = $this->qrstrset($frame, $xStart, ($yStart + $ydx), $finder[$ydx]);
191:         }
192:         return $frame;
193:     }
194: 
195:     /**
196:      * Put an alignment pattern.
197:      *
198:      * @param int   $version Version
199:      * @param array $frame   Frame
200:      * @param int   $width   Width
201:      *
202:      * @return array frame
203:      */
204:     public function putAlignmentPattern($version, $frame, $width)
205:     {
206:         if ($version < 2) {
207:             return $frame;
208:         }
209:         $dval = Data::$alignmentPattern[$version][1] - Data::$alignmentPattern[$version][0];
210:         if ($dval < 0) {
211:             $wdt = 2;
212:         } else {
213:             $wdt = (int)(($width - Data::$alignmentPattern[$version][0]) / $dval + 2);
214:         }
215:         if ($wdt * $wdt - 3 == 1) {
216:             $psx = Data::$alignmentPattern[$version][0];
217:             $psy = Data::$alignmentPattern[$version][0];
218:             $frame = $this->putAlignmentMarker($frame, $psx, $psy);
219:             return $frame;
220:         }
221:         $cpx = Data::$alignmentPattern[$version][0];
222:         $wdo = $wdt - 1;
223:         for ($xpos = 1; $xpos < $wdo; ++$xpos) {
224:             $frame = $this->putAlignmentMarker($frame, 6, $cpx);
225:             $frame = $this->putAlignmentMarker($frame, $cpx, 6);
226:             $cpx += $dval;
227:         }
228:         $cpy = Data::$alignmentPattern[$version][0];
229:         for ($y=0; $y < $wdo; ++$y) {
230:             $cpx = Data::$alignmentPattern[$version][0];
231:             for ($xpos = 0; $xpos < $wdo; ++$xpos) {
232:                 $frame = $this->putAlignmentMarker($frame, $cpx, $cpy);
233:                 $cpx += $dval;
234:             }
235:             $cpy += $dval;
236:         }
237:         return $frame;
238:     }
239: 
240:     /**
241:      * Return BCH encoded version information pattern that is used for the symbol of version 7 or greater.
242:      * Use lower 18 bits.
243:      *
244:      * @param int $version Version
245:      *
246:      * @return BCH encoded version information pattern
247:      */
248:     public function getVersionPattern($version)
249:     {
250:         if (($version < 7) || ($version > Data::QRSPEC_VERSION_MAX)) {
251:             return 0;
252:         }
253:         return Data::$versionPattern[($version - 7)];
254:     }
255: 
256:     /**
257:      * Return BCH encoded format information pattern.
258:      *
259:      * @param array $maskNo Mask number
260:      * @param int   $level  Error correction level
261:      *
262:      * @return BCH encoded format information pattern
263:      */
264:     public function getFormatInfo($maskNo, $level)
265:     {
266:         if (($maskNo < 0)
267:             || ($maskNo > 7)
268:             || ($level < 0)
269:             || ($level > 3)
270:         ) {
271:             return 0;
272:         }
273:         return Data::$formatInfo[$level][$maskNo];
274:     }
275: 
276:     /**
277:      * Put a finder pattern.
278:      *
279:      * @param array $frame Frame
280:      * @param int   $pox   X center coordinate of the pattern
281:      * @param int   $poy   Y center coordinate of the pattern
282:      *
283:      * @return array frame
284:      */
285:     public function putFinderPattern($frame, $pox, $poy)
286:     {
287:         $finder = array(
288:             "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
289:             "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
290:             "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
291:             "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
292:             "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
293:             "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
294:             "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
295:         );
296:         for ($ypos = 0; $ypos < 7; ++$ypos) {
297:             $frame = $this->qrstrset($frame, $pox, ($poy + $ypos), $finder[$ypos]);
298:         }
299:         return $frame;
300:     }
301: }
302: 
 

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