PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / tecnickcom / tcpdf / tcpdf_barcodes_1d.php
matomo / app / vendor / tecnickcom / tcpdf Last commit date
config 2 years ago fonts 2 years ago include 1 year ago LICENSE.TXT 2 years ago README.md 2 years ago VERSION 1 year ago tcpdf.php 1 year ago tcpdf_autoconfig.php 2 years ago tcpdf_barcodes_1d.php 2 years ago tcpdf_barcodes_2d.php 2 years ago tcpdf_import.php 2 years ago tcpdf_parser.php 2 years ago
tcpdf_barcodes_1d.php
2272 lines
1 <?php
2
3 namespace {
4 //============================================================+
5 // File name : tcpdf_barcodes_1d.php
6 // Version : 1.0.027
7 // Begin : 2008-06-09
8 // Last Update : 2014-10-20
9 // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
10 // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
11 // -------------------------------------------------------------------
12 // Copyright (C) 2008-2014 Nicola Asuni - Tecnick.com LTD
13 //
14 // This file is part of TCPDF software library.
15 //
16 // TCPDF is free software: you can redistribute it and/or modify it
17 // under the terms of the GNU Lesser General Public License as
18 // published by the Free Software Foundation, either version 3 of the
19 // License, or (at your option) any later version.
20 //
21 // TCPDF is distributed in the hope that it will be useful, but
22 // WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 // See the GNU Lesser General Public License for more details.
25 //
26 // You should have received a copy of the GNU Lesser General Public License
27 // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
28 //
29 // See LICENSE.TXT file for more information.
30 // -------------------------------------------------------------------
31 //
32 // Description : PHP class to creates array representations for
33 // common 1D barcodes to be used with TCPDF.
34 //
35 //============================================================+
36 /**
37 * @file
38 * PHP class to creates array representations for common 1D barcodes to be used with TCPDF.
39 * @package com.tecnick.tcpdf
40 * @author Nicola Asuni
41 * @version 1.0.027
42 */
43 /**
44 * @class TCPDFBarcode
45 * PHP class to creates array representations for common 1D barcodes to be used with TCPDF (http://www.tcpdf.org).<br>
46 * @package com.tecnick.tcpdf
47 * @version 1.0.027
48 * @author Nicola Asuni
49 */
50 class TCPDFBarcode
51 {
52 /**
53 * Array representation of barcode.
54 * @protected
55 */
56 protected $barcode_array = array();
57 /**
58 * This is the class constructor.
59 * Return an array representations for common 1D barcodes:<ul>
60 * <li>$arrcode['code'] code to be printed on text label</li>
61 * <li>$arrcode['maxh'] max barcode height</li>
62 * <li>$arrcode['maxw'] max barcode width</li>
63 * <li>$arrcode['bcode'][$k] single bar or space in $k position</li>
64 * <li>$arrcode['bcode'][$k]['t'] bar type: true = bar, false = space.</li>
65 * <li>$arrcode['bcode'][$k]['w'] bar width in units.</li>
66 * <li>$arrcode['bcode'][$k]['h'] bar height in units.</li>
67 * <li>$arrcode['bcode'][$k]['p'] bar top position (0 = top, 1 = middle)</li></ul>
68 * @param string $code code to print
69 * @param string $type type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extension</li><li>EAN5 : 5-Digits UPC-Based Extension</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
70 * @public
71 */
72 public function __construct($code, $type)
73 {
74 $this->setBarcode($code, $type);
75 }
76 /**
77 * Return an array representations of barcode.
78 * @return array
79 * @public
80 */
81 public function getBarcodeArray()
82 {
83 return $this->barcode_array;
84 }
85 /**
86 * Send barcode as SVG image object to the standard output.
87 * @param int $w Minimum width of a single bar in user units.
88 * @param int $h Height of barcode in user units.
89 * @param string $color Foreground color (in SVG format) for bar elements (background is transparent).
90 * @public
91 */
92 public function getBarcodeSVG($w = 2, $h = 30, $color = 'black')
93 {
94 // send headers
95 $code = $this->getBarcodeSVGcode($w, $h, $color);
96 \header('Content-Type: application/svg+xml');
97 \header('Cache-Control: public, must-revalidate, max-age=0');
98 // HTTP/1.1
99 \header('Pragma: public');
100 \header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
101 // Date in the past
102 \header('Last-Modified: ' . \gmdate('D, d M Y H:i:s') . ' GMT');
103 \header('Content-Disposition: inline; filename="' . \md5($code) . '.svg";');
104 //header('Content-Length: '.strlen($code));
105 echo $code;
106 }
107 /**
108 * Return a SVG string representation of barcode.
109 * @param int $w Minimum width of a single bar in user units.
110 * @param int $h Height of barcode in user units.
111 * @param string $color Foreground color (in SVG format) for bar elements (background is transparent).
112 * @return string SVG code.
113 * @public
114 */
115 public function getBarcodeSVGcode($w = 2, $h = 30, $color = 'black')
116 {
117 // replace table for special characters
118 $repstr = array("\x00" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
119 $svg = '<' . '?' . 'xml version="1.0" standalone="no"' . '?' . '>' . "\n";
120 $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
121 $svg .= '<svg width="' . \round($this->barcode_array['maxw'] * $w, 3) . '" height="' . $h . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . "\n";
122 $svg .= "\t" . '<desc>' . \strtr($this->barcode_array['code'], $repstr) . '</desc>' . "\n";
123 $svg .= "\t" . '<g id="bars" fill="' . $color . '" stroke="none">' . "\n";
124 // print bars
125 $x = 0;
126 foreach ($this->barcode_array['bcode'] as $k => $v) {
127 $bw = \round($v['w'] * $w, 3);
128 $bh = \round($v['h'] * $h / $this->barcode_array['maxh'], 3);
129 if ($v['t']) {
130 $y = \round($v['p'] * $h / $this->barcode_array['maxh'], 3);
131 // draw a vertical bar
132 $svg .= "\t\t" . '<rect x="' . $x . '" y="' . $y . '" width="' . $bw . '" height="' . $bh . '" />' . "\n";
133 }
134 $x += $bw;
135 }
136 $svg .= "\t" . '</g>' . "\n";
137 $svg .= '</svg>' . "\n";
138 return $svg;
139 }
140 /**
141 * Return an HTML representation of barcode.
142 * @param int $w Width of a single bar element in pixels.
143 * @param int $h Height of a single bar element in pixels.
144 * @param string $color Foreground color for bar elements (background is transparent).
145 * @return string HTML code.
146 * @public
147 */
148 public function getBarcodeHTML($w = 2, $h = 30, $color = 'black')
149 {
150 $html = '<div style="font-size:0;position:relative;width:' . $this->barcode_array['maxw'] * $w . 'px;height:' . $h . 'px;">' . "\n";
151 // print bars
152 $x = 0;
153 foreach ($this->barcode_array['bcode'] as $k => $v) {
154 $bw = \round($v['w'] * $w, 3);
155 $bh = \round($v['h'] * $h / $this->barcode_array['maxh'], 3);
156 if ($v['t']) {
157 $y = \round($v['p'] * $h / $this->barcode_array['maxh'], 3);
158 // draw a vertical bar
159 $html .= '<div style="background-color:' . $color . ';width:' . $bw . 'px;height:' . $bh . 'px;position:absolute;left:' . $x . 'px;top:' . $y . 'px;">&nbsp;</div>' . "\n";
160 }
161 $x += $bw;
162 }
163 $html .= '</div>' . "\n";
164 return $html;
165 }
166 /**
167 * Send a PNG image representation of barcode (requires GD or Imagick library).
168 * @param int $w Width of a single bar element in pixels.
169 * @param int $h Height of a single bar element in pixels.
170 * @param array $color RGB (0-255) foreground color for bar elements (background is transparent).
171 * @public
172 */
173 public function getBarcodePNG($w = 2, $h = 30, $color = array(0, 0, 0))
174 {
175 $data = $this->getBarcodePngData($w, $h, $color);
176 // send headers
177 \header('Content-Type: image/png');
178 \header('Cache-Control: public, must-revalidate, max-age=0');
179 // HTTP/1.1
180 \header('Pragma: public');
181 \header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
182 // Date in the past
183 \header('Last-Modified: ' . \gmdate('D, d M Y H:i:s') . ' GMT');
184 //header('Content-Length: '.strlen($data));
185 echo $data;
186 }
187 /**
188 * Return a PNG image representation of barcode (requires GD or Imagick library).
189 * @param int $w Width of a single bar element in pixels.
190 * @param int $h Height of a single bar element in pixels.
191 * @param array $color RGB (0-255) foreground color for bar elements (background is transparent).
192 * @return string|Imagick|false image or false in case of error.
193 * @public
194 */
195 public function getBarcodePngData($w = 2, $h = 30, $color = array(0, 0, 0))
196 {
197 // calculate image size
198 $width = $this->barcode_array['maxw'] * $w;
199 $height = $h;
200 if (\function_exists('imagecreate')) {
201 // GD library
202 $imagick = \false;
203 $png = \imagecreate($width, $height);
204 $bgcol = \imagecolorallocate($png, 255, 255, 255);
205 \imagecolortransparent($png, $bgcol);
206 $fgcol = \imagecolorallocate($png, $color[0], $color[1], $color[2]);
207 } elseif (\extension_loaded('imagick')) {
208 $imagick = \true;
209 $bgcol = new \imagickpixel('rgb(255,255,255');
210 $fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
211 $png = new \Imagick();
212 $png->newImage($width, $height, 'none', 'png');
213 $bar = new \imagickdraw();
214 $bar->setfillcolor($fgcol);
215 } else {
216 return \false;
217 }
218 // print bars
219 $x = 0;
220 foreach ($this->barcode_array['bcode'] as $k => $v) {
221 $bw = \round($v['w'] * $w, 3);
222 $bh = \round($v['h'] * $h / $this->barcode_array['maxh'], 3);
223 if ($v['t']) {
224 $y = \round($v['p'] * $h / $this->barcode_array['maxh'], 3);
225 // draw a vertical bar
226 if ($imagick) {
227 $bar->rectangle($x, $y, $x + $bw - 1, $y + $bh - 1);
228 } else {
229 \imagefilledrectangle($png, $x, $y, $x + $bw - 1, $y + $bh - 1, $fgcol);
230 }
231 }
232 $x += $bw;
233 }
234 if ($imagick) {
235 $png->drawimage($bar);
236 return $png;
237 } else {
238 \ob_start();
239 \imagepng($png);
240 $imagedata = \ob_get_clean();
241 \imagedestroy($png);
242 return $imagedata;
243 }
244 }
245 /**
246 * Set the barcode.
247 * @param string $code code to print
248 * @param string $type type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extension</li><li>EAN5 : 5-Digits UPC-Based Extension</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>IMBPRE: Pre-processed Intelligent Mail Barcode - Onecode - USPS-B-3200, using only F,A,D,T letters</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
249 * @return void
250 * @public
251 */
252 public function setBarcode($code, $type)
253 {
254 switch (\strtoupper($type)) {
255 case 'C39':
256 // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
257 $arrcode = $this->barcode_code39($code, \false, \false);
258 break;
259 case 'C39+':
260 // CODE 39 with checksum
261 $arrcode = $this->barcode_code39($code, \false, \true);
262 break;
263 case 'C39E':
264 // CODE 39 EXTENDED
265 $arrcode = $this->barcode_code39($code, \true, \false);
266 break;
267 case 'C39E+':
268 // CODE 39 EXTENDED + CHECKSUM
269 $arrcode = $this->barcode_code39($code, \true, \true);
270 break;
271 case 'C93':
272 // CODE 93 - USS-93
273 $arrcode = $this->barcode_code93($code);
274 break;
275 case 'S25':
276 // Standard 2 of 5
277 $arrcode = $this->barcode_s25($code, \false);
278 break;
279 case 'S25+':
280 // Standard 2 of 5 + CHECKSUM
281 $arrcode = $this->barcode_s25($code, \true);
282 break;
283 case 'I25':
284 // Interleaved 2 of 5
285 $arrcode = $this->barcode_i25($code, \false);
286 break;
287 case 'I25+':
288 // Interleaved 2 of 5 + CHECKSUM
289 $arrcode = $this->barcode_i25($code, \true);
290 break;
291 case 'C128':
292 // CODE 128
293 $arrcode = $this->barcode_c128($code, '');
294 break;
295 case 'C128A':
296 // CODE 128 A
297 $arrcode = $this->barcode_c128($code, 'A');
298 break;
299 case 'C128B':
300 // CODE 128 B
301 $arrcode = $this->barcode_c128($code, 'B');
302 break;
303 case 'C128C':
304 // CODE 128 C
305 $arrcode = $this->barcode_c128($code, 'C');
306 break;
307 case 'EAN2':
308 // 2-Digits UPC-Based Extension
309 $arrcode = $this->barcode_eanext($code, 2);
310 break;
311 case 'EAN5':
312 // 5-Digits UPC-Based Extension
313 $arrcode = $this->barcode_eanext($code, 5);
314 break;
315 case 'EAN8':
316 // EAN 8
317 $arrcode = $this->barcode_eanupc($code, 8);
318 break;
319 case 'EAN13':
320 // EAN 13
321 $arrcode = $this->barcode_eanupc($code, 13);
322 break;
323 case 'UPCA':
324 // UPC-A
325 $arrcode = $this->barcode_eanupc($code, 12);
326 break;
327 case 'UPCE':
328 // UPC-E
329 $arrcode = $this->barcode_eanupc($code, 6);
330 break;
331 case 'MSI':
332 // MSI (Variation of Plessey code)
333 $arrcode = $this->barcode_msi($code, \false);
334 break;
335 case 'MSI+':
336 // MSI + CHECKSUM (modulo 11)
337 $arrcode = $this->barcode_msi($code, \true);
338 break;
339 case 'POSTNET':
340 // POSTNET
341 $arrcode = $this->barcode_postnet($code, \false);
342 break;
343 case 'PLANET':
344 // PLANET
345 $arrcode = $this->barcode_postnet($code, \true);
346 break;
347 case 'RMS4CC':
348 // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
349 $arrcode = $this->barcode_rms4cc($code, \false);
350 break;
351 case 'KIX':
352 // KIX (Klant index - Customer index)
353 $arrcode = $this->barcode_rms4cc($code, \true);
354 break;
355 case 'IMB':
356 // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
357 $arrcode = $this->barcode_imb($code);
358 break;
359 case 'IMBPRE':
360 // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200- pre-processed
361 $arrcode = $this->barcode_imb_pre($code);
362 break;
363 case 'CODABAR':
364 // CODABAR
365 $arrcode = $this->barcode_codabar($code);
366 break;
367 case 'CODE11':
368 // CODE 11
369 $arrcode = $this->barcode_code11($code);
370 break;
371 case 'PHARMA':
372 // PHARMACODE
373 $arrcode = $this->barcode_pharmacode($code);
374 break;
375 case 'PHARMA2T':
376 // PHARMACODE TWO-TRACKS
377 $arrcode = $this->barcode_pharmacode2t($code);
378 break;
379 default:
380 $this->barcode_array = array();
381 $arrcode = \false;
382 break;
383 }
384 $this->barcode_array = $arrcode;
385 }
386 /**
387 * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
388 * General-purpose code in very wide use world-wide
389 * @param string $code code to represent.
390 * @param boolean $extended if true uses the extended mode.
391 * @param boolean $checksum if true add a checksum to the code.
392 * @return array barcode representation.
393 * @protected
394 */
395 protected function barcode_code39($code, $extended = \false, $checksum = \false)
396 {
397 $chr['0'] = '111331311';
398 $chr['1'] = '311311113';
399 $chr['2'] = '113311113';
400 $chr['3'] = '313311111';
401 $chr['4'] = '111331113';
402 $chr['5'] = '311331111';
403 $chr['6'] = '113331111';
404 $chr['7'] = '111311313';
405 $chr['8'] = '311311311';
406 $chr['9'] = '113311311';
407 $chr['A'] = '311113113';
408 $chr['B'] = '113113113';
409 $chr['C'] = '313113111';
410 $chr['D'] = '111133113';
411 $chr['E'] = '311133111';
412 $chr['F'] = '113133111';
413 $chr['G'] = '111113313';
414 $chr['H'] = '311113311';
415 $chr['I'] = '113113311';
416 $chr['J'] = '111133311';
417 $chr['K'] = '311111133';
418 $chr['L'] = '113111133';
419 $chr['M'] = '313111131';
420 $chr['N'] = '111131133';
421 $chr['O'] = '311131131';
422 $chr['P'] = '113131131';
423 $chr['Q'] = '111111333';
424 $chr['R'] = '311111331';
425 $chr['S'] = '113111331';
426 $chr['T'] = '111131331';
427 $chr['U'] = '331111113';
428 $chr['V'] = '133111113';
429 $chr['W'] = '333111111';
430 $chr['X'] = '131131113';
431 $chr['Y'] = '331131111';
432 $chr['Z'] = '133131111';
433 $chr['-'] = '131111313';
434 $chr['.'] = '331111311';
435 $chr[' '] = '133111311';
436 $chr['$'] = '131313111';
437 $chr['/'] = '131311131';
438 $chr['+'] = '131113131';
439 $chr['%'] = '111313131';
440 $chr['*'] = '131131311';
441 $code = \strtoupper($code);
442 if ($extended) {
443 // extended mode
444 $code = $this->encode_code39_ext($code);
445 }
446 if ($code === \false) {
447 return \false;
448 }
449 if ($checksum) {
450 // checksum
451 $code .= $this->checksum_code39($code);
452 }
453 // add start and stop codes
454 $code = '*' . $code . '*';
455 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
456 $k = 0;
457 $clen = \strlen($code);
458 for ($i = 0; $i < $clen; ++$i) {
459 $char = $code[$i];
460 if (!isset($chr[$char])) {
461 // invalid character
462 return \false;
463 }
464 for ($j = 0; $j < 9; ++$j) {
465 if ($j % 2 == 0) {
466 $t = \true;
467 // bar
468 } else {
469 $t = \false;
470 // space
471 }
472 $w = $chr[$char][$j];
473 $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
474 $bararray['maxw'] += $w;
475 ++$k;
476 }
477 // intercharacter gap
478 $bararray['bcode'][$k] = array('t' => \false, 'w' => 1, 'h' => 1, 'p' => 0);
479 $bararray['maxw'] += 1;
480 ++$k;
481 }
482 return $bararray;
483 }
484 /**
485 * Encode a string to be used for CODE 39 Extended mode.
486 * @param string $code code to represent.
487 * @return string encoded string.
488 * @protected
489 */
490 protected function encode_code39_ext($code)
491 {
492 $encode = array(\chr(0) => '%U', \chr(1) => '$A', \chr(2) => '$B', \chr(3) => '$C', \chr(4) => '$D', \chr(5) => '$E', \chr(6) => '$F', \chr(7) => '$G', \chr(8) => '$H', \chr(9) => '$I', \chr(10) => '$J', \chr(11) => '£K', \chr(12) => '$L', \chr(13) => '$M', \chr(14) => '$N', \chr(15) => '$O', \chr(16) => '$P', \chr(17) => '$Q', \chr(18) => '$R', \chr(19) => '$S', \chr(20) => '$T', \chr(21) => '$U', \chr(22) => '$V', \chr(23) => '$W', \chr(24) => '$X', \chr(25) => '$Y', \chr(26) => '$Z', \chr(27) => '%A', \chr(28) => '%B', \chr(29) => '%C', \chr(30) => '%D', \chr(31) => '%E', \chr(32) => ' ', \chr(33) => '/A', \chr(34) => '/B', \chr(35) => '/C', \chr(36) => '/D', \chr(37) => '/E', \chr(38) => '/F', \chr(39) => '/G', \chr(40) => '/H', \chr(41) => '/I', \chr(42) => '/J', \chr(43) => '/K', \chr(44) => '/L', \chr(45) => '-', \chr(46) => '.', \chr(47) => '/O', \chr(48) => '0', \chr(49) => '1', \chr(50) => '2', \chr(51) => '3', \chr(52) => '4', \chr(53) => '5', \chr(54) => '6', \chr(55) => '7', \chr(56) => '8', \chr(57) => '9', \chr(58) => '/Z', \chr(59) => '%F', \chr(60) => '%G', \chr(61) => '%H', \chr(62) => '%I', \chr(63) => '%J', \chr(64) => '%V', \chr(65) => 'A', \chr(66) => 'B', \chr(67) => 'C', \chr(68) => 'D', \chr(69) => 'E', \chr(70) => 'F', \chr(71) => 'G', \chr(72) => 'H', \chr(73) => 'I', \chr(74) => 'J', \chr(75) => 'K', \chr(76) => 'L', \chr(77) => 'M', \chr(78) => 'N', \chr(79) => 'O', \chr(80) => 'P', \chr(81) => 'Q', \chr(82) => 'R', \chr(83) => 'S', \chr(84) => 'T', \chr(85) => 'U', \chr(86) => 'V', \chr(87) => 'W', \chr(88) => 'X', \chr(89) => 'Y', \chr(90) => 'Z', \chr(91) => '%K', \chr(92) => '%L', \chr(93) => '%M', \chr(94) => '%N', \chr(95) => '%O', \chr(96) => '%W', \chr(97) => '+A', \chr(98) => '+B', \chr(99) => '+C', \chr(100) => '+D', \chr(101) => '+E', \chr(102) => '+F', \chr(103) => '+G', \chr(104) => '+H', \chr(105) => '+I', \chr(106) => '+J', \chr(107) => '+K', \chr(108) => '+L', \chr(109) => '+M', \chr(110) => '+N', \chr(111) => '+O', \chr(112) => '+P', \chr(113) => '+Q', \chr(114) => '+R', \chr(115) => '+S', \chr(116) => '+T', \chr(117) => '+U', \chr(118) => '+V', \chr(119) => '+W', \chr(120) => '+X', \chr(121) => '+Y', \chr(122) => '+Z', \chr(123) => '%P', \chr(124) => '%Q', \chr(125) => '%R', \chr(126) => '%S', \chr(127) => '%T');
493 $code_ext = '';
494 $clen = \strlen($code);
495 for ($i = 0; $i < $clen; ++$i) {
496 if (\ord($code[$i]) > 127) {
497 return \false;
498 }
499 $code_ext .= $encode[$code[$i]];
500 }
501 return $code_ext;
502 }
503 /**
504 * Calculate CODE 39 checksum (modulo 43).
505 * @param string $code code to represent.
506 * @return string char checksum.
507 * @protected
508 */
509 protected function checksum_code39($code)
510 {
511 $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
512 $sum = 0;
513 $clen = \strlen($code);
514 for ($i = 0; $i < $clen; ++$i) {
515 $k = \array_keys($chars, $code[$i]);
516 $sum += $k[0];
517 }
518 $j = $sum % 43;
519 return $chars[$j];
520 }
521 /**
522 * CODE 93 - USS-93
523 * Compact code similar to Code 39
524 * @param string $code code to represent.
525 * @return array barcode representation.
526 * @protected
527 */
528 protected function barcode_code93($code)
529 {
530 $chr[48] = '131112';
531 // 0
532 $chr[49] = '111213';
533 // 1
534 $chr[50] = '111312';
535 // 2
536 $chr[51] = '111411';
537 // 3
538 $chr[52] = '121113';
539 // 4
540 $chr[53] = '121212';
541 // 5
542 $chr[54] = '121311';
543 // 6
544 $chr[55] = '111114';
545 // 7
546 $chr[56] = '131211';
547 // 8
548 $chr[57] = '141111';
549 // 9
550 $chr[65] = '211113';
551 // A
552 $chr[66] = '211212';
553 // B
554 $chr[67] = '211311';
555 // C
556 $chr[68] = '221112';
557 // D
558 $chr[69] = '221211';
559 // E
560 $chr[70] = '231111';
561 // F
562 $chr[71] = '112113';
563 // G
564 $chr[72] = '112212';
565 // H
566 $chr[73] = '112311';
567 // I
568 $chr[74] = '122112';
569 // J
570 $chr[75] = '132111';
571 // K
572 $chr[76] = '111123';
573 // L
574 $chr[77] = '111222';
575 // M
576 $chr[78] = '111321';
577 // N
578 $chr[79] = '121122';
579 // O
580 $chr[80] = '131121';
581 // P
582 $chr[81] = '212112';
583 // Q
584 $chr[82] = '212211';
585 // R
586 $chr[83] = '211122';
587 // S
588 $chr[84] = '211221';
589 // T
590 $chr[85] = '221121';
591 // U
592 $chr[86] = '222111';
593 // V
594 $chr[87] = '112122';
595 // W
596 $chr[88] = '112221';
597 // X
598 $chr[89] = '122121';
599 // Y
600 $chr[90] = '123111';
601 // Z
602 $chr[45] = '121131';
603 // -
604 $chr[46] = '311112';
605 // .
606 $chr[32] = '311211';
607 //
608 $chr[36] = '321111';
609 // $
610 $chr[47] = '112131';
611 // /
612 $chr[43] = '113121';
613 // +
614 $chr[37] = '211131';
615 // %
616 $chr[128] = '121221';
617 // ($)
618 $chr[129] = '311121';
619 // (/)
620 $chr[130] = '122211';
621 // (+)
622 $chr[131] = '312111';
623 // (%)
624 $chr[42] = '111141';
625 // start-stop
626 $code = \strtoupper($code);
627 $encode = array(\chr(0) => \chr(131) . 'U', \chr(1) => \chr(128) . 'A', \chr(2) => \chr(128) . 'B', \chr(3) => \chr(128) . 'C', \chr(4) => \chr(128) . 'D', \chr(5) => \chr(128) . 'E', \chr(6) => \chr(128) . 'F', \chr(7) => \chr(128) . 'G', \chr(8) => \chr(128) . 'H', \chr(9) => \chr(128) . 'I', \chr(10) => \chr(128) . 'J', \chr(11) => '£K', \chr(12) => \chr(128) . 'L', \chr(13) => \chr(128) . 'M', \chr(14) => \chr(128) . 'N', \chr(15) => \chr(128) . 'O', \chr(16) => \chr(128) . 'P', \chr(17) => \chr(128) . 'Q', \chr(18) => \chr(128) . 'R', \chr(19) => \chr(128) . 'S', \chr(20) => \chr(128) . 'T', \chr(21) => \chr(128) . 'U', \chr(22) => \chr(128) . 'V', \chr(23) => \chr(128) . 'W', \chr(24) => \chr(128) . 'X', \chr(25) => \chr(128) . 'Y', \chr(26) => \chr(128) . 'Z', \chr(27) => \chr(131) . 'A', \chr(28) => \chr(131) . 'B', \chr(29) => \chr(131) . 'C', \chr(30) => \chr(131) . 'D', \chr(31) => \chr(131) . 'E', \chr(32) => ' ', \chr(33) => \chr(129) . 'A', \chr(34) => \chr(129) . 'B', \chr(35) => \chr(129) . 'C', \chr(36) => \chr(129) . 'D', \chr(37) => \chr(129) . 'E', \chr(38) => \chr(129) . 'F', \chr(39) => \chr(129) . 'G', \chr(40) => \chr(129) . 'H', \chr(41) => \chr(129) . 'I', \chr(42) => \chr(129) . 'J', \chr(43) => \chr(129) . 'K', \chr(44) => \chr(129) . 'L', \chr(45) => '-', \chr(46) => '.', \chr(47) => \chr(129) . 'O', \chr(48) => '0', \chr(49) => '1', \chr(50) => '2', \chr(51) => '3', \chr(52) => '4', \chr(53) => '5', \chr(54) => '6', \chr(55) => '7', \chr(56) => '8', \chr(57) => '9', \chr(58) => \chr(129) . 'Z', \chr(59) => \chr(131) . 'F', \chr(60) => \chr(131) . 'G', \chr(61) => \chr(131) . 'H', \chr(62) => \chr(131) . 'I', \chr(63) => \chr(131) . 'J', \chr(64) => \chr(131) . 'V', \chr(65) => 'A', \chr(66) => 'B', \chr(67) => 'C', \chr(68) => 'D', \chr(69) => 'E', \chr(70) => 'F', \chr(71) => 'G', \chr(72) => 'H', \chr(73) => 'I', \chr(74) => 'J', \chr(75) => 'K', \chr(76) => 'L', \chr(77) => 'M', \chr(78) => 'N', \chr(79) => 'O', \chr(80) => 'P', \chr(81) => 'Q', \chr(82) => 'R', \chr(83) => 'S', \chr(84) => 'T', \chr(85) => 'U', \chr(86) => 'V', \chr(87) => 'W', \chr(88) => 'X', \chr(89) => 'Y', \chr(90) => 'Z', \chr(91) => \chr(131) . 'K', \chr(92) => \chr(131) . 'L', \chr(93) => \chr(131) . 'M', \chr(94) => \chr(131) . 'N', \chr(95) => \chr(131) . 'O', \chr(96) => \chr(131) . 'W', \chr(97) => \chr(130) . 'A', \chr(98) => \chr(130) . 'B', \chr(99) => \chr(130) . 'C', \chr(100) => \chr(130) . 'D', \chr(101) => \chr(130) . 'E', \chr(102) => \chr(130) . 'F', \chr(103) => \chr(130) . 'G', \chr(104) => \chr(130) . 'H', \chr(105) => \chr(130) . 'I', \chr(106) => \chr(130) . 'J', \chr(107) => \chr(130) . 'K', \chr(108) => \chr(130) . 'L', \chr(109) => \chr(130) . 'M', \chr(110) => \chr(130) . 'N', \chr(111) => \chr(130) . 'O', \chr(112) => \chr(130) . 'P', \chr(113) => \chr(130) . 'Q', \chr(114) => \chr(130) . 'R', \chr(115) => \chr(130) . 'S', \chr(116) => \chr(130) . 'T', \chr(117) => \chr(130) . 'U', \chr(118) => \chr(130) . 'V', \chr(119) => \chr(130) . 'W', \chr(120) => \chr(130) . 'X', \chr(121) => \chr(130) . 'Y', \chr(122) => \chr(130) . 'Z', \chr(123) => \chr(131) . 'P', \chr(124) => \chr(131) . 'Q', \chr(125) => \chr(131) . 'R', \chr(126) => \chr(131) . 'S', \chr(127) => \chr(131) . 'T');
628 $code_ext = '';
629 $clen = \strlen($code);
630 for ($i = 0; $i < $clen; ++$i) {
631 if (\ord($code[$i]) > 127) {
632 return \false;
633 }
634 $code_ext .= $encode[$code[$i]];
635 }
636 // checksum
637 $code_ext .= $this->checksum_code93($code_ext);
638 // add start and stop codes
639 $code = '*' . $code_ext . '*';
640 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
641 $k = 0;
642 $clen = \strlen($code);
643 for ($i = 0; $i < $clen; ++$i) {
644 $char = \ord($code[$i]);
645 if (!isset($chr[$char])) {
646 // invalid character
647 return \false;
648 }
649 for ($j = 0; $j < 6; ++$j) {
650 if ($j % 2 == 0) {
651 $t = \true;
652 // bar
653 } else {
654 $t = \false;
655 // space
656 }
657 $w = $chr[$char][$j];
658 $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
659 $bararray['maxw'] += $w;
660 ++$k;
661 }
662 }
663 $bararray['bcode'][$k] = array('t' => \true, 'w' => 1, 'h' => 1, 'p' => 0);
664 $bararray['maxw'] += 1;
665 ++$k;
666 return $bararray;
667 }
668 /**
669 * Calculate CODE 93 checksum (modulo 47).
670 * @param string $code code to represent.
671 * @return string checksum code.
672 * @protected
673 */
674 protected function checksum_code93($code)
675 {
676 $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', '<', '=', '>', '?');
677 // translate special characters
678 $code = \strtr($code, \chr(128) . \chr(131) . \chr(129) . \chr(130), '<=>?');
679 $len = \strlen($code);
680 // calculate check digit C
681 $p = 1;
682 $check = 0;
683 for ($i = $len - 1; $i >= 0; --$i) {
684 $k = \array_keys($chars, $code[$i]);
685 $check += $k[0] * $p;
686 ++$p;
687 if ($p > 20) {
688 $p = 1;
689 }
690 }
691 $check %= 47;
692 $c = $chars[$check];
693 $code .= $c;
694 // calculate check digit K
695 $p = 1;
696 $check = 0;
697 for ($i = $len; $i >= 0; --$i) {
698 $k = \array_keys($chars, $code[$i]);
699 $check += $k[0] * $p;
700 ++$p;
701 if ($p > 15) {
702 $p = 1;
703 }
704 }
705 $check %= 47;
706 $k = $chars[$check];
707 $checksum = $c . $k;
708 // resto respecial characters
709 $checksum = \strtr($checksum, '<=>?', \chr(128) . \chr(131) . \chr(129) . \chr(130));
710 return $checksum;
711 }
712 /**
713 * Checksum for standard 2 of 5 barcodes.
714 * @param string $code code to process.
715 * @return int checksum.
716 * @protected
717 */
718 protected function checksum_s25($code)
719 {
720 $len = \strlen($code);
721 $sum = 0;
722 for ($i = 0; $i < $len; $i += 2) {
723 $sum += $code[$i];
724 }
725 $sum *= 3;
726 for ($i = 1; $i < $len; $i += 2) {
727 $sum += $code[$i];
728 }
729 $r = $sum % 10;
730 if ($r > 0) {
731 $r = 10 - $r;
732 }
733 return $r;
734 }
735 /**
736 * MSI.
737 * Variation of Plessey code, with similar applications
738 * Contains digits (0 to 9) and encodes the data only in the width of bars.
739 * @param string $code code to represent.
740 * @param boolean $checksum if true add a checksum to the code (modulo 11)
741 * @return array barcode representation.
742 * @protected
743 */
744 protected function barcode_msi($code, $checksum = \false)
745 {
746 $chr['0'] = '100100100100';
747 $chr['1'] = '100100100110';
748 $chr['2'] = '100100110100';
749 $chr['3'] = '100100110110';
750 $chr['4'] = '100110100100';
751 $chr['5'] = '100110100110';
752 $chr['6'] = '100110110100';
753 $chr['7'] = '100110110110';
754 $chr['8'] = '110100100100';
755 $chr['9'] = '110100100110';
756 $chr['A'] = '110100110100';
757 $chr['B'] = '110100110110';
758 $chr['C'] = '110110100100';
759 $chr['D'] = '110110100110';
760 $chr['E'] = '110110110100';
761 $chr['F'] = '110110110110';
762 if ($checksum) {
763 // add checksum
764 $clen = \strlen($code);
765 $p = 2;
766 $check = 0;
767 for ($i = $clen - 1; $i >= 0; --$i) {
768 $check += \hexdec($code[$i]) * $p;
769 ++$p;
770 if ($p > 7) {
771 $p = 2;
772 }
773 }
774 $check %= 11;
775 if ($check > 0) {
776 $check = 11 - $check;
777 }
778 $code .= $check;
779 }
780 $seq = '110';
781 // left guard
782 $clen = \strlen($code);
783 for ($i = 0; $i < $clen; ++$i) {
784 $digit = $code[$i];
785 if (!isset($chr[$digit])) {
786 // invalid character
787 return \false;
788 }
789 $seq .= $chr[$digit];
790 }
791 $seq .= '1001';
792 // right guard
793 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
794 return $this->binseq_to_array($seq, $bararray);
795 }
796 /**
797 * Standard 2 of 5 barcodes.
798 * Used in airline ticket marking, photofinishing
799 * Contains digits (0 to 9) and encodes the data only in the width of bars.
800 * @param string $code code to represent.
801 * @param boolean $checksum if true add a checksum to the code
802 * @return array barcode representation.
803 * @protected
804 */
805 protected function barcode_s25($code, $checksum = \false)
806 {
807 $chr['0'] = '10101110111010';
808 $chr['1'] = '11101010101110';
809 $chr['2'] = '10111010101110';
810 $chr['3'] = '11101110101010';
811 $chr['4'] = '10101110101110';
812 $chr['5'] = '11101011101010';
813 $chr['6'] = '10111011101010';
814 $chr['7'] = '10101011101110';
815 $chr['8'] = '11101010111010';
816 $chr['9'] = '10111010111010';
817 if ($checksum) {
818 // add checksum
819 $code .= $this->checksum_s25($code);
820 }
821 if (\strlen($code) % 2 != 0) {
822 // add leading zero if code-length is odd
823 $code = '0' . $code;
824 }
825 $seq = '1110111010';
826 $clen = \strlen($code);
827 for ($i = 0; $i < $clen; ++$i) {
828 $digit = $code[$i];
829 if (!isset($chr[$digit])) {
830 // invalid character
831 return \false;
832 }
833 $seq .= $chr[$digit];
834 }
835 $seq .= '111010111';
836 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
837 return $this->binseq_to_array($seq, $bararray);
838 }
839 /**
840 * Convert binary barcode sequence to WarnockPDF barcode array.
841 * @param string $seq barcode as binary sequence.
842 * @param array $bararray barcode array to fill up
843 * @return array barcode representation.
844 * @protected
845 */
846 protected function binseq_to_array($seq, $bararray)
847 {
848 $len = \strlen($seq);
849 $w = 0;
850 $k = 0;
851 for ($i = 0; $i < $len; ++$i) {
852 $w += 1;
853 if ($i == $len - 1 or $i < $len - 1 and $seq[$i] != $seq[$i + 1]) {
854 if ($seq[$i] == '1') {
855 $t = \true;
856 // bar
857 } else {
858 $t = \false;
859 // space
860 }
861 $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
862 $bararray['maxw'] += $w;
863 ++$k;
864 $w = 0;
865 }
866 }
867 return $bararray;
868 }
869 /**
870 * Interleaved 2 of 5 barcodes.
871 * Compact numeric code, widely used in industry, air cargo
872 * Contains digits (0 to 9) and encodes the data in the width of both bars and spaces.
873 * @param string $code code to represent.
874 * @param boolean $checksum if true add a checksum to the code
875 * @return array barcode representation.
876 * @protected
877 */
878 protected function barcode_i25($code, $checksum = \false)
879 {
880 $chr['0'] = '11221';
881 $chr['1'] = '21112';
882 $chr['2'] = '12112';
883 $chr['3'] = '22111';
884 $chr['4'] = '11212';
885 $chr['5'] = '21211';
886 $chr['6'] = '12211';
887 $chr['7'] = '11122';
888 $chr['8'] = '21121';
889 $chr['9'] = '12121';
890 $chr['A'] = '11';
891 $chr['Z'] = '21';
892 if ($checksum) {
893 // add checksum
894 $code .= $this->checksum_s25($code);
895 }
896 if (\strlen($code) % 2 != 0) {
897 // add leading zero if code-length is odd
898 $code = '0' . $code;
899 }
900 // add start and stop codes
901 $code = 'AA' . \strtolower($code) . 'ZA';
902 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
903 $k = 0;
904 $clen = \strlen($code);
905 for ($i = 0; $i < $clen; $i = $i + 2) {
906 $char_bar = $code[$i];
907 $char_space = $code[$i + 1];
908 if (!isset($chr[$char_bar]) or !isset($chr[$char_space])) {
909 // invalid character
910 return \false;
911 }
912 // create a bar-space sequence
913 $seq = '';
914 $chrlen = \strlen($chr[$char_bar]);
915 for ($s = 0; $s < $chrlen; $s++) {
916 $seq .= $chr[$char_bar][$s] . $chr[$char_space][$s];
917 }
918 $seqlen = \strlen($seq);
919 for ($j = 0; $j < $seqlen; ++$j) {
920 if ($j % 2 == 0) {
921 $t = \true;
922 // bar
923 } else {
924 $t = \false;
925 // space
926 }
927 $w = (float) $seq[$j];
928 $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
929 $bararray['maxw'] += $w;
930 ++$k;
931 }
932 }
933 return $bararray;
934 }
935 /**
936 * C128 barcodes.
937 * Very capable code, excellent density, high reliability; in very wide use world-wide
938 * @param string $code code to represent.
939 * @param string $type barcode type: A, B, C or empty for automatic switch (AUTO mode)
940 * @return array barcode representation.
941 * @protected
942 */
943 protected function barcode_c128($code, $type = '')
944 {
945 $chr = array(
946 '212222',
947 /* 00 */
948 '222122',
949 /* 01 */
950 '222221',
951 /* 02 */
952 '121223',
953 /* 03 */
954 '121322',
955 /* 04 */
956 '131222',
957 /* 05 */
958 '122213',
959 /* 06 */
960 '122312',
961 /* 07 */
962 '132212',
963 /* 08 */
964 '221213',
965 /* 09 */
966 '221312',
967 /* 10 */
968 '231212',
969 /* 11 */
970 '112232',
971 /* 12 */
972 '122132',
973 /* 13 */
974 '122231',
975 /* 14 */
976 '113222',
977 /* 15 */
978 '123122',
979 /* 16 */
980 '123221',
981 /* 17 */
982 '223211',
983 /* 18 */
984 '221132',
985 /* 19 */
986 '221231',
987 /* 20 */
988 '213212',
989 /* 21 */
990 '223112',
991 /* 22 */
992 '312131',
993 /* 23 */
994 '311222',
995 /* 24 */
996 '321122',
997 /* 25 */
998 '321221',
999 /* 26 */
1000 '312212',
1001 /* 27 */
1002 '322112',
1003 /* 28 */
1004 '322211',
1005 /* 29 */
1006 '212123',
1007 /* 30 */
1008 '212321',
1009 /* 31 */
1010 '232121',
1011 /* 32 */
1012 '111323',
1013 /* 33 */
1014 '131123',
1015 /* 34 */
1016 '131321',
1017 /* 35 */
1018 '112313',
1019 /* 36 */
1020 '132113',
1021 /* 37 */
1022 '132311',
1023 /* 38 */
1024 '211313',
1025 /* 39 */
1026 '231113',
1027 /* 40 */
1028 '231311',
1029 /* 41 */
1030 '112133',
1031 /* 42 */
1032 '112331',
1033 /* 43 */
1034 '132131',
1035 /* 44 */
1036 '113123',
1037 /* 45 */
1038 '113321',
1039 /* 46 */
1040 '133121',
1041 /* 47 */
1042 '313121',
1043 /* 48 */
1044 '211331',
1045 /* 49 */
1046 '231131',
1047 /* 50 */
1048 '213113',
1049 /* 51 */
1050 '213311',
1051 /* 52 */
1052 '213131',
1053 /* 53 */
1054 '311123',
1055 /* 54 */
1056 '311321',
1057 /* 55 */
1058 '331121',
1059 /* 56 */
1060 '312113',
1061 /* 57 */
1062 '312311',
1063 /* 58 */
1064 '332111',
1065 /* 59 */
1066 '314111',
1067 /* 60 */
1068 '221411',
1069 /* 61 */
1070 '431111',
1071 /* 62 */
1072 '111224',
1073 /* 63 */
1074 '111422',
1075 /* 64 */
1076 '121124',
1077 /* 65 */
1078 '121421',
1079 /* 66 */
1080 '141122',
1081 /* 67 */
1082 '141221',
1083 /* 68 */
1084 '112214',
1085 /* 69 */
1086 '112412',
1087 /* 70 */
1088 '122114',
1089 /* 71 */
1090 '122411',
1091 /* 72 */
1092 '142112',
1093 /* 73 */
1094 '142211',
1095 /* 74 */
1096 '241211',
1097 /* 75 */
1098 '221114',
1099 /* 76 */
1100 '413111',
1101 /* 77 */
1102 '241112',
1103 /* 78 */
1104 '134111',
1105 /* 79 */
1106 '111242',
1107 /* 80 */
1108 '121142',
1109 /* 81 */
1110 '121241',
1111 /* 82 */
1112 '114212',
1113 /* 83 */
1114 '124112',
1115 /* 84 */
1116 '124211',
1117 /* 85 */
1118 '411212',
1119 /* 86 */
1120 '421112',
1121 /* 87 */
1122 '421211',
1123 /* 88 */
1124 '212141',
1125 /* 89 */
1126 '214121',
1127 /* 90 */
1128 '412121',
1129 /* 91 */
1130 '111143',
1131 /* 92 */
1132 '111341',
1133 /* 93 */
1134 '131141',
1135 /* 94 */
1136 '114113',
1137 /* 95 */
1138 '114311',
1139 /* 96 */
1140 '411113',
1141 /* 97 */
1142 '411311',
1143 /* 98 */
1144 '113141',
1145 /* 99 */
1146 '114131',
1147 /* 100 */
1148 '311141',
1149 /* 101 */
1150 '411131',
1151 /* 102 */
1152 '211412',
1153 /* 103 START A */
1154 '211214',
1155 /* 104 START B */
1156 '211232',
1157 /* 105 START C */
1158 '233111',
1159 /* STOP */
1160 '200000',
1161 );
1162 // ASCII characters for code A (ASCII 00 - 95)
1163 $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
1164 $keys_a .= \chr(0) . \chr(1) . \chr(2) . \chr(3) . \chr(4) . \chr(5) . \chr(6) . \chr(7) . \chr(8) . \chr(9);
1165 $keys_a .= \chr(10) . \chr(11) . \chr(12) . \chr(13) . \chr(14) . \chr(15) . \chr(16) . \chr(17) . \chr(18) . \chr(19);
1166 $keys_a .= \chr(20) . \chr(21) . \chr(22) . \chr(23) . \chr(24) . \chr(25) . \chr(26) . \chr(27) . \chr(28) . \chr(29);
1167 $keys_a .= \chr(30) . \chr(31);
1168 // ASCII characters for code B (ASCII 32 - 127)
1169 $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . \chr(127);
1170 // special codes
1171 $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101);
1172 $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100);
1173 // array of symbols
1174 $code_data = array();
1175 // length of the code
1176 $len = \strlen($code);
1177 switch (\strtoupper($type)) {
1178 case 'A':
1179 // MODE A
1180 $startid = 103;
1181 for ($i = 0; $i < $len; ++$i) {
1182 $char = $code[$i];
1183 $char_id = \ord($char);
1184 if ($char_id >= 241 and $char_id <= 244) {
1185 $code_data[] = $fnc_a[$char_id];
1186 } elseif ($char_id >= 0 and $char_id <= 95) {
1187 $code_data[] = \strpos($keys_a, $char);
1188 } else {
1189 return \false;
1190 }
1191 }
1192 break;
1193 case 'B':
1194 // MODE B
1195 $startid = 104;
1196 for ($i = 0; $i < $len; ++$i) {
1197 $char = $code[$i];
1198 $char_id = \ord($char);
1199 if ($char_id >= 241 and $char_id <= 244) {
1200 $code_data[] = $fnc_b[$char_id];
1201 } elseif ($char_id >= 32 and $char_id <= 127) {
1202 $code_data[] = \strpos($keys_b, $char);
1203 } else {
1204 return \false;
1205 }
1206 }
1207 break;
1208 case 'C':
1209 // MODE C
1210 $startid = 105;
1211 if (\ord($code[0]) == 241) {
1212 $code_data[] = 102;
1213 $code = \substr($code, 1);
1214 --$len;
1215 }
1216 if ($len % 2 != 0) {
1217 // the length must be even
1218 return \false;
1219 }
1220 for ($i = 0; $i < $len; $i += 2) {
1221 $chrnum = $code[$i] . $code[$i + 1];
1222 if (\preg_match('/([0-9]{2})/', $chrnum) > 0) {
1223 $code_data[] = \intval($chrnum);
1224 } else {
1225 return \false;
1226 }
1227 }
1228 break;
1229 default:
1230 // MODE AUTO
1231 // split code into sequences
1232 $sequence = array();
1233 // get numeric sequences (if any)
1234 $numseq = array();
1235 \preg_match_all('/([0-9]{4,})/', $code, $numseq, \PREG_OFFSET_CAPTURE);
1236 if (isset($numseq[1]) and !empty($numseq[1])) {
1237 $end_offset = 0;
1238 foreach ($numseq[1] as $val) {
1239 $offset = $val[1];
1240 if ($offset > $end_offset) {
1241 // non numeric sequence
1242 $sequence = \array_merge($sequence, $this->get128ABsequence(\substr($code, $end_offset, $offset - $end_offset)));
1243 }
1244 // numeric sequence
1245 $slen = \strlen($val[0]);
1246 if ($slen % 2 != 0) {
1247 // the length must be even
1248 --$slen;
1249 }
1250 $sequence[] = array('C', \substr($code, $offset, $slen), $slen);
1251 $end_offset = $offset + $slen;
1252 }
1253 if ($end_offset < $len) {
1254 $sequence = \array_merge($sequence, $this->get128ABsequence(\substr($code, $end_offset)));
1255 }
1256 } else {
1257 // text code (non C mode)
1258 $sequence = \array_merge($sequence, $this->get128ABsequence($code));
1259 }
1260 // process the sequence
1261 foreach ($sequence as $key => $seq) {
1262 switch ($seq[0]) {
1263 case 'A':
1264 if ($key == 0) {
1265 $startid = 103;
1266 } elseif ($sequence[$key - 1][0] != 'A') {
1267 if ($seq[2] == 1 and $key > 0 and $sequence[$key - 1][0] == 'B' and !isset($sequence[$key - 1][3])) {
1268 // single character shift
1269 $code_data[] = 98;
1270 // mark shift
1271 $sequence[$key][3] = \true;
1272 } elseif (!isset($sequence[$key - 1][3])) {
1273 $code_data[] = 101;
1274 }
1275 }
1276 for ($i = 0; $i < $seq[2]; ++$i) {
1277 $char = $seq[1][$i];
1278 $char_id = \ord($char);
1279 if ($char_id >= 241 and $char_id <= 244) {
1280 $code_data[] = $fnc_a[$char_id];
1281 } else {
1282 $code_data[] = \strpos($keys_a, $char);
1283 }
1284 }
1285 break;
1286 case 'B':
1287 if ($key == 0) {
1288 $tmpchr = \ord($seq[1][0]);
1289 if ($seq[2] == 1 and $tmpchr >= 241 and $tmpchr <= 244 and isset($sequence[$key + 1]) and $sequence[$key + 1][0] != 'B') {
1290 switch ($sequence[$key + 1][0]) {
1291 case 'A':
1292 $startid = 103;
1293 $sequence[$key][0] = 'A';
1294 $code_data[] = $fnc_a[$tmpchr];
1295 break;
1296 case 'C':
1297 $startid = 105;
1298 $sequence[$key][0] = 'C';
1299 $code_data[] = $fnc_a[$tmpchr];
1300 break;
1301 }
1302 break;
1303 } else {
1304 $startid = 104;
1305 }
1306 } elseif ($sequence[$key - 1][0] != 'B') {
1307 if ($seq[2] == 1 and $key > 0 and $sequence[$key - 1][0] == 'A' and !isset($sequence[$key - 1][3])) {
1308 // single character shift
1309 $code_data[] = 98;
1310 // mark shift
1311 $sequence[$key][3] = \true;
1312 } elseif (!isset($sequence[$key - 1][3])) {
1313 $code_data[] = 100;
1314 }
1315 }
1316 for ($i = 0; $i < $seq[2]; ++$i) {
1317 $char = $seq[1][$i];
1318 $char_id = \ord($char);
1319 if ($char_id >= 241 and $char_id <= 244) {
1320 $code_data[] = $fnc_b[$char_id];
1321 } else {
1322 $code_data[] = \strpos($keys_b, $char);
1323 }
1324 }
1325 break;
1326 case 'C':
1327 if ($key == 0) {
1328 $startid = 105;
1329 } elseif ($sequence[$key - 1][0] != 'C') {
1330 $code_data[] = 99;
1331 }
1332 for ($i = 0; $i < $seq[2]; $i += 2) {
1333 $chrnum = $seq[1][$i] . $seq[1][$i + 1];
1334 $code_data[] = \intval($chrnum);
1335 }
1336 break;
1337 }
1338 }
1339 }
1340 // calculate check character
1341 $sum = $startid;
1342 foreach ($code_data as $key => $val) {
1343 $sum += $val * ($key + 1);
1344 }
1345 // add check character
1346 $code_data[] = $sum % 103;
1347 // add stop sequence
1348 $code_data[] = 106;
1349 $code_data[] = 107;
1350 // add start code at the beginning
1351 \array_unshift($code_data, $startid);
1352 // build barcode array
1353 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
1354 foreach ($code_data as $val) {
1355 $seq = $chr[$val];
1356 for ($j = 0; $j < 6; ++$j) {
1357 if ($j % 2 == 0) {
1358 $t = \true;
1359 // bar
1360 } else {
1361 $t = \false;
1362 // space
1363 }
1364 $w = (float) $seq[$j];
1365 $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
1366 $bararray['maxw'] += $w;
1367 }
1368 }
1369 return $bararray;
1370 }
1371 /**
1372 * Split text code in A/B sequence for 128 code
1373 * @param string $code code to split.
1374 * @return array sequence
1375 * @protected
1376 */
1377 protected function get128ABsequence($code)
1378 {
1379 $len = \strlen($code);
1380 $sequence = array();
1381 // get A sequences (if any)
1382 $numseq = array();
1383 \preg_match_all('/([\\0-\\31])/', $code, $numseq, \PREG_OFFSET_CAPTURE);
1384 if (isset($numseq[1]) and !empty($numseq[1])) {
1385 $end_offset = 0;
1386 foreach ($numseq[1] as $val) {
1387 $offset = $val[1];
1388 if ($offset > $end_offset) {
1389 // B sequence
1390 $sequence[] = array('B', \substr($code, $end_offset, $offset - $end_offset), $offset - $end_offset);
1391 }
1392 // A sequence
1393 $slen = \strlen($val[0]);
1394 $sequence[] = array('A', \substr($code, $offset, $slen), $slen);
1395 $end_offset = $offset + $slen;
1396 }
1397 if ($end_offset < $len) {
1398 $sequence[] = array('B', \substr($code, $end_offset), $len - $end_offset);
1399 }
1400 } else {
1401 // only B sequence
1402 $sequence[] = array('B', $code, $len);
1403 }
1404 return $sequence;
1405 }
1406 /**
1407 * EAN13 and UPC-A barcodes.
1408 * EAN13: European Article Numbering international retail product code
1409 * UPC-A: Universal product code seen on almost all retail products in the USA and Canada
1410 * UPC-E: Short version of UPC symbol
1411 * @param string $code code to represent.
1412 * @param string $len barcode type: 6 = UPC-E, 8 = EAN8, 13 = EAN13, 12 = UPC-A
1413 * @return array barcode representation.
1414 * @protected
1415 */
1416 protected function barcode_eanupc($code, $len = 13)
1417 {
1418 $upce = \false;
1419 if ($len == 6) {
1420 $len = 12;
1421 // UPC-A
1422 $upce = \true;
1423 // UPC-E mode
1424 }
1425 $data_len = $len - 1;
1426 //Padding
1427 $code = \str_pad($code, $data_len, '0', \STR_PAD_LEFT);
1428 $code_len = \strlen($code);
1429 // calculate check digit
1430 $sum_a = 0;
1431 for ($i = 1; $i < $data_len; $i += 2) {
1432 $sum_a += $code[$i];
1433 }
1434 if ($len > 12) {
1435 $sum_a *= 3;
1436 }
1437 $sum_b = 0;
1438 for ($i = 0; $i < $data_len; $i += 2) {
1439 $sum_b += $code[$i];
1440 }
1441 if ($len < 13) {
1442 $sum_b *= 3;
1443 }
1444 $r = ($sum_a + $sum_b) % 10;
1445 if ($r > 0) {
1446 $r = 10 - $r;
1447 }
1448 if ($code_len == $data_len) {
1449 // add check digit
1450 $code .= $r;
1451 } elseif ($r !== \intval($code[$data_len])) {
1452 // wrong checkdigit
1453 return \false;
1454 }
1455 if ($len == 12) {
1456 // UPC-A
1457 $code = '0' . $code;
1458 ++$len;
1459 }
1460 if ($upce) {
1461 // convert UPC-A to UPC-E
1462 $tmp = \substr($code, 4, 3);
1463 if ($tmp == '000' or $tmp == '100' or $tmp == '200') {
1464 // manufacturer code ends in 000, 100, or 200
1465 $upce_code = \substr($code, 2, 2) . \substr($code, 9, 3) . \substr($code, 4, 1);
1466 } else {
1467 $tmp = \substr($code, 5, 2);
1468 if ($tmp == '00') {
1469 // manufacturer code ends in 00
1470 $upce_code = \substr($code, 2, 3) . \substr($code, 10, 2) . '3';
1471 } else {
1472 $tmp = \substr($code, 6, 1);
1473 if ($tmp == '0') {
1474 // manufacturer code ends in 0
1475 $upce_code = \substr($code, 2, 4) . \substr($code, 11, 1) . '4';
1476 } else {
1477 // manufacturer code does not end in zero
1478 $upce_code = \substr($code, 2, 5) . \substr($code, 11, 1);
1479 }
1480 }
1481 }
1482 }
1483 //Convert digits to bars
1484 $codes = array('A' => array(
1485 // left odd parity
1486 '0' => '0001101',
1487 '1' => '0011001',
1488 '2' => '0010011',
1489 '3' => '0111101',
1490 '4' => '0100011',
1491 '5' => '0110001',
1492 '6' => '0101111',
1493 '7' => '0111011',
1494 '8' => '0110111',
1495 '9' => '0001011',
1496 ), 'B' => array(
1497 // left even parity
1498 '0' => '0100111',
1499 '1' => '0110011',
1500 '2' => '0011011',
1501 '3' => '0100001',
1502 '4' => '0011101',
1503 '5' => '0111001',
1504 '6' => '0000101',
1505 '7' => '0010001',
1506 '8' => '0001001',
1507 '9' => '0010111',
1508 ), 'C' => array(
1509 // right
1510 '0' => '1110010',
1511 '1' => '1100110',
1512 '2' => '1101100',
1513 '3' => '1000010',
1514 '4' => '1011100',
1515 '5' => '1001110',
1516 '6' => '1010000',
1517 '7' => '1000100',
1518 '8' => '1001000',
1519 '9' => '1110100',
1520 ));
1521 $parities = array('0' => array('A', 'A', 'A', 'A', 'A', 'A'), '1' => array('A', 'A', 'B', 'A', 'B', 'B'), '2' => array('A', 'A', 'B', 'B', 'A', 'B'), '3' => array('A', 'A', 'B', 'B', 'B', 'A'), '4' => array('A', 'B', 'A', 'A', 'B', 'B'), '5' => array('A', 'B', 'B', 'A', 'A', 'B'), '6' => array('A', 'B', 'B', 'B', 'A', 'A'), '7' => array('A', 'B', 'A', 'B', 'A', 'B'), '8' => array('A', 'B', 'A', 'B', 'B', 'A'), '9' => array('A', 'B', 'B', 'A', 'B', 'A'));
1522 $upce_parities = array();
1523 $upce_parities[0] = array('0' => array('B', 'B', 'B', 'A', 'A', 'A'), '1' => array('B', 'B', 'A', 'B', 'A', 'A'), '2' => array('B', 'B', 'A', 'A', 'B', 'A'), '3' => array('B', 'B', 'A', 'A', 'A', 'B'), '4' => array('B', 'A', 'B', 'B', 'A', 'A'), '5' => array('B', 'A', 'A', 'B', 'B', 'A'), '6' => array('B', 'A', 'A', 'A', 'B', 'B'), '7' => array('B', 'A', 'B', 'A', 'B', 'A'), '8' => array('B', 'A', 'B', 'A', 'A', 'B'), '9' => array('B', 'A', 'A', 'B', 'A', 'B'));
1524 $upce_parities[1] = array('0' => array('A', 'A', 'A', 'B', 'B', 'B'), '1' => array('A', 'A', 'B', 'A', 'B', 'B'), '2' => array('A', 'A', 'B', 'B', 'A', 'B'), '3' => array('A', 'A', 'B', 'B', 'B', 'A'), '4' => array('A', 'B', 'A', 'A', 'B', 'B'), '5' => array('A', 'B', 'B', 'A', 'A', 'B'), '6' => array('A', 'B', 'B', 'B', 'A', 'A'), '7' => array('A', 'B', 'A', 'B', 'A', 'B'), '8' => array('A', 'B', 'A', 'B', 'B', 'A'), '9' => array('A', 'B', 'B', 'A', 'B', 'A'));
1525 $k = 0;
1526 $seq = '101';
1527 // left guard bar
1528 if ($upce) {
1529 $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
1530 $p = $upce_parities[$code[1]][$r];
1531 for ($i = 0; $i < 6; ++$i) {
1532 $seq .= $codes[$p[$i]][$upce_code[$i]];
1533 }
1534 $seq .= '010101';
1535 // right guard bar
1536 } else {
1537 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
1538 $half_len = \intval(\ceil($len / 2));
1539 if ($len == 8) {
1540 for ($i = 0; $i < $half_len; ++$i) {
1541 $seq .= $codes['A'][$code[$i]];
1542 }
1543 } else {
1544 $p = $parities[$code[0]];
1545 for ($i = 1; $i < $half_len; ++$i) {
1546 $seq .= $codes[$p[$i - 1]][$code[$i]];
1547 }
1548 }
1549 $seq .= '01010';
1550 // center guard bar
1551 for ($i = $half_len; $i < $len; ++$i) {
1552 $seq .= $codes['C'][$code[$i]];
1553 }
1554 $seq .= '101';
1555 // right guard bar
1556 }
1557 $clen = \strlen($seq);
1558 $w = 0;
1559 for ($i = 0; $i < $clen; ++$i) {
1560 $w += 1;
1561 if ($i == $clen - 1 or $i < $clen - 1 and $seq[$i] != $seq[$i + 1]) {
1562 if ($seq[$i] == '1') {
1563 $t = \true;
1564 // bar
1565 } else {
1566 $t = \false;
1567 // space
1568 }
1569 $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
1570 $bararray['maxw'] += $w;
1571 ++$k;
1572 $w = 0;
1573 }
1574 }
1575 return $bararray;
1576 }
1577 /**
1578 * UPC-Based Extensions
1579 * 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers
1580 * 5-Digit Ext.: Used to mark suggested retail price of books
1581 * @param string $code code to represent.
1582 * @param string $len barcode type: 2 = 2-Digit, 5 = 5-Digit
1583 * @return array barcode representation.
1584 * @protected
1585 */
1586 protected function barcode_eanext($code, $len = 5)
1587 {
1588 //Padding
1589 $code = \str_pad($code, $len, '0', \STR_PAD_LEFT);
1590 // calculate check digit
1591 if ($len == 2) {
1592 $r = $code % 4;
1593 } elseif ($len == 5) {
1594 $r = 3 * ($code[0] + $code[2] + $code[4]) + 9 * ($code[1] + $code[3]);
1595 $r %= 10;
1596 } else {
1597 return \false;
1598 }
1599 //Convert digits to bars
1600 $codes = array('A' => array(
1601 // left odd parity
1602 '0' => '0001101',
1603 '1' => '0011001',
1604 '2' => '0010011',
1605 '3' => '0111101',
1606 '4' => '0100011',
1607 '5' => '0110001',
1608 '6' => '0101111',
1609 '7' => '0111011',
1610 '8' => '0110111',
1611 '9' => '0001011',
1612 ), 'B' => array(
1613 // left even parity
1614 '0' => '0100111',
1615 '1' => '0110011',
1616 '2' => '0011011',
1617 '3' => '0100001',
1618 '4' => '0011101',
1619 '5' => '0111001',
1620 '6' => '0000101',
1621 '7' => '0010001',
1622 '8' => '0001001',
1623 '9' => '0010111',
1624 ));
1625 $parities = array();
1626 $parities[2] = array('0' => array('A', 'A'), '1' => array('A', 'B'), '2' => array('B', 'A'), '3' => array('B', 'B'));
1627 $parities[5] = array('0' => array('B', 'B', 'A', 'A', 'A'), '1' => array('B', 'A', 'B', 'A', 'A'), '2' => array('B', 'A', 'A', 'B', 'A'), '3' => array('B', 'A', 'A', 'A', 'B'), '4' => array('A', 'B', 'B', 'A', 'A'), '5' => array('A', 'A', 'B', 'B', 'A'), '6' => array('A', 'A', 'A', 'B', 'B'), '7' => array('A', 'B', 'A', 'B', 'A'), '8' => array('A', 'B', 'A', 'A', 'B'), '9' => array('A', 'A', 'B', 'A', 'B'));
1628 $p = $parities[$len][$r];
1629 $seq = '1011';
1630 // left guard bar
1631 $seq .= $codes[$p[0]][$code[0]];
1632 for ($i = 1; $i < $len; ++$i) {
1633 $seq .= '01';
1634 // separator
1635 $seq .= $codes[$p[$i]][$code[$i]];
1636 }
1637 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
1638 return $this->binseq_to_array($seq, $bararray);
1639 }
1640 /**
1641 * POSTNET and PLANET barcodes.
1642 * Used by U.S. Postal Service for automated mail sorting
1643 * @param string $code zip code to represent. Must be a string containing a zip code of the form DDDDD or DDDDD-DDDD.
1644 * @param boolean $planet if true print the PLANET barcode, otherwise print POSTNET
1645 * @return array barcode representation.
1646 * @protected
1647 */
1648 protected function barcode_postnet($code, $planet = \false)
1649 {
1650 // bar length
1651 if ($planet) {
1652 $barlen = array(0 => array(1, 1, 2, 2, 2), 1 => array(2, 2, 2, 1, 1), 2 => array(2, 2, 1, 2, 1), 3 => array(2, 2, 1, 1, 2), 4 => array(2, 1, 2, 2, 1), 5 => array(2, 1, 2, 1, 2), 6 => array(2, 1, 1, 2, 2), 7 => array(1, 2, 2, 2, 1), 8 => array(1, 2, 2, 1, 2), 9 => array(1, 2, 1, 2, 2));
1653 } else {
1654 $barlen = array(0 => array(2, 2, 1, 1, 1), 1 => array(1, 1, 1, 2, 2), 2 => array(1, 1, 2, 1, 2), 3 => array(1, 1, 2, 2, 1), 4 => array(1, 2, 1, 1, 2), 5 => array(1, 2, 1, 2, 1), 6 => array(1, 2, 2, 1, 1), 7 => array(2, 1, 1, 1, 2), 8 => array(2, 1, 1, 2, 1), 9 => array(2, 1, 2, 1, 1));
1655 }
1656 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
1657 $k = 0;
1658 $code = \str_replace('-', '', $code);
1659 $code = \str_replace(' ', '', $code);
1660 $len = \strlen($code);
1661 // calculate checksum
1662 $sum = 0;
1663 for ($i = 0; $i < $len; ++$i) {
1664 $sum += \intval($code[$i]);
1665 }
1666 $chkd = $sum % 10;
1667 if ($chkd > 0) {
1668 $chkd = 10 - $chkd;
1669 }
1670 $code .= $chkd;
1671 $len = \strlen($code);
1672 // start bar
1673 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
1674 $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
1675 $bararray['maxw'] += 2;
1676 for ($i = 0; $i < $len; ++$i) {
1677 for ($j = 0; $j < 5; ++$j) {
1678 $h = $barlen[$code[$i]][$j];
1679 $p = \floor(1 / $h);
1680 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
1681 $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
1682 $bararray['maxw'] += 2;
1683 }
1684 }
1685 // end bar
1686 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
1687 $bararray['maxw'] += 1;
1688 return $bararray;
1689 }
1690 /**
1691 * RMS4CC - CBC - KIX
1692 * RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) - KIX (Klant index - Customer index)
1693 * RM4SCC is the name of the barcode symbology used by the Royal Mail for its Cleanmail service.
1694 * @param string $code code to print
1695 * @param boolean $kix if true prints the KIX variation (doesn't use the start and end symbols, and the checksum) - in this case the house number must be sufficed with an X and placed at the end of the code.
1696 * @return array barcode representation.
1697 * @protected
1698 */
1699 protected function barcode_rms4cc($code, $kix = \false)
1700 {
1701 $notkix = !$kix;
1702 // bar mode
1703 // 1 = pos 1, length 2
1704 // 2 = pos 1, length 3
1705 // 3 = pos 2, length 1
1706 // 4 = pos 2, length 2
1707 $barmode = array('0' => array(3, 3, 2, 2), '1' => array(3, 4, 1, 2), '2' => array(3, 4, 2, 1), '3' => array(4, 3, 1, 2), '4' => array(4, 3, 2, 1), '5' => array(4, 4, 1, 1), '6' => array(3, 1, 4, 2), '7' => array(3, 2, 3, 2), '8' => array(3, 2, 4, 1), '9' => array(4, 1, 3, 2), 'A' => array(4, 1, 4, 1), 'B' => array(4, 2, 3, 1), 'C' => array(3, 1, 2, 4), 'D' => array(3, 2, 1, 4), 'E' => array(3, 2, 2, 3), 'F' => array(4, 1, 1, 4), 'G' => array(4, 1, 2, 3), 'H' => array(4, 2, 1, 3), 'I' => array(1, 3, 4, 2), 'J' => array(1, 4, 3, 2), 'K' => array(1, 4, 4, 1), 'L' => array(2, 3, 3, 2), 'M' => array(2, 3, 4, 1), 'N' => array(2, 4, 3, 1), 'O' => array(1, 3, 2, 4), 'P' => array(1, 4, 1, 4), 'Q' => array(1, 4, 2, 3), 'R' => array(2, 3, 1, 4), 'S' => array(2, 3, 2, 3), 'T' => array(2, 4, 1, 3), 'U' => array(1, 1, 4, 4), 'V' => array(1, 2, 3, 4), 'W' => array(1, 2, 4, 3), 'X' => array(2, 1, 3, 4), 'Y' => array(2, 1, 4, 3), 'Z' => array(2, 2, 3, 3));
1708 $code = \strtoupper($code);
1709 $len = \strlen($code);
1710 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
1711 if ($notkix) {
1712 // table for checksum calculation (row,col)
1713 $checktable = array('0' => array(1, 1), '1' => array(1, 2), '2' => array(1, 3), '3' => array(1, 4), '4' => array(1, 5), '5' => array(1, 0), '6' => array(2, 1), '7' => array(2, 2), '8' => array(2, 3), '9' => array(2, 4), 'A' => array(2, 5), 'B' => array(2, 0), 'C' => array(3, 1), 'D' => array(3, 2), 'E' => array(3, 3), 'F' => array(3, 4), 'G' => array(3, 5), 'H' => array(3, 0), 'I' => array(4, 1), 'J' => array(4, 2), 'K' => array(4, 3), 'L' => array(4, 4), 'M' => array(4, 5), 'N' => array(4, 0), 'O' => array(5, 1), 'P' => array(5, 2), 'Q' => array(5, 3), 'R' => array(5, 4), 'S' => array(5, 5), 'T' => array(5, 0), 'U' => array(0, 1), 'V' => array(0, 2), 'W' => array(0, 3), 'X' => array(0, 4), 'Y' => array(0, 5), 'Z' => array(0, 0));
1714 $row = 0;
1715 $col = 0;
1716 for ($i = 0; $i < $len; ++$i) {
1717 $row += $checktable[$code[$i]][0];
1718 $col += $checktable[$code[$i]][1];
1719 }
1720 $row %= 6;
1721 $col %= 6;
1722 $chk = \array_keys($checktable, array($row, $col));
1723 $code .= $chk[0];
1724 ++$len;
1725 }
1726 $k = 0;
1727 if ($notkix) {
1728 // start bar
1729 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
1730 $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
1731 $bararray['maxw'] += 2;
1732 }
1733 for ($i = 0; $i < $len; ++$i) {
1734 for ($j = 0; $j < 4; ++$j) {
1735 switch ($barmode[$code[$i]][$j]) {
1736 case 1:
1737 $p = 0;
1738 $h = 2;
1739 break;
1740 case 2:
1741 $p = 0;
1742 $h = 3;
1743 break;
1744 case 3:
1745 $p = 1;
1746 $h = 1;
1747 break;
1748 case 4:
1749 $p = 1;
1750 $h = 2;
1751 break;
1752 }
1753 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
1754 $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
1755 $bararray['maxw'] += 2;
1756 }
1757 }
1758 if ($notkix) {
1759 // stop bar
1760 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0);
1761 $bararray['maxw'] += 1;
1762 }
1763 return $bararray;
1764 }
1765 /**
1766 * CODABAR barcodes.
1767 * Older code often used in library systems, sometimes in blood banks
1768 * @param string $code code to represent.
1769 * @return array barcode representation.
1770 * @protected
1771 */
1772 protected function barcode_codabar($code)
1773 {
1774 $chr = array('0' => '11111221', '1' => '11112211', '2' => '11121121', '3' => '22111111', '4' => '11211211', '5' => '21111211', '6' => '12111121', '7' => '12112111', '8' => '12211111', '9' => '21121111', '-' => '11122111', '$' => '11221111', ':' => '21112121', '/' => '21211121', '.' => '21212111', '+' => '11222221', 'A' => '11221211', 'B' => '12121121', 'C' => '11121221', 'D' => '11122211');
1775 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
1776 $k = 0;
1777 $w = 0;
1778 $seq = '';
1779 $code = 'A' . \strtoupper($code) . 'A';
1780 $len = \strlen($code);
1781 for ($i = 0; $i < $len; ++$i) {
1782 if (!isset($chr[$code[$i]])) {
1783 return \false;
1784 }
1785 $seq = $chr[$code[$i]];
1786 for ($j = 0; $j < 8; ++$j) {
1787 if ($j % 2 == 0) {
1788 $t = \true;
1789 // bar
1790 } else {
1791 $t = \false;
1792 // space
1793 }
1794 $w = (float) $seq[$j];
1795 $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
1796 $bararray['maxw'] += $w;
1797 ++$k;
1798 }
1799 }
1800 return $bararray;
1801 }
1802 /**
1803 * CODE11 barcodes.
1804 * Used primarily for labeling telecommunications equipment
1805 * @param string $code code to represent.
1806 * @return array barcode representation.
1807 * @protected
1808 */
1809 protected function barcode_code11($code)
1810 {
1811 $chr = array('0' => '111121', '1' => '211121', '2' => '121121', '3' => '221111', '4' => '112121', '5' => '212111', '6' => '122111', '7' => '111221', '8' => '211211', '9' => '211111', '-' => '112111', 'S' => '112211');
1812 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
1813 $k = 0;
1814 $w = 0;
1815 $seq = '';
1816 $len = \strlen($code);
1817 // calculate check digit C
1818 $p = 1;
1819 $check = 0;
1820 for ($i = $len - 1; $i >= 0; --$i) {
1821 $digit = $code[$i];
1822 if ($digit == '-') {
1823 $dval = 10;
1824 } else {
1825 $dval = \intval($digit);
1826 }
1827 $check += $dval * $p;
1828 ++$p;
1829 if ($p > 10) {
1830 $p = 1;
1831 }
1832 }
1833 $check %= 11;
1834 if ($check == 10) {
1835 $check = '-';
1836 }
1837 $code .= $check;
1838 if ($len > 10) {
1839 // calculate check digit K
1840 $p = 1;
1841 $check = 0;
1842 for ($i = $len; $i >= 0; --$i) {
1843 $digit = $code[$i];
1844 if ($digit == '-') {
1845 $dval = 10;
1846 } else {
1847 $dval = \intval($digit);
1848 }
1849 $check += $dval * $p;
1850 ++$p;
1851 if ($p > 9) {
1852 $p = 1;
1853 }
1854 }
1855 $check %= 11;
1856 $code .= $check;
1857 ++$len;
1858 }
1859 $code = 'S' . $code . 'S';
1860 $len += 3;
1861 for ($i = 0; $i < $len; ++$i) {
1862 if (!isset($chr[$code[$i]])) {
1863 return \false;
1864 }
1865 $seq = $chr[$code[$i]];
1866 for ($j = 0; $j < 6; ++$j) {
1867 if ($j % 2 == 0) {
1868 $t = \true;
1869 // bar
1870 } else {
1871 $t = \false;
1872 // space
1873 }
1874 $w = (float) $seq[$j];
1875 $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
1876 $bararray['maxw'] += $w;
1877 ++$k;
1878 }
1879 }
1880 return $bararray;
1881 }
1882 /**
1883 * Pharmacode
1884 * Contains digits (0 to 9)
1885 * @param string $code code to represent.
1886 * @return array barcode representation.
1887 * @protected
1888 */
1889 protected function barcode_pharmacode($code)
1890 {
1891 $seq = '';
1892 $code = \intval($code);
1893 while ($code > 0) {
1894 if ($code % 2 == 0) {
1895 $seq .= '11100';
1896 $code -= 2;
1897 } else {
1898 $seq .= '100';
1899 $code -= 1;
1900 }
1901 $code /= 2;
1902 }
1903 $seq = \substr($seq, 0, -2);
1904 $seq = \strrev($seq);
1905 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
1906 return $this->binseq_to_array($seq, $bararray);
1907 }
1908 /**
1909 * Pharmacode two-track
1910 * Contains digits (0 to 9)
1911 * @param string $code code to represent.
1912 * @return array barcode representation.
1913 * @protected
1914 */
1915 protected function barcode_pharmacode2t($code)
1916 {
1917 $seq = '';
1918 $code = \intval($code);
1919 do {
1920 switch ($code % 3) {
1921 case 0:
1922 $seq .= '3';
1923 $code = ($code - 3) / 3;
1924 break;
1925 case 1:
1926 $seq .= '1';
1927 $code = ($code - 1) / 3;
1928 break;
1929 case 2:
1930 $seq .= '2';
1931 $code = ($code - 2) / 3;
1932 break;
1933 }
1934 } while ($code != 0);
1935 $seq = \strrev($seq);
1936 $k = 0;
1937 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
1938 $len = \strlen($seq);
1939 for ($i = 0; $i < $len; ++$i) {
1940 switch ($seq[$i]) {
1941 case '1':
1942 $p = 1;
1943 $h = 1;
1944 break;
1945 case '2':
1946 $p = 0;
1947 $h = 1;
1948 break;
1949 case '3':
1950 $p = 0;
1951 $h = 2;
1952 break;
1953 }
1954 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
1955 $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
1956 $bararray['maxw'] += 2;
1957 }
1958 unset($bararray['bcode'][$k - 1]);
1959 --$bararray['maxw'];
1960 return $bararray;
1961 }
1962 /**
1963 * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
1964 * (requires PHP bcmath extension)
1965 * Intelligent Mail barcode is a 65-bar code for use on mail in the United States.
1966 * The fields are described as follows:<ul><li>The Barcode Identifier shall be assigned by USPS to encode the presort identification that is currently printed in human readable form on the optional endorsement line (OEL) as well as for future USPS use. This shall be two digits, with the second digit in the range of 0–4. The allowable encoding ranges shall be 00–04, 10–14, 20–24, 30–34, 40–44, 50–54, 60–64, 70–74, 80–84, and 90–94.</li><li>The Service Type Identifier shall be assigned by USPS for any combination of services requested on the mailpiece. The allowable encoding range shall be 000http://it2.php.net/manual/en/function.dechex.php–999. Each 3-digit value shall correspond to a particular mail class with a particular combination of service(s). Each service program, such as OneCode Confirm and OneCode ACS, shall provide the list of Service Type Identifier values.</li><li>The Mailer or Customer Identifier shall be assigned by USPS as a unique, 6 or 9 digit number that identifies a business entity. The allowable encoding range for the 6 digit Mailer ID shall be 000000- 899999, while the allowable encoding range for the 9 digit Mailer ID shall be 900000000-999999999.</li><li>The Serial or Sequence Number shall be assigned by the mailer for uniquely identifying and tracking mailpieces. The allowable encoding range shall be 000000000–999999999 when used with a 6 digit Mailer ID and 000000-999999 when used with a 9 digit Mailer ID. e. The Delivery Point ZIP Code shall be assigned by the mailer for routing the mailpiece. This shall replace POSTNET for routing the mailpiece to its final delivery point. The length may be 0, 5, 9, or 11 digits. The allowable encoding ranges shall be no ZIP Code, 00000–99999, 000000000–999999999, and 00000000000–99999999999.</li></ul>
1967 * @param string $code code to print, separate the ZIP (routing code) from the rest using a minus char '-' (BarcodeID_ServiceTypeID_MailerID_SerialNumber-RoutingCode)
1968 * @return array barcode representation.
1969 * @protected
1970 */
1971 protected function barcode_imb($code)
1972 {
1973 $asc_chr = array(4, 0, 2, 6, 3, 5, 1, 9, 8, 7, 1, 2, 0, 6, 4, 8, 2, 9, 5, 3, 0, 1, 3, 7, 4, 6, 8, 9, 2, 0, 5, 1, 9, 4, 3, 8, 6, 7, 1, 2, 4, 3, 9, 5, 7, 8, 3, 0, 2, 1, 4, 0, 9, 1, 7, 0, 2, 4, 6, 3, 7, 1, 9, 5, 8);
1974 $dsc_chr = array(7, 1, 9, 5, 8, 0, 2, 4, 6, 3, 5, 8, 9, 7, 3, 0, 6, 1, 7, 4, 6, 8, 9, 2, 5, 1, 7, 5, 4, 3, 8, 7, 6, 0, 2, 5, 4, 9, 3, 0, 1, 6, 8, 2, 0, 4, 5, 9, 6, 7, 5, 2, 6, 3, 8, 5, 1, 9, 8, 7, 4, 0, 2, 6, 3);
1975 $asc_pos = array(3, 0, 8, 11, 1, 12, 8, 11, 10, 6, 4, 12, 2, 7, 9, 6, 7, 9, 2, 8, 4, 0, 12, 7, 10, 9, 0, 7, 10, 5, 7, 9, 6, 8, 2, 12, 1, 4, 2, 0, 1, 5, 4, 6, 12, 1, 0, 9, 4, 7, 5, 10, 2, 6, 9, 11, 2, 12, 6, 7, 5, 11, 0, 3, 2);
1976 $dsc_pos = array(2, 10, 12, 5, 9, 1, 5, 4, 3, 9, 11, 5, 10, 1, 6, 3, 4, 1, 10, 0, 2, 11, 8, 6, 1, 12, 3, 8, 6, 4, 4, 11, 0, 6, 1, 9, 11, 5, 3, 7, 3, 10, 7, 11, 8, 2, 10, 3, 5, 8, 0, 3, 12, 11, 8, 4, 5, 1, 3, 0, 7, 12, 9, 8, 10);
1977 $code_arr = \explode('-', $code);
1978 $tracking_number = $code_arr[0];
1979 if (isset($code_arr[1])) {
1980 $routing_code = $code_arr[1];
1981 } else {
1982 $routing_code = '';
1983 }
1984 // Conversion of Routing Code
1985 switch (\strlen($routing_code)) {
1986 case 0:
1987 $binary_code = 0;
1988 break;
1989 case 5:
1990 $binary_code = \bcadd($routing_code, '1');
1991 break;
1992 case 9:
1993 $binary_code = \bcadd($routing_code, '100001');
1994 break;
1995 case 11:
1996 $binary_code = \bcadd($routing_code, '1000100001');
1997 break;
1998 default:
1999 return \false;
2000 break;
2001 }
2002 $binary_code = \bcmul($binary_code, 10);
2003 $binary_code = \bcadd($binary_code, $tracking_number[0]);
2004 $binary_code = \bcmul($binary_code, 5);
2005 $binary_code = \bcadd($binary_code, $tracking_number[1]);
2006 $binary_code .= \substr($tracking_number, 2, 18);
2007 // convert to hexadecimal
2008 $binary_code = $this->dec_to_hex($binary_code);
2009 // pad to get 13 bytes
2010 $binary_code = \str_pad($binary_code, 26, '0', \STR_PAD_LEFT);
2011 // convert string to array of bytes
2012 $binary_code_arr = \chunk_split($binary_code, 2, "\r");
2013 $binary_code_arr = \substr($binary_code_arr, 0, -1);
2014 $binary_code_arr = \explode("\r", $binary_code_arr);
2015 // calculate frame check sequence
2016 $fcs = $this->imb_crc11fcs($binary_code_arr);
2017 // exclude first 2 bits from first byte
2018 $first_byte = \sprintf('%2s', \dechex(\hexdec($binary_code_arr[0]) << 2 >> 2));
2019 $binary_code_102bit = $first_byte . \substr($binary_code, 2);
2020 // convert binary data to codewords
2021 $codewords = array();
2022 $data = $this->hex_to_dec($binary_code_102bit);
2023 $codewords[0] = \bcmod($data, 636) * 2;
2024 $data = \bcdiv($data, 636);
2025 for ($i = 1; $i < 9; ++$i) {
2026 $codewords[$i] = \bcmod($data, 1365);
2027 $data = \bcdiv($data, 1365);
2028 }
2029 $codewords[9] = $data;
2030 if ($fcs >> 10 == 1) {
2031 $codewords[9] += 659;
2032 }
2033 // generate lookup tables
2034 $table2of13 = $this->imb_tables(2, 78);
2035 $table5of13 = $this->imb_tables(5, 1287);
2036 // convert codewords to characters
2037 $characters = array();
2038 $bitmask = 512;
2039 foreach ($codewords as $k => $val) {
2040 if ($val <= 1286) {
2041 $chrcode = $table5of13[$val];
2042 } else {
2043 $chrcode = $table2of13[$val - 1287];
2044 }
2045 if (($fcs & $bitmask) > 0) {
2046 // bitwise invert
2047 $chrcode = ~$chrcode & 8191;
2048 }
2049 $characters[] = $chrcode;
2050 $bitmask /= 2;
2051 }
2052 $characters = \array_reverse($characters);
2053 // build bars
2054 $k = 0;
2055 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
2056 for ($i = 0; $i < 65; ++$i) {
2057 $asc = ($characters[$asc_chr[$i]] & \pow(2, $asc_pos[$i])) > 0;
2058 $dsc = ($characters[$dsc_chr[$i]] & \pow(2, $dsc_pos[$i])) > 0;
2059 if ($asc and $dsc) {
2060 // full bar (F)
2061 $p = 0;
2062 $h = 3;
2063 } elseif ($asc) {
2064 // ascender (A)
2065 $p = 0;
2066 $h = 2;
2067 } elseif ($dsc) {
2068 // descender (D)
2069 $p = 1;
2070 $h = 2;
2071 } else {
2072 // tracker (T)
2073 $p = 1;
2074 $h = 1;
2075 }
2076 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
2077 $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
2078 $bararray['maxw'] += 2;
2079 }
2080 unset($bararray['bcode'][$k - 1]);
2081 --$bararray['maxw'];
2082 return $bararray;
2083 }
2084 /**
2085 * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
2086 *
2087 * @param string $code pre-formatted IMB barcode (65 chars "FADT")
2088 * @return array barcode representation.
2089 * @protected
2090 */
2091 protected function barcode_imb_pre($code)
2092 {
2093 if (!\preg_match('/^[fadtFADT]{65}$/', $code) == 1) {
2094 return \false;
2095 }
2096 $characters = \str_split(\strtolower($code), 1);
2097 // build bars
2098 $k = 0;
2099 $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
2100 for ($i = 0; $i < 65; ++$i) {
2101 switch ($characters[$i]) {
2102 case 'f':
2103 // full bar
2104 $p = 0;
2105 $h = 3;
2106 break;
2107 case 'a':
2108 // ascender
2109 $p = 0;
2110 $h = 2;
2111 break;
2112 case 'd':
2113 // descender
2114 $p = 1;
2115 $h = 2;
2116 break;
2117 case 't':
2118 // tracker (short)
2119 $p = 1;
2120 $h = 1;
2121 break;
2122 }
2123 $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
2124 $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
2125 $bararray['maxw'] += 2;
2126 }
2127 unset($bararray['bcode'][$k - 1]);
2128 --$bararray['maxw'];
2129 return $bararray;
2130 }
2131 /**
2132 * Convert large integer number to hexadecimal representation.
2133 * (requires PHP bcmath extension)
2134 * @param string $number number to convert specified as a string
2135 * @return string hexadecimal representation
2136 */
2137 public function dec_to_hex($number)
2138 {
2139 $i = 0;
2140 $hex = array();
2141 if ($number == 0) {
2142 return '00';
2143 }
2144 while ($number > 0) {
2145 if ($number == 0) {
2146 \array_push($hex, '0');
2147 } else {
2148 \array_push($hex, \strtoupper(\dechex(\bcmod($number, '16'))));
2149 $number = \bcdiv($number, '16', 0);
2150 }
2151 }
2152 $hex = \array_reverse($hex);
2153 return \implode($hex);
2154 }
2155 /**
2156 * Convert large hexadecimal number to decimal representation (string).
2157 * (requires PHP bcmath extension)
2158 * @param string $hex hexadecimal number to convert specified as a string
2159 * @return string hexadecimal representation
2160 */
2161 public function hex_to_dec($hex)
2162 {
2163 $dec = 0;
2164 $bitval = 1;
2165 $len = \strlen($hex);
2166 for ($pos = $len - 1; $pos >= 0; --$pos) {
2167 $dec = \bcadd($dec, \bcmul(\hexdec($hex[$pos]), $bitval));
2168 $bitval = \bcmul($bitval, 16);
2169 }
2170 return $dec;
2171 }
2172 /**
2173 * Intelligent Mail Barcode calculation of Frame Check Sequence
2174 * @param string $code_arr array of hexadecimal values (13 bytes holding 102 bits right justified).
2175 * @return int 11 bit Frame Check Sequence as integer (decimal base)
2176 * @protected
2177 */
2178 protected function imb_crc11fcs($code_arr)
2179 {
2180 $genpoly = 0xf35;
2181 // generator polynomial
2182 $fcs = 0x7ff;
2183 // Frame Check Sequence
2184 // do most significant byte skipping the 2 most significant bits
2185 $data = \hexdec($code_arr[0]) << 5;
2186 for ($bit = 2; $bit < 8; ++$bit) {
2187 if (($fcs ^ $data) & 0x400) {
2188 $fcs = $fcs << 1 ^ $genpoly;
2189 } else {
2190 $fcs = $fcs << 1;
2191 }
2192 $fcs &= 0x7ff;
2193 $data <<= 1;
2194 }
2195 // do rest of bytes
2196 for ($byte = 1; $byte < 13; ++$byte) {
2197 $data = \hexdec($code_arr[$byte]) << 3;
2198 for ($bit = 0; $bit < 8; ++$bit) {
2199 if (($fcs ^ $data) & 0x400) {
2200 $fcs = $fcs << 1 ^ $genpoly;
2201 } else {
2202 $fcs = $fcs << 1;
2203 }
2204 $fcs &= 0x7ff;
2205 $data <<= 1;
2206 }
2207 }
2208 return $fcs;
2209 }
2210 /**
2211 * Reverse unsigned short value
2212 * @param int $num value to reversr
2213 * @return int reversed value
2214 * @protected
2215 */
2216 protected function imb_reverse_us($num)
2217 {
2218 $rev = 0;
2219 for ($i = 0; $i < 16; ++$i) {
2220 $rev <<= 1;
2221 $rev |= $num & 1;
2222 $num >>= 1;
2223 }
2224 return $rev;
2225 }
2226 /**
2227 * generate Nof13 tables used for Intelligent Mail Barcode
2228 * @param int $n is the type of table: 2 for 2of13 table, 5 for 5of13table
2229 * @param int $size size of table (78 for n=2 and 1287 for n=5)
2230 * @return array requested table
2231 * @protected
2232 */
2233 protected function imb_tables($n, $size)
2234 {
2235 $table = array();
2236 $lli = 0;
2237 // LUT lower index
2238 $lui = $size - 1;
2239 // LUT upper index
2240 for ($count = 0; $count < 8192; ++$count) {
2241 $bit_count = 0;
2242 for ($bit_index = 0; $bit_index < 13; ++$bit_index) {
2243 $bit_count += \intval(($count & 1 << $bit_index) != 0);
2244 }
2245 // if we don't have the right number of bits on, go on to the next value
2246 if ($bit_count == $n) {
2247 $reverse = $this->imb_reverse_us($count) >> 3;
2248 // if the reverse is less than count, we have already visited this pair before
2249 if ($reverse >= $count) {
2250 // If count is symmetric, place it at the first free slot from the end of the list.
2251 // Otherwise, place it at the first free slot from the beginning of the list AND place $reverse ath the next free slot from the beginning of the list
2252 if ($reverse == $count) {
2253 $table[$lui] = $count;
2254 --$lui;
2255 } else {
2256 $table[$lli] = $count;
2257 ++$lli;
2258 $table[$lli] = $reverse;
2259 ++$lli;
2260 }
2261 }
2262 }
2263 }
2264 return $table;
2265 }
2266 }
2267 // end of class
2268 //============================================================+
2269 // END OF FILE
2270 //============================================================+
2271 }
2272