Upca.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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-2010 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_Ean13
  24. */
  25. require_once 'Zend/Barcode/Object/Ean13.php';
  26. /**
  27. * @see Zend_Validate_Barcode
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate UpcA barcode
  32. *
  33. * @category Zend
  34. * @package Zend_Barcode
  35. * @copyright Copyright (c) 2005-2010 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_Upca extends Zend_Barcode_Object_Ean13
  39. {
  40. /**
  41. * Default options for Postnet barcode
  42. * @return void
  43. */
  44. protected function _getDefaultOptions()
  45. {
  46. $this->_barcodeLength = 12;
  47. $this->_mandatoryChecksum = true;
  48. }
  49. /**
  50. * Width of the barcode (in pixels)
  51. * @return integer
  52. */
  53. protected function _calculateBarcodeWidth()
  54. {
  55. $quietZone = $this->getQuietZone();
  56. $startCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  57. $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
  58. $stopCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  59. $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 12;
  60. return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
  61. }
  62. /**
  63. * Prepare array to draw barcode
  64. * @return array
  65. */
  66. protected function _prepareBarcode()
  67. {
  68. $barcodeTable = array();
  69. $height = ($this->_drawText) ? 1.1 : 1;
  70. // Start character (101)
  71. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  72. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  73. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  74. $textTable = str_split($this->getText());
  75. // First character
  76. $bars = str_split($this->_codingMap['A'][$textTable[0]]);
  77. foreach ($bars as $b) {
  78. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
  79. }
  80. // First part
  81. for ($i = 1; $i < 6; $i++) {
  82. $bars = str_split($this->_codingMap['A'][$textTable[$i]]);
  83. foreach ($bars as $b) {
  84. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  85. }
  86. }
  87. // Middle character (01010)
  88. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  89. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  90. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  91. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  92. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  93. // Second part
  94. for ($i = 6; $i < 11; $i++) {
  95. $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
  96. foreach ($bars as $b) {
  97. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  98. }
  99. }
  100. // Last character
  101. $bars = str_split($this->_codingMap['C'][$textTable[11]]);
  102. foreach ($bars as $b) {
  103. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
  104. }
  105. // Stop character (101)
  106. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  107. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  108. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  109. return $barcodeTable;
  110. }
  111. /**
  112. * Partial function to draw text
  113. * @return void
  114. */
  115. protected function _drawText()
  116. {
  117. if ($this->_drawText) {
  118. $text = $this->getTextToDisplay();
  119. $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
  120. $leftPosition = $this->getQuietZone() - $characterWidth;
  121. for ($i = 0; $i < $this->_barcodeLength; $i ++) {
  122. $fontSize = $this->_fontSize;
  123. if ($i == 0 || $i == 11) {
  124. $fontSize *= 0.8;
  125. }
  126. $this->_addText(
  127. $text{$i},
  128. $fontSize * $this->_factor,
  129. $this->_rotate(
  130. $leftPosition,
  131. (int) $this->_withBorder * 2
  132. + $this->_factor * ($this->_barHeight + $fontSize) + 1
  133. ),
  134. $this->_font,
  135. $this->_foreColor,
  136. 'left',
  137. - $this->_orientation
  138. );
  139. switch ($i) {
  140. case 0:
  141. $factor = 10;
  142. break;
  143. case 5:
  144. $factor = 4;
  145. break;
  146. case 10:
  147. $factor = 11;
  148. break;
  149. default:
  150. $factor = 0;
  151. }
  152. $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
  153. }
  154. }
  155. }
  156. }