PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/pdf/lib/mpdf50/classes/svg.php

http://openbiz-cubi.googlecode.com/
PHP | 2030 lines | 1569 code | 296 blank | 165 comment | 541 complexity | aaa5e4765137d0b9dd3d94d9de076728 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // svg class modified for mPDF version 4.4.003 by Ian Back: based on -
  3. // svg2pdf fpdf class
  4. // sylvain briand (syb@godisaduck.com), modified by rick trevino (rtrevino1@yahoo.com)
  5. // http://www.godisaduck.com/svg2pdf_with_fpdf
  6. // http://rhodopsin.blogspot.com
  7. //
  8. // cette class etendue est open source, toute modification devra cependant etre repertoriée~
  9. // NB UNITS - Works in pixels as main units - converting to PDF units when outputing to PDF string
  10. // and on returning size
  11. class SVG {
  12. var $svg_gradient; // array - contient les infos sur les gradient fill du svg classé par id du svg
  13. var $svg_shadinglist; // array - contient les ids des objet shading
  14. var $svg_info; // array contenant les infos du svg voulue par l'utilisateur
  15. var $svg_attribs; // array - holds all attributes of root <svg> tag
  16. var $svg_style; // array contenant les style de groupes du svg
  17. var $svg_string; // String contenant le tracage du svg en lui m?me.
  18. var $txt_data; // array - holds string info to write txt to image
  19. var $txt_style; // array - current text style
  20. var $mpdf_ref;
  21. var $xbase; // mPDF 4.4.003
  22. var $ybase; // mPDF 4.4.003
  23. var $svg_error; // mPDF 4.4.003
  24. var $subPathInit; // mPDF 4.4.003
  25. var $spxstart; // mPDF 4.4.003
  26. var $spystart; // mPDF 4.4.003
  27. var $kp; // mPDF 4.4.003 convert pixels to PDF units
  28. function SVG(&$mpdf){
  29. $this->svg_gradient = array();
  30. $this->svg_shadinglist = array();
  31. $this->txt_data = array();
  32. $this->svg_string = '';
  33. $this->svg_info = array();
  34. $this->svg_attribs = array();
  35. $this->xbase = 0;
  36. $this->ybase = 0;
  37. $this->svg_error = false;
  38. $this->subPathInit = false; // mPDF 4.4.003
  39. $this->dashesUsed = false; // mPDF 5.0
  40. $this->mpdf_ref =& $mpdf;
  41. $this->kp = 72 / $mpdf->img_dpi; // mPDF 4.4.003 constant To convert pixels to pts/PDF units
  42. $this->svg_style = array(
  43. array(
  44. 'fill' => 'black', // mPDF 4.4.008
  45. 'fill-opacity' => 1, // remplissage opaque par defaut
  46. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  47. 'stroke' => 'none', // pas de trait par defaut
  48. 'stroke-linecap' => 'butt', // style de langle par defaut
  49. 'stroke-linejoin' => 'miter', //
  50. 'stroke-miterlimit' => 4, // limite de langle par defaut
  51. 'stroke-opacity' => 1, // trait opaque par defaut
  52. 'stroke-width' => 1, // mPDF 4.4.011
  53. 'stroke-dasharray' => 0, // mPDF 4.4.003
  54. 'stroke-dashoffset' => 0, // mPDF 4.4.003
  55. 'color' => '' // mPDF 4.4.005
  56. )
  57. );
  58. $this->txt_style = array(
  59. array(
  60. 'fill' => 'black', // pas de remplissage par defaut
  61. 'font-family' => $mpdf->default_font,
  62. 'font-size' => $mpdf->default_font_size, // ****** this is pts
  63. 'font-weight' => 'normal', // normal | bold
  64. 'font-style' => 'normal', // italic | normal
  65. 'text-anchor' => 'start' // alignment: start, middle, end
  66. )
  67. );
  68. }
  69. function svgGradient($gradient_info, $attribs, $element){
  70. $n = count($this->mpdf_ref->gradients)+1;
  71. // Get bounding dimensions of element
  72. $w = 100;
  73. $h = 100;
  74. $x_offset = 0;
  75. $y_offset = 0;
  76. if ($element=='rect') {
  77. $w = $attribs['width'];
  78. $h = $attribs['height'];
  79. $x_offset = $attribs['x'];
  80. $y_offset = $attribs['y'];
  81. }
  82. else if ($element=='ellipse') {
  83. $w = $attribs['rx']*2;
  84. $h = $attribs['ry']*2;
  85. $x_offset = $attribs['cx']-$attribs['rx'];
  86. $y_offset = $attribs['cy']-$attribs['ry'];
  87. }
  88. else if ($element=='circle') {
  89. $w = $attribs['r']*2;
  90. $h = $attribs['r']*2;
  91. $x_offset = $attribs['cx']-$attribs['r'];
  92. $y_offset = $attribs['cy']-$attribs['r'];
  93. }
  94. else if ($element=='polygon') {
  95. $pts = preg_split('/[ ,]+/', trim($attribs['points']));
  96. $maxr=$maxb=0;
  97. $minl=$mint=999999;
  98. for ($i=0;$i<count($pts); $i++) {
  99. if ($i % 2 == 0) { // x values
  100. $minl = min($minl,$pts[$i]);
  101. $maxr = max($maxr,$pts[$i]);
  102. }
  103. else { // y values
  104. $mint = min($mint,$pts[$i]);
  105. $maxb = max($maxb,$pts[$i]);
  106. }
  107. }
  108. $w = $maxr-$minl;
  109. $h = $maxb-$mint;
  110. $x_offset = $minl;
  111. $y_offset = $mint;
  112. }
  113. else if ($element=='path') {
  114. preg_match_all('/([a-z]|[A-Z])([ ,\-.\d]+)*/', $attribs['d'], $commands, PREG_SET_ORDER);
  115. $maxr=$maxb=0;
  116. $minl=$mint=999999;
  117. foreach($commands as $c){
  118. if(count($c)==3){
  119. list($tmp, $cmd, $arg) = $c;
  120. if ($cmd=='M' || $cmd=='L' || $cmd=='C' || $cmd=='S' || $cmd=='Q' || $cmd=='T') {
  121. $pts = preg_split('/[ ,]+/', trim($arg));
  122. for ($i=0;$i<count($pts); $i++) {
  123. if ($i % 2 == 0) { // x values
  124. $minl = min($minl,$pts[$i]);
  125. $maxr = max($maxr,$pts[$i]);
  126. }
  127. else { // y values
  128. $mint = min($mint,$pts[$i]);
  129. $maxb = max($maxb,$pts[$i]);
  130. }
  131. }
  132. }
  133. if ($cmd=='H') { // sets new x
  134. $minl = min($minl,$arg);
  135. $maxr = max($maxr,$arg);
  136. }
  137. if ($cmd=='V') { // sets new y
  138. $mint = min($mint,$arg);
  139. $maxb = max($maxb,$arg);
  140. }
  141. }
  142. }
  143. $w = $maxr-$minl;
  144. $h = $maxb-$mint;
  145. $x_offset = $minl;
  146. $y_offset = $mint;
  147. }
  148. if (!$w) { $w = 100; }
  149. if (!$h) { $h = 100; }
  150. if ($x_offset==999999) { $x_offset = 0; }
  151. if ($y_offset==999999) { $y_offset = 0; }
  152. // mPDF 4.5.010
  153. // TRANSFORMATIONS
  154. $transformations = '';
  155. if (isset($gradient_info['transform'])){
  156. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$gradient_info['transform'],$m);
  157. if (count($m[0])) {
  158. for($i=0; $i<count($m[0]); $i++) {
  159. $c = strtolower($m[1][$i]);
  160. $v = trim($m[2][$i]);
  161. $vv = preg_split('/[ ,]+/',$v);
  162. if ($c=='matrix' && count($vv)==6) {
  163. $transformations .= sprintf(' %.3f %.3f %.3f %.3f %.3f %.3f cm ', $vv[0], $vv[1], $vv[2], $vv[3], $vv[4]*$this->kp, $vv[5]*$this->kp);
  164. }
  165. else if ($c=='translate' && count($vv)) {
  166. $tm[4] = $vv[0];
  167. if (count($vv)==2) { $t_y = -$vv[1]; }
  168. else { $t_y = 0; }
  169. $tm[5] = $t_y;
  170. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  171. }
  172. else if ($c=='scale' && count($vv)) {
  173. if (count($vv)==2) { $s_y = $vv[1]; }
  174. else { $s_y = $vv[0]; }
  175. $tm[0] = $vv[0];
  176. $tm[3] = $s_y;
  177. $transformations .= sprintf(' %.3f 0 0 %.3f 0 0 cm ', $tm[0], $tm[3]);
  178. }
  179. else if ($c=='rotate' && count($vv)) {
  180. $tm[0] = cos(deg2rad(-$vv[0]));
  181. $tm[1] = sin(deg2rad(-$vv[0]));
  182. $tm[2] = -$tm[1];
  183. $tm[3] = $tm[0];
  184. if (count($vv)==3) {
  185. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  186. }
  187. $transformations .= sprintf(' %.3f %.3f %.3f %.3f 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  188. if (count($vv)==3) {
  189. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  190. }
  191. }
  192. else if ($c=='skewx' && count($vv)) {
  193. $tm[2] = tan(deg2rad(-$vv[0]));
  194. $transformations .= sprintf(' 1 0 %.3f 1 0 0 cm ', $tm[2]);
  195. }
  196. else if ($c=='skewy' && count($vv)) {
  197. $tm[1] = tan(deg2rad(-$vv[0]));
  198. $transformations .= sprintf(' 1 %.3f 0 1 0 0 cm ', $tm[1]);
  199. }
  200. }
  201. }
  202. }
  203. $return = "";
  204. // This ought to make it better - but makes it worse!
  205. // if ($transformations) { $return .= $transformations; } // mPDF 4.5.010
  206. if ($gradient_info['type'] == 'linear'){
  207. // mPDF 4.4.003
  208. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  209. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  210. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  211. if (isset($gradient_info['info']['x2'])) { $gradient_info['info']['x2'] = ($gradient_info['info']['x2']-$x_offset) / $w; }
  212. if (isset($gradient_info['info']['y2'])) { $gradient_info['info']['y2'] = ($gradient_info['info']['y2']-$y_offset) / $h; }
  213. }
  214. if (isset($gradient_info['info']['x1'])) { $x1 = $gradient_info['info']['x1']; }
  215. else { $x1 = 0; }
  216. if (isset($gradient_info['info']['y1'])) { $y1 = $gradient_info['info']['y1']; }
  217. else { $y1 = 0; }
  218. if (isset($gradient_info['info']['x2'])) { $x2 = $gradient_info['info']['x2']; }
  219. else { $x2 = 1; }
  220. if (isset($gradient_info['info']['y2'])) { $y2 = $gradient_info['info']['y2']; }
  221. else { $y2 = 0; }
  222. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  223. if (stristr($x2, '%')!== false) { $x2 = ($x2+0)/100; }
  224. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  225. if (stristr($y2, '%')!== false) { $y2 = ($y2+0)/100; }
  226. $a = $w; // width
  227. $b = 0;
  228. $c = 0;
  229. $d = -$h; // height
  230. $e = $x_offset; // x- offset
  231. $f = -$y_offset; // -y-offset
  232. $return .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm ', $a*$this->kp, $b, $c, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  233. // mPDF 4.4.007 Gradient STOPs
  234. $stops = count($gradient_info['color']);
  235. if ($stops < 2) { return ''; }
  236. for ($i=0; $i<($stops-1); $i++) {
  237. $first_stop = $gradient_info['color'][$i]['offset'];
  238. $last_stop = $gradient_info['color'][($i+1)]['offset'];
  239. if (stristr($first_stop, '%')!== false) { $first_stop = ($first_stop+0)/100; }
  240. if (stristr($last_stop, '%')!== false) { $last_stop = ($last_stop+0)/100; }
  241. if ($first_stop < 0) { $first_stop = 0; }
  242. if ($last_stop > 1) { $last_stop = 1; }
  243. if ($last_stop < $first_stop) { $last_stop = $first_stop; }
  244. $grx1 = $x1 + ($x2-$x1)*$first_stop;
  245. $gry1 = $y1 + ($y2-$y1)*$first_stop;
  246. $grx2 = $x1 + ($x2-$x1)*$last_stop;
  247. $gry2 = $y1 + ($y2-$y1)*$last_stop;
  248. $this->mpdf_ref->gradients[$n+$i]['type'] = 2;
  249. $this->mpdf_ref->gradients[$n+$i]['coords']=array($grx1, $gry1, $grx2, $gry2);
  250. if (!$gradient_info['color'][$i]['color']) { $gradient_info['color'][$i]['color'] = '0 0 0'; }
  251. if (!$gradient_info['color'][$i+1]['color']) { $gradient_info['color'][$i+1]['color'] = '0 0 0'; }
  252. $this->mpdf_ref->gradients[$n+$i]['col1'] = $gradient_info['color'][$i]['color'];
  253. $this->mpdf_ref->gradients[$n+$i]['col2'] = $gradient_info['color'][$i+1]['color'];
  254. if ($i == 0 && $i == ($stops-2) )
  255. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','true');
  256. else if ($i==0)
  257. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','false');
  258. else if ($i == ($stops-2) )
  259. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','true');
  260. else
  261. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','false');
  262. $return .= '/Sh'.($n+$i).' sh ';
  263. }
  264. $return .= " Q\n";
  265. }
  266. else if ($gradient_info['type'] == 'radial'){
  267. // mPDF 4.4.003
  268. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  269. if ($w > $h) { $h = $w; }
  270. else { $w = $h; }
  271. if (isset($gradient_info['info']['x0'])) { $gradient_info['info']['x0'] = ($gradient_info['info']['x0']-$x_offset) / $w; }
  272. if (isset($gradient_info['info']['y0'])) { $gradient_info['info']['y0'] = ($gradient_info['info']['y0']-$y_offset) / $h; }
  273. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  274. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  275. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['rx'] = $gradient_info['info']['r'] / $w; }
  276. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['ry'] = $gradient_info['info']['r'] / $h; }
  277. }
  278. if ($gradient_info['info']['x0'] || $gradient_info['info']['x0']===0) { $x0 = $gradient_info['info']['x0']; }
  279. else { $x0 = 0.5; }
  280. if ($gradient_info['info']['y0'] || $gradient_info['info']['y0']===0) { $y0 = $gradient_info['info']['y0']; }
  281. else { $y0 = 0.5; }
  282. if ($gradient_info['info']['rx'] || $gradient_info['info']['rx']===0) { $rx = $gradient_info['info']['rx']; }
  283. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $rx = $gradient_info['info']['r']; }
  284. else { $rx = 0.5; }
  285. if ($gradient_info['info']['ry'] || $gradient_info['info']['ry']===0) { $ry = $gradient_info['info']['ry']; }
  286. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $ry = $gradient_info['info']['r']; }
  287. else { $ry = 0.5; }
  288. if ($gradient_info['info']['x1'] || $gradient_info['info']['x1']===0) { $x1 = $gradient_info['info']['x1']; }
  289. else { $x1 = $x0; }
  290. if ($gradient_info['info']['y1'] || $gradient_info['info']['y1']===0) { $y1 = $gradient_info['info']['y1']; }
  291. else { $y1 = $y0; }
  292. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  293. if (stristr($x0, '%')!== false) { $x0 = ($x0+0)/100; }
  294. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  295. if (stristr($y0, '%')!== false) { $y0 = ($y0+0)/100; }
  296. if (stristr($rx, '%')!== false) { $rx = ($rx+0)/100; }
  297. if (stristr($ry, '%')!== false) { $ry = ($ry+0)/100; }
  298. $r = $rx;
  299. $a = $w; // width
  300. $b = 0;
  301. $c = 0;
  302. $d = -$h; // -height
  303. $e = $x_offset; // x- offset
  304. $f = -$y_offset; // -y-offset
  305. $return .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm ', $a*$this->kp, $b, $c, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  306. // x1 and y1 (fx, fy) should be inside the circle defined by x0 y0 and r else error in mPDF
  307. while (pow(($x1-$x0),2) + pow(($y1 - $y0),2) >= pow($r,2)) { $r += 0.05; }
  308. // mPDF 4.4.007 Gradient STOPs
  309. $stops = count($gradient_info['color']);
  310. if ($stops < 2) { return ''; }
  311. for ($i=0; $i<($stops-1); $i++) {
  312. $first_stop = $gradient_info['color'][$i]['offset'];
  313. $last_stop = $gradient_info['color'][($i+1)]['offset'];
  314. if (stristr($first_stop, '%')!== false) { $first_stop = ($first_stop+0)/100; }
  315. if (stristr($last_stop, '%')!== false) { $last_stop = ($last_stop+0)/100; }
  316. if ($first_stop < 0) { $first_stop = 0; }
  317. if ($last_stop > 1) { $last_stop = 1; }
  318. if ($last_stop < $first_stop) { $last_stop = $first_stop; }
  319. $grx1 = $x1 + ($x0-$x1)*$first_stop;
  320. $gry1 = $y1 + ($y0-$y1)*$first_stop;
  321. $grx2 = $x1 + ($x0-$x1)*$last_stop;
  322. $gry2 = $y1 + ($y0-$y1)*$last_stop;
  323. $grir = $r*$first_stop;
  324. $grr = $r*$last_stop;
  325. $this->mpdf_ref->gradients[$n+$i]['type'] = 3;
  326. $this->mpdf_ref->gradients[$n+$i]['coords']=array($grx1, $gry1, $grx2, $gry2, abs($grr), abs($grir) );
  327. if (!$gradient_info['color'][$i]['color']) { $gradient_info['color'][$i]['color'] = '0 0 0'; }
  328. if (!$gradient_info['color'][$i+1]['color']) { $gradient_info['color'][$i+1]['color'] = '0 0 0'; }
  329. $this->mpdf_ref->gradients[$n+$i]['col1'] = $gradient_info['color'][$i]['color'];
  330. $this->mpdf_ref->gradients[$n+$i]['col2'] = $gradient_info['color'][$i+1]['color'];
  331. if ($i == 0 && $i == ($stops-2) )
  332. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','true');
  333. else if ($i == 0 )
  334. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','false');
  335. else if ($i == ($stops-2) )
  336. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','true');
  337. else
  338. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','false');
  339. $return .= '/Sh'.($n+$i).' sh ';
  340. }
  341. $return .= " Q\n";
  342. }
  343. return $return;
  344. }
  345. function svgOffset ($attribs){
  346. // save all <svg> tag attributes
  347. $this->svg_attribs = $attribs;
  348. if(isset($this->svg_attribs['viewBox'])) {
  349. $vb = preg_split('/\s+/is', trim($this->svg_attribs['viewBox']));
  350. if (count($vb)==4) {
  351. $this->svg_info['x'] = $vb[0];
  352. $this->svg_info['y'] = $vb[1];
  353. $this->svg_info['w'] = $vb[2];
  354. $this->svg_info['h'] = $vb[3];
  355. return;
  356. }
  357. }
  358. $svg_w = $this->mpdf_ref->ConvertSize($attribs['width']); // mm (interprets numbers as pixels)
  359. $svg_h = $this->mpdf_ref->ConvertSize($attribs['height']); // mm
  360. // Added to handle file without height or width specified
  361. if (!$svg_w && !$svg_h) { $svg_w = $svg_h = $this->mpdf_ref->blk[$this->mpdf_ref->blklvl]['inner_width'] ; } // DEFAULT
  362. if (!$svg_w) { $svg_w = $svg_h; }
  363. if (!$svg_h) { $svg_h = $svg_w; }
  364. $this->svg_info['x'] = 0;
  365. $this->svg_info['y'] = 0;
  366. $this->svg_info['w'] = $svg_w/0.2645; // mm->pixels
  367. $this->svg_info['h'] = $svg_h/0.2645; // mm->pixels
  368. }
  369. //
  370. // check if points are within svg, if not, set to max
  371. function svg_overflow($x,$y)
  372. {
  373. $x2 = $x;
  374. $y2 = $y;
  375. if(isset($this->svg_attribs['overflow']))
  376. {
  377. if($this->svg_attribs['overflow'] == 'hidden')
  378. {
  379. // Not sure if this is supposed to strip off units, but since I dont use any I will omlt this step
  380. $svg_w = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['width']);
  381. $svg_h = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['height']);
  382. // $xmax = floor($this->svg_attribs['width']);
  383. $xmax = floor($svg_w);
  384. $xmin = 0;
  385. // $ymax = floor(($this->svg_attribs['height'] * -1));
  386. $ymax = floor(($svg_h * -1));
  387. $ymin = 0;
  388. if($x > $xmax) $x2 = $xmax; // right edge
  389. if($x < $xmin) $x2 = $xmin; // left edge
  390. if($y < $ymax) $y2 = $ymax; // bottom
  391. if($y > $ymin) $y2 = $ymin; // top
  392. }
  393. }
  394. return array( 'x' => $x2, 'y' => $y2);
  395. }
  396. function svgDefineStyle($critere_style){
  397. $tmp = count($this->svg_style)-1;
  398. $current_style = $this->svg_style[$tmp];
  399. unset($current_style['transformations']);
  400. // TRANSFORM SCALE
  401. $transformations = '';
  402. if (isset($critere_style['transform'])){
  403. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$critere_style['transform'],$m);
  404. if (count($m[0])) {
  405. for($i=0; $i<count($m[0]); $i++) {
  406. $c = strtolower($m[1][$i]);
  407. $v = trim($m[2][$i]);
  408. $vv = preg_split('/[ ,]+/',$v);
  409. if ($c=='matrix' && count($vv)==6) {
  410. $transformations .= sprintf(' %.3f %.3f %.3f %.3f %.3f %.3f cm ', $vv[0], $vv[1], $vv[2], $vv[3], $vv[4]*$this->kp, $vv[5]*$this->kp);
  411. }
  412. else if ($c=='translate' && count($vv)) {
  413. $tm[4] = $vv[0];
  414. if (count($vv)==2) { $t_y = -$vv[1]; }
  415. else { $t_y = 0; }
  416. $tm[5] = $t_y;
  417. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  418. }
  419. else if ($c=='scale' && count($vv)) {
  420. if (count($vv)==2) { $s_y = $vv[1]; }
  421. else { $s_y = $vv[0]; }
  422. $tm[0] = $vv[0];
  423. $tm[3] = $s_y;
  424. $transformations .= sprintf(' %.3f 0 0 %.3f 0 0 cm ', $tm[0], $tm[3]);
  425. }
  426. else if ($c=='rotate' && count($vv)) {
  427. $tm[0] = cos(deg2rad(-$vv[0]));
  428. $tm[1] = sin(deg2rad(-$vv[0]));
  429. $tm[2] = -$tm[1];
  430. $tm[3] = $tm[0];
  431. if (count($vv)==3) {
  432. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  433. }
  434. $transformations .= sprintf(' %.3f %.3f %.3f %.3f 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  435. if (count($vv)==3) {
  436. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  437. }
  438. }
  439. else if ($c=='skewx' && count($vv)) {
  440. $tm[2] = tan(deg2rad(-$vv[0]));
  441. $transformations .= sprintf(' 1 0 %.3f 1 0 0 cm ', $tm[2]);
  442. }
  443. else if ($c=='skewy' && count($vv)) {
  444. $tm[1] = tan(deg2rad(-$vv[0]));
  445. $transformations .= sprintf(' 1 %.3f 0 1 0 0 cm ', $tm[1]);
  446. }
  447. }
  448. }
  449. $current_style['transformations'] = $transformations;
  450. }
  451. if (isset($critere_style['style'])){
  452. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  453. $current_style['fill'] = '#'.str_pad(dechex($m[1]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[2]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[3]), 2, "0", STR_PAD_LEFT);
  454. }
  455. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']); // mPDF 4.4.003
  456. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  457. }
  458. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  459. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  460. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  461. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  462. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  463. $current_style['stroke'] = '#'.str_pad(dechex($m[1]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[2]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[3]), 2, "0", STR_PAD_LEFT);
  464. }
  465. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  466. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  467. }
  468. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  469. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  470. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  471. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  472. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  473. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  474. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  475. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  476. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  477. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  478. // mPDF 4.4.003
  479. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  480. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  481. // mPDF 4.4.003
  482. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  483. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  484. }
  485. if(isset($critere_style['fill'])){
  486. $current_style['fill'] = $critere_style['fill'];
  487. }
  488. if(isset($critere_style['fill-opacity'])){
  489. $current_style['fill-opacity'] = $critere_style['fill-opacity'];
  490. }
  491. if(isset($critere_style['fill-rule'])){
  492. $current_style['fill-rule'] = $critere_style['fill-rule'];
  493. }
  494. if(isset($critere_style['stroke'])){
  495. $current_style['stroke'] = $critere_style['stroke'];
  496. }
  497. if(isset($critere_style['stroke-linecap'])){
  498. $current_style['stroke-linecap'] = $critere_style['stroke-linecap'];
  499. }
  500. if(isset($critere_style['stroke-linejoin'])){
  501. $current_style['stroke-linejoin'] = $critere_style['stroke-linejoin'];
  502. }
  503. if(isset($critere_style['stroke-miterlimit'])){
  504. $current_style['stroke-miterlimit'] = $critere_style['stroke-miterlimit'];
  505. }
  506. if(isset($critere_style['stroke-opacity'])){
  507. $current_style['stroke-opacity'] = $critere_style['stroke-opacity'];
  508. }
  509. if(isset($critere_style['stroke-width'])){
  510. $current_style['stroke-width'] = $critere_style['stroke-width'];
  511. }
  512. // mPDF 4.4.003
  513. if(isset($critere_style['stroke-dasharray'])){
  514. $current_style['stroke-dasharray'] = $critere_style['stroke-dasharray'];
  515. }
  516. if(isset($critere_style['stroke-dashoffset'])){
  517. $current_style['stroke-dashoffset'] = $critere_style['stroke-dashoffset'];
  518. }
  519. // mPDF 4.4.005 Used as indirect setting for currentColor
  520. if(isset($critere_style['color']) && $critere_style['color'] != 'inherit'){
  521. $current_style['color'] = $critere_style['color'];
  522. }
  523. return $current_style;
  524. }
  525. //
  526. // Cette fonction ecrit le style dans le stream svg.
  527. function svgStyle($critere_style, $attribs, $element){
  528. $path_style = '';
  529. if (substr_count($critere_style['fill'],'url')>0){
  530. //
  531. // couleur degradé
  532. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['fill']);
  533. if ($id_gradient != $critere_style['fill']) {
  534. if (isset($this->svg_gradient[$id_gradient])) {
  535. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  536. if ($fill_gradient) { // mPDF 4.4.003
  537. $path_style = "q ";
  538. $w = "W";
  539. $style .= 'N';
  540. }
  541. }
  542. }
  543. }
  544. // mPDF 4.4.005 Used as indirect setting for currentColor
  545. else if (strtolower($critere_style['fill']) == 'currentcolor'){
  546. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  547. if ($col) {
  548. $path_style .= sprintf('%.3f %.3f %.3f rg ',$col['R']/255,$col['G']/255,$col['B']/255);
  549. $style .= 'F';
  550. }
  551. }
  552. else if ($critere_style['fill'] != 'none'){
  553. // fill couleur pleine
  554. $col = $this->mpdf_ref->ConvertColor($critere_style['fill']);
  555. if ($col) {
  556. $path_style .= sprintf('%.3f %.3f %.3f rg ',$col['R']/255,$col['G']/255,$col['B']/255);
  557. $style .= 'F';
  558. }
  559. }
  560. // mPDF 4.4.005 Used as indirect setting for currentColor
  561. if (strtolower($critere_style['stroke']) == 'currentcolor'){
  562. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  563. if ($col) {
  564. $path_style .= sprintf('%.3f %.3f %.3f RG ',$col['R']/255,$col['G']/255,$col['B']/255);
  565. $style .= 'D';
  566. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']);
  567. $path_style .= sprintf('%.3f w ',$lw*$this->kp);
  568. }
  569. }
  570. else if ($critere_style['stroke'] != 'none'){
  571. $col = $this->mpdf_ref->ConvertColor($critere_style['stroke']);
  572. if ($col) {
  573. $path_style .= sprintf('%.3f %.3f %.3f RG ',$col['R']/255,$col['G']/255,$col['B']/255);
  574. $style .= 'D';
  575. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']); // mPDF 4.4.003
  576. $path_style .= sprintf('%.3f w ',$lw*$this->kp);
  577. }
  578. }
  579. if ($critere_style['stroke'] != 'none'){
  580. if ($critere_style['stroke-linejoin'] == 'miter'){
  581. $path_style .= ' 0 j ';
  582. }
  583. else if ($critere_style['stroke-linejoin'] == 'round'){
  584. $path_style .= ' 1 j ';
  585. }
  586. else if ($critere_style['stroke-linejoin'] == 'bevel'){
  587. $path_style .= ' 2 j ';
  588. }
  589. if ($critere_style['stroke-linecap'] == 'butt'){
  590. $path_style .= ' 0 J ';
  591. }
  592. else if ($critere_style['stroke-linecap'] == 'round'){
  593. $path_style .= ' 1 J ';
  594. }
  595. else if ($critere_style['stroke-linecap'] == 'square'){
  596. $path_style .= ' 2 J ';
  597. }
  598. if (isset($critere_style['stroke-miterlimit'])){
  599. if ($critere_style['stroke-miterlimit'] == 'none'){
  600. }
  601. else if (preg_match('/^[\d.]+$/',$critere_style['stroke-miterlimit'])) {
  602. $path_style .= sprintf('%.2f M ',$critere_style['stroke-miterlimit']);
  603. }
  604. }
  605. // mPDF 4.4.003
  606. if (isset($critere_style['stroke-dasharray'])){
  607. $off = 0;
  608. $d = preg_split('/[ ,]/',$critere_style['stroke-dasharray']);
  609. if (count($d) == 1 && $d[0]==0) {
  610. $path_style .= '[] 0 d ';
  611. }
  612. else {
  613. if (count($d) % 2 == 1) { $d = array_merge($d, $d); } // 5, 3, 1 => 5,3,1,5,3,1 OR 3 => 3,3
  614. $arr = '';
  615. for($i=0; $i<count($d); $i+=2) {
  616. $arr .= sprintf('%.3f %.3f ', $d[$i]*$this->kp, $d[$i+1]*$this->kp);
  617. }
  618. if (isset($critere_style['stroke-dashoffset'])){ $off = $critere_style['stroke-dashoffset'] + 0; }
  619. $path_style .= sprintf('[%s] %.3f d ', $arr, $off*$this->kp);
  620. }
  621. }
  622. }
  623. // mPDF 4.4.003
  624. if ($critere_style['fill-rule']=='evenodd') { $fr = '*'; }
  625. else { $fr = ''; }
  626. // mPDF 4.4.003
  627. if (isset($critere_style['fill-opacity'])) {
  628. $opacity = 1;
  629. if ($critere_style['fill-opacity'] == 0) { $opacity = 0; }
  630. else if ($critere_style['fill-opacity'] > 1) { $opacity = 1; }
  631. else if ($critere_style['fill-opacity'] > 0) { $opacity = $critere_style['fill-opacity']; }
  632. else if ($critere_style['fill-opacity'] < 0) { $opacity = 0; }
  633. $gs = $this->mpdf_ref->AddExtGState(array('ca'=>$opacity, 'BM'=>'/Normal'));
  634. $path_style .= sprintf(' /GS%d gs ', $gs);
  635. }
  636. // mPDF 4.4.003
  637. if (isset($critere_style['stroke-opacity'])) {
  638. $opacity = 1;
  639. if ($critere_style['stroke-opacity'] == 0) { $opacity = 0; }
  640. else if ($critere_style['stroke-opacity'] > 1) { $opacity = 1; }
  641. else if ($critere_style['stroke-opacity'] > 0) { $opacity = $critere_style['stroke-opacity']; }
  642. else if ($critere_style['stroke-opacity'] < 0) { $opacity = 0; }
  643. $gs = $this->mpdf_ref->AddExtGState(array('CA'=>$opacity, 'BM'=>'/Normal'));
  644. $path_style .= sprintf(' /GS%d gs ', $gs);
  645. }
  646. switch ($style){
  647. case 'F':
  648. $op = 'f';
  649. break;
  650. case 'FD':
  651. $op = 'B';
  652. break;
  653. case 'ND':
  654. $op = 'S';
  655. break;
  656. case 'D':
  657. $op = 'S';
  658. break;
  659. default:
  660. $op = 'n';
  661. }
  662. // mPDF 5.0
  663. $prestyle = $path_style.' ';
  664. $poststyle = $w.' '. $op.$fr.' '.$fill_gradient."\n";
  665. return array($prestyle,$poststyle);
  666. }
  667. //
  668. // fonction retracant les <path />
  669. function svgPath($command, $arguments){
  670. $path_cmd = '';
  671. $newsubpath = false; // mPDF 4.4.003
  672. // mPDF 4.4.003
  673. preg_match_all('/[\-^]?[\d.]+(e[\-]?[\d]+){0,1}/i', $arguments, $a, PREG_SET_ORDER);
  674. // if the command is a capital letter, the coords go absolute, otherwise relative
  675. if(strtolower($command) == $command) $relative = true;
  676. else $relative = false;
  677. $ile_argumentow = count($a);
  678. // each command may have different needs for arguments [1 to 8]
  679. switch(strtolower($command)){
  680. case 'm': // move
  681. for($i = 0; $i<$ile_argumentow; $i+=2){
  682. $x = $a[$i][0];
  683. $y = $a[$i+1][0];
  684. if($relative){
  685. $pdfx = ($this->xbase + $x);
  686. $pdfy = ($this->ybase - $y);
  687. $this->xbase += $x;
  688. $this->ybase += -$y;
  689. }
  690. else{
  691. $pdfx = $x;
  692. $pdfy = -$y ;
  693. $this->xbase = $x;
  694. $this->ybase = -$y;
  695. }
  696. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  697. if($i == 0) $path_cmd .= sprintf('%.3f %.3f m ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  698. else $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  699. // mPDF 4.4.003 Save start points of subpath
  700. if ($this->subPathInit) {
  701. $this->spxstart = $this->xbase;
  702. $this->spystart = $this->ybase;
  703. $this->subPathInit = false;
  704. }
  705. }
  706. break;
  707. case 'l': // a simple line
  708. for($i = 0; $i<$ile_argumentow; $i+=2){
  709. $x = ($a[$i][0]);
  710. $y = ($a[$i+1][0]);
  711. if($relative){
  712. $pdfx = ($this->xbase + $x);
  713. $pdfy = ($this->ybase - $y);
  714. $this->xbase += $x;
  715. $this->ybase += -$y;
  716. }
  717. else{
  718. $pdfx = $x ;
  719. $pdfy = -$y ;
  720. $this->xbase = $x;
  721. $this->ybase = -$y;
  722. }
  723. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  724. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  725. }
  726. break;
  727. case 'h': // a very simple horizontal line
  728. for($i = 0; $i<$ile_argumentow; $i++){
  729. $x = ($a[$i][0]);
  730. if($relative){
  731. $y = 0;
  732. $pdfx = ($this->xbase + $x) ;
  733. $pdfy = ($this->ybase - $y) ;
  734. $this->xbase += $x;
  735. $this->ybase += -$y;
  736. }
  737. else{
  738. $y = -$this->ybase;
  739. $pdfx = $x;
  740. $pdfy = -$y;
  741. $this->xbase = $x;
  742. $this->ybase = -$y;
  743. }
  744. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  745. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  746. }
  747. break;
  748. case 'v': // the simplest line, vertical
  749. for($i = 0; $i<$ile_argumentow; $i++){
  750. $y = ($a[$i][0]);
  751. if($relative){
  752. $x = 0;
  753. $pdfx = ($this->xbase + $x);
  754. $pdfy = ($this->ybase - $y);
  755. $this->xbase += $x;
  756. $this->ybase += -$y;
  757. }
  758. else{
  759. $x = $this->xbase;
  760. $pdfx = $x;
  761. $pdfy = -$y;
  762. $this->xbase = $x;
  763. $this->ybase = -$y;
  764. }
  765. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  766. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  767. }
  768. break;
  769. case 's': // bezier with first vertex equal first control
  770. // mPDF 4.4.003
  771. if (!($this->lastcommand == 'C' || $this->lastcommand == 'c' || $this->lastcommand == 'S' || $this->lastcommand == 's')) {
  772. $this->lastcontrolpoints = array(0,0);
  773. }
  774. for($i = 0; $i<$ile_argumentow; $i += 4){
  775. $x1 = $this->lastcontrolpoints[0];
  776. $y1 = $this->lastcontrolpoints[1];
  777. $x2 = ($a[$i][0]);
  778. $y2 = ($a[$i+1][0]);
  779. $x = ($a[$i+2][0]);
  780. $y = ($a[$i+3][0]);
  781. if($relative){
  782. $pdfx1 = ($this->xbase + $x1);
  783. $pdfy1 = ($this->ybase - $y1);
  784. $pdfx2 = ($this->xbase + $x2);
  785. $pdfy2 = ($this->ybase - $y2);
  786. $pdfx = ($this->xbase + $x);
  787. $pdfy = ($this->ybase - $y);
  788. $this->xbase += $x;
  789. $this->ybase += -$y;
  790. }
  791. else{
  792. $pdfx1 = $this->xbase + $x1;
  793. $pdfy1 = $this->ybase -$y1;
  794. $pdfx2 = $x2;
  795. $pdfy2 = -$y2;
  796. $pdfx = $x;
  797. $pdfy = -$y;
  798. $this->xbase = $x;
  799. $this->ybase = -$y;
  800. }
  801. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  802. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  803. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  804. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  805. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  806. {
  807. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  808. }
  809. else
  810. {
  811. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $pdfx1*$this->kp, $pdfy1*$this->kp, $pdfx2*$this->kp, $pdfy2*$this->kp, $pdfx*$this->kp, $pdfy*$this->kp);
  812. }
  813. }
  814. break;
  815. case 'c': // bezier with second vertex equal second control
  816. for($i = 0; $i<$ile_argumentow; $i += 6){
  817. $x1 = ($a[$i][0]);
  818. $y1 = ($a[$i+1][0]);
  819. $x2 = ($a[$i+2][0]);
  820. $y2 = ($a[$i+3][0]);
  821. $x = ($a[$i+4][0]);
  822. $y = ($a[$i+5][0]);
  823. if($relative){
  824. $pdfx1 = ($this->xbase + $x1);
  825. $pdfy1 = ($this->ybase - $y1);
  826. $pdfx2 = ($this->xbase + $x2);
  827. $pdfy2 = ($this->ybase - $y2);
  828. $pdfx = ($this->xbase + $x);
  829. $pdfy = ($this->ybase - $y);
  830. $this->xbase += $x;
  831. $this->ybase += -$y;
  832. }
  833. else{
  834. $pdfx1 = $x1;
  835. $pdfy1 = -$y1;
  836. $pdfx2 = $x2;
  837. $pdfy2 = -$y2;
  838. $pdfx = $x;
  839. $pdfy = -$y;
  840. $this->xbase = $x;
  841. $this->ybase = -$y;
  842. }
  843. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  844. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  845. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  846. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  847. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  848. {
  849. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  850. }
  851. else
  852. {
  853. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $pdfx1*$this->kp, $pdfy1*$this->kp, $pdfx2*$this->kp, $pdfy2*$this->kp, $pdfx*$this->kp, $pdfy*$this->kp);
  854. }
  855. }
  856. break;
  857. case 'q': // bezier quadratic avec point de control
  858. for($i = 0; $i<$ile_argumentow; $i += 4){
  859. $x1 = ($a[$i][0]);
  860. $y1 = ($a[$i+1][0]);
  861. $x = ($a[$i+2][0]);
  862. $y = ($a[$i+3][0]);
  863. if($relative){
  864. $pdfx = ($this->xbase + $x);
  865. $pdfy = ($this->ybase - $y);
  866. $pdfx1 = ($this->xbase + ($x1*2/3));
  867. $pdfy1 = ($this->ybase - ($y1*2/3));
  868. // mPDF 4.4.003
  869. $pdfx2 = $pdfx1 + 1/3 *($x);
  870. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  871. $this->xbase += $x;
  872. $this->ybase += -$y;
  873. }
  874. else{
  875. $pdfx = $x;
  876. $pdfy = -$y;
  877. $pdfx1 = ($this->xbase+(($x1-$this->xbase)*2/3));
  878. $pdfy1 = ($this->ybase-(($y1+$this->ybase)*2/3));
  879. $pdfx2 = ($x+(($x1-$x)*2/3));
  880. $pdfy2 = (-$y-(($y1-$y)*2/3));
  881. // mPDF 4.4.003
  882. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  883. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  884. $this->xbase = $x;
  885. $this->ybase = -$y;
  886. }
  887. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  888. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  889. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  890. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  891. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  892. {
  893. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  894. }
  895. else
  896. {
  897. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $pdfx1*$this->kp, $pdfy1*$this->kp, $pdfx2*$this->kp, $pdfy2*$this->kp, $pdfx*$this->kp, $pdfy*$this->kp);
  898. }
  899. }
  900. break;
  901. case 't': // bezier quadratic avec point de control simetrique a lancien point de control
  902. // mPDF 4.4.003
  903. if (!($this->lastcommand == 'Q' || $this->lastcommand == 'q' || $this->lastcommand == 'T' || $this->lastcommand == 't')) {
  904. $this->lastcontrolpoints = array(0,0);
  905. }
  906. for($i = 0; $i<$ile_argumentow; $i += 2){
  907. $x = ($a[$i][0]);
  908. $y = ($a[$i+1][0]);
  909. $x1 = $this->lastcontrolpoints[0];
  910. $y1 = $this->lastcontrolpoints[1];
  911. if($relative){
  912. $pdfx = ($this->xbase + $x);
  913. $pdfy = ($this->ybase - $y);
  914. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  915. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  916. // mPDF 4.4.003
  917. $pdfx2 = $pdfx1 + 1/3 *($x);
  918. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  919. $this->xbase += $x;
  920. $this->ybase += -$y;
  921. }
  922. else{
  923. $pdfx = $x;
  924. $pdfy = -$y;
  925. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  926. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  927. // mPDF 4.4.003
  928. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  929. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  930. $this->xbase = $x;
  931. $this->ybase = -$y;
  932. }
  933. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  934. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $pdfx1*$this->kp, $pdfy1*$this->kp, $pdfx2*$this->kp, $pdfy2*$this->kp, $pdfx*$this->kp, $pdfy*$this->kp);
  935. }
  936. break;
  937. case 'a': // Elliptical arc
  938. for($i = 0; $i<$ile_argumentow; $i += 7){
  939. $rx = ($a[$i][0]);
  940. $ry = ($a[$i+1][0]);
  941. $angle = ($a[$i+2][0]); //x-axis-rotation
  942. $largeArcFlag = ($a[$i+3][0]);
  943. $sweepFlag = ($a[$i+4][0]);
  944. $x2 = ($a[$i+5][0]);
  945. $y2 = ($a[$i+6][0]);
  946. $x1 = $this->xbase;
  947. $y1 = -$this->ybase;
  948. if($relative){
  949. $x2 = $this->xbase + $x2;
  950. $y2 = -$this->ybase + $y2;
  951. $this->xbase += ($a[$i+5][0]);
  952. $this->ybase += -($a[$i+6][0]);
  953. }
  954. else{
  955. $this->xbase = $x2;
  956. $this->ybase = -$y2;
  957. }
  958. $path_cmd .= $this->Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag);
  959. }
  960. break;
  961. case'z':
  962. $path_cmd .= 'h ';
  963. // mPDF 4.4.003
  964. $this->subPathInit = true;
  965. $newsubpath = true;
  966. $this->xbase = $this->spxstart;
  967. $this->ybase = $this->spystart;
  968. break;
  969. default:
  970. break;
  971. }
  972. if (!$newsubpath) { $this->subPathInit = false; } // mPDF 4.4.003
  973. $this->lastcommand = $command;
  974. return $path_cmd;
  975. }
  976. function Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag) {
  977. // 1. Treat out-of-range parameters as described in
  978. // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  979. // If the endpoints (x1, y1) and (x2, y2) are identical, then this
  980. // is equivalent to omitting the elliptical arc segment entirely
  981. if ($x1 == $x2 && $y1 == $y2) return '';
  982. // If rX = 0 or rY = 0 then this arc is treated as a straight line
  983. // segment (a "lineto") joining the endpoints.
  984. if ($rx == 0.0 || $ry == 0.0) {
  985. // return Lineto(x2, y2); // ****
  986. }
  987. // If rX or rY have negative signs, these are dropped; the absolute
  988. // value is used instead.
  989. if ($rx<0.0) $rx = -$rx;
  990. if ($ry<0.0) $ry = -$ry;
  991. // 2. convert to center parameterization as shown in
  992. // http://www.w3.org/TR/SVG/implnote.html
  993. $sinPhi = sin(deg2rad($angle));
  994. $cosPhi = cos(deg2rad($angle));
  995. $x1dash = $cosPhi * ($x1-$x2)/2.0 + $sinPhi * ($y1-$y2)/2.0;
  996. $y1dash = -$sinPhi * ($x1-$x2)/2.0 + $cosPhi * ($y1-$y2)/2.0;
  997. $numerator = $rx*$rx*$ry*$ry - $rx*$rx*$y1dash*$y1dash - $ry*$ry*$x1dash*$x1dash;
  998. if ($numerator < 0.0) {
  999. // If rX , rY and are such that there is no solution (basically,
  1000. // the ellipse is not big enough to reach from (x1, y1) to (x2,
  1001. // y2)) then the ellipse is scaled up uniformly until there is
  1002. // exactly one solution (until the ellipse is just big enough).
  1003. // -> find factor s, such that numerator' with rx'=s*rx and
  1004. // ry'=s*ry becomes 0 :
  1005. $s = sqrt(1.0 - $numerator/($rx*$rx*$ry*$ry));
  1006. $rx *= $s;
  1007. $ry *= $s;
  1008. $root = 0.0;
  1009. }
  1010. else {
  1011. $root = ($largeArcFlag == $sweepFlag ? -1.0 : 1.0) * sqrt( $numerator/($rx*$rx*$y1dash*$y1dash+$ry*$ry*$x1dash*$x1dash) );
  1012. }
  1013. $cxdash = $root*$rx*$y1dash/$ry;
  1014. $cydash = -$root*$ry*$x1dash/$rx;
  1015. $cx = $cosPhi * $cxdash - $sinPhi * $cydash + ($x1+$x2)/2.0;
  1016. $cy = $sinPhi * $cxdash + $cosPhi * $cydash + ($y1+$y2)/2.0;
  1017. $theta1 = $this->CalcVectorAngle(1.0, 0.0, ($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry);
  1018. $dtheta = $this->CalcVectorAngle(($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry, (-$x1dash-$cxdash)/$rx, (-$y1dash-$cydash)/$ry);
  1019. if (!$sweepFlag && $dtheta>0)
  1020. $dtheta -= 2.0*M_PI;
  1021. else if ($sweepFlag && $dtheta<0)
  1022. $dtheta += 2.0*M_PI;
  1023. // 3. convert into cubic bezier segments <= 90deg
  1024. $segments = ceil(abs($dtheta/(M_PI/2.0)));
  1025. $delta = $dtheta/$segments;
  1026. $t = 8.0/3.0 * sin($delta/4.0) * sin($delta/4.0) / sin($delta/2.0);
  1027. $coords = array();
  1028. for ($i = 0; $i < $segments; $i++) {
  1029. $cosTheta1 = cos($theta1);
  1030. $sinTheta1 = sin($theta1);
  1031. $theta2 = $theta1 + $delta;
  1032. $cosTheta2 = cos($theta2);
  1033. $sinTheta2 = sin($theta2);
  1034. // a) calculate endpoint of the segment:
  1035. $xe = $cosPhi * $rx*$cosTheta2 - $sinPhi * $ry*$sinTheta2 + $cx;
  1036. $ye = $sinPhi * $rx*$cosTheta2 + $cosPhi * $ry*$sinTheta2 + $cy;
  1037. // b) calculate gradients at start/end points of segment:
  1038. $dx1 = $t * ( - $cosPhi * $rx*$sinTheta1 - $sinPhi * $ry*$cosTheta1);
  1039. $dy1 = $t * ( - $sinPhi * $rx*$sinTheta1 + $cosPhi * $ry*$cosTheta1);
  1040. $dxe = $t * ( $cosPhi * $rx*$sinTheta2 + $sinPhi * $ry*$cosTheta2);
  1041. $dye = $t * ( $sinPhi * $rx*$sinTheta2 - $cosPhi * $ry*$cosTheta2);
  1042. // c) draw the cubic bezier:
  1043. $coords[$i] = array(($x1+$dx1), ($y1+$dy1), ($xe+$dxe), ($ye+$dye), $xe, $ye);
  1044. // do next segment
  1045. $theta1 = $theta2;
  1046. $x1 = $xe;
  1047. $y1 = $ye;
  1048. }
  1049. $path = ' ';
  1050. foreach($coords AS $c) {
  1051. $cpx1 = $c[0];
  1052. $cpy1 = $c[1];
  1053. $cpx2 = $c[2];
  1054. $cpy2 = $c[3];
  1055. $x2 = $c[4];
  1056. $y2 = $c[5];
  1057. $path .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $cpx1*$this->kp, -$cpy1*$this->kp, $cpx2*$this->kp, -$cpy2*$this->kp, $x2*$this->kp, -$y2*$this->kp) ."\n";
  1058. }
  1059. return $path ;
  1060. }
  1061. function CalcVectorAngle($ux, $uy, $vx, $vy) {
  1062. $ta = atan2($uy, $ux);
  1063. $tb = atan2($vy, $vx);
  1064. if ($tb >= $ta)
  1065. return ($tb-$ta);
  1066. return (6.28318530718 - ($ta-$tb));
  1067. }
  1068. // mPDF 4.4.003
  1069. function ConvertSVGSizePixels($size=5,$maxsize='x'){
  1070. // maxsize in pixels (user units) or 'y' or 'x'
  1071. // e.g. $w = $this->ConvertSVGSizePixels($arguments['w'],$this->svg_info['w']*(25.4/$this->mpdf_ref->dpi));
  1072. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1073. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1074. // For text $maxsize = Fontsize
  1075. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1076. if ($maxsize == 'y') { $maxsize = $this->svg_info['h']; }
  1077. else if ($maxsize == 'x') { $maxsize = $this->svg_info['w']; }
  1078. $maxsize *= (25.4/$this->mpdf_ref->dpi); // convert pixels to mm
  1079. $fontsize=$this->mpdf_ref->FontSize;
  1080. //Return as pixels
  1081. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,$fontsize,false) * 1/(25.4/$this->mpdf_ref->dpi);
  1082. return $size;
  1083. }
  1084. // mPDF 4.4.003
  1085. function ConvertSVGSizePts($size=5){
  1086. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1087. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1088. // For text $maxsize = Fontsize
  1089. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1090. $maxsize=$this->mpdf_ref->FontSize;
  1091. //Return as pts
  1092. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,false,true) * 72/25.4;
  1093. return $size;
  1094. }
  1095. //
  1096. // fonction retracant les <rect />
  1097. function svgRect($arguments){
  1098. if ($arguments['h']==0 || $arguments['w']==0) { return ''; } // mPDF 4.4.003
  1099. $x = $this->ConvertSVGSizePixels($arguments['x'],'x'); // mPDF 4.4.003
  1100. $y = $this->ConvertSVGSizePixels($arguments['y'],'y'); // mPDF 4.4.003
  1101. $h = $this->ConvertSVGSizePixels($arguments['h'],'y'); // mPDF 4.4.003
  1102. $w = $this->ConvertSVGSizePixels($arguments['w'],'x'); // mPDF 4.4.003
  1103. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1104. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1105. if ($rx > $w/2) { $rx = $w/2; } // mPDF 4.4.003
  1106. if ($ry > $h/2) { $ry = $h/2; } // mPDF 4.4.003
  1107. if ($rx>0 and $ry == 0){$ry = $rx;}
  1108. if ($ry>0 and $rx == 0){$rx = $ry;}
  1109. if ($rx == 0 and $ry == 0){
  1110. // trace un rectangle sans angle arrondit
  1111. $path_cmd = sprintf('%.3f %.3f m ', ($x*$this->kp), -($y*$this->kp));
  1112. $path_cmd .= sprintf('%.3f %.3f l ', (($x+$w)*$this->kp), -($y*$this->kp));
  1113. $path_cmd .= sprintf('%.3f %.3f l ', (($x+$w)*$this->kp), -(($y+$h)*$this->kp));
  1114. $path_cmd .= sprintf('%.3f %.3f l ', ($x)*$this->kp, -(($y+$h)*$this->kp));
  1115. $path_cmd .= sprintf('%.3f %.3f l h ', ($x*$this->kp), -($y*$this->kp));
  1116. }
  1117. else {
  1118. // trace un rectangle avec les arrondit
  1119. // les points de controle du bezier sont deduis grace a la constante kappa
  1120. $kappa = 4*(sqrt(2)-1)/3;
  1121. $kx = $kappa*$rx;
  1122. $ky = $kappa*$ry;
  1123. $path_cmd = sprintf('%.3f %.3f m ', ($x+$rx)*$this->kp, -$y*$this->kp);
  1124. $path_cmd .= sprintf('%.3f %.3f l ', ($x+($w-$rx))*$this->kp, -$y*$this->kp);
  1125. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x+($w-$rx+$kx))*$this->kp, -$y*$this->kp, ($x+$w)*$this->kp, (-$y+(-$ry+$ky))*$this->kp, ($x+$w)*$this->kp, (-$y+(-$ry))*$this->kp );
  1126. $path_cmd .= sprintf('%.3f %.3f l ', ($x+$w)*$this->kp, (-$y+(-$h+$ry))*$this->kp);
  1127. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x+$w)*$this->kp, (-$y+(-$h-$ky+$ry))*$this->kp, ($x+($w-$rx+$kx))*$this->kp, (-$y+(-$h))*$this->kp, ($x+($w-$rx))*$this->kp, (-$y+(-$h))*$this->kp );
  1128. $path_cmd .= sprintf('%.3f %.3f l ', ($x+$rx)*$this->kp, (-$y+(-$h))*$this->kp);
  1129. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x+($rx-$kx))*$this->kp, (-$y+(-$h))*$this->kp, $x*$this->kp, (-$y+(-$h-$ky+$ry))*$this->kp, $x*$this->kp, (-$y+(-$h+$ry))*$this->kp );
  1130. $path_cmd .= sprintf('%.3f %.3f l ', $x*$this->kp, (-$y+(-$ry))*$this->kp);
  1131. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c h ', $x*$this->kp, (-$y+(-$ry+$ky))*$this->kp, ($x+($rx-$kx))*$this->kp, -$y*$this->kp, ($x+$rx)*$this->kp, -$y*$this->kp );
  1132. }
  1133. return $path_cmd;
  1134. }
  1135. //
  1136. // fonction retracant les <ellipse /> et <circle />
  1137. // le cercle est tracé grave a 4 bezier cubic, les poitn de controles
  1138. // sont deduis grace a la constante kappa * rayon
  1139. function svgEllipse($arguments){
  1140. if ($arguments['rx']==0 || $arguments['ry']==0) { return ''; } // mPDF 4.4.003
  1141. $kappa = 4*(sqrt(2)-1)/3;
  1142. $cx = $this->ConvertSVGSizePixels($arguments['cx'],'x'); // mPDF 4.4.003
  1143. $cy = $this->ConvertSVGSizePixels($arguments['cy'],'y'); // mPDF 4.4.003
  1144. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1145. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1146. $x1 = $cx;
  1147. $y1 = -$cy+$ry;
  1148. $x2 = $cx+$rx;
  1149. $y2 = -$cy;
  1150. $x3 = $cx;
  1151. $y3 = -$cy-$ry;
  1152. $x4 = $cx-$rx;
  1153. $y4 = -$cy;
  1154. $path_cmd = sprintf('%.3f %.3f m ', $x1*$this->kp, $y1*$this->kp);
  1155. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x1+($rx*$kappa))*$this->kp, $y1*$this->kp, $x2*$this->kp, ($y2+($ry*$kappa))*$this->kp, $x2*$this->kp, $y2*$this->kp);
  1156. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $x2*$this->kp, ($y2-($ry*$kappa))*$this->kp, ($x3+($rx*$kappa))*$this->kp, $y3*$this->kp, $x3*$this->kp, $y3*$this->kp);
  1157. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x3-($rx*$kappa))*$this->kp, $y3*$this->kp, $x4*$this->kp, ($y4-($ry*$kappa))*$this->kp, $x4*$this->kp, $y4*$this->kp);
  1158. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $x4*$this->kp, ($y4+($ry*$kappa))*$this->kp, ($x1-($rx*$kappa))*$this->kp, $y1*$this->kp, $x1*$this->kp, $y1*$this->kp);
  1159. $path_cmd .= 'h ';
  1160. return $path_cmd;
  1161. }
  1162. //
  1163. // fonction retracant les <polyline /> et les <line />
  1164. function svgPolyline($arguments,$ispolyline=true){
  1165. if ($ispolyline) {
  1166. $xbase = $arguments[0] ;
  1167. $ybase = - $arguments[1] ;
  1168. }
  1169. else {
  1170. if ($arguments[0]==$arguments[2] && $arguments[1]==$arguments[3]) { return ''; } // mPDF 4.4.003 Zero length line
  1171. $xbase = $this->C

Large files files are truncated, but you can click here to view the full file