Changed license type to GPLv3 in top of files
[fa-stable.git] / reporting / includes / class.pdf.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         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/gpl-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                                         case "zh_TW" :  $fontname = "chinese_traditional_cid0";         break;
104                                         default :       $fontname = "dejavu";           break;
105                                 }
106                         }
107                         elseif ($this->encoding === "ISO-8859-2")
108                         {
109                                 switch ($this->l['a_meta_language'])
110                                 {
111                                         default :               $fontname = "freesans";         break;
112                                 }
113                         }
114                         elseif ($this->encoding === "ISO-8859-5")
115                         {
116                                 switch ($this->l['a_meta_language'])
117                                 {
118                                         default :               $fontname = "freesans5";        break;
119                                 }
120                         }
121                         // else use built-in adobe fonts helvetica.
122                 }
123                 $this->SetFont($fontname, $type);
124         }
125
126         function Header1()
127         {
128         }
129
130         function Footer()
131         {
132         }
133
134         function newPage()
135         {
136                 TCPDF::AddPage();
137         }
138
139         function line($x1,$y1,$x2,$y2)
140         {
141                 TCPDF::line($x1, $this->h-$y1, $x2, $this->h-$y2);
142         }
143
144         function rectangle($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array())
145         {
146                 TCPDF::Rect($x, $this->h-$y, $w, $h, $style, $border_style, $fill_color);
147         }
148
149
150         function addText($xb,$yb,$size,$txt)//,$angle=0,$wordSpaceAdjust=0)
151         {
152                 if ($this->isunicode && $this->encoding != "UTF-8")
153                         $txt = iconv($this->encoding, "UTF-8", $txt);
154                 $this->SetFontSize($size);
155                 $this->Text($xb, $this->h-$yb, $txt);
156         }
157
158         function addInfo($label,$value)
159         {
160                 if($label=='Title')
161                 {
162                         $this->SetTitle($value);
163                 }
164                 if ($label=='Subject')
165                 {
166                         $this->SetSubject($value);
167                 }
168                 if($label=='Creator')
169                 {
170                         // The Creator info in source is not exactly it should be ;)
171                         $value = str_replace( "ros.co.nz", "tcpdf.org", $value );
172                         $value = str_replace( "R&OS", "", $value );
173                         $this->SetCreator( $value );
174                 }
175                 if($label=='Author')
176                 {
177                         $this->SetAuthor($value);
178                 }
179         }
180
181         function addJpegFromFile($img,$x,$y,$w=0,$h=0)
182         {
183                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
184         }
185
186         function addPngFromFile($img,$x,$y,$w=0,$h=0)
187         {
188                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
189         }
190         /*
191         * Next Two functions are adopted from R&OS pdf class
192         */
193
194         /**
195         * draw a part of an ellipse
196         */
197         function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8)
198         {
199                 $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
200         }
201
202         /**
203         * draw an ellipse
204         * note that the part and filled ellipse are just special cases of this function
205         *
206         * draws an ellipse in the current line style
207         * centered at $x0,$y0, radii $r1,$r2
208         * if $r2 is not set, then a circle is drawn
209         * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
210         * pretty crappy shape at 2, as we are approximating with bezier curves.
211         */
212         function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0)
213         {
214                 TCPDF::Ellipse($x0, $y0, $r1, $r2, $angle, $astart. $afinish, ($close?'C':''), "", "", $nSeg);
215         }
216
217         function Stream()
218         {
219                 TCPDF::Output('', 'I');
220         }
221
222         function calcTextWrap($txt, $width, $spacebreak=false)
223         {
224                 $ret = "";
225                 $txt2 = $txt;
226                 $w = $this->GetStringWidth($txt);
227                 if ($w > $width && $w > 0 && $width != 0)
228                 {
229                         $n = strlen($txt);
230                         $k = intval($n * $width / $w);
231                         if ($k > 0 && $k < $n)
232                         {
233                                 $txt2 = substr($txt, 0, $k);
234                                 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
235                                 {
236                                         $txt2 = substr($txt2, 0, $pos);
237                                         $ret = substr($txt, $pos+1);
238                                 }
239                                 else
240                                         $ret = substr($txt, $k);
241                         }
242                 }
243                 return array($txt2, $ret);
244         }
245
246         function addTextWrap($xb, $yb, $w, $h, $txt, $align='left', $border=0, $fill=0, $spacebreak=false)
247         {
248                 $ret = "";
249                 if (!$this->rtl)
250                 {
251                         if ($align == 'right')
252                                 $align = 'R';
253                         elseif ($align == 'left')
254                                 $align = 'L';
255                 }
256                 else
257                         $align = 'R';
258                 $txt = $this->calcTextWrap($txt, $w, $spacebreak);
259                 $ret = $txt[1];
260                 $txt = $txt[0];
261                 $this->SetXY($xb, $this->h - $yb - $h);
262                 $txt = TCPDF::unhtmlentities($txt);
263                 if ($this->isunicode && $this->encoding != "UTF-8")
264                         $txt = iconv($this->encoding, "UTF-8", $txt);
265                 $this->Cell($w, $h, $txt, $border, 0, $align, $fill);
266                 return $ret;
267         }
268 /**
269 * sets the colour for stroke operations
270 */
271     function setStrokeColor($r,$g,$b,$force=0)
272     {
273         TCPDF::SetDrawColor($r,$g,$b);
274     }
275 /**
276 * this sets the line drawing style.
277 * width, is the thickness of the line in user units
278 * cap is the type of cap to put on the line, values can be 'butt','round','square'
279 *    where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
280 *    end of the line.
281 * join can be 'miter', 'round', 'bevel'
282 * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
283 *   on and off dashes.
284 *   (2) represents 2 on, 2 off, 2 on , 2 off ...
285 *   (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
286 * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
287 */
288         function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0)
289         {
290                 $this->SetLineWidth($width);
291         }
292
293         function Text($x, $y, $txt, $stroke=0, $clip=false)
294         {
295                 TCPDF::Text($x,$y, TCPDF::unhtmlentities($txt), $stroke, $clip);
296         }
297
298 } // end of class
299
300 ?>