Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Barcode / Object / Upce.php
blob228f8693d572c7f5b4a025888a3ff824677a8a3d
1 <?php
2 /**
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
8 */
10 namespace Zend\Barcode\Object;
12 use Zend\Validator\Barcode as BarcodeValidator;
14 /**
15 * Class for generate UpcA barcode
17 class Upce extends Ean13
20 protected $parities = array(
21 0 => 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')),
32 1 => array(
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'))
45 /**
46 * Default options for Postnet barcode
47 * @return void
49 protected function getDefaultOptions()
51 $this->barcodeLength = 8;
52 $this->mandatoryChecksum = true;
53 $this->mandatoryQuietZones = true;
56 /**
57 * Retrieve text to encode
58 * @return string
60 public function getText()
62 $text = parent::getText();
63 if ($text{0} != 1) {
64 $text{0} = 0;
66 return $text;
69 /**
70 * Width of the barcode (in pixels)
71 * @return int
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;
82 /**
83 * Prepare array to draw barcode
84 * @return array
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());
97 $system = 0;
98 if ($textTable[0] == 1) {
99 $system = 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
123 * @return void
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) {
134 $fontSize *= 0.8;
136 $this->addText(
137 $text{$i},
138 $fontSize * $this->factor,
139 $this->rotate(
140 $leftPosition,
141 (int) $this->withBorder * 2
142 + $this->factor * ($this->barHeight + $fontSize) + 1
144 $this->font,
145 $this->foreColor,
146 'left',
147 - $this->orientation
149 switch ($i) {
150 case 0:
151 $factor = 3;
152 break;
153 case 6:
154 $factor = 5;
155 break;
156 default:
157 $factor = 0;
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(
175 'adapter' => 'upce',
176 'checksum' => false,
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
191 * @return int
193 public function getChecksum($text)
195 $text = $this->addLeadingZeros($text, true);
196 if ($text{0} != 1) {
197 $text{0} = 0;
199 return parent::getChecksum($text);