PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Barcode/Object/Upce.php

http://github.com/michael-romer/zf-boilerplate
PHP | 228 lines | 139 code | 18 blank | 71 comment | 15 complexity | ce2c3206249917f4f52f295d945c5a22 MD5 | raw file
Possible License(s): Unlicense, Apache-2.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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Upce.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Barcode_Object_Ean13
  24. */
  25. require_once 'Zend/Barcode/Object/Ean13.php';
  26. /**
  27. * @see Zend_Validate_Barcode
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate UpcA barcode
  32. *
  33. * @category Zend
  34. * @package Zend_Barcode
  35. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Barcode_Object_Upce extends Zend_Barcode_Object_Ean13
  39. {
  40. protected $_parities = array(
  41. 0 => array(
  42. 0 => array('B','B','B','A','A','A'),
  43. 1 => array('B','B','A','B','A','A'),
  44. 2 => array('B','B','A','A','B','A'),
  45. 3 => array('B','B','A','A','A','B'),
  46. 4 => array('B','A','B','B','A','A'),
  47. 5 => array('B','A','A','B','B','A'),
  48. 6 => array('B','A','A','A','B','B'),
  49. 7 => array('B','A','B','A','B','A'),
  50. 8 => array('B','A','B','A','A','B'),
  51. 9 => array('B','A','A','B','A','B')),
  52. 1 => array(
  53. 0 => array('A','A','A','B','B','B'),
  54. 1 => array('A','A','B','A','B','B'),
  55. 2 => array('A','A','B','B','A','B'),
  56. 3 => array('A','A','B','B','B','A'),
  57. 4 => array('A','B','A','A','B','B'),
  58. 5 => array('A','B','B','A','A','B'),
  59. 6 => array('A','B','B','B','A','A'),
  60. 7 => array('A','B','A','B','A','B'),
  61. 8 => array('A','B','A','B','B','A'),
  62. 9 => array('A','B','B','A','B','A'))
  63. );
  64. /**
  65. * Default options for Postnet barcode
  66. * @return void
  67. */
  68. protected function _getDefaultOptions()
  69. {
  70. $this->_barcodeLength = 8;
  71. $this->_mandatoryChecksum = true;
  72. $this->_mandatoryQuietZones = true;
  73. }
  74. /**
  75. * Retrieve text to encode
  76. * @return string
  77. */
  78. public function getText()
  79. {
  80. $text = parent::getText();
  81. if ($text{0} != 1) {
  82. $text{0} = 0;
  83. }
  84. return $text;
  85. }
  86. /**
  87. * Width of the barcode (in pixels)
  88. * @return integer
  89. */
  90. protected function _calculateBarcodeWidth()
  91. {
  92. $quietZone = $this->getQuietZone();
  93. $startCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  94. $stopCharacter = (6 * $this->_barThinWidth) * $this->_factor;
  95. $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 6;
  96. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  97. }
  98. /**
  99. * Prepare array to draw barcode
  100. * @return array
  101. */
  102. protected function _prepareBarcode()
  103. {
  104. $barcodeTable = array();
  105. $height = ($this->_drawText) ? 1.1 : 1;
  106. // Start character (101)
  107. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  108. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  109. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  110. $textTable = str_split($this->getText());
  111. $system = 0;
  112. if ($textTable[0] == 1) {
  113. $system = 1;
  114. }
  115. $checksum = $textTable[7];
  116. $parity = $this->_parities[$system][$checksum];
  117. for ($i = 1; $i < 7; $i++) {
  118. $bars = str_split($this->_codingMap[$parity[$i - 1]][$textTable[$i]]);
  119. foreach ($bars as $b) {
  120. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  121. }
  122. }
  123. // Stop character (10101)
  124. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  125. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  126. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  127. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  128. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  129. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  130. return $barcodeTable;
  131. }
  132. /**
  133. * Partial function to draw text
  134. * @return void
  135. */
  136. protected function _drawText()
  137. {
  138. if ($this->_drawText) {
  139. $text = $this->getTextToDisplay();
  140. $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
  141. $leftPosition = $this->getQuietZone() - $characterWidth;
  142. for ($i = 0; $i < $this->_barcodeLength; $i ++) {
  143. $fontSize = $this->_fontSize;
  144. if ($i == 0 || $i == 7) {
  145. $fontSize *= 0.8;
  146. }
  147. $this->_addText(
  148. $text{$i},
  149. $fontSize * $this->_factor,
  150. $this->_rotate(
  151. $leftPosition,
  152. (int) $this->_withBorder * 2
  153. + $this->_factor * ($this->_barHeight + $fontSize) + 1
  154. ),
  155. $this->_font,
  156. $this->_foreColor,
  157. 'left',
  158. - $this->_orientation
  159. );
  160. switch ($i) {
  161. case 0:
  162. $factor = 3;
  163. break;
  164. case 6:
  165. $factor = 5;
  166. break;
  167. default:
  168. $factor = 0;
  169. }
  170. $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
  171. }
  172. }
  173. }
  174. /**
  175. * Particular validation for Upce barcode objects
  176. * (to suppress checksum character substitution)
  177. * @param string $value
  178. * @param array $options
  179. */
  180. protected function _validateText($value, $options = array())
  181. {
  182. $validator = new Zend_Validate_Barcode(array(
  183. 'adapter' => 'upce',
  184. 'checksum' => false,
  185. ));
  186. $value = $this->_addLeadingZeros($value, true);
  187. if (!$validator->isValid($value)) {
  188. $message = implode("\n", $validator->getMessages());
  189. /**
  190. * @see Zend_Barcode_Object_Exception
  191. */
  192. require_once 'Zend/Barcode/Object/Exception.php';
  193. throw new Zend_Barcode_Object_Exception($message);
  194. }
  195. }
  196. /**
  197. * Get barcode checksum
  198. *
  199. * @param string $text
  200. * @return int
  201. */
  202. public function getChecksum($text)
  203. {
  204. $text = $this->_addLeadingZeros($text, true);
  205. if ($text{0} != 1) {
  206. $text{0} = 0;
  207. }
  208. return parent::getChecksum($text);
  209. }
  210. }