Code39.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /**
  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-2009 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. private $_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. * Check for invalid characters
  92. * @param string $value Text to be ckecked
  93. * @return void
  94. */
  95. public function validateText($value)
  96. {
  97. $validator = new Zend_Validate_Barcode(array(
  98. 'adapter' => 'code39',
  99. 'checksum' => false,
  100. ));
  101. if (!$validator->isValid($value)) {
  102. $message = implode("\n", $validator->getMessages());
  103. /**
  104. * @see Zend_Barcode_Object_Exception
  105. */
  106. require_once 'Zend/Barcode/Object/Exception.php';
  107. throw new Zend_Barcode_Object_Exception($message);
  108. }
  109. }
  110. /**
  111. * Partial check of Code39 barcode
  112. * @return void
  113. */
  114. protected function _checkParams()
  115. {
  116. $this->_checkRatio();
  117. }
  118. /**
  119. * Width of the barcode (in pixels)
  120. * @return int
  121. */
  122. protected function _calculateBarcodeWidth()
  123. {
  124. $quietZone = $this->getQuietZone();
  125. $characterLength = (6 * $this->_barThinWidth + 3 * $this->_barThickWidth + 1) * $this->_factor;
  126. $encodedData = strlen($this->getText()) * $characterLength - $this->_factor;
  127. return $quietZone + $encodedData + $quietZone;
  128. }
  129. /**
  130. * Retrieve text to display
  131. * @return string
  132. */
  133. public function getText()
  134. {
  135. return '*' . parent::getText() . '*';
  136. }
  137. /**
  138. * Retrieve text to display
  139. * @return string
  140. */
  141. public function getTextToDisplay()
  142. {
  143. return '*' . parent::getTextToDisplay() . '*';
  144. }
  145. /**
  146. * Prepare array to draw barcode
  147. * @return array
  148. */
  149. protected function _prepareBarcode()
  150. {
  151. $text = str_split($this->getText());
  152. $barcodeTable = array();
  153. foreach ($text as $char) {
  154. $barcodeChar = str_split($this->_codingMap[$char]);
  155. $visible = true;
  156. foreach ($barcodeChar as $c) {
  157. /* visible, width, top, length */
  158. $width = $c ? $this->_barThickWidth : $this->_barThinWidth;
  159. $barcodeTable[] = array((int) $visible, $width, 0, 1);
  160. $visible = ! $visible;
  161. }
  162. $barcodeTable[] = array(0 , 1);
  163. }
  164. return $barcodeTable;
  165. }
  166. /**
  167. * Get barcode checksum
  168. *
  169. * @param string $text
  170. * @return int
  171. */
  172. public function getChecksum($text)
  173. {
  174. $this->_checkText($text);
  175. $text = str_split($text);
  176. $charset = array_flip(array_keys($this->_codingMap));
  177. $checksum = 0;
  178. foreach ($text as $character) {
  179. $checksum += $charset[$character];
  180. }
  181. return array_search(($checksum % 43), $charset);
  182. }
  183. /**
  184. * Get text with appended checksum
  185. *
  186. * @return string
  187. */
  188. protected function _getTextWithChecksum()
  189. {
  190. return $this->_text . $this->getChecksum($this->_text);
  191. }
  192. }