Code39.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  23. * @see Zend_Barcode_Object_ObjectAbstract
  24. */
  25. require_once 'Zend/Barcode/Object/ObjectAbstract.php';
  26. /**
  27. * @see 'Zend_Validate_Barcode'
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate Code39 barcode
  32. *
  33. * @category Zend
  34. * @package Zend_Barcode
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Barcode_Object_Code39 extends Zend_Barcode_Object_ObjectAbstract
  39. {
  40. /**
  41. * Coding map
  42. * @var array
  43. */
  44. protected $_codingMap = array(
  45. '0' => '000110100',
  46. '1' => '100100001',
  47. '2' => '001100001',
  48. '3' => '101100000',
  49. '4' => '000110001',
  50. '5' => '100110000',
  51. '6' => '001110000',
  52. '7' => '000100101',
  53. '8' => '100100100',
  54. '9' => '001100100',
  55. 'A' => '100001001',
  56. 'B' => '001001001',
  57. 'C' => '101001000',
  58. 'D' => '000011001',
  59. 'E' => '100011000',
  60. 'F' => '001011000',
  61. 'G' => '000001101',
  62. 'H' => '100001100',
  63. 'I' => '001001100',
  64. 'J' => '000011100',
  65. 'K' => '100000011',
  66. 'L' => '001000011',
  67. 'M' => '101000010',
  68. 'N' => '000010011',
  69. 'O' => '100010010',
  70. 'P' => '001010010',
  71. 'Q' => '000000111',
  72. 'R' => '100000110',
  73. 'S' => '001000110',
  74. 'T' => '000010110',
  75. 'U' => '110000001',
  76. 'V' => '011000001',
  77. 'W' => '111000000',
  78. 'X' => '010010001',
  79. 'Y' => '110010000',
  80. 'Z' => '011010000',
  81. '-' => '010000101',
  82. '.' => '110000100',
  83. ' ' => '011000100',
  84. '$' => '010101000',
  85. '/' => '010100010',
  86. '+' => '010001010',
  87. '%' => '000101010',
  88. '*' => '010010100',
  89. );
  90. /**
  91. * Partial check of Code39 barcode
  92. * @return void
  93. */
  94. protected function _checkParams()
  95. {
  96. $this->_checkRatio();
  97. }
  98. /**
  99. * Width of the barcode (in pixels)
  100. * @return int
  101. */
  102. protected function _calculateBarcodeWidth()
  103. {
  104. $quietZone = $this->getQuietZone();
  105. $characterLength = (6 * $this->_barThinWidth + 3 * $this->_barThickWidth + 1) * $this->_factor;
  106. $encodedData = strlen($this->getText()) * $characterLength - $this->_factor;
  107. return $quietZone + $encodedData + $quietZone;
  108. }
  109. /**
  110. * Set text to encode
  111. * @param string $value
  112. * @return Zend_Barcode_Object
  113. */
  114. public function setText($value)
  115. {
  116. $this->_text = $value;
  117. return $this;
  118. }
  119. /**
  120. * Retrieve text to display
  121. * @return string
  122. */
  123. public function getText()
  124. {
  125. return '*' . parent::getText() . '*';
  126. }
  127. /**
  128. * Retrieve text to display
  129. * @return string
  130. */
  131. public function getTextToDisplay()
  132. {
  133. $text = parent::getTextToDisplay();
  134. if (substr($text, 0, 1) != '*' && substr($text, -1) != '*') {
  135. return '*' . $text . '*';
  136. } else {
  137. return $text;
  138. }
  139. }
  140. /**
  141. * Prepare array to draw barcode
  142. * @return array
  143. */
  144. protected function _prepareBarcode()
  145. {
  146. $text = str_split($this->getText());
  147. $barcodeTable = array();
  148. foreach ($text as $char) {
  149. $barcodeChar = str_split($this->_codingMap[$char]);
  150. $visible = true;
  151. foreach ($barcodeChar as $c) {
  152. /* visible, width, top, length */
  153. $width = $c ? $this->_barThickWidth : $this->_barThinWidth;
  154. $barcodeTable[] = array((int) $visible, $width, 0, 1);
  155. $visible = ! $visible;
  156. }
  157. $barcodeTable[] = array(0 , $this->_barThinWidth);
  158. }
  159. return $barcodeTable;
  160. }
  161. /**
  162. * Get barcode checksum
  163. *
  164. * @param string $text
  165. * @return int
  166. */
  167. public function getChecksum($text)
  168. {
  169. $this->_checkText($text);
  170. $text = str_split($text);
  171. $charset = array_flip(array_keys($this->_codingMap));
  172. $checksum = 0;
  173. foreach ($text as $character) {
  174. $checksum += $charset[$character];
  175. }
  176. return array_search(($checksum % 43), $charset);
  177. }
  178. }