Upca.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_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-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_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. $this->_mandatoryQuietZones = true;
  49. }
  50. /**
  51. * Width of the barcode (in pixels)
  52. * @return integer
  53. */
  54. protected function _calculateBarcodeWidth()
  55. {
  56. $quietZone = $this->getQuietZone();
  57. $startCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  58. $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
  59. $stopCharacter = (3 * $this->_barThinWidth) * $this->_factor;
  60. $encodedData = (7 * $this->_barThinWidth) * $this->_factor * 12;
  61. return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
  62. }
  63. /**
  64. * Prepare array to draw barcode
  65. * @return array
  66. */
  67. protected function _prepareBarcode()
  68. {
  69. $barcodeTable = array();
  70. $height = ($this->_drawText) ? 1.1 : 1;
  71. // Start character (101)
  72. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  73. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  74. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  75. $textTable = str_split($this->getText());
  76. // First character
  77. $bars = str_split($this->_codingMap['A'][$textTable[0]]);
  78. foreach ($bars as $b) {
  79. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
  80. }
  81. // First part
  82. for ($i = 1; $i < 6; $i++) {
  83. $bars = str_split($this->_codingMap['A'][$textTable[$i]]);
  84. foreach ($bars as $b) {
  85. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  86. }
  87. }
  88. // Middle character (01010)
  89. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  90. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  91. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  92. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  93. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  94. // Second part
  95. for ($i = 6; $i < 11; $i++) {
  96. $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
  97. foreach ($bars as $b) {
  98. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
  99. }
  100. }
  101. // Last character
  102. $bars = str_split($this->_codingMap['C'][$textTable[11]]);
  103. foreach ($bars as $b) {
  104. $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
  105. }
  106. // Stop character (101)
  107. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  108. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
  109. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
  110. return $barcodeTable;
  111. }
  112. /**
  113. * Partial function to draw text
  114. * @return void
  115. */
  116. protected function _drawText()
  117. {
  118. if ($this->_drawText) {
  119. $text = $this->getTextToDisplay();
  120. $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
  121. $leftPosition = $this->getQuietZone() - $characterWidth;
  122. for ($i = 0; $i < $this->_barcodeLength; $i ++) {
  123. $fontSize = $this->_fontSize;
  124. if ($i == 0 || $i == 11) {
  125. $fontSize *= 0.8;
  126. }
  127. $this->_addText(
  128. $text{$i},
  129. $fontSize * $this->_factor,
  130. $this->_rotate(
  131. $leftPosition,
  132. (int) $this->_withBorder * 2
  133. + $this->_factor * ($this->_barHeight + $fontSize) + 1
  134. ),
  135. $this->_font,
  136. $this->_foreColor,
  137. 'left',
  138. - $this->_orientation
  139. );
  140. switch ($i) {
  141. case 0:
  142. $factor = 10;
  143. break;
  144. case 5:
  145. $factor = 4;
  146. break;
  147. case 10:
  148. $factor = 11;
  149. break;
  150. default:
  151. $factor = 0;
  152. }
  153. $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
  154. }
  155. }
  156. }
  157. }