Merged changes from main trunk 2.2.1-2.2.2
[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(), $pageOrientation='P')
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($pageOrientation, '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         // $fontname should be a standard PDF font (like 'times', 'helvetica' or 'courier')
86         // or one that's been installed on your system.  An empty string can also be used
87         // which will retain the font currently in use.
88         // $style is either:
89         //   * a special case string:
90         //     * bold
91         //     * italic
92         //   * or a case-insensitive string where each char represents a style choice
93         // and you can use more than one or none at all.  Possible choices:
94         //      * empty string: regular
95     //      * B: bold
96     //      * I: italic
97     //      * U: underline
98     //      * D: line trough (aka "strike through")
99         function selectFont($fontname, $style = '')
100         {
101                 // Parse the style - check for special cases, otherwise leave as-is
102                 if ($style == 'italic')
103                         $style = 'i';
104                 elseif ($style == 'bold')
105                         $style = 'b';
106
107                 // Parse the fontname
108                 if ($fontname != '')
109                         $fontname = basename($fontname);
110                 if ($fontname == '')
111                 {
112                         if ($this->isunicode)
113                         {
114                                 switch ($this->l['a_meta_language'])
115                                 {
116                                         case "ar_EG" :  $fontname = "ae_tholoth";       break;
117                                         case "zh_CN" :  $fontname = "gbsn00lp";         break;
118                                         case "zh_TW" :  $fontname = "chinese_traditional_cid0";         break;
119                                         default :       $fontname = "dejavu";           break;
120                                 }
121                         }
122                         elseif ($this->encoding === "ISO-8859-2")
123                         {
124                                 switch ($this->l['a_meta_language'])
125                                 {
126                                         default :               $fontname = "freesans";         break;
127                                 }
128                         }
129                         elseif ($this->encoding === "ISO-8859-5")
130                         {
131                                 switch ($this->l['a_meta_language'])
132                                 {
133                                         default :               $fontname = "freesans5";        break;
134                                 }
135                         }
136                         // else use built-in adobe fonts helvetica.
137                 }
138                 $this->SetFont($fontname, $style);
139         }
140
141         function Header1()
142         {
143         }
144
145         function Footer()
146         {
147         }
148
149         function newPage()
150         {
151                 parent::AddPage();
152         }
153
154         function line($x1,$y1,$x2,$y2, $style = array())
155         {
156                 parent::Line($x1, $this->h-$y1, $x2, $this->h-$y2, $style);
157         }
158
159         function rectangle($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array())
160         {
161                 parent::Rect($x, $this->h-$y, $w, $h, $style, $border_style, $fill_color);
162         }
163
164
165         function addText($xb,$yb,$size,$txt)//,$angle=0,$wordSpaceAdjust=0)
166         {
167                 if ($this->isunicode && $this->encoding != "UTF-8")
168                         $txt = iconv($this->encoding, "UTF-8", $txt);
169                 $this->SetFontSize($size);
170                 $this->Text($xb, $this->h-$yb, $txt);
171         }
172
173         function addInfo($label,$value)
174         {
175                 if($label=='Title')
176                 {
177                         $this->SetTitle($value);
178                 }
179                 if ($label=='Subject')
180                 {
181                         $this->SetSubject($value);
182                 }
183                 if($label=='Creator')
184                 {
185                         // The Creator info in source is not exactly it should be ;)
186                         $value = str_replace( "ros.co.nz", "tcpdf.org", $value );
187                         $value = str_replace( "R&OS", "", $value );
188                         $this->SetCreator( $value );
189                 }
190                 if($label=='Author')
191                 {
192                         $this->SetAuthor($value);
193                 }
194         }
195
196         function addJpegFromFile($img,$x,$y,$w=0,$h=0)
197         {
198                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
199         }
200
201         function addPngFromFile($img,$x,$y,$w=0,$h=0)
202         {
203                 $this->Image($img, $x, $this->h-$y-$h, $w, $h);
204         }
205         /*
206         * Next Two functions are adopted from R&OS pdf class
207         */
208
209         /**
210         * draw a part of an ellipse
211         */
212         function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8)
213         {
214                 $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
215         }
216
217         /**
218         * draw an ellipse
219         * note that the part and filled ellipse are just special cases of this function
220         *
221         * draws an ellipse in the current line style
222         * centered at $x0,$y0, radii $r1,$r2
223         * if $r2 is not set, then a circle is drawn
224         * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
225         * pretty crappy shape at 2, as we are approximating with bezier curves.
226         */
227         function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0)
228         {
229                 parent::Ellipse($x0, $y0, $r1, $r2, $angle, $astart. $afinish, ($close?'C':''), "", "", $nSeg);
230         }
231
232         function Stream()
233         {
234                 parent::Output('', 'I');
235         }
236
237         function calcTextWrap($txt, $width, $spacebreak=false)
238         {
239                 $ret = "";
240                 $txt2 = $txt;
241                 $w = $this->GetStringWidth($txt);
242                 if ($w > $width && $w > 0 && $width != 0)
243                 {
244                         $n = strlen($txt);
245                         $k = intval($n * $width / $w);
246                         if ($k > 0 && $k < $n)
247                         {
248                                 $txt2 = substr($txt, 0, $k);
249                                 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
250                                 {
251                                         $txt2 = substr($txt2, 0, $pos);
252                                         $ret = substr($txt, $pos+1);
253                                 }
254                                 else
255                                         $ret = substr($txt, $k);
256                         }
257                 }
258                 return array($txt2, $ret);
259         }
260
261         function addTextWrap($xb, $yb, $w, $h, $txt, $align='left', $border=0, $fill=0, $link = NULL, $stretch = 0, $spacebreak=false)
262         {
263                 $ret = "";
264                 if (!$this->rtl)
265                 {
266                         if ($align == 'right')
267                                 $align = 'R';
268                         elseif ($align == 'left')
269                                 $align = 'L';
270                         elseif ($align == 'center')
271                                 $align = 'C';
272                         elseif ($align == 'justify')
273                                 $align = 'J';
274                 }
275                 else
276                         $align = 'R';
277
278                 // If horizontal scaling was requested, check to see if we're trying to scale
279                 // too much.  If so, cut back string first and then scale it.
280                 $maxScaleFactor = 1.4;
281                 if ($stretch == 1 || $stretch == 2)
282                 $txt = $this->calcTextWrap($txt, $w * $maxScaleFactor, $spacebreak);
283                 // Wrap text if stretching isn't turned on
284                 else
285                 $txt = $this->calcTextWrap($txt, $w, $spacebreak);
286                 $ret = $txt[1];
287                 $txt = $txt[0];
288                 $this->SetXY($xb, $this->h - $yb - $h);
289                 $txt = parent::unhtmlentities($txt);
290                 if ($this->isunicode && $this->encoding != "UTF-8")
291                         $txt = iconv($this->encoding, "UTF-8", $txt);
292                 $this->Cell($w, $h, $txt, $border, 0, $align, $fill, $link, $stretch);
293                 return $ret;
294         }
295
296         function Text($x, $y, $txt, $stroke=0, $clip=false)
297         {
298                 parent::Text($x,$y, parent::unhtmlentities($txt), $stroke, $clip);
299         }
300
301 } // end of class
302
303 ?>