2
0

Code25interleaved.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_Code25interleaved 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. * Width of the barcode (in pixels)
  66. * @return integer
  67. */
  68. protected function _calculateBarcodeWidth()
  69. {
  70. $quietZone = $this->getQuietZone();
  71. $startCharacter = (4 * $this->_barThinWidth) * $this->_factor;
  72. $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth) * $this->_factor;
  73. $encodedData = strlen($this->getText()) * $characterLength;
  74. $stopCharacter = ($this->_barThickWidth + 2 * $this->_barThinWidth) * $this->_factor;
  75. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  76. }
  77. /**
  78. * Prepare array to draw barcode
  79. * @return array
  80. */
  81. protected function _prepareBarcode()
  82. {
  83. if ($this->_withBearerBars) {
  84. $this->_withBorder = false;
  85. }
  86. // Start character (0000)
  87. $barcodeTable[] = array(1, $this->_barThinWidth, 0, 1);
  88. $barcodeTable[] = array(0, $this->_barThinWidth, 0, 1);
  89. $barcodeTable[] = array(1, $this->_barThinWidth, 0, 1);
  90. $barcodeTable[] = array(0, $this->_barThinWidth, 0, 1);
  91. // Encoded $text
  92. $text = $this->getText();
  93. for ($i = 0; $i < strlen($text); $i += 2) { // Draw 2 chars at a time
  94. $char1 = substr($text, $i, 1);
  95. $char2 = substr($text, $i + 1, 1);
  96. // Interleave
  97. for ($ibar = 0; $ibar < 5; $ibar ++) {
  98. // Draws char1 bar (fore color)
  99. $barWidth = (substr($this->_codingMap[$char1], $ibar, 1))
  100. ? $this->_barThickWidth
  101. : $this->_barThinWidth;
  102. $barcodeTable[] = array(1, $barWidth, 0, 1);
  103. // Left space corresponding to char2 (background color)
  104. $barWidth = (substr($this->_codingMap[$char2], $ibar, 1))
  105. ? $this->_barThickWidth
  106. : $this->_barThinWidth;
  107. $barcodeTable[] = array(0, $barWidth, 0 , 1);
  108. }
  109. }
  110. // Stop character (100)
  111. $barcodeTable[] = array(1 , $this->_barThickWidth, 0, 1);
  112. $barcodeTable[] = array(0 , $this->_barThinWidth, 0, 1);
  113. $barcodeTable[] = array(1 , $this->_barThinWidth, 0, 1);
  114. return $barcodeTable;
  115. }
  116. /**
  117. * Drawing of bearer bars (if enabled)
  118. *
  119. * @return void
  120. */
  121. protected function _postDrawBarcode()
  122. {
  123. if (!$this->_withBearerBars) {
  124. return;
  125. }
  126. $width = $this->_barThickWidth * $this->_factor;
  127. $point1 = $this->_rotate(-1, -1);
  128. $point2 = $this->_rotate($this->_calculateWidth() - 1, -1);
  129. $point3 = $this->_rotate($this->_calculateWidth() - 1, $width - 1);
  130. $point4 = $this->_rotate(-1, $width - 1);
  131. $this->_addPolygon(array(
  132. $point1,
  133. $point2,
  134. $point3,
  135. $point4,
  136. ));
  137. $point1 = $this->_rotate(
  138. 0,
  139. 0 + $this->_barHeight * $this->_factor - 1
  140. );
  141. $point2 = $this->_rotate(
  142. $this->_calculateWidth() - 1,
  143. 0 + $this->_barHeight * $this->_factor - 1
  144. );
  145. $point3 = $this->_rotate(
  146. $this->_calculateWidth() - 1,
  147. 0 + $this->_barHeight * $this->_factor - $width
  148. );
  149. $point4 = $this->_rotate(
  150. 0,
  151. 0 + $this->_barHeight * $this->_factor - $width
  152. );
  153. $this->_addPolygon(array(
  154. $point1,
  155. $point2,
  156. $point3,
  157. $point4,
  158. ));
  159. }
  160. }