web/lib/Zend/Barcode/Object/Ean13.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: Ean13.php 22999 2010-09-23 19:43:14Z mikaelkael $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Barcode_Object_ObjectAbstract
       
    25  */
       
    26 require_once 'Zend/Barcode/Object/ObjectAbstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Validate_Barcode
       
    30  */
       
    31 require_once 'Zend/Validate/Barcode.php';
       
    32 
       
    33 /**
       
    34  * Class for generate Ean13 barcode
       
    35  *
       
    36  * @category   Zend
       
    37  * @package    Zend_Barcode
       
    38  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    40  */
       
    41 class Zend_Barcode_Object_Ean13 extends Zend_Barcode_Object_ObjectAbstract
       
    42 {
       
    43 
       
    44     /**
       
    45      * Coding map
       
    46      * - 0 = narrow bar
       
    47      * - 1 = wide bar
       
    48      * @var array
       
    49      */
       
    50     protected $_codingMap = array(
       
    51         'A' => array(
       
    52             0 => "0001101", 1 => "0011001", 2 => "0010011", 3 => "0111101", 4 => "0100011",
       
    53             5 => "0110001", 6 => "0101111", 7 => "0111011", 8 => "0110111", 9 => "0001011"
       
    54         ),
       
    55         'B' => array(
       
    56             0 => "0100111", 1 => "0110011", 2 => "0011011", 3 => "0100001", 4 => "0011101",
       
    57             5 => "0111001", 6 => "0000101", 7 => "0010001", 8 => "0001001", 9 => "0010111"
       
    58         ),
       
    59         'C' => array(
       
    60             0 => "1110010", 1 => "1100110", 2 => "1101100", 3 => "1000010", 4 => "1011100",
       
    61             5 => "1001110", 6 => "1010000", 7 => "1000100", 8 => "1001000", 9 => "1110100"
       
    62         ));
       
    63 
       
    64     protected $_parities = array(
       
    65         0 => array('A','A','A','A','A','A'),
       
    66         1 => array('A','A','B','A','B','B'),
       
    67         2 => array('A','A','B','B','A','B'),
       
    68         3 => array('A','A','B','B','B','A'),
       
    69         4 => array('A','B','A','A','B','B'),
       
    70         5 => array('A','B','B','A','A','B'),
       
    71         6 => array('A','B','B','B','A','A'),
       
    72         7 => array('A','B','A','B','A','B'),
       
    73         8 => array('A','B','A','B','B','A'),
       
    74         9 => array('A','B','B','A','B','A')
       
    75     );
       
    76 
       
    77     /**
       
    78      * Default options for Postnet barcode
       
    79      * @return void
       
    80      */
       
    81     protected function _getDefaultOptions()
       
    82     {
       
    83         $this->_barcodeLength = 13;
       
    84         $this->_mandatoryChecksum = true;
       
    85         $this->_mandatoryQuietZones = true;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Width of the barcode (in pixels)
       
    90      * @return integer
       
    91      */
       
    92     protected function _calculateBarcodeWidth()
       
    93     {
       
    94         $quietZone       = $this->getQuietZone();
       
    95         $startCharacter  = (3 * $this->_barThinWidth) * $this->_factor;
       
    96         $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
       
    97         $stopCharacter   = (3 * $this->_barThinWidth) * $this->_factor;
       
    98         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor * 12;
       
    99         return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
       
   100     }
       
   101 
       
   102     /**
       
   103      * Partial check of interleaved EAN/UPC barcode
       
   104      * @return void
       
   105      */
       
   106     protected function _checkParams()
       
   107     {}
       
   108 
       
   109     /**
       
   110      * Prepare array to draw barcode
       
   111      * @return array
       
   112      */
       
   113     protected function _prepareBarcode()
       
   114     {
       
   115         $barcodeTable = array();
       
   116         $height = ($this->_drawText) ? 1.1 : 1;
       
   117 
       
   118         // Start character (101)
       
   119         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   120         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
   121         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   122 
       
   123         $textTable = str_split($this->getText());
       
   124         $parity = $this->_parities[$textTable[0]];
       
   125 
       
   126         // First part
       
   127         for ($i = 1; $i < 7; $i++) {
       
   128             $bars = str_split($this->_codingMap[$parity[$i - 1]][$textTable[$i]]);
       
   129             foreach ($bars as $b) {
       
   130                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
       
   131             }
       
   132         }
       
   133 
       
   134         // Middle character (01010)
       
   135         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
   136         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   137         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
   138         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   139         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
   140 
       
   141         // Second part
       
   142         for ($i = 7; $i < 13; $i++) {
       
   143             $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
       
   144             foreach ($bars as $b) {
       
   145                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
       
   146             }
       
   147         }
       
   148 
       
   149         // Stop character (101)
       
   150         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   151         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
   152         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   153         return $barcodeTable;
       
   154     }
       
   155 
       
   156     /**
       
   157      * Get barcode checksum
       
   158      *
       
   159      * @param  string $text
       
   160      * @return int
       
   161      */
       
   162     public function getChecksum($text)
       
   163     {
       
   164         $this->_checkText($text);
       
   165         $factor   = 3;
       
   166         $checksum = 0;
       
   167 
       
   168         for ($i = strlen($text); $i > 0; $i --) {
       
   169             $checksum += intval($text{$i - 1}) * $factor;
       
   170             $factor    = 4 - $factor;
       
   171         }
       
   172 
       
   173         $checksum = (10 - ($checksum % 10)) % 10;
       
   174 
       
   175         return $checksum;
       
   176     }
       
   177 
       
   178     /**
       
   179      * Partial function to draw text
       
   180      * @return void
       
   181      */
       
   182     protected function _drawText()
       
   183     {
       
   184         if (get_class($this) == 'Zend_Barcode_Object_Ean13') {
       
   185             $this->_drawEan13Text();
       
   186         } else {
       
   187             parent::_drawText();
       
   188         }
       
   189     }
       
   190 
       
   191     protected function _drawEan13Text()
       
   192     {
       
   193         if ($this->_drawText) {
       
   194             $text = $this->getTextToDisplay();
       
   195             $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
       
   196             $leftPosition = $this->getQuietZone() - $characterWidth;
       
   197             for ($i = 0; $i < $this->_barcodeLength; $i ++) {
       
   198                 $this->_addText(
       
   199                     $text{$i},
       
   200                     $this->_fontSize * $this->_factor,
       
   201                     $this->_rotate(
       
   202                         $leftPosition,
       
   203                         (int) $this->_withBorder * 2
       
   204                             + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
       
   205                     ),
       
   206                     $this->_font,
       
   207                     $this->_foreColor,
       
   208                     'left',
       
   209                     - $this->_orientation
       
   210                 );
       
   211                 switch ($i) {
       
   212                     case 0:
       
   213                         $factor = 3;
       
   214                         break;
       
   215                     case 6:
       
   216                         $factor = 4;
       
   217                         break;
       
   218                     default:
       
   219                         $factor = 0;
       
   220                 }
       
   221                 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
       
   222             }
       
   223         }
       
   224     }
       
   225 }