home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / class.phpdocpdf.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  11.2 KB  |  339 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | phpDocumentor                                                          |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2000-2003 Joshua Eichorn, Gregory Beaver                 |
  7. // | Email         jeichorn@phpdoc.org, cellog@phpdoc.org                   |
  8. // | Web           http://www.phpdoc.org                                    |
  9. // | Mirror        http://phpdocu.sourceforge.net/                          |
  10. // | PEAR          http://pear.php.net/package-info.php?pacid=137           |
  11. // +------------------------------------------------------------------------+
  12. // | This source file is subject to version 3.00 of the PHP License,        |
  13. // | that is available at http://www.php.net/license/3_0.txt.               |
  14. // | If you did not receive a copy of the PHP license and are unable to     |
  15. // | obtain it through the world-wide-web, please send a note to            |
  16. // | license@php.net so we can mail you a copy immediately.                 |
  17. // +------------------------------------------------------------------------+
  18. //
  19. /**
  20.  * Helper class, extension to ezpdf
  21.  * @package Converters
  22.  * @subpackage PDFdefault
  23.  */
  24.  
  25. /** ezPdf libraries */
  26. include_once 'phpDocumentor/Converters/PDF/default/class.ezpdf.php';
  27. include_once 'phpDocumentor/Converters/PDF/default/ParserPDF.inc';
  28.  
  29. // define a class extension to allow the use of a callback to get the table of
  30. // contents, and to put the dots in the toc
  31. /**
  32.  * @package Converters
  33.  * @subpackage PDFdefault
  34.  */
  35. class phpdocpdf extends Cezpdf
  36. {
  37.     var $reportContents = array();
  38.     var $indexContents = array();
  39.     var $indents = array();
  40.     var $font_dir = false;
  41.     var $set_pageNumbering = false;
  42.     var $converter;
  43.     var $_save = '';
  44.     var $listType = 'ordered';
  45.     var $_colorStack = array();
  46.  
  47.     function phpdocpdf(&$pdfconverter,$fontdir,$paper='a4',$orientation='portrait')
  48.     {
  49.         Cezpdf::Cezpdf($paper,$orientation);
  50.         $this->converter = $pdfconverter;
  51.         $this->font_dir = $fontdir;
  52.     }
  53.     
  54.     /**
  55.      * This really should be in the parent class
  56.      */
  57.     function getColor()
  58.     {
  59.         return $this->currentColour;
  60.     }
  61.     
  62.     function setColorArray($color)
  63.     {
  64.         $this->setColor($color['r'], $color['g'], $color['b']);
  65.     }
  66.     
  67.     /**
  68.      * Extract Pdfphp-format color from html-format color
  69.      * @return array
  70.      * @access private
  71.      */
  72.     function _extractColor($htmlcolor)
  73.     {
  74.         preg_match('/#([a-fA-F0-9][a-fA-F0-9])([a-fA-F0-9][a-fA-F0-9])([a-fA-F0-9][a-fA-F0-9])/', $htmlcolor, $color);
  75.         if (count($color) != 4)
  76.         {
  77.             return false;
  78.         }
  79.         $red = hexdec($color[1]) / hexdec('FF');
  80.         $green = hexdec($color[2]) / hexdec('FF');
  81.         $blue = hexdec($color[3]) / hexdec('FF');
  82.         return array('r' => $red, 'g' => $green, 'b' => $blue);
  83.     }
  84.     
  85.     function validHTMLColor($color)
  86.     {
  87.         return $this->_extractColor($htmlcolor);
  88.     }
  89.     
  90.     function setHTMLColor($color)
  91.     {
  92.         fancy_debug('toplevel setting to', $color);
  93.         $this->setColor($color['r'], $color['g'], $color['b']);
  94.     }
  95.     
  96.     function textcolor($info)
  97.     {
  98.         if ($info['status'] == 'start')
  99.         {
  100.             array_push($this->_colorStack, $this->getColor());
  101.             $color = $this->_extractColor($info['p']);
  102.             if ($color)
  103.             {
  104. //                fancy_debug('set color to ',$info['p'],$color, $this->_colorStack);
  105.                 $this->setColorArray($color);
  106.             } else
  107.             {
  108.                 array_pop($this->_colorStack);
  109.             }
  110.         } elseif ($info['status'] == 'end')
  111.         {
  112. //            debug('unsetting');
  113.             $this->setColorArray(array_pop($this->_colorStack));
  114.         }
  115.     }
  116.  
  117.     function rf($info)
  118.     {
  119.         $tmp = $info['p'];
  120.         $lvl = $tmp[0];
  121.         $lbl = rawurldecode(substr($tmp,1));
  122.         $num=$this->ezWhatPageNumber($this->ezGetCurrentPageNumber());
  123.         $this->reportContents[] = array($lbl,$num,$lvl );
  124.         $this->addDestination('toc'.(count($this->reportContents)-1),'FitH',$info['y']+$info['height']);
  125.     }
  126.     
  127.     function index($info)
  128.     {
  129.         $res = explode('|||',rawurldecode($info['p']));
  130.         $name = $res[0];
  131.         $descrip = $res[1];
  132.         $letter = $name[0];
  133.         if ($letter == '$') $letter = $name[1];
  134.         $this->indexContents[strtoupper($letter)][] = array($name,$descrip,$this->ezWhatPageNumber($this->ezGetCurrentPageNumber()),count($this->reportContents) - 1);
  135.     }
  136.     
  137.     function IndexLetter($info)
  138.     {
  139.         $letter = $info['p'];
  140.         $this->transaction('start');
  141.         $ok=0;
  142.         while (!$ok){
  143.           $thisPageNum = $this->ezPageCount;
  144.           $this->saveState();
  145.           $this->setColor(0.9,0.9,0.9);
  146.           $this->filledRectangle($this->ez['leftMargin'],$this->y-$this->getFontHeight(18)+$this->getFontDecender(18),$this->ez['pageWidth']-$this->ez['leftMargin']-$this->ez['rightMargin'],$this->getFontHeight(18));
  147.           $this->restoreState();
  148.           $this->_ezText($letter,18,array('justification'=>'left'));
  149.           if ($this->ezPageCount==$thisPageNum){
  150.             $this->transaction('commit');
  151.             $ok=1;
  152.           } else {
  153.             // then we have moved onto a new page, bad bad, as the background colour will be on the old one
  154.             $this->transaction('rewind');
  155.             $this->ezNewPage();
  156.           }
  157.         }
  158.     }
  159.     
  160.     function dots($info)
  161.     {
  162.         // draw a dotted line over to the right and put on a page number
  163.         $tmp = $info['p'];
  164.         $lvl = $tmp[0];
  165.         $lbl = substr($tmp,1);
  166.         $xpos = 520;
  167.         
  168.         switch($lvl)
  169.         {
  170.             case '1':
  171.                 $size=16;
  172.                 $thick=1;
  173.             break;
  174.             case '2':
  175.                 $size=14;
  176.                 $thick=1;
  177.             break;
  178.             case '3':
  179.                 $size=12;
  180.                 $thick=1;
  181.             break;
  182.             case '4':
  183.                 $size=11;
  184.                 $thick=1;
  185.             break;
  186.         }
  187.         
  188.         $adjust = 0;
  189.         if ($size != 16) $adjust = 1;
  190.         $this->saveState();
  191.         $this->setLineStyle($thick,'round','',array(0,10));
  192.         $this->line($xpos - (5*$adjust),$info['y'],$info['x']+5,$info['y']);
  193.         $this->restoreState();
  194.         $this->addText($xpos - (5*$adjust)+5,$info['y'],$size,$lbl);
  195.     }
  196.     
  197.     /**
  198.      * @uses PDFParser extracts all meta-tags and processes text for output
  199.      */
  200.     function ezText($text,$size=0,$options=array(),$test=0)
  201.     {
  202.         $text = str_replace("\t","   ",$text);
  203.         // paragraph breaks
  204.         $text = str_replace("<##P##>","\n    ",$text);
  205.         $text = str_replace("<<c:i",'< <c:i',$text);
  206.         $text = str_replace("ilink>>","ilink> >",$text);
  207.         $this->_save .= $text;
  208.     }
  209.     
  210.     function setupTOC()
  211.     {
  212.         $parser = new PDFParser;
  213.         $parser->parse($this->_save,$this->font_dir,$this);
  214.         $this->_save = '';
  215.     }
  216.     
  217.     function ezOutput($debug = false, $template)
  218.     {
  219.         if ($debug) return $this->_save;
  220.         $this->setupTOC();
  221.         if ($template)
  222.         {
  223.             uksort($this->indexContents,'strnatcasecmp');
  224.             $xpos = 520;
  225.             $z = 0;
  226.             foreach($this->indexContents as $letter => $contents)
  227.             {
  228.                 if ($z++/50 == 0) {phpDocumentor_out('.');flush();}
  229.                 uksort($this->indexContents[$letter],array($this,'mystrnatcasecmp'));
  230.             }
  231.             $template->assign('indexcontents',$this->indexContents);
  232.             $this->ezText($template->fetch('index.tpl'));
  233.             $this->setupTOC();
  234.         }
  235.         return parent::ezOutput();
  236.     }
  237.     
  238.     function _ezText($text,$size=0,$options=array(),$test=0)
  239.     {
  240.         return parent::ezText($text,$size,$options,$test);
  241.     }
  242.     
  243.     function getYPlusOffset($offset)
  244.     {
  245.         return $this->y + $offset;
  246.     }
  247.     
  248.     function addMessage($message)
  249.     {
  250.         return parent::addMessage($message);
  251.         phpDocumentor_out($message."\n");
  252.         flush();
  253.     }
  254.     
  255.     function ezProcessText($text){
  256.       // this function will intially be used to implement underlining support, but could be used for a range of other
  257.       // purposes
  258.       $text = parent::ezProcessText($text);
  259.       $text = str_replace(array('<UL>','</UL>','<LI>','</LI>','<OL>','</OL>','</ol>','<blockquote>','</blockquote>'),
  260.                           array('<ul>','</ul>','<li>','</li>','<ol>','</ul>','</ul>',"<C:indent:20>\n","<C:indent:-20>"),$text);
  261. //      $text = str_replace("<ul>\n","<ul>",$text);
  262.       $text = preg_replace("/\n+\s*(<ul>|<ol>)/", "\n\\1", $text);
  263.       // some problemos fixed here - hack
  264.       $text = preg_replace('/<text [^]]+>/', '', $text);
  265.       $text = str_replace("<li>\n","<li>",$text);
  266.       $text = preg_replace("/\n+\s*<li>/", "<li>", $text);
  267.       $text = str_replace("<mybr>","\n",$text);
  268.       $text = str_replace('</li></ul>','</ul>',$text);
  269.       $text = preg_replace("/^\n(\d+\s+.*)/", '\\1', $text);
  270.       $search = array('<ul>','</ul>','<ol>','<li>','</li>');
  271.       $replace = array("<C:indent:20>\n","\n<C:indent:-20>","\n<C:indent:20:ordered>\n",'<C:bullet>',"\n");
  272.       $text = str_replace($search,$replace,$text);
  273.       $text = preg_replace("/([^\n])<C:bullet/", "\\1\n<C:bullet", $text);
  274.       if (false) {
  275.         $fp = @fopen("C:/Documents and Settings/Owner/Desktop/pdfsourceorig.txt",'a');
  276.         if ($fp)
  277.         {
  278.             fwrite($fp, $text);
  279.             fclose($fp);
  280.         }
  281.       }
  282.       return $text;
  283.     }
  284.     
  285.     function indent($info)
  286.     {
  287.         $stuff = explode(':', $info['p']);
  288.         $margin = $stuff[0];
  289.         if (count($stuff) - 1)
  290.         {
  291.             $this->listType = 'ordered';
  292.             $this->listIndex = 1;
  293.         } else
  294.         {
  295.             if ($margin > 0)
  296.             {
  297.                 $this->listIndex = 1;
  298.             }
  299.             $this->listType = 'unordered';
  300.         }
  301.         $this->ez['leftMargin'] += $margin;
  302.     }
  303.     
  304.     /**
  305.      * @author Murray Shields
  306.      */
  307.     function bullet($Data) 
  308.     {
  309.         if ($this->listType == 'ordered')
  310.         {
  311.             return $this->orderedBullet($Data);
  312.         }
  313.         $D = abs($Data["decender"]); 
  314.         $X = $Data["x"] - ($D * 2) - 10; 
  315.         $Y = $Data["y"] + ($D * 1.5); 
  316.         $this->setLineStyle($D, "butt", "miter", array()); 
  317.         $this->setColor(0,0,0); 
  318.         $this->ellipse($X, $Y, 1); 
  319.     }
  320.     
  321.     function orderedBullet($info)
  322.     {
  323.         $this->addText($info['x']-20, $info['y']-1, 10, $this->listIndex++ . '.');
  324.     }
  325.     
  326.     function ezNewPage($debug=false)
  327.     {
  328.         parent::ezNewPage();
  329.         if (!$this->set_pageNumbering)
  330.         {
  331.             $template = $this->converter->newSmarty();
  332.             $parser = new PDFParser;
  333.             $parser->parse($template->fetch('pagenumbering.tpl'),$this->font_dir,$this);
  334.         }
  335.         $this->set_pageNumbering = true;
  336.     }
  337. }
  338. ?>
  339.