source: branches/version-2_4/data/module/pdf/fpdf.php @ 18394

Revision 18394, 40.0 KB checked in by kajiwara, 14 years ago (diff)

#529 pdf 関連モジュールを適切な場所に配置変更いたしました。

Line 
1<?php
2/*******************************************************************************
3* Software: FPDF                                                               *
4* Version:  1.53                                                               *
5* Date:     2004-12-31                                                         *
6* Author:   Olivier PLATHEY                                                    *
7* License:  Freeware                                                           *
8*                                                                              *
9* You may use, modify and redistribute this software as you wish.              *
10*******************************************************************************/
11
12if(!class_exists('FPDF'))
13{
14define('FPDF_VERSION','1.53');
15
16class FPDF
17{
18//Private properties
19var $page;               //current page number
20var $n;                  //current object number
21var $offsets;            //array of object offsets
22var $buffer;             //buffer holding in-memory PDF
23var $pages;              //array containing pages
24var $state;              //current document state
25var $compress;           //compression flag
26var $DefOrientation;     //default orientation
27var $CurOrientation;     //current orientation
28var $OrientationChanges; //array indicating orientation changes
29var $k;                  //scale factor (number of points in user unit)
30var $fwPt,$fhPt;         //dimensions of page format in points
31var $fw,$fh;             //dimensions of page format in user unit
32var $wPt,$hPt;           //current dimensions of page in points
33var $w,$h;               //current dimensions of page in user unit
34var $lMargin;            //left margin
35var $tMargin;            //top margin
36var $rMargin;            //right margin
37var $bMargin;            //page break margin
38var $cMargin;            //cell margin
39var $x,$y;               //current position in user unit for cell positioning
40var $lasth;              //height of last cell printed
41var $LineWidth;          //line width in user unit
42var $CoreFonts;          //array of standard font names
43var $fonts;              //array of used fonts
44var $FontFiles;          //array of font files
45var $diffs;              //array of encoding differences
46var $images;             //array of used images
47var $PageLinks;          //array of links in pages
48var $links;              //array of internal links
49var $FontFamily;         //current font family
50var $FontStyle;          //current font style
51var $underline;          //underlining flag
52var $CurrentFont;        //current font info
53var $FontSizePt;         //current font size in points
54var $FontSize;           //current font size in user unit
55var $DrawColor;          //commands for drawing color
56var $FillColor;          //commands for filling color
57var $TextColor;          //commands for text color
58var $ColorFlag;          //indicates whether fill and text colors are different
59var $ws;                 //word spacing
60var $AutoPageBreak;      //automatic page breaking
61var $PageBreakTrigger;   //threshold used to trigger page breaks
62var $InFooter;           //flag set when processing footer
63var $ZoomMode;           //zoom display mode
64var $LayoutMode;         //layout display mode
65var $title;              //title
66var $subject;            //subject
67var $author;             //author
68var $keywords;           //keywords
69var $creator;            //creator
70var $AliasNbPages;       //alias for total number of pages
71var $PDFVersion;         //PDF version number
72
73/*******************************************************************************
74*                                                                              *
75*                               Public methods                                 *
76*                                                                              *
77*******************************************************************************/
78function FPDF($orientation='P',$unit='mm',$format='A4')
79{
80    //Some checks
81    $this->_dochecks();
82    //Initialization of properties
83    $this->page=0;
84    $this->n=2;
85    $this->buffer='';
86    $this->pages=array();
87    $this->OrientationChanges=array();
88    $this->state=0;
89    $this->fonts=array();
90    $this->FontFiles=array();
91    $this->diffs=array();
92    $this->images=array();
93    $this->links=array();
94    $this->InFooter=false;
95    $this->lasth=0;
96    $this->FontFamily='';
97    $this->FontStyle='';
98    $this->FontSizePt=12;
99    $this->underline=false;
100    $this->DrawColor='0 G';
101    $this->FillColor='0 g';
102    $this->TextColor='0 g';
103    $this->ColorFlag=false;
104    $this->ws=0;
105    //Standard fonts
106    $this->CoreFonts=array('courier'=>'Courier','courierB'=>'Courier-Bold','courierI'=>'Courier-Oblique','courierBI'=>'Courier-BoldOblique',
107        'helvetica'=>'Helvetica','helveticaB'=>'Helvetica-Bold','helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',
108        'times'=>'Times-Roman','timesB'=>'Times-Bold','timesI'=>'Times-Italic','timesBI'=>'Times-BoldItalic',
109        'symbol'=>'Symbol','zapfdingbats'=>'ZapfDingbats');
110    //Scale factor
111    if($unit=='pt')
112        $this->k=1;
113    elseif($unit=='mm')
114        $this->k=72/25.4;
115    elseif($unit=='cm')
116        $this->k=72/2.54;
117    elseif($unit=='in')
118        $this->k=72;
119    else
120        $this->Error('Incorrect unit: '.$unit);
121    //Page format
122    if(is_string($format))
123    {
124        $format=strtolower($format);
125        if($format=='a3')
126            $format=array(841.89,1190.55);
127        elseif($format=='a4')
128            $format=array(595.28,841.89);
129        elseif($format=='a5')
130            $format=array(420.94,595.28);
131        elseif($format=='letter')
132            $format=array(612,792);
133        elseif($format=='legal')
134            $format=array(612,1008);
135        else
136            $this->Error('Unknown page format: '.$format);
137        $this->fwPt=$format[0];
138        $this->fhPt=$format[1];
139    }
140    else
141    {
142        $this->fwPt=$format[0]*$this->k;
143        $this->fhPt=$format[1]*$this->k;
144    }
145    $this->fw=$this->fwPt/$this->k;
146    $this->fh=$this->fhPt/$this->k;
147    //Page orientation
148    $orientation=strtolower($orientation);
149    if($orientation=='p' || $orientation=='portrait')
150    {
151        $this->DefOrientation='P';
152        $this->wPt=$this->fwPt;
153        $this->hPt=$this->fhPt;
154    }
155    elseif($orientation=='l' || $orientation=='landscape')
156    {
157        $this->DefOrientation='L';
158        $this->wPt=$this->fhPt;
159        $this->hPt=$this->fwPt;
160    }
161    else
162        $this->Error('Incorrect orientation: '.$orientation);
163    $this->CurOrientation=$this->DefOrientation;
164    $this->w=$this->wPt/$this->k;
165    $this->h=$this->hPt/$this->k;
166    //Page margins (1 cm)
167    $margin=28.35/$this->k;
168    $this->SetMargins($margin,$margin);
169    //Interior cell margin (1 mm)
170    $this->cMargin=$margin/10;
171    //Line width (0.2 mm)
172    $this->LineWidth=.567/$this->k;
173    //Automatic page break
174    $this->SetAutoPageBreak(true,2*$margin);
175    //Full width display mode
176    $this->SetDisplayMode('fullwidth');
177    //Enable compression
178    $this->SetCompression(true);
179    //Set default PDF version number
180    $this->PDFVersion='1.3';
181}
182
183function SetMargins($left,$top,$right=-1)
184{
185    //Set left, top and right margins
186    $this->lMargin=$left;
187    $this->tMargin=$top;
188    if($right==-1)
189        $right=$left;
190    $this->rMargin=$right;
191}
192
193function SetLeftMargin($margin)
194{
195    //Set left margin
196    $this->lMargin=$margin;
197    if($this->page>0 && $this->x<$margin)
198        $this->x=$margin;
199}
200
201function SetTopMargin($margin)
202{
203    //Set top margin
204    $this->tMargin=$margin;
205}
206
207function SetRightMargin($margin)
208{
209    //Set right margin
210    $this->rMargin=$margin;
211}
212
213function SetAutoPageBreak($auto,$margin=0)
214{
215    //Set auto page break mode and triggering margin
216    $this->AutoPageBreak=$auto;
217    $this->bMargin=$margin;
218    $this->PageBreakTrigger=$this->h-$margin;
219}
220
221function SetDisplayMode($zoom,$layout='continuous')
222{
223    //Set display mode in viewer
224    if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
225        $this->ZoomMode=$zoom;
226    else
227        $this->Error('Incorrect zoom display mode: '.$zoom);
228    if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
229        $this->LayoutMode=$layout;
230    else
231        $this->Error('Incorrect layout display mode: '.$layout);
232}
233
234function SetCompression($compress)
235{
236    //Set page compression
237    if(function_exists('gzcompress'))
238        $this->compress=$compress;
239    else
240        $this->compress=false;
241}
242
243function SetTitle($title)
244{
245    //Title of document
246    $this->title=$title;
247}
248
249function SetSubject($subject)
250{
251    //Subject of document
252    $this->subject=$subject;
253}
254
255function SetAuthor($author)
256{
257    //Author of document
258    $this->author=$author;
259}
260
261function SetKeywords($keywords)
262{
263    //Keywords of document
264    $this->keywords=$keywords;
265}
266
267function SetCreator($creator)
268{
269    //Creator of document
270    $this->creator=$creator;
271}
272
273function AliasNbPages($alias='{nb}')
274{
275    //Define an alias for total number of pages
276    $this->AliasNbPages=$alias;
277}
278
279function Error($msg)
280{
281    //Fatal error
282    die('<B>FPDF error: </B>'.$msg);
283}
284
285function Open()
286{
287    //Begin document
288    $this->state=1;
289}
290
291function Close()
292{
293    //Terminate document
294    if($this->state==3)
295        return;
296    if($this->page==0)
297        $this->AddPage();
298    //Page footer
299    $this->InFooter=true;
300    $this->Footer();
301    $this->InFooter=false;
302    //Close page
303    $this->_endpage();
304    //Close document
305    $this->_enddoc();
306}
307
308function AddPage($orientation='')
309{
310    //Start a new page
311    if($this->state==0)
312        $this->Open();
313    $family=$this->FontFamily;
314    $style=$this->FontStyle.($this->underline ? 'U' : '');
315    $size=$this->FontSizePt;
316    $lw=$this->LineWidth;
317    $dc=$this->DrawColor;
318    $fc=$this->FillColor;
319    $tc=$this->TextColor;
320    $cf=$this->ColorFlag;
321    if($this->page>0)
322    {
323        //Page footer
324        $this->InFooter=true;
325        $this->Footer();
326        $this->InFooter=false;
327        //Close page
328        $this->_endpage();
329    }
330    //Start new page
331    $this->_beginpage($orientation);
332    //Set line cap style to square
333    $this->_out('2 J');
334    //Set line width
335    $this->LineWidth=$lw;
336    $this->_out(sprintf('%.2f w',$lw*$this->k));
337    //Set font
338    if($family)
339        $this->SetFont($family,$style,$size);
340    //Set colors
341    $this->DrawColor=$dc;
342    if($dc!='0 G')
343        $this->_out($dc);
344    $this->FillColor=$fc;
345    if($fc!='0 g')
346        $this->_out($fc);
347    $this->TextColor=$tc;
348    $this->ColorFlag=$cf;
349    //Page header
350    $this->Header();
351    //Restore line width
352    if($this->LineWidth!=$lw)
353    {
354        $this->LineWidth=$lw;
355        $this->_out(sprintf('%.2f w',$lw*$this->k));
356    }
357    //Restore font
358    if($family)
359        $this->SetFont($family,$style,$size);
360    //Restore colors
361    if($this->DrawColor!=$dc)
362    {
363        $this->DrawColor=$dc;
364        $this->_out($dc);
365    }
366    if($this->FillColor!=$fc)
367    {
368        $this->FillColor=$fc;
369        $this->_out($fc);
370    }
371    $this->TextColor=$tc;
372    $this->ColorFlag=$cf;
373}
374
375function Header()
376{
377    //To be implemented in your own inherited class
378}
379
380function Footer()
381{
382    //To be implemented in your own inherited class
383}
384
385function PageNo()
386{
387    //Get current page number
388    return $this->page;
389}
390
391function SetDrawColor($r,$g=-1,$b=-1)
392{
393    //Set color for all stroking operations
394    if(($r==0 && $g==0 && $b==0) || $g==-1)
395        $this->DrawColor=sprintf('%.3f G',$r/255);
396    else
397        $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
398    if($this->page>0)
399        $this->_out($this->DrawColor);
400}
401
402function SetFillColor($r,$g=-1,$b=-1)
403{
404    //Set color for all filling operations
405    if(($r==0 && $g==0 && $b==0) || $g==-1)
406        $this->FillColor=sprintf('%.3f g',$r/255);
407    else
408        $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
409    $this->ColorFlag=($this->FillColor!=$this->TextColor);
410    if($this->page>0)
411        $this->_out($this->FillColor);
412}
413
414function SetTextColor($r,$g=-1,$b=-1)
415{
416    //Set color for text
417    if(($r==0 && $g==0 && $b==0) || $g==-1)
418        $this->TextColor=sprintf('%.3f g',$r/255);
419    else
420        $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
421    $this->ColorFlag=($this->FillColor!=$this->TextColor);
422}
423
424function GetStringWidth($s)
425{
426    //Get width of a string in the current font
427    $s=(string)$s;
428    $cw=&$this->CurrentFont['cw'];
429    $w=0;
430    $l=strlen($s);
431    for($i=0;$i<$l;$i++)
432        $w+=$cw[$s{$i}];
433    return $w*$this->FontSize/1000;
434}
435
436function SetLineWidth($width)
437{
438    //Set line width
439    $this->LineWidth=$width;
440    if($this->page>0)
441        $this->_out(sprintf('%.2f w',$width*$this->k));
442}
443
444function Line($x1,$y1,$x2,$y2)
445{
446    //Draw a line
447    $this->_out(sprintf('%.2f %.2f m %.2f %.2f l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k));
448}
449
450function Rect($x,$y,$w,$h,$style='')
451{
452    //Draw a rectangle
453    if($style=='F')
454        $op='f';
455    elseif($style=='FD' || $style=='DF')
456        $op='B';
457    else
458        $op='S';
459    $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
460}
461
462function AddFont($family,$style='',$file='')
463{
464    //Add a TrueType or Type1 font
465    $family=strtolower($family);
466    if($file=='')
467        $file=str_replace(' ','',$family).strtolower($style).'.php';
468    if($family=='arial')
469        $family='helvetica';
470    $style=strtoupper($style);
471    if($style=='IB')
472        $style='BI';
473    $fontkey=$family.$style;
474    if(isset($this->fonts[$fontkey]))
475        $this->Error('Font already added: '.$family.' '.$style);
476    include($this->_getfontpath().$file);
477    if(!isset($name))
478        $this->Error('Could not include font definition file');
479    $i=count($this->fonts)+1;
480    $this->fonts[$fontkey]=array('i'=>$i,'type'=>$type,'name'=>$name,'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file);
481    if($diff)
482    {
483        //Search existing encodings
484        $d=0;
485        $nb=count($this->diffs);
486        for($i=1;$i<=$nb;$i++)
487        {
488            if($this->diffs[$i]==$diff)
489            {
490                $d=$i;
491                break;
492            }
493        }
494        if($d==0)
495        {
496            $d=$nb+1;
497            $this->diffs[$d]=$diff;
498        }
499        $this->fonts[$fontkey]['diff']=$d;
500    }
501    if($file)
502    {
503        if($type=='TrueType')
504            $this->FontFiles[$file]=array('length1'=>$originalsize);
505        else
506            $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
507    }
508}
509
510function SetFont($family,$style='',$size=0)
511{
512    //Select a font; size given in points
513    global $fpdf_charwidths;
514
515    $family=strtolower($family);
516    if($family=='')
517        $family=$this->FontFamily;
518    if($family=='arial')
519        $family='helvetica';
520    elseif($family=='symbol' || $family=='zapfdingbats')
521        $style='';
522    $style=strtoupper($style);
523    if(strpos($style,'U')!==false)
524    {
525        $this->underline=true;
526        $style=str_replace('U','',$style);
527    }
528    else
529        $this->underline=false;
530    if($style=='IB')
531        $style='BI';
532    if($size==0)
533        $size=$this->FontSizePt;
534    //Test if font is already selected
535    if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
536        return;
537    //Test if used for the first time
538    $fontkey=$family.$style;
539    if(!isset($this->fonts[$fontkey]))
540    {
541        //Check if one of the standard fonts
542        if(isset($this->CoreFonts[$fontkey]))
543        {
544            if(!isset($fpdf_charwidths[$fontkey]))
545            {
546                //Load metric file
547                $file=$family;
548                if($family=='times' || $family=='helvetica')
549                    $file.=strtolower($style);
550                include($this->_getfontpath().$file.'.php');
551                if(!isset($fpdf_charwidths[$fontkey]))
552                    $this->Error('Could not include font metric file');
553            }
554            $i=count($this->fonts)+1;
555            $this->fonts[$fontkey]=array('i'=>$i,'type'=>'core','name'=>$this->CoreFonts[$fontkey],'up'=>-100,'ut'=>50,'cw'=>$fpdf_charwidths[$fontkey]);
556        }
557        else
558            $this->Error('Undefined font: '.$family.' '.$style);
559    }
560    //Select it
561    $this->FontFamily=$family;
562    $this->FontStyle=$style;
563    $this->FontSizePt=$size;
564    $this->FontSize=$size/$this->k;
565    $this->CurrentFont=&$this->fonts[$fontkey];
566    if($this->page>0)
567        $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
568}
569
570function SetFontSize($size)
571{
572    //Set font size in points
573    if($this->FontSizePt==$size)
574        return;
575    $this->FontSizePt=$size;
576    $this->FontSize=$size/$this->k;
577    if($this->page>0)
578        $this->_out(sprintf('BT /F%d %.2f Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
579}
580
581function AddLink()
582{
583    //Create a new internal link
584    $n=count($this->links)+1;
585    $this->links[$n]=array(0,0);
586    return $n;
587}
588
589function SetLink($link,$y=0,$page=-1)
590{
591    //Set destination of internal link
592    if($y==-1)
593        $y=$this->y;
594    if($page==-1)
595        $page=$this->page;
596    $this->links[$link]=array($page,$y);
597}
598
599function Link($x,$y,$w,$h,$link)
600{
601    //Put a link on the page
602    $this->PageLinks[$this->page][]=array($x*$this->k,$this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link);
603}
604
605function Text($x,$y,$txt)
606{
607    //Output a string
608    $s=sprintf('BT %.2f %.2f Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
609    if($this->underline && $txt!='')
610        $s.=' '.$this->_dounderline($x,$y,$txt);
611    if($this->ColorFlag)
612        $s='q '.$this->TextColor.' '.$s.' Q';
613    $this->_out($s);
614}
615
616function AcceptPageBreak()
617{
618    //Accept automatic page break or not
619    return $this->AutoPageBreak;
620}
621
622function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
623{
624    //Output a cell
625    $k=$this->k;
626    if($this->y+$h>$this->PageBreakTrigger && !$this->InFooter && $this->AcceptPageBreak())
627    {
628        //Automatic page break
629        $x=$this->x;
630        $ws=$this->ws;
631        if($ws>0)
632        {
633            $this->ws=0;
634            $this->_out('0 Tw');
635        }
636        $this->AddPage($this->CurOrientation);
637        $this->x=$x;
638        if($ws>0)
639        {
640            $this->ws=$ws;
641            $this->_out(sprintf('%.3f Tw',$ws*$k));
642        }
643    }
644    if($w==0)
645        $w=$this->w-$this->rMargin-$this->x;
646    $s='';
647    if($fill==1 || $border==1)
648    {
649        if($fill==1)
650            $op=($border==1) ? 'B' : 'f';
651        else
652            $op='S';
653        $s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
654    }
655    if(is_string($border))
656    {
657        $x=$this->x;
658        $y=$this->y;
659        if(strpos($border,'L')!==false)
660            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
661        if(strpos($border,'T')!==false)
662            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
663        if(strpos($border,'R')!==false)
664            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
665        if(strpos($border,'B')!==false)
666            $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
667    }
668    if($txt!=='')
669    {
670        if($align=='R')
671            $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
672        elseif($align=='C')
673            $dx=($w-$this->GetStringWidth($txt))/2;
674        else
675            $dx=$this->cMargin;
676        if($this->ColorFlag)
677            $s.='q '.$this->TextColor.' ';
678        $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
679        $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
680        if($this->underline)
681            $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
682        if($this->ColorFlag)
683            $s.=' Q';
684        if($link)
685            $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
686    }
687    if($s)
688        $this->_out($s);
689    $this->lasth=$h;
690    if($ln>0)
691    {
692        //Go to next line
693        $this->y+=$h;
694        if($ln==1)
695            $this->x=$this->lMargin;
696    }
697    else
698        $this->x+=$w;
699}
700
701function MultiCell($w,$h,$txt,$border=0,$align='J',$fill=0, $ln = 2)
702{
703    //Output text with automatic or explicit line breaks
704    $cw=&$this->CurrentFont['cw'];
705    if($w==0)
706        $w=$this->w-$this->rMargin-$this->x;
707    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
708    $s=str_replace("\r",'',$txt);
709    $nb=strlen($s);
710    if($nb>0 && $s[$nb-1]=="\n")
711        $nb--;
712    $b=0;
713    if($border)
714    {
715        if($border==1)
716        {
717            $border='LTRB';
718            $b='LRT';
719            $b2='LR';
720        }
721        else
722        {
723            $b2='';
724            if(strpos($border,'L')!==false)
725                $b2.='L';
726            if(strpos($border,'R')!==false)
727                $b2.='R';
728            $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
729        }
730    }
731    $sep=-1;
732    $i=0;
733    $j=0;
734    $l=0;
735    $ns=0;
736    $nl=1;
737    $this->rise_h = 0; //‚‚³ŒvŽZ—p
738   
739    while($i<$nb)
740    {
741        //Get next character
742        $c=$s{$i};
743        if($c=="\n")
744        {
745            //Explicit line break
746            if($this->ws>0)
747            {
748                $this->ws=0;
749                $this->_out('0 Tw');
750            }
751            $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
752            $i++;
753            $sep=-1;
754            $j=$i;
755            $l=0;
756            $ns=0;
757            $nl++;
758            $this->rise_h += $h; //‚‚³ŒvŽZ—p
759           
760            if($border && $nl==2)
761                $b=$b2;
762            continue;
763        }
764        if($c==' ')
765        {
766            $sep=$i;
767            $ls=$l;
768            $ns++;
769        }
770        $l+=$cw[$c];
771        if($l>$wmax)
772        {
773            //Automatic line break
774            if($sep==-1)
775            {
776                if($i==$j)
777                    $i++;
778                if($this->ws>0)
779                {
780                    $this->ws=0;
781                    $this->_out('0 Tw');
782                }
783                $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
784            }
785            else
786            {
787                if($align=='J')
788                {
789                    $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
790                    $this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
791                }
792                $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
793                $i=$sep+1;
794            }
795            $this->rise_h += $h; //‚‚³ŒvŽZ—p
796            $sep=-1;
797            $j=$i;
798            $l=0;
799            $ns=0;
800            $nl++;
801            if($border && $nl==2)
802                $b=$b2;
803        }
804        else
805            $i++;
806    }
807    //Last chunk
808    if($this->ws>0)
809    {
810        $this->ws=0;
811        $this->_out('0 Tw');
812    }
813    if($border && strpos($border,'B')!==false)
814        $b.='B';
815    $this->Cell($w,$h,substr($s,$j,$i-$j),$b,$ln,$align,$fill);
816    $this->rise_h += $h; //‘‰Á•ª‚̍‚‚³‚ðŒvŽZ
817    //‰üs‚È‚µÝ’è‚©‚A‚‚³‚ª‹K’è‚̍‚‚³ˆÈã‚Å‚ ‚ê‚ÎYŽ²‚ðÝ’肵‚È‚¨‚·B
818    if($ln == 0 and $h < $this->rise_h) {
819      $this->y = $this->y - $this->rise_h + $h;
820    }
821   
822    //$this->x=$this->lMargin;
823}
824
825function Write($h,$txt,$link='')
826{
827    //Output text in flowing mode
828    $cw=&$this->CurrentFont['cw'];
829    $w=$this->w-$this->rMargin-$this->x;
830    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
831    $s=str_replace("\r",'',$txt);
832    $nb=strlen($s);
833    $sep=-1;
834    $i=0;
835    $j=0;
836    $l=0;
837    $nl=1;
838    while($i<$nb)
839    {
840        //Get next character
841        $c=$s{$i};
842        if($c=="\n")
843        {
844            //Explicit line break
845            $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
846            $i++;
847            $sep=-1;
848            $j=$i;
849            $l=0;
850            if($nl==1)
851            {
852                $this->x=$this->lMargin;
853                $w=$this->w-$this->rMargin-$this->x;
854                $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
855            }
856            $nl++;
857            continue;
858        }
859        if($c==' ')
860            $sep=$i;
861        $l+=$cw[$c];
862        if($l>$wmax)
863        {
864            //Automatic line break
865            if($sep==-1)
866            {
867                if($this->x>$this->lMargin)
868                {
869                    //Move to next line
870                    $this->x=$this->lMargin;
871                    $this->y+=$h;
872                    $w=$this->w-$this->rMargin-$this->x;
873                    $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
874                    $i++;
875                    $nl++;
876                    continue;
877                }
878                if($i==$j)
879                    $i++;
880                $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
881            }
882            else
883            {
884                $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
885                $i=$sep+1;
886            }
887            $sep=-1;
888            $j=$i;
889            $l=0;
890            if($nl==1)
891            {
892                $this->x=$this->lMargin;
893                $w=$this->w-$this->rMargin-$this->x;
894                $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
895            }
896            $nl++;
897        }
898        else
899            $i++;
900    }
901    //Last chunk
902    if($i!=$j)
903        $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link);
904}
905
906function Image($file,$x,$y,$w=0,$h=0,$type='',$link='')
907{
908    //Put an image on the page
909    if(!isset($this->images[$file]))
910    {
911        //First use of image, get info
912        if($type=='')
913        {
914            $pos=strrpos($file,'.');
915            if(!$pos)
916                $this->Error('Image file has no extension and no type was specified: '.$file);
917            $type=substr($file,$pos+1);
918        }
919        $type=strtolower($type);
920        $mqr=get_magic_quotes_runtime();
921        set_magic_quotes_runtime(0);
922        if($type=='jpg' || $type=='jpeg')
923            $info=$this->_parsejpg($file);
924        elseif($type=='png')
925            $info=$this->_parsepng($file);
926        else
927        {
928            //Allow for additional formats
929            $mtd='_parse'.$type;
930            if(!method_exists($this,$mtd))
931                $this->Error('Unsupported image type: '.$type);
932            $info=$this->$mtd($file);
933        }
934        set_magic_quotes_runtime($mqr);
935        $info['i']=count($this->images)+1;
936        $this->images[$file]=$info;
937    }
938    else
939        $info=$this->images[$file];
940    //Automatic width and height calculation if needed
941    if($w==0 && $h==0)
942    {
943        //Put image at 72 dpi
944        $w=$info['w']/$this->k;
945        $h=$info['h']/$this->k;
946    }
947    if($w==0)
948        $w=$h*$info['w']/$info['h'];
949    if($h==0)
950        $h=$w*$info['h']/$info['w'];
951    $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i']));
952    if($link)
953        $this->Link($x,$y,$w,$h,$link);
954}
955
956function Ln($h='')
957{
958    //Line feed; default value is last cell height
959    $this->x=$this->lMargin;
960    if(is_string($h))
961        $this->y+=$this->lasth;
962    else
963        $this->y+=$h;
964}
965
966function GetX()
967{
968    //Get x position
969    return $this->x;
970}
971
972function SetX($x)
973{
974    //Set x position
975    if($x>=0)
976        $this->x=$x;
977    else
978        $this->x=$this->w+$x;
979}
980
981function GetY()
982{
983    //Get y position
984    return $this->y;
985}
986
987function SetY($y)
988{
989    //Set y position and reset x
990    $this->x=$this->lMargin;
991    if($y>=0)
992        $this->y=$y;
993    else
994        $this->y=$this->h+$y;
995}
996
997function SetXY($x,$y)
998{
999    //Set x and y positions
1000    $this->SetY($y);
1001    $this->SetX($x);
1002}
1003
1004function Output($name='',$dest='')
1005{
1006    //Output PDF to some destination
1007    //Finish document if necessary
1008    if($this->state<3)
1009        $this->Close();
1010    //Normalize parameters
1011    if(is_bool($dest))
1012        $dest=$dest ? 'D' : 'F';
1013    $dest=strtoupper($dest);
1014    if($dest=='')
1015    {
1016        if($name=='')
1017        {
1018            $name='doc.pdf';
1019            $dest='I';
1020        }
1021        else
1022            $dest='F';
1023    }
1024    switch($dest)
1025    {
1026        case 'I':
1027            //Send to standard output
1028            if(ob_get_contents())
1029                $this->Error('Some data has already been output, can\'t send PDF file');
1030            if(php_sapi_name()!='cli')
1031            {
1032                //We send to a browser
1033                header('Content-Type: application/pdf');
1034                if(headers_sent())
1035                    $this->Error('Some data has already been output to browser, can\'t send PDF file');
1036                header('Content-Length: '.strlen($this->buffer));
1037                header('Content-disposition: inline; filename="'.$name.'"');
1038            }
1039            echo $this->buffer;
1040            break;
1041        case 'D':
1042            //Download file
1043            if(ob_get_contents())
1044                $this->Error('Some data has already been output, can\'t send PDF file');
1045            if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
1046                header('Content-Type: application/force-download');
1047            else
1048                header('Content-Type: application/octet-stream');
1049            if(headers_sent())
1050                $this->Error('Some data has already been output to browser, can\'t send PDF file');
1051            header('Content-Length: '.strlen($this->buffer));
1052            header('Content-disposition: attachment; filename="'.$name.'"');
1053            echo $this->buffer;
1054            break;
1055        case 'F':
1056            //Save to local file
1057            $f=fopen($name,'wb');
1058            if(!$f)
1059                $this->Error('Unable to create output file: '.$name);
1060            fwrite($f,$this->buffer,strlen($this->buffer));
1061            fclose($f);
1062            break;
1063        case 'S':
1064            //Return as a string
1065            return $this->buffer;
1066        default:
1067            $this->Error('Incorrect output destination: '.$dest);
1068    }
1069    return '';
1070}
1071
1072/*******************************************************************************
1073*                                                                              *
1074*                              Protected methods                               *
1075*                                                                              *
1076*******************************************************************************/
1077function _dochecks()
1078{
1079    //Check for locale-related bug
1080    if(1.1==1)
1081        $this->Error('Don\'t alter the locale before including class file');
1082    //Check for decimal separator
1083    if(sprintf('%.1f',1.0)!='1.0')
1084        setlocale(LC_NUMERIC,'C');
1085}
1086
1087function _getfontpath()
1088{
1089    if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font'))
1090        define('FPDF_FONTPATH',dirname(__FILE__).'/font/');
1091    return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
1092}
1093
1094function _putpages()
1095{
1096    $nb=$this->page;
1097    if(!empty($this->AliasNbPages))
1098    {
1099        //Replace number of pages
1100        for($n=1;$n<=$nb;$n++)
1101            $this->pages[$n]=str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
1102    }
1103    if($this->DefOrientation=='P')
1104    {
1105        $wPt=$this->fwPt;
1106        $hPt=$this->fhPt;
1107    }
1108    else
1109    {
1110        $wPt=$this->fhPt;
1111        $hPt=$this->fwPt;
1112    }
1113    $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1114    for($n=1;$n<=$nb;$n++)
1115    {
1116        //Page
1117        $this->_newobj();
1118        $this->_out('<</Type /Page');
1119        $this->_out('/Parent 1 0 R');
1120        if(isset($this->OrientationChanges[$n]))
1121            $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
1122        $this->_out('/Resources 2 0 R');
1123        if(isset($this->PageLinks[$n]))
1124        {
1125            //Links
1126            $annots='/Annots [';
1127            foreach($this->PageLinks[$n] as $pl)
1128            {
1129                $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1130                $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1131                if(is_string($pl[4]))
1132                    $annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1133                else
1134                {
1135                    $l=$this->links[$pl[4]];
1136                    $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
1137                    $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
1138                }
1139            }
1140            $this->_out($annots.']');
1141        }
1142        $this->_out('/Contents '.($this->n+1).' 0 R>>');
1143        $this->_out('endobj');
1144        //Page content
1145        $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
1146        $this->_newobj();
1147        $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
1148        $this->_putstream($p);
1149        $this->_out('endobj');
1150    }
1151    //Pages root
1152    $this->offsets[1]=strlen($this->buffer);
1153    $this->_out('1 0 obj');
1154    $this->_out('<</Type /Pages');
1155    $kids='/Kids [';
1156    for($i=0;$i<$nb;$i++)
1157        $kids.=(3+2*$i).' 0 R ';
1158    $this->_out($kids.']');
1159    $this->_out('/Count '.$nb);
1160    $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
1161    $this->_out('>>');
1162    $this->_out('endobj');
1163}
1164
1165function _putfonts()
1166{
1167    $nf=$this->n;
1168    foreach($this->diffs as $diff)
1169    {
1170        //Encodings
1171        $this->_newobj();
1172        $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
1173        $this->_out('endobj');
1174    }
1175    $mqr=get_magic_quotes_runtime();
1176    set_magic_quotes_runtime(0);
1177    foreach($this->FontFiles as $file=>$info)
1178    {
1179        //Font file embedding
1180        $this->_newobj();
1181        $this->FontFiles[$file]['n']=$this->n;
1182        $font='';
1183        $f=fopen($this->_getfontpath().$file,'rb',1);
1184        if(!$f)
1185            $this->Error('Font file not found');
1186        while(!feof($f))
1187            $font.=fread($f,8192);
1188        fclose($f);
1189        $compressed=(substr($file,-2)=='.z');
1190        if(!$compressed && isset($info['length2']))
1191        {
1192            $header=(ord($font{0})==128);
1193            if($header)
1194            {
1195                //Strip first binary header
1196                $font=substr($font,6);
1197            }
1198            if($header && ord($font{$info['length1']})==128)
1199            {
1200                //Strip second binary header
1201                $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
1202            }
1203        }
1204        $this->_out('<</Length '.strlen($font));
1205        if($compressed)
1206            $this->_out('/Filter /FlateDecode');
1207        $this->_out('/Length1 '.$info['length1']);
1208        if(isset($info['length2']))
1209            $this->_out('/Length2 '.$info['length2'].' /Length3 0');
1210        $this->_out('>>');
1211        $this->_putstream($font);
1212        $this->_out('endobj');
1213    }
1214    set_magic_quotes_runtime($mqr);
1215    foreach($this->fonts as $k=>$font)
1216    {
1217        //Font objects
1218        $this->fonts[$k]['n']=$this->n+1;
1219        $type=$font['type'];
1220        $name=$font['name'];
1221        if($type=='core')
1222        {
1223            //Standard font
1224            $this->_newobj();
1225            $this->_out('<</Type /Font');
1226            $this->_out('/BaseFont /'.$name);
1227            $this->_out('/Subtype /Type1');
1228            if($name!='Symbol' && $name!='ZapfDingbats')
1229                $this->_out('/Encoding /WinAnsiEncoding');
1230            $this->_out('>>');
1231            $this->_out('endobj');
1232        }
1233        elseif($type=='Type1' || $type=='TrueType')
1234        {
1235            //Additional Type1 or TrueType font
1236            $this->_newobj();
1237            $this->_out('<</Type /Font');
1238            $this->_out('/BaseFont /'.$name);
1239            $this->_out('/Subtype /'.$type);
1240            $this->_out('/FirstChar 32 /LastChar 255');
1241            $this->_out('/Widths '.($this->n+1).' 0 R');
1242            $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1243            if($font['enc'])
1244            {
1245                if(isset($font['diff']))
1246                    $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
1247                else
1248                    $this->_out('/Encoding /WinAnsiEncoding');
1249            }
1250            $this->_out('>>');
1251            $this->_out('endobj');
1252            //Widths
1253            $this->_newobj();
1254            $cw=&$font['cw'];
1255            $s='[';
1256            for($i=32;$i<=255;$i++)
1257                $s.=$cw[chr($i)].' ';
1258            $this->_out($s.']');
1259            $this->_out('endobj');
1260            //Descriptor
1261            $this->_newobj();
1262            $s='<</Type /FontDescriptor /FontName /'.$name;
1263            foreach($font['desc'] as $k=>$v)
1264                $s.=' /'.$k.' '.$v;
1265            $file=$font['file'];
1266            if($file)
1267                $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
1268            $this->_out($s.'>>');
1269            $this->_out('endobj');
1270        }
1271        else
1272        {
1273            //Allow for additional types
1274            $mtd='_put'.strtolower($type);
1275            if(!method_exists($this,$mtd))
1276                $this->Error('Unsupported font type: '.$type);
1277            $this->$mtd($font);
1278        }
1279    }
1280}
1281
1282function _putimages()
1283{
1284    $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
1285    reset($this->images);
1286    while(list($file,$info)=each($this->images))
1287    {
1288        $this->_newobj();
1289        $this->images[$file]['n']=$this->n;
1290        $this->_out('<</Type /XObject');
1291        $this->_out('/Subtype /Image');
1292        $this->_out('/Width '.$info['w']);
1293        $this->_out('/Height '.$info['h']);
1294        if($info['cs']=='Indexed')
1295            $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
1296        else
1297        {
1298            $this->_out('/ColorSpace /'.$info['cs']);
1299            if($info['cs']=='DeviceCMYK')
1300                $this->_out('/Decode [1 0 1 0 1 0 1 0]');
1301        }
1302        $this->_out('/BitsPerComponent '.$info['bpc']);
1303        if(isset($info['f']))
1304            $this->_out('/Filter /'.$info['f']);
1305        if(isset($info['parms']))
1306            $this->_out($info['parms']);
1307        if(isset($info['trns']) && is_array($info['trns']))
1308        {
1309            $trns='';
1310            for($i=0;$i<count($info['trns']);$i++)
1311                $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
1312            $this->_out('/Mask ['.$trns.']');
1313        }
1314        $this->_out('/Length '.strlen($info['data']).'>>');
1315        $this->_putstream($info['data']);
1316        unset($this->images[$file]['data']);
1317        $this->_out('endobj');
1318        //Palette
1319        if($info['cs']=='Indexed')
1320        {
1321            $this->_newobj();
1322            $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
1323            $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
1324            $this->_putstream($pal);
1325            $this->_out('endobj');
1326        }
1327    }
1328}
1329
1330function _putxobjectdict()
1331{
1332    foreach($this->images as $image)
1333        $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
1334}
1335
1336function _putresourcedict()
1337{
1338    $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
1339    $this->_out('/Font <<');
1340    foreach($this->fonts as $font)
1341        $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
1342    $this->_out('>>');
1343    $this->_out('/XObject <<');
1344    $this->_putxobjectdict();
1345    $this->_out('>>');
1346}
1347
1348function _putresources()
1349{
1350    $this->_putfonts();
1351    $this->_putimages();
1352    //Resource dictionary
1353    $this->offsets[2]=strlen($this->buffer);
1354    $this->_out('2 0 obj');
1355    $this->_out('<<');
1356    $this->_putresourcedict();
1357    $this->_out('>>');
1358    $this->_out('endobj');
1359}
1360
1361function _putinfo()
1362{
1363    $this->_out('/Producer '.$this->_textstring('FPDF '.FPDF_VERSION));
1364    if(!empty($this->title))
1365        $this->_out('/Title '.$this->_textstring($this->title));
1366    if(!empty($this->subject))
1367        $this->_out('/Subject '.$this->_textstring($this->subject));
1368    if(!empty($this->author))
1369        $this->_out('/Author '.$this->_textstring($this->author));
1370    if(!empty($this->keywords))
1371        $this->_out('/Keywords '.$this->_textstring($this->keywords));
1372    if(!empty($this->creator))
1373        $this->_out('/Creator '.$this->_textstring($this->creator));
1374    $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
1375}
1376
1377function _putcatalog()
1378{
1379    $this->_out('/Type /Catalog');
1380    $this->_out('/Pages 1 0 R');
1381    if($this->ZoomMode=='fullpage')
1382        $this->_out('/OpenAction [3 0 R /Fit]');
1383    elseif($this->ZoomMode=='fullwidth')
1384        $this->_out('/OpenAction [3 0 R /FitH null]');
1385    elseif($this->ZoomMode=='real')
1386        $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
1387    elseif(!is_string($this->ZoomMode))
1388        $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
1389    if($this->LayoutMode=='single')
1390        $this->_out('/PageLayout /SinglePage');
1391    elseif($this->LayoutMode=='continuous')
1392        $this->_out('/PageLayout /OneColumn');
1393    elseif($this->LayoutMode=='two')
1394        $this->_out('/PageLayout /TwoColumnLeft');
1395}
1396
1397function _putheader()
1398{
1399    $this->_out('%PDF-'.$this->PDFVersion);
1400}
1401
1402function _puttrailer()
1403{
1404    $this->_out('/Size '.($this->n+1));
1405    $this->_out('/Root '.$this->n.' 0 R');
1406    $this->_out('/Info '.($this->n-1).' 0 R');
1407}
1408
1409function _enddoc()
1410{
1411    $this->_putheader();
1412    $this->_putpages();
1413    $this->_putresources();
1414    //Info
1415    $this->_newobj();
1416    $this->_out('<<');
1417    $this->_putinfo();
1418    $this->_out('>>');
1419    $this->_out('endobj');
1420    //Catalog
1421    $this->_newobj();
1422    $this->_out('<<');
1423    $this->_putcatalog();
1424    $this->_out('>>');
1425    $this->_out('endobj');
1426    //Cross-ref
1427    $o=strlen($this->buffer);
1428    $this->_out('xref');
1429    $this->_out('0 '.($this->n+1));
1430    $this->_out('0000000000 65535 f ');
1431    for($i=1;$i<=$this->n;$i++)
1432        $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
1433    //Trailer
1434    $this->_out('trailer');
1435    $this->_out('<<');
1436    $this->_puttrailer();
1437    $this->_out('>>');
1438    $this->_out('startxref');
1439    $this->_out($o);
1440    $this->_out('%%EOF');
1441    $this->state=3;
1442}
1443
1444function _beginpage($orientation)
1445{
1446    $this->page++;
1447    $this->pages[$this->page]='';
1448    $this->state=2;
1449    $this->x=$this->lMargin;
1450    $this->y=$this->tMargin;
1451    $this->FontFamily='';
1452    //Page orientation
1453    if(!$orientation)
1454        $orientation=$this->DefOrientation;
1455    else
1456    {
1457        $orientation=strtoupper($orientation{0});
1458        if($orientation!=$this->DefOrientation)
1459            $this->OrientationChanges[$this->page]=true;
1460    }
1461    if($orientation!=$this->CurOrientation)
1462    {
1463        //Change orientation
1464        if($orientation=='P')
1465        {
1466            $this->wPt=$this->fwPt;
1467            $this->hPt=$this->fhPt;
1468            $this->w=$this->fw;
1469            $this->h=$this->fh;
1470        }
1471        else
1472        {
1473            $this->wPt=$this->fhPt;
1474            $this->hPt=$this->fwPt;
1475            $this->w=$this->fh;
1476            $this->h=$this->fw;
1477        }
1478        $this->PageBreakTrigger=$this->h-$this->bMargin;
1479        $this->CurOrientation=$orientation;
1480    }
1481}
1482
1483function _endpage()
1484{
1485    //End of page contents
1486    $this->state=1;
1487}
1488
1489function _newobj()
1490{
1491    //Begin a new object
1492    $this->n++;
1493    $this->offsets[$this->n]=strlen($this->buffer);
1494    $this->_out($this->n.' 0 obj');
1495}
1496
1497function _dounderline($x,$y,$txt)
1498{
1499    //Underline text
1500    $up=$this->CurrentFont['up'];
1501    $ut=$this->CurrentFont['ut'];
1502    $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
1503    return sprintf('%.2f %.2f %.2f %.2f re f',$x*$this->k,($this->h-($y-$up/1000*$this->FontSize))*$this->k,$w*$this->k,-$ut/1000*$this->FontSizePt);
1504}
1505
1506function _parsejpg($file)
1507{
1508    //Extract info from a JPEG file
1509    $a=GetImageSize($file);
1510    if(!$a)
1511        $this->Error('Missing or incorrect image file: '.$file);
1512    if($a[2]!=2)
1513        $this->Error('Not a JPEG file: '.$file);
1514    if(!isset($a['channels']) || $a['channels']==3)
1515        $colspace='DeviceRGB';
1516    elseif($a['channels']==4)
1517        $colspace='DeviceCMYK';
1518    else
1519        $colspace='DeviceGray';
1520    $bpc=isset($a['bits']) ? $a['bits'] : 8;
1521    //Read whole file
1522    $f=fopen($file,'rb');
1523    $data='';
1524    while(!feof($f))
1525        $data.=fread($f,4096);
1526    fclose($f);
1527    return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
1528}
1529
1530function _parsepng($file)
1531{
1532    //Extract info from a PNG file
1533    $f=fopen($file,'rb');
1534    if(!$f)
1535        $this->Error('Can\'t open image file: '.$file);
1536    //Check signature
1537    if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1538        $this->Error('Not a PNG file: '.$file);
1539    //Read header chunk
1540    fread($f,4);
1541    if(fread($f,4)!='IHDR')
1542        $this->Error('Incorrect PNG file: '.$file);
1543    $w=$this->_freadint($f);
1544    $h=$this->_freadint($f);
1545    $bpc=ord(fread($f,1));
1546    if($bpc>8)
1547        $this->Error('16-bit depth not supported: '.$file);
1548    $ct=ord(fread($f,1));
1549    if($ct==0)
1550        $colspace='DeviceGray';
1551    elseif($ct==2)
1552        $colspace='DeviceRGB';
1553    elseif($ct==3)
1554        $colspace='Indexed';
1555    else
1556        $this->Error('Alpha channel not supported: '.$file);
1557    if(ord(fread($f,1))!=0)
1558        $this->Error('Unknown compression method: '.$file);
1559    if(ord(fread($f,1))!=0)
1560        $this->Error('Unknown filter method: '.$file);
1561    if(ord(fread($f,1))!=0)
1562        $this->Error('Interlacing not supported: '.$file);
1563    fread($f,4);
1564    $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
1565    //Scan chunks looking for palette, transparency and image data
1566    $pal='';
1567    $trns='';
1568    $data='';
1569    do
1570    {
1571        $n=$this->_freadint($f);
1572        $type=fread($f,4);
1573        if($type=='PLTE')
1574        {
1575            //Read palette
1576            $pal=fread($f,$n);
1577            fread($f,4);
1578        }
1579        elseif($type=='tRNS')
1580        {
1581            //Read transparency info
1582            $t=fread($f,$n);
1583            if($ct==0)
1584                $trns=array(ord(substr($t,1,1)));
1585            elseif($ct==2)
1586                $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
1587            else
1588            {
1589                $pos=strpos($t,chr(0));
1590                if($pos!==false)
1591                    $trns=array($pos);
1592            }
1593            fread($f,4);
1594        }
1595        elseif($type=='IDAT')
1596        {
1597            //Read image data block
1598            $data.=fread($f,$n);
1599            fread($f,4);
1600        }
1601        elseif($type=='IEND')
1602            break;
1603        else
1604            fread($f,$n+4);
1605    }
1606    while($n);
1607    if($colspace=='Indexed' && empty($pal))
1608        $this->Error('Missing palette in '.$file);
1609    fclose($f);
1610    return array('w'=>$w,'h'=>$h,'cs'=>$colspace,'bpc'=>$bpc,'f'=>'FlateDecode','parms'=>$parms,'pal'=>$pal,'trns'=>$trns,'data'=>$data);
1611}
1612
1613function _freadint($f)
1614{
1615    //Read a 4-byte integer from file
1616    $a=unpack('Ni',fread($f,4));
1617    return $a['i'];
1618}
1619
1620function _textstring($s)
1621{
1622    //Format a text string
1623    return '('.$this->_escape($s).')';
1624}
1625
1626function _escape($s)
1627{
1628    //Add \ before \, ( and )
1629    return str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$s)));
1630}
1631
1632function _putstream($s)
1633{
1634    $this->_out('stream');
1635    $this->_out($s);
1636    $this->_out('endstream');
1637}
1638
1639function _out($s)
1640{
1641    //Add a line to the document
1642    if($this->state==2)
1643        $this->pages[$this->page].=$s."\n";
1644    else
1645        $this->buffer.=$s."\n";
1646}
1647//End of class
1648}
1649
1650//Handle special IE contype request
1651if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
1652{
1653    header('Content-Type: application/pdf');
1654    exit;
1655}
1656
1657}
1658?>
Note: See TracBrowser for help on using the repository browser.