Ean13.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2010 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. }
  80. /**
  81. * Width of the barcode (in pixels)
  82. * @return integer
  83. */
  84. protected function _calculateBarcodeWidth()
  85. {
  86. $quietZone = $this->getQuietZone();
  87. $startCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  88. $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
  89. $stopCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  90. $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 12;
  91. return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
  92. }
  93. /**
  94. * Partial check of interleaved EAN/UPC barcode
  95. * @return void
  96. */
  97. protected function _checkParams()
  98. {}
  99. /**
  100. * Prepare array to draw barcode
  101. * @return array
  102. */
  103. protected function _prepareBarcode()
  104. {
  105. $barcodeTable = array();
  106. $height = ($this->_drawText) ? 1.1 : 1;
  107. // Start character (101)
  108. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  109. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  110. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  111. $textTable = str_split($this->getText());
  112. $parity = $this->_parities[$textTable[0]];
  113. // First part
  114. for ($i = 1; $i < 7; $i++) {
  115. $bars = str_split($this->_codingMap[$parity[$i - 1]][$textTable[$i]]);
  116. foreach ($bars as $b) {
  117. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  118. }
  119. }
  120. // Middle character (01010)
  121. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  122. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  123. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  124. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  125. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  126. // Second part
  127. for ($i = 7; $i < 13; $i++) {
  128. $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
  129. foreach ($bars as $b) {
  130. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  131. }
  132. }
  133. // Stop character (101)
  134. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  135. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  136. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  137. return $barcodeTable;
  138. }
  139. /**
  140. * Get barcode checksum
  141. *
  142. * @param string $text
  143. * @return int
  144. */
  145. public function getChecksum($text)
  146. {
  147. $this->_checkText($text);
  148. $factor = 3;
  149. $checksum = 0;
  150. for ($i = strlen($text); $i > 0; $i --) {
  151. $checksum += intval($text{$i - 1}) * $factor;
  152. $factor = 4 - $factor;
  153. }
  154. $checksum = (10 - ($checksum % 10)) % 10;
  155. return $checksum;
  156. }
  157. /**
  158. * Partial function to draw text
  159. * @return void
  160. */
  161. protected function _drawText()
  162. {
  163. if (get_class($this) == 'Zend_Barcode_Object_Ean13') {
  164. $this->_drawEan13Text();
  165. } else {
  166. parent::_drawText();
  167. }
  168. }
  169. protected function _drawEan13Text()
  170. {
  171. if ($this->_drawText) {
  172. $text = $this->getTextToDisplay();
  173. $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
  174. $leftPosition = $this->getQuietZone() - $characterWidth;
  175. for ($i = 0; $i < $this->_barcodeLength; $i ++) {
  176. $this->_addText(
  177. $text{$i},
  178. $this->_fontSize * $this->_factor,
  179. $this->_rotate(
  180. $leftPosition,
  181. (int) $this->_withBorder * 2
  182. + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
  183. ),
  184. $this->_font,
  185. $this->_foreColor,
  186. 'left',
  187. - $this->_orientation
  188. );
  189. switch ($i) {
  190. case 0:
  191. $factor = 3;
  192. break;
  193. case 6:
  194. $factor = 4;
  195. break;
  196. default:
  197. $factor = 0;
  198. }
  199. $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
  200. }
  201. }
  202. }
  203. }