|
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: Postnet.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 Postnet 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_Postnet extends Zend_Barcode_Object_ObjectAbstract |
|
42 { |
|
43 |
|
44 /** |
|
45 * Coding map |
|
46 * - 0 = half bar |
|
47 * - 1 = complete bar |
|
48 * @var array |
|
49 */ |
|
50 protected $_codingMap = array( |
|
51 0 => "11000", |
|
52 1 => "00011", |
|
53 2 => "00101", |
|
54 3 => "00110", |
|
55 4 => "01001", |
|
56 5 => "01010", |
|
57 6 => "01100", |
|
58 7 => "10001", |
|
59 8 => "10010", |
|
60 9 => "10100" |
|
61 ); |
|
62 |
|
63 /** |
|
64 * Default options for Postnet barcode |
|
65 * @return void |
|
66 */ |
|
67 protected function _getDefaultOptions() |
|
68 { |
|
69 $this->_barThinWidth = 2; |
|
70 $this->_barHeight = 20; |
|
71 $this->_drawText = false; |
|
72 $this->_stretchText = true; |
|
73 $this->_mandatoryChecksum = true; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Width of the barcode (in pixels) |
|
78 * @return integer |
|
79 */ |
|
80 protected function _calculateBarcodeWidth() |
|
81 { |
|
82 $quietZone = $this->getQuietZone(); |
|
83 $startCharacter = (2 * $this->_barThinWidth) * $this->_factor; |
|
84 $stopCharacter = (1 * $this->_barThinWidth) * $this->_factor; |
|
85 $encodedData = (10 * $this->_barThinWidth) * $this->_factor * strlen($this->getText()); |
|
86 return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone; |
|
87 } |
|
88 |
|
89 /** |
|
90 * Partial check of interleaved Postnet barcode |
|
91 * @return void |
|
92 */ |
|
93 protected function _checkParams() |
|
94 {} |
|
95 |
|
96 /** |
|
97 * Prepare array to draw barcode |
|
98 * @return array |
|
99 */ |
|
100 protected function _prepareBarcode() |
|
101 { |
|
102 $barcodeTable = array(); |
|
103 |
|
104 // Start character (1) |
|
105 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1); |
|
106 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1); |
|
107 |
|
108 // Text to encode |
|
109 $textTable = str_split($this->getText()); |
|
110 foreach ($textTable as $char) { |
|
111 $bars = str_split($this->_codingMap[$char]); |
|
112 foreach ($bars as $b) { |
|
113 $barcodeTable[] = array(1 , $this->_barThinWidth , 0.5 - $b * 0.5 , 1); |
|
114 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1); |
|
115 } |
|
116 } |
|
117 |
|
118 // Stop character (1) |
|
119 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1); |
|
120 return $barcodeTable; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Get barcode checksum |
|
125 * |
|
126 * @param string $text |
|
127 * @return int |
|
128 */ |
|
129 public function getChecksum($text) |
|
130 { |
|
131 $this->_checkText($text); |
|
132 $sum = array_sum(str_split($text)); |
|
133 $checksum = (10 - ($sum % 10)) % 10; |
|
134 return $checksum; |
|
135 } |
|
136 } |