reverted erroneous previous commit ecaf3a658c94eb6af81fa0b1989a9b60d3d318b0
[openemr.git] / library / html2pdf / _mypdf / 01_fpdf_bookmark.class.php
blob62889993cdaa8dfe46851b3c33a68225c9e2fa7f
1 <?php
2 /*************************************************************************
3 * http://www.fpdf.org/en/script/script1.php
4 *
5 * @author Olivier
6 *
7 * This extension adds bookmark support. The method to add a bookmark is:
8 *
9 * function Bookmark(string txt [, int level [, float y]])
11 * txt: the bookmark title.
12 * level: the bookmark level (0 is top level, 1 is just below, and so on).
13 * y: the y position of the bookmark destination in the current page. -1 means the current position. Default value: 0.
15 * The title must be encoded in ISO Latin-1.
16 ************************************************************************/
17 /*************************************************************************
18 * http://www.fpdf.org/en/script/script13.php
20 * @author Min's
22 * This class prints an index from the created bookmarks.
23 ************************************************************************/
25 if (!defined('__CLASS_FPDF_BOOKMARK__'))
27 define('__CLASS_FPDF_BOOKMARK__', true);
29 require_once(dirname(__FILE__).'/00_fpdf_codebar.class.php');
31 class FPDF_BookMark extends FPDF_Codebar
33 var $outlines=array();
34 var $OutlineRoot;
36 function FPDF_BookMark($orientation='P',$unit='mm',$format='A4')
38 $this->FPDF_Codebar($orientation,$unit,$format);
42 function Bookmark($txt, $level=0, $y=0)
44 if($y==-1) $y=$this->GetY();
45 $this->outlines[]=array('t'=>$txt, 'l'=>$level, 'y'=>($this->h-$y)*$this->k, 'p'=>$this->PageNo());
48 function _putbookmarks()
50 $nb=count($this->outlines);
51 if($nb==0) return;
52 $lru=array();
53 $level=0;
54 foreach($this->outlines as $i=>$o)
56 if($o['l']>0)
58 $parent=$lru[$o['l']-1];
59 //Set parent and last pointers
60 $this->outlines[$i]['parent']=$parent;
61 $this->outlines[$parent]['last']=$i;
62 if($o['l']>$level)
64 //Level increasing: set first pointer
65 $this->outlines[$parent]['first']=$i;
68 else
69 $this->outlines[$i]['parent']=$nb;
71 if($o['l']<=$level and $i>0)
73 //Set prev and next pointers
74 $prev=$lru[$o['l']];
75 $this->outlines[$prev]['next']=$i;
76 $this->outlines[$i]['prev']=$prev;
78 $lru[$o['l']]=$i;
79 $level=$o['l'];
82 //Outline items
83 $n=$this->n+1;
84 foreach($this->outlines as $i=>$o)
86 $this->_newobj();
87 $this->_out('<</Title '.$this->_textstring($o['t']));
88 $this->_out('/Parent '.($n+$o['parent']).' 0 R');
89 if(isset($o['prev']))
90 $this->_out('/Prev '.($n+$o['prev']).' 0 R');
91 if(isset($o['next']))
92 $this->_out('/Next '.($n+$o['next']).' 0 R');
93 if(isset($o['first']))
94 $this->_out('/First '.($n+$o['first']).' 0 R');
95 if(isset($o['last']))
96 $this->_out('/Last '.($n+$o['last']).' 0 R');
97 $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]',1+2*$o['p'],$o['y']));
98 $this->_out('/Count 0>>');
99 $this->_out('endobj');
102 //Outline root
103 $this->_newobj();
104 $this->OutlineRoot=$this->n;
105 $this->_out('<</Type /Outlines /First '.$n.' 0 R');
106 $this->_out('/Last '.($n+$lru[0]).' 0 R>>');
107 $this->_out('endobj');
110 function _putresources()
112 parent::_putresources();
113 $this->_putbookmarks();
116 function _putcatalog()
118 parent::_putcatalog();
119 if(count($this->outlines)>0)
121 $this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
122 $this->_out('/PageMode /UseOutlines');
126 function CreateIndex(&$obj, $titre = 'Index', $size_title = 20, $size_bookmark = 15, $bookmark_title = true, $display_page = true, $page = null)
128 if ($bookmark_title) $this->Bookmark($titre, 0, -1);
130 //Index title
131 $this->SetFontSize($size_title);
132 $this->Cell(0,5,$titre,0,1,'C');
133 $this->SetFontSize($size_bookmark);
134 $this->Ln(10);
136 $size=sizeof($this->outlines);
137 $PageCellSize=$this->GetStringWidth('p. '.$this->outlines[$size-1]['p'])+2;
138 for ($i=0;$i<$size;$i++)
140 if ($this->getY()+$this->FontSize>=($this->h - $this->bMargin))
142 $obj->INDEX_NewPage($page);
143 $this->SetFontSize($size_bookmark);
146 //Offset
147 $level=$this->outlines[$i]['l'];
148 if($level>0) $this->Cell($level*8);
150 //Caption
151 $str=$this->outlines[$i]['t'];
152 $strsize=$this->GetStringWidth($str);
153 $avail_size=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-4;
154 while ($strsize>=$avail_size)
156 $str=substr($str,0,-1);
157 $strsize=$this->GetStringWidth($str);
159 if ($display_page)
161 $this->Cell($strsize+2,$this->FontSize+2,$str);
163 //Filling dots
164 $w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-($strsize+2);
165 $nb=$w/$this->GetStringWidth('.');
166 $dots=str_repeat('.',$nb);
167 $this->Cell($w,$this->FontSize+2,$dots,0,0,'R');
169 //Page number
170 $this->Cell($PageCellSize,$this->FontSize+2,'p. '.$this->outlines[$i]['p'],0,1,'R');
172 else
174 $this->Cell($strsize+2,$this->FontSize+2,$str, 0, 1);