Int25.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * Retrieve text to encode
  75. * @return string
  76. */
  77. public function getText()
  78. {
  79. if ($this->_withChecksum) {
  80. $text = $this->_getTextWithChecksum();
  81. } else {
  82. $text = $this->_text;
  83. }
  84. return (strlen($text) % 2 ? '0' . $text : $text);
  85. }
  86. /**
  87. * Retrieve text to display
  88. * @return string
  89. */
  90. public function getTextToDisplay()
  91. {
  92. if ($this->_withChecksumInText) {
  93. $text = $this->_getTextWithChecksum();
  94. return (strlen($text) % 2 ? '0' . $text : $text);
  95. }
  96. $text = $this->_text;
  97. if ($this->_withChecksum) {
  98. return (strlen($text) % 2 ? $text : '0' . $text);
  99. }
  100. return (strlen($text) % 2 ? '0' . $text : $text);
  101. }
  102. /**
  103. * Width of the barcode (in pixels)
  104. * @return integer
  105. */
  106. protected function _calculateBarcodeWidth()
  107. {
  108. $quietZone = $this->getQuietZone();
  109. $startCharacter = (4 * $this->_barThinWidth) * $this->_factor;
  110. $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth) * $this->_factor;
  111. $encodedData = strlen($this->getText()) * $characterLength;
  112. $stopCharacter = ($this->_barThickWidth + 2 * $this->_barThinWidth) * $this->_factor;
  113. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  114. }
  115. /**
  116. * Prepare array to draw barcode
  117. * @return array
  118. */
  119. protected function _prepareBarcode()
  120. {
  121. if ($this->_withBearerBars) {
  122. $this->_withBorder = false;
  123. }
  124. // Start character (0000)
  125. $barcodeTable[] = array(1, $this->_barThinWidth, 0, 1);
  126. $barcodeTable[] = array(0, $this->_barThinWidth, 0, 1);
  127. $barcodeTable[] = array(1, $this->_barThinWidth, 0, 1);
  128. $barcodeTable[] = array(0, $this->_barThinWidth, 0, 1);
  129. // Encoded $text
  130. $text = $this->getText();
  131. for ($i = 0; $i < strlen($text); $i += 2) { // Draw 2 chars at a time
  132. $char1 = substr($text, $i, 1);
  133. $char2 = substr($text, $i + 1, 1);
  134. // Interleave
  135. for ($ibar = 0; $ibar < 5; $ibar ++) {
  136. // Draws char1 bar (fore color)
  137. $barWidth = (substr($this->_codingMap[$char1], $ibar, 1))
  138. ? $this->_barThickWidth
  139. : $this->_barThinWidth;
  140. $barcodeTable[] = array(1, $barWidth, 0, 1);
  141. // Left space corresponding to char2 (background color)
  142. $barWidth = (substr($this->_codingMap[$char2], $ibar, 1))
  143. ? $this->_barThickWidth
  144. : $this->_barThinWidth;
  145. $barcodeTable[] = array(0, $barWidth, 0 , 1);
  146. }
  147. }
  148. // Stop character (100)
  149. $barcodeTable[] = array(1 , $this->_barThickWidth, 0, 1);
  150. $barcodeTable[] = array(0 , $this->_barThinWidth, 0, 1);
  151. $barcodeTable[] = array(1 , $this->_barThinWidth, 0, 1);
  152. return $barcodeTable;
  153. }
  154. /**
  155. * Drawing of bearer bars (if enabled)
  156. *
  157. * @return void
  158. */
  159. protected function _postDrawBarcode()
  160. {
  161. if (!$this->_withBearerBars) {
  162. return;
  163. }
  164. $width = $this->_barThickWidth * $this->_factor;
  165. $point1 = $this->_rotate(-1, -1);
  166. $point2 = $this->_rotate($this->_calculateWidth() - 1, -1);
  167. $point3 = $this->_rotate($this->_calculateWidth() - 1, $width - 1);
  168. $point4 = $this->_rotate(-1, $width - 1);
  169. $this->_addPolygon(array(
  170. $point1,
  171. $point2,
  172. $point3,
  173. $point4,
  174. ));
  175. $point1 = $this->_rotate(
  176. 0,
  177. 0 + $this->_barHeight * $this->_factor - 1
  178. );
  179. $point2 = $this->_rotate(
  180. $this->_calculateWidth() - 1,
  181. 0 + $this->_barHeight * $this->_factor - 1
  182. );
  183. $point3 = $this->_rotate(
  184. $this->_calculateWidth() - 1,
  185. 0 + $this->_barHeight * $this->_factor - $width
  186. );
  187. $point4 = $this->_rotate(
  188. 0,
  189. 0 + $this->_barHeight * $this->_factor - $width
  190. );
  191. $this->_addPolygon(array(
  192. $point1,
  193. $point2,
  194. $point3,
  195. $point4,
  196. ));
  197. }
  198. }