linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file BarcodeObject

Documentation is available at BarcodeObject

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

Documentation generated on Fri, 01 Apr 2011 09:27:53 +0200 by phpDocumentor 1.4.1