Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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