Int25.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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-2009 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. /** @see Zend_Barcode_Object_Code25 */
  23. require_once 'Zend/Barcode/Object/Code25.php';
  24. /** @see Zend_Validate_Barcode */
  25. require_once 'Zend/Validate/Barcode.php';
  26. /**
  27. * Class for generate Interleaved 2 of 5 barcode
  28. *
  29. * @category Zend
  30. * @package Zend_Barcode
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Barcode_Object_Int25 extends Zend_Barcode_Object_Code25
  35. {
  36. /**
  37. * Drawing of bearer bars
  38. * @var boolean
  39. */
  40. private $_withBearerBars = false;
  41. /**
  42. * @var $_barcodeLength integer | string
  43. */
  44. protected $_barcodeLength = 'even';
  45. /**
  46. * Activate/deactivate drawing of bearer bars
  47. * @param boolean $value
  48. * @return Zend_Barcode_Object_Int25
  49. */
  50. public function setWithBearerBars($value)
  51. {
  52. $this->_withBearerBars = (bool) $value;
  53. return $this;
  54. }
  55. /**
  56. * Retrieve if bearer bars are enabled
  57. * @return boolean
  58. */
  59. public function getWithBearerBars()
  60. {
  61. return $this->_withBearerBars;
  62. }
  63. /**
  64. * Check allowed characters
  65. * @param string $value
  66. * @return string
  67. * @throw Zend_Barcode_Object_Exception
  68. */
  69. public function validateText($value)
  70. {
  71. $this->_validateText($value, array('automaticPrepend' => 0, 'validator' => 'code25'));
  72. }
  73. /**
  74. * Width of the barcode (in pixels)
  75. * @return integer
  76. */
  77. protected function _calculateBarcodeWidth()
  78. {
  79. $quietZone = $this->getQuietZone();
  80. $startCharacter = (4 * $this->_barThinWidth) * $this->_factor;
  81. $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth) * $this->_factor;
  82. $encodedData = strlen($this->getText()) * $characterLength;
  83. $stopCharacter = ($this->_barThickWidth + 2 * $this->_barThinWidth) * $this->_factor;
  84. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  85. }
  86. /**
  87. * Prepare array to draw barcode
  88. * @return array
  89. */
  90. protected function _prepareBarcode()
  91. {
  92. if ($this->_withBearerBars) {
  93. $this->_withBorder = false;
  94. }
  95. // Start character (0000)
  96. $barcodeTable[] = array(1, $this->_barThinWidth, 0, 1);
  97. $barcodeTable[] = array(0, $this->_barThinWidth, 0, 1);
  98. $barcodeTable[] = array(1, $this->_barThinWidth, 0, 1);
  99. $barcodeTable[] = array(0, $this->_barThinWidth, 0, 1);
  100. // Encoded $text
  101. $text = $this->getText();
  102. for ($i = 0; $i < strlen($text); $i += 2) { // Draw 2 chars at a time
  103. $char1 = substr($text, $i, 1);
  104. $char2 = substr($text, $i + 1, 1);
  105. // Interleave
  106. for ($ibar = 0; $ibar < 5; $ibar ++) {
  107. // Draws char1 bar (fore color)
  108. $barWidth = (substr($this->_codingMap[$char1], $ibar, 1))
  109. ? $this->_barThickWidth
  110. : $this->_barThinWidth;
  111. $barcodeTable[] = array(1, $barWidth, 0, 1);
  112. // Left space corresponding to char2 (background color)
  113. $barWidth = (substr($this->_codingMap[$char2], $ibar, 1))
  114. ? $this->_barThickWidth
  115. : $this->_barThinWidth;
  116. $barcodeTable[] = array(0, $barWidth, 0 , 1);
  117. }
  118. }
  119. // Stop character (100)
  120. $barcodeTable[] = array(1 , $this->_barThickWidth, 0, 1);
  121. $barcodeTable[] = array(0 , $this->_barThinWidth, 0, 1);
  122. $barcodeTable[] = array(1 , $this->_barThinWidth, 0, 1);
  123. return $barcodeTable;
  124. }
  125. /**
  126. * Drawing of bearer bars (if enabled)
  127. *
  128. * @return void
  129. */
  130. protected function _postDrawBarcode()
  131. {
  132. if (!$this->_withBearerBars) {
  133. return;
  134. }
  135. $width = $this->_barThickWidth * $this->_factor;
  136. $point1 = $this->_rotate(-1, -1);
  137. $point2 = $this->_rotate($this->_calculateWidth() - 1, -1);
  138. $point3 = $this->_rotate($this->_calculateWidth() - 1, $width - 1);
  139. $point4 = $this->_rotate(-1, $width - 1);
  140. $this->_addPolygon(array(
  141. $point1,
  142. $point2,
  143. $point3,
  144. $point4,
  145. ));
  146. $point1 = $this->_rotate(
  147. 0,
  148. 0 + $this->_barHeight * $this->_factor - 1
  149. );
  150. $point2 = $this->_rotate(
  151. $this->_calculateWidth() - 1,
  152. 0 + $this->_barHeight * $this->_factor - 1
  153. );
  154. $point3 = $this->_rotate(
  155. $this->_calculateWidth() - 1,
  156. 0 + $this->_barHeight * $this->_factor - $width
  157. );
  158. $point4 = $this->_rotate(
  159. 0,
  160. 0 + $this->_barHeight * $this->_factor - $width
  161. );
  162. $this->_addPolygon(array(
  163. $point1,
  164. $point2,
  165. $point3,
  166. $point4,
  167. ));
  168. }
  169. }