[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

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

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


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