FPDF 内部
FPDF のプログラムで使われている変数を読み解きながらプログラムを読み進めて見ます。 version 1.52 FPDF
SetMargins
SetLeftMargin
SetTopMargin
SetRightMargin
SetAutoPageBreak
function SetAutoPageBreak($auto,$margin=0) ページの自動改行の設定を変更するメソッド。
SetDisplayMode
function SetDisplayMode($zoom,$layout='continuous') PDF ファイルをビューワーで開いたときの最初の表示形式を指定するメソッド。
SetCompression
function SetCompression($compress) PDF の内部データを圧縮するかどうかを設定するメソッド。 //Set page compression if(function_exists('gzcompress')) $this->compress=$compress; else $this->compress=false;
SetTitle
function SetTitle($title) PDF のタイトルを設定するメソッド。
SetSubject
function SetSubject($subject) PDF のサブジェクトを設定するメソッド。
SetAuthor
function SetAuthor($author) 著者を設定するメソッド。
SetKeywords
function SetKeywords($keywords) キーワードを設定するメソッド。
SetCreator
function SetCreator($creator) 作成者を設定するメソッド。
AliasNbPages
function AliasNbPages($alias='{nb}') ページ番号のエイリアスを設定するメソッド。
Error
function Error($msg) 引数の持つエラーメッセージを出力してスクリプトを終了するメソッド。
Open
function Open() PDF を開始するメソッド。$state==0 のときは関数 _begindoc() を呼び出す。
Close
function Close() PDF ドキュメントを閉じるメソッド。 AddPage
function AddPage($orientation='') Header()
function Header() Footer()
function Footer() PageNo()
function PageNo() 現在のページ番号を戻す。
SetDrawColor
function SetDrawColor($r,$g=-1,$b=-1) 図形や線などの描写色を変更する。 //Set color for all stroking operations if(($r==0 and $g==0 and $b==0) or $g==-1) $this->DrawColor=sprintf('%.3f G',$r/255); $r のみが指定されたときにグレースケールでの表示のために色の変更をもどす。色のは %.3f として浮動小数点型,小数点以下3桁で整形される。G は線を描く (stroking) 色を指定することを指す。 else $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255); 引数がすべて指定されたときには RGB 形式で色を変更する。フォーマットはグレースケールのものに類似。 if($this->page>0) $this->_out($this->DrawColor); 現在のページが 0 ページ目でなければ _out 関数により色の変更が出力される。 SetFillColor
function SetFillColor($r,$g=-1,$b=-1) 塗りの色を変更する。 //Set color for all filling operations if(($r==0 and $g==0 and $b==0) or $g==-1) $this->FillColor=sprintf('%.3f g',$r/255); $r のみ指定されたときはグレースケールでの設定を行う。書式は SetDrawColor のものと同じである。一部違うのは,g で,これは線を描く色以外 (nonstroking) の色を指定していることを示す。 else $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255); $this->ColorFlag=($this->FillColor!=$this->TextColor); RGB 形式の色の指定を行う。rg は線を描く色以外の色を指定していることをしめす。
if($this->page>0) $this->_out($this->FillColor); ページがまだ存在しないときには _out 関数で塗りの色の設定を出力する。 SetTextColor
function SetTextColor($r,$g=-1,$b=-1) //Set color for text if(($r==0 and $g==0 and $b==0) or $g==-1) $this->TextColor=sprintf('%.3f g',$r/255); $r のみ指定されたときにはグレースケールで指定する。グレースケールでの塗りの色は g オペレータで指定する。 else $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255); $this->ColorFlag=($this->FillColor!=$this->TextColor); RGB での色の設定。塗りの RGB は rg で指定する。 GetStringWidth
function GetStringWidth($s) SetLineWidth
function SetLineWidth($width) 描写する線の幅を変更する。 //Set line width $this->LineWidth=$width; 線の幅を $LineWidth に保存する。 if($this->page>0) $this->_out(sprintf('%.2f w',$width*$this->k)); ページがないときには _out 関数で線の幅の変更を出力する。フォーマットは線の幅をポイント数で。w は線の幅を設定することを示す。 Line
function Line($x1,$y1,$x2,$y2) //Draw a line $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)); m で新しいパスを作成する位置を設定する。x, y l は直線を現在の位置から x,y まで描く。 S はパスを描くことを示す。 すべての座標はスケールファクター $k をかけることでポイント単位 (pt) に直される。 Rect
function Rect($x,$y,$w,$h,$style='') 四角形を描く。 //Draw a rectangle if($style=='F') $op='f'; elseif($style=='FD' or $style=='DF') $op='B'; else $op='S'; 描くスタイルを示すキャラクターを決定する。f: 塗りつぶす。B: 枠線とともに塗りつぶす。S: パスのみを描く。 $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s', $x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op)); re で x, y, 幅, 高さ で四角形を描く。パスのオペレータの後に塗りのためにオペレータ ($op 変数に保存されている値) を指定する。 AddFont
function AddFont($family,$style='',$file='') フォントを追加する。 //Add a TrueType or Type1 font $family=strtolower($family); フォントファミリー名は小文字で比較される if($family=='arial') $family='helvetica'; Arial フォントファミリーは Helvetica のフォントファミリーファイルに置き換えて PDF を作成する。 $style=strtoupper($style); if($style=='IB') $style='BI'; フォントのスタイルは大文字で比較。IB のときは比較しやすいように BI の順に。 if(isset($this->fonts[$family.$style])) $this->Error('Font already added: '.$family.' '.$style); すでに同じフォントが追加されているときにはエラーを表示。 if($file=='') $file=str_replace(' ','',$family).strtolower($style).'.php'; フォントの定義ファイルの指定がないときにはファミリー名 + スタイル + .php if(defined('FPDF_FONTPATH')) $file=FPDF_FONTPATH.$file; include($file); 定数 FPDF_FONTPATH からフォント定義ファイルの場所を取得してファイル名を補完。ファイルをインクルードする。 if(!isset($name)) $this->Error('Could not include font definition file'); $i=count($this->fonts)+1; $this->fonts[$family.$style]= array('i'=>$i,'type'=>$type,'name'=>$name, 'desc'=>$desc,'up'=>$up,'ut'=>$ut,'cw'=>$cw,'enc'=>$enc,'file'=>$file); if($diff) { //Search existing encodings $d=0; $nb=count($this->diffs); for($i=1;$i<=$nb;$i++) if($this->diffs[$i]==$diff) { $d=$i; break; } if($d==0) { $d=$nb+1; $this->diffs[$d]=$diff; } $this->fonts[$family.$style]['diff']=$d; } if($file) { if($type=='TrueType') $this->FontFiles[$file]=array('length1'=>$originalsize); else $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2); } SetFont
function SetFont($family,$style='',$size=0) SetFontSize
function SetFontSize($size) フォントサイズのみを変更する。 //Set font size in points if($this->FontSizePt==$size) return; 指定されたフォントサイズが現在のフォントサイズと同じときには変更しない。 $this->FontSizePt=$size; $this->FontSize=$size/$this->k; 指定されたフォントサイズをフォントサイズをポイント数で持つ $FontSizePt 変数に保存。また,$FontSize 変数にはユーザー定義の単位でのフォントのサイズを保存。 if($this->page>0) $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'],$this->FontSizePt)); ページがまだ作成されていないときにはフォントサイズの変更を出力。 AddLink
function AddLink() 新しいリンクを作成し,その識別番号を戻す。 //Create a new internal link $n=count($this->links)+1; リンクを保存している $links 配列の個数を調べ,次のリンクの配列の番号を戻す。 $this->links[$n]=array(0,0); リンクに予備的な ページ, y 座標を保持するための配列を作成する。 return $n; リンクを特定する識別番号を戻す。 SetLink
function SetLink($link,$y=0,$page=-1) リンクの位置を設定する。 //Set destination of internal link if($y==-1) $y=$this->y; if($page==-1) $page=$this->page; y 座標およびページは指定されなければ現在の y 座標および現在のページ。 $this->links[$link]=array($page,$y); ページとそのページの y 座標を指定する。既知のリンク番号に設定する。 Link
function Link($x,$y,$w,$h,$link) 四角形の範囲にリンクを設定する。$link はリンクの識別子またはリンクの URL。 //Put a link on the page $this->PageLinks[$this->page][]= array($x*$this->k,$this->hPt-$y*$this->k,$w*$this->k,$h*$this->k,$link); $PageLinks 配列にリンクをフォーマットしたものを保存しておく。リンクは _putpages 関数で実際にページに配置される。 Text
function Text($x,$y,$txt) //Output a string $s=sprintf('BT %.2f %.2f Td (%s) Tj ET', $x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt)); 文字列の描写は BT で始めて ET で終わる。Td で文字の位置を調整する。x, y の位置指定。文字列は () でくくる。$txt 変数の中の文字列のうち (, ), \ を _escape 関数でエスケープする。 if($this->underline and $txt!='') $s.=' '.$this->_dounderline($x,$y,$txt); 文字のアンダーラインを作成する。 if($this->ColorFlag) $s='q '.$this->TextColor.' '.$s.' Q'; $this->_out($s); AcceptPageBreak
function AcceptPageBreak() Cell
function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='') セルを出力する。 //Output a cell $k=$this->k; $k スケールファクター。ユーザー定義の単位のポイント (pt) 数。 if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak()) { //Automatic page break $x=$this->x; $ws=$this->ws; if($ws>0) { $this->ws=0; $this->_out('0 Tw'); } $this->AddPage($this->CurOrientation); $this->x=$x; if($ws>0) { $this->ws=$ws; $this->_out(sprintf('%.3f Tw',$ws*$k)); } } 自動改ページを制御する部分。Tw は単語間の間隔 (Word Spacing) を変更するオペレーターで,初期値は 0 。シングルバイト文字の文字コード 32 のスペースの幅によって間隔を調整する。 if($w==0) $w=$this->w-$this->rMargin-$this->x; セルの指定幅が 0 の時にはセル幅をページ余白を除いた範囲に広げる。 $s=''; 使用する文字列変数を空に初期化。 if($fill==1 or $border==1) { if($fill==1) $op=($border==1) ? 'B' : 'f'; else $op='S'; $s=sprintf('%.2f %.2f %.2f %.2f re %s ', $this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op); } 枠線を描くとき ($border==1) または塗りつぶすとき ($fill==1) に実行される。塗りつぶし,かつ枠線を描くときには B (パスを描き,さらに塗りつぶすオペレータ),塗りつぶしのみのときには f (パスを描かずに塗りのみを描くオペレータ)。枠線のみを描くときには S (パスのみを描くオペレータ)。re オペレータで四角形を描く。指定は x, y, 幅, 高さの順。$op の指定で描く四角形の描写と塗りを指定。 if(is_string($border)) { $x=$this->x; $y=$this->y; if(is_int(strpos($border,'L'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); if(is_int(strpos($border,'T'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); if(is_int(strpos($border,'R'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', ($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k); if(is_int(strpos($border,'B'))) $s.=sprintf('%.2f %.2f m %.2f %.2f l S ', $x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k); } 枠線の指定が辺ごとに指定されたときの処理。まず,現在の x, y 座標を $x, $y に取得。つづいて,左,上,右,下の順に別々に枠線を描く。m オペレータでパスの開始位置を指定。 l オペレータで直線パスを描く。S オペレータでパスのみを描く指定。m オペレータでパスを開始する x, y 座標を指定。l オペレータで現在の位置 (m オペレータで指定した位置) から指定した x, y 座標まで直線を描く。S オペレータの指定はパスのみを描き,塗りを行わない。 if($txt!='') { if($align=='R') $dx=$w-$this->cMargin-$this->GetStringWidth($txt); elseif($align=='C') $dx=($w-$this->GetStringWidth($txt))/2; else $dx=$this->cMargin; セルの内容を右ぞろへにするときは,セルの幅 - セルマージン - テキスト幅がセル内での相対座標による文字の開始位置。中央ぞろへのときには,(セルの幅 - テキスト幅)/2 がセル内での文字の開始位置。そのほかのときには (左ぞろへ) セルマージンを取っておく。 if($this->ColorFlag) $s.='q '.$this->TextColor.' '; 文字の色と枠線の色が違うときには色を変更する。 $txt2=str_replace(')','\\)', str_replace('(','\\(',str_replace('\\','\\\\',$txt))); セル内に描く文字列中の (, ), \\ を \(, \), \\\\ に置換する。 $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET', ($this->x+$dx)*$k, ($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2); PDF ないのフォーマットに従った文字列を準備しておく。$s 変数に保存。PDF 中では文字列を BT で始めて ET で終わる。文字列自体は () でくくる。 if($this->underline) $s.=' '.$this->_dounderline($this->x+$dx, $this->y+.5*$h+.3*$this->FontSize,$txt); if($this->ColorFlag) $s.=' Q'; if($link) $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize, $this->GetStringWidth($txt),$this->FontSize,$link); } リンクを描くときには Link 関数に下請けにまわす。 if($s) $this->_out($s); セル全体をまとめて出力する。 $this->lasth=$h; if($ln>0) { //Go to next line $this->y+=$h; if($ln==1) $this->x=$this->lMargin; } else $this->x+=$w; セルの出力後の位置の移動を行う。$ln に指定された移動。$ln==0 で右方向に移動。$ln==1 で次の行の開始位置。$ln==2 で下方向へ移動。$ln==0 のときには x 座標のみをセルの幅分だけ移動。他の時にはセルの高さ分だけ y 座標を移動。さらに,$ln==1 のときには x 座標を余白位置まで移動。 MultiCell
function MultiCell($w,$h,$txt,$border=0,$align='J',$fill=0) 複数行可能なセルを出力する。 //Output text with automatic or explicit line breaks $cw=&$this->CurrentFont['cw']; if($w==0) $w=$this->w-$this->rMargin-$this->x; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize; $s=str_replace("\r",'',$txt); $nb=strlen($s); if($nb>0 and $s[$nb-1]=="\n") $nb--; $b=0; if($border) { if($border==1) { $border='LTRB'; $b='LRT'; $b2='LR'; } else { $b2=''; if(is_int(strpos($border,'L'))) $b2.='L'; if(is_int(strpos($border,'R'))) $b2.='R'; $b=is_int(strpos($border,'T')) ? $b2.'T' : $b2; } } $sep=-1; $i=0; $j=0; $l=0; $ns=0; $nl=1; while($i<$nb) { //Get next character $c=$s{$i}; if($c=="\n") { //Explicit line break if($this->ws>0) { $this->ws=0; $this->_out('0 Tw'); } $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); $i++; $sep=-1; $j=$i; $l=0; $ns=0; $nl++; if($border and $nl==2) $b=$b2; continue; } if($c==' ') { $sep=$i; $ls=$l; $ns++; } $l+=$cw[$c]; if($l>$wmax) { //Automatic line break if($sep==-1) { if($i==$j) $i++; if($this->ws>0) { $this->ws=0; $this->_out('0 Tw'); } $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); } else { if($align=='J') { $this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0; $this->_out(sprintf('%.3f Tw',$this->ws*$this->k)); } $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill); $i=$sep+1; } $sep=-1; $j=$i; $l=0; $ns=0; $nl++; if($border and $nl==2) $b=$b2; } else $i++; } //Last chunk if($this->ws>0) { $this->ws=0; $this->_out('0 Tw'); } if($border and is_int(strpos($border,'B'))) $b.='B'; $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); $this->x=$this->lMargin; Write
function Write($h,$txt,$link='') //Output text in flowing mode $cw=&$this->CurrentFont['cw']; $w=$this->w-$this->rMargin-$this->x; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize; $s=str_replace("\r",'',$txt); $nb=strlen($s); $sep=-1; $i=0; $j=0; $l=0; $nl=1; while($i<$nb) { //Get next character $c=$s{$i}; if($c=="\n") { //Explicit line break $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); $i++; $sep=-1; $j=$i; $l=0; if($nl==1) { $this->x=$this->lMargin; $w=$this->w-$this->rMargin-$this->x; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize; } $nl++; continue; } if($c==' ') $sep=$i; $l+=$cw[$c]; if($l>$wmax) { //Automatic line break if($sep==-1) { if($this->x>$this->lMargin) { //Move to next line $this->x=$this->lMargin; $this->y+=$h; $w=$this->w-$this->rMargin-$this->x; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize; $i++; $nl++; continue; } if($i==$j) $i++; $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); } else { $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link); $i=$sep+1; } $sep=-1; $j=$i; $l=0; if($nl==1) { $this->x=$this->lMargin; $w=$this->w-$this->rMargin-$this->x; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize; } $nl++; } else $i++; } //Last chunk if($i!=$j) $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',0,$link); Image
function Image($file,$x,$y,$w=0,$h=0,$type='',$link='') function Ln($h='') GetX
function GetX() 現在の x 座標の値を取得する。 //Get x position return $this->x; SetX
function SetX($x) x 座標を設定する。$x が負の値のときにはページの右からの x 座標とする。 //Set x position if($x>=0) $this->x=$x; else $this->x=$this->w+$x; $x が負のときにはページの幅を用いて座標を計算する。$w はユーザー定義の単位でページの幅を保持している変数。 GetY
function GetY() 現在の y 座標を取得する。 //Get y position return $this->y; SetY
function SetY($y) y 座標を設定する。$y が負の値の時にはページの下から座標を設定する。 //Set y position and reset x $this->x=$this->lMargin; if($y>=0) $this->y=$y; else $this->y=$this->h+$y; $y が負の時にはページの高さを用いて座標を計算する。$h はユーザ定義でのページの高さを保持している変数。 SetXY
function SetXY($x,$y) x, y 座標を設定する。 //Set x and y positions $this->SetY($y); $this->SetX($x); 設定は上記の関数 SetY, SetX に依頼。 Output
function Output($name='',$dest='') function _dochecks() _begindoc()
function _begindoc() $state 変数を 1 に設定する。この状態では PDF が開始されていることを示している。関数 _out() を呼び出して,PDF ファイルの宣言 (一行目) を出力する。 _putpages()
function _putpages() function _putfonts() function _putimages() function _putresources() function _putinfo() function _putcatalog() function _puttrailer() function _enddoc() function _beginpage($orientation) function _endpage() function _newobj() function _dounderline($x,$y,$txt) function _parsejpg($file) function _parsepng($file) function _freadint($f) function _textstring($s) function _escape($s) function _putstream($s) function _out($s) |