Merged all main trunk bugfixes up to release 2.0.5
[fa-stable.git] / reporting / includes / class.pdf.inc
1 <?php
2 /*
3         This class is an extension to the tcpdf class using a syntax that the original reports were written in
4         (the R &OS pdf.php class) - due to easily portation this wrapper class
5         was written to allow the same code base to use the more functional tcpdf.class by Nicola Asuni
6
7 *       Wrapper for use R&OSpdf API with tcpdf.org class
8 *       Joe Hunt <joe.hunt.consulting@gmail.com> and Janusz Dobrowolski <janusz@iron.from.pl>
9 */
10
11 define("K_RE_PATTERN_RTL", "/(
12           \xD6\xBE                                                                                                                                                                                      # R
13         | \xD7[\x80\x83\x86\x90-\xAA\xB0-\xB4]                                                                  # R
14         | \xDF[\x80-\xAA\xB4\xB5\xBA]                                                                                                           # R
15         | \xE2\x80\x8F                                                                                                                                                                  # R
16         | \xEF\xAC[\x9D\x9F\xA0-\xA8\xAA-\xB6\xB8-\xBC\xBE]                     # R
17         | \xEF\xAD[\x80\x81\x83\x84\x86-\x8F]                                                                           # R
18         | \xF0\x90\xA0[\x80-\x85\x88\x8A-\xB5\xB7\xB8\xBC\xBF]  # R
19         | \xF0\x90\xA4[\x80-\x99]                                                                                                                               # R
20         | \xF0\x90\xA8[\x80\x90-\x93\x95-\x97\x99-\xB3]                                 # R
21         | \xF0\x90\xA9[\x80-\x87\x90-\x98]                                                                                      # R
22         | \xE2\x80[\xAB\xAE]                                                                                                                                            # RLE & RLO
23         )/x");
24
25 /*
26  * Pattern to test Arabic strings using regular expressions.
27  * source: http://www.w3.org/International/questions/qa-forms-utf-8
28  */
29 define("K_RE_PATTERN_ARABIC", "/(
30                           \xD8[\x80-\x83\x8B\x8D\x9B\x9E\x9F\xA1-\xBA]  # AL
31                         | \xD9[\x80-\x8A\xAD-\xAF\xB1-\xBF]                                                     # AL
32                         | \xDA[\x80-\xBF]                                                                                                                               # AL
33                         | \xDB[\x80-\x95\x9D\xA5\xA6\xAE\xAF\xBA-\xBF]  # AL
34                         | \xDC[\x80-\x8D\x90\x92-\xAF]                                                                  # AL
35                         | \xDD[\x8D-\xAD]                                                                                                                               # AL
36                         | \xDE[\x80-\xA5\xB1]                                                                                                           # AL
37                         | \xEF\xAD[\x90-\xBF]                                                                                                           # AL
38                         | \xEF\xAE[\x80-\xB1]                                                                                                           # AL
39                         | \xEF\xAF[\x93-\xBF]                                                                                                           # AL
40                         | \xEF[\xB0-\xB3][\x80-\xBF]                                                                            # AL
41                         | \xEF\xB4[\x80-\xBD]                                                                                                           # AL
42                         | \xEF\xB5[\x90-\xBF]                                                                                                           # AL
43                         | \xEF\xB6[\x80-\x8F\x92-\xBF]                                                                  # AL
44                         | \xEF\xB7[\x80-\x87\xB0-\xBC]                                                                  # AL
45                         | \xEF\xB9[\xB0-\xB4\xB6-\xBF]                                                                  # AL
46                         | \xEF\xBA[\x80-\xBF]                                                                                                           # AL
47                         | \xEF\xBB[\x80-\xBC]                                                                                                           # AL
48                         | \xD9[\xA0-\xA9\xAB\xAC]                                                                                               # AN
49                         )/x");
50
51 include_once (dirname(__FILE__).'/tcpdf.php');
52
53 class Cpdf extends TCPDF {
54
55         function Cpdf($pageSize='A4', $l=array())
56         {
57                 if (!isset($l['a_meta_charset']))
58                         $l = array('a_meta_charset' => 'ISO-8859-1', 'a_meta_dir' => 'ltr', 'a_meta_language' => 'en_GB', 'w_page' => 'page');
59                 $enc = $l['a_meta_charset'];
60                 $uni = ($enc == 'UTF-8' || $enc == 'GB2312' ? true : false);
61                 if ($uni)
62                         ini_set("memory_limit", "48M");
63                 $this->TCPDF('P', 'pt', $pageSize, $uni, $enc);
64                 $this->setLanguageArray($l);
65                 $this->setPrintHeader(false);
66                 $this->setPrintFooter(false);
67                 $this->setPDFVersion("1.3");
68                 $this->setAutoPageBreak(0);
69                 $this->AddPage();
70
71                 $this->SetLineWidth(1);
72                 $this->cMargin = 0;
73         }
74
75         function selectFont($fontname, $style='')
76         {
77                 if ($fontname != '')
78                         $fontname = basename($fontname);
79                 if ($style == 'italic')
80                         $type = 'i';
81                 elseif ($style == 'bold')
82                         $type = 'b';
83                 else
84                         $type = '';
85                 if ($fontname == '')
86                 {
87                         if ($this->isunicode)
88                         {
89                                 switch ($this->l['a_meta_language'])
90                                 {
91                                         case "ar_EG" :  $fontname = "ae_tholoth";       break;
92                                         case "zh_CN" :  $fontname = "gbsn00lp";         break;
93                                         default :       $fontname = "dejavu";           break;
94                                 }
95                         }
96                         elseif ($this->encoding === "ISO-8859-2")
97                         {
98                                 switch ($this->l['a_meta_language'])
99                                 {
100                                         default :               $fontname = "freesans";         break;
101                                 }
102                         }
103                         elseif ($this->encoding === "ISO-8859-5")
104                         {
105                                 switch ($this->l['a_meta_language'])
106                                 {
107                                         default :               $fontname = "freesans5";        break;
108                                 }
109                         }
110                         // else use built-in adobe fonts helvetica.
111                 }
112                 $this->SetFont($fontname, $type);
113         }
114
115         function Header1()
116         {
117         }
118
119         function Footer()
120         {
121         }
122
123         function newPage()
124         {
125                 TCPDF::AddPage();
126         }
127
128         function line($x1,$y1,$x2,$y2)
129         {
130                 TCPDF::line($x1, $this->h-$y1, $x2, $this->h-$y2);
131         }
132
133         function rectangle($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array())
134         {
135                 TCPDF::Rect($x, $this->h-$y, $w, $h, $style, $border_style, $fill_color);
136         }
137
138
139         function addText($xb,$yb,$size,$txt)//,$angle=0,$wordSpaceAdjust=0)
140         {
141                 if ($this->isunicode && $this->encoding != "UTF-8")
142                         $txt = iconv($this->encoding, "UTF-8", $txt);
143                 $this->SetFontSize($size);
144                 $this->Text($xb, $this->h-$yb, $txt);
145         }
146
147         function addInfo($label,$value)
148         {
149                 if($label=='Title')
150                 {
151                         $this->SetTitle($value);
152                 }
153                 if ($label=='Subject')
154                 {
155                         $this->SetSubject($value);
156                 }
157                 if($label=='Creator')
158                 {
159                         // The Creator info in source is not exactly it should be ;)
160                         $value = str_replace( "ros.co.nz", "tcpdf.org", $value );
161                         $value = str_replace( "R&OS", "", $value );
162                         $this->SetCreator( $value );
163                 }
164                 if($label=='Author')
165                 {
166                         $this->SetAuthor($value);
167                 }
168         }
169
170         function addJpegFromFile($img,$x,$y,$w=0,$h=0)
171         {
172                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
173         }
174
175         function addPngFromFile($img,$x,$y,$w=0,$h=0)
176         {
177                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
178         }
179         /*
180         * Next Two functions are adopted from R&OS pdf class
181         */
182
183         /**
184         * draw a part of an ellipse
185         */
186         function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8)
187         {
188                 $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
189         }
190
191         /**
192         * draw an ellipse
193         * note that the part and filled ellipse are just special cases of this function
194         *
195         * draws an ellipse in the current line style
196         * centered at $x0,$y0, radii $r1,$r2
197         * if $r2 is not set, then a circle is drawn
198         * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
199         * pretty crappy shape at 2, as we are approximating with bezier curves.
200         */
201         function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0)
202         {
203                 TCPDF::Ellipse($x0, $y0, $r1, $r2, $angle, $astart. $afinish, ($close?'C':''), "", "", $nSeg);
204         }
205
206         function Stream()
207         {
208                 TCPDF::Output('', 'I');
209         }
210
211         function calcTextWrap($txt, $width, $spacebreak=false)
212         {
213                 $ret = "";
214                 $txt2 = $txt;
215                 $w = $this->GetStringWidth($txt);
216                 if ($w > $width && $w > 0 && $width != 0)
217                 {
218                         $n = strlen($txt);
219                         $k = intval($n * $width / $w);
220                         if ($k > 0 && $k < $n)
221                         {
222                                 $txt2 = substr($txt, 0, $k);
223                                 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
224                                 {
225                                         $txt2 = substr($txt2, 0, $pos);
226                                         $ret = substr($txt, $pos+1);
227                                 }
228                                 else
229                                         $ret = substr($txt, $k);
230                         }
231                 }
232                 return array($txt2, $ret);
233         }
234
235         function addTextWrap($xb, $yb, $w, $h, $txt, $align='left', $border=0, $fill=0, $spacebreak=false)
236         {
237                 $ret = "";
238                 if (!$this->rtl)
239                 {
240                         if ($align == 'right')
241                                 $align = 'R';
242                         elseif ($align == 'left')
243                                 $align = 'L';
244                 }
245                 else
246                         $align = 'R';
247                 $txt = $this->calcTextWrap($txt, $w, $spacebreak);
248                 $ret = $txt[1];
249                 $txt = $txt[0];
250                 $this->SetXY($xb, $this->h - $yb - $h);
251                 $txt = TCPDF::unhtmlentities($txt);
252                 if ($this->isunicode && $this->encoding != "UTF-8")
253                         $txt = iconv($this->encoding, "UTF-8", $txt);
254                 $this->Cell($w, $h, $txt, $border, 0, $align, $fill);
255                 return $ret;
256         }
257 /**
258 * sets the colour for stroke operations
259 */
260     function setStrokeColor($r,$g,$b,$force=0)
261     {
262         TCPDF::SetDrawColor($r,$g,$b);
263     }
264 /**
265 * this sets the line drawing style.
266 * width, is the thickness of the line in user units
267 * cap is the type of cap to put on the line, values can be 'butt','round','square'
268 *    where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
269 *    end of the line.
270 * join can be 'miter', 'round', 'bevel'
271 * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
272 *   on and off dashes.
273 *   (2) represents 2 on, 2 off, 2 on , 2 off ...
274 *   (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
275 * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
276 */
277         function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0)
278         {
279                 $this->SetLineWidth($width);
280         }
281
282         function Text($x, $y, $txt, $stroke=0, $clip=false)
283         {
284                 TCPDF::Text($x,$y, TCPDF::unhtmlentities($txt), $stroke, $clip);
285         }
286
287 } // end of class
288
289 ?>