|
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: Code39.php 23398 2010-11-19 17:17:05Z mikaelkael $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Barcode_Object_ObjectAbstract |
|
25 */ |
|
26 require_once 'Zend/Barcode/Object/ObjectAbstract.php'; |
|
27 |
|
28 /** |
|
29 * @see 'Zend_Validate_Barcode' |
|
30 */ |
|
31 require_once 'Zend/Validate/Barcode.php'; |
|
32 |
|
33 /** |
|
34 * Class for generate Code39 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_Code39 extends Zend_Barcode_Object_ObjectAbstract |
|
42 { |
|
43 /** |
|
44 * Coding map |
|
45 * @var array |
|
46 */ |
|
47 protected $_codingMap = array( |
|
48 '0' => '000110100', |
|
49 '1' => '100100001', |
|
50 '2' => '001100001', |
|
51 '3' => '101100000', |
|
52 '4' => '000110001', |
|
53 '5' => '100110000', |
|
54 '6' => '001110000', |
|
55 '7' => '000100101', |
|
56 '8' => '100100100', |
|
57 '9' => '001100100', |
|
58 'A' => '100001001', |
|
59 'B' => '001001001', |
|
60 'C' => '101001000', |
|
61 'D' => '000011001', |
|
62 'E' => '100011000', |
|
63 'F' => '001011000', |
|
64 'G' => '000001101', |
|
65 'H' => '100001100', |
|
66 'I' => '001001100', |
|
67 'J' => '000011100', |
|
68 'K' => '100000011', |
|
69 'L' => '001000011', |
|
70 'M' => '101000010', |
|
71 'N' => '000010011', |
|
72 'O' => '100010010', |
|
73 'P' => '001010010', |
|
74 'Q' => '000000111', |
|
75 'R' => '100000110', |
|
76 'S' => '001000110', |
|
77 'T' => '000010110', |
|
78 'U' => '110000001', |
|
79 'V' => '011000001', |
|
80 'W' => '111000000', |
|
81 'X' => '010010001', |
|
82 'Y' => '110010000', |
|
83 'Z' => '011010000', |
|
84 '-' => '010000101', |
|
85 '.' => '110000100', |
|
86 ' ' => '011000100', |
|
87 '$' => '010101000', |
|
88 '/' => '010100010', |
|
89 '+' => '010001010', |
|
90 '%' => '000101010', |
|
91 '*' => '010010100', |
|
92 ); |
|
93 |
|
94 /** |
|
95 * Partial check of Code39 barcode |
|
96 * @return void |
|
97 */ |
|
98 protected function _checkParams() |
|
99 { |
|
100 $this->_checkRatio(); |
|
101 } |
|
102 |
|
103 /** |
|
104 * Width of the barcode (in pixels) |
|
105 * @return int |
|
106 */ |
|
107 protected function _calculateBarcodeWidth() |
|
108 { |
|
109 $quietZone = $this->getQuietZone(); |
|
110 $characterLength = (6 * $this->_barThinWidth + 3 * $this->_barThickWidth + 1) * $this->_factor; |
|
111 $encodedData = strlen($this->getText()) * $characterLength - $this->_factor; |
|
112 return $quietZone + $encodedData + $quietZone; |
|
113 } |
|
114 |
|
115 /** |
|
116 * Set text to encode |
|
117 * @param string $value |
|
118 * @return Zend_Barcode_Object |
|
119 */ |
|
120 public function setText($value) |
|
121 { |
|
122 $this->_text = $value; |
|
123 return $this; |
|
124 } |
|
125 |
|
126 /** |
|
127 * Retrieve text to display |
|
128 * @return string |
|
129 */ |
|
130 public function getText() |
|
131 { |
|
132 return '*' . parent::getText() . '*'; |
|
133 } |
|
134 |
|
135 /** |
|
136 * Retrieve text to display |
|
137 * @return string |
|
138 */ |
|
139 public function getTextToDisplay() |
|
140 { |
|
141 $text = parent::getTextToDisplay(); |
|
142 if (substr($text, 0, 1) != '*' && substr($text, -1) != '*') { |
|
143 return '*' . $text . '*'; |
|
144 } else { |
|
145 return $text; |
|
146 } |
|
147 } |
|
148 |
|
149 /** |
|
150 * Prepare array to draw barcode |
|
151 * @return array |
|
152 */ |
|
153 protected function _prepareBarcode() |
|
154 { |
|
155 $text = str_split($this->getText()); |
|
156 $barcodeTable = array(); |
|
157 foreach ($text as $char) { |
|
158 $barcodeChar = str_split($this->_codingMap[$char]); |
|
159 $visible = true; |
|
160 foreach ($barcodeChar as $c) { |
|
161 /* visible, width, top, length */ |
|
162 $width = $c ? $this->_barThickWidth : $this->_barThinWidth; |
|
163 $barcodeTable[] = array((int) $visible, $width, 0, 1); |
|
164 $visible = ! $visible; |
|
165 } |
|
166 $barcodeTable[] = array(0 , 1); |
|
167 } |
|
168 return $barcodeTable; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Get barcode checksum |
|
173 * |
|
174 * @param string $text |
|
175 * @return int |
|
176 */ |
|
177 public function getChecksum($text) |
|
178 { |
|
179 $this->_checkText($text); |
|
180 $text = str_split($text); |
|
181 $charset = array_flip(array_keys($this->_codingMap)); |
|
182 $checksum = 0; |
|
183 foreach ($text as $character) { |
|
184 $checksum += $charset[$character]; |
|
185 } |
|
186 return array_search(($checksum % 43), $charset); |
|
187 } |
|
188 } |