PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/app/library/MPDF45/classes/svg.php

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

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