Int25.php 6.3 KB

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