Code25.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Interleaved 2 of 5 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_Code25 extends Zend_Barcode_Object_ObjectAbstract
  39. {
  40. /**
  41. * Coding map
  42. * - 0 = narrow bar
  43. * - 1 = wide bar
  44. * @var array
  45. */
  46. protected $_codingMap = array(
  47. '0' => '00110',
  48. '1' => '10001',
  49. '2' => '01001',
  50. '3' => '11000',
  51. '4' => '00101',
  52. '5' => '10100',
  53. '6' => '01100',
  54. '7' => '00011',
  55. '8' => '10010',
  56. '9' => '01010',
  57. );
  58. /**
  59. * Check allowed characters
  60. * @param string $value
  61. * @return string
  62. * @throw Zend_Barcode_Object_Exception
  63. */
  64. public function validateText($value)
  65. {
  66. $this->_validateText($value);
  67. }
  68. /**
  69. * Width of the barcode (in pixels)
  70. * @return integer
  71. */
  72. protected function _calculateBarcodeWidth()
  73. {
  74. $quietZone = $this->getQuietZone();
  75. $startCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
  76. $characterLength = (3 * $this->_barThinWidth + 2 * $this->_barThickWidth + 5 * $this->_barThinWidth)
  77. * $this->_factor;
  78. $encodedData = strlen($this->getText()) * $characterLength;
  79. $stopCharacter = (2 * $this->_barThickWidth + 4 * $this->_barThinWidth) * $this->_factor;
  80. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  81. }
  82. /**
  83. * Partial check of interleaved 2 of 5 barcode
  84. * @return void
  85. */
  86. protected function _checkParams()
  87. {
  88. $this->_checkRatio();
  89. }
  90. /**
  91. * Prepare array to draw barcode
  92. * @return array
  93. */
  94. protected function _prepareBarcode()
  95. {
  96. $barcodeTable = array();
  97. // Start character (30301)
  98. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  99. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  100. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  101. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  102. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  103. $barcodeTable[] = array(0 , 1);
  104. $text = str_split($this->getText());
  105. foreach ($text as $char) {
  106. $barcodeChar = str_split($this->_codingMap[$char]);
  107. foreach ($barcodeChar as $c) {
  108. /* visible, width, top, length */
  109. $width = $c ? $this->_barThickWidth : $this->_barThinWidth;
  110. $barcodeTable[] = array(1 , $width , 0 , 1);
  111. $barcodeTable[] = array(0 , 1);
  112. }
  113. }
  114. // Stop character (30103)
  115. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  116. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  117. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  118. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  119. $barcodeTable[] = array(1 , $this->_barThickWidth , 0 , 1);
  120. return $barcodeTable;
  121. }
  122. /**
  123. * Get barcode checksum
  124. *
  125. * @param string $text
  126. * @return int
  127. */
  128. public function getChecksum($text)
  129. {
  130. $this->_checkText($text);
  131. $factor = 3;
  132. $checksum = 0;
  133. for ($i = strlen($text); $i > 0; $i --) {
  134. $checksum += intval($text{$i - 1}) * $factor;
  135. $factor = 4 - $factor;
  136. }
  137. $checksum = 10 - ($checksum % 10);
  138. if ($checksum == 10) {
  139. $checksum = 0;
  140. }
  141. return $checksum;
  142. }
  143. /**
  144. * Retrieve text with checksum appended
  145. *
  146. * @return string
  147. */
  148. protected function _getTextWithChecksum()
  149. {
  150. return $this->_text . $this->getChecksum($this->_text);
  151. }
  152. }