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