3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Barcode\
Object;
12 use Zend\Validator\Barcode
as BarcodeValidator
;
15 * Class for generate UpcA barcode
17 class Upce
extends Ean13
20 protected $parities = array(
22 0 => array('B','B','B','A','A','A'),
23 1 => array('B','B','A','B','A','A'),
24 2 => array('B','B','A','A','B','A'),
25 3 => array('B','B','A','A','A','B'),
26 4 => array('B','A','B','B','A','A'),
27 5 => array('B','A','A','B','B','A'),
28 6 => array('B','A','A','A','B','B'),
29 7 => array('B','A','B','A','B','A'),
30 8 => array('B','A','B','A','A','B'),
31 9 => array('B','A','A','B','A','B')),
33 0 => array('A','A','A','B','B','B'),
34 1 => array('A','A','B','A','B','B'),
35 2 => array('A','A','B','B','A','B'),
36 3 => array('A','A','B','B','B','A'),
37 4 => array('A','B','A','A','B','B'),
38 5 => array('A','B','B','A','A','B'),
39 6 => array('A','B','B','B','A','A'),
40 7 => array('A','B','A','B','A','B'),
41 8 => array('A','B','A','B','B','A'),
42 9 => array('A','B','B','A','B','A'))
46 * Default options for Postnet barcode
49 protected function getDefaultOptions()
51 $this->barcodeLength
= 8;
52 $this->mandatoryChecksum
= true;
53 $this->mandatoryQuietZones
= true;
57 * Retrieve text to encode
60 public function getText()
62 $text = parent
::getText();
70 * Width of the barcode (in pixels)
73 protected function calculateBarcodeWidth()
75 $quietZone = $this->getQuietZone();
76 $startCharacter = (3 * $this->barThinWidth
) * $this->factor
;
77 $stopCharacter = (6 * $this->barThinWidth
) * $this->factor
;
78 $encodedData = (7 * $this->barThinWidth
) * $this->factor
* 6;
79 return $quietZone +
$startCharacter +
$encodedData +
$stopCharacter +
$quietZone;
83 * Prepare array to draw barcode
86 protected function prepareBarcode()
88 $barcodeTable = array();
89 $height = ($this->drawText
) ?
1.1 : 1;
91 // Start character (101)
92 $barcodeTable[] = array(1, $this->barThinWidth
, 0, $height);
93 $barcodeTable[] = array(0, $this->barThinWidth
, 0, $height);
94 $barcodeTable[] = array(1, $this->barThinWidth
, 0, $height);
96 $textTable = str_split($this->getText());
98 if ($textTable[0] == 1) {
101 $checksum = $textTable[7];
102 $parity = $this->parities
[$system][$checksum];
104 for ($i = 1; $i < 7; $i++
) {
105 $bars = str_split($this->codingMap
[$parity[$i - 1]][$textTable[$i]]);
106 foreach ($bars as $b) {
107 $barcodeTable[] = array($b, $this->barThinWidth
, 0, 1);
111 // Stop character (10101)
112 $barcodeTable[] = array(0, $this->barThinWidth
, 0, $height);
113 $barcodeTable[] = array(1, $this->barThinWidth
, 0, $height);
114 $barcodeTable[] = array(0, $this->barThinWidth
, 0, $height);
115 $barcodeTable[] = array(1, $this->barThinWidth
, 0, $height);
116 $barcodeTable[] = array(0, $this->barThinWidth
, 0, $height);
117 $barcodeTable[] = array(1, $this->barThinWidth
, 0, $height);
118 return $barcodeTable;
122 * Partial function to draw text
125 protected function drawText()
127 if ($this->drawText
) {
128 $text = $this->getTextToDisplay();
129 $characterWidth = (7 * $this->barThinWidth
) * $this->factor
;
130 $leftPosition = $this->getQuietZone() - $characterWidth;
131 for ($i = 0; $i < $this->barcodeLength
; $i ++
) {
132 $fontSize = $this->fontSize
;
133 if ($i == 0 ||
$i == 7) {
138 $fontSize * $this->factor
,
141 (int) $this->withBorder
* 2
142 +
$this->factor
* ($this->barHeight +
$fontSize) +
1
159 $leftPosition = $leftPosition +
$characterWidth +
($factor * $this->barThinWidth
* $this->factor
);
165 * Particular validation for Upce barcode objects
166 * (to suppress checksum character substitution)
168 * @param string $value
169 * @param array $options
170 * @throws Exception\BarcodeValidationException
172 protected function validateSpecificText($value, $options = array())
174 $validator = new BarcodeValidator(array(
179 $value = $this->addLeadingZeros($value, true);
181 if (!$validator->isValid($value)) {
182 $message = implode("\n", $validator->getMessages());
183 throw new Exception\
BarcodeValidationException($message);
188 * Get barcode checksum
190 * @param string $text
193 public function getChecksum($text)
195 $text = $this->addLeadingZeros($text, true);
199 return parent
::getChecksum($text);