web/enmi/Zend/Barcode/Object/Ean8.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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: Ean8.php 21667 2010-03-28 17:45:14Z mikaelkael $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Barcode_Object_Ean13
       
    25  */
       
    26 require_once 'Zend/Barcode/Object/Ean13.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Validate_Barcode
       
    30  */
       
    31 require_once 'Zend/Validate/Barcode.php';
       
    32 
       
    33 /**
       
    34  * Class for generate Ean8 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_Ean8 extends Zend_Barcode_Object_Ean13
       
    42 {
       
    43 
       
    44     /**
       
    45      * Default options for Postnet barcode
       
    46      * @return void
       
    47      */
       
    48     protected function _getDefaultOptions()
       
    49     {
       
    50         $this->_barcodeLength = 8;
       
    51         $this->_mandatoryChecksum = true;
       
    52     }
       
    53 
       
    54     /**
       
    55      * Width of the barcode (in pixels)
       
    56      * @return integer
       
    57      */
       
    58     protected function _calculateBarcodeWidth()
       
    59     {
       
    60         $quietZone       = $this->getQuietZone();
       
    61         $startCharacter  = (3 * $this->_barThinWidth) * $this->_factor;
       
    62         $middleCharacter = (5 * $this->_barThinWidth) * $this->_factor;
       
    63         $stopCharacter   = (3 * $this->_barThinWidth) * $this->_factor;
       
    64         $encodedData     = (7 * $this->_barThinWidth) * $this->_factor * 8;
       
    65         return $quietZone + $startCharacter + $middleCharacter + $encodedData + $stopCharacter + $quietZone;
       
    66     }
       
    67 
       
    68         /**
       
    69      * Prepare array to draw barcode
       
    70      * @return array
       
    71      */
       
    72     protected function _prepareBarcode()
       
    73     {
       
    74         $barcodeTable = array();
       
    75         $height = ($this->_drawText) ? 1.1 : 1;
       
    76 
       
    77         // Start character (101)
       
    78         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
    79         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
    80         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
    81 
       
    82         $textTable = str_split($this->getText());
       
    83 
       
    84         // First part
       
    85         for ($i = 0; $i < 4; $i++) {
       
    86             $bars = str_split($this->_codingMap['A'][$textTable[$i]]);
       
    87             foreach ($bars as $b) {
       
    88                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
       
    89             }
       
    90         }
       
    91 
       
    92         // Middle character (01010)
       
    93         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
    94         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
    95         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
    96         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
    97         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
    98 
       
    99         // Second part
       
   100         for ($i = 4; $i < 8; $i++) {
       
   101             $bars = str_split($this->_codingMap['C'][$textTable[$i]]);
       
   102             foreach ($bars as $b) {
       
   103                 $barcodeTable[] = array($b , $this->_barThinWidth , 0 , 1);
       
   104             }
       
   105         }
       
   106 
       
   107         // Stop character (101)
       
   108         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   109         $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , $height);
       
   110         $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , $height);
       
   111         return $barcodeTable;
       
   112     }
       
   113 
       
   114     /**
       
   115      * Partial function to draw text
       
   116      * @return void
       
   117      */
       
   118     protected function _drawText()
       
   119     {
       
   120         if ($this->_drawText) {
       
   121             $text = $this->getTextToDisplay();
       
   122             $characterWidth = (7 * $this->_barThinWidth) * $this->_factor;
       
   123             $leftPosition = $this->getQuietZone() + (3 * $this->_barThinWidth) * $this->_factor;
       
   124             for ($i = 0; $i < $this->_barcodeLength; $i ++) {
       
   125                 $this->_addText(
       
   126                     $text{$i},
       
   127                     $this->_fontSize * $this->_factor,
       
   128                     $this->_rotate(
       
   129                         $leftPosition,
       
   130                         (int) $this->_withBorder * 2
       
   131                             + $this->_factor * ($this->_barHeight + $this->_fontSize) + 1
       
   132                     ),
       
   133                     $this->_font,
       
   134                     $this->_foreColor,
       
   135                     'left',
       
   136                     - $this->_orientation
       
   137                 );
       
   138                 switch ($i) {
       
   139                     case 3:
       
   140                         $factor = 4;
       
   141                         break;
       
   142                     default:
       
   143                         $factor = 0;
       
   144                 }
       
   145                 $leftPosition = $leftPosition + $characterWidth + ($factor * $this->_barThinWidth * $this->_factor);
       
   146             }
       
   147         }
       
   148     }
       
   149 
       
   150     /**
       
   151      * Particular validation for Ean8 barcode objects
       
   152      * (to suppress checksum character substitution)
       
   153      * @param string $value
       
   154      * @param array  $options
       
   155      */
       
   156     protected function _validateText($value, $options = array())
       
   157     {
       
   158         $validator = new Zend_Validate_Barcode(array(
       
   159             'adapter'  => 'ean8',
       
   160             'checksum' => false,
       
   161         ));
       
   162 
       
   163         $value = $this->_addLeadingZeros($value, true);
       
   164 
       
   165         if (!$validator->isValid($value)) {
       
   166             $message = implode("\n", $validator->getMessages());
       
   167 
       
   168             /**
       
   169              * @see Zend_Barcode_Object_Exception
       
   170              */
       
   171             require_once 'Zend/Barcode/Object/Exception.php';
       
   172             throw new Zend_Barcode_Object_Exception($message);
       
   173         }
       
   174     }
       
   175 }