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

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