PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 1ms

/app/vendors/mpdf/classes/svg.php

https://bitbucket.org/ducnv66/bbb-admin
PHP | 2590 lines | 1984 code | 333 blank | 273 comment | 680 complexity | 02af0b687dc61316a1780bef257ee53e 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. 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. if ($col[0]==5) { $critere_style['fill-opacity'] = $col[4]; } // RGBa
  820. if ($col[0]==6) { $critere_style['fill-opacity'] = $col[5]; } // CMYKa
  821. $path_style .= $this->mpdf_ref->SetFColor($col, true).' '; // mPDF 5.0.051
  822. $style .= 'F';
  823. }
  824. }
  825. else if ($critere_style['fill'] != 'none'){
  826. $col = $this->mpdf_ref->ConvertColor($critere_style['fill']);
  827. if ($col) {
  828. // mPDF 5.0.051
  829. if ($col[0]==5) { $critere_style['fill-opacity'] = $col[4]; } // RGBa
  830. if ($col[0]==6) { $critere_style['fill-opacity'] = $col[5]; } // CMYKa
  831. $path_style .= $this->mpdf_ref->SetFColor($col, true).' '; // mPDF 5.0.051
  832. $style .= 'F';
  833. }
  834. }
  835. // mPDF 5.0.040
  836. if (substr_count($critere_style['stroke'],'url')>0){
  837. /*
  838. // Cannot put a gradient on a "stroke" in PDF?
  839. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['stroke']);
  840. if ($id_gradient != $critere_style['stroke']) {
  841. if (isset($this->svg_gradient[$id_gradient])) {
  842. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  843. if ($fill_gradient) {
  844. $path_style = "q ";
  845. $w = "W";
  846. $style .= 'D';
  847. }
  848. }
  849. }
  850. */
  851. }
  852. // mPDF 4.4.005 Used as indirect setting for currentColor
  853. else if (strtolower($critere_style['stroke']) == 'currentcolor'){
  854. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  855. if ($col) {
  856. // mPDF 5.0.051
  857. if ($col[0]==5) { $critere_style['stroke-opacity'] = $col[4]; } // RGBa
  858. if ($col[0]==6) { $critere_style['stroke-opacity'] = $col[5]; } // CMYKa
  859. $path_style .= $this->mpdf_ref->SetDColor($col, true).' '; // mPDF 5.0.051
  860. $style .= 'D';
  861. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']);
  862. $path_style .= sprintf('%.3f w ',$lw*$this->kp);
  863. }
  864. }
  865. else if ($critere_style['stroke'] != 'none'){
  866. $col = $this->mpdf_ref->ConvertColor($critere_style['stroke']);
  867. if ($col) {
  868. // mPDF 5.0.051
  869. if ($col[0]==5) { $critere_style['stroke-opacity'] = $col[4]; } // RGBa
  870. if ($col[0]==6) { $critere_style['stroke-opacity'] = $col[5]; } // CMYKa
  871. $path_style .= $this->mpdf_ref->SetDColor($col, true).' '; // mPDF 5.0.051
  872. $style .= 'D';
  873. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']); // mPDF 4.4.003
  874. $path_style .= sprintf('%.3f w ',$lw*$this->kp);
  875. }
  876. }
  877. if ($critere_style['stroke'] != 'none'){
  878. if ($critere_style['stroke-linejoin'] == 'miter'){
  879. $path_style .= ' 0 j ';
  880. }
  881. else if ($critere_style['stroke-linejoin'] == 'round'){
  882. $path_style .= ' 1 j ';
  883. }
  884. else if ($critere_style['stroke-linejoin'] == 'bevel'){
  885. $path_style .= ' 2 j ';
  886. }
  887. if ($critere_style['stroke-linecap'] == 'butt'){
  888. $path_style .= ' 0 J ';
  889. }
  890. else if ($critere_style['stroke-linecap'] == 'round'){
  891. $path_style .= ' 1 J ';
  892. }
  893. else if ($critere_style['stroke-linecap'] == 'square'){
  894. $path_style .= ' 2 J ';
  895. }
  896. if (isset($critere_style['stroke-miterlimit'])){
  897. if ($critere_style['stroke-miterlimit'] == 'none'){
  898. }
  899. else if (preg_match('/^[\d.]+$/',$critere_style['stroke-miterlimit'])) {
  900. $path_style .= sprintf('%.2f M ',$critere_style['stroke-miterlimit']);
  901. }
  902. }
  903. // mPDF 4.4.003
  904. if (isset($critere_style['stroke-dasharray'])){
  905. $off = 0;
  906. $d = preg_split('/[ ,]/',$critere_style['stroke-dasharray']);
  907. if (count($d) == 1 && $d[0]==0) {
  908. $path_style .= '[] 0 d ';
  909. }
  910. else {
  911. if (count($d) % 2 == 1) { $d = array_merge($d, $d); } // 5, 3, 1 => 5,3,1,5,3,1 OR 3 => 3,3
  912. $arr = '';
  913. for($i=0; $i<count($d); $i+=2) {
  914. $arr .= sprintf('%.3f %.3f ', $d[$i]*$this->kp, $d[$i+1]*$this->kp);
  915. }
  916. if (isset($critere_style['stroke-dashoffset'])){ $off = $critere_style['stroke-dashoffset'] + 0; }
  917. $path_style .= sprintf('[%s] %.3f d ', $arr, $off*$this->kp);
  918. }
  919. }
  920. }
  921. // mPDF 4.4.003
  922. if ($critere_style['fill-rule']=='evenodd') { $fr = '*'; }
  923. else { $fr = ''; }
  924. // mPDF 4.4.003
  925. if (isset($critere_style['fill-opacity'])) {
  926. $opacity = 1;
  927. if ($critere_style['fill-opacity'] == 0) { $opacity = 0; }
  928. else if ($critere_style['fill-opacity'] > 1) { $opacity = 1; }
  929. else if ($critere_style['fill-opacity'] > 0) { $opacity = $critere_style['fill-opacity']; }
  930. else if ($critere_style['fill-opacity'] < 0) { $opacity = 0; }
  931. $gs = $this->mpdf_ref->AddExtGState(array('ca'=>$opacity, 'BM'=>'/Normal'));
  932. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  933. $path_style .= sprintf(' /GS%d gs ', $gs);
  934. }
  935. // mPDF 4.4.003
  936. if (isset($critere_style['stroke-opacity'])) {
  937. $opacity = 1;
  938. if ($critere_style['stroke-opacity'] == 0) { $opacity = 0; }
  939. else if ($critere_style['stroke-opacity'] > 1) { $opacity = 1; }
  940. else if ($critere_style['stroke-opacity'] > 0) { $opacity = $critere_style['stroke-opacity']; }
  941. else if ($critere_style['stroke-opacity'] < 0) { $opacity = 0; }
  942. $gs = $this->mpdf_ref->AddExtGState(array('CA'=>$opacity, 'BM'=>'/Normal'));
  943. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  944. $path_style .= sprintf(' /GS%d gs ', $gs);
  945. }
  946. switch ($style){
  947. case 'F':
  948. $op = 'f';
  949. break;
  950. case 'FD':
  951. $op = 'B';
  952. break;
  953. case 'ND':
  954. $op = 'S';
  955. break;
  956. case 'D':
  957. $op = 'S';
  958. break;
  959. default:
  960. $op = 'n';
  961. }
  962. // mPDF 5.0
  963. $prestyle = $path_style.' ';
  964. $poststyle = $w.' '. $op.$fr.' '.$fill_gradient."\n";
  965. return array($prestyle,$poststyle);
  966. }
  967. //
  968. // fonction retracant les <path />
  969. function svgPath($command, $arguments){
  970. $path_cmd = '';
  971. $newsubpath = false; // mPDF 4.4.003
  972. // mPDF 5.0.039
  973. $minl = $this->pathBBox[0];
  974. $mint = $this->pathBBox[1];
  975. $maxr = $this->pathBBox[2]+$this->pathBBox[0];
  976. $maxb = $this->pathBBox[3]+$this->pathBBox[1];
  977. // mPDF 5.0.040
  978. $start = array($this->xbase, -$this->ybase);
  979. // mPDF 4.4.003
  980. preg_match_all('/[\-^]?[\d.]+(e[\-]?[\d]+){0,1}/i', $arguments, $a, PREG_SET_ORDER);
  981. // if the command is a capital letter, the coords go absolute, otherwise relative
  982. if(strtolower($command) == $command) $relative = true;
  983. else $relative = false;
  984. $ile_argumentow = count($a);
  985. // each command may have different needs for arguments [1 to 8]
  986. switch(strtolower($command)){
  987. case 'm': // move
  988. for($i = 0; $i<$ile_argumentow; $i+=2){
  989. $x = $a[$i][0];
  990. $y = $a[$i+1][0];
  991. if($relative){
  992. $pdfx = ($this->xbase + $x);
  993. $pdfy = ($this->ybase - $y);
  994. $this->xbase += $x;
  995. $this->ybase += -$y;
  996. }
  997. else{
  998. $pdfx = $x;
  999. $pdfy = -$y ;
  1000. $this->xbase = $x;
  1001. $this->ybase = -$y;
  1002. }
  1003. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1004. // mPDF 5.0.039
  1005. $minl = min($minl,$pdf_pt['x']);
  1006. $maxr = max($maxr,$pdf_pt['x']);
  1007. $mint = min($mint,-$pdf_pt['y']);
  1008. $maxb = max($maxb,-$pdf_pt['y']);
  1009. if($i == 0) $path_cmd .= sprintf('%.3f %.3f m ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1010. else $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1011. // mPDF 4.4.003 Save start points of subpath
  1012. if ($this->subPathInit) {
  1013. $this->spxstart = $this->xbase;
  1014. $this->spystart = $this->ybase;
  1015. $this->subPathInit = false;
  1016. }
  1017. }
  1018. break;
  1019. case 'l': // a simple line
  1020. for($i = 0; $i<$ile_argumentow; $i+=2){
  1021. $x = ($a[$i][0]);
  1022. $y = ($a[$i+1][0]);
  1023. if($relative){
  1024. $pdfx = ($this->xbase + $x);
  1025. $pdfy = ($this->ybase - $y);
  1026. $this->xbase += $x;
  1027. $this->ybase += -$y;
  1028. }
  1029. else{
  1030. $pdfx = $x ;
  1031. $pdfy = -$y ;
  1032. $this->xbase = $x;
  1033. $this->ybase = -$y;
  1034. }
  1035. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1036. // mPDF 5.0.039
  1037. $minl = min($minl,$pdf_pt['x']);
  1038. $maxr = max($maxr,$pdf_pt['x']);
  1039. $mint = min($mint,-$pdf_pt['y']);
  1040. $maxb = max($maxb,-$pdf_pt['y']);
  1041. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1042. }
  1043. break;
  1044. case 'h': // a very simple horizontal line
  1045. for($i = 0; $i<$ile_argumentow; $i++){
  1046. $x = ($a[$i][0]);
  1047. if($relative){
  1048. $y = 0;
  1049. $pdfx = ($this->xbase + $x) ;
  1050. $pdfy = ($this->ybase - $y) ;
  1051. $this->xbase += $x;
  1052. $this->ybase += -$y;
  1053. }
  1054. else{
  1055. $y = -$this->ybase;
  1056. $pdfx = $x;
  1057. $pdfy = -$y;
  1058. $this->xbase = $x;
  1059. $this->ybase = -$y;
  1060. }
  1061. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1062. // mPDF 5.0.039
  1063. $minl = min($minl,$pdf_pt['x']);
  1064. $maxr = max($maxr,$pdf_pt['x']);
  1065. $mint = min($mint,-$pdf_pt['y']);
  1066. $maxb = max($maxb,-$pdf_pt['y']);
  1067. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1068. }
  1069. break;
  1070. case 'v': // the simplest line, vertical
  1071. for($i = 0; $i<$ile_argumentow; $i++){
  1072. $y = ($a[$i][0]);
  1073. if($relative){
  1074. $x = 0;
  1075. $pdfx = ($this->xbase + $x);
  1076. $pdfy = ($this->ybase - $y);
  1077. $this->xbase += $x;
  1078. $this->ybase += -$y;
  1079. }
  1080. else{
  1081. $x = $this->xbase;
  1082. $pdfx = $x;
  1083. $pdfy = -$y;
  1084. $this->xbase = $x;
  1085. $this->ybase = -$y;
  1086. }
  1087. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1088. // mPDF 5.0.039
  1089. $minl = min($minl,$pdf_pt['x']);
  1090. $maxr = max($maxr,$pdf_pt['x']);
  1091. $mint = min($mint,-$pdf_pt['y']);
  1092. $maxb = max($maxb,-$pdf_pt['y']);
  1093. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1094. }
  1095. break;
  1096. case 's': // bezier with first vertex equal first control
  1097. // mPDF 4.4.003
  1098. if (!($this->lastcommand == 'C' || $this->lastcommand == 'c' || $this->lastcommand == 'S' || $this->lastcommand == 's')) {
  1099. $this->lastcontrolpoints = array(0,0);
  1100. }
  1101. for($i = 0; $i<$ile_argumentow; $i += 4){
  1102. $x1 = $this->lastcontrolpoints[0];
  1103. $y1 = $this->lastcontrolpoints[1];
  1104. $x2 = ($a[$i][0]);
  1105. $y2 = ($a[$i+1][0]);
  1106. $x = ($a[$i+2][0]);
  1107. $y = ($a[$i+3][0]);
  1108. if($relative){
  1109. $pdfx1 = ($this->xbase + $x1);
  1110. $pdfy1 = ($this->ybase - $y1);
  1111. $pdfx2 = ($this->xbase + $x2);
  1112. $pdfy2 = ($this->ybase - $y2);
  1113. $pdfx = ($this->xbase + $x);
  1114. $pdfy = ($this->ybase - $y);
  1115. $this->xbase += $x;
  1116. $this->ybase += -$y;
  1117. }
  1118. else{
  1119. $pdfx1 = $this->xbase + $x1;
  1120. $pdfy1 = $this->ybase -$y1;
  1121. $pdfx2 = $x2;
  1122. $pdfy2 = -$y2;
  1123. $pdfx = $x;
  1124. $pdfy = -$y;
  1125. $this->xbase = $x;
  1126. $this->ybase = -$y;
  1127. }
  1128. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1129. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1130. // mPDF 5.0.040
  1131. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1132. $bx = calc_bezier_bbox($start, $curves);
  1133. $minl = min($minl,$bx[0]);
  1134. $maxr = max($maxr,$bx[2]);
  1135. $mint = min($mint,$bx[1]);
  1136. $maxb = max($maxb,$bx[3]);
  1137. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1138. {
  1139. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1140. }
  1141. else
  1142. {
  1143. $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);
  1144. }
  1145. }
  1146. break;
  1147. case 'c': // bezier with second vertex equal second control
  1148. for($i = 0; $i<$ile_argumentow; $i += 6){
  1149. $x1 = ($a[$i][0]);
  1150. $y1 = ($a[$i+1][0]);
  1151. $x2 = ($a[$i+2][0]);
  1152. $y2 = ($a[$i+3][0]);
  1153. $x = ($a[$i+4][0]);
  1154. $y = ($a[$i+5][0]);
  1155. if($relative){
  1156. $pdfx1 = ($this->xbase + $x1);
  1157. $pdfy1 = ($this->ybase - $y1);
  1158. $pdfx2 = ($this->xbase + $x2);
  1159. $pdfy2 = ($this->ybase - $y2);
  1160. $pdfx = ($this->xbase + $x);
  1161. $pdfy = ($this->ybase - $y);
  1162. $this->xbase += $x;
  1163. $this->ybase += -$y;
  1164. }
  1165. else{
  1166. $pdfx1 = $x1;
  1167. $pdfy1 = -$y1;
  1168. $pdfx2 = $x2;
  1169. $pdfy2 = -$y2;
  1170. $pdfx = $x;
  1171. $pdfy = -$y;
  1172. $this->xbase = $x;
  1173. $this->ybase = -$y;
  1174. }
  1175. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1176. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  1177. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  1178. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1179. // mPDF 5.0.040
  1180. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1181. $bx = calc_bezier_bbox($start, $curves);
  1182. $minl = min($minl,$bx[0]);
  1183. $maxr = max($maxr,$bx[2]);
  1184. $mint = min($mint,$bx[1]);
  1185. $maxb = max($maxb,$bx[3]);
  1186. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1187. {
  1188. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1189. }
  1190. else
  1191. {
  1192. $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);
  1193. }
  1194. }
  1195. break;
  1196. case 'q': // bezier quadratic avec point de control
  1197. for($i = 0; $i<$ile_argumentow; $i += 4){
  1198. $x1 = ($a[$i][0]);
  1199. $y1 = ($a[$i+1][0]);
  1200. $x = ($a[$i+2][0]);
  1201. $y = ($a[$i+3][0]);
  1202. if($relative){
  1203. $pdfx = ($this->xbase + $x);
  1204. $pdfy = ($this->ybase - $y);
  1205. $pdfx1 = ($this->xbase + ($x1*2/3));
  1206. $pdfy1 = ($this->ybase - ($y1*2/3));
  1207. // mPDF 4.4.003
  1208. $pdfx2 = $pdfx1 + 1/3 *($x);
  1209. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  1210. $this->xbase += $x;
  1211. $this->ybase += -$y;
  1212. }
  1213. else{
  1214. $pdfx = $x;
  1215. $pdfy = -$y;
  1216. $pdfx1 = ($this->xbase+(($x1-$this->xbase)*2/3));
  1217. $pdfy1 = ($this->ybase-(($y1+$this->ybase)*2/3));
  1218. $pdfx2 = ($x+(($x1-$x)*2/3));
  1219. $pdfy2 = (-$y-(($y1-$y)*2/3));
  1220. // mPDF 4.4.003
  1221. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  1222. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  1223. $this->xbase = $x;
  1224. $th

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