File indexing completed on 2024-12-22 05:36:29

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_Upce extends Zend_Barcode_Object_Ean13
0042 {
0043 
0044     protected $_parities = array(
0045         0 => array(
0046             0 => array('B','B','B','A','A','A'),
0047             1 => array('B','B','A','B','A','A'),
0048             2 => array('B','B','A','A','B','A'),
0049             3 => array('B','B','A','A','A','B'),
0050             4 => array('B','A','B','B','A','A'),
0051             5 => array('B','A','A','B','B','A'),
0052             6 => array('B','A','A','A','B','B'),
0053             7 => array('B','A','B','A','B','A'),
0054             8 => array('B','A','B','A','A','B'),
0055             9 => array('B','A','A','B','A','B')),
0056         1 => array(
0057             0 => array('A','A','A','B','B','B'),
0058             1 => array('A','A','B','A','B','B'),
0059             2 => array('A','A','B','B','A','B'),
0060             3 => array('A','A','B','B','B','A'),
0061             4 => array('A','B','A','A','B','B'),
0062             5 => array('A','B','B','A','A','B'),
0063             6 => array('A','B','B','B','A','A'),
0064             7 => array('A','B','A','B','A','B'),
0065             8 => array('A','B','A','B','B','A'),
0066             9 => array('A','B','B','A','B','A'))
0067     );
0068 
0069     /**
0070      * Default options for Postnet barcode
0071      * @return void
0072      */
0073     protected function _getDefaultOptions()
0074     {
0075         $this->_barcodeLength = 8;
0076         $this->_mandatoryChecksum = true;
0077         $this->_mandatoryQuietZones = true;
0078     }
0079 
0080     /**
0081      * Retrieve text to encode
0082      * @return string
0083      */
0084     public function getText()
0085     {
0086         $text = parent::getText();
0087         if ($text{0} != 1) {
0088             $text{0} = 0;
0089         }
0090         return $text;
0091     }
0092 
0093     /**
0094      * Width of the barcode (in pixels)
0095      * @return integer
0096      */
0097     protected function _calculateBarcodeWidth()
0098     {
0099         $quietZone       = $this->getQuietZone();
0100         $startCharacter  = (3 * $this->_barThinWidth) * $this->_factor;
0101         $stopCharacter   = (6 * $this->_barThinWidth) * $this->_factor;
0102         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor * 6;
0103         return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
0104     }
0105 
0106     /**
0107      * Prepare array to draw barcode
0108      * @return array
0109      */
0110     protected function _prepareBarcode()
0111     {
0112         $barcodeTable = array();
0113         $height = ($this->_drawText) ? 1.1 : 1;
0114 
0115         // Start character (101)
0116         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0117         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0118         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0119 
0120         $textTable = str_split($this->getText());
0121         $system = 0;
0122         if ($textTable[0] == 1) {
0123             $system = 1;
0124         }
0125         $checksum = $textTable[7];
0126         $parity = $this->_parities[$system][$checksum];
0127 
0128         for ($i = 1; $i < 7; $i++) {
0129             $bars = str_split($this->_codingMap[$parity[$i - 1]][$textTable[$i]]);
0130             foreach ($bars as $b) {
0131                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0132             }
0133         }
0134 
0135         // Stop character (10101)
0136         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0137         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0138         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0139         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0140         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0141         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0142         return $barcodeTable;
0143     }
0144 
0145     /**
0146      * Partial function to draw text
0147      * @return void
0148      */
0149     protected function _drawText()
0150     {
0151         if ($this->_drawText) {
0152             $text = $this->getTextToDisplay();
0153             $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
0154             $leftPosition = $this->getQuietZone() - $characterWidth;
0155             for ($i = 0; $i < $this->_barcodeLength; $i ++) {
0156                 $fontSize = $this->_fontSize;
0157                 if ($i == 0 || $i == 7) {
0158                     $fontSize *= 0.8;
0159                 }
0160                 $this->_addText(
0161                     $text{$i},
0162                     $fontSize * $this->_factor,
0163                     $this->_rotate(
0164                         $leftPosition,
0165                         (int) $this->_withBorder * 2
0166                             + $this->_factor * ($this->_barHeight + $fontSize) + 1
0167                     ),
0168                     $this->_font,
0169                     $this->_foreColor,
0170                     'left',
0171                     - $this->_orientation
0172                 );
0173                 switch ($i) {
0174                     case 0:
0175                         $factor = 3;
0176                         break;
0177                     case 6:
0178                         $factor = 5;
0179                         break;
0180                     default:
0181                         $factor = 0;
0182                 }
0183                 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
0184             }
0185         }
0186     }
0187 
0188     /**
0189      * Particular validation for Upce barcode objects
0190      * (to suppress checksum character substitution)
0191      *
0192      * @param string $value
0193      * @param array  $options
0194      * @throws Zend_Barcode_Object_Exception
0195      */
0196     protected function _validateText($value, $options = array())
0197     {
0198         $validator = new Zend_Validate_Barcode(array(
0199             'adapter'  => 'upce',
0200             'checksum' => false,
0201         ));
0202 
0203         $value = $this->_addLeadingZeros($value, true);
0204 
0205         if (!$validator->isValid($value)) {
0206             $message = implode("\n", $validator->getMessages());
0207 
0208             /**
0209              * @see Zend_Barcode_Object_Exception
0210              */
0211             // require_once 'Zend/Barcode/Object/Exception.php';
0212             throw new Zend_Barcode_Object_Exception($message);
0213         }
0214     }
0215 
0216     /**
0217      * Get barcode checksum
0218      *
0219      * @param  string $text
0220      * @return int
0221      */
0222     public function getChecksum($text)
0223     {
0224         $text = $this->_addLeadingZeros($text, true);
0225         if ($text{0} != 1) {
0226             $text{0} = 0;
0227         }
0228         return parent::getChecksum($text);
0229     }
0230 }