|
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: Code25.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
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 Interleaved 2 of 5 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_Code25 extends Zend_Barcode_Object_ObjectAbstract |
|
42 { |
|
43 /** |
|
44 * Coding map |
|
45 * - 0 = narrow bar |
|
46 * - 1 = wide bar |
|
47 * @var array |
|
48 */ |
|
49 protected $_codingMap = array( |
|
50 '0' => '00110', |
|
51 '1' => '10001', |
|
52 '2' => '01001', |
|
53 '3' => '11000', |
|
54 '4' => '00101', |
|
55 '5' => '10100', |
|
56 '6' => '01100', |
|
57 '7' => '00011', |
|
58 '8' => '10010', |
|
59 '9' => '01010', |
|
60 ); |
|
61 |
|
62 /** |
|
63 * Width of the barcode (in pixels) |
|
64 * @return integer |
|
65 */ |
|
66 protected function _calculateBarcodeWidth() |
|
67 { |
|
68 $quietZone = $this->getQuietZone(); |
|
69 $startCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor; |
|
70 $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth + 5 * $this->_barThinWidth) |
|
71 * $this->_factor; |
|
72 $encodedData = strlen($this->getText()) * $characterLength; |
|
73 $stopCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor; |
|
74 return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Partial check of interleaved 2 of 5 barcode |
|
79 * @return void |
|
80 */ |
|
81 protected function _checkParams() |
|
82 { |
|
83 $this->_checkRatio(); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Prepare array to draw barcode |
|
88 * @return array |
|
89 */ |
|
90 protected function _prepareBarcode() |
|
91 { |
|
92 $barcodeTable = array(); |
|
93 |
|
94 // Start character (30301) |
|
95 $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1); |
|
96 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1); |
|
97 $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1); |
|
98 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1); |
|
99 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1); |
|
100 $barcodeTable[] = array(0 , 1); |
|
101 |
|
102 $text = str_split($this->getText()); |
|
103 foreach ($text as $char) { |
|
104 $barcodeChar = str_split($this->_codingMap[$char]); |
|
105 foreach ($barcodeChar as $c) { |
|
106 /* visible, width, top, length */ |
|
107 $width = $c ? $this->_barThickWidth : $this->_barThinWidth; |
|
108 $barcodeTable[] = array(1 , $width , 0 , 1); |
|
109 $barcodeTable[] = array(0 , 1); |
|
110 } |
|
111 } |
|
112 |
|
113 // Stop character (30103) |
|
114 $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1); |
|
115 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1); |
|
116 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1); |
|
117 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1); |
|
118 $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1); |
|
119 return $barcodeTable; |
|
120 } |
|
121 |
|
122 /** |
|
123 * Get barcode checksum |
|
124 * |
|
125 * @param string $text |
|
126 * @return int |
|
127 */ |
|
128 public function getChecksum($text) |
|
129 { |
|
130 $this->_checkText($text); |
|
131 $factor = 3; |
|
132 $checksum = 0; |
|
133 |
|
134 for ($i = strlen($text); $i > 0; $i --) { |
|
135 $checksum += intval($text{$i - 1}) * $factor; |
|
136 $factor = 4 - $factor; |
|
137 } |
|
138 |
|
139 $checksum = (10 - ($checksum % 10)) % 10; |
|
140 |
|
141 return $checksum; |
|
142 } |
|
143 } |