web/lib/Zend/Barcode/Object/Code128.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Barcode
    16  * @package    Zend_Barcode
    17  * @subpackage Object
    17  * @subpackage Object
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Code25.php 20096 2010-01-06 02:05:09Z bkarwin $
    20  * @version    $Id: Code25.php 20096 2010-01-06 02:05:09Z bkarwin $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    33 /**
    33 /**
    34  * Class for generate Code128 barcode
    34  * Class for generate Code128 barcode
    35  *
    35  *
    36  * @category   Zend
    36  * @category   Zend
    37  * @package    Zend_Barcode
    37  * @package    Zend_Barcode
    38  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    40  */
    40  */
    41 class Zend_Barcode_Object_Code128 extends Zend_Barcode_Object_ObjectAbstract
    41 class Zend_Barcode_Object_Code128 extends Zend_Barcode_Object_ObjectAbstract
    42 {
    42 {
    43     /**
    43     /**
    46      * @var boolean
    46      * @var boolean
    47      */
    47      */
    48     protected $_withChecksum = true;
    48     protected $_withChecksum = true;
    49 
    49 
    50     /**
    50     /**
    51 	 * @var array
    51      * @var array
    52 	 */
    52      */
    53 	protected $_convertedText = array();
    53     protected $_convertedText = array();
    54 
    54 
    55     protected $_codingMap = array(
    55     protected $_codingMap = array(
    56                  0 => "11011001100",   1 => "11001101100",   2 => "11001100110",
    56                  0 => "11011001100",   1 => "11001101100",   2 => "11001100110",
    57                  3 => "10010011000",   4 => "10010001100",   5 => "10001001100",
    57                  3 => "10010011000",   4 => "10010001100",   5 => "10001001100",
    58                  6 => "10011001000",   7 => "10011000100",   8 => "10001100100",
    58                  6 => "10011001000",   7 => "10011000100",   8 => "10001100100",
   190         $quietZone = $this->getQuietZone();
   190         $quietZone = $this->getQuietZone();
   191         // Each characters contain 11 bars...
   191         // Each characters contain 11 bars...
   192         $characterLength = 11 * $this->_barThinWidth * $this->_factor;
   192         $characterLength = 11 * $this->_barThinWidth * $this->_factor;
   193         $convertedChars = count($this->_convertToBarcodeChars($this->getText()));
   193         $convertedChars = count($this->_convertToBarcodeChars($this->getText()));
   194         if ($this->_withChecksum) {
   194         if ($this->_withChecksum) {
   195         	$convertedChars++;
   195             $convertedChars++;
   196         }
   196         }
   197         $encodedData = $convertedChars * $characterLength;
   197         $encodedData = $convertedChars * $characterLength;
   198         // ...except the STOP character (13)
   198         // ...except the STOP character (13)
   199         $encodedData += $characterLength + 2 * $this->_barThinWidth * $this->_factor;
   199         $encodedData += $characterLength + 2 * $this->_barThinWidth * $this->_factor;
   200         $width = $quietZone + $encodedData + $quietZone;
   200         $width = $quietZone + $encodedData + $quietZone;
   218         $barcodeTable = array();
   218         $barcodeTable = array();
   219 
   219 
   220         $convertedChars = $this->_convertToBarcodeChars($this->getText());
   220         $convertedChars = $this->_convertToBarcodeChars($this->getText());
   221 
   221 
   222         if ($this->_withChecksum) {
   222         if ($this->_withChecksum) {
   223         	$convertedChars[] = $this->getChecksum($this->getText());
   223             $convertedChars[] = $this->getChecksum($this->getText());
   224         }
   224         }
   225 
   225 
   226         // STOP CHARACTER
   226         // STOP CHARACTER
   227         $convertedChars[] = 106;
   227         $convertedChars[] = 106;
   228 
   228 
   229         foreach ($convertedChars as $barcodeChar) {
   229         foreach ($convertedChars as $barcodeChar) {
   230             $barcodePattern = $this->_codingMap[$barcodeChar];
   230             $barcodePattern = $this->_codingMap[$barcodeChar];
   231             foreach (str_split($barcodePattern) as $c) {
   231             foreach (str_split($barcodePattern) as $c) {
   232                 $barcodeTable[] = array($c, 1, 0, 1);
   232                 $barcodeTable[] = array($c, $this->_barThinWidth, 0, 1);
   233             }
   233             }
   234         }
   234         }
   235         return $barcodeTable;
   235         return $barcodeTable;
   236     }
   236     }
   237 
   237 
   238     /**
   238     /**
   239      * Checks if the next $length chars of $string starting at $pos are numeric.
   239      * Checks if the next $length chars of $string starting at $pos are numeric.
   240      * Returns false if the end of the string is reached.
   240      * Returns false if the end of the string is reached.
   241      * @param $string String to search
   241      * @param string $string String to search
   242      * @param $pos Starting position
   242      * @param int    $pos Starting position
   243      * @param $length Length to search
   243      * @param int    $length Length to search
   244      * @return bool
   244      * @return bool
   245      */
   245      */
   246     protected static function _isDigit($string, $pos, $length = 2)
   246     protected static function _isDigit($string, $pos, $length = 2)
   247     {
   247     {
   248 	    if ($pos + $length > strlen($string)) {
   248         if ($pos + $length > strlen($string)) {
   249 	       return false;
   249            return false;
   250 	    }
   250         }
   251 
   251 
   252 		for ($i = $pos; $i < $pos + $length; $i++) {
   252         for ($i = $pos; $i < $pos + $length; $i++) {
   253 		      if (!is_numeric($string[$i])) {
   253               if (!is_numeric($string[$i])) {
   254 		          return false;
   254                   return false;
   255 		      }
   255               }
   256 		}
   256         }
   257 		return true;
   257         return true;
   258 	}
   258     }
   259 
   259 
   260     /**
   260     /**
   261      * Convert string to barcode string
   261      * Convert string to barcode string
   262      * @return array
   262      * @return array
   263      */
   263      */
   264     protected function _convertToBarcodeChars($string)
   264     protected function _convertToBarcodeChars($string)
   265     {
   265     {
   266     	$string = (string) $string;
   266         $string = (string) $string;
   267     	if (!strlen($string)) {
   267         if (!strlen($string)) {
   268     		return array();
   268             return array();
   269     	}
   269         }
   270 
   270 
   271     	if (isset($this->_convertedText[md5($string)])) {
   271         if (isset($this->_convertedText[md5($string)])) {
   272     		return $this->_convertedText[md5($string)];
   272             return $this->_convertedText[md5($string)];
   273     	}
   273         }
   274 
   274 
   275         $currentCharset = null;
   275         $currentCharset = null;
   276         $sum = 0;
   276         $sum = 0;
   277         $fak = 0;
   277         $fak = 0;
   278         $result = array();
   278         $result = array();
   361      * @param  string $text
   361      * @param  string $text
   362      * @return int
   362      * @return int
   363      */
   363      */
   364     public function getChecksum($text)
   364     public function getChecksum($text)
   365     {
   365     {
   366     	$tableOfChars = $this->_convertToBarcodeChars($text);
   366         $tableOfChars = $this->_convertToBarcodeChars($text);
   367 
   367 
   368     	$sum = $tableOfChars[0];
   368         $sum = $tableOfChars[0];
   369     	unset($tableOfChars[0]);
   369         unset($tableOfChars[0]);
   370 
   370 
   371     	$k = 1;
   371         $k = 1;
   372     	foreach ($tableOfChars as $char) {
   372         foreach ($tableOfChars as $char) {
   373     		$sum += ($k++) * $char;
   373             $sum += ($k++) * $char;
   374     	}
   374         }
   375 
   375 
   376     	$checksum = $sum % 103;
   376         $checksum = $sum % 103;
   377 
   377 
   378         return $checksum;
   378         return $checksum;
   379     }
   379     }
   380 
   380 
   381     /**
   381     /**
   383      * @param string $value
   383      * @param string $value
   384      * @param array  $options
   384      * @param array  $options
   385      */
   385      */
   386     protected function _validateText($value, $options = array())
   386     protected function _validateText($value, $options = array())
   387     {
   387     {
   388     	// @TODO: add code128 validator
   388         // @TODO: add code128 validator
   389     	return true;
   389         return true;
   390     }
   390     }
   391 }
   391 }