PageRenderTime 122ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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. $this->ybase = -$y;
  1225. }
  1226. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1227. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1228. // mPDF 5.0.040
  1229. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1230. $bx = calc_bezier_bbox($start, $curves);
  1231. $minl = min($minl,$bx[0]);
  1232. $maxr = max($maxr,$bx[2]);
  1233. $mint = min($mint,$bx[1]);
  1234. $maxb = max($maxb,$bx[3]);
  1235. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1236. {
  1237. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1238. }
  1239. else
  1240. {
  1241. $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);
  1242. }
  1243. }
  1244. break;
  1245. case 't': // bezier quadratic avec point de control simetrique a lancien point de control
  1246. // mPDF 4.4.003
  1247. if (!($this->lastcommand == 'Q' || $this->lastcommand == 'q' || $this->lastcommand == 'T' || $this->lastcommand == 't')) {
  1248. $this->lastcontrolpoints = array(0,0);
  1249. }
  1250. for($i = 0; $i<$ile_argumentow; $i += 2){
  1251. $x = ($a[$i][0]);
  1252. $y = ($a[$i+1][0]);
  1253. $x1 = $this->lastcontrolpoints[0];
  1254. $y1 = $this->lastcontrolpoints[1];
  1255. if($relative){
  1256. $pdfx = ($this->xbase + $x);
  1257. $pdfy = ($this->ybase - $y);
  1258. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  1259. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  1260. // mPDF 4.4.003
  1261. $pdfx2 = $pdfx1 + 1/3 *($x);
  1262. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  1263. $this->xbase += $x;
  1264. $this->ybase += -$y;
  1265. }
  1266. else{
  1267. $pdfx = $x;
  1268. $pdfy = -$y;
  1269. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  1270. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  1271. // mPDF 4.4.003
  1272. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  1273. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  1274. $this->xbase = $x;
  1275. $this->ybase = -$y;
  1276. }
  1277. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1278. // mPDF 5.0.040
  1279. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1280. $bx = calc_bezier_bbox($start, $curves);
  1281. $minl = min($minl,$bx[0]);
  1282. $maxr = max($maxr,$bx[2]);
  1283. $mint = min($mint,$bx[1]);
  1284. $maxb = max($maxb,$bx[3]);
  1285. $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);
  1286. }
  1287. break;
  1288. case 'a': // Elliptical arc
  1289. for($i = 0; $i<$ile_argumentow; $i += 7){
  1290. $rx = ($a[$i][0]);
  1291. $ry = ($a[$i+1][0]);
  1292. $angle = ($a[$i+2][0]); //x-axis-rotation
  1293. $largeArcFlag = ($a[$i+3][0]);
  1294. $sweepFlag = ($a[$i+4][0]);
  1295. $x2 = ($a[$i+5][0]);
  1296. $y2 = ($a[$i+6][0]);
  1297. $x1 = $this->xbase;
  1298. $y1 = -$this->ybase;
  1299. if($relative){
  1300. $x2 = $this->xbase + $x2;
  1301. $y2 = -$this->ybase + $y2;
  1302. $this->xbase += ($a[$i+5][0]);
  1303. $this->ybase += -($a[$i+6][0]);
  1304. }
  1305. else{
  1306. $this->xbase = $x2;
  1307. $this->ybase = -$y2;
  1308. }
  1309. // mPDF 5.0.039 // mPDF 5.0.040
  1310. list($pcmd, $bounds) = $this->Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag);
  1311. $minl = min($minl,$x2,min($bounds[0]));
  1312. $maxr = max($maxr,$x2,max($bounds[0]));
  1313. $mint = min($mint,$y2,min($bounds[1]));
  1314. $maxb = max($maxb,$y2,max($bounds[1]));
  1315. $path_cmd .= $pcmd;
  1316. }
  1317. break;
  1318. case'z':
  1319. $path_cmd .= 'h ';
  1320. // mPDF 4.4.003
  1321. $this->subPathInit = true;
  1322. $newsubpath = true;
  1323. $this->xbase = $this->spxstart;
  1324. $this->ybase = $this->spystart;
  1325. break;
  1326. default:
  1327. break;
  1328. }
  1329. if (!$newsubpath) { $this->subPathInit = false; } // mPDF 4.4.003
  1330. $this->lastcommand = $command;
  1331. // mPDF 5.0.039
  1332. $this->pathBBox[0] = $minl;
  1333. $this->pathBBox[1] = $mint;
  1334. $this->pathBBox[2] = $maxr - $this->pathBBox[0];
  1335. $this->pathBBox[3] = $maxb - $this->pathBBox[1];
  1336. return $path_cmd;
  1337. }
  1338. function Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag) {
  1339. // mPDF 5.0.040
  1340. $bounds = array(0=>array($x1,$x2),1=>array($y1,$y2));
  1341. // 1. Treat out-of-range parameters as described in
  1342. // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  1343. // If the endpoints (x1, y1) and (x2, y2) are identical, then this
  1344. // is equivalent to omitting the elliptical arc segment entirely
  1345. if ($x1 == $x2 && $y1 == $y2) return array('', $bounds); // mPD 5.0.040
  1346. // If rX = 0 or rY = 0 then this arc is treated as a straight line
  1347. // segment (a "lineto") joining the endpoints.
  1348. if ($rx == 0.0 || $ry == 0.0) {
  1349. // return array(Lineto(x2, y2), $bounds); // mPD 5.0.040
  1350. }
  1351. // If rX or rY have negative signs, these are dropped; the absolute
  1352. // value is used instead.
  1353. if ($rx<0.0) $rx = -$rx;
  1354. if ($ry<0.0) $ry = -$ry;
  1355. // 2. convert to center parameterization as shown in
  1356. // http://www.w3.org/TR/SVG/implnote.html
  1357. $sinPhi = sin(deg2rad($angle));
  1358. $cosPhi = cos(deg2rad($angle));
  1359. $x1dash = $cosPhi * ($x1-$x2)/2.0 + $sinPhi * ($y1-$y2)/2.0;
  1360. $y1dash = -$sinPhi * ($x1-$x2)/2.0 + $cosPhi * ($y1-$y2)/2.0;
  1361. $numerator = $rx*$rx*$ry*$ry - $rx*$rx*$y1dash*$y1dash - $ry*$ry*$x1dash*$x1dash;
  1362. if ($numerator < 0.0) {
  1363. // If rX , rY and are such that there is no solution (basically,
  1364. // the ellipse is not big enough to reach from (x1, y1) to (x2,
  1365. // y2)) then the ellipse is scaled up uniformly until there is
  1366. // exactly one solution (until the ellipse is just big enough).
  1367. // -> find factor s, such that numerator' with rx'=s*rx and
  1368. // ry'=s*ry becomes 0 :
  1369. $s = sqrt(1.0 - $numerator/($rx*$rx*$ry*$ry));
  1370. $rx *= $s;
  1371. $ry *= $s;
  1372. $root = 0.0;
  1373. }
  1374. else {
  1375. $root = ($largeArcFlag == $sweepFlag ? -1.0 : 1.0) * sqrt( $numerator/($rx*$rx*$y1dash*$y1dash+$ry*$ry*$x1dash*$x1dash) );
  1376. }
  1377. $cxdash = $root*$rx*$y1dash/$ry;
  1378. $cydash = -$root*$ry*$x1dash/$rx;
  1379. $cx = $cosPhi * $cxdash - $sinPhi * $cydash + ($x1+$x2)/2.0;
  1380. $cy = $sinPhi * $cxdash + $cosPhi * $cydash + ($y1+$y2)/2.0;
  1381. $theta1 = $this->CalcVectorAngle(1.0, 0.0, ($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry);
  1382. $dtheta = $this->CalcVectorAngle(($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry, (-$x1dash-$cxdash)/$rx, (-$y1dash-$cydash)/$ry);
  1383. if (!$sweepFlag && $dtheta>0)
  1384. $dtheta -= 2.0*M_PI;
  1385. else if ($sweepFlag && $dtheta<0)
  1386. $dtheta += 2.0*M_PI;
  1387. // 3. convert into cubic bezier segments <= 90deg
  1388. $segments = ceil(abs($dtheta/(M_PI/2.0)));
  1389. $delta = $dtheta/$segments;
  1390. $t = 8.0/3.0 * sin($delta/4.0) * sin($delta/4.0) / sin($delta/2.0);
  1391. $coords = array();
  1392. for ($i = 0; $i < $segments; $i++) {
  1393. $cosTheta1 = cos($theta1);
  1394. $sinTheta1 = sin($theta1);
  1395. $theta2 = $theta1 + $delta;
  1396. $cosTheta2 = cos($theta2);
  1397. $sinTheta2 = sin($theta2);
  1398. // a) calculate endpoint of the segment:
  1399. $xe = $cosPhi * $rx*$cosTheta2 - $sinPhi * $ry*$sinTheta2 + $cx;
  1400. $ye = $sinPhi * $rx*$cosTheta2 + $cosPhi * $ry*$sinTheta2 + $cy;
  1401. // b) calculate gradients at start/end points of segment:
  1402. $dx1 = $t * ( - $cosPhi * $rx*$sinTheta1 - $sinPhi * $ry*$cosTheta1);
  1403. $dy1 = $t * ( - $sinPhi * $rx*$sinTheta1 + $cosPhi * $ry*$cosTheta1);
  1404. $dxe = $t * ( $cosPhi * $rx*$sinTheta2 + $sinPhi * $ry*$cosTheta2);
  1405. $dye = $t * ( $sinPhi * $rx*$sinTheta2 - $cosPhi * $ry*$cosTheta2);
  1406. // c) draw the cubic bezier:
  1407. $coords[$i] = array(($x1+$dx1), ($y1+$dy1), ($xe+$dxe), ($ye+$dye), $xe, $ye);
  1408. // do next segment
  1409. $theta1 = $theta2;
  1410. $x1 = $xe;
  1411. $y1 = $ye;
  1412. }
  1413. $path = ' ';
  1414. foreach($coords AS $c) {
  1415. $cpx1 = $c[0];
  1416. $cpy1 = $c[1];
  1417. $cpx2 = $c[2];
  1418. $cpy2 = $c[3];
  1419. $x2 = $c[4];
  1420. $y2 = $c[5];
  1421. $path .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $cpx1*$this->kp, -$cpy1*$this->kp, $cpx2*$this->kp, -$cpy2*$this->kp, $x2*$this->kp, -$y2*$this->kp) ."\n";
  1422. // mPDF 5.0.040
  1423. $bounds[0][] = $c[4];
  1424. $bounds[1][] = $c[5];
  1425. }
  1426. return array($path, $bounds); // mPD 5.0.040
  1427. }
  1428. function CalcVectorAngle($ux, $uy, $vx, $vy) {
  1429. $ta = atan2($uy, $ux);
  1430. $tb = atan2($vy, $vx);
  1431. if ($tb >= $ta)
  1432. return ($tb-$ta);
  1433. return (6.28318530718 - ($ta-$tb));
  1434. }
  1435. // mPDF 4.4.003
  1436. function ConvertSVGSizePixels($size=5,$maxsize='x'){
  1437. // maxsize in pixels (user units) or 'y' or 'x'
  1438. // e.g. $w = $this->ConvertSVGSizePixels($arguments['w'],$this->svg_info['w']*(25.4/$this->mpdf_ref->dpi));
  1439. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1440. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1441. // For text $maxsize = Fontsize
  1442. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1443. if ($maxsize == 'y') { $maxsize = $this->svg_info['h']; }
  1444. else if ($maxsize == 'x') { $maxsize = $this->svg_info['w']; }
  1445. $maxsize *= (25.4/$this->mpdf_ref->dpi); // convert pixels to mm
  1446. $fontsize=$this->mpdf_ref->FontSize;
  1447. //Return as pixels
  1448. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,$fontsize,false) * 1/(25.4/$this->mpdf_ref->dpi);
  1449. return $size;
  1450. }
  1451. // mPDF 4.4.003
  1452. function ConvertSVGSizePts($size=5){
  1453. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1454. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1455. // For text $maxsize = Fontsize
  1456. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1457. $maxsize=$this->mpdf_ref->FontSize;
  1458. //Return as pts
  1459. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,false,true) * 72/25.4;
  1460. return $size;
  1461. }
  1462. //
  1463. // fonction retracant les <rect />
  1464. function svgRect($arguments){
  1465. if ($arguments['h']==0 || $arguments['w']==0) { return ''; } // mPDF 4.4.003
  1466. $x = $this->ConvertSVGSizePixels($arguments['x'],'x'); // mPDF 4.4.003
  1467. $y = $this->ConvertSVGSizePixels($arguments['y'],'y'); // mPDF 4.4.003
  1468. $h = $this->ConvertSVGSizePixels($arguments['h'],'y'); // mPDF 4.4.003
  1469. $w = $this->ConvertSVGSizePixels($arguments['w'],'x'); // mPDF 4.4.003
  1470. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1471. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1472. if ($rx > $w/2) { $rx = $w/2; } // mPDF 4.4.003
  1473. if ($ry > $h/2) { $ry = $h/2; } // mPDF 4.4.003
  1474. if ($rx>0 and $ry == 0){$ry = $rx;}
  1475. if ($ry>0 and $rx == 0){$rx = $ry;}
  1476. if ($rx == 0 and $ry == 0){
  1477. // trace un rectangle sans angle arrondit
  1478. $path_cmd = sprintf('%.3f %.3f m ', ($x*$this->kp), -($y*$this->kp));
  1479. $path_cmd .= sprintf('%.3f %.3f l ', (($x+$w)*$this->kp), -($y*$this->kp));
  1480. $path_cmd .= sprintf('%.3f %.3f l ', (($x+$w)*$this->kp), -(($y+$h)*$this->kp));
  1481. $path_cmd .= sprintf('%.3f %.3f l ', ($x)*$this->kp, -(($y+$h)*$this->kp));
  1482. $path_cmd .= sprintf('%.3f %.3f l h ', ($x*$this->kp), -($y*$this->kp));
  1483. }
  1484. else {
  1485. // trace un rectangle avec les arrondit
  1486. // les points de controle du bezier sont deduis grace a la constante kappa
  1487. $kappa = 4*(sqrt(2)-1)/3;
  1488. $kx = $kappa*$rx;
  1489. $ky = $kappa*$ry;
  1490. $path_cmd = sprintf('%.3f %.3f m ', ($x+$rx)*$this->kp, -$y*$this->kp);
  1491. $path_cmd .= sprintf('%.3f %.3f l ', ($x+($w-$rx))*$this->kp, -$y*$this->kp);
  1492. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x+($w-$rx+$kx))*$this->kp, -$y*$this->kp, ($x+$w)*$this->kp, (-$y+(-$ry+$ky))*$this->kp, ($x+$w)*$this->kp, (-$y+(-$ry))*$this->kp );
  1493. $path_cmd .= sprintf('%.3f %.3f l ', ($x+$w)*$this->kp, (-$y+(-$h+$ry))*$this->kp);
  1494. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x+$w)*$this->kp, (-$y+(-$h-$ky+$ry))*$this->kp, ($x+($w-$rx+$kx))*$this->kp, (-$y+(-$h))*$this->kp, ($x+($w-$rx))*$this->kp, (-$y+(-$h))*$this->kp );
  1495. $path_cmd .= sprintf('%.3f %.3f l ', ($x+$rx)*$this->kp, (-$y+(-$h))*$this->kp);
  1496. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x+($rx-$kx))*$this->kp, (-$y+(-$h))*$this->kp, $x*$this->kp, (-$y+(-$h-$ky+$ry))*$this->kp, $x*$this->kp, (-$y+(-$h+$ry))*$this->kp );
  1497. $path_cmd .= sprintf('%.3f %.3f l ', $x*$this->kp, (-$y+(-$ry))*$this->kp);
  1498. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c h ', $x*$this->kp, (-$y+(-$ry+$ky))*$this->kp, ($x+($rx-$kx))*$this->kp, -$y*$this->kp, ($x+$rx)*$this->kp, -$y*$this->kp );
  1499. }
  1500. return $path_cmd;
  1501. }
  1502. //
  1503. // fonction retracant les <ellipse /> et <circle />
  1504. // le cercle est tracé grave a 4 bezier cubic, les poitn de controles
  1505. // sont deduis grace a la constante kappa * rayon
  1506. function svgEllipse($arguments){
  1507. if ($arguments['rx']==0 || $arguments['ry']==0) { return ''; } // mPDF 4.4.003
  1508. $kappa = 4*(sqrt(2)-1)/3;
  1509. $cx = $this->ConvertSVGSizePixels($arguments['cx'],'x'); // mPDF 4.4.003
  1510. $cy = $this->ConvertSVGSizePixels($arguments['cy'],'y'); // mPDF 4.4.003
  1511. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1512. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1513. $x1 = $cx;
  1514. $y1 = -$cy+$ry;
  1515. $x2 = $cx+$rx;
  1516. $y2 = -$cy;
  1517. $x3 = $cx;
  1518. $y3 = -$cy-$ry;
  1519. $x4 = $cx-$rx;
  1520. $y4 = -$cy;
  1521. $path_cmd = sprintf('%.3f %.3f m ', $x1*$this->kp, $y1*$this->kp);
  1522. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x1+($rx*$kappa))*$this->kp, $y1*$this->kp, $x2*$this->kp, ($y2+($ry*$kappa))*$this->kp, $x2*$this->kp, $y2*$this->kp);
  1523. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $x2*$this->kp, ($y2-($ry*$kappa))*$this->kp, ($x3+($rx*$kappa))*$this->kp, $y3*$this->kp, $x3*$this->kp, $y3*$this->kp);
  1524. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', ($x3-($rx*$kappa))*$this->kp, $y3*$this->kp, $x4*$this->kp, ($y4-($ry*$kappa))*$this->kp, $x4*$this->kp, $y4*$this->kp);
  1525. $path_cmd .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f c ', $x4*$this->kp, ($y4+($ry*$kappa))*$this->kp, ($x1-($rx*$kappa))*$this->kp, $y1*$this->kp, $x1*$this->kp, $y1*$this->kp);
  1526. $path_cmd .= 'h ';
  1527. return $path_cmd;
  1528. }
  1529. //
  1530. // fonction retracant les <polyline /> et les <line />
  1531. function svgPolyline($arguments,$ispolyline=true){
  1532. if ($ispolyline) {
  1533. $xbase = $arguments[0] ;
  1534. $ybase = - $arguments[1] ;
  1535. }
  1536. else {
  1537. if ($arguments[0]==$arguments[2] && $arguments[1]==$arguments[3]) { return ''; } // mPDF 4.4.003 Zero length line
  1538. $xbase = $this->ConvertSVGSizePixels($arguments[0],'x'); // mPDF 4.4.003
  1539. $ybase = - $this->ConvertSVGSizePixels($arguments[1],'y'); // mPDF 4.4.003
  1540. }
  1541. $path_cmd = sprintf('%.3f %.3f m ', $xbase*$this->kp, $ybase*$this->kp);
  1542. for ($i = 2; $i<count($arguments);$i += 2) {
  1543. if ($ispolyline) {
  1544. $tmp_x = $arguments[$i] ;
  1545. $tmp_y = - $arguments[($i+1)] ;
  1546. }
  1547. else {
  1548. $tmp_x = $this->ConvertSVGSizePixels($arguments[$i],'x') ; // mPDF 4.4.003
  1549. $tmp_y = - $this->ConvertSVGSizePixels($arguments[($i+1)],'y') ; // mPDF 4.4.003
  1550. }
  1551. $path_cmd .= sprintf('%.3f %.3f l ', $tmp_x*$this->kp, $tmp_y*$this->kp);
  1552. }
  1553. // $path_cmd .= 'h '; // ?? In error - don't close subpath here
  1554. return $path_cmd;
  1555. }
  1556. //
  1557. // fonction retracant les <polygone />
  1558. function svgPolygon($arguments){
  1559. $xbase = $arguments[0] ;
  1560. $ybase = - $arguments[1] ;
  1561. $path_cmd = sprintf('%.3f %.3f m ', $xbase*$this->kp, $ybase*$this->kp);
  1562. for ($i = 2; $i<count($arguments);$i += 2) {
  1563. $tmp_x = $arguments[$i] ;
  1564. $tmp_y = - $arguments[($i+1)] ;
  1565. $path_cmd .= sprintf('%.3f %.3f l ', $tmp_x*$this->kp, $tmp_y*$this->kp);
  1566. }
  1567. $path_cmd .= sprintf('%.3f %.3f l ', $xbase*$this->kp, $ybase*$this->kp);
  1568. $path_cmd .= 'h ';
  1569. return $path_cmd;
  1570. }
  1571. //
  1572. // write string to image
  1573. function svgText() {
  1574. // $tmp = count($this->txt_style)-1;
  1575. $current_style = array_pop($this->txt_style);
  1576. $style = '';
  1577. $render = -1;
  1578. if(isset($this->txt_data[2]))
  1579. {
  1580. // select font
  1581. $style .= ($current_style['font-weight'] == 'bold')?'B':'';
  1582. $style .= ($current_style['font-style'] == 'italic')?'I':'';
  1583. $size = $current_style['font-size']*$this->kf; // mPDF 5.0.039
  1584. // mPDF 5.0
  1585. $current_style['font-family'] = $this->mpdf_ref->SetFont($current_style['font-family'],$style,$size,false);
  1586. $this->mpdf_ref->CurrentFont['fo'] = true; // mPDF 5.0.039
  1587. // mPDF 5.0.041
  1588. $opacitystr = '';
  1589. $opacity = 1;
  1590. if (isset($current_style['fill-opacity'])) {
  1591. if ($current_style['fill-opacity'] == 0) { $opacity = 0; }
  1592. else if ($current_style['fill-opacity'] > 1) { $opacity = 1; }
  1593. else if ($current_style['fill-opacity'] > 0) { $opacity = $current_style['fill-opacity']; }
  1594. else if ($current_style['fill-opacity'] < 0) { $opacity = 0; }
  1595. }
  1596. $gs = $this->mpdf_ref->AddExtGState(array('ca'=>$opacity, 'BM'=>'/Normal'));
  1597. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  1598. $opacitystr = sprintf(' /GS%d gs ', $gs);
  1599. // mPDF 5.0.051
  1600. $fillstr = '';
  1601. if (isset($current_style['fill']) && $current_style['fill']!='none') {
  1602. $col = $this->mpdf_ref->ConvertColor($current_style['fill']);
  1603. // mPDF 5.0.051
  1604. $fillstr = $this->mpdf_ref->SetFColor($col, true);
  1605. $render = "0"; // Fill (only)
  1606. }
  1607. $strokestr = '';
  1608. if (isset($current_style['stroke-width']) && $current_style['stroke-width']>0 && $current_style['stroke']!='none') {
  1609. $scol = $this->mpdf_ref->ConvertColor($current_style['stroke']);
  1610. if ($scol) {
  1611. $strokestr .= $this->mpdf_ref->SetDColor($scol, true).' '; // mPDF 5.0.051
  1612. }
  1613. $linewidth = $this->ConvertSVGSizePixels($current_style['stroke-width']);
  1614. if ($linewidth > 0) {
  1615. $strokestr .= sprintf('%.3f w 1 J 1 j ',$linewidth*$this->kp);
  1616. if ($render == -1) { $render = "1"; } // stroke only
  1617. else { $render = "2"; } // fill and stroke
  1618. }
  1619. }
  1620. if ($render == -1) { return ''; }
  1621. $x = $this->ConvertSVGSizePixels($this->txt_data[0],'x'); // mPDF 4.4.003
  1622. $y = $this->ConvertSVGSizePixels($this->txt_data[1],'y'); // mPDF 4.4.003
  1623. $txt = $this->txt_data[2];
  1624. // mPDF 4.4.003
  1625. $txt = preg_replace('/\f/','',$txt);
  1626. $txt = preg_replace('/\r/','',$txt);
  1627. $txt = preg_replace('/\n/',' ',$txt);
  1628. $txt = preg_replace('/\t/',' ',$txt);
  1629. $txt = preg_replace("/[ ]+/u",' ',$txt);
  1630. $txt = trim($txt);
  1631. $txt = $this->mpdf_ref->purify_utf8_text($txt);
  1632. if ($this->mpdf_ref->text_input_as_HTML) {
  1633. $txt = $this->mpdf_ref->all_entities_to_utf8($txt);
  1634. }
  1635. // mPDF 5.0
  1636. if ($this->mpdf_ref->usingCoreFont) { $txt = mb_convert_encoding($txt,$this->mpdf_ref->mb_enc,'UTF-8'); }
  1637. if (preg_match("/([".$this->mpdf_ref->pregRTLchars."])/u", $txt)) { $this->mpdf_ref->biDirectional = true; } // mPDF 4.4.003
  1638. $this->mpdf_ref->magic_reverse_dir($txt, true, 'ltr'); // mPDF 5.0.054
  1639. $this->mpdf_ref->ConvertIndic($txt);
  1640. if ($current_style['text-anchor']=='middle') {
  1641. $tw = $this->mpdf_ref->GetStringWidth($txt)*$this->mpdf_ref->k/2; // mPDF 4.4.003
  1642. }
  1643. else if ($current_style['text-anchor']=='end') {
  1644. $tw = $this->mpdf_ref->GetStringWidth($txt)*$this->mpdf_ref->k; // mPDF 4.4.003
  1645. }
  1646. else $tw = 0;
  1647. if (!$this->mpdf_ref->usingCoreFont) {
  1648. $this->mpdf_ref->UTF8StringToArray($txt); // mPDF 5.0 adds chars to subset list
  1649. $txt= $this->mpdf_ref->UTF8ToUTF16BE($txt, false);
  1650. }
  1651. $txt='('.$this->mpdf_ref->_escape($txt).')';
  1652. $this->mpdf_ref->CurrentFont['used']= true;
  1653. $pdfx = $x - $tw/$this->kp; // mPDF 4.4.009
  1654. $pdfy = -$y ;
  1655. $xbase = $x;
  1656. $ybase = -$y;
  1657. // mPDF 5.0.041
  1658. // mPDF 5.0.051
  1659. $path_cmd = sprintf('q BT /F%d %s %.3f Tf %.3f %.3f Td %s Tr %s %s %s Tj ET Q ',$this->mpdf_ref->CurrentFont['i'],$opacitystr, $this->mpdf_ref->FontSizePt,$pdfx*$this->kp,$pdfy*$this->kp,$render,$fillstr,$strokestr,$txt)."\n";
  1660. unset($this->txt_data[0], $this->txt_data[1],$this->txt_data[2]);
  1661. }
  1662. else
  1663. {
  1664. return ' ';
  1665. }
  1666. // $path_cmd .= 'h '; // mPDF 5.0
  1667. return $path_cmd;
  1668. }
  1669. function svgDefineTxtStyle($critere_style)
  1670. {
  1671. // get copy of current/default txt style, and modify it with supplied attributes
  1672. $tmp = count($this->txt_style)-1;
  1673. $current_style = $this->txt_style[$tmp];
  1674. if (isset($critere_style['style'])){
  1675. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1676. $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);
  1677. }
  1678. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']);
  1679. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  1680. }
  1681. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1682. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  1683. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1684. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  1685. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1686. $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);
  1687. }
  1688. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1689. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  1690. }
  1691. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1692. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  1693. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1694. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  1695. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1696. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  1697. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1698. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  1699. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1700. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  1701. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  1702. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  1703. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1704. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  1705. // mPDF 5.0.039
  1706. $tmp = preg_replace("/(.*)font-family:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1707. if ($tmp != $critere_style['style']){ $critere_style['font-family'] = $tmp;}
  1708. $tmp = preg_replace("/(.*)font-size:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1709. if ($tmp != $critere_style['style']){ $critere_style['font-size'] = $tmp;}
  1710. $tmp = preg_replace("/(.*)font-weight:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1711. if ($tmp != $critere_style['style']){ $critere_style['font-weight'] = $tmp;}
  1712. $tmp = preg_replace("/(.*)font-style:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1713. if ($tmp != $critere_style['style']){ $critere_style['font-style'] = $tmp;}
  1714. }
  1715. if (isset($critere_style['font'])){
  1716. // [ [ <'font-style'> || <'font-variant'> || <'font-weight'> ]?<'font-size'> [ / <'line-height'> ]? <'font-family'> ]
  1717. $tmp = preg_replace("/(.*)(italic|oblique)(.*)/i","$2",$critere_style['font']);
  1718. if ($tmp != $critere_style['font']){
  1719. if($tmp == 'oblique'){
  1720. $tmp = 'italic';
  1721. }
  1722. $current_style['font-style'] = $tmp;
  1723. }
  1724. $tmp = preg_replace("/(.*)(bold|bolder)(.*)/i","$2",$critere_style['font']);
  1725. if ($tmp != $critere_style['font']){
  1726. if($tmp == 'bolder'){
  1727. $tmp = 'bold';
  1728. }
  1729. $current_style['font-weight'] = $tmp;
  1730. }
  1731. // select digits not followed by percent sign nor preceeded by forward slash
  1732. $tmp = preg_replace("/(.*)\b(\d+)[\b|\/](.*)/i","$2",$critere_style['font']);
  1733. if ($tmp != $critere_style['font']){
  1734. // mPDF 4.4.003
  1735. $current_style['font-size'] = $this->ConvertSVGSizePts($tmp);
  1736. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1737. }
  1738. }
  1739. if(isset($critere_style['fill'])){
  1740. $current_style['fill'] = $critere_style['fill'];
  1741. }
  1742. // mPDF 4.4.003
  1743. if(isset($critere_style['stroke'])){
  1744. $current_style['stroke'] = $critere_style['stroke'];
  1745. }
  1746. if(isset($critere_style['stroke-width'])){
  1747. $current_style['stroke-width'] = $critere_style['stroke-width'];
  1748. }
  1749. if(isset($critere_style['font-style'])){
  1750. if(strtolower($critere_style['font-style']) == 'oblique')
  1751. {
  1752. $critere_style['font-style'] = 'italic';
  1753. }
  1754. $current_style['font-style'] = $critere_style['font-style'];
  1755. }
  1756. if(isset($critere_style['font-weight'])){
  1757. if(strtolower($critere_style['font-weight']) == 'bolder')
  1758. {
  1759. $critere_style['font-weight'] = 'bold';
  1760. }
  1761. $current_style['font-weight'] = $critere_style['font-weight'];
  1762. }
  1763. if(isset($critere_style['font-size'])){
  1764. // mPDF 4.4.003
  1765. $current_style['font-size'] = $this->ConvertSVGSizePts($critere_style['font-size']);
  1766. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1767. }
  1768. if(isset($critere_style['font-family'])){
  1769. // mPDF 4.4.003
  1770. $v = $critere_style['font-family'];
  1771. $aux_fontlist = explode(",",$v);
  1772. $found = 0;
  1773. foreach($aux_fontlist AS $f) {
  1774. $fonttype = trim($f);
  1775. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  1776. $fonttype = preg_replace('/ /','',$fonttype);
  1777. $v = strtolower(trim($fonttype));
  1778. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) { $v = $this->mpdf_ref->fonttrans[$v]; }
  1779. if ((!$this->mpdf_ref->usingCoreFont && in_array($v,$this->mpdf_ref->available_unifonts)) ||
  1780. ($this->mpdf_ref->usingCoreFont && in_array($v,array('courier','times','helvetica','arial'))) ||
  1781. in_array($v, array('sjis','uhc','big5','gb'))) {
  1782. $current_style['font-family'] = $v;
  1783. $found = 1;
  1784. break;
  1785. }
  1786. }
  1787. if (!$found) {
  1788. foreach($aux_fontlist AS $f) {
  1789. $fonttype = trim($f);
  1790. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  1791. $fonttype = preg_replace('/ /','',$fonttype);
  1792. $v = strtolower(trim($fonttype));
  1793. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) { $v = $this->mpdf_ref->fonttrans[$v]; }
  1794. if (in_array($v,$this->mpdf_ref->sans_fonts) || in_array($v,$this->mpdf_ref->serif_fonts) || in_array($v,$this->mpdf_ref->mono_fonts) ) {
  1795. $current_style['font-family'] = $v;
  1796. break;
  1797. }
  1798. }
  1799. }
  1800. }
  1801. if(isset($critere_style['text-anchor'])){
  1802. $current_style['text-anchor'] = $critere_style['text-anchor'];
  1803. }
  1804. // add current style to text style array (will remove it later after writing text to svg_string)
  1805. array_push($this->txt_style,$current_style);
  1806. }
  1807. //
  1808. // fonction ajoutant un gradient
  1809. function svgAddGradient($id,$array_gradient){
  1810. $this->svg_gradient[$id] = $array_gradient;
  1811. }
  1812. //
  1813. // Ajoute une couleur dans le gradient correspondant
  1814. //
  1815. // function ecrivant dans le svgstring
  1816. function svgWriteString($content){
  1817. $this->svg_string .= $content;
  1818. }
  1819. // analise le svg et renvoie aux fonctions precedente our le traitement
  1820. function ImageSVG($data){
  1821. $this->svg_info = array();
  1822. // mPDF 4.4.006
  1823. if (preg_match('/<!ENTITY/si',$data)) {
  1824. // Get User-defined entities
  1825. preg_match_all('/<!ENTITY\s+([a-z]+)\s+\"(.*?)\">/si',$data, $ent);
  1826. // Replace entities
  1827. for ($i=0; $i<count($ent[0]); $i++) {
  1828. $data = preg_replace('/&'.preg_quote($ent[1][$i],'/').';/is', $ent[2][$i], $data);
  1829. }
  1830. }
  1831. // mPDF 4.4.003
  1832. if (preg_match('/xlink:href=/si',$data)) {
  1833. // Get links
  1834. preg_match_all('/(<(linearGradient|radialgradient)[^>]*)xlink:href=["\']#(.*?)["\'](.*?)\/>/si',$data, $links);
  1835. if (count($links[0])) { $links[5] = array(); } // mPDF 4.5.010
  1836. // Delete links from data - keeping in $links
  1837. for ($i=0; $i<count($links[0]); $i++) {
  1838. $links[5][$i] = 'tmpLink'.RAND(100000,9999999); // mPDF 4.5.010
  1839. $data = preg_replace('/'.preg_quote($links[0][$i],'/').'/is', '<MYLINKS'.$links[5][$i].'>' , $data); // mPDF 4.5.010
  1840. }
  1841. // Get targets
  1842. preg_match_all('/<(linearGradient|radialgradient)([^>]*)id=["\'](.*?)["\'](.*?)>(.*?)<\/(linearGradient|radialgradient)>/si',$data, $m);
  1843. $targets = array();
  1844. $stops = array();
  1845. // keeping in $targets
  1846. for ($i=0; $i<count($m[0]); $i++) {
  1847. $stops[$m[3][$i]] = $m[5][$i];
  1848. }
  1849. // Add back links this time as targets (gradients)
  1850. for ($i=0; $i<count($links[0]); $i++) {
  1851. $def = $links[1][$i] .' '.$links[4][$i].'>'. $stops[$links[3][$i]].'</'.$links[2][$i] .'>' ; // mPDF 4.5.010
  1852. $data = preg_replace('/<MYLINKS'.$links[5][$i].'>/is', $def , $data); // mPDF 4.5.010
  1853. }
  1854. }
  1855. // mPDF 4.4.003 - Removes <pattern>
  1856. $data = preg_replace('/<pattern.*?<\/pattern>/is', '', $data);
  1857. // mPDF 4.4.003 - Removes <marker>
  1858. $data = preg_replace('/<marker.*?<\/marker>/is', '', $data);
  1859. $this->svg_info['data'] = $data;
  1860. $this->svg_string = '';
  1861. //
  1862. // chargement unique des fonctions
  1863. if(!function_exists(xml_svg2pdf_start)){
  1864. function xml_svg2pdf_start($parser, $name, $attribs){
  1865. //
  1866. // definition
  1867. global $svg_class, $last_gradid;
  1868. // mPDF 4.4.003
  1869. $svg_class->xbase = 0;
  1870. $svg_class->ybase = 0;
  1871. switch (strtolower($name)){
  1872. // mPDF 5.0.039 - Don't output stuff inside <defs>
  1873. case 'defs':
  1874. $svg_class->inDefs = true;
  1875. return;
  1876. case 'svg':
  1877. $svg_class->svgOffset($attribs);
  1878. break;
  1879. case 'path':
  1880. $path = $attribs['d'];
  1881. // mPDF 4.4.003
  1882. preg_match_all('/([MZLHVCSQTAmzlhvcsqta])([e ,\-.\d]+)*/', $path, $commands, PREG_SET_ORDER);
  1883. $path_cmd = '';
  1884. $svg_class->subPathInit = true; // mPDF 4.4.003
  1885. // mPDF 5.0.039
  1886. $svg_class->pathBBox = array(999999,999999,-999999,-999999);
  1887. foreach($commands as $c){
  1888. if(count($c)==3 || $c[2]==''){
  1889. list($tmp, $command, $arguments) = $c;
  1890. }
  1891. else{
  1892. list($tmp, $command) = $c;
  1893. $arguments = '';
  1894. }
  1895. $path_cmd .= $svg_class->svgPath($command, $arguments);
  1896. }
  1897. // mPDF 5.0.039
  1898. if ($svg_class->pathBBox[2]==-1999998) { $svg_class->pathBBox[2] = 100; }
  1899. if ($svg_class->pathBBox[3]==-1999998) { $svg_class->pathBBox[3] = 100; }
  1900. if ($svg_class->pathBBox[0]==999999) { $svg_class->pathBBox[0] = 0; }
  1901. if ($svg_class->pathBBox[1]==999999) { $svg_class->pathBBox[1] = 0; }
  1902. $critere_style = $attribs;
  1903. unset($critere_style['d']);
  1904. $path_style = $svg_class->svgDefineStyle($critere_style);
  1905. break;
  1906. case 'rect':
  1907. if (!isset($attribs['x'])) {$attribs['x'] = 0;}
  1908. if (!isset($attribs['y'])) {$attribs['y'] = 0;}
  1909. if (!isset($attribs['rx'])) {$attribs['rx'] = 0;}
  1910. if (!isset($attribs['ry'])) {$attribs['ry'] = 0;}
  1911. $arguments = array(
  1912. 'x' => $attribs['x'],
  1913. 'y' => $attribs['y'],
  1914. 'w' => $attribs['width'],
  1915. 'h' => $attribs['height'],
  1916. 'rx' => $attribs['rx'],
  1917. 'ry' => $attribs['ry']
  1918. );
  1919. $path_cmd = $svg_class->svgRect($arguments);
  1920. $critere_style = $attribs;
  1921. unset($critere_style['x'],$critere_style['y'],$critere_style['rx'],$critere_style['ry'],$critere_style['height'],$critere_style['width']);
  1922. $path_style = $svg_class->svgDefineStyle($critere_style);
  1923. break;
  1924. case 'circle':
  1925. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1926. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1927. $arguments = array(
  1928. 'cx' => $attribs['cx'],
  1929. 'cy' => $attribs['cy'],
  1930. 'rx' => $attribs['r'],
  1931. 'ry' => $attribs['r']
  1932. );
  1933. $path_cmd = $svg_class->svgEllipse($arguments);
  1934. $critere_style = $attribs;
  1935. unset($critere_style['cx'],$critere_style['cy'],$critere_style['r']);
  1936. $path_style = $svg_class->svgDefineStyle($critere_style);
  1937. break;
  1938. case 'ellipse':
  1939. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1940. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1941. $arguments = array(
  1942. 'cx' => $attribs['cx'],
  1943. 'cy' => $attribs['cy'],
  1944. 'rx' => $attribs['rx'],
  1945. 'ry' => $attribs['ry']
  1946. );
  1947. $path_cmd = $svg_class->svgEllipse($arguments);
  1948. $critere_style = $attribs;
  1949. unset($critere_style['cx'],$critere_style['cy'],$critere_style['rx'],$critere_style['ry']);
  1950. $path_style = $svg_class->svgDefineStyle($critere_style);
  1951. break;
  1952. case 'line':
  1953. $arguments = array($attribs['x1'],$attribs['y1'],$attribs['x2'],$attribs['y2']);
  1954. $path_cmd = $svg_class->svgPolyline($arguments,false); // mPDF 4.4.003
  1955. $critere_style = $attribs;
  1956. unset($critere_style['x1'],$critere_style['y1'],$critere_style['x2'],$critere_style['y2']);
  1957. $path_style = $svg_class->svgDefineStyle($critere_style);
  1958. break;
  1959. case 'polyline':
  1960. $path = $attribs['points'];
  1961. preg_match_all('/[0-9\-\.]*/',$path, $tmp, PREG_SET_ORDER);
  1962. $arguments = array();
  1963. for ($i=0;$i<count($tmp);$i++){
  1964. if ($tmp[$i][0] !=''){
  1965. array_push($arguments, $tmp[$i][0]);
  1966. }
  1967. }
  1968. $path_cmd = $svg_class->svgPolyline($arguments);
  1969. $critere_style = $attribs;
  1970. unset($critere_style['points']);
  1971. $path_style = $svg_class->svgDefineStyle($critere_style);
  1972. break;
  1973. case 'polygon':
  1974. $path = $attribs['points'];
  1975. preg_match_all('/([\-]*[0-9\.]+)/',$path, $tmp);
  1976. $arguments = array();
  1977. for ($i=0;$i<count($tmp[0]);$i++){
  1978. if ($tmp[0][$i] !=''){
  1979. array_push($arguments, $tmp[0][$i]);
  1980. }
  1981. }
  1982. $path_cmd = $svg_class->svgPolygon($arguments);
  1983. // definition du style de la forme:
  1984. $critere_style = $attribs;
  1985. unset($critere_style['points']);
  1986. $path_style = $svg_class->svgDefineStyle($critere_style);
  1987. break;
  1988. case 'lineargradient':
  1989. $tmp_gradient = array(
  1990. 'type' => 'linear',
  1991. 'info' => array(
  1992. 'x1' => $attribs['x1'],
  1993. 'y1' => $attribs['y1'],
  1994. 'x2' => $attribs['x2'],
  1995. 'y2' => $attribs['y2']
  1996. ),
  1997. 'transform' => $attribs['gradientTransform'],
  1998. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  1999. 'spread' => $attribs['spreadMethod'], /* mPDF 5.0.040 */
  2000. 'color' => array()
  2001. );
  2002. $last_gradid = $attribs['id'];
  2003. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  2004. break;
  2005. case 'radialgradient':
  2006. $tmp_gradient = array(
  2007. 'type' => 'radial',
  2008. 'info' => array(
  2009. 'x0' => $attribs['cx'],
  2010. 'y0' => $attribs['cy'],
  2011. 'x1' => $attribs['fx'],
  2012. 'y1' => $attribs['fy'],
  2013. 'r' => $attribs['r']
  2014. ),
  2015. 'transform' => $attribs['gradientTransform'],
  2016. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  2017. 'spread' => $attribs['spreadMethod'], /* mPDF 5.0.040 */
  2018. 'color' => array()
  2019. );
  2020. $last_gradid = $attribs['id'];
  2021. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  2022. break;
  2023. case 'stop':
  2024. if (!$last_gradid) break;
  2025. // mPDF 4.4.003 // mPDF 5.0.040
  2026. if (isset($attribs['style']) AND preg_match('/stop-color:\s*([^;]*)/i',$attribs['style'],$m)) {
  2027. $color = trim($m[1]);
  2028. } else if (isset($attribs['stop-color'])) {
  2029. $color = $attribs['stop-color'];
  2030. }
  2031. $col = $svg_class->mpdf_ref->ConvertColor($color);
  2032. // mPDF 5.0.051
  2033. if ($col[0]==3 || $col[0]==5) { // RGB
  2034. $color_final = sprintf('%.3f %.3f %.3f',$col[1]/255,$col[2]/255,$col[3]/255);
  2035. $svg_class->svg_gradient[$last_gradid]['colorspace']='RGB';
  2036. }
  2037. else if ($col[0]==4 || $col[0]==6) { // CMYK
  2038. $color_final = sprintf('%.3f %.3f %.3f %.3f',$col[1]/100,$col[2]/100,$col[3]/100,$col[4]/100);
  2039. $svg_class->svg_gradient[$last_gradid]['colorspace']='CMYK';
  2040. }
  2041. else if ($col[0]==1) { // RGB
  2042. $color_final = sprintf('%.3f',$col[1]/255);
  2043. $svg_class->svg_gradient[$last_gradid]['colorspace']='Gray';
  2044. }
  2045. // mPDF 5.0.020
  2046. $stop_opacity = 1;
  2047. // mPDF 4.4.003
  2048. if (isset($attribs['style']) AND preg_match('/stop-opacity:\s*([0-9.]*)/i',$attribs['style'],$m)) {
  2049. $stop_opacity = $m[1];
  2050. } else if (isset($attribs['stop-opacity'])) {
  2051. $stop_opacity = $attribs['stop-opacity'];
  2052. }
  2053. // mPDF 5.0.051
  2054. else if ($col[0]==5) { // RGBa
  2055. $stop_opacity = $col[4];
  2056. }
  2057. else if ($col[0]==5) { // CMYKa
  2058. $stop_opacity = $col[5];
  2059. }
  2060. $tmp_color = array(
  2061. 'color' => $color_final,
  2062. 'offset' => $attribs['offset'],
  2063. 'opacity' => $stop_opacity
  2064. );
  2065. array_push($svg_class->svg_gradient[$last_gradid]['color'],$tmp_color);
  2066. break;
  2067. case 'a':
  2068. if (isset($attribs['xlink:href'])) {
  2069. unset($attribs['xlink:href']); // this should be a hyperlink
  2070. // not handled like a xlink:href in other elements
  2071. } // then continue like a <g>
  2072. case 'g':
  2073. $array_style = $svg_class->svgDefineStyle($attribs);
  2074. if ($array_style['transformations']) {
  2075. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  2076. }
  2077. array_push($svg_class->svg_style,$array_style);
  2078. $svg_class->svgDefineTxtStyle($attribs); // mPDF 4.4.003
  2079. break;
  2080. case 'text':
  2081. // mPDF 4.4.003
  2082. $array_style = $svg_class->svgDefineStyle($attribs);
  2083. if ($array_style['transformations']) {
  2084. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  2085. }
  2086. array_push($svg_class->svg_style,$array_style);
  2087. $svg_class->txt_data = array();
  2088. $svg_class->txt_data[0] = $attribs['x'];
  2089. $svg_class->txt_data[1] = $attribs['y'];
  2090. $critere_style = $attribs;
  2091. unset($critere_style['x'], $critere_style['y']);
  2092. $svg_class->svgDefineTxtStyle($critere_style);
  2093. break;
  2094. }
  2095. //
  2096. //insertion des path et du style dans le flux de donné general.
  2097. if (isset($path_cmd) && $path_cmd) { // mPDF 4.4.003
  2098. // mPDF 5.0
  2099. list($prestyle,$poststyle) = $svg_class->svgStyle($path_style, $attribs, strtolower($name));
  2100. if ($path_style['transformations']) { // transformation on an element
  2101. $svg_class->svgWriteString(" q ".$path_style['transformations']. " $prestyle $path_cmd $poststyle" . " Q\n");
  2102. }
  2103. else {
  2104. $svg_class->svgWriteString("$prestyle $path_cmd $poststyle\n");
  2105. }
  2106. }
  2107. }
  2108. function characterData($parser, $data)
  2109. {
  2110. global $svg_class;
  2111. if(isset($svg_class->txt_data[2])) {
  2112. $svg_class->txt_data[2] .= $data;
  2113. }
  2114. else {
  2115. $svg_class->txt_data[2] = $data;
  2116. }
  2117. }
  2118. function xml_svg2pdf_end($parser, $name){
  2119. global $svg_class;
  2120. switch($name){
  2121. case "g":
  2122. case "a":
  2123. $tmp = count($svg_class->svg_style)-1;
  2124. $current_style = $svg_class->svg_style[$tmp];
  2125. if ($current_style['transformations']) {
  2126. $svg_class->svgWriteString(" Q\n");
  2127. }
  2128. array_pop($svg_class->svg_style);
  2129. array_pop($svg_class->txt_style); // mPDF 4.4.003
  2130. break;
  2131. case 'radialgradient':
  2132. case 'lineargradient':
  2133. $last_gradid = '';
  2134. break;
  2135. case "text":
  2136. $path_cmd = $svg_class->svgText();
  2137. // echo 'path >> '.$path_cmd."<br><br>";
  2138. // echo "style >> ".$get_style[1]."<br><br>";
  2139. $svg_class->svgWriteString($path_cmd);
  2140. // mPDF 4.4.003
  2141. $tmp = count($svg_class->svg_style)-1;
  2142. $current_style = $svg_class->svg_style[$tmp];
  2143. if ($current_style['transformations']) {
  2144. $svg_class->svgWriteString(" Q\n");
  2145. }
  2146. array_pop($svg_class->svg_style);
  2147. break;
  2148. }
  2149. // mPDF 5.0.039 - Don't output stuff inside <defs>
  2150. if ($name == 'defs') {
  2151. $svg_class->inDefs = false;
  2152. }
  2153. }
  2154. }
  2155. $svg2pdf_xml='';
  2156. global $svg_class;
  2157. $svg_class = $this;
  2158. // mPDF 5.0.039 - Don't output stuff inside <defs>
  2159. $svg_class->inDefs = false;
  2160. $svg2pdf_xml_parser = xml_parser_create("utf-8");
  2161. xml_parser_set_option($svg2pdf_xml_parser, XML_OPTION_CASE_FOLDING, false);
  2162. xml_set_element_handler($svg2pdf_xml_parser, "xml_svg2pdf_start", "xml_svg2pdf_end");
  2163. xml_set_character_data_handler($svg2pdf_xml_parser, "characterData");
  2164. xml_parse($svg2pdf_xml_parser, $data);
  2165. // mPDF 4.4.003
  2166. if ($this->svg_error) { return false; }
  2167. else {
  2168. return array('x'=>$this->svg_info['x']*$this->kp,'y'=>-$this->svg_info['y']*$this->kp,'w'=>$this->svg_info['w']*$this->kp,'h'=>-$this->svg_info['h']*$this->kp,'data'=>$svg_class->svg_string);
  2169. }
  2170. }
  2171. }
  2172. // END OF CLASS
  2173. // mPDF 5.0.040
  2174. function calc_bezier_bbox($start, $c) {
  2175. $P0 = array($start[0],$start[1]);
  2176. $P1 = array($c[0],$c[1]);
  2177. $P2 = array($c[2],$c[3]);
  2178. $P3 = array($c[4],$c[5]);
  2179. $bounds = array();
  2180. $bounds[0][] = $P0[0];
  2181. $bounds[1][] = $P0[1];
  2182. $bounds[0][] = $P3[0];
  2183. $bounds[1][] = $P3[1];
  2184. for ($i=0;$i<=1;$i++) {
  2185. $b = 6 * $P0[$i] - 12 * $P1[$i] + 6 * $P2[$i];
  2186. $a = -3 * $P0[$i] + 9 * $P1[$i] - 9 * $P2[$i] + 3 * $P3[$i];
  2187. $c = 3 * $P1[$i] - 3 * $P0[$i];
  2188. if ($a == 0) {
  2189. if ($b == 0) { continue; }
  2190. $t = -$c / $b;
  2191. if ($t>0 && $t<1) {
  2192. $bounds[$i][] = (pow((1-$t),3) * $P0[$i] + 3 * pow((1-$t),2) * $t * $P1[$i] + 3 * (1-$t) * pow($t,2) * $P2[$i] + pow($t,3) * $P3[$i]);
  2193. }
  2194. continue;
  2195. }
  2196. $b2ac = pow($b, 2) - 4 * $c * $a;
  2197. if ($b2ac < 0) { continue; }
  2198. $t1 = (-$b + sqrt($b2ac))/(2 * $a);
  2199. if ($t1>0 && $t1<1) {
  2200. $bounds[$i][] = (pow((1-$t1),3) * $P0[$i] + 3 * pow((1-$t1),2) * $t1 * $P1[$i] + 3 * (1-$t1) * pow($t1,2) * $P2[$i] + pow($t1,3) * $P3[$i]);
  2201. }
  2202. $t2 = (-$b - sqrt($b2ac))/(2 * $a);
  2203. if ($t2>0 && $t2<1) {
  2204. $bounds[$i][] = (pow((1-$t2),3) * $P0[$i] + 3 * pow((1-$t2),2) * $t2 * $P1[$i] + 3 * (1-$t2) * pow($t2,2) * $P2[$i] + pow($t2,3) * $P3[$i]);
  2205. }
  2206. }
  2207. $x = min($bounds[0]);
  2208. $x2 = max($bounds[0]);
  2209. $y = min($bounds[1]);
  2210. $y2 = max($bounds[1]);
  2211. return array($x, $y, $x2, $y2);
  2212. }
  2213. // mPDF 5.0.040
  2214. function _testIntersectCircle($cx, $cy, $cr) {
  2215. // Tests whether a circle fully encloses a rectangle 0,0,1,1
  2216. // to see if any further radial gradients need adding (SVG)
  2217. // If centre of circle is inside 0,0,1,1 square
  2218. if ($cx >= 0 && $cx <= 1 && $cy >= 0 && $cy <= 1) {
  2219. $maxd = 1.5;
  2220. }
  2221. // distance to four corners
  2222. else {
  2223. $d1 = sqrt(pow(($cy-0),2) + pow(($cx-0),2));
  2224. $d2 = sqrt(pow(($cy-1),2) + pow(($cx-0),2));
  2225. $d3 = sqrt(pow(($cy-0),2) + pow(($cx-1),2));
  2226. $d4 = sqrt(pow(($cy-1),2) + pow(($cx-1),2));
  2227. $maxd = max($d1,$d2,$d3,$d4);
  2228. }
  2229. if ($cr < $maxd) { return true; }
  2230. else { return false; }
  2231. }
  2232. // mPDF 5.0.040
  2233. function _testIntersect($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) {
  2234. // Tests whether line (x1, y1) and (x2, y2) [a gradient axis (perpendicular)]
  2235. // intersects with a specific line segment (x3, y3) and (x4, y4)
  2236. $a1 = $y2-$y1;
  2237. $b1 = $x1-$x2;
  2238. $c1 = $a1*$x1+$b1*$y1;
  2239. $a2 = $y4-$y3;
  2240. $b2 = $x3-$x4;
  2241. $c2 = $a2*$x3+$b2*$y3;
  2242. $det = $a1*$b2 - $a2*$b1;
  2243. if($det == 0){ //Lines are parallel
  2244. return false;
  2245. }
  2246. else{
  2247. $x = ($b2*$c1 - $b1*$c2)/$det;
  2248. $y = ($a1*$c2 - $a2*$c1)/$det;
  2249. if ($x >= $x3 && $x <= $x4 && $y >= $y3 && $y <= $y4) { return true; }
  2250. }
  2251. return false;
  2252. }
  2253. ?>