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

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_ObjectAbstract
0025  */
0026 // require_once 'Zend/Barcode/Object/ObjectAbstract.php';
0027 
0028 /**
0029  * @see Zend_Validate_Barcode
0030  */
0031 // require_once 'Zend/Validate/Barcode.php';
0032 
0033 /**
0034  * Class for generate Ean13 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_Ean13 extends Zend_Barcode_Object_ObjectAbstract
0042 {
0043 
0044     /**
0045      * Coding map
0046      * - 0 = narrow bar
0047      * - 1 = wide bar
0048      * @var array
0049      */
0050     protected $_codingMap = array(
0051         'A' => array(
0052             0 => "0001101", 1 => "0011001", 2 => "0010011", 3 => "0111101", 4 => "0100011",
0053             5 => "0110001", 6 => "0101111", 7 => "0111011", 8 => "0110111", 9 => "0001011"
0054         ),
0055         'B' => array(
0056             0 => "0100111", 1 => "0110011", 2 => "0011011", 3 => "0100001", 4 => "0011101",
0057             5 => "0111001", 6 => "0000101", 7 => "0010001", 8 => "0001001", 9 => "0010111"
0058         ),
0059         'C' => array(
0060             0 => "1110010", 1 => "1100110", 2 => "1101100", 3 => "1000010", 4 => "1011100",
0061             5 => "1001110", 6 => "1010000", 7 => "1000100", 8 => "1001000", 9 => "1110100"
0062         ));
0063 
0064     protected $_parities = array(
0065         0 => array('A','A','A','A','A','A'),
0066         1 => array('A','A','B','A','B','B'),
0067         2 => array('A','A','B','B','A','B'),
0068         3 => array('A','A','B','B','B','A'),
0069         4 => array('A','B','A','A','B','B'),
0070         5 => array('A','B','B','A','A','B'),
0071         6 => array('A','B','B','B','A','A'),
0072         7 => array('A','B','A','B','A','B'),
0073         8 => array('A','B','A','B','B','A'),
0074         9 => array('A','B','B','A','B','A')
0075     );
0076 
0077     /**
0078      * Default options for Postnet barcode
0079      * @return void
0080      */
0081     protected function _getDefaultOptions()
0082     {
0083         $this->_barcodeLength = 13;
0084         $this->_mandatoryChecksum = true;
0085         $this->_mandatoryQuietZones = true;
0086     }
0087 
0088     /**
0089      * Width of the barcode (in pixels)
0090      * @return integer
0091      */
0092     protected function _calculateBarcodeWidth()
0093     {
0094         $quietZone       = $this->getQuietZone();
0095         $startCharacter  = (3 * $this->_barThinWidth) * $this->_factor;
0096         $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
0097         $stopCharacter   = (3 * $this->_barThinWidth) * $this->_factor;
0098         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor * 12;
0099         return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
0100     }
0101 
0102     /**
0103      * Partial check of interleaved EAN/UPC barcode
0104      * @return void
0105      */
0106     protected function _checkParams()
0107     {}
0108 
0109     /**
0110      * Prepare array to draw barcode
0111      * @return array
0112      */
0113     protected function _prepareBarcode()
0114     {
0115         $barcodeTable = array();
0116         $height = ($this->_drawText) ? 1.1 : 1;
0117 
0118         // Start character (101)
0119         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0120         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0121         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0122 
0123         $textTable = str_split($this->getText());
0124         $parity = $this->_parities[$textTable[0]];
0125 
0126         // First part
0127         for ($i = 1; $i < 7; $i++) {
0128             $bars = str_split($this->_codingMap[$parity[$i - 1]][$textTable[$i]]);
0129             foreach ($bars as $b) {
0130                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0131             }
0132         }
0133 
0134         // Middle character (01010)
0135         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0136         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0137         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0138         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0139         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0140 
0141         // Second part
0142         for ($i = 7; $i < 13; $i++) {
0143             $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
0144             foreach ($bars as $b) {
0145                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
0146             }
0147         }
0148 
0149         // Stop character (101)
0150         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0151         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
0152         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
0153         return $barcodeTable;
0154     }
0155 
0156     /**
0157      * Get barcode checksum
0158      *
0159      * @param  string $text
0160      * @return int
0161      */
0162     public function getChecksum($text)
0163     {
0164         $this->_checkText($text);
0165         $factor   = 3;
0166         $checksum = 0;
0167 
0168         for ($i = strlen($text); $i > 0; $i --) {
0169             $checksum += intval($text{$i - 1}) * $factor;
0170             $factor    = 4 - $factor;
0171         }
0172 
0173         $checksum = (10 - ($checksum % 10)) % 10;
0174 
0175         return $checksum;
0176     }
0177 
0178     /**
0179      * Partial function to draw text
0180      * @return void
0181      */
0182     protected function _drawText()
0183     {
0184         if (get_class($this) == 'Zend_Barcode_Object_Ean13') {
0185             $this->_drawEan13Text();
0186         } else {
0187             parent::_drawText();
0188         }
0189     }
0190 
0191     protected function _drawEan13Text()
0192     {
0193         if ($this->_drawText) {
0194             $text = $this->getTextToDisplay();
0195             $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
0196             $leftPosition = $this->getQuietZone() - $characterWidth;
0197             for ($i = 0; $i < $this->_barcodeLength; $i ++) {
0198                 $this->_addText(
0199                     $text{$i},
0200                     $this->_fontSize * $this->_factor,
0201                     $this->_rotate(
0202                         $leftPosition,
0203                         (int) $this->_withBorder * 2
0204                             + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
0205                     ),
0206                     $this->_font,
0207                     $this->_foreColor,
0208                     'left',
0209                     - $this->_orientation
0210                 );
0211                 switch ($i) {
0212                     case 0:
0213                         $factor = 3;
0214                         break;
0215                     case 6:
0216                         $factor = 4;
0217                         break;
0218                     default:
0219                         $factor = 0;
0220                 }
0221                 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
0222             }
0223         }
0224     }
0225 }