Code25interleaved.php 5.4 KB

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