[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/libraries/tcpdf/barcode/ -> c128bobject.php (source)

   1  <?php
   2  //============================================================+
   3  // File name   : c128bobject.php
   4  // Begin       : 2002-07-31
   5  // Last Update : 2004-12-29
   6  // Author      : Karim Mribti [[email protected]]
   7  // Version     : 0.0.8a  2001-04-01 (original code)
   8  // License     : GNU LGPL (Lesser General Public License) 2.1
   9  //               http://www.gnu.org/copyleft/lesser.txt
  10  // Source Code : http://www.mribti.com/barcode/
  11  //
  12  // Description : Code 128-B Barcode Render Class for PHP using
  13  //               the GD graphics library.
  14  //               Code 128-B is a continuous, multilevel and full
  15  //               ASCII code.
  16  //
  17  // NOTE:
  18  // This version contains changes by Nicola Asuni:
  19  //  - porting to PHP5
  20  //  - code style and formatting
  21  //  - automatic php documentation in PhpDocumentor Style
  22  //    (www.phpdoc.org)
  23  //  - minor bug fixing
  24  //============================================================+
  25  
  26  /**
  27  * Code 128-B Barcode Render Class for PHP using the GD graphics library.<br>
  28  * Code 128-B is a continuous, multilevel and full ASCII code.
  29  * @author Karim Mribti, Nicola Asuni
  30  * @name BarcodeObject
  31  * @package com.tecnick.tcpdf
  32  * @version 0.0.8a  2001-04-01 (original code)
  33  * @since 2001-03-25
  34  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  35  */
  36  
  37  /**
  38  * Code 128-B Barcode Render Class for PHP using the GD graphics library.<br>
  39  * Code 128-B is a continuous, multilevel and full ASCII code.
  40  * @author Karim Mribti, Nicola Asuni
  41  * @name BarcodeObject
  42  * @package com.tecnick.tcpdf
  43  * @version 0.0.8a  2001-04-01 (original code)
  44  * @since 2001-03-25
  45  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  46  */
  47  class C128BObject extends BarcodeObject {
  48      
  49      /**
  50       * Class Constructor.
  51       * @param int $Width Image width in pixels.
  52       * @param int $Height Image height in pixels. 
  53       * @param int $Style Barcode style.
  54       * @param int $Value value to print on barcode.
  55       */
  56  	public function __construct($Width, $Height, $Style, $Value) {
  57          parent::__construct($Width, $Height, $Style);
  58          $this->mValue = $Value;
  59          $this->mChars = " !\"#$%&'()*+�-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{ }~";
  60          $this->mCharSet = array (
  61          "212222",   /*   00 */
  62          "222122",   /*   01 */
  63          "222221",   /*   02 */
  64          "121223",   /*   03 */
  65          "121322",   /*   04 */
  66          "131222",   /*   05 */
  67          "122213",   /*   06 */
  68          "122312",   /*   07 */
  69          "132212",   /*   08 */
  70          "221213",   /*   09 */
  71          "221312",   /*   10 */
  72          "231212",   /*   11 */
  73          "112232",   /*   12 */
  74          "122132",   /*   13 */
  75          "122231",   /*   14 */
  76          "113222",   /*   15 */
  77          "123122",   /*   16 */
  78          "123221",   /*   17 */
  79          "223211",   /*   18 */
  80          "221132",   /*   19 */
  81          "221231",   /*   20 */
  82          "213212",   /*   21 */
  83          "223112",   /*   22 */
  84          "312131",   /*   23 */
  85          "311222",   /*   24 */
  86          "321122",   /*   25 */
  87          "321221",   /*   26 */
  88          "312212",   /*   27 */
  89          "322112",   /*   28 */
  90          "322211",   /*   29 */
  91          "212123",   /*   30 */
  92          "212321",   /*   31 */
  93          "232121",   /*   32 */
  94          "111323",   /*   33 */
  95          "131123",   /*   34 */
  96          "131321",   /*   35 */
  97          "112313",   /*   36 */
  98          "132113",   /*   37 */
  99          "132311",   /*   38 */
 100          "211313",   /*   39 */
 101          "231113",   /*   40 */
 102          "231311",   /*   41 */
 103          "112133",   /*   42 */
 104          "112331",   /*   43 */
 105          "132131",   /*   44 */
 106          "113123",   /*   45 */
 107          "113321",   /*   46 */
 108          "133121",   /*   47 */
 109          "313121",   /*   48 */
 110          "211331",   /*   49 */
 111          "231131",   /*   50 */
 112          "213113",   /*   51 */
 113          "213311",   /*   52 */
 114          "213131",   /*   53 */
 115          "311123",   /*   54 */
 116          "311321",   /*   55 */
 117          "331121",   /*   56 */
 118          "312113",   /*   57 */
 119          "312311",   /*   58 */
 120          "332111",   /*   59 */
 121          "314111",   /*   60 */
 122          "221411",   /*   61 */
 123          "431111",   /*   62 */
 124          "111224",   /*   63 */
 125          "111422",   /*   64 */
 126          "121124",   /*   65 */
 127          "121421",   /*   66 */
 128          "141122",   /*   67 */
 129          "141221",   /*   68 */
 130          "112214",   /*   69 */
 131          "112412",   /*   70 */
 132          "122114",   /*   71 */
 133          "122411",   /*   72 */
 134          "142112",   /*   73 */
 135          "142211",   /*   74 */
 136          "241211",   /*   75 */
 137          "221114",   /*   76 */
 138          "413111",   /*   77 */
 139          "241112",   /*   78 */
 140          "134111",   /*   79 */
 141          "111242",   /*   80 */
 142          "121142",   /*   81 */
 143          "121241",   /*   82 */
 144          "114212",   /*   83 */
 145          "124112",   /*   84 */
 146          "124211",   /*   85 */
 147          "411212",   /*   86 */
 148          "421112",   /*   87 */
 149          "421211",   /*   88 */
 150          "212141",   /*   89 */
 151          "214121",   /*   90 */
 152          "412121",   /*   91 */
 153          "111143",   /*   92 */
 154          "111341",   /*   93 */
 155          "131141",   /*   94 */
 156          "114113",   /*   95 */
 157          "114311",   /*   96 */
 158          "411113",   /*   97 */
 159          "411311",   /*   98 */
 160          "113141",   /*   99 */
 161          "114131",   /*  100 */
 162          "311141",   /*  101 */
 163          "411131"    /*  102 */
 164          );
 165      }
 166  
 167      /**
 168       * Returns the character index.
 169       * @param char $char character.
 170       * @return int character index or -1 in case of error.
 171       * @access private
 172       */
 173  	private function GetCharIndex($char) {
 174          for ($i=0;$i<95;$i++) {
 175              if ($this->mChars[$i] == $char) {
 176                  return $i;
 177              }
 178          }
 179          return -1;
 180      }
 181      
 182      /**
 183       * Returns the bar size.
 184       * @param int $xres Horizontal resolution.
 185       * @param char $char Character.
 186       * @return int barcode size.
 187       * @access private
 188       */
 189  	private function GetBarSize($xres, $char) {
 190          switch ($char) {
 191              case '1': {
 192                  $cVal = BCD_C128_BAR_1;
 193                  break;
 194              }
 195              case '2': {
 196                  $cVal = BCD_C128_BAR_2;
 197                  break;
 198              }
 199              case '3': {
 200                  $cVal = BCD_C128_BAR_3;
 201                  break;
 202              }
 203              case '4': {
 204                  $cVal = BCD_C128_BAR_4;
 205                  break;
 206              }
 207              default: {
 208                  $cVal = 0;
 209              }
 210          }
 211          return  $cVal * $xres;
 212      }
 213      
 214      /**
 215       * Returns barcode size.
 216       * @param int $xres Horizontal resolution.
 217       * @return barcode size.
 218       * @access private
 219       */
 220  	private function GetSize($xres) {
 221          $len = strlen($this->mValue);
 222  
 223          if ($len == 0)  {
 224              $this->mError = "Null value";
 225              return false;
 226          }
 227          $ret = 0;
 228          for ($i=0;$i<$len;$i++) {
 229              if (($id = $this->GetCharIndex($this->mValue[$i])) == -1) {
 230                  $this->mError = "C128B not include the char '".$this->mValue[$i]."'";
 231                  return false;
 232              } else {
 233                  $cset = $this->mCharSet[$id];
 234                  $ret += $this->GetBarSize($xres, $cset[0]);
 235                  $ret += $this->GetBarSize($xres, $cset[1]);
 236                  $ret += $this->GetBarSize($xres, $cset[2]);
 237                  $ret += $this->GetBarSize($xres, $cset[3]);
 238                  $ret += $this->GetBarSize($xres, $cset[4]);
 239                  $ret += $this->GetBarSize($xres, $cset[5]);
 240              }
 241          }
 242          /* length of Check character */
 243          $cset = $this->GetCheckCharValue();
 244          $CheckSize = 0;
 245          for ($i=0;$i<6;$i++) {
 246              $CheckSize += $this->GetBarSize($cset[$i], $xres);
 247          }
 248  
 249          $StartSize = 2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + BCD_C128_BAR_4*$xres;
 250          $StopSize  = 2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + 2*BCD_C128_BAR_3*$xres;
 251  
 252          return $StartSize + $ret + $CheckSize + $StopSize;
 253      }
 254      
 255      /**
 256       * Returns the check-char value.
 257       * @return string.
 258       * @access private
 259       */
 260  	private function GetCheckCharValue() {
 261          $len = strlen($this->mValue);
 262          $sum = 104; // 'B' type;
 263          for ($i=0;$i<$len;$i++) {
 264              $sum += $this->GetCharIndex($this->mValue[$i]) * ($i+1);
 265          }
 266          $check  = $sum % 103;
 267          return $this->mCharSet[$check];
 268      }
 269  
 270      /**
 271       * Draws the start code.
 272       * @param int $DrawPos Drawing position.
 273       * @param int $yPos Vertical position.
 274       * @param int $ySize Vertical size.
 275       * @param int $xres Horizontal resolution.
 276       * @return int drawing position.
 277       * @access private
 278       */
 279  	private function DrawStart($DrawPos, $yPos, $ySize, $xres) {
 280          /* Start code is '211214' */
 281          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize('2', $xres), $ySize);
 282          $DrawPos += $this->GetBarSize('2', $xres);
 283          $DrawPos += $this->GetBarSize('1', $xres);
 284          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize('1', $xres), $ySize);
 285          $DrawPos += $this->GetBarSize('1', $xres);
 286          $DrawPos += $this->GetBarSize('2', $xres);
 287          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize('1', $xres), $ySize);
 288          $DrawPos += $this->GetBarSize('1', $xres);
 289          $DrawPos += $this->GetBarSize('4', $xres);
 290          return $DrawPos;
 291      }
 292      
 293      /**
 294       * Draws the stop code.
 295       * @param int $DrawPos Drawing position.
 296       * @param int $yPos Vertical position.
 297       * @param int $ySize Vertical size.
 298       * @param int $xres Horizontal resolution.
 299       * @return int drawing position.
 300       * @access private
 301       */
 302  	private function DrawStop($DrawPos, $yPos, $ySize, $xres) {
 303          /* Stop code is '2331112' */
 304          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize('2', $xres) , $ySize);
 305          $DrawPos += $this->GetBarSize('2', $xres);
 306          $DrawPos += $this->GetBarSize('3', $xres);
 307          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize('3', $xres) , $ySize);
 308          $DrawPos += $this->GetBarSize('3', $xres);
 309          $DrawPos += $this->GetBarSize('1', $xres);
 310          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize('1', $xres) , $ySize);
 311          $DrawPos += $this->GetBarSize('1', $xres);
 312          $DrawPos += $this->GetBarSize('1', $xres);
 313          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize('2', $xres) , $ySize);
 314          $DrawPos += $this->GetBarSize('2', $xres);
 315          return $DrawPos;
 316      }
 317      
 318      /**
 319       * Draws the check-char code.
 320       * @param int $DrawPos Drawing position.
 321       * @param int $yPos Vertical position.
 322       * @param int $ySize Vertical size.
 323       * @param int $xres Horizontal resolution.
 324       * @return int drawing position.
 325       * @access private
 326       */
 327  	private function DrawCheckChar($DrawPos, $yPos, $ySize, $xres) {
 328          $cset = $this->GetCheckCharValue();
 329          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize($cset[0], $xres) , $ySize);
 330          $DrawPos += $this->GetBarSize($cset[0], $xres);
 331          $DrawPos += $this->GetBarSize($cset[1], $xres);
 332          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize($cset[2], $xres) , $ySize);
 333          $DrawPos += $this->GetBarSize($cset[2], $xres);
 334          $DrawPos += $this->GetBarSize($cset[3], $xres);
 335          $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize($cset[4], $xres) , $ySize);
 336          $DrawPos += $this->GetBarSize($cset[4], $xres);
 337          $DrawPos += $this->GetBarSize($cset[5], $xres);
 338          return $DrawPos;
 339      }
 340  
 341      /**
 342       * Draws the barcode object.
 343       * @param int $xres Horizontal resolution.
 344       * @return bool true in case of success.
 345       */
 346  	public function DrawObject($xres) {
 347          $len = strlen($this->mValue);
 348          if (($size = $this->GetSize($xres))==0) {
 349              return false;
 350          }
 351  
 352          if ($this->mStyle & BCS_ALIGN_CENTER) $sPos = (integer)(($this->mWidth - $size ) / 2);
 353          else if ($this->mStyle & BCS_ALIGN_RIGHT) $sPos = $this->mWidth - $size;
 354          else $sPos = 0;
 355  
 356          /* Total height of bar code -Bars only- */
 357          if ($this->mStyle & BCS_DRAW_TEXT) $ysize = $this->mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2 - $this->GetFontHeight($this->mFont);
 358          else $ysize = $this->mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2;
 359  
 360          /* Draw text */
 361          if ($this->mStyle & BCS_DRAW_TEXT) {
 362              if ($this->mStyle & BCS_STRETCH_TEXT) {
 363                  for ($i=0;$i<$len;$i++) {
 364                      $this->DrawChar($this->mFont, $sPos+(2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + BCD_C128_BAR_4*$xres)+($size/$len)*$i,
 365                      $ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, $this->mValue[$i]);
 366                  }
 367              } else {/* Center */
 368              $text_width = $this->GetFontWidth($this->mFont) * strlen($this->mValue);
 369              $this->DrawText($this->mFont, $sPos+(($size-$text_width)/2)+(2*BCD_C128_BAR_2*$xres + 3*BCD_C128_BAR_1*$xres + BCD_C128_BAR_4*$xres),
 370              $ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, $this->mValue);
 371              }
 372          }
 373  
 374          $cPos = 0;
 375          $DrawPos = $this->DrawStart($sPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres);
 376          do {
 377              $c     = $this->GetCharIndex($this->mValue[$cPos]);
 378              $cset  = $this->mCharSet[$c];
 379              $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize($cset[0], $xres) , $ysize);
 380              $DrawPos += $this->GetBarSize($cset[0], $xres);
 381              $DrawPos += $this->GetBarSize($cset[1], $xres);
 382              $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize($cset[2], $xres) , $ysize);
 383              $DrawPos += $this->GetBarSize($cset[2], $xres);
 384              $DrawPos += $this->GetBarSize($cset[3], $xres);
 385              $this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, $this->GetBarSize($cset[4], $xres) , $ysize);
 386              $DrawPos += $this->GetBarSize($cset[4], $xres);
 387              $DrawPos += $this->GetBarSize($cset[5], $xres);
 388              $cPos++;
 389          } while ($cPos<$len);
 390          $DrawPos = $this->DrawCheckChar($DrawPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres);
 391          $DrawPos =  $this->DrawStop($DrawPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres);
 392          return true;
 393      }
 394  }
 395  
 396  //============================================================+
 397  // END OF FILE
 398  //============================================================+
 399  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1