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

/application/helpers/mpdf/classes/svg.php

https://bitbucket.org/woolfg/fusioninvoice
PHP | 2600 lines | 1990 code | 333 blank | 277 comment | 682 complexity | 6c44e0c0b88fc77a1c9b2fb137254866 MD5 | raw file
Possible License(s): LGPL-2.1

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. var $pathBBox; // mPDF 5.0.039
  29. function SVG(&$mpdf){
  30. $this->svg_gradient = array();
  31. $this->svg_shadinglist = array();
  32. $this->txt_data = array();
  33. $this->svg_string = '';
  34. $this->svg_info = array();
  35. $this->svg_attribs = array();
  36. $this->xbase = 0;
  37. $this->ybase = 0;
  38. $this->svg_error = false;
  39. $this->subPathInit = false; // mPDF 4.4.003
  40. $this->dashesUsed = false; // mPDF 5.0
  41. $this->mpdf_ref =& $mpdf;
  42. $this->kp = 72 / $mpdf->img_dpi; // mPDF 4.4.003 constant To convert pixels to pts/PDF units
  43. $this->kf = 1; // mPDF 5.0.039 constant To convert font size if re-mapped
  44. $this->pathBBox = array(); // mPDF 5.0.039
  45. $this->svg_style = array(
  46. array(
  47. 'fill' => 'black', // mPDF 4.4.008
  48. 'fill-opacity' => 1, // remplissage opaque par defaut
  49. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  50. 'stroke' => 'none', // pas de trait par defaut
  51. 'stroke-linecap' => 'butt', // style de langle par defaut
  52. 'stroke-linejoin' => 'miter', //
  53. 'stroke-miterlimit' => 4, // limite de langle par defaut
  54. 'stroke-opacity' => 1, // trait opaque par defaut
  55. 'stroke-width' => 1, // mPDF 4.4.011
  56. 'stroke-dasharray' => 0, // mPDF 4.4.003
  57. 'stroke-dashoffset' => 0, // mPDF 4.4.003
  58. 'color' => '' // mPDF 4.4.005
  59. )
  60. );
  61. $this->txt_style = array(
  62. array(
  63. 'fill' => 'black', // pas de remplissage par defaut
  64. 'font-family' => $mpdf->default_font,
  65. 'font-size' => $mpdf->default_font_size, // ****** this is pts
  66. 'font-weight' => 'normal', // normal | bold
  67. 'font-style' => 'normal', // italic | normal
  68. 'text-anchor' => 'start', // alignment: start, middle, end
  69. /* mPDF 5.0.041 */
  70. 'fill-opacity' => 1, // remplissage opaque par defaut
  71. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  72. 'stroke' => 'none', // pas de trait par defaut
  73. 'stroke-opacity' => 1, // trait opaque par defaut
  74. 'stroke-width' => 1, // mPDF 4.4.011
  75. 'color' => '' // mPDF 4.4.005
  76. )
  77. );
  78. }
  79. function svgGradient($gradient_info, $attribs, $element){
  80. $n = count($this->mpdf_ref->gradients)+1;
  81. // Get bounding dimensions of element
  82. $w = 100;
  83. $h = 100;
  84. $x_offset = 0;
  85. $y_offset = 0;
  86. if ($element=='rect') {
  87. $w = $attribs['width'];
  88. $h = $attribs['height'];
  89. $x_offset = $attribs['x'];
  90. $y_offset = $attribs['y'];
  91. }
  92. else if ($element=='ellipse') {
  93. $w = $attribs['rx']*2;
  94. $h = $attribs['ry']*2;
  95. $x_offset = $attribs['cx']-$attribs['rx'];
  96. $y_offset = $attribs['cy']-$attribs['ry'];
  97. }
  98. else if ($element=='circle') {
  99. $w = $attribs['r']*2;
  100. $h = $attribs['r']*2;
  101. $x_offset = $attribs['cx']-$attribs['r'];
  102. $y_offset = $attribs['cy']-$attribs['r'];
  103. }
  104. else if ($element=='polygon') {
  105. $pts = preg_split('/[ ,]+/', trim($attribs['points']));
  106. $maxr=$maxb=0;
  107. $minl=$mint=999999;
  108. for ($i=0;$i<count($pts); $i++) {
  109. if ($i % 2 == 0) { // x values
  110. $minl = min($minl,$pts[$i]);
  111. $maxr = max($maxr,$pts[$i]);
  112. }
  113. else { // y values
  114. $mint = min($mint,$pts[$i]);
  115. $maxb = max($maxb,$pts[$i]);
  116. }
  117. }
  118. $w = $maxr-$minl;
  119. $h = $maxb-$mint;
  120. $x_offset = $minl;
  121. $y_offset = $mint;
  122. }
  123. else if ($element=='path') {
  124. // mPDF 5.0.039
  125. if (is_array($this->pathBBox) && $this->pathBBox[2]>0) {
  126. $w = $this->pathBBox[2];
  127. $h = $this->pathBBox[3];
  128. $x_offset = $this->pathBBox[0];
  129. $y_offset = $this->pathBBox[1];
  130. }
  131. else {
  132. preg_match_all('/([a-z]|[A-Z])([ ,\-.\d]+)*/', $attribs['d'], $commands, PREG_SET_ORDER);
  133. $maxr=$maxb=0;
  134. $minl=$mint=999999;
  135. foreach($commands as $c){
  136. if(count($c)==3){
  137. list($tmp, $cmd, $arg) = $c;
  138. if ($cmd=='M' || $cmd=='L' || $cmd=='C' || $cmd=='S' || $cmd=='Q' || $cmd=='T') {
  139. $pts = preg_split('/[ ,]+/', trim($arg));
  140. for ($i=0;$i<count($pts); $i++) {
  141. if ($i % 2 == 0) { // x values
  142. $minl = min($minl,$pts[$i]);
  143. $maxr = max($maxr,$pts[$i]);
  144. }
  145. else { // y values
  146. $mint = min($mint,$pts[$i]);
  147. $maxb = max($maxb,$pts[$i]);
  148. }
  149. }
  150. }
  151. if ($cmd=='H') { // sets new x
  152. $minl = min($minl,$arg);
  153. $maxr = max($maxr,$arg);
  154. }
  155. if ($cmd=='V') { // sets new y
  156. $mint = min($mint,$arg);
  157. $maxb = max($maxb,$arg);
  158. }
  159. }
  160. }
  161. $w = $maxr-$minl;
  162. $h = $maxb-$mint;
  163. $x_offset = $minl;
  164. $y_offset = $mint;
  165. }
  166. }
  167. if (!$w || $w==-999999) { $w = 100; }
  168. if (!$h || $h==-999999) { $h = 100; }
  169. if ($x_offset==999999) { $x_offset = 0; }
  170. if ($y_offset==999999) { $y_offset = 0; }
  171. // mPDF 4.5.010
  172. // TRANSFORMATIONS
  173. $transformations = '';
  174. if (isset($gradient_info['transform'])){
  175. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$gradient_info['transform'],$m);
  176. if (count($m[0])) {
  177. for($i=0; $i<count($m[0]); $i++) {
  178. $c = strtolower($m[1][$i]);
  179. $v = trim($m[2][$i]);
  180. $vv = preg_split('/[ ,]+/',$v);
  181. if ($c=='matrix' && count($vv)==6) {
  182. // mPDF 5.0.039
  183. // Note angle of rotation is reversed (from SVG to PDF), so vv[1] and vv[2] are negated
  184. // cf svgDefineStyle()
  185. $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);
  186. }
  187. else if ($c=='translate' && count($vv)) {
  188. $tm[4] = $vv[0];
  189. if (count($vv)==2) { $t_y = -$vv[1]; }
  190. else { $t_y = 0; }
  191. $tm[5] = $t_y;
  192. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  193. }
  194. else if ($c=='scale' && count($vv)) {
  195. if (count($vv)==2) { $s_y = $vv[1]; }
  196. else { $s_y = $vv[0]; }
  197. $tm[0] = $vv[0];
  198. $tm[3] = $s_y;
  199. $transformations .= sprintf(' %.3F 0 0 %.3F 0 0 cm ', $tm[0], $tm[3]);
  200. }
  201. else if ($c=='rotate' && count($vv)) {
  202. $tm[0] = cos(deg2rad(-$vv[0]));
  203. $tm[1] = sin(deg2rad(-$vv[0]));
  204. $tm[2] = -$tm[1];
  205. $tm[3] = $tm[0];
  206. if (count($vv)==3) {
  207. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  208. }
  209. $transformations .= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  210. if (count($vv)==3) {
  211. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  212. }
  213. }
  214. else if ($c=='skewx' && count($vv)) {
  215. $tm[2] = tan(deg2rad(-$vv[0]));
  216. $transformations .= sprintf(' 1 0 %.3F 1 0 0 cm ', $tm[2]);
  217. }
  218. else if ($c=='skewy' && count($vv)) {
  219. $tm[1] = tan(deg2rad(-$vv[0]));
  220. $transformations .= sprintf(' 1 %.3F 0 1 0 0 cm ', $tm[1]);
  221. }
  222. }
  223. }
  224. }
  225. $return = "";
  226. // mPDF 5.0.039
  227. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  228. if ($transformations) { $return .= $transformations; }
  229. }
  230. // mPDF 5.0.040
  231. $spread = 'P'; // pad
  232. if (isset($gradient_info['spread'])) {
  233. if (strtolower($gradient_info['spread'])=='reflect') { $spread = 'F'; } // reflect
  234. else if (strtolower($gradient_info['spread'])=='repeat') { $spread = 'R'; } // repeat
  235. }
  236. for ($i=0; $i<(count($gradient_info['color'])); $i++) {
  237. if (stristr($gradient_info['color'][$i]['offset'], '%')!== false) { $gradient_info['color'][$i]['offset'] = ($gradient_info['color'][$i]['offset']+0)/100; }
  238. if (stristr($gradient_info['color'][($i+1)]['offset'], '%')!== false) { $gradient_info['color'][($i+1)]['offset'] = ($gradient_info['color'][($i+1)]['offset']+0)/100; }
  239. if ($gradient_info['color'][$i]['offset']<0) { $gradient_info['color'][$i]['offset'] = 0; }
  240. if ($gradient_info['color'][$i]['offset']>1) { $gradient_info['color'][$i]['offset'] = 1; }
  241. if ($i>0) {
  242. if ($gradient_info['color'][$i]['offset']<$gradient_info['color'][($i-1)]['offset']) {
  243. $gradient_info['color'][$i]['offset']=$gradient_info['color'][($i-1)]['offset'];
  244. }
  245. }
  246. }
  247. if ($gradient_info['color'][0]['offset']>0) {
  248. array_unshift($gradient_info['color'], $gradient_info['color'][0]);
  249. $gradient_info['color'][0]['offset'] = 0;
  250. }
  251. $ns = count($gradient_info['color']);
  252. if ($gradient_info['color'][($ns-1)]['offset']<1) {
  253. $gradient_info['color'][] = $gradient_info['color'][($ns-1)];
  254. $gradient_info['color'][($ns)]['offset'] = 1;
  255. }
  256. $ns = count($gradient_info['color']);
  257. if ($gradient_info['type'] == 'linear'){
  258. // mPDF 4.4.003
  259. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  260. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  261. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  262. if (isset($gradient_info['info']['x2'])) { $gradient_info['info']['x2'] = ($gradient_info['info']['x2']-$x_offset) / $w; }
  263. if (isset($gradient_info['info']['y2'])) { $gradient_info['info']['y2'] = ($gradient_info['info']['y2']-$y_offset) / $h; }
  264. }
  265. if (isset($gradient_info['info']['x1'])) { $x1 = $gradient_info['info']['x1']; }
  266. else { $x1 = 0; }
  267. if (isset($gradient_info['info']['y1'])) { $y1 = $gradient_info['info']['y1']; }
  268. else { $y1 = 0; }
  269. if (isset($gradient_info['info']['x2'])) { $x2 = $gradient_info['info']['x2']; }
  270. else { $x2 = 1; }
  271. if (isset($gradient_info['info']['y2'])) { $y2 = $gradient_info['info']['y2']; }
  272. else { $y2 = 0; }
  273. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  274. if (stristr($x2, '%')!== false) { $x2 = ($x2+0)/100; }
  275. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  276. if (stristr($y2, '%')!== false) { $y2 = ($y2+0)/100; }
  277. // mPDF 5.0.042
  278. $bboxw = $w;
  279. $bboxh = $h;
  280. $usex = $x_offset;
  281. $usey = $y_offset;
  282. $usew = $bboxw;
  283. $useh = $bboxh;
  284. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  285. $angle = rad2deg(atan2(($gradient_info['info']['y2']-$gradient_info['info']['y1']), ($gradient_info['info']['x2']-$gradient_info['info']['x1'])));
  286. if ($angle < 0) { $angle += 360; }
  287. else if ($angle > 360) { $angle -= 360; }
  288. if ($angle!=0 && $angle!=360 && $angle!=90 && $angle!=180 && $angle!=270) {
  289. if ($w >= $h) {
  290. $y1 *= $h/$w ;
  291. $y2 *= $h/$w ;
  292. $usew = $useh = $bboxw;
  293. }
  294. else {
  295. $x1 *= $w/$h ;
  296. $x2 *= $w/$h ;
  297. $usew = $useh = $bboxh;
  298. }
  299. }
  300. }
  301. $a = $usew; // width
  302. $d = -$useh; // height
  303. $e = $usex; // x- offset
  304. $f = -$usey; // -y-offset
  305. $return .= sprintf('%.3F 0 0 %.3F %.3F %.3F cm ', $a*$this->kp, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  306. // mPDF 5.0.039
  307. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='objectboundingbox') {
  308. if ($transformations) { $return .= $transformations; }
  309. }
  310. // mPDF 5.0.020
  311. $trans = false;
  312. // mPDF 5.0.040
  313. if ($spread=='R' || $spread=='F') { // Repeat / Reflect
  314. $offs = array();
  315. for($i=0;$i<$ns;$i++) {
  316. $offs[$i] = $gradient_info['color'][$i]['offset'];
  317. }
  318. $gp = 0;
  319. $inside=true;
  320. while($inside) {
  321. $gp++;
  322. for($i=0;$i<$ns;$i++) {
  323. if ($spread=='F' && ($gp % 2) == 1) { // Reflect
  324. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][(($ns*($gp-1))+($ns-$i-1))];
  325. $tmp = $gp+(1-$offs[($ns-$i-1)]) ;
  326. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  327. }
  328. else { // Reflect
  329. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][$i];
  330. $tmp = $gp+$offs[$i] ;
  331. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  332. }
  333. // IF STILL INSIDE BOX OR STILL VALID
  334. // Point on axis to test
  335. $px1 = $x1 + ($x2-$x1)*$tmp;
  336. $py1 = $y1 + ($y2-$y1)*$tmp;
  337. // Get perpendicular axis
  338. $alpha = atan2($y2-$y1, $x2-$x1);
  339. $alpha += M_PI/2; // rotate 90 degrees
  340. // Get arbitrary point to define line perpendicular to axis
  341. $px2 = $px1+cos($alpha);
  342. $py2 = $py1+sin($alpha);
  343. $res1 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 0, 1); // $x=0 vert axis
  344. $res2 = _testIntersect($px1, $py1, $px2, $py2, 1, 0, 1, 1); // $x=1 vert axis
  345. $res3 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 1, 0); // $y=0 horiz axis
  346. $res4 = _testIntersect($px1, $py1, $px2, $py2, 0, 1, 1, 1); // $y=1 horiz axis
  347. if (!$res1 && !$res2 && !$res3 && !$res4) { $inside = false; }
  348. }
  349. }
  350. $inside=true;
  351. $gp = 0;
  352. while($inside) {
  353. $gp++;
  354. $newarr = array();
  355. for($i=0;$i<$ns;$i++) {
  356. if ($spread=='F') { // Reflect
  357. $newarr[$i] = $gradient_info['color'][($ns-$i-1)];
  358. if (($gp % 2) == 1) {
  359. $tmp = -$gp+(1-$offs[($ns-$i-1)]);
  360. $newarr[$i]['offset'] = $tmp;
  361. }
  362. else {
  363. $tmp = -$gp+$offs[$i];
  364. $newarr[$i]['offset'] = $tmp;
  365. }
  366. }
  367. else { // Reflect
  368. $newarr[$i] = $gradient_info['color'][$i];
  369. $tmp = -$gp+$offs[$i];
  370. $newarr[$i]['offset'] = $tmp;
  371. }
  372. // IF STILL INSIDE BOX OR STILL VALID
  373. // Point on axis to test
  374. $px1 = $x1 + ($x2-$x1)*$tmp;
  375. $py1 = $y1 + ($y2-$y1)*$tmp;
  376. // Get perpendicular axis
  377. $alpha = atan2($y2-$y1, $x2-$x1);
  378. $alpha += M_PI/2; // rotate 90 degrees
  379. // Get arbitrary point to define line perpendicular to axis
  380. $px2 = $px1+cos($alpha);
  381. $py2 = $py1+sin($alpha);
  382. $res1 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 0, 1); // $x=0 vert axis
  383. $res2 = _testIntersect($px1, $py1, $px2, $py2, 1, 0, 1, 1); // $x=1 vert axis
  384. $res3 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 1, 0); // $y=0 horiz axis
  385. $res4 = _testIntersect($px1, $py1, $px2, $py2, 0, 1, 1, 1); // $y=1 horiz axis
  386. if (!$res1 && !$res2 && !$res3 && !$res4) { $inside = false; }
  387. }
  388. for($i=($ns-1);$i>=0;$i--) {
  389. if (isset($newarr[$i]['offset'])) array_unshift($gradient_info['color'], $newarr[$i]);
  390. }
  391. }
  392. }
  393. // mPDF 4.4.007 Gradient STOPs
  394. $stops = count($gradient_info['color']);
  395. if ($stops < 2) { return ''; }
  396. // mPDF 5.0.042
  397. $range = $gradient_info['color'][count($gradient_info['color'])-1]['offset']-$gradient_info['color'][0]['offset'];
  398. $min = $gradient_info['color'][0]['offset'];
  399. for ($i=0; $i<($stops); $i++) {
  400. // mPDF 5.0.051
  401. if (!$gradient_info['color'][$i]['color']) {
  402. if ($gradient_info['colorspace']=='RGB') $gradient_info['color'][$i]['color'] = '0 0 0';
  403. else if ($gradient_info['colorspace']=='Gray') $gradient_info['color'][$i]['color'] = '0';
  404. else if ($gradient_info['colorspace']=='CMYK') $gradient_info['color'][$i]['color'] = '1 1 1 1';
  405. }
  406. $offset = ($gradient_info['color'][$i]['offset'] - $min)/$range;
  407. $this->mpdf_ref->gradients[$n]['stops'][] = array(
  408. 'col' => $gradient_info['color'][$i]['color'],
  409. 'opacity' => $gradient_info['color'][$i]['opacity'],
  410. 'offset' => $offset);
  411. if ($gradient_info['color'][$i]['opacity']<1) { $trans = true; }
  412. }
  413. $grx1 = $x1 + ($x2-$x1)*$gradient_info['color'][0]['offset'];
  414. $gry1 = $y1 + ($y2-$y1)*$gradient_info['color'][0]['offset'];
  415. $grx2 = $x1 + ($x2-$x1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  416. $gry2 = $y1 + ($y2-$y1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  417. $this->mpdf_ref->gradients[$n]['coords']=array($grx1, $gry1, $grx2, $gry2);
  418. $this->mpdf_ref->gradients[$n]['colorspace'] = $gradient_info['colorspace']; // mPDF 5.0.051
  419. $this->mpdf_ref->gradients[$n]['type'] = 2;
  420. $this->mpdf_ref->gradients[$n]['fo'] = true;
  421. $this->mpdf_ref->gradients[$n]['extend']=array('true','true');
  422. if ($trans) {
  423. $this->mpdf_ref->gradients[$n]['trans'] = true;
  424. $return .= ' /TGS'.($n).' gs ';
  425. }
  426. $return .= ' /Sh'.($n).' sh ';
  427. $return .= " Q\n";
  428. }
  429. else if ($gradient_info['type'] == 'radial'){
  430. // mPDF 4.4.003
  431. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  432. if ($w > $h) { $h = $w; }
  433. else { $w = $h; }
  434. if (isset($gradient_info['info']['x0'])) { $gradient_info['info']['x0'] = ($gradient_info['info']['x0']-$x_offset) / $w; }
  435. if (isset($gradient_info['info']['y0'])) { $gradient_info['info']['y0'] = ($gradient_info['info']['y0']-$y_offset) / $h; }
  436. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  437. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  438. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['rx'] = $gradient_info['info']['r'] / $w; }
  439. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['ry'] = $gradient_info['info']['r'] / $h; }
  440. }
  441. if ($gradient_info['info']['x0'] || $gradient_info['info']['x0']===0) { $x0 = $gradient_info['info']['x0']; }
  442. else { $x0 = 0.5; }
  443. if ($gradient_info['info']['y0'] || $gradient_info['info']['y0']===0) { $y0 = $gradient_info['info']['y0']; }
  444. else { $y0 = 0.5; }
  445. if ($gradient_info['info']['rx'] || $gradient_info['info']['rx']===0) { $rx = $gradient_info['info']['rx']; }
  446. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $rx = $gradient_info['info']['r']; }
  447. else { $rx = 0.5; }
  448. if ($gradient_info['info']['ry'] || $gradient_info['info']['ry']===0) { $ry = $gradient_info['info']['ry']; }
  449. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $ry = $gradient_info['info']['r']; }
  450. else { $ry = 0.5; }
  451. if ($gradient_info['info']['x1'] || $gradient_info['info']['x1']===0) { $x1 = $gradient_info['info']['x1']; }
  452. else { $x1 = $x0; }
  453. if ($gradient_info['info']['y1'] || $gradient_info['info']['y1']===0) { $y1 = $gradient_info['info']['y1']; }
  454. else { $y1 = $y0; }
  455. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  456. if (stristr($x0, '%')!== false) { $x0 = ($x0+0)/100; }
  457. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  458. if (stristr($y0, '%')!== false) { $y0 = ($y0+0)/100; }
  459. if (stristr($rx, '%')!== false) { $rx = ($rx+0)/100; }
  460. if (stristr($ry, '%')!== false) { $ry = ($ry+0)/100; }
  461. // mPDF 5.0.043
  462. $bboxw = $w;
  463. $bboxh = $h;
  464. $usex = $x_offset;
  465. $usey = $y_offset;
  466. $usew = $bboxw;
  467. $useh = $bboxh;
  468. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  469. $angle = rad2deg(atan2(($gradient_info['info']['y0']-$gradient_info['info']['y1']), ($gradient_info['info']['x0']-$gradient_info['info']['x1'])));
  470. if ($angle < 0) { $angle += 360; }
  471. else if ($angle > 360) { $angle -= 360; }
  472. if ($angle!=0 && $angle!=360 && $angle!=90 && $angle!=180 && $angle!=270) {
  473. if ($w >= $h) {
  474. $y1 *= $h/$w ;
  475. $y0 *= $h/$w ;
  476. $rx *= $h/$w ;
  477. $ry *= $h/$w ;
  478. $usew = $useh = $bboxw;
  479. }
  480. else {
  481. $x1 *= $w/$h ;
  482. $x0 *= $w/$h ;
  483. $rx *= $w/$h ;
  484. $ry *= $w/$h ;
  485. $usew = $useh = $bboxh;
  486. }
  487. }
  488. }
  489. $a = $usew; // width
  490. $d = -$useh; // height
  491. $e = $usex; // x- offset
  492. $f = -$usey; // -y-offset
  493. $r = $rx;
  494. $return .= sprintf('%.3F 0 0 %.3F %.3F %.3F cm ', $a*$this->kp, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  495. // mPDF 5.0.039
  496. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='objectboundingbox') {
  497. if ($transformations) { $return .= $transformations; }
  498. }
  499. // x1 and y1 (fx, fy) should be inside the circle defined by x0 y0 and r else error in mPDF
  500. while (pow(($x1-$x0),2) + pow(($y1 - $y0),2) >= pow($r,2)) { $r += 0.05; }
  501. // mPDF 5.0.040
  502. if ($spread=='R' || $spread=='F') { // Repeat / Reflect
  503. $offs = array();
  504. for($i=0;$i<$ns;$i++) {
  505. $offs[$i] = $gradient_info['color'][$i]['offset'];
  506. }
  507. $gp = 0;
  508. $inside=true;
  509. while($inside) {
  510. $gp++;
  511. for($i=0;$i<$ns;$i++) {
  512. if ($spread=='F' && ($gp % 2) == 1) { // Reflect
  513. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][(($ns*($gp-1))+($ns-$i-1))];
  514. $tmp = $gp+(1-$offs[($ns-$i-1)]) ;
  515. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  516. }
  517. else { // Reflect
  518. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][$i];
  519. $tmp = $gp+$offs[$i] ;
  520. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  521. }
  522. // IF STILL INSIDE BOX OR STILL VALID
  523. // TEST IF circle (perimeter) intersects with
  524. // or is enclosed
  525. // Point on axis to test
  526. $px = $x1 + ($x0-$x1)*$tmp;
  527. $py = $y1 + ($y0-$y1)*$tmp;
  528. $pr = $r*$tmp;
  529. $res = _testIntersectCircle($px, $py, $pr);
  530. if (!$res) { $inside = false; }
  531. }
  532. }
  533. }
  534. // mPDF 4.4.007 Gradient STOPs
  535. $stops = count($gradient_info['color']);
  536. if ($stops < 2) { return ''; }
  537. // mPDF 5.0.043
  538. $range = $gradient_info['color'][count($gradient_info['color'])-1]['offset']-$gradient_info['color'][0]['offset'];
  539. $min = $gradient_info['color'][0]['offset'];
  540. for ($i=0; $i<($stops); $i++) {
  541. // mPDF 5.0.051
  542. if (!$gradient_info['color'][$i]['color']) {
  543. if ($gradient_info['colorspace']=='RGB') $gradient_info['color'][$i]['color'] = '0 0 0';
  544. else if ($gradient_info['colorspace']=='Gray') $gradient_info['color'][$i]['color'] = '0';
  545. else if ($gradient_info['colorspace']=='CMYK') $gradient_info['color'][$i]['color'] = '1 1 1 1';
  546. }
  547. $offset = ($gradient_info['color'][$i]['offset'] - $min)/$range;
  548. $this->mpdf_ref->gradients[$n]['stops'][] = array(
  549. 'col' => $gradient_info['color'][$i]['color'],
  550. 'opacity' => $gradient_info['color'][$i]['opacity'],
  551. 'offset' => $offset);
  552. if ($gradient_info['color'][$i]['opacity']<1) { $trans = true; }
  553. }
  554. $grx1 = $x1 + ($x0-$x1)*$gradient_info['color'][0]['offset'];
  555. $gry1 = $y1 + ($y0-$y1)*$gradient_info['color'][0]['offset'];
  556. $grx2 = $x1 + ($x0-$x1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  557. $gry2 = $y1 + ($y0-$y1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  558. $grir = $r*$gradient_info['color'][0]['offset'];
  559. $grr = $r*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  560. $this->mpdf_ref->gradients[$n]['coords']=array($grx1, $gry1, $grx2, $gry2, abs($grr), abs($grir) );
  561. $grx1 = $x1 + ($x0-$x1)*$first_stop;
  562. $gry1 = $y1 + ($y0-$y1)*$first_stop;
  563. $grx2 = $x1 + ($x0-$x1)*$last_stop;
  564. $gry2 = $y1 + ($y0-$y1)*$last_stop;
  565. $grir = $r*$first_stop;
  566. $grr = $r*$last_stop;
  567. $this->mpdf_ref->gradients[$n]['colorspace'] = $gradient_info['colorspace']; // mPDF 5.0.051
  568. $this->mpdf_ref->gradients[$n]['type'] = 3;
  569. $this->mpdf_ref->gradients[$n]['fo'] = true;
  570. $this->mpdf_ref->gradients[$n]['extend']=array('true','true');
  571. if ($trans) {
  572. $this->mpdf_ref->gradients[$n]['trans'] = true;
  573. $return .= ' /TGS'.($n).' gs ';
  574. }
  575. $return .= ' /Sh'.($n).' sh ';
  576. $return .= " Q\n";
  577. }
  578. return $return;
  579. }
  580. function svgOffset ($attribs){
  581. // save all <svg> tag attributes
  582. $this->svg_attribs = $attribs;
  583. if(isset($this->svg_attribs['viewBox'])) {
  584. $vb = preg_split('/\s+/is', trim($this->svg_attribs['viewBox']));
  585. if (count($vb)==4) {
  586. $this->svg_info['x'] = $vb[0];
  587. $this->svg_info['y'] = $vb[1];
  588. $this->svg_info['w'] = $vb[2];
  589. $this->svg_info['h'] = $vb[3];
  590. // return; // mPDF 5.0.005
  591. }
  592. }
  593. $svg_w = $this->mpdf_ref->ConvertSize($attribs['width']); // mm (interprets numbers as pixels)
  594. $svg_h = $this->mpdf_ref->ConvertSize($attribs['height']); // mm
  595. ///*
  596. // mPDF 5.0.005
  597. if ($this->svg_info['w']) { // if 'w' set by viewBox
  598. if ($svg_w) { // if width also set, use these values to determine to set size of "pixel"
  599. $this->kp *= ($svg_w/0.2645) / $this->svg_info['w'];
  600. $this->kf = ($svg_w/0.2645) / $this->svg_info['w']; // mPDF 5.0.039
  601. }
  602. else if ($svg_h) {
  603. $this->kp *= ($svg_h/0.2645) / $this->svg_info['h'];
  604. $this->kf = ($svg_h/0.2645) / $this->svg_info['h']; // mPDF 5.0.039
  605. }
  606. return;
  607. }
  608. //*/
  609. // Added to handle file without height or width specified
  610. if (!$svg_w && !$svg_h) { $svg_w = $svg_h = $this->mpdf_ref->blk[$this->mpdf_ref->blklvl]['inner_width'] ; } // DEFAULT
  611. if (!$svg_w) { $svg_w = $svg_h; }
  612. if (!$svg_h) { $svg_h = $svg_w; }
  613. $this->svg_info['x'] = 0;
  614. $this->svg_info['y'] = 0;
  615. $this->svg_info['w'] = $svg_w/0.2645; // mm->pixels
  616. $this->svg_info['h'] = $svg_h/0.2645; // mm->pixels
  617. }
  618. //
  619. // check if points are within svg, if not, set to max
  620. function svg_overflow($x,$y)
  621. {
  622. $x2 = $x;
  623. $y2 = $y;
  624. if(isset($this->svg_attribs['overflow']))
  625. {
  626. if($this->svg_attribs['overflow'] == 'hidden')
  627. {
  628. // Not sure if this is supposed to strip off units, but since I dont use any I will omlt this step
  629. $svg_w = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['width']);
  630. $svg_h = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['height']);
  631. // $xmax = floor($this->svg_attribs['width']);
  632. $xmax = floor($svg_w);
  633. $xmin = 0;
  634. // $ymax = floor(($this->svg_attribs['height'] * -1));
  635. $ymax = floor(($svg_h * -1));
  636. $ymin = 0;
  637. if($x > $xmax) $x2 = $xmax; // right edge
  638. if($x < $xmin) $x2 = $xmin; // left edge
  639. if($y < $ymax) $y2 = $ymax; // bottom
  640. if($y > $ymin) $y2 = $ymin; // top
  641. }
  642. }
  643. return array( 'x' => $x2, 'y' => $y2);
  644. }
  645. function svgDefineStyle($critere_style){
  646. $tmp = count($this->svg_style)-1;
  647. $current_style = $this->svg_style[$tmp];
  648. unset($current_style['transformations']);
  649. // TRANSFORM SCALE
  650. $transformations = '';
  651. if (isset($critere_style['transform'])){
  652. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$critere_style['transform'],$m);
  653. if (count($m[0])) {
  654. for($i=0; $i<count($m[0]); $i++) {
  655. $c = strtolower($m[1][$i]);
  656. $v = trim($m[2][$i]);
  657. $vv = preg_split('/[ ,]+/',$v);
  658. if ($c=='matrix' && count($vv)==6) {
  659. // mPDF 5.0.039
  660. // Note angle of rotation is reversed (from SVG to PDF), so vv[1] and vv[2] are negated
  661. $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);
  662. /*
  663. // The long way of doing this??
  664. // need to reverse angle of rotation from SVG to PDF
  665. $sx=sqrt(pow($vv[0],2)+pow($vv[2],2));
  666. if ($vv[0] < 0) { $sx *= -1; } // change sign
  667. $sy=sqrt(pow($vv[1],2)+pow($vv[3],2));
  668. if ($vv[3] < 0) { $sy *= -1; } // change sign
  669. // rotation angle is
  670. $t=atan2($vv[1],$vv[3]);
  671. $t=atan2(-$vv[2],$vv[0]); // Should be the same value or skew has been applied
  672. // Reverse angle
  673. $t *= -1;
  674. // Rebuild matrix
  675. $ma = $sx * cos($t);
  676. $mb = $sy * sin($t);
  677. $mc = -$sx * sin($t);
  678. $md = $sy * cos($t);
  679. // $transformations .= sprintf(' %.3F %.3F %.3F %.3F %.3F %.3F cm ', $ma, $mb, $mc, $md, $vv[4]*$this->kp, -$vv[5]*$this->kp);
  680. */
  681. }
  682. else if ($c=='translate' && count($vv)) {
  683. $tm[4] = $vv[0];
  684. if (count($vv)==2) { $t_y = -$vv[1]; }
  685. else { $t_y = 0; }
  686. $tm[5] = $t_y;
  687. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  688. }
  689. else if ($c=='scale' && count($vv)) {
  690. if (count($vv)==2) { $s_y = $vv[1]; }
  691. else { $s_y = $vv[0]; }
  692. $tm[0] = $vv[0];
  693. $tm[3] = $s_y;
  694. $transformations .= sprintf(' %.3F 0 0 %.3F 0 0 cm ', $tm[0], $tm[3]);
  695. }
  696. else if ($c=='rotate' && count($vv)) {
  697. $tm[0] = cos(deg2rad(-$vv[0]));
  698. $tm[1] = sin(deg2rad(-$vv[0]));
  699. $tm[2] = -$tm[1];
  700. $tm[3] = $tm[0];
  701. if (count($vv)==3) {
  702. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  703. }
  704. $transformations .= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  705. if (count($vv)==3) {
  706. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  707. }
  708. }
  709. else if ($c=='skewx' && count($vv)) {
  710. $tm[2] = tan(deg2rad(-$vv[0]));
  711. $transformations .= sprintf(' 1 0 %.3F 1 0 0 cm ', $tm[2]);
  712. }
  713. else if ($c=='skewy' && count($vv)) {
  714. $tm[1] = tan(deg2rad(-$vv[0]));
  715. $transformations .= sprintf(' 1 %.3F 0 1 0 0 cm ', $tm[1]);
  716. }
  717. }
  718. }
  719. $current_style['transformations'] = $transformations;
  720. }
  721. if (isset($critere_style['style'])){
  722. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  723. $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);
  724. }
  725. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']); // mPDF 4.4.003
  726. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  727. }
  728. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  729. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  730. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  731. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  732. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  733. $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);
  734. }
  735. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  736. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  737. }
  738. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  739. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  740. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  741. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  742. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  743. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  744. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  745. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  746. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  747. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  748. // mPDF 4.4.003
  749. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  750. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  751. // mPDF 4.4.003
  752. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  753. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  754. }
  755. if(isset($critere_style['fill'])){
  756. $current_style['fill'] = $critere_style['fill'];
  757. }
  758. if(isset($critere_style['fill-opacity'])){
  759. $current_style['fill-opacity'] = $critere_style['fill-opacity'];
  760. }
  761. if(isset($critere_style['fill-rule'])){
  762. $current_style['fill-rule'] = $critere_style['fill-rule'];
  763. }
  764. if(isset($critere_style['stroke'])){
  765. $current_style['stroke'] = $critere_style['stroke'];
  766. }
  767. if(isset($critere_style['stroke-linecap'])){
  768. $current_style['stroke-linecap'] = $critere_style['stroke-linecap'];
  769. }
  770. if(isset($critere_style['stroke-linejoin'])){
  771. $current_style['stroke-linejoin'] = $critere_style['stroke-linejoin'];
  772. }
  773. if(isset($critere_style['stroke-miterlimit'])){
  774. $current_style['stroke-miterlimit'] = $critere_style['stroke-miterlimit'];
  775. }
  776. if(isset($critere_style['stroke-opacity'])){
  777. $current_style['stroke-opacity'] = $critere_style['stroke-opacity'];
  778. }
  779. if(isset($critere_style['stroke-width'])){
  780. $current_style['stroke-width'] = $critere_style['stroke-width'];
  781. }
  782. // mPDF 4.4.003
  783. if(isset($critere_style['stroke-dasharray'])){
  784. $current_style['stroke-dasharray'] = $critere_style['stroke-dasharray'];
  785. }
  786. if(isset($critere_style['stroke-dashoffset'])){
  787. $current_style['stroke-dashoffset'] = $critere_style['stroke-dashoffset'];
  788. }
  789. // mPDF 4.4.005 Used as indirect setting for currentColor
  790. if(isset($critere_style['color']) && $critere_style['color'] != 'inherit'){
  791. $current_style['color'] = $critere_style['color'];
  792. }
  793. return $current_style;
  794. }
  795. //
  796. // Cette fonction ecrit le style dans le stream svg.
  797. function svgStyle($critere_style, $attribs, $element){
  798. $path_style = '';
  799. if (substr_count($critere_style['fill'],'url')>0){
  800. //
  801. // couleur degradé
  802. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['fill']);
  803. if ($id_gradient != $critere_style['fill']) {
  804. if (isset($this->svg_gradient[$id_gradient])) {
  805. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  806. if ($fill_gradient) { // mPDF 4.4.003
  807. $path_style = "q ";
  808. $w = "W";
  809. $style .= 'N';
  810. }
  811. }
  812. }
  813. }
  814. // mPDF 4.4.005 Used as indirect setting for currentColor
  815. else if (strtolower($critere_style['fill']) == 'currentcolor'){
  816. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  817. if ($col) {
  818. // mPDF 5.0.051
  819. // mPDF 5.3.74
  820. if ($col{0}==5) { $critere_style['fill-opacity'] = ord($col{4}/100); } // RGBa
  821. if ($col{0}==6) { $critere_style['fill-opacity'] = ord($col{5}/100); } // CMYKa
  822. $path_style .= $this->mpdf_ref->SetFColor($col, true).' '; // mPDF 5.0.051
  823. $style .= 'F';
  824. }
  825. }
  826. else if ($critere_style['fill'] != 'none'){
  827. $col = $this->mpdf_ref->ConvertColor($critere_style['fill']);
  828. if ($col) {
  829. // mPDF 5.0.051
  830. // mPDF 5.3.74
  831. if ($col{0}==5) { $critere_style['fill-opacity'] = ord($col{4}/100); } // RGBa
  832. if ($col{0}==6) { $critere_style['fill-opacity'] = ord($col{5}/100); } // CMYKa
  833. $path_style .= $this->mpdf_ref->SetFColor($col, true).' '; // mPDF 5.0.051
  834. $style .= 'F';
  835. }
  836. }
  837. // mPDF 5.0.040
  838. if (substr_count($critere_style['stroke'],'url')>0){
  839. /*
  840. // Cannot put a gradient on a "stroke" in PDF?
  841. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['stroke']);
  842. if ($id_gradient != $critere_style['stroke']) {
  843. if (isset($this->svg_gradient[$id_gradient])) {
  844. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  845. if ($fill_gradient) {
  846. $path_style = "q ";
  847. $w = "W";
  848. $style .= 'D';
  849. }
  850. }
  851. }
  852. */
  853. }
  854. // mPDF 4.4.005 Used as indirect setting for currentColor
  855. else if (strtolower($critere_style['stroke']) == 'currentcolor'){
  856. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  857. if ($col) {
  858. // mPDF 5.0.051
  859. // mPDF 5.3.74
  860. if ($col{0}==5) { $critere_style['stroke-opacity'] = ord($col{4}/100); } // RGBa
  861. if ($col{0}==6) { $critere_style['stroke-opacity'] = ord($col{5}/100); } // CMYKa
  862. $path_style .= $this->mpdf_ref->SetDColor($col, true).' '; // mPDF 5.0.051
  863. $style .= 'D';
  864. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']);
  865. $path_style .= sprintf('%.3F w ',$lw*$this->kp);
  866. }
  867. }
  868. else if ($critere_style['stroke'] != 'none'){
  869. $col = $this->mpdf_ref->ConvertColor($critere_style['stroke']);
  870. if ($col) {
  871. // mPDF 5.0.051
  872. // mPDF 5.3.74
  873. if ($col{0}==5) { $critere_style['stroke-opacity'] = ord($col{4}/100); } // RGBa
  874. if ($col{0}==6) { $critere_style['stroke-opacity'] = ord($col{5}/100); } // CMYKa
  875. $path_style .= $this->mpdf_ref->SetDColor($col, true).' '; // mPDF 5.0.051
  876. $style .= 'D';
  877. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']); // mPDF 4.4.003
  878. $path_style .= sprintf('%.3F w ',$lw*$this->kp);
  879. }
  880. }
  881. if ($critere_style['stroke'] != 'none'){
  882. if ($critere_style['stroke-linejoin'] == 'miter'){
  883. $path_style .= ' 0 j ';
  884. }
  885. else if ($critere_style['stroke-linejoin'] == 'round'){
  886. $path_style .= ' 1 j ';
  887. }
  888. else if ($critere_style['stroke-linejoin'] == 'bevel'){
  889. $path_style .= ' 2 j ';
  890. }
  891. if ($critere_style['stroke-linecap'] == 'butt'){
  892. $path_style .= ' 0 J ';
  893. }
  894. else if ($critere_style['stroke-linecap'] == 'round'){
  895. $path_style .= ' 1 J ';
  896. }
  897. else if ($critere_style['stroke-linecap'] == 'square'){
  898. $path_style .= ' 2 J ';
  899. }
  900. if (isset($critere_style['stroke-miterlimit'])){
  901. if ($critere_style['stroke-miterlimit'] == 'none'){
  902. }
  903. else if (preg_match('/^[\d.]+$/',$critere_style['stroke-miterlimit'])) {
  904. $path_style .= sprintf('%.2F M ',$critere_style['stroke-miterlimit']);
  905. }
  906. }
  907. // mPDF 4.4.003
  908. if (isset($critere_style['stroke-dasharray'])){
  909. $off = 0;
  910. $d = preg_split('/[ ,]/',$critere_style['stroke-dasharray']);
  911. if (count($d) == 1 && $d[0]==0) {
  912. $path_style .= '[] 0 d ';
  913. }
  914. else {
  915. if (count($d) % 2 == 1) { $d = array_merge($d, $d); } // 5, 3, 1 => 5,3,1,5,3,1 OR 3 => 3,3
  916. $arr = '';
  917. for($i=0; $i<count($d); $i+=2) {
  918. $arr .= sprintf('%.3F %.3F ', $d[$i]*$this->kp, $d[$i+1]*$this->kp);
  919. }
  920. if (isset($critere_style['stroke-dashoffset'])){ $off = $critere_style['stroke-dashoffset'] + 0; }
  921. $path_style .= sprintf('[%s] %.3F d ', $arr, $off*$this->kp);
  922. }
  923. }
  924. }
  925. // mPDF 4.4.003
  926. if ($critere_style['fill-rule']=='evenodd') { $fr = '*'; }
  927. else { $fr = ''; }
  928. // mPDF 4.4.003
  929. if (isset($critere_style['fill-opacity'])) {
  930. $opacity = 1;
  931. if ($critere_style['fill-opacity'] == 0) { $opacity = 0; }
  932. else if ($critere_style['fill-opacity'] > 1) { $opacity = 1; }
  933. else if ($critere_style['fill-opacity'] > 0) { $opacity = $critere_style['fill-opacity']; }
  934. else if ($critere_style['fill-opacity'] < 0) { $opacity = 0; }
  935. $gs = $this->mpdf_ref->AddExtGState(array('ca'=>$opacity, 'BM'=>'/Normal'));
  936. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  937. $path_style .= sprintf(' /GS%d gs ', $gs);
  938. }
  939. // mPDF 4.4.003
  940. if (isset($critere_style['stroke-opacity'])) {
  941. $opacity = 1;
  942. if ($critere_style['stroke-opacity'] == 0) { $opacity = 0; }
  943. else if ($critere_style['stroke-opacity'] > 1) { $opacity = 1; }
  944. else if ($critere_style['stroke-opacity'] > 0) { $opacity = $critere_style['stroke-opacity']; }
  945. else if ($critere_style['stroke-opacity'] < 0) { $opacity = 0; }
  946. $gs = $this->mpdf_ref->AddExtGState(array('CA'=>$opacity, 'BM'=>'/Normal'));
  947. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  948. $path_style .= sprintf(' /GS%d gs ', $gs);
  949. }
  950. switch ($style){
  951. case 'F':
  952. $op = 'f';
  953. break;
  954. case 'FD':
  955. $op = 'B';
  956. break;
  957. case 'ND':
  958. $op = 'S';
  959. break;
  960. case 'D':
  961. $op = 'S';
  962. break;
  963. default:
  964. $op = 'n';
  965. }
  966. // mPDF 5.0
  967. $prestyle = $path_style.' ';
  968. $poststyle = $w.' '. $op.$fr.' '.$fill_gradient."\n";
  969. return array($prestyle,$poststyle);
  970. }
  971. //
  972. // fonction retracant les <path />
  973. function svgPath($command, $arguments){
  974. $path_cmd = '';
  975. $newsubpath = false; // mPDF 4.4.003
  976. // mPDF 5.0.039
  977. $minl = $this->pathBBox[0];
  978. $mint = $this->pathBBox[1];
  979. $maxr = $this->pathBBox[2]+$this->pathBBox[0];
  980. $maxb = $this->pathBBox[3]+$this->pathBBox[1];
  981. // mPDF 5.0.040
  982. $start = array($this->xbase, -$this->ybase);
  983. // mPDF 4.4.003
  984. preg_match_all('/[\-^]?[\d.]+(e[\-]?[\d]+){0,1}/i', $arguments, $a, PREG_SET_ORDER);
  985. // if the command is a capital letter, the coords go absolute, otherwise relative
  986. if(strtolower($command) == $command) $relative = true;
  987. else $relative = false;
  988. $ile_argumentow = count($a);
  989. // each command may have different needs for arguments [1 to 8]
  990. switch(strtolower($command)){
  991. case 'm': // move
  992. for($i = 0; $i<$ile_argumentow; $i+=2){
  993. $x = $a[$i][0];
  994. $y = $a[$i+1][0];
  995. if($relative){
  996. $pdfx = ($this->xbase + $x);
  997. $pdfy = ($this->ybase - $y);
  998. $this->xbase += $x;
  999. $this->ybase += -$y;
  1000. }
  1001. else{
  1002. $pdfx = $x;
  1003. $pdfy = -$y ;
  1004. $this->xbase = $x;
  1005. $this->ybase = -$y;
  1006. }
  1007. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1008. // mPDF 5.0.039
  1009. $minl = min($minl,$pdf_pt['x']);
  1010. $maxr = max($maxr,$pdf_pt['x']);
  1011. $mint = min($mint,-$pdf_pt['y']);
  1012. $maxb = max($maxb,-$pdf_pt['y']);
  1013. if($i == 0) $path_cmd .= sprintf('%.3F %.3F m ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1014. else $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1015. // mPDF 4.4.003 Save start points of subpath
  1016. if ($this->subPathInit) {
  1017. $this->spxstart = $this->xbase;
  1018. $this->spystart = $this->ybase;
  1019. $this->subPathInit = false;
  1020. }
  1021. }
  1022. break;
  1023. case 'l': // a simple line
  1024. for($i = 0; $i<$ile_argumentow; $i+=2){
  1025. $x = ($a[$i][0]);
  1026. $y = ($a[$i+1][0]);
  1027. if($relative){
  1028. $pdfx = ($this->xbase + $x);
  1029. $pdfy = ($this->ybase - $y);
  1030. $this->xbase += $x;
  1031. $this->ybase += -$y;
  1032. }
  1033. else{
  1034. $pdfx = $x ;
  1035. $pdfy = -$y ;
  1036. $this->xbase = $x;
  1037. $this->ybase = -$y;
  1038. }
  1039. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1040. // mPDF 5.0.039
  1041. $minl = min($minl,$pdf_pt['x']);
  1042. $maxr = max($maxr,$pdf_pt['x']);
  1043. $mint = min($mint,-$pdf_pt['y']);
  1044. $maxb = max($maxb,-$pdf_pt['y']);
  1045. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1046. }
  1047. break;
  1048. case 'h': // a very simple horizontal line
  1049. for($i = 0; $i<$ile_argumentow; $i++){
  1050. $x = ($a[$i][0]);
  1051. if($relative){
  1052. $y = 0;
  1053. $pdfx = ($this->xbase + $x) ;
  1054. $pdfy = ($this->ybase - $y) ;
  1055. $this->xbase += $x;
  1056. $this->ybase += -$y;
  1057. }
  1058. else{
  1059. $y = -$this->ybase;
  1060. $pdfx = $x;
  1061. $pdfy = -$y;
  1062. $this->xbase = $x;
  1063. $this->ybase = -$y;
  1064. }
  1065. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1066. // mPDF 5.0.039
  1067. $minl = min($minl,$pdf_pt['x']);
  1068. $maxr = max($maxr,$pdf_pt['x']);
  1069. $mint = min($mint,-$pdf_pt['y']);
  1070. $maxb = max($maxb,-$pdf_pt['y']);
  1071. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1072. }
  1073. break;
  1074. case 'v': // the simplest line, vertical
  1075. for($i = 0; $i<$ile_argumentow; $i++){
  1076. $y = ($a[$i][0]);
  1077. if($relative){
  1078. $x = 0;
  1079. $pdfx = ($this->xbase + $x);
  1080. $pdfy = ($this->ybase - $y);
  1081. $this->xbase += $x;
  1082. $this->ybase += -$y;
  1083. }
  1084. else{
  1085. $x = $this->xbase;
  1086. $pdfx = $x;
  1087. $pdfy = -$y;
  1088. $this->xbase = $x;
  1089. $this->ybase = -$y;
  1090. }
  1091. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1092. // mPDF 5.0.039
  1093. $minl = min($minl,$pdf_pt['x']);
  1094. $maxr = max($maxr,$pdf_pt['x']);
  1095. $mint = min($mint,-$pdf_pt['y']);
  1096. $maxb = max($maxb,-$pdf_pt['y']);
  1097. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1098. }
  1099. break;
  1100. case 's': // bezier with first vertex equal first control
  1101. // mPDF 4.4.003
  1102. if (!($this->lastcommand == 'C' || $this->lastcommand == 'c' || $this->lastcommand == 'S' || $this->lastcommand == 's')) {
  1103. $this->lastcontrolpoints = array(0,0);
  1104. }
  1105. for($i = 0; $i<$ile_argumentow; $i += 4){
  1106. $x1 = $this->lastcontrolpoints[0];
  1107. $y1 = $this->lastcontrolpoints[1];
  1108. $x2 = ($a[$i][0]);
  1109. $y2 = ($a[$i+1][0]);
  1110. $x = ($a[$i+2][0]);
  1111. $y = ($a[$i+3][0]);
  1112. if($relative){
  1113. $pdfx1 = ($this->xbase + $x1);
  1114. $pdfy1 = ($this->ybase - $y1);
  1115. $pdfx2 = ($this->xbase + $x2);
  1116. $pdfy2 = ($this->ybase - $y2);
  1117. $pdfx = ($this->xbase + $x);
  1118. $pdfy = ($this->ybase - $y);
  1119. $this->xbase += $x;
  1120. $this->ybase += -$y;
  1121. }
  1122. else{
  1123. $pdfx1 = $this->xbase + $x1;
  1124. $pdfy1 = $this->ybase -$y1;
  1125. $pdfx2 = $x2;
  1126. $pdfy2 = -$y2;
  1127. $pdfx = $x;
  1128. $pdfy = -$y;
  1129. $this->xbase = $x;
  1130. $this->ybase = -$y;
  1131. }
  1132. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1133. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1134. // mPDF 5.0.040
  1135. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1136. $bx = calc_bezier_bbox($start, $curves);
  1137. $minl = min($minl,$bx[0]);
  1138. $maxr = max($maxr,$bx[2]);
  1139. $mint = min($mint,$bx[1]);
  1140. $maxb = max($maxb,$bx[3]);
  1141. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1142. {
  1143. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1144. }
  1145. else
  1146. {
  1147. $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);
  1148. }
  1149. }
  1150. break;
  1151. case 'c': // bezier with second vertex equal second control
  1152. for($i = 0; $i<$ile_argumentow; $i += 6){
  1153. $x1 = ($a[$i][0]);
  1154. $y1 = ($a[$i+1][0]);
  1155. $x2 = ($a[$i+2][0]);
  1156. $y2 = ($a[$i+3][0]);
  1157. $x = ($a[$i+4][0]);
  1158. $y = ($a[$i+5][0]);
  1159. if($relative){
  1160. $pdfx1 = ($this->xbase + $x1);
  1161. $pdfy1 = ($this->ybase - $y1);
  1162. $pdfx2 = ($this->xbase + $x2);
  1163. $pdfy2 = ($this->ybase - $y2);
  1164. $pdfx = ($this->xbase + $x);
  1165. $pdfy = ($this->ybase - $y);
  1166. $this->xbase += $x;
  1167. $this->ybase += -$y;
  1168. }
  1169. else{
  1170. $pdfx1 = $x1;
  1171. $pdfy1 = -$y1;
  1172. $pdfx2 = $x2;
  1173. $pdfy2 = -$y2;
  1174. $pdfx = $x;
  1175. $pdfy = -$y;
  1176. $this->xbase = $x;
  1177. $this->ybase = -$y;
  1178. }
  1179. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1180. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  1181. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  1182. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1183. // mPDF 5.0.040
  1184. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1185. $bx = calc_bezier_bbox($start, $curves);
  1186. $minl = min($minl,$bx[0]);
  1187. $maxr = max($maxr,$bx[2]);
  1188. $mint = min($mint,$bx[1]);
  1189. $maxb = max($maxb,$bx[3]);
  1190. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1191. {
  1192. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1193. }
  1194. else
  1195. {
  1196. $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);
  1197. }
  1198. }
  1199. break;
  1200. case 'q': // bezier quadratic avec point de control
  1201. for($i = 0; $i<$ile_argumentow; $i += 4){
  1202. $x1 = ($a[$i][0]);
  1203. $y1 = ($a[$i+1][0]);
  1204. $x = ($a[$i+2][0]);
  1205. $y = ($a[$i+3][0]);
  1206. if($relative){
  1207. $pdfx = ($this->xbase + $x);
  1208. $pdfy = ($this->ybase - $y);
  1209. $pdfx1 = ($this->xbase + ($x1*2/3));
  1210. $pdfy1 = ($this->ybase - ($y1*2/3));
  1211. // mPDF 4.4.003
  1212. $pdfx2 = $pdfx1 + 1/3 *($x);
  1213. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  1214. $this->xbase += $x;
  1215. $this->ybase += -$y;
  1216. }
  1217. else{
  1218. $pdfx = $x;
  1219. $pdfy = -$y;
  1220. $pdfx1 = ($this->xbase+(($x1-$this->xbase)*2/3));
  1221. $pdfy1 = ($this->ybase-(($y1+$this->ybase)*2/3));
  1222. $pdfx2 = ($x+(($x1-$x)*2/3));
  1223. $pdfy2 = (-$y-(($y1-$y)*2/3));
  1224. // mPDF 4.4.003 …

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