File indexing completed on 2025-03-02 05:29:11

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Barcode
0017  * @subpackage Object
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Barcode_Object_Ean13
0025  */
0026 // require_once 'Zend/Barcode/Object/Ean13.php';
0027 
0028 /**
0029  * @see Zend_Validate_Barcode
0030  */
0031 // require_once 'Zend/Validate/Barcode.php';
0032 
0033 /**
0034  * Class for generate UpcA barcode
0035  *
0036  * @category   Zend
0037  * @package    Zend_Barcode
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  */
0041 class Zend_Barcode_Object_Upca extends Zend_Barcode_Object_Ean13
0042 {
0043 
0044     /**
0045      * Default options for Postnet barcode
0046      * @return void
0047      */
0048     protected function _getDefaultOptions()
0049     {
0050         $this->_barcodeLength = 12;
0051         $this->_mandatoryChecksum = true;
0052         $this->_mandatoryQuietZones = true;
0053     }
0054 
0055     /**
0056      * Width of the barcode (in pixels)
0057      * @return integer
0058      */
0059     protected function _calculateBarcodeWidth()
0060     {
0061         $quietZone       = $this->getQuietZone();
0062         $startCharacter  = (3 * $this->_barThinWidth) * $this->_factor;
0063         $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
0064         $stopCharacter   = (3 * $this->_barThinWidth) * $this->_factor;
0065         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor * 12;
0066         return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
0067     }
0068 
0069         /**
0070      * Prepare array to draw barcode
0071      * @return array
0072      */
0073     protected function _prepareBarcode()
0074     {
0075         $barcodeTable = array();
0076         $height = ($this->_drawText) ? 1.1 : 1;
0077 
0078         // Start character (101)
0079         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0080         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0081         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0082 
0083         $textTable = str_split($this->getText());
0084 
0085         // First character
0086         $bars = str_split($this->_codingMap['A'][$textTable[0]]);
0087         foreach ($bars as $b) {
0088             $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
0089         }
0090 
0091         // First part
0092         for ($i = 1; $i < 6; $i++) {
0093             $bars = str_split($this->_codingMap['A'][$textTable[$i]]);
0094             foreach ($bars as $b) {
0095                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0096             }
0097         }
0098 
0099         // Middle character (01010)
0100         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0101         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0102         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0103         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0104         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0105 
0106         // Second part
0107         for ($i = 6; $i < 11; $i++) {
0108             $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
0109             foreach ($bars as $b) {
0110                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0111             }
0112         }
0113 
0114         // Last character
0115         $bars = str_split($this->_codingMap['C'][$textTable[11]]);
0116         foreach ($bars as $b) {
0117             $barcodeTable[] = array($b , $this->_barThinWidth , 0 , $height);
0118         }
0119 
0120         // Stop character (101)
0121         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0122         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0123         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0124         return $barcodeTable;
0125     }
0126 
0127     /**
0128      * Partial function to draw text
0129      * @return void
0130      */
0131     protected function _drawText()
0132     {
0133         if ($this->_drawText) {
0134             $text = $this->getTextToDisplay();
0135             $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
0136             $leftPosition = $this->getQuietZone() - $characterWidth;
0137             for ($i = 0; $i < $this->_barcodeLength; $i ++) {
0138                 $fontSize = $this->_fontSize;
0139                 if ($i == 0 || $i == 11) {
0140                     $fontSize *= 0.8;
0141                 }
0142                 $this->_addText(
0143                     $text{$i},
0144                     $fontSize * $this->_factor,
0145                     $this->_rotate(
0146                         $leftPosition,
0147                         (int) $this->_withBorder * 2
0148                             + $this->_factor * ($this->_barHeight + $fontSize) + 1
0149                     ),
0150                     $this->_font,
0151                     $this->_foreColor,
0152                     'left',
0153                     - $this->_orientation
0154                 );
0155                 switch ($i) {
0156                     case 0:
0157                         $factor = 10;
0158                         break;
0159                     case 5:
0160                         $factor = 4;
0161                         break;
0162                     case 10:
0163                         $factor = 11;
0164                         break;
0165                     default:
0166                         $factor = 0;
0167                 }
0168                 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
0169             }
0170         }
0171     }
0172 }