Added HTML2PDF and FPDI to the project.
[openemr.git] / library / html2pdf / _mypdf / mypdf.class.php
blob8b2b7ffae6ed1cf853380783bb9953d12d13eeab
1 <?php
2 /**
3 * Logiciel : HTML2PDF - classe MyPDF
4 *
5 * Convertisseur HTML => PDF
6 * Distribué sous la licence LGPL.
8 * @author Laurent MINGUET <webmaster@html2pdf.fr>
9 * @version 3.31
12 if (!defined('__CLASS_MYPDF__'))
14 define('__CLASS_MYPDF__', true);
16 require_once(dirname(__FILE__).'/htmlcolors.php'); // couleurs HTML, contient les memes que le fichier de TCPDF
17 require_once(dirname(__FILE__).'/99_fpdf_protection.class.php'); // classe fpdf_protection
19 class MyPDF extends FPDF_Protection
21 var $footer_param = array();
22 var $transf = array();
24 var $underline = false;
25 var $linethrough = false;
26 var $overline = false;
28 function MyPDF($sens = 'P', $unit = 'mm', $format = 'A4')
30 $this->underline = false;
31 $this->overline = false;
32 $this->linethrough = false;
34 $this->FPDF_Protection($sens, $unit, $format);
35 $this->AliasNbPages();
36 $this->SetMyFooter();
39 function SetMyFooter($page = null, $date = null, $heure = null, $form = null)
41 $page = ($page ? true : false);
42 $date = ($date ? true : false);
43 $heure = ($heure ? true : false);
44 $form = ($form ? true : false);
46 $this->footer_param = array('page' => $page, 'date' => $date, 'heure' => $heure, 'form' => $form);
49 function Footer()
51 $txt = '';
52 if ($this->footer_param['form']) $txt = (HTML2PDF::textGET('pdf05'));
53 if ($this->footer_param['date'] && $this->footer_param['heure']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf03'));
54 if ($this->footer_param['date'] && !$this->footer_param['heure']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf01'));
55 if (!$this->footer_param['date'] && $this->footer_param['heure']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf02'));
56 if ($this->footer_param['page']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf04'));
58 if (strlen($txt)>0)
60 $txt = str_replace('[[date_d]]', date('d'), $txt);
61 $txt = str_replace('[[date_m]]', date('m'), $txt);
62 $txt = str_replace('[[date_y]]', date('Y'), $txt);
63 $txt = str_replace('[[date_h]]', date('H'), $txt);
64 $txt = str_replace('[[date_i]]', date('i'), $txt);
65 $txt = str_replace('[[date_s]]', date('s'), $txt);
66 $txt = str_replace('[[current]]', $this->PageNo(), $txt);
67 $txt = str_replace('[[nb]]', '{nb}', $txt);
69 parent::SetY(-11);
70 $this->setOverline(false);
71 $this->setLinethrough(false);
72 $this->SetFont('helvetica', 'I', 8);
73 $this->Cell(0, 10, $txt, 0, 0, 'R');
77 // Draw a polygon
78 // Auteur : Andrew Meier
79 // Licence : Freeware
80 function Polygon($points, $style='D')
82 if($style=='F') $op='f';
83 elseif($style=='FD' or $style=='DF') $op='b';
84 else $op='s';
86 $h = $this->h;
87 $k = $this->k;
89 $points_string = '';
90 for($i=0; $i<count($points); $i+=2)
92 $points_string .= sprintf('%.2F %.2F', $points[$i]*$k, ($h-$points[$i+1])*$k);
93 if($i==0) $points_string .= ' m ';
94 else $points_string .= ' l ';
96 $this->_out($points_string . $op);
99 function setOverline($value = true)
101 $this->overline = $value;
104 function setLinethrough($value = true)
106 $this->linethrough = $value;
109 // redéfinition de la methode Text de FPDF afin de rajouter la gestion des overline et linethrough
110 function Text($x, $y, $txt)
112 //Output a string
113 $s=sprintf('BT %.2F %.2F Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
115 /* MODIFICATION HTML2PDF pour le support de underline, overline, linethrough */
116 if ($txt!='')
118 if($this->underline) $s.=' '.$this->_dounderline($x,$y,$txt);
119 if($this->overline) $s.=' '.$this->_dooverline($x,$y,$txt);
120 if($this->linethrough) $s.=' '.$this->_dolinethrough($x,$y,$txt);
122 /* FIN MODIFICATION */
124 if($this->ColorFlag)
125 $s='q '.$this->TextColor.' '.$s.' Q';
126 $this->_out($s);
129 // redéfinition de la methode Cell de FPDF afin de rajouter la gestion des overline et linethrough
130 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
132 //Output a cell
133 $k=$this->k;
134 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
136 //Automatic page break
137 $x=$this->x;
138 $ws=$this->ws;
139 if($ws>0) $this->setWordSpacing(0);
140 $this->AddPage($this->CurOrientation,$this->CurPageFormat);
141 $this->x=$x;
142 if($ws>0) $this->setWordSpacing($ws);
144 if($w==0)
145 $w=$this->w-$this->rMargin-$this->x;
146 $s='';
147 if($fill || $border==1)
149 if($fill)
150 $op=($border==1) ? 'B' : 'f';
151 else
152 $op='S';
153 $s=sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
155 if(is_string($border))
157 $x=$this->x;
158 $y=$this->y;
159 if(strpos($border,'L')!==false)
160 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
161 if(strpos($border,'T')!==false)
162 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
163 if(strpos($border,'R')!==false)
164 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
165 if(strpos($border,'B')!==false)
166 $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
169 if($txt!=='')
171 if($align=='R')
172 $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
173 elseif($align=='C')
174 $dx=($w-$this->GetStringWidth($txt))/2;
175 else
176 $dx=$this->cMargin;
177 if($this->ColorFlag)
178 $s.='q '.$this->TextColor.' ';
179 $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
180 $s.=sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
182 /* MODIFICATION HTML2PDF pour le support de underline, overline, linethrough */
183 if($this->underline) $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
184 if($this->overline) $s.=' '.$this->_dooverline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
185 if($this->linethrough) $s.=' '.$this->_dolinethrough($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
186 /* FIN MODIFICATION */
188 if($this->ColorFlag)
189 $s.=' Q';
190 if($link)
191 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
194 if($s)
195 $this->_out($s);
196 $this->lasth=$h;
197 if($ln>0)
199 //Go to next line
200 $this->y+=$h;
201 if($ln==1)
202 $this->x=$this->lMargin;
204 else
205 $this->x+=$w;
208 function _dounderline($x, $y, $txt)
210 //Underline text
211 $up=$this->CurrentFont['up'];
212 $ut=$this->CurrentFont['ut'];
214 $p_x = $x*$this->k;
215 $p_y = ($this->h-($y-$up/1000*$this->FontSize))*$this->k;
216 $p_w = ($this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '))*$this->k;
217 $p_h = -$ut/1000*$this->FontSizePt;
219 return sprintf('%.2F %.2F %.2F %.2F re f',$p_x,$p_y,$p_w,$p_h);
222 function _dooverline($x, $y, $txt)
224 //Overline text
225 $up=$this->CurrentFont['up'];
226 $ut=$this->CurrentFont['ut'];
228 $p_x = $x*$this->k;
229 $p_y = ($this->h-($y-(1000+1.5*$up)/1000*$this->FontSize))*$this->k;
230 $p_w = ($this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '))*$this->k;
231 $p_h = -$ut/1000*$this->FontSizePt;
233 return sprintf('%.2F %.2F %.2F %.2F re f',$p_x,$p_y,$p_w,$p_h);
236 function _dolinethrough($x, $y, $txt)
238 //Linethrough text
239 $up=$this->CurrentFont['up'];
240 $ut=$this->CurrentFont['ut'];
242 $p_x = $x*$this->k;
243 $p_y = ($this->h-($y-(1000+2.5*$up)/2000*$this->FontSize))*$this->k;
244 $p_w = ($this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '))*$this->k;
245 $p_h = -$ut/1000*$this->FontSizePt;
247 return sprintf('%.2F %.2F %.2F %.2F re f',$p_x,$p_y,$p_w,$p_h);
250 function cloneFontFrom(&$pdf)
252 $this->fonts = &$pdf->getFonts();
253 $this->FontFiles = &$pdf->getFontFiles();
254 $this->diffs = &$pdf->getDiffs();
257 function &getFonts() { return $this->fonts; }
258 function &getFontFiles() { return $this->FontFiles; }
259 function &getDiffs() { return $this->diffs; }
261 function isLoadedFont($fontkey)
263 if (isset($this->fonts[$fontkey]))
264 return true;
266 if (isset($this->CoreFonts[$fontkey]))
267 return true;
269 return false;
272 function setWordSpacing($ws=0.)
274 $this->ws = $ws;
275 $this->_out(sprintf('%.3F Tw',$ws*$this->k));
278 function clippingPathOpen($x = null, $y = null, $w = null, $h = null, $coin_TL=null, $coin_TR=null, $coin_BL=null, $coin_BR=null)
280 $path = '';
281 if ($x!==null && $y!==null && $w!==null && $h!==null)
283 $x1 = $x*$this->k;
284 $y1 = ($this->h-$y)*$this->k;
286 $x2 = ($x+$w)*$this->k;
287 $y2 = ($this->h-$y)*$this->k;
289 $x3 = ($x+$w)*$this->k;
290 $y3 = ($this->h-$y-$h)*$this->k;
292 $x4 = $x*$this->k;
293 $y4 = ($this->h-$y-$h)*$this->k;
295 if ($coin_TL || $coin_TR || $coin_BL || $coin_BR)
297 if ($coin_TL) { $coin_TL[0] = $coin_TL[0]*$this->k; $coin_TL[1] =-$coin_TL[1]*$this->k; }
298 if ($coin_TR) { $coin_TR[0] = $coin_TR[0]*$this->k; $coin_TR[1] =-$coin_TR[1]*$this->k; }
299 if ($coin_BL) { $coin_BL[0] = $coin_BL[0]*$this->k; $coin_BL[1] =-$coin_BL[1]*$this->k; }
300 if ($coin_BR) { $coin_BR[0] = $coin_BR[0]*$this->k; $coin_BR[1] =-$coin_BR[1]*$this->k; }
302 $MyArc = 4/3 * (sqrt(2) - 1);
304 if ($coin_TL)
305 $path.= sprintf('%.2F %.2F m ', $x1+$coin_TL[0], $y1);
306 else
307 $path.= sprintf('%.2F %.2F m ', $x1, $y1);
309 if ($coin_TR)
311 $xt1 = ($x2-$coin_TR[0])+$coin_TR[0]*$MyArc;
312 $yt1 = ($y2+$coin_TR[1])-$coin_TR[1];
313 $xt2 = ($x2-$coin_TR[0])+$coin_TR[0];
314 $yt2 = ($y2+$coin_TR[1])-$coin_TR[1]*$MyArc;
316 $path.= sprintf('%.2F %.2F l ', $x2-$coin_TR[0], $y2);
317 $path.= sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $xt1, $yt1, $xt2, $yt2, $x2, $y2+$coin_TR[1]);
319 else
320 $path.= sprintf('%.2F %.2F l ', $x2, $y2);
322 if ($coin_BR)
324 $xt1 = ($x3-$coin_BR[0])+$coin_BR[0];
325 $yt1 = ($y3-$coin_BR[1])+$coin_BR[1]*$MyArc;
326 $xt2 = ($x3-$coin_BR[0])+$coin_BR[0]*$MyArc;
327 $yt2 = ($y3-$coin_BR[1])+$coin_BR[1];
329 $path.= sprintf('%.2F %.2F l ', $x3, $y3-$coin_BR[1]);
330 $path.= sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $xt1, $yt1, $xt2, $yt2, $x3-$coin_BR[0], $y3);
332 else
333 $path.= sprintf('%.2F %.2F l ', $x3, $y3);
335 if ($coin_BL)
337 $xt1 = ($x4+$coin_BL[0])-$coin_BL[0]*$MyArc;
338 $yt1 = ($y4-$coin_BL[1])+$coin_BL[1];
339 $xt2 = ($x4+$coin_BL[0])-$coin_BL[0];
340 $yt2 = ($y4-$coin_BL[1])+$coin_BL[1]*$MyArc;
342 $path.= sprintf('%.2F %.2F l ', $x4+$coin_BL[0], $y4);
343 $path.= sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $xt1, $yt1, $xt2, $yt2, $x4, $y4-$coin_BL[1]);
345 else
346 $path.= sprintf('%.2F %.2F l ', $x4, $y4);
348 if ($coin_TL)
350 $xt1 = ($x1+$coin_TL[0])-$coin_TL[0];
351 $yt1 = ($y1+$coin_TL[1])-$coin_TL[1]*$MyArc;
352 $xt2 = ($x1+$coin_TL[0])-$coin_TL[0]*$MyArc;
353 $yt2 = ($y1+$coin_TL[1])-$coin_TL[1];
355 $path.= sprintf('%.2F %.2F l ', $x1, $y1+$coin_TL[1]);
356 $path.= sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $xt1, $yt1, $xt2, $yt2, $x1+$coin_TL[0], $y1);
359 else
361 $path.= sprintf('%.2F %.2F m ', $x1, $y1);
362 $path.= sprintf('%.2F %.2F l ', $x2, $y2);
363 $path.= sprintf('%.2F %.2F l ', $x3, $y3);
364 $path.= sprintf('%.2F %.2F l ', $x4, $y4);
367 $path.= ' h W n';
369 $this->_out('q '.$path.' ');
372 function clippingPathClose()
374 $this->_out(' Q');
377 function drawCourbe($ext1_x, $ext1_y, $ext2_x, $ext2_y, $int1_x, $int1_y, $int2_x, $int2_y, $cen_x, $cen_y)
379 $MyArc = 4/3 * (sqrt(2) - 1);
381 $ext1_x = $ext1_x*$this->k; $ext1_y = ($this->h-$ext1_y)*$this->k;
382 $ext2_x = $ext2_x*$this->k; $ext2_y = ($this->h-$ext2_y)*$this->k;
383 $int1_x = $int1_x*$this->k; $int1_y = ($this->h-$int1_y)*$this->k;
384 $int2_x = $int2_x*$this->k; $int2_y = ($this->h-$int2_y)*$this->k;
385 $cen_x = $cen_x*$this->k; $cen_y = ($this->h-$cen_y) *$this->k;
387 $path = '';
389 if ($ext1_x-$cen_x!=0)
391 $xt1 = $cen_x+($ext1_x-$cen_x);
392 $yt1 = $cen_y+($ext2_y-$cen_y)*$MyArc;
393 $xt2 = $cen_x+($ext1_x-$cen_x)*$MyArc;
394 $yt2 = $cen_y+($ext2_y-$cen_y);
396 else
398 $xt1 = $cen_x+($ext2_x-$cen_x)*$MyArc;
399 $yt1 = $cen_y+($ext1_y-$cen_y);
400 $xt2 = $cen_x+($ext2_x-$cen_x);
401 $yt2 = $cen_y+($ext1_y-$cen_y)*$MyArc;
405 $path.= sprintf('%.2F %.2F m ', $ext1_x, $ext1_y);
406 $path.= sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $xt1, $yt1, $xt2, $yt2, $ext2_x, $ext2_y);
408 if ($int1_x-$cen_x!=0)
410 $xt1 = $cen_x+($int1_x-$cen_x)*$MyArc;
411 $yt1 = $cen_y+($int2_y-$cen_y);
412 $xt2 = $cen_x+($int1_x-$cen_x);
413 $yt2 = $cen_y+($int2_y-$cen_y)*$MyArc;
415 else
417 $xt1 = $cen_x+($int2_x-$cen_x);
418 $yt1 = $cen_y+($int1_y-$cen_y)*$MyArc;
419 $xt2 = $cen_x+($int2_x-$cen_x)*$MyArc;
420 $yt2 = $cen_y+($int1_y-$cen_y);
424 $path.= sprintf('%.2F %.2F l ', $int2_x, $int2_y);
425 $path.= sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $xt1, $yt1, $xt2, $yt2, $int1_x, $int1_y);
427 $this->_out($path . 'f');
430 function drawCoin($ext1_x, $ext1_y, $ext2_x, $ext2_y, $int_x, $int_y, $cen_x, $cen_y)
432 $MyArc = 4/3 * (sqrt(2) - 1);
434 $ext1_x = $ext1_x*$this->k; $ext1_y = ($this->h-$ext1_y)*$this->k;
435 $ext2_x = $ext2_x*$this->k; $ext2_y = ($this->h-$ext2_y)*$this->k;
436 $int_x = $int_x*$this->k; $int_y = ($this->h-$int_y)*$this->k;
437 $cen_x = $cen_x*$this->k; $cen_y = ($this->h-$cen_y) *$this->k;
439 $path = '';
441 if ($ext1_x-$cen_x!=0)
443 $xt1 = $cen_x+($ext1_x-$cen_x);
444 $yt1 = $cen_y+($ext2_y-$cen_y)*$MyArc;
445 $xt2 = $cen_x+($ext1_x-$cen_x)*$MyArc;
446 $yt2 = $cen_y+($ext2_y-$cen_y);
448 else
450 $xt1 = $cen_x+($ext2_x-$cen_x)*$MyArc;
451 $yt1 = $cen_y+($ext1_y-$cen_y);
452 $xt2 = $cen_x+($ext2_x-$cen_x);
453 $yt2 = $cen_y+($ext1_y-$cen_y)*$MyArc;
457 $path.= sprintf('%.2F %.2F m ', $ext1_x, $ext1_y);
458 $path.= sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c ', $xt1, $yt1, $xt2, $yt2, $ext2_x, $ext2_y);
459 $path.= sprintf('%.2F %.2F l ', $int_x, $int_y);
460 $path.= sprintf('%.2F %.2F l ', $ext1_x, $ext1_y);
462 $this->_out($path . 'f');
465 function startTransform()
467 $this->_out('q');
470 function stopTransform()
472 $this->_out('Q');
475 function setTranslate($t_x, $t_y)
477 // matrice de transformation
478 $tm[0]=1;
479 $tm[1]=0;
480 $tm[2]=0;
481 $tm[3]=1;
482 $tm[4]=$t_x*$this->k;
483 $tm[5]=-$t_y*$this->k;
485 $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0],$tm[1],$tm[2],$tm[3],$tm[4],$tm[5]));
488 function setRotation($angle, $x='', $y='')
490 if($x === '') $x=$this->x;
491 if($y === '') $y=$this->y;
493 $y=($this->h-$y)*$this->k;
494 $x*=$this->k;
496 // matrice de transformation
497 $tm[0]=cos(deg2rad($angle));
498 $tm[1]=sin(deg2rad($angle));
499 $tm[2]=-$tm[1];
500 $tm[3]=$tm[0];
501 $tm[4]=$x+$tm[1]*$y-$tm[0]*$x;
502 $tm[5]=$y-$tm[0]*$y-$tm[1]*$x;
504 $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0],$tm[1],$tm[2],$tm[3],$tm[4],$tm[5]));
507 function SetX($x)
509 $this->x=$x;
512 function SetY($y, $resetx=true)
514 if ($resetx)
515 $this->x=$this->lMargin;
517 $this->y=$y;
520 function SetXY($x, $y)
522 $this->x=$x;
523 $this->y=$y;
526 function getK() { return $this->k; }
527 function getW() { return $this->w; }
528 function getH() { return $this->h; }
529 function getPage() { return $this->page; }
530 function getlMargin() { return $this->lMargin; }
531 function getrMargin() { return $this->rMargin; }
532 function gettMargin() { return $this->tMargin; }
533 function getbMargin() { return $this->bMargin; }
534 function setbMargin($v) { $this->bMargin=$v; }
535 function setcMargin($v) { $this->cMargin=$v; }
536 function setPage($v) { $this->page=$v; }
538 function svgSetStyle($styles)
540 $style = '';
542 if ($styles['fill'])
544 $this->setMyFillColor($styles['fill']);
545 $style.= 'F';
547 if ($styles['stroke'] && $styles['stroke-width'])
549 $this->SetMyDrawColor($styles['stroke']);
550 $this->SetLineWidth($styles['stroke-width']);
551 $style.= 'D';
553 if ($styles['fill-opacity'])
555 // $this->SetAlpha($styles['fill-opacity']);
558 return $style;
561 function svgRect($x, $y, $w, $h, $style)
563 $xa=$x; $xb=$x+$w; $xc=$x+$w; $xd=$x;
564 $ya=$y; $yb=$y; $yc=$y+$h; $yd=$y+$h;
566 if($style=='F') $op='f';
567 elseif($style=='FD' || $style=='DF') $op='B';
568 else $op='S';
569 $this->_Point($xa, $ya, true);
570 $this->_Line($xb, $yb, true);
571 $this->_Line($xc, $yc, true);
572 $this->_Line($xd, $yd, true);
573 $this->_Line($xa, $ya, true);
574 $this->_out($op);
577 function svgLine($x1, $y1, $x2, $y2)
579 $op='S';
580 $this->_Point($x1, $y1, true);
581 $this->_Line($x2, $y2, true);
582 $this->_out($op);
585 function svgEllipse($x0, $y0, $rx, $ry, $style)
587 if($style=='F') $op='f';
588 elseif($style=='FD' || $style=='DF') $op='B';
589 else $op='S';
591 $this->_Arc($x0, $y0, $rx, $ry, 0, 2*M_PI, true, true, true);
592 $this->_out($op);
595 function svgPolygone($actions, $style)
597 if($style=='F') $op='f';
598 elseif($style=='FD' || $style=='DF') $op='B';
599 else $op='S';
601 $first = array('', 0, 0);
602 $last = array(0, 0, 0, 0);
604 foreach($actions as $action)
606 switch($action[0])
608 case 'M':
609 case 'm':
610 $first = $action;
611 $x = $action[1]; $y = $action[2]; $xc = $x; $yc = $y;
612 $this->_Point($x, $y, true);
613 break;
615 case 'Z':
616 case 'z':
617 $x = $first[1]; $y = $first[2]; $xc = $x; $yc = $y;
618 $this->_Line($x, $y, true);
619 break;
621 case 'L':
622 $x = $action[1]; $y = $action[2]; $xc = $x; $yc = $y;
623 $this->_Line($x, $y, true);
624 break;
626 case 'l':
627 $x = $last[0]+$action[1]; $y = $last[1]+$action[2]; $xc = $x; $yc = $y;
628 $this->_Line($x, $y, true);
629 break;
631 case 'H':
632 $x = $action[1]; $y = $last[1]; $xc = $x; $yc = $y;
633 $this->_Line($x, $y, true);
634 break;
636 case 'h':
637 $x = $last[0]+$action[1]; $y = $last[1]; $xc = $x; $yc = $y;
638 $this->_Line($x, $y, true);
639 break;
641 case 'V':
642 $x = $last[0]; $y = $action[1]; $xc = $x; $yc = $y;
643 $this->_Line($x, $y, true);
644 break;
646 case 'v':
647 $x = $last[0]; $y = $last[1]+$action[1]; $xc = $x; $yc = $y;
648 $this->_Line($x, $y, true);
649 break;
651 case 'A':
652 $rx = $action[1]; // rx
653 $ry = $action[2]; // ry
654 $a = $action[3]; // angle de deviation de l'axe X
655 $l = $action[4]; // large-arc-flag
656 $s = $action[5]; // sweep-flag
657 $x1 = $last[0]; // begin x
658 $y1 = $last[1]; // begin y
659 $x2 = $action[6]; // final x
660 $y2 = $action[7]; // final y
662 $this->_Arc2($x1, $y1, $x2, $y2, $rx, $ry, $a, $l, $s, true);
664 $x = $x2; $y = $y2; $xc = $x; $yc = $y;
665 break;
667 case 'a':
668 $rx = $action[1]; // rx
669 $ry = $action[2]; // ry
670 $a = $action[3]; // angle de deviation de l'axe X
671 $l = $action[4]; // large-arc-flag
672 $s = $action[5]; // sweep-flag
673 $x1 = $last[0]; // begin x
674 $y1 = $last[1]; // begin y
675 $x2 = $last[0]+$action[6]; // final x
676 $y2 = $last[1]+$action[7]; // final y
678 $this->_Arc2($x1, $y1, $x2, $y2, $rx, $ry, $a, $l, $s, true);
680 $x = $x2; $y = $y2; $xc = $x; $yc = $y;
681 break;
683 case 'C':
684 $x1 = $action[1];
685 $y1 = $action[2];
686 $x2 = $action[3];
687 $y2 = $action[4];
688 $xf = $action[5];
689 $yf = $action[6];
690 $this->_Curve($x1, $y1, $x2, $y2,$xf, $yf, true);
691 $x = $xf; $y = $yf; $xc = $x2; $yc = $y2;
692 break;
694 case 'c':
695 $x1 = $last[0]+$action[1];
696 $y1 = $last[1]+$action[2];
697 $x2 = $last[0]+$action[3];
698 $y2 = $last[1]+$action[4];
699 $xf = $last[0]+$action[5];
700 $yf = $last[1]+$action[6];
701 $this->_Curve($x1, $y1, $x2, $y2,$xf, $yf, true);
702 $x = $xf; $y = $yf; $xc = $x2; $yc = $y2;
703 break;
705 default:
706 echo 'MyPDF Path : <b>'.$action[0].'</b> non reconnu...';
707 exit;
709 $last = array($x, $y, $xc, $yc);
711 $this->_out($op);
714 function _Point($x, $y, $trans = false)
716 if ($trans) $this->ptTransform($x, $y);
718 $this->_out(sprintf('%.2F %.2F m', $x, $y));
721 function _Line($x, $y, $trans = false)
723 if ($trans) $this->ptTransform($x, $y);
725 $this->_out(sprintf('%.2F %.2F l', $x, $y));
728 function _Curve($x1, $y1, $x2, $y2, $x3, $y3, $trans = false)
730 if ($trans)
732 $this->ptTransform($x1, $y1);
733 $this->ptTransform($x2, $y2);
734 $this->ptTransform($x3, $y3);
736 $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c', $x1, $y1, $x2, $y2, $x3, $y3));
739 function _Arc($xc, $yc, $rx, $ry, $a_debut, $a_fin, $sens = true, $draw_first = true, $trans=false)
741 $nSeg = 8;
743 if (!$sens) $a_debut+= M_PI*2.;
745 $totalAngle = $a_fin - $a_debut;
746 $dt = $totalAngle/$nSeg;
747 $dtm = $dt/3;
749 $x0 = $xc; $y0 = $yc;
751 $t1 = $a_debut;
752 $a0 = $x0 + ($rx * cos($t1));
753 $b0 = $y0 + ($ry * sin($t1));
754 $c0 = -$rx * sin($t1);
755 $d0 = $ry * cos($t1);
756 if ($draw_first) $this->_Point($a0, $b0, $trans);
757 for ($i = 1; $i <= $nSeg; $i++)
759 // Draw this bit of the total curve
760 $t1 = ($i * $dt)+$a_debut;
761 $a1 = $x0 + ($rx * cos($t1));
762 $b1 = $y0 + ($ry * sin($t1));
763 $c1 = -$rx * sin($t1);
764 $d1 = $ry * cos($t1);
765 $this->_Curve(
766 $a0 + ($c0 * $dtm), $b0 + ($d0 * $dtm),
767 $a1 - ($c1 * $dtm), $b1 - ($d1 * $dtm),
768 $a1, $b1,
769 $trans
771 $a0 = $a1;
772 $b0 = $b1;
773 $c0 = $c1;
774 $d0 = $d1;
778 function _Arc2($x1, $y1, $x2, $y2, $rx, $ry, $a=0, $l=0, $s=0, $trans = false)
780 $v = array();
781 $v['x1'] = $x1;
782 $v['y1'] = $y1;
783 $v['x2'] = $x2;
784 $v['y2'] = $y2;
785 $v['rx'] = $rx;
786 $v['ry'] = $ry;
787 $v['xr1'] = $v['x1']*cos($a) - $v['y1']*sin($a);
788 $v['yr1'] = $v['x1']*sin($a) + $v['y1']*cos($a);
789 $v['xr2'] = $v['x2']*cos($a) - $v['y2']*sin($a);
790 $v['yr2'] = $v['x2']*sin($a) + $v['y2']*cos($a);
791 $v['Xr1'] = $v['xr1']/$v['rx'];
792 $v['Yr1'] = $v['yr1']/$v['ry'];
793 $v['Xr2'] = $v['xr2']/$v['rx'];
794 $v['Yr2'] = $v['yr2']/$v['ry'];
795 $v['dXr'] = $v['Xr2'] - $v['Xr1'];
796 $v['dYr'] = $v['Yr2'] - $v['Yr1'];
797 $v['D'] = $v['dXr']*$v['dXr'] + $v['dYr']*$v['dYr'];
799 if ($v['D']==0 || $v['D']>4)
801 $this->_Line($x2, $y2, $trans);
802 return false;
805 $v['s1'] = array();
806 $v['s2'] = array();
807 $v['s1']['t'] = sqrt((4.-$v['D'])/$v['D']);
808 $v['s1']['Xr'] = ($v['Xr1']+$v['Xr2'])/2. + $v['s1']['t']*($v['Yr2']-$v['Yr1'])/2.;
809 $v['s1']['Yr'] = ($v['Yr1']+$v['Yr2'])/2. + $v['s1']['t']*($v['Xr1']-$v['Xr2'])/2.;
810 $v['s1']['xr'] = $v['s1']['Xr']*$v['rx'];
811 $v['s1']['yr'] = $v['s1']['Yr']*$v['ry'];
812 $v['s1']['x'] = $v['s1']['xr']*cos($a)+$v['s1']['yr']*sin($a);
813 $v['s1']['y'] =-$v['s1']['xr']*sin($a)+$v['s1']['yr']*cos($a);
814 $v['s1']['a1'] = atan2($v['y1']-$v['s1']['y'], $v['x1']-$v['s1']['x']);
815 $v['s1']['a2'] = atan2($v['y2']-$v['s1']['y'], $v['x2']-$v['s1']['x']);
816 if ($v['s1']['a1']>$v['s1']['a2']) $v['s1']['a1']-=2*M_PI;
818 $v['s2']['t'] = -$v['s1']['t'];
819 $v['s2']['Xr'] = ($v['Xr1']+$v['Xr2'])/2. + $v['s2']['t']*($v['Yr2']-$v['Yr1'])/2.;
820 $v['s2']['Yr'] = ($v['Yr1']+$v['Yr2'])/2. + $v['s2']['t']*($v['Xr1']-$v['Xr2'])/2.;
821 $v['s2']['xr'] = $v['s2']['Xr']*$v['rx'];
822 $v['s2']['yr'] = $v['s2']['Yr']*$v['ry'];
823 $v['s2']['x'] = $v['s2']['xr']*cos($a)+$v['s2']['yr']*sin($a);
824 $v['s2']['y'] =-$v['s2']['xr']*sin($a)+$v['s2']['yr']*cos($a);
825 $v['s2']['a1'] = atan2($v['y1']-$v['s2']['y'], $v['x1']-$v['s2']['x']);
826 $v['s2']['a2'] = atan2($v['y2']-$v['s2']['y'], $v['x2']-$v['s2']['x']);
827 if ($v['s2']['a1']>$v['s2']['a2']) $v['s2']['a1']-=2*M_PI;
829 if (!$l)
831 if ($s)
833 $xc = $v['s2']['x'];
834 $yc = $v['s2']['y'];
835 $a1 = $v['s2']['a1'];
836 $a2 = $v['s2']['a2'];
837 $this->_Arc($xc, $yc, $rx, $ry, $a1, $a2, true, false, $trans);
840 else
842 $xc = $v['s1']['x'];
843 $yc = $v['s1']['y'];
844 $a1 = $v['s1']['a1'];
845 $a2 = $v['s1']['a2'];
846 $this->_Arc($xc, $yc, $rx, $ry, $a1, $a2, false, false, $trans);
849 else
851 if ($s)
853 $xc = $v['s1']['x'];
854 $yc = $v['s1']['y'];
855 $a1 = $v['s1']['a1'];
856 $a2 = $v['s1']['a2'];
857 $this->_Arc($xc, $yc, $rx, $ry, $a1, $a2, true, false, $trans);
859 else
861 $xc = $v['s2']['x'];
862 $yc = $v['s2']['y'];
863 $a1 = $v['s2']['a1'];
864 $a2 = $v['s2']['a2'];
865 $this->_Arc($xc, $yc, $rx, $ry, $a1, $a2, false, false, $trans);
870 function ptTransform(&$x, &$y, $trans=true)
872 $nb = count($this->transf);
873 if ($nb) $m = $this->transf[$nb-1];
874 else $m = array(1,0,0,1,0,0);
876 list($x,$y) = array(($x*$m[0]+$y*$m[2]+$m[4]),($x*$m[1]+$y*$m[3]+$m[5]));
878 if ($trans)
880 $x = $x*$this->k;
881 $y = ($this->h-$y)*$this->k;
884 return true;
887 function doTransform($n = null)
889 $nb = count($this->transf);
890 if ($nb) $m = $this->transf[$nb-1];
891 else $m = array(1,0,0,1,0,0);
893 if (!$n) $n = array(1,0,0,1,0,0);
895 $n = array(
896 $m[0]*$n[0]+$m[2]*$n[1],
897 $m[1]*$n[0]+$m[3]*$n[1],
898 $m[0]*$n[2]+$m[2]*$n[3],
899 $m[1]*$n[2]+$m[3]*$n[3],
900 $m[0]*$n[4]+$m[2]*$n[5]+$m[4],
901 $m[1]*$n[4]+$m[3]*$n[5]+$m[5]
904 // echo 'do-'.count($this->transf).' => '.print_r($n, true).'<br>';
905 $this->transf[] = $n;
908 function undoTransform()
910 array_pop($this->transf);
911 // echo 'un-'.count($this->transf).'<br>';
914 function setMyDrawColor($c)
916 $c = $this->setMyColor($c, true);
917 if (!$c) return false;
919 $this->DrawColor=$c;
920 if($this->page>0) $this->_out($this->DrawColor);
923 function setMyFillColor($c)
925 $c = $this->setMyColor($c);
926 if (!$c) return false;
928 $this->FillColor=$c;
929 $this->ColorFlag=($this->FillColor!=$this->TextColor);
930 if($this->page>0) $this->_out($this->FillColor);
933 function setMyTextColor($c)
935 $c = $this->setMyColor($c);
936 if (!$c) return false;
938 $this->TextColor=$c;
939 $this->ColorFlag=($this->FillColor!=$this->TextColor);
942 function setMyColor($c, $mode = false)
944 if (!is_array($c)) return sprintf('%.3F ',$c).($mode ? 'G' : 'g');
945 elseif (count($c)==3) return sprintf('%.3F %.3F %.3F ',$c[0],$c[1],$c[2]).($mode ? 'RG' : 'rg');
946 elseif (count($c)==4) return sprintf('%.3F %.3F %.3F %.3F ',$c[0],$c[1],$c[2],$c[3]).($mode ? 'K' : 'k');
947 return null;