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   : c39object.php
  4. // Begin       : 2002-07-31
  5. // Last Update : 2004-12-29
  6. // Author      : Karim Mribti [barcode@mribti.com]
  7. //             : Nicola Asuni [info@tecnick.com]
  8. // Version     : 0.0.8a  2001-04-01 (original code)
  9. // License     : GNU LGPL (Lesser General Public License) 2.1
  10. //               http://www.gnu.org/copyleft/lesser.txt
  11. // Source Code : http://www.mribti.com/barcode/
  12. //
  13. // Description : Code 39 Barcode Render Class for PHP using
  14. //               the GD graphics library.
  15. //               Code 39 is an alphanumeric bar code that can
  16. //               encode decimal number, case alphabet and some
  17. //               special symbols.
  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 39 Barcode Render Class.<br>
  30.  * Code 39 is an alphanumeric bar code that can encode decimal number, case alphabet and some special symbols.
  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 39 Barcode Render Class.<br>
  42.  * Code 39 is an alphanumeric bar code that can encode decimal number, case alphabet and some special symbols.
  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 C39Object 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 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
  64.         $this->mCharSet = array (
  65.         /* 0  */ "000110100",
  66.         /* 1  */ "100100001",
  67.         /* 2  */ "001100001",
  68.         /* 3  */ "101100000",
  69.         /* 4  */ "000110001",
  70.         /* 5  */ "100110000",
  71.         /* 6  */ "001110000",
  72.         /* 7  */ "000100101",
  73.         /* 8  */ "100100100",
  74.         /* 9  */ "001100100",
  75.         /* A  */ "100001001",
  76.         /* B  */ "001001001",
  77.         /* C  */ "101001000",
  78.         /* D  */ "000011001",
  79.         /* E  */ "100011000",
  80.         /* F  */ "001011000",
  81.         /* G  */ "000001101",
  82.         /* H  */ "100001100",
  83.         /* I  */ "001001100",
  84.         /* J  */ "000011100",
  85.         /* K  */ "100000011",
  86.         /* L  */ "001000011",
  87.         /* M  */ "101000010",
  88.         /* N  */ "000010011",
  89.         /* O  */ "100010010",
  90.         /* P  */ "001010010",
  91.         /* Q  */ "000000111",
  92.         /* R  */ "100000110",
  93.         /* S  */ "001000110",
  94.         /* T  */ "000010110",
  95.         /* U  */ "110000001",
  96.         /* V  */ "011000001",
  97.         /* W  */ "111000000",
  98.         /* X  */ "010010001",
  99.         /* Y  */ "110010000",
  100.         /* Z  */ "011010000",
  101.         /* -  */ "010000101",
  102.         /* .  */ "110000100",
  103.         /* SP */ "011000100",
  104.         /* *  */ "010010100",
  105.         /* $  */ "010101000",
  106.         /* /  */ "010100010",
  107.         /* +  */ "010001010",
  108.         /* %  */ "000101010"
  109.         );
  110.     }
  111.  
  112.     /**
  113.      * Returns the character index.
  114.      * @param char $char character.
  115.      * @return int character index or -1 in case of error.
  116.      * @access private
  117.      */
  118.     private function GetCharIndex($char{
  119.         for ($i=0;$i<44;$i++{
  120.             if ($this->mChars[$i== $char{
  121.                 return $i;
  122.             }
  123.         }
  124.         return -1;
  125.     }
  126.     
  127.     /**
  128.      * Returns barcode size.
  129.      * @param int $xres Horizontal resolution.
  130.      * @return barcode size.
  131.      * @access private
  132.      */
  133.     private function GetSize($xres{
  134.         $len strlen($this->mValue);
  135.  
  136.         if ($len == 0)  {
  137.             $this->mError = "Null value";
  138.             return false;
  139.         }
  140.  
  141.         for ($i=0;$i<$len;$i++{
  142.             if ($this->GetCharIndex($this->mValue[$i]== -|| $this->mValue[$i== '*'{
  143.                 /* The asterisk is only used as a start and stop code */
  144.                 $this->mError = "C39 not include the char '".$this->mValue[$i]."'";
  145.                 return false;
  146.             }
  147.         }
  148.  
  149.         /* Start, Stop is 010010100 == '*'  */
  150.         $StartSize BCD_C39_NARROW_BAR $xres BCD_C39_WIDE_BAR $xres 3;
  151.         $StopSize  BCD_C39_NARROW_BAR $xres BCD_C39_WIDE_BAR $xres 3;
  152.         $CharSize  BCD_C39_NARROW_BAR $xres BCD_C39_WIDE_BAR $xres 3/* Same for all chars */
  153.  
  154.         return $CharSize $len $StartSize $StopSize /* Space between chars */ BCD_C39_NARROW_BAR $xres ($len-1);
  155.     }
  156.  
  157.     /**
  158.      * Draws the start code.
  159.      * @param int $DrawPos Drawing position.
  160.      * @param int $yPos Vertical position.
  161.      * @param int $ySize Vertical size.
  162.      * @param int $xres Horizontal resolution.
  163.      * @return int drawing position.
  164.      * @access private
  165.      */
  166.     private function DrawStart($DrawPos$yPos$ySize$xres{
  167.         /* Start code is '*' */
  168.         $narrow BCD_C39_NARROW_BAR $xres;
  169.         $wide   BCD_C39_WIDE_BAR $xres;
  170.         $this->DrawSingleBar($DrawPos$yPos$narrow $ySize);
  171.         $DrawPos += $narrow;
  172.         $DrawPos += $wide;
  173.         $this->DrawSingleBar($DrawPos$yPos$narrow $ySize);
  174.         $DrawPos += $narrow;
  175.         $DrawPos += $narrow;
  176.         $this->DrawSingleBar($DrawPos$yPos$wide $ySize);
  177.         $DrawPos += $wide;
  178.         $DrawPos += $narrow;
  179.         $this->DrawSingleBar($DrawPos$yPos$wide $ySize);
  180.         $DrawPos += $wide;
  181.         $DrawPos += $narrow;
  182.         $this->DrawSingleBar($DrawPos$yPos$narrow$ySize);
  183.         $DrawPos += $narrow;
  184.         $DrawPos += $narrow/* Space between chars */
  185.         return $DrawPos;
  186.     }
  187.     
  188.     /**
  189.      * Draws the stop code.
  190.      * @param int $DrawPos Drawing position.
  191.      * @param int $yPos Vertical position.
  192.      * @param int $ySize Vertical size.
  193.      * @param int $xres Horizontal resolution.
  194.      * @return int drawing position.
  195.      * @access private
  196.      */
  197.     private function DrawStop($DrawPos$yPos$ySize$xres{
  198.         /* Stop code is '*' */
  199.         $narrow BCD_C39_NARROW_BAR $xres;
  200.         $wide   BCD_C39_WIDE_BAR $xres;
  201.         $this->DrawSingleBar($DrawPos$yPos$narrow $ySize);
  202.         $DrawPos += $narrow;
  203.         $DrawPos += $wide;
  204.         $this->DrawSingleBar($DrawPos$yPos$narrow $ySize);
  205.         $DrawPos += $narrow;
  206.         $DrawPos += $narrow;
  207.         $this->DrawSingleBar($DrawPos$yPos$wide $ySize);
  208.         $DrawPos += $wide;
  209.         $DrawPos += $narrow;
  210.         $this->DrawSingleBar($DrawPos$yPos$wide $ySize);
  211.         $DrawPos += $wide;
  212.         $DrawPos += $narrow;
  213.         $this->DrawSingleBar($DrawPos$yPos$narrow$ySize);
  214.         $DrawPos += $narrow;
  215.         return $DrawPos;
  216.     }
  217.     
  218.     /**
  219.      * Draws the barcode object.
  220.      * @param int $xres Horizontal resolution.
  221.      * @return bool true in case of success.
  222.      */
  223.     public function DrawObject($xres{
  224.         $len strlen($this->mValue);
  225.  
  226.         $narrow BCD_C39_NARROW_BAR $xres;
  227.         $wide   BCD_C39_WIDE_BAR $xres;
  228.  
  229.         if (($size $this->GetSize($xres))==0{
  230.             return false;
  231.         }
  232.  
  233.         $cPos 0;
  234.         if ($this->mStyle BCS_ALIGN_CENTER$sPos = (integer)(($this->mWidth - $size 2);
  235.         else if ($this->mStyle BCS_ALIGN_RIGHT$sPos $this->mWidth - $size;
  236.         else $sPos 0;
  237.  
  238.         /* Total height of bar code -Bars only- */
  239.         if ($this->mStyle BCS_DRAW_TEXT$ysize $this->mHeight - BCD_DEFAULT_MAR_Y1 BCD_DEFAULT_MAR_Y2 $this->GetFontHeight($this->mFont);
  240.         else $ysize $this->mHeight - BCD_DEFAULT_MAR_Y1 BCD_DEFAULT_MAR_Y2;
  241.  
  242.         /* Draw text */
  243.         if ($this->mStyle BCS_DRAW_TEXT{
  244.             if ($this->mStyle BCS_STRETCH_TEXT{
  245.                 for ($i=0;$i<$len;$i++{
  246.                     $this->DrawChar($this->mFont$sPos+($narrow*6+$wide*3)+($size/$len)*$i,
  247.                     $ysize BCD_DEFAULT_MAR_Y1 BCD_DEFAULT_TEXT_OFFSET$this->mValue[$i]);
  248.                 }
  249.             else {/* Center */
  250.             $text_width $this->GetFontWidth($this->mFontstrlen($this->mValue);
  251.             $this->DrawText($this->mFont$sPos+(($size-$text_width)/2)+($narrow*6+$wide*3),
  252.             $ysize BCD_DEFAULT_MAR_Y1 BCD_DEFAULT_TEXT_OFFSET$this->mValue);
  253.             }
  254.         }
  255.  
  256.         $DrawPos $this->DrawStart($sPosBCD_DEFAULT_MAR_Y1 $ysize$xres);
  257.         do {
  258.             $c     $this->GetCharIndex($this->mValue[$cPos]);
  259.             $cset  $this->mCharSet[$c];
  260.             $this->DrawSingleBar($DrawPosBCD_DEFAULT_MAR_Y1($cset[0== '0'$narrow $wide $ysize);
  261.             $DrawPos += ($cset[0== '0'$narrow $wide;
  262.             $DrawPos += ($cset[1== '0'$narrow $wide;
  263.             $this->DrawSingleBar($DrawPosBCD_DEFAULT_MAR_Y1($cset[2== '0'$narrow $wide $ysize);
  264.             $DrawPos += ($cset[2== '0'$narrow $wide;
  265.             $DrawPos += ($cset[3== '0'$narrow $wide;
  266.             $this->DrawSingleBar($DrawPosBCD_DEFAULT_MAR_Y1($cset[4== '0'$narrow $wide $ysize);
  267.             $DrawPos += ($cset[4== '0'$narrow $wide;
  268.             $DrawPos += ($cset[5== '0'$narrow $wide;
  269.             $this->DrawSingleBar($DrawPosBCD_DEFAULT_MAR_Y1($cset[6== '0'$narrow $wide $ysize);
  270.             $DrawPos += ($cset[6== '0'$narrow $wide;
  271.             $DrawPos += ($cset[7== '0'$narrow $wide;
  272.             $this->DrawSingleBar($DrawPosBCD_DEFAULT_MAR_Y1($cset[8== '0'$narrow $wide $ysize);
  273.             $DrawPos += ($cset[8== '0'$narrow $wide;
  274.             $DrawPos += $narrow/* Space between chars */
  275.             $cPos++;
  276.         while ($cPos<$len);
  277.         $DrawPos =  $this->DrawStop($DrawPosBCD_DEFAULT_MAR_Y1 $ysize$xres);
  278.         return true;
  279.     }
  280. }
  281.  
  282. //============================================================+
  283. // END OF FILE
  284. //============================================================+
  285. ?>

Documentation generated on Fri, 16 Oct 2009 09:28:45 +0200 by phpDocumentor 1.4.1