Ean8.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-2015 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_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 Ean8 barcode
  32. *
  33. * @category Zend
  34. * @package Zend_Barcode
  35. * @copyright Copyright (c) 2005-2015 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_Ean8 extends Zend_Barcode_Object_Ean13
  39. {
  40. /**
  41. * Default options for Postnet barcode
  42. * @return void
  43. */
  44. protected function _getDefaultOptions()
  45. {
  46. $this->_barcodeLength = 8;
  47. $this->_mandatoryChecksum = true;
  48. }
  49. /**
  50. * Width of the barcode (in pixels)
  51. * @return integer
  52. */
  53. protected function _calculateBarcodeWidth()
  54. {
  55. $quietZone = $this->getQuietZone();
  56. $startCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  57. $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
  58. $stopCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  59. $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 8;
  60. return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
  61. }
  62. /**
  63. * Prepare array to draw barcode
  64. * @return array
  65. */
  66. protected function _prepareBarcode()
  67. {
  68. $barcodeTable = array();
  69. $height = ($this->_drawText) ? 1.1 : 1;
  70. // Start character (101)
  71. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  72. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  73. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  74. $textTable = str_split($this->getText());
  75. // First part
  76. for ($i = 0; $i < 4; $i++) {
  77. $bars = str_split($this->_codingMap['A'][$textTable[$i]]);
  78. foreach ($bars as $b) {
  79. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  80. }
  81. }
  82. // Middle character (01010)
  83. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  84. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  85. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  86. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  87. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  88. // Second part
  89. for ($i = 4; $i < 8; $i++) {
  90. $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
  91. foreach ($bars as $b) {
  92. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  93. }
  94. }
  95. // Stop character (101)
  96. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  97. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  98. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  99. return $barcodeTable;
  100. }
  101. /**
  102. * Partial function to draw text
  103. * @return void
  104. */
  105. protected function _drawText()
  106. {
  107. if ($this->_drawText) {
  108. $text = $this->getTextToDisplay();
  109. $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
  110. $leftPosition = $this->getQuietZone() + (3 * $this->_barThinWidth) * $this->_factor;
  111. for ($i = 0; $i < $this->_barcodeLength; $i ++) {
  112. $this->_addText(
  113. $text{$i},
  114. $this->_fontSize * $this->_factor,
  115. $this->_rotate(
  116. $leftPosition,
  117. (int) $this->_withBorder * 2
  118. + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
  119. ),
  120. $this->_font,
  121. $this->_foreColor,
  122. 'left',
  123. - $this->_orientation
  124. );
  125. switch ($i) {
  126. case 3:
  127. $factor = 4;
  128. break;
  129. default:
  130. $factor = 0;
  131. }
  132. $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
  133. }
  134. }
  135. }
  136. /**
  137. * Particular validation for Ean8 barcode objects
  138. * (to suppress checksum character substitution)
  139. *
  140. * @param string $value
  141. * @param array $options
  142. * @throws Zend_Barcode_Object_Exception
  143. */
  144. protected function _validateText($value, $options = array())
  145. {
  146. $validator = new Zend_Validate_Barcode(array(
  147. 'adapter' => 'ean8',
  148. 'checksum' => false,
  149. ));
  150. $value = $this->_addLeadingZeros($value, true);
  151. if (!$validator->isValid($value)) {
  152. $message = implode("\n", $validator->getMessages());
  153. /**
  154. * @see Zend_Barcode_Object_Exception
  155. */
  156. require_once 'Zend/Barcode/Object/Exception.php';
  157. throw new Zend_Barcode_Object_Exception($message);
  158. }
  159. }
  160. }