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

/Zend/Barcode/Object/Ean13.php

https://gitlab.com/luisrepo/ClienteWS
PHP | 225 lines | 133 code | 21 blank | 71 comment | 9 complexity | 2d278674027d57e5b1545e8f265f6cb6 MD5 | raw file
  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: Ean13.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Barcode_Object_ObjectAbstract
  24. */
  25. require_once 'Zend/Barcode/Object/ObjectAbstract.php';
  26. /**
  27. * @see Zend_Validate_Barcode
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate Ean13 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_Ean13 extends Zend_Barcode_Object_ObjectAbstract
  39. {
  40. /**
  41. * Coding map
  42. * - 0 = narrow bar
  43. * - 1 = wide bar
  44. * @var array
  45. */
  46. protected $_codingMap = array(
  47. 'A' => array(
  48. 0 => "0001101", 1 => "0011001", 2 => "0010011", 3 => "0111101", 4 => "0100011",
  49. 5 => "0110001", 6 => "0101111", 7 => "0111011", 8 => "0110111", 9 => "0001011"
  50. ),
  51. 'B' => array(
  52. 0 => "0100111", 1 => "0110011", 2 => "0011011", 3 => "0100001", 4 => "0011101",
  53. 5 => "0111001", 6 => "0000101", 7 => "0010001", 8 => "0001001", 9 => "0010111"
  54. ),
  55. 'C' => array(
  56. 0 => "1110010", 1 => "1100110", 2 => "1101100", 3 => "1000010", 4 => "1011100",
  57. 5 => "1001110", 6 => "1010000", 7 => "1000100", 8 => "1001000", 9 => "1110100"
  58. ));
  59. protected $_parities = array(
  60. 0 => array('A','A','A','A','A','A'),
  61. 1 => array('A','A','B','A','B','B'),
  62. 2 => array('A','A','B','B','A','B'),
  63. 3 => array('A','A','B','B','B','A'),
  64. 4 => array('A','B','A','A','B','B'),
  65. 5 => array('A','B','B','A','A','B'),
  66. 6 => array('A','B','B','B','A','A'),
  67. 7 => array('A','B','A','B','A','B'),
  68. 8 => array('A','B','A','B','B','A'),
  69. 9 => array('A','B','B','A','B','A')
  70. );
  71. /**
  72. * Default options for Postnet barcode
  73. * @return void
  74. */
  75. protected function _getDefaultOptions()
  76. {
  77. $this->_barcodeLength = 13;
  78. $this->_mandatoryChecksum = true;
  79. $this->_mandatoryQuietZones = true;
  80. }
  81. /**
  82. * Width of the barcode (in pixels)
  83. * @return integer
  84. */
  85. protected function _calculateBarcodeWidth()
  86. {
  87. $quietZone = $this->getQuietZone();
  88. $startCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  89. $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
  90. $stopCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  91. $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 12;
  92. return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
  93. }
  94. /**
  95. * Partial check of interleaved EAN/UPC barcode
  96. * @return void
  97. */
  98. protected function _checkParams()
  99. {}
  100. /**
  101. * Prepare array to draw barcode
  102. * @return array
  103. */
  104. protected function _prepareBarcode()
  105. {
  106. $barcodeTable = array();
  107. $height = ($this->_drawText) ? 1.1 : 1;
  108. // Start character (101)
  109. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  110. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  111. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  112. $textTable = str_split($this->getText());
  113. $parity = $this->_parities[$textTable[0]];
  114. // First part
  115. for ($i = 1; $i < 7; $i++) {
  116. $bars = str_split($this->_codingMap[$parity[$i - 1]][$textTable[$i]]);
  117. foreach ($bars as $b) {
  118. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  119. }
  120. }
  121. // Middle character (01010)
  122. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  123. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  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. // Second part
  128. for ($i = 7; $i < 13; $i++) {
  129. $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
  130. foreach ($bars as $b) {
  131. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  132. }
  133. }
  134. // Stop character (101)
  135. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  136. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  137. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  138. return $barcodeTable;
  139. }
  140. /**
  141. * Get barcode checksum
  142. *
  143. * @param string $text
  144. * @return int
  145. */
  146. public function getChecksum($text)
  147. {
  148. $this->_checkText($text);
  149. $factor = 3;
  150. $checksum = 0;
  151. for ($i = strlen($text); $i > 0; $i --) {
  152. $checksum += intval($text{$i - 1}) * $factor;
  153. $factor = 4 - $factor;
  154. }
  155. $checksum = (10 - ($checksum % 10)) % 10;
  156. return $checksum;
  157. }
  158. /**
  159. * Partial function to draw text
  160. * @return void
  161. */
  162. protected function _drawText()
  163. {
  164. if (get_class($this) == 'Zend_Barcode_Object_Ean13') {
  165. $this->_drawEan13Text();
  166. } else {
  167. parent::_drawText();
  168. }
  169. }
  170. protected function _drawEan13Text()
  171. {
  172. if ($this->_drawText) {
  173. $text = $this->getTextToDisplay();
  174. $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
  175. $leftPosition = $this->getQuietZone() - $characterWidth;
  176. for ($i = 0; $i < $this->_barcodeLength; $i ++) {
  177. $this->_addText(
  178. $text{$i},
  179. $this->_fontSize * $this->_factor,
  180. $this->_rotate(
  181. $leftPosition,
  182. (int) $this->_withBorder * 2
  183. + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
  184. ),
  185. $this->_font,
  186. $this->_foreColor,
  187. 'left',
  188. - $this->_orientation
  189. );
  190. switch ($i) {
  191. case 0:
  192. $factor = 3;
  193. break;
  194. case 6:
  195. $factor = 4;
  196. break;
  197. default:
  198. $factor = 0;
  199. }
  200. $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
  201. }
  202. }
  203. }
  204. }