Ean8.php 5.6 KB

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