1<?php
2
3namespace jucksearm\barcode\lib;
4
5use jucksearm\barcode\lib\Datamatrix;
6use jucksearm\barcode\lib\PDF417;
7use jucksearm\barcode\lib\QRcode;
8
9//============================================================+
10// File name   : tcpdf_barcodes_2d.php
11// Version     : 1.0.015
12// Begin       : 2009-04-07
13// Last Update : 2014-05-20
14// Author      : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
15// License     : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
16// -------------------------------------------------------------------
17// Copyright (C) 2009-2014 Nicola Asuni - Tecnick.com LTD
18//
19// This file is part of TCPDF software library.
20//
21// TCPDF is free software: you can redistribute it and/or modify it
22// under the terms of the GNU Lesser General Public License as
23// published by the Free Software Foundation, either version 3 of the
24// License, or (at your option) any later version.
25//
26// TCPDF is distributed in the hope that it will be useful, but
27// WITHOUT ANY WARRANTY; without even the implied warranty of
28// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29// See the GNU Lesser General Public License for more details.
30//
31// You should have received a copy of the GNU Lesser General Public License
32// along with TCPDF.  If not, see <http://www.gnu.org/licenses/>.
33//
34// See LICENSE.TXT file for more information.
35// -------------------------------------------------------------------
36//
37// Description : PHP class to creates array representations for
38//               2D barcodes to be used with TCPDF.
39//
40//============================================================+
41
42/**
43 * @file
44 * PHP class to creates array representations for 2D barcodes to be used with TCPDF.
45 * @package com.tecnick.tcpdf
46 * @author Nicola Asuni
47 * @version 1.0.015
48 */
49
50/**
51 * @class Barcode2D replace TCPDF2DBarcode
52 * PHP class to creates array representations for 2D barcodes to be used with TCPDF (http://www.tcpdf.org).
53 * @package com.tecnick.tcpdf
54 * @version 1.0.015
55 * @author Nicola Asuni
56 */
57class Barcode2D
58{
59
60	/**
61	 * Array representation of barcode.
62	 * @protected
63	 */
64	protected $barcode_array = false;
65
66	/**
67	 * This is the class constructor.
68	 * Return an array representations for 2D barcodes:<ul>
69	 * <li>$arrcode['code'] code to be printed on text label</li>
70	 * <li>$arrcode['num_rows'] required number of rows</li>
71	 * <li>$arrcode['num_cols'] required number of columns</li>
72	 * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
73	 * @param $code (string) code to print
74 	 * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
75	 */
76	public function __construct($code, $type) {
77		$this->setBarcode($code, $type);
78	}
79
80	/**
81	 * Return an array representations of barcode.
82 	 * @return array
83	 */
84	public function getBarcodeArray() {
85		return $this->barcode_array;
86	}
87
88	/**
89	 * Set the barcode.
90	 * @param $code (string) code to print
91 	 * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
92 	 * @return array
93	 */
94	public function setBarcode($code, $type) {
95		$mode = explode(',', $type);
96		$qrtype = strtoupper($mode[0]);
97		switch ($qrtype) {
98			case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022)
99				// require_once(dirname(__FILE__).'/include/barcodes/datamatrix.php');
100				$qrcode = new Datamatrix($code);
101				$this->barcode_array = $qrcode->getBarcodeArray();
102				$this->barcode_array['code'] = $code;
103				break;
104			}
105			case 'PDF417': { // PDF417 (ISO/IEC 15438:2006)
106				// require_once(dirname(__FILE__).'/include/barcodes/pdf417.php');
107				if (!isset($mode[1]) OR ($mode[1] === '')) {
108					$aspectratio = 2; // default aspect ratio (width / height)
109				} else {
110					$aspectratio = floatval($mode[1]);
111				}
112				if (!isset($mode[2]) OR ($mode[2] === '')) {
113					$ecl = -1; // default error correction level (auto)
114				} else {
115					$ecl = intval($mode[2]);
116				}
117				// set macro block
118				$macro = array();
119				if (isset($mode[3]) AND ($mode[3] !== '') AND isset($mode[4]) AND ($mode[4] !== '') AND isset($mode[5]) AND ($mode[5] !== '')) {
120					$macro['segment_total'] = intval($mode[3]);
121					$macro['segment_index'] = intval($mode[4]);
122					$macro['file_id'] = strtr($mode[5], "\xff", ',');
123					for ($i = 0; $i < 7; ++$i) {
124						$o = $i + 6;
125						if (isset($mode[$o]) AND ($mode[$o] !== '')) {
126							// add option
127							$macro['option_'.$i] = strtr($mode[$o], "\xff", ',');
128						}
129					}
130				}
131				$qrcode = new PDF417($code, $ecl, $aspectratio, $macro);
132				$this->barcode_array = $qrcode->getBarcodeArray();
133				$this->barcode_array['code'] = $code;
134				break;
135			}
136			case 'QRCODE': { // QR-CODE
137				// require_once(dirname(__FILE__).'/include/barcodes/qrcode.php');
138				if (!isset($mode[1]) OR (!in_array($mode[1],array('L','M','Q','H')))) {
139					$mode[1] = 'L'; // Ddefault: Low error correction
140				}
141				$qrcode = new QRcode($code, strtoupper($mode[1]));
142				$this->barcode_array = $qrcode->getBarcodeArray();
143				$this->barcode_array['code'] = $code;
144				break;
145			}
146			case 'RAW':
147			case 'RAW2': { // RAW MODE
148				// remove spaces
149				$code = preg_replace('/[\s]*/si', '', $code);
150				if (strlen($code) < 3) {
151					break;
152				}
153				if ($qrtype == 'RAW') {
154					// comma-separated rows
155					$rows = explode(',', $code);
156				} else { // RAW2
157					// rows enclosed in square parentheses
158					$code = substr($code, 1, -1);
159					$rows = explode('][', $code);
160				}
161				$this->barcode_array['num_rows'] = count($rows);
162				$this->barcode_array['num_cols'] = strlen($rows[0]);
163				$this->barcode_array['bcode'] = array();
164				foreach ($rows as $r) {
165					$this->barcode_array['bcode'][] = str_split($r, 1);
166				}
167				$this->barcode_array['code'] = $code;
168				break;
169			}
170			case 'TEST': { // TEST MODE
171				$this->barcode_array['num_rows'] = 5;
172				$this->barcode_array['num_cols'] = 15;
173				$this->barcode_array['bcode'] = array(
174					array(1,1,1,0,1,1,1,0,1,1,1,0,1,1,1),
175					array(0,1,0,0,1,0,0,0,1,0,0,0,0,1,0),
176					array(0,1,0,0,1,1,0,0,1,1,1,0,0,1,0),
177					array(0,1,0,0,1,0,0,0,0,0,1,0,0,1,0),
178					array(0,1,0,0,1,1,1,0,1,1,1,0,0,1,0));
179				$this->barcode_array['code'] = $code;
180				break;
181			}
182			default: {
183				$this->barcode_array = false;
184			}
185		}
186	}
187} // end of class
188
189//============================================================+
190// END OF FILE
191//============================================================+