Int25.php 5.6 KB

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