|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Barcode |
|
17 * @subpackage Object |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Upce.php 21667 2010-03-28 17:45:14Z mikaelkael $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Barcode_Object_Ean13 |
|
25 */ |
|
26 require_once 'Zend/Barcode/Object/Ean13.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Validate_Barcode |
|
30 */ |
|
31 require_once 'Zend/Validate/Barcode.php'; |
|
32 |
|
33 /** |
|
34 * Class for generate UpcA barcode |
|
35 * |
|
36 * @category Zend |
|
37 * @package Zend_Barcode |
|
38 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
39 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
40 */ |
|
41 class Zend_Barcode_Object_Upce extends Zend_Barcode_Object_Ean13 |
|
42 { |
|
43 |
|
44 protected $_parities = array( |
|
45 0 => array( |
|
46 0 => array('B','B','B','A','A','A'), |
|
47 1 => array('B','B','A','B','A','A'), |
|
48 2 => array('B','B','A','A','B','A'), |
|
49 3 => array('B','B','A','A','A','B'), |
|
50 4 => array('B','A','B','B','A','A'), |
|
51 5 => array('B','A','A','B','B','A'), |
|
52 6 => array('B','A','A','A','B','B'), |
|
53 7 => array('B','A','B','A','B','A'), |
|
54 8 => array('B','A','B','A','A','B'), |
|
55 9 => array('B','A','A','B','A','B')), |
|
56 1 => array( |
|
57 0 => array('A','A','A','B','B','B'), |
|
58 1 => array('A','A','B','A','B','B'), |
|
59 2 => array('A','A','B','B','A','B'), |
|
60 3 => array('A','A','B','B','B','A'), |
|
61 4 => array('A','B','A','A','B','B'), |
|
62 5 => array('A','B','B','A','A','B'), |
|
63 6 => array('A','B','B','B','A','A'), |
|
64 7 => array('A','B','A','B','A','B'), |
|
65 8 => array('A','B','A','B','B','A'), |
|
66 9 => array('A','B','B','A','B','A')) |
|
67 ); |
|
68 |
|
69 /** |
|
70 * Default options for Postnet barcode |
|
71 * @return void |
|
72 */ |
|
73 protected function _getDefaultOptions() |
|
74 { |
|
75 $this->_barcodeLength = 8; |
|
76 $this->_mandatoryChecksum = true; |
|
77 $this->_mandatoryQuietZones = true; |
|
78 } |
|
79 |
|
80 /** |
|
81 * Retrieve text to encode |
|
82 * @return string |
|
83 */ |
|
84 public function getText() |
|
85 { |
|
86 $text = parent::getText(); |
|
87 if ($text{0} != 1) { |
|
88 $text{0} = 0; |
|
89 } |
|
90 return $text; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Width of the barcode (in pixels) |
|
95 * @return integer |
|
96 */ |
|
97 protected function _calculateBarcodeWidth() |
|
98 { |
|
99 $quietZone = $this->getQuietZone(); |
|
100 $startCharacter = (3 * $this->_barThinWidth) * $this->_factor; |
|
101 $stopCharacter = (6 * $this->_barThinWidth) * $this->_factor; |
|
102 $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 6; |
|
103 return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone; |
|
104 } |
|
105 |
|
106 /** |
|
107 * Prepare array to draw barcode |
|
108 * @return array |
|
109 */ |
|
110 protected function _prepareBarcode() |
|
111 { |
|
112 $barcodeTable = array(); |
|
113 $height = ($this->_drawText) ? 1.1 : 1; |
|
114 |
|
115 // Start character (101) |
|
116 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height); |
|
117 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height); |
|
118 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height); |
|
119 |
|
120 $textTable = str_split($this->getText()); |
|
121 $system = 0; |
|
122 if ($textTable[0] == 1) { |
|
123 $system = 1; |
|
124 } |
|
125 $checksum = $textTable[7]; |
|
126 $parity = $this->_parities[$system][$checksum]; |
|
127 |
|
128 for ($i = 1; $i < 7; $i++) { |
|
129 $bars = str_split($this->_codingMap[$parity[$i - 1]][$textTable[$i]]); |
|
130 foreach ($bars as $b) { |
|
131 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1); |
|
132 } |
|
133 } |
|
134 |
|
135 // Stop character (10101) |
|
136 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height); |
|
137 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height); |
|
138 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height); |
|
139 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height); |
|
140 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height); |
|
141 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height); |
|
142 return $barcodeTable; |
|
143 } |
|
144 |
|
145 /** |
|
146 * Partial function to draw text |
|
147 * @return void |
|
148 */ |
|
149 protected function _drawText() |
|
150 { |
|
151 if ($this->_drawText) { |
|
152 $text = $this->getTextToDisplay(); |
|
153 $characterWidth = (7 * $this->_barThinWidth) * $this->_factor; |
|
154 $leftPosition = $this->getQuietZone() - $characterWidth; |
|
155 for ($i = 0; $i < $this->_barcodeLength; $i ++) { |
|
156 $fontSize = $this->_fontSize; |
|
157 if ($i == 0 || $i == 7) { |
|
158 $fontSize *= 0.8; |
|
159 } |
|
160 $this->_addText( |
|
161 $text{$i}, |
|
162 $fontSize * $this->_factor, |
|
163 $this->_rotate( |
|
164 $leftPosition, |
|
165 (int) $this->_withBorder * 2 |
|
166 + $this->_factor * ($this->_barHeight + $fontSize) + 1 |
|
167 ), |
|
168 $this->_font, |
|
169 $this->_foreColor, |
|
170 'left', |
|
171 - $this->_orientation |
|
172 ); |
|
173 switch ($i) { |
|
174 case 0: |
|
175 $factor = 3; |
|
176 break; |
|
177 case 6: |
|
178 $factor = 5; |
|
179 break; |
|
180 default: |
|
181 $factor = 0; |
|
182 } |
|
183 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor); |
|
184 } |
|
185 } |
|
186 } |
|
187 |
|
188 /** |
|
189 * Particular validation for Upce barcode objects |
|
190 * (to suppress checksum character substitution) |
|
191 * @param string $value |
|
192 * @param array $options |
|
193 */ |
|
194 protected function _validateText($value, $options = array()) |
|
195 { |
|
196 $validator = new Zend_Validate_Barcode(array( |
|
197 'adapter' => 'upce', |
|
198 'checksum' => false, |
|
199 )); |
|
200 |
|
201 $value = $this->_addLeadingZeros($value, true); |
|
202 |
|
203 if (!$validator->isValid($value)) { |
|
204 $message = implode("\n", $validator->getMessages()); |
|
205 |
|
206 /** |
|
207 * @see Zend_Barcode_Object_Exception |
|
208 */ |
|
209 require_once 'Zend/Barcode/Object/Exception.php'; |
|
210 throw new Zend_Barcode_Object_Exception($message); |
|
211 } |
|
212 } |
|
213 |
|
214 /** |
|
215 * Get barcode checksum |
|
216 * |
|
217 * @param string $text |
|
218 * @return int |
|
219 */ |
|
220 public function getChecksum($text) |
|
221 { |
|
222 $text = $this->_addLeadingZeros($text, true); |
|
223 if ($text{0} != 1) { |
|
224 $text{0} = 0; |
|
225 } |
|
226 return parent::getChecksum($text); |
|
227 } |
|
228 } |