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 Code39 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_Code39 extends Zend_Barcode_Object_ObjectAbstract
0042 {
0043     /**
0044      * Coding map
0045      * @var array
0046      */
0047     protected $_codingMap = array(
0048         '0' => '000110100',
0049         '1' => '100100001',
0050         '2' => '001100001',
0051         '3' => '101100000',
0052         '4' => '000110001',
0053         '5' => '100110000',
0054         '6' => '001110000',
0055         '7' => '000100101',
0056         '8' => '100100100',
0057         '9' => '001100100',
0058         'A' => '100001001',
0059         'B' => '001001001',
0060         'C' => '101001000',
0061         'D' => '000011001',
0062         'E' => '100011000',
0063         'F' => '001011000',
0064         'G' => '000001101',
0065         'H' => '100001100',
0066         'I' => '001001100',
0067         'J' => '000011100',
0068         'K' => '100000011',
0069         'L' => '001000011',
0070         'M' => '101000010',
0071         'N' => '000010011',
0072         'O' => '100010010',
0073         'P' => '001010010',
0074         'Q' => '000000111',
0075         'R' => '100000110',
0076         'S' => '001000110',
0077         'T' => '000010110',
0078         'U' => '110000001',
0079         'V' => '011000001',
0080         'W' => '111000000',
0081         'X' => '010010001',
0082         'Y' => '110010000',
0083         'Z' => '011010000',
0084         '-' => '010000101',
0085         '.' => '110000100',
0086         ' ' => '011000100',
0087         '$' => '010101000',
0088         '/' => '010100010',
0089         '+' => '010001010',
0090         '%' => '000101010',
0091         '*' => '010010100',
0092     );
0093 
0094     /**
0095      * Partial check of Code39 barcode
0096      * @return void
0097      */
0098     protected function _checkParams()
0099     {
0100         $this->_checkRatio();
0101     }
0102 
0103     /**
0104      * Width of the barcode (in pixels)
0105      * @return int
0106      */
0107     protected function _calculateBarcodeWidth()
0108     {
0109         $quietZone       = $this->getQuietZone();
0110         $characterLength = (6 * $this->_barThinWidth + 3 * $this->_barThickWidth + 1) * $this->_factor;
0111         $encodedData     = strlen($this->getText()) * $characterLength - $this->_factor;
0112         return $quietZone + $encodedData + $quietZone;
0113     }
0114 
0115     /**
0116      * Set text to encode
0117      * @param string $value
0118      * @return Zend_Barcode_Object
0119      */
0120     public function setText($value)
0121     {
0122         $this->_text = $value;
0123         return $this;
0124     }
0125 
0126     /**
0127      * Retrieve text to display
0128      * @return string
0129      */
0130     public function getText()
0131     {
0132         return '*' . parent::getText() . '*';
0133     }
0134 
0135     /**
0136      * Retrieve text to display
0137      * @return string
0138      */
0139     public function getTextToDisplay()
0140     {
0141         $text = parent::getTextToDisplay();
0142         if (substr($text, 0, 1) != '*' && substr($text, -1) != '*') {
0143             return '*' . $text . '*';
0144         } else {
0145             return $text;
0146         }
0147     }
0148 
0149     /**
0150      * Prepare array to draw barcode
0151      * @return array
0152      */
0153     protected function _prepareBarcode()
0154     {
0155         $text         = str_split($this->getText());
0156         $barcodeTable = array();
0157         foreach ($text as $char) {
0158             $barcodeChar = str_split($this->_codingMap[$char]);
0159             $visible     = true;
0160             foreach ($barcodeChar as $c) {
0161                 /* visible, width, top, length */
0162                 $width          = $c ? $this->_barThickWidth : $this->_barThinWidth;
0163                 $barcodeTable[] = array((int) $visible, $width, 0, 1);
0164                 $visible = ! $visible;
0165             }
0166             $barcodeTable[] = array(0 , $this->_barThinWidth);
0167         }
0168         return $barcodeTable;
0169     }
0170 
0171     /**
0172      * Get barcode checksum
0173      *
0174      * @param  string $text
0175      * @return int
0176      */
0177     public function getChecksum($text)
0178     {
0179         $this->_checkText($text);
0180         $text     = str_split($text);
0181         $charset  = array_flip(array_keys($this->_codingMap));
0182         $checksum = 0;
0183         foreach ($text as $character) {
0184             $checksum += $charset[$character];
0185         }
0186         return array_search(($checksum % 43), $charset);
0187     }
0188 }