/library/Zend/Barcode/Object/Upca.php
PHP | 172 lines | 100 code | 15 blank | 57 comment | 9 complexity | 4a17f6666a835722c48e49f2998cfb7a MD5 | raw file
Possible License(s): AGPL-1.0
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-2012 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Upca.php 24594 2012-01-05 21:27:01Z matthew $
21 */
22
23/**
24 * @see Zend_Barcode_Object_Ean13
25 */
26require_once 'Zend/Barcode/Object/Ean13.php';
27
28/**
29 * @see Zend_Validate_Barcode
30 */
31require_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-2012 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
40 */
41class Zend_Barcode_Object_Upca extends Zend_Barcode_Object_Ean13
42{
43
44 /**
45 * Default options for Postnet barcode
46 * @return void
47 */
48 protected function _getDefaultOptions()
49 {
50 $this->_barcodeLength = 12;
51 $this->_mandatoryChecksum = true;
52 $this->_mandatoryQuietZones = true;
53 }
54
55 /**
56 * Width of the barcode (in pixels)
57 * @return integer
58 */
59 protected function _calculateBarcodeWidth()
60 {
61 $quietZone = $this->getQuietZone();
62 $startCharacter = (3 * $this->_barThinWidth) * $this->_factor;
63 $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
64 $stopCharacter = (3 * $this->_barThinWidth) * $this->_factor;
65 $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 12;
66 return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
67 }
68
69 /**
70 * Prepare array to draw barcode
71 * @return array
72 */
73 protected function _prepareBarcode()
74 {
75 $barcodeTable = array();
76 $height = ($this->_drawText) ? 1.1 : 1;
77
78 // Start character (101)
79 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
80 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
81 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
82
83 $textTable = str_split($this->getText());
84
85 // First character
86 $bars = str_split($this->_codingMap['A'][$textTable[0]]);
87 foreach ($bars as $b) {
88 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
89 }
90
91 // First part
92 for ($i = 1; $i < 6; $i++) {
93 $bars = str_split($this->_codingMap['A'][$textTable[$i]]);
94 foreach ($bars as $b) {
95 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
96 }
97 }
98
99 // Middle character (01010)
100 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
101 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
102 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
103 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
104 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
105
106 // Second part
107 for ($i = 6; $i < 11; $i++) {
108 $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
109 foreach ($bars as $b) {
110 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
111 }
112 }
113
114 // Last character
115 $bars = str_split($this->_codingMap['C'][$textTable[11]]);
116 foreach ($bars as $b) {
117 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
118 }
119
120 // Stop character (101)
121 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
122 $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
123 $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
124 return $barcodeTable;
125 }
126
127 /**
128 * Partial function to draw text
129 * @return void
130 */
131 protected function _drawText()
132 {
133 if ($this->_drawText) {
134 $text = $this->getTextToDisplay();
135 $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
136 $leftPosition = $this->getQuietZone() - $characterWidth;
137 for ($i = 0; $i < $this->_barcodeLength; $i ++) {
138 $fontSize = $this->_fontSize;
139 if ($i == 0 || $i == 11) {
140 $fontSize *= 0.8;
141 }
142 $this->_addText(
143 $text{$i},
144 $fontSize * $this->_factor,
145 $this->_rotate(
146 $leftPosition,
147 (int) $this->_withBorder * 2
148 + $this->_factor * ($this->_barHeight + $fontSize) + 1
149 ),
150 $this->_font,
151 $this->_foreColor,
152 'left',
153 - $this->_orientation
154 );
155 switch ($i) {
156 case 0:
157 $factor = 10;
158 break;
159 case 5:
160 $factor = 4;
161 break;
162 case 10:
163 $factor = 11;
164 break;
165 default:
166 $factor = 0;
167 }
168 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
169 }
170 }
171 }
172}