PageRenderTime 114ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/woolfg/fusioninvoice
PHP | 2600 lines | 1990 code | 333 blank | 277 comment | 682 complexity | 6c44e0c0b88fc77a1c9b2fb137254866 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // svg class modified for mPDF version 4.4.003 by Ian Back: based on -
  3. // svg2pdf fpdf class
  4. // sylvain briand (syb@godisaduck.com), modified by rick trevino (rtrevino1@yahoo.com)
  5. // http://www.godisaduck.com/svg2pdf_with_fpdf
  6. // http://rhodopsin.blogspot.com
  7. //
  8. // cette class etendue est open source, toute modification devra cependant etre repertoriée~
  9. // NB UNITS - Works in pixels as main units - converting to PDF units when outputing to PDF string
  10. // and on returning size
  11. class SVG {
  12. var $svg_gradient; // array - contient les infos sur les gradient fill du svg classé par id du svg
  13. var $svg_shadinglist; // array - contient les ids des objet shading
  14. var $svg_info; // array contenant les infos du svg voulue par l'utilisateur
  15. var $svg_attribs; // array - holds all attributes of root <svg> tag
  16. var $svg_style; // array contenant les style de groupes du svg
  17. var $svg_string; // String contenant le tracage du svg en lui męme.
  18. var $txt_data; // array - holds string info to write txt to image
  19. var $txt_style; // array - current text style
  20. var $mpdf_ref;
  21. var $xbase; // mPDF 4.4.003
  22. var $ybase; // mPDF 4.4.003
  23. var $svg_error; // mPDF 4.4.003
  24. var $subPathInit; // mPDF 4.4.003
  25. var $spxstart; // mPDF 4.4.003
  26. var $spystart; // mPDF 4.4.003
  27. var $kp; // mPDF 4.4.003 convert pixels to PDF units
  28. var $pathBBox; // mPDF 5.0.039
  29. function SVG(&$mpdf){
  30. $this->svg_gradient = array();
  31. $this->svg_shadinglist = array();
  32. $this->txt_data = array();
  33. $this->svg_string = '';
  34. $this->svg_info = array();
  35. $this->svg_attribs = array();
  36. $this->xbase = 0;
  37. $this->ybase = 0;
  38. $this->svg_error = false;
  39. $this->subPathInit = false; // mPDF 4.4.003
  40. $this->dashesUsed = false; // mPDF 5.0
  41. $this->mpdf_ref =& $mpdf;
  42. $this->kp = 72 / $mpdf->img_dpi; // mPDF 4.4.003 constant To convert pixels to pts/PDF units
  43. $this->kf = 1; // mPDF 5.0.039 constant To convert font size if re-mapped
  44. $this->pathBBox = array(); // mPDF 5.0.039
  45. $this->svg_style = array(
  46. array(
  47. 'fill' => 'black', // mPDF 4.4.008
  48. 'fill-opacity' => 1, // remplissage opaque par defaut
  49. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  50. 'stroke' => 'none', // pas de trait par defaut
  51. 'stroke-linecap' => 'butt', // style de langle par defaut
  52. 'stroke-linejoin' => 'miter', //
  53. 'stroke-miterlimit' => 4, // limite de langle par defaut
  54. 'stroke-opacity' => 1, // trait opaque par defaut
  55. 'stroke-width' => 1, // mPDF 4.4.011
  56. 'stroke-dasharray' => 0, // mPDF 4.4.003
  57. 'stroke-dashoffset' => 0, // mPDF 4.4.003
  58. 'color' => '' // mPDF 4.4.005
  59. )
  60. );
  61. $this->txt_style = array(
  62. array(
  63. 'fill' => 'black', // pas de remplissage par defaut
  64. 'font-family' => $mpdf->default_font,
  65. 'font-size' => $mpdf->default_font_size, // ****** this is pts
  66. 'font-weight' => 'normal', // normal | bold
  67. 'font-style' => 'normal', // italic | normal
  68. 'text-anchor' => 'start', // alignment: start, middle, end
  69. /* mPDF 5.0.041 */
  70. 'fill-opacity' => 1, // remplissage opaque par defaut
  71. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  72. 'stroke' => 'none', // pas de trait par defaut
  73. 'stroke-opacity' => 1, // trait opaque par defaut
  74. 'stroke-width' => 1, // mPDF 4.4.011
  75. 'color' => '' // mPDF 4.4.005
  76. )
  77. );
  78. }
  79. function svgGradient($gradient_info, $attribs, $element){
  80. $n = count($this->mpdf_ref->gradients)+1;
  81. // Get bounding dimensions of element
  82. $w = 100;
  83. $h = 100;
  84. $x_offset = 0;
  85. $y_offset = 0;
  86. if ($element=='rect') {
  87. $w = $attribs['width'];
  88. $h = $attribs['height'];
  89. $x_offset = $attribs['x'];
  90. $y_offset = $attribs['y'];
  91. }
  92. else if ($element=='ellipse') {
  93. $w = $attribs['rx']*2;
  94. $h = $attribs['ry']*2;
  95. $x_offset = $attribs['cx']-$attribs['rx'];
  96. $y_offset = $attribs['cy']-$attribs['ry'];
  97. }
  98. else if ($element=='circle') {
  99. $w = $attribs['r']*2;
  100. $h = $attribs['r']*2;
  101. $x_offset = $attribs['cx']-$attribs['r'];
  102. $y_offset = $attribs['cy']-$attribs['r'];
  103. }
  104. else if ($element=='polygon') {
  105. $pts = preg_split('/[ ,]+/', trim($attribs['points']));
  106. $maxr=$maxb=0;
  107. $minl=$mint=999999;
  108. for ($i=0;$i<count($pts); $i++) {
  109. if ($i % 2 == 0) { // x values
  110. $minl = min($minl,$pts[$i]);
  111. $maxr = max($maxr,$pts[$i]);
  112. }
  113. else { // y values
  114. $mint = min($mint,$pts[$i]);
  115. $maxb = max($maxb,$pts[$i]);
  116. }
  117. }
  118. $w = $maxr-$minl;
  119. $h = $maxb-$mint;
  120. $x_offset = $minl;
  121. $y_offset = $mint;
  122. }
  123. else if ($element=='path') {
  124. // mPDF 5.0.039
  125. if (is_array($this->pathBBox) && $this->pathBBox[2]>0) {
  126. $w = $this->pathBBox[2];
  127. $h = $this->pathBBox[3];
  128. $x_offset = $this->pathBBox[0];
  129. $y_offset = $this->pathBBox[1];
  130. }
  131. else {
  132. preg_match_all('/([a-z]|[A-Z])([ ,\-.\d]+)*/', $attribs['d'], $commands, PREG_SET_ORDER);
  133. $maxr=$maxb=0;
  134. $minl=$mint=999999;
  135. foreach($commands as $c){
  136. if(count($c)==3){
  137. list($tmp, $cmd, $arg) = $c;
  138. if ($cmd=='M' || $cmd=='L' || $cmd=='C' || $cmd=='S' || $cmd=='Q' || $cmd=='T') {
  139. $pts = preg_split('/[ ,]+/', trim($arg));
  140. for ($i=0;$i<count($pts); $i++) {
  141. if ($i % 2 == 0) { // x values
  142. $minl = min($minl,$pts[$i]);
  143. $maxr = max($maxr,$pts[$i]);
  144. }
  145. else { // y values
  146. $mint = min($mint,$pts[$i]);
  147. $maxb = max($maxb,$pts[$i]);
  148. }
  149. }
  150. }
  151. if ($cmd=='H') { // sets new x
  152. $minl = min($minl,$arg);
  153. $maxr = max($maxr,$arg);
  154. }
  155. if ($cmd=='V') { // sets new y
  156. $mint = min($mint,$arg);
  157. $maxb = max($maxb,$arg);
  158. }
  159. }
  160. }
  161. $w = $maxr-$minl;
  162. $h = $maxb-$mint;
  163. $x_offset = $minl;
  164. $y_offset = $mint;
  165. }
  166. }
  167. if (!$w || $w==-999999) { $w = 100; }
  168. if (!$h || $h==-999999) { $h = 100; }
  169. if ($x_offset==999999) { $x_offset = 0; }
  170. if ($y_offset==999999) { $y_offset = 0; }
  171. // mPDF 4.5.010
  172. // TRANSFORMATIONS
  173. $transformations = '';
  174. if (isset($gradient_info['transform'])){
  175. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$gradient_info['transform'],$m);
  176. if (count($m[0])) {
  177. for($i=0; $i<count($m[0]); $i++) {
  178. $c = strtolower($m[1][$i]);
  179. $v = trim($m[2][$i]);
  180. $vv = preg_split('/[ ,]+/',$v);
  181. if ($c=='matrix' && count($vv)==6) {
  182. // mPDF 5.0.039
  183. // Note angle of rotation is reversed (from SVG to PDF), so vv[1] and vv[2] are negated
  184. // cf svgDefineStyle()
  185. $transformations .= sprintf(' %.3F %.3F %.3F %.3F %.3F %.3F cm ', $vv[0], -$vv[1], -$vv[2], $vv[3], $vv[4]*$this->kp, -$vv[5]*$this->kp);
  186. }
  187. else if ($c=='translate' && count($vv)) {
  188. $tm[4] = $vv[0];
  189. if (count($vv)==2) { $t_y = -$vv[1]; }
  190. else { $t_y = 0; }
  191. $tm[5] = $t_y;
  192. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  193. }
  194. else if ($c=='scale' && count($vv)) {
  195. if (count($vv)==2) { $s_y = $vv[1]; }
  196. else { $s_y = $vv[0]; }
  197. $tm[0] = $vv[0];
  198. $tm[3] = $s_y;
  199. $transformations .= sprintf(' %.3F 0 0 %.3F 0 0 cm ', $tm[0], $tm[3]);
  200. }
  201. else if ($c=='rotate' && count($vv)) {
  202. $tm[0] = cos(deg2rad(-$vv[0]));
  203. $tm[1] = sin(deg2rad(-$vv[0]));
  204. $tm[2] = -$tm[1];
  205. $tm[3] = $tm[0];
  206. if (count($vv)==3) {
  207. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  208. }
  209. $transformations .= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  210. if (count($vv)==3) {
  211. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  212. }
  213. }
  214. else if ($c=='skewx' && count($vv)) {
  215. $tm[2] = tan(deg2rad(-$vv[0]));
  216. $transformations .= sprintf(' 1 0 %.3F 1 0 0 cm ', $tm[2]);
  217. }
  218. else if ($c=='skewy' && count($vv)) {
  219. $tm[1] = tan(deg2rad(-$vv[0]));
  220. $transformations .= sprintf(' 1 %.3F 0 1 0 0 cm ', $tm[1]);
  221. }
  222. }
  223. }
  224. }
  225. $return = "";
  226. // mPDF 5.0.039
  227. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  228. if ($transformations) { $return .= $transformations; }
  229. }
  230. // mPDF 5.0.040
  231. $spread = 'P'; // pad
  232. if (isset($gradient_info['spread'])) {
  233. if (strtolower($gradient_info['spread'])=='reflect') { $spread = 'F'; } // reflect
  234. else if (strtolower($gradient_info['spread'])=='repeat') { $spread = 'R'; } // repeat
  235. }
  236. for ($i=0; $i<(count($gradient_info['color'])); $i++) {
  237. if (stristr($gradient_info['color'][$i]['offset'], '%')!== false) { $gradient_info['color'][$i]['offset'] = ($gradient_info['color'][$i]['offset']+0)/100; }
  238. if (stristr($gradient_info['color'][($i+1)]['offset'], '%')!== false) { $gradient_info['color'][($i+1)]['offset'] = ($gradient_info['color'][($i+1)]['offset']+0)/100; }
  239. if ($gradient_info['color'][$i]['offset']<0) { $gradient_info['color'][$i]['offset'] = 0; }
  240. if ($gradient_info['color'][$i]['offset']>1) { $gradient_info['color'][$i]['offset'] = 1; }
  241. if ($i>0) {
  242. if ($gradient_info['color'][$i]['offset']<$gradient_info['color'][($i-1)]['offset']) {
  243. $gradient_info['color'][$i]['offset']=$gradient_info['color'][($i-1)]['offset'];
  244. }
  245. }
  246. }
  247. if ($gradient_info['color'][0]['offset']>0) {
  248. array_unshift($gradient_info['color'], $gradient_info['color'][0]);
  249. $gradient_info['color'][0]['offset'] = 0;
  250. }
  251. $ns = count($gradient_info['color']);
  252. if ($gradient_info['color'][($ns-1)]['offset']<1) {
  253. $gradient_info['color'][] = $gradient_info['color'][($ns-1)];
  254. $gradient_info['color'][($ns)]['offset'] = 1;
  255. }
  256. $ns = count($gradient_info['color']);
  257. if ($gradient_info['type'] == 'linear'){
  258. // mPDF 4.4.003
  259. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  260. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  261. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  262. if (isset($gradient_info['info']['x2'])) { $gradient_info['info']['x2'] = ($gradient_info['info']['x2']-$x_offset) / $w; }
  263. if (isset($gradient_info['info']['y2'])) { $gradient_info['info']['y2'] = ($gradient_info['info']['y2']-$y_offset) / $h; }
  264. }
  265. if (isset($gradient_info['info']['x1'])) { $x1 = $gradient_info['info']['x1']; }
  266. else { $x1 = 0; }
  267. if (isset($gradient_info['info']['y1'])) { $y1 = $gradient_info['info']['y1']; }
  268. else { $y1 = 0; }
  269. if (isset($gradient_info['info']['x2'])) { $x2 = $gradient_info['info']['x2']; }
  270. else { $x2 = 1; }
  271. if (isset($gradient_info['info']['y2'])) { $y2 = $gradient_info['info']['y2']; }
  272. else { $y2 = 0; }
  273. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  274. if (stristr($x2, '%')!== false) { $x2 = ($x2+0)/100; }
  275. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  276. if (stristr($y2, '%')!== false) { $y2 = ($y2+0)/100; }
  277. // mPDF 5.0.042
  278. $bboxw = $w;
  279. $bboxh = $h;
  280. $usex = $x_offset;
  281. $usey = $y_offset;
  282. $usew = $bboxw;
  283. $useh = $bboxh;
  284. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  285. $angle = rad2deg(atan2(($gradient_info['info']['y2']-$gradient_info['info']['y1']), ($gradient_info['info']['x2']-$gradient_info['info']['x1'])));
  286. if ($angle < 0) { $angle += 360; }
  287. else if ($angle > 360) { $angle -= 360; }
  288. if ($angle!=0 && $angle!=360 && $angle!=90 && $angle!=180 && $angle!=270) {
  289. if ($w >= $h) {
  290. $y1 *= $h/$w ;
  291. $y2 *= $h/$w ;
  292. $usew = $useh = $bboxw;
  293. }
  294. else {
  295. $x1 *= $w/$h ;
  296. $x2 *= $w/$h ;
  297. $usew = $useh = $bboxh;
  298. }
  299. }
  300. }
  301. $a = $usew; // width
  302. $d = -$useh; // height
  303. $e = $usex; // x- offset
  304. $f = -$usey; // -y-offset
  305. $return .= sprintf('%.3F 0 0 %.3F %.3F %.3F cm ', $a*$this->kp, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  306. // mPDF 5.0.039
  307. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='objectboundingbox') {
  308. if ($transformations) { $return .= $transformations; }
  309. }
  310. // mPDF 5.0.020
  311. $trans = false;
  312. // mPDF 5.0.040
  313. if ($spread=='R' || $spread=='F') { // Repeat / Reflect
  314. $offs = array();
  315. for($i=0;$i<$ns;$i++) {
  316. $offs[$i] = $gradient_info['color'][$i]['offset'];
  317. }
  318. $gp = 0;
  319. $inside=true;
  320. while($inside) {
  321. $gp++;
  322. for($i=0;$i<$ns;$i++) {
  323. if ($spread=='F' && ($gp % 2) == 1) { // Reflect
  324. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][(($ns*($gp-1))+($ns-$i-1))];
  325. $tmp = $gp+(1-$offs[($ns-$i-1)]) ;
  326. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  327. }
  328. else { // Reflect
  329. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][$i];
  330. $tmp = $gp+$offs[$i] ;
  331. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  332. }
  333. // IF STILL INSIDE BOX OR STILL VALID
  334. // Point on axis to test
  335. $px1 = $x1 + ($x2-$x1)*$tmp;
  336. $py1 = $y1 + ($y2-$y1)*$tmp;
  337. // Get perpendicular axis
  338. $alpha = atan2($y2-$y1, $x2-$x1);
  339. $alpha += M_PI/2; // rotate 90 degrees
  340. // Get arbitrary point to define line perpendicular to axis
  341. $px2 = $px1+cos($alpha);
  342. $py2 = $py1+sin($alpha);
  343. $res1 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 0, 1); // $x=0 vert axis
  344. $res2 = _testIntersect($px1, $py1, $px2, $py2, 1, 0, 1, 1); // $x=1 vert axis
  345. $res3 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 1, 0); // $y=0 horiz axis
  346. $res4 = _testIntersect($px1, $py1, $px2, $py2, 0, 1, 1, 1); // $y=1 horiz axis
  347. if (!$res1 && !$res2 && !$res3 && !$res4) { $inside = false; }
  348. }
  349. }
  350. $inside=true;
  351. $gp = 0;
  352. while($inside) {
  353. $gp++;
  354. $newarr = array();
  355. for($i=0;$i<$ns;$i++) {
  356. if ($spread=='F') { // Reflect
  357. $newarr[$i] = $gradient_info['color'][($ns-$i-1)];
  358. if (($gp % 2) == 1) {
  359. $tmp = -$gp+(1-$offs[($ns-$i-1)]);
  360. $newarr[$i]['offset'] = $tmp;
  361. }
  362. else {
  363. $tmp = -$gp+$offs[$i];
  364. $newarr[$i]['offset'] = $tmp;
  365. }
  366. }
  367. else { // Reflect
  368. $newarr[$i] = $gradient_info['color'][$i];
  369. $tmp = -$gp+$offs[$i];
  370. $newarr[$i]['offset'] = $tmp;
  371. }
  372. // IF STILL INSIDE BOX OR STILL VALID
  373. // Point on axis to test
  374. $px1 = $x1 + ($x2-$x1)*$tmp;
  375. $py1 = $y1 + ($y2-$y1)*$tmp;
  376. // Get perpendicular axis
  377. $alpha = atan2($y2-$y1, $x2-$x1);
  378. $alpha += M_PI/2; // rotate 90 degrees
  379. // Get arbitrary point to define line perpendicular to axis
  380. $px2 = $px1+cos($alpha);
  381. $py2 = $py1+sin($alpha);
  382. $res1 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 0, 1); // $x=0 vert axis
  383. $res2 = _testIntersect($px1, $py1, $px2, $py2, 1, 0, 1, 1); // $x=1 vert axis
  384. $res3 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 1, 0); // $y=0 horiz axis
  385. $res4 = _testIntersect($px1, $py1, $px2, $py2, 0, 1, 1, 1); // $y=1 horiz axis
  386. if (!$res1 && !$res2 && !$res3 && !$res4) { $inside = false; }
  387. }
  388. for($i=($ns-1);$i>=0;$i--) {
  389. if (isset($newarr[$i]['offset'])) array_unshift($gradient_info['color'], $newarr[$i]);
  390. }
  391. }
  392. }
  393. // mPDF 4.4.007 Gradient STOPs
  394. $stops = count($gradient_info['color']);
  395. if ($stops < 2) { return ''; }
  396. // mPDF 5.0.042
  397. $range = $gradient_info['color'][count($gradient_info['color'])-1]['offset']-$gradient_info['color'][0]['offset'];
  398. $min = $gradient_info['color'][0]['offset'];
  399. for ($i=0; $i<($stops); $i++) {
  400. // mPDF 5.0.051
  401. if (!$gradient_info['color'][$i]['color']) {
  402. if ($gradient_info['colorspace']=='RGB') $gradient_info['color'][$i]['color'] = '0 0 0';
  403. else if ($gradient_info['colorspace']=='Gray') $gradient_info['color'][$i]['color'] = '0';
  404. else if ($gradient_info['colorspace']=='CMYK') $gradient_info['color'][$i]['color'] = '1 1 1 1';
  405. }
  406. $offset = ($gradient_info['color'][$i]['offset'] - $min)/$range;
  407. $this->mpdf_ref->gradients[$n]['stops'][] = array(
  408. 'col' => $gradient_info['color'][$i]['color'],
  409. 'opacity' => $gradient_info['color'][$i]['opacity'],
  410. 'offset' => $offset);
  411. if ($gradient_info['color'][$i]['opacity']<1) { $trans = true; }
  412. }
  413. $grx1 = $x1 + ($x2-$x1)*$gradient_info['color'][0]['offset'];
  414. $gry1 = $y1 + ($y2-$y1)*$gradient_info['color'][0]['offset'];
  415. $grx2 = $x1 + ($x2-$x1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  416. $gry2 = $y1 + ($y2-$y1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  417. $this->mpdf_ref->gradients[$n]['coords']=array($grx1, $gry1, $grx2, $gry2);
  418. $this->mpdf_ref->gradients[$n]['colorspace'] = $gradient_info['colorspace']; // mPDF 5.0.051
  419. $this->mpdf_ref->gradients[$n]['type'] = 2;
  420. $this->mpdf_ref->gradients[$n]['fo'] = true;
  421. $this->mpdf_ref->gradients[$n]['extend']=array('true','true');
  422. if ($trans) {
  423. $this->mpdf_ref->gradients[$n]['trans'] = true;
  424. $return .= ' /TGS'.($n).' gs ';
  425. }
  426. $return .= ' /Sh'.($n).' sh ';
  427. $return .= " Q\n";
  428. }
  429. else if ($gradient_info['type'] == 'radial'){
  430. // mPDF 4.4.003
  431. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  432. if ($w > $h) { $h = $w; }
  433. else { $w = $h; }
  434. if (isset($gradient_info['info']['x0'])) { $gradient_info['info']['x0'] = ($gradient_info['info']['x0']-$x_offset) / $w; }
  435. if (isset($gradient_info['info']['y0'])) { $gradient_info['info']['y0'] = ($gradient_info['info']['y0']-$y_offset) / $h; }
  436. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  437. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  438. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['rx'] = $gradient_info['info']['r'] / $w; }
  439. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['ry'] = $gradient_info['info']['r'] / $h; }
  440. }
  441. if ($gradient_info['info']['x0'] || $gradient_info['info']['x0']===0) { $x0 = $gradient_info['info']['x0']; }
  442. else { $x0 = 0.5; }
  443. if ($gradient_info['info']['y0'] || $gradient_info['info']['y0']===0) { $y0 = $gradient_info['info']['y0']; }
  444. else { $y0 = 0.5; }
  445. if ($gradient_info['info']['rx'] || $gradient_info['info']['rx']===0) { $rx = $gradient_info['info']['rx']; }
  446. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $rx = $gradient_info['info']['r']; }
  447. else { $rx = 0.5; }
  448. if ($gradient_info['info']['ry'] || $gradient_info['info']['ry']===0) { $ry = $gradient_info['info']['ry']; }
  449. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $ry = $gradient_info['info']['r']; }
  450. else { $ry = 0.5; }
  451. if ($gradient_info['info']['x1'] || $gradient_info['info']['x1']===0) { $x1 = $gradient_info['info']['x1']; }
  452. else { $x1 = $x0; }
  453. if ($gradient_info['info']['y1'] || $gradient_info['info']['y1']===0) { $y1 = $gradient_info['info']['y1']; }
  454. else { $y1 = $y0; }
  455. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  456. if (stristr($x0, '%')!== false) { $x0 = ($x0+0)/100; }
  457. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  458. if (stristr($y0, '%')!== false) { $y0 = ($y0+0)/100; }
  459. if (stristr($rx, '%')!== false) { $rx = ($rx+0)/100; }
  460. if (stristr($ry, '%')!== false) { $ry = ($ry+0)/100; }
  461. // mPDF 5.0.043
  462. $bboxw = $w;
  463. $bboxh = $h;
  464. $usex = $x_offset;
  465. $usey = $y_offset;
  466. $usew = $bboxw;
  467. $useh = $bboxh;
  468. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  469. $angle = rad2deg(atan2(($gradient_info['info']['y0']-$gradient_info['info']['y1']), ($gradient_info['info']['x0']-$gradient_info['info']['x1'])));
  470. if ($angle < 0) { $angle += 360; }
  471. else if ($angle > 360) { $angle -= 360; }
  472. if ($angle!=0 && $angle!=360 && $angle!=90 && $angle!=180 && $angle!=270) {
  473. if ($w >= $h) {
  474. $y1 *= $h/$w ;
  475. $y0 *= $h/$w ;
  476. $rx *= $h/$w ;
  477. $ry *= $h/$w ;
  478. $usew = $useh = $bboxw;
  479. }
  480. else {
  481. $x1 *= $w/$h ;
  482. $x0 *= $w/$h ;
  483. $rx *= $w/$h ;
  484. $ry *= $w/$h ;
  485. $usew = $useh = $bboxh;
  486. }
  487. }
  488. }
  489. $a = $usew; // width
  490. $d = -$useh; // height
  491. $e = $usex; // x- offset
  492. $f = -$usey; // -y-offset
  493. $r = $rx;
  494. $return .= sprintf('%.3F 0 0 %.3F %.3F %.3F cm ', $a*$this->kp, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  495. // mPDF 5.0.039
  496. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='objectboundingbox') {
  497. if ($transformations) { $return .= $transformations; }
  498. }
  499. // x1 and y1 (fx, fy) should be inside the circle defined by x0 y0 and r else error in mPDF
  500. while (pow(($x1-$x0),2) + pow(($y1 - $y0),2) >= pow($r,2)) { $r += 0.05; }
  501. // mPDF 5.0.040
  502. if ($spread=='R' || $spread=='F') { // Repeat / Reflect
  503. $offs = array();
  504. for($i=0;$i<$ns;$i++) {
  505. $offs[$i] = $gradient_info['color'][$i]['offset'];
  506. }
  507. $gp = 0;
  508. $inside=true;
  509. while($inside) {
  510. $gp++;
  511. for($i=0;$i<$ns;$i++) {
  512. if ($spread=='F' && ($gp % 2) == 1) { // Reflect
  513. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][(($ns*($gp-1))+($ns-$i-1))];
  514. $tmp = $gp+(1-$offs[($ns-$i-1)]) ;
  515. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  516. }
  517. else { // Reflect
  518. $gradient_info['color'][(($ns*$gp)+$i)] = $gradient_info['color'][$i];
  519. $tmp = $gp+$offs[$i] ;
  520. $gradient_info['color'][(($ns*$gp)+$i)]['offset'] = $tmp;
  521. }
  522. // IF STILL INSIDE BOX OR STILL VALID
  523. // TEST IF circle (perimeter) intersects with
  524. // or is enclosed
  525. // Point on axis to test
  526. $px = $x1 + ($x0-$x1)*$tmp;
  527. $py = $y1 + ($y0-$y1)*$tmp;
  528. $pr = $r*$tmp;
  529. $res = _testIntersectCircle($px, $py, $pr);
  530. if (!$res) { $inside = false; }
  531. }
  532. }
  533. }
  534. // mPDF 4.4.007 Gradient STOPs
  535. $stops = count($gradient_info['color']);
  536. if ($stops < 2) { return ''; }
  537. // mPDF 5.0.043
  538. $range = $gradient_info['color'][count($gradient_info['color'])-1]['offset']-$gradient_info['color'][0]['offset'];
  539. $min = $gradient_info['color'][0]['offset'];
  540. for ($i=0; $i<($stops); $i++) {
  541. // mPDF 5.0.051
  542. if (!$gradient_info['color'][$i]['color']) {
  543. if ($gradient_info['colorspace']=='RGB') $gradient_info['color'][$i]['color'] = '0 0 0';
  544. else if ($gradient_info['colorspace']=='Gray') $gradient_info['color'][$i]['color'] = '0';
  545. else if ($gradient_info['colorspace']=='CMYK') $gradient_info['color'][$i]['color'] = '1 1 1 1';
  546. }
  547. $offset = ($gradient_info['color'][$i]['offset'] - $min)/$range;
  548. $this->mpdf_ref->gradients[$n]['stops'][] = array(
  549. 'col' => $gradient_info['color'][$i]['color'],
  550. 'opacity' => $gradient_info['color'][$i]['opacity'],
  551. 'offset' => $offset);
  552. if ($gradient_info['color'][$i]['opacity']<1) { $trans = true; }
  553. }
  554. $grx1 = $x1 + ($x0-$x1)*$gradient_info['color'][0]['offset'];
  555. $gry1 = $y1 + ($y0-$y1)*$gradient_info['color'][0]['offset'];
  556. $grx2 = $x1 + ($x0-$x1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  557. $gry2 = $y1 + ($y0-$y1)*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  558. $grir = $r*$gradient_info['color'][0]['offset'];
  559. $grr = $r*$gradient_info['color'][count($gradient_info['color'])-1]['offset'];
  560. $this->mpdf_ref->gradients[$n]['coords']=array($grx1, $gry1, $grx2, $gry2, abs($grr), abs($grir) );
  561. $grx1 = $x1 + ($x0-$x1)*$first_stop;
  562. $gry1 = $y1 + ($y0-$y1)*$first_stop;
  563. $grx2 = $x1 + ($x0-$x1)*$last_stop;
  564. $gry2 = $y1 + ($y0-$y1)*$last_stop;
  565. $grir = $r*$first_stop;
  566. $grr = $r*$last_stop;
  567. $this->mpdf_ref->gradients[$n]['colorspace'] = $gradient_info['colorspace']; // mPDF 5.0.051
  568. $this->mpdf_ref->gradients[$n]['type'] = 3;
  569. $this->mpdf_ref->gradients[$n]['fo'] = true;
  570. $this->mpdf_ref->gradients[$n]['extend']=array('true','true');
  571. if ($trans) {
  572. $this->mpdf_ref->gradients[$n]['trans'] = true;
  573. $return .= ' /TGS'.($n).' gs ';
  574. }
  575. $return .= ' /Sh'.($n).' sh ';
  576. $return .= " Q\n";
  577. }
  578. return $return;
  579. }
  580. function svgOffset ($attribs){
  581. // save all <svg> tag attributes
  582. $this->svg_attribs = $attribs;
  583. if(isset($this->svg_attribs['viewBox'])) {
  584. $vb = preg_split('/\s+/is', trim($this->svg_attribs['viewBox']));
  585. if (count($vb)==4) {
  586. $this->svg_info['x'] = $vb[0];
  587. $this->svg_info['y'] = $vb[1];
  588. $this->svg_info['w'] = $vb[2];
  589. $this->svg_info['h'] = $vb[3];
  590. // return; // mPDF 5.0.005
  591. }
  592. }
  593. $svg_w = $this->mpdf_ref->ConvertSize($attribs['width']); // mm (interprets numbers as pixels)
  594. $svg_h = $this->mpdf_ref->ConvertSize($attribs['height']); // mm
  595. ///*
  596. // mPDF 5.0.005
  597. if ($this->svg_info['w']) { // if 'w' set by viewBox
  598. if ($svg_w) { // if width also set, use these values to determine to set size of "pixel"
  599. $this->kp *= ($svg_w/0.2645) / $this->svg_info['w'];
  600. $this->kf = ($svg_w/0.2645) / $this->svg_info['w']; // mPDF 5.0.039
  601. }
  602. else if ($svg_h) {
  603. $this->kp *= ($svg_h/0.2645) / $this->svg_info['h'];
  604. $this->kf = ($svg_h/0.2645) / $this->svg_info['h']; // mPDF 5.0.039
  605. }
  606. return;
  607. }
  608. //*/
  609. // Added to handle file without height or width specified
  610. if (!$svg_w && !$svg_h) { $svg_w = $svg_h = $this->mpdf_ref->blk[$this->mpdf_ref->blklvl]['inner_width'] ; } // DEFAULT
  611. if (!$svg_w) { $svg_w = $svg_h; }
  612. if (!$svg_h) { $svg_h = $svg_w; }
  613. $this->svg_info['x'] = 0;
  614. $this->svg_info['y'] = 0;
  615. $this->svg_info['w'] = $svg_w/0.2645; // mm->pixels
  616. $this->svg_info['h'] = $svg_h/0.2645; // mm->pixels
  617. }
  618. //
  619. // check if points are within svg, if not, set to max
  620. function svg_overflow($x,$y)
  621. {
  622. $x2 = $x;
  623. $y2 = $y;
  624. if(isset($this->svg_attribs['overflow']))
  625. {
  626. if($this->svg_attribs['overflow'] == 'hidden')
  627. {
  628. // Not sure if this is supposed to strip off units, but since I dont use any I will omlt this step
  629. $svg_w = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['width']);
  630. $svg_h = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['height']);
  631. // $xmax = floor($this->svg_attribs['width']);
  632. $xmax = floor($svg_w);
  633. $xmin = 0;
  634. // $ymax = floor(($this->svg_attribs['height'] * -1));
  635. $ymax = floor(($svg_h * -1));
  636. $ymin = 0;
  637. if($x > $xmax) $x2 = $xmax; // right edge
  638. if($x < $xmin) $x2 = $xmin; // left edge
  639. if($y < $ymax) $y2 = $ymax; // bottom
  640. if($y > $ymin) $y2 = $ymin; // top
  641. }
  642. }
  643. return array( 'x' => $x2, 'y' => $y2);
  644. }
  645. function svgDefineStyle($critere_style){
  646. $tmp = count($this->svg_style)-1;
  647. $current_style = $this->svg_style[$tmp];
  648. unset($current_style['transformations']);
  649. // TRANSFORM SCALE
  650. $transformations = '';
  651. if (isset($critere_style['transform'])){
  652. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$critere_style['transform'],$m);
  653. if (count($m[0])) {
  654. for($i=0; $i<count($m[0]); $i++) {
  655. $c = strtolower($m[1][$i]);
  656. $v = trim($m[2][$i]);
  657. $vv = preg_split('/[ ,]+/',$v);
  658. if ($c=='matrix' && count($vv)==6) {
  659. // mPDF 5.0.039
  660. // Note angle of rotation is reversed (from SVG to PDF), so vv[1] and vv[2] are negated
  661. $transformations .= sprintf(' %.3F %.3F %.3F %.3F %.3F %.3F cm ', $vv[0], -$vv[1], -$vv[2], $vv[3], $vv[4]*$this->kp, -$vv[5]*$this->kp);
  662. /*
  663. // The long way of doing this??
  664. // need to reverse angle of rotation from SVG to PDF
  665. $sx=sqrt(pow($vv[0],2)+pow($vv[2],2));
  666. if ($vv[0] < 0) { $sx *= -1; } // change sign
  667. $sy=sqrt(pow($vv[1],2)+pow($vv[3],2));
  668. if ($vv[3] < 0) { $sy *= -1; } // change sign
  669. // rotation angle is
  670. $t=atan2($vv[1],$vv[3]);
  671. $t=atan2(-$vv[2],$vv[0]); // Should be the same value or skew has been applied
  672. // Reverse angle
  673. $t *= -1;
  674. // Rebuild matrix
  675. $ma = $sx * cos($t);
  676. $mb = $sy * sin($t);
  677. $mc = -$sx * sin($t);
  678. $md = $sy * cos($t);
  679. // $transformations .= sprintf(' %.3F %.3F %.3F %.3F %.3F %.3F cm ', $ma, $mb, $mc, $md, $vv[4]*$this->kp, -$vv[5]*$this->kp);
  680. */
  681. }
  682. else if ($c=='translate' && count($vv)) {
  683. $tm[4] = $vv[0];
  684. if (count($vv)==2) { $t_y = -$vv[1]; }
  685. else { $t_y = 0; }
  686. $tm[5] = $t_y;
  687. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  688. }
  689. else if ($c=='scale' && count($vv)) {
  690. if (count($vv)==2) { $s_y = $vv[1]; }
  691. else { $s_y = $vv[0]; }
  692. $tm[0] = $vv[0];
  693. $tm[3] = $s_y;
  694. $transformations .= sprintf(' %.3F 0 0 %.3F 0 0 cm ', $tm[0], $tm[3]);
  695. }
  696. else if ($c=='rotate' && count($vv)) {
  697. $tm[0] = cos(deg2rad(-$vv[0]));
  698. $tm[1] = sin(deg2rad(-$vv[0]));
  699. $tm[2] = -$tm[1];
  700. $tm[3] = $tm[0];
  701. if (count($vv)==3) {
  702. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  703. }
  704. $transformations .= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  705. if (count($vv)==3) {
  706. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  707. }
  708. }
  709. else if ($c=='skewx' && count($vv)) {
  710. $tm[2] = tan(deg2rad(-$vv[0]));
  711. $transformations .= sprintf(' 1 0 %.3F 1 0 0 cm ', $tm[2]);
  712. }
  713. else if ($c=='skewy' && count($vv)) {
  714. $tm[1] = tan(deg2rad(-$vv[0]));
  715. $transformations .= sprintf(' 1 %.3F 0 1 0 0 cm ', $tm[1]);
  716. }
  717. }
  718. }
  719. $current_style['transformations'] = $transformations;
  720. }
  721. if (isset($critere_style['style'])){
  722. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  723. $current_style['fill'] = '#'.str_pad(dechex($m[1]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[2]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[3]), 2, "0", STR_PAD_LEFT);
  724. }
  725. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']); // mPDF 4.4.003
  726. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  727. }
  728. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  729. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  730. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  731. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  732. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  733. $current_style['stroke'] = '#'.str_pad(dechex($m[1]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[2]), 2, "0", STR_PAD_LEFT).str_pad(dechex($m[3]), 2, "0", STR_PAD_LEFT);
  734. }
  735. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  736. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  737. }
  738. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  739. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  740. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  741. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  742. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  743. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  744. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  745. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  746. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  747. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  748. // mPDF 4.4.003
  749. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  750. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  751. // mPDF 4.4.003
  752. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  753. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  754. }
  755. if(isset($critere_style['fill'])){
  756. $current_style['fill'] = $critere_style['fill'];
  757. }
  758. if(isset($critere_style['fill-opacity'])){
  759. $current_style['fill-opacity'] = $critere_style['fill-opacity'];
  760. }
  761. if(isset($critere_style['fill-rule'])){
  762. $current_style['fill-rule'] = $critere_style['fill-rule'];
  763. }
  764. if(isset($critere_style['stroke'])){
  765. $current_style['stroke'] = $critere_style['stroke'];
  766. }
  767. if(isset($critere_style['stroke-linecap'])){
  768. $current_style['stroke-linecap'] = $critere_style['stroke-linecap'];
  769. }
  770. if(isset($critere_style['stroke-linejoin'])){
  771. $current_style['stroke-linejoin'] = $critere_style['stroke-linejoin'];
  772. }
  773. if(isset($critere_style['stroke-miterlimit'])){
  774. $current_style['stroke-miterlimit'] = $critere_style['stroke-miterlimit'];
  775. }
  776. if(isset($critere_style['stroke-opacity'])){
  777. $current_style['stroke-opacity'] = $critere_style['stroke-opacity'];
  778. }
  779. if(isset($critere_style['stroke-width'])){
  780. $current_style['stroke-width'] = $critere_style['stroke-width'];
  781. }
  782. // mPDF 4.4.003
  783. if(isset($critere_style['stroke-dasharray'])){
  784. $current_style['stroke-dasharray'] = $critere_style['stroke-dasharray'];
  785. }
  786. if(isset($critere_style['stroke-dashoffset'])){
  787. $current_style['stroke-dashoffset'] = $critere_style['stroke-dashoffset'];
  788. }
  789. // mPDF 4.4.005 Used as indirect setting for currentColor
  790. if(isset($critere_style['color']) && $critere_style['color'] != 'inherit'){
  791. $current_style['color'] = $critere_style['color'];
  792. }
  793. return $current_style;
  794. }
  795. //
  796. // Cette fonction ecrit le style dans le stream svg.
  797. function svgStyle($critere_style, $attribs, $element){
  798. $path_style = '';
  799. if (substr_count($critere_style['fill'],'url')>0){
  800. //
  801. // couleur degradé
  802. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['fill']);
  803. if ($id_gradient != $critere_style['fill']) {
  804. if (isset($this->svg_gradient[$id_gradient])) {
  805. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  806. if ($fill_gradient) { // mPDF 4.4.003
  807. $path_style = "q ";
  808. $w = "W";
  809. $style .= 'N';
  810. }
  811. }
  812. }
  813. }
  814. // mPDF 4.4.005 Used as indirect setting for currentColor
  815. else if (strtolower($critere_style['fill']) == 'currentcolor'){
  816. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  817. if ($col) {
  818. // mPDF 5.0.051
  819. // mPDF 5.3.74
  820. if ($col{0}==5) { $critere_style['fill-opacity'] = ord($col{4}/100); } // RGBa
  821. if ($col{0}==6) { $critere_style['fill-opacity'] = ord($col{5}/100); } // CMYKa
  822. $path_style .= $this->mpdf_ref->SetFColor($col, true).' '; // mPDF 5.0.051
  823. $style .= 'F';
  824. }
  825. }
  826. else if ($critere_style['fill'] != 'none'){
  827. $col = $this->mpdf_ref->ConvertColor($critere_style['fill']);
  828. if ($col) {
  829. // mPDF 5.0.051
  830. // mPDF 5.3.74
  831. if ($col{0}==5) { $critere_style['fill-opacity'] = ord($col{4}/100); } // RGBa
  832. if ($col{0}==6) { $critere_style['fill-opacity'] = ord($col{5}/100); } // CMYKa
  833. $path_style .= $this->mpdf_ref->SetFColor($col, true).' '; // mPDF 5.0.051
  834. $style .= 'F';
  835. }
  836. }
  837. // mPDF 5.0.040
  838. if (substr_count($critere_style['stroke'],'url')>0){
  839. /*
  840. // Cannot put a gradient on a "stroke" in PDF?
  841. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['stroke']);
  842. if ($id_gradient != $critere_style['stroke']) {
  843. if (isset($this->svg_gradient[$id_gradient])) {
  844. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  845. if ($fill_gradient) {
  846. $path_style = "q ";
  847. $w = "W";
  848. $style .= 'D';
  849. }
  850. }
  851. }
  852. */
  853. }
  854. // mPDF 4.4.005 Used as indirect setting for currentColor
  855. else if (strtolower($critere_style['stroke']) == 'currentcolor'){
  856. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  857. if ($col) {
  858. // mPDF 5.0.051
  859. // mPDF 5.3.74
  860. if ($col{0}==5) { $critere_style['stroke-opacity'] = ord($col{4}/100); } // RGBa
  861. if ($col{0}==6) { $critere_style['stroke-opacity'] = ord($col{5}/100); } // CMYKa
  862. $path_style .= $this->mpdf_ref->SetDColor($col, true).' '; // mPDF 5.0.051
  863. $style .= 'D';
  864. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']);
  865. $path_style .= sprintf('%.3F w ',$lw*$this->kp);
  866. }
  867. }
  868. else if ($critere_style['stroke'] != 'none'){
  869. $col = $this->mpdf_ref->ConvertColor($critere_style['stroke']);
  870. if ($col) {
  871. // mPDF 5.0.051
  872. // mPDF 5.3.74
  873. if ($col{0}==5) { $critere_style['stroke-opacity'] = ord($col{4}/100); } // RGBa
  874. if ($col{0}==6) { $critere_style['stroke-opacity'] = ord($col{5}/100); } // CMYKa
  875. $path_style .= $this->mpdf_ref->SetDColor($col, true).' '; // mPDF 5.0.051
  876. $style .= 'D';
  877. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']); // mPDF 4.4.003
  878. $path_style .= sprintf('%.3F w ',$lw*$this->kp);
  879. }
  880. }
  881. if ($critere_style['stroke'] != 'none'){
  882. if ($critere_style['stroke-linejoin'] == 'miter'){
  883. $path_style .= ' 0 j ';
  884. }
  885. else if ($critere_style['stroke-linejoin'] == 'round'){
  886. $path_style .= ' 1 j ';
  887. }
  888. else if ($critere_style['stroke-linejoin'] == 'bevel'){
  889. $path_style .= ' 2 j ';
  890. }
  891. if ($critere_style['stroke-linecap'] == 'butt'){
  892. $path_style .= ' 0 J ';
  893. }
  894. else if ($critere_style['stroke-linecap'] == 'round'){
  895. $path_style .= ' 1 J ';
  896. }
  897. else if ($critere_style['stroke-linecap'] == 'square'){
  898. $path_style .= ' 2 J ';
  899. }
  900. if (isset($critere_style['stroke-miterlimit'])){
  901. if ($critere_style['stroke-miterlimit'] == 'none'){
  902. }
  903. else if (preg_match('/^[\d.]+$/',$critere_style['stroke-miterlimit'])) {
  904. $path_style .= sprintf('%.2F M ',$critere_style['stroke-miterlimit']);
  905. }
  906. }
  907. // mPDF 4.4.003
  908. if (isset($critere_style['stroke-dasharray'])){
  909. $off = 0;
  910. $d = preg_split('/[ ,]/',$critere_style['stroke-dasharray']);
  911. if (count($d) == 1 && $d[0]==0) {
  912. $path_style .= '[] 0 d ';
  913. }
  914. else {
  915. if (count($d) % 2 == 1) { $d = array_merge($d, $d); } // 5, 3, 1 => 5,3,1,5,3,1 OR 3 => 3,3
  916. $arr = '';
  917. for($i=0; $i<count($d); $i+=2) {
  918. $arr .= sprintf('%.3F %.3F ', $d[$i]*$this->kp, $d[$i+1]*$this->kp);
  919. }
  920. if (isset($critere_style['stroke-dashoffset'])){ $off = $critere_style['stroke-dashoffset'] + 0; }
  921. $path_style .= sprintf('[%s] %.3F d ', $arr, $off*$this->kp);
  922. }
  923. }
  924. }
  925. // mPDF 4.4.003
  926. if ($critere_style['fill-rule']=='evenodd') { $fr = '*'; }
  927. else { $fr = ''; }
  928. // mPDF 4.4.003
  929. if (isset($critere_style['fill-opacity'])) {
  930. $opacity = 1;
  931. if ($critere_style['fill-opacity'] == 0) { $opacity = 0; }
  932. else if ($critere_style['fill-opacity'] > 1) { $opacity = 1; }
  933. else if ($critere_style['fill-opacity'] > 0) { $opacity = $critere_style['fill-opacity']; }
  934. else if ($critere_style['fill-opacity'] < 0) { $opacity = 0; }
  935. $gs = $this->mpdf_ref->AddExtGState(array('ca'=>$opacity, 'BM'=>'/Normal'));
  936. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  937. $path_style .= sprintf(' /GS%d gs ', $gs);
  938. }
  939. // mPDF 4.4.003
  940. if (isset($critere_style['stroke-opacity'])) {
  941. $opacity = 1;
  942. if ($critere_style['stroke-opacity'] == 0) { $opacity = 0; }
  943. else if ($critere_style['stroke-opacity'] > 1) { $opacity = 1; }
  944. else if ($critere_style['stroke-opacity'] > 0) { $opacity = $critere_style['stroke-opacity']; }
  945. else if ($critere_style['stroke-opacity'] < 0) { $opacity = 0; }
  946. $gs = $this->mpdf_ref->AddExtGState(array('CA'=>$opacity, 'BM'=>'/Normal'));
  947. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  948. $path_style .= sprintf(' /GS%d gs ', $gs);
  949. }
  950. switch ($style){
  951. case 'F':
  952. $op = 'f';
  953. break;
  954. case 'FD':
  955. $op = 'B';
  956. break;
  957. case 'ND':
  958. $op = 'S';
  959. break;
  960. case 'D':
  961. $op = 'S';
  962. break;
  963. default:
  964. $op = 'n';
  965. }
  966. // mPDF 5.0
  967. $prestyle = $path_style.' ';
  968. $poststyle = $w.' '. $op.$fr.' '.$fill_gradient."\n";
  969. return array($prestyle,$poststyle);
  970. }
  971. //
  972. // fonction retracant les <path />
  973. function svgPath($command, $arguments){
  974. $path_cmd = '';
  975. $newsubpath = false; // mPDF 4.4.003
  976. // mPDF 5.0.039
  977. $minl = $this->pathBBox[0];
  978. $mint = $this->pathBBox[1];
  979. $maxr = $this->pathBBox[2]+$this->pathBBox[0];
  980. $maxb = $this->pathBBox[3]+$this->pathBBox[1];
  981. // mPDF 5.0.040
  982. $start = array($this->xbase, -$this->ybase);
  983. // mPDF 4.4.003
  984. preg_match_all('/[\-^]?[\d.]+(e[\-]?[\d]+){0,1}/i', $arguments, $a, PREG_SET_ORDER);
  985. // if the command is a capital letter, the coords go absolute, otherwise relative
  986. if(strtolower($command) == $command) $relative = true;
  987. else $relative = false;
  988. $ile_argumentow = count($a);
  989. // each command may have different needs for arguments [1 to 8]
  990. switch(strtolower($command)){
  991. case 'm': // move
  992. for($i = 0; $i<$ile_argumentow; $i+=2){
  993. $x = $a[$i][0];
  994. $y = $a[$i+1][0];
  995. if($relative){
  996. $pdfx = ($this->xbase + $x);
  997. $pdfy = ($this->ybase - $y);
  998. $this->xbase += $x;
  999. $this->ybase += -$y;
  1000. }
  1001. else{
  1002. $pdfx = $x;
  1003. $pdfy = -$y ;
  1004. $this->xbase = $x;
  1005. $this->ybase = -$y;
  1006. }
  1007. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1008. // mPDF 5.0.039
  1009. $minl = min($minl,$pdf_pt['x']);
  1010. $maxr = max($maxr,$pdf_pt['x']);
  1011. $mint = min($mint,-$pdf_pt['y']);
  1012. $maxb = max($maxb,-$pdf_pt['y']);
  1013. if($i == 0) $path_cmd .= sprintf('%.3F %.3F m ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1014. else $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1015. // mPDF 4.4.003 Save start points of subpath
  1016. if ($this->subPathInit) {
  1017. $this->spxstart = $this->xbase;
  1018. $this->spystart = $this->ybase;
  1019. $this->subPathInit = false;
  1020. }
  1021. }
  1022. break;
  1023. case 'l': // a simple line
  1024. for($i = 0; $i<$ile_argumentow; $i+=2){
  1025. $x = ($a[$i][0]);
  1026. $y = ($a[$i+1][0]);
  1027. if($relative){
  1028. $pdfx = ($this->xbase + $x);
  1029. $pdfy = ($this->ybase - $y);
  1030. $this->xbase += $x;
  1031. $this->ybase += -$y;
  1032. }
  1033. else{
  1034. $pdfx = $x ;
  1035. $pdfy = -$y ;
  1036. $this->xbase = $x;
  1037. $this->ybase = -$y;
  1038. }
  1039. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1040. // mPDF 5.0.039
  1041. $minl = min($minl,$pdf_pt['x']);
  1042. $maxr = max($maxr,$pdf_pt['x']);
  1043. $mint = min($mint,-$pdf_pt['y']);
  1044. $maxb = max($maxb,-$pdf_pt['y']);
  1045. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1046. }
  1047. break;
  1048. case 'h': // a very simple horizontal line
  1049. for($i = 0; $i<$ile_argumentow; $i++){
  1050. $x = ($a[$i][0]);
  1051. if($relative){
  1052. $y = 0;
  1053. $pdfx = ($this->xbase + $x) ;
  1054. $pdfy = ($this->ybase - $y) ;
  1055. $this->xbase += $x;
  1056. $this->ybase += -$y;
  1057. }
  1058. else{
  1059. $y = -$this->ybase;
  1060. $pdfx = $x;
  1061. $pdfy = -$y;
  1062. $this->xbase = $x;
  1063. $this->ybase = -$y;
  1064. }
  1065. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1066. // mPDF 5.0.039
  1067. $minl = min($minl,$pdf_pt['x']);
  1068. $maxr = max($maxr,$pdf_pt['x']);
  1069. $mint = min($mint,-$pdf_pt['y']);
  1070. $maxb = max($maxb,-$pdf_pt['y']);
  1071. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1072. }
  1073. break;
  1074. case 'v': // the simplest line, vertical
  1075. for($i = 0; $i<$ile_argumentow; $i++){
  1076. $y = ($a[$i][0]);
  1077. if($relative){
  1078. $x = 0;
  1079. $pdfx = ($this->xbase + $x);
  1080. $pdfy = ($this->ybase - $y);
  1081. $this->xbase += $x;
  1082. $this->ybase += -$y;
  1083. }
  1084. else{
  1085. $x = $this->xbase;
  1086. $pdfx = $x;
  1087. $pdfy = -$y;
  1088. $this->xbase = $x;
  1089. $this->ybase = -$y;
  1090. }
  1091. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1092. // mPDF 5.0.039
  1093. $minl = min($minl,$pdf_pt['x']);
  1094. $maxr = max($maxr,$pdf_pt['x']);
  1095. $mint = min($mint,-$pdf_pt['y']);
  1096. $maxb = max($maxb,-$pdf_pt['y']);
  1097. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1098. }
  1099. break;
  1100. case 's': // bezier with first vertex equal first control
  1101. // mPDF 4.4.003
  1102. if (!($this->lastcommand == 'C' || $this->lastcommand == 'c' || $this->lastcommand == 'S' || $this->lastcommand == 's')) {
  1103. $this->lastcontrolpoints = array(0,0);
  1104. }
  1105. for($i = 0; $i<$ile_argumentow; $i += 4){
  1106. $x1 = $this->lastcontrolpoints[0];
  1107. $y1 = $this->lastcontrolpoints[1];
  1108. $x2 = ($a[$i][0]);
  1109. $y2 = ($a[$i+1][0]);
  1110. $x = ($a[$i+2][0]);
  1111. $y = ($a[$i+3][0]);
  1112. if($relative){
  1113. $pdfx1 = ($this->xbase + $x1);
  1114. $pdfy1 = ($this->ybase - $y1);
  1115. $pdfx2 = ($this->xbase + $x2);
  1116. $pdfy2 = ($this->ybase - $y2);
  1117. $pdfx = ($this->xbase + $x);
  1118. $pdfy = ($this->ybase - $y);
  1119. $this->xbase += $x;
  1120. $this->ybase += -$y;
  1121. }
  1122. else{
  1123. $pdfx1 = $this->xbase + $x1;
  1124. $pdfy1 = $this->ybase -$y1;
  1125. $pdfx2 = $x2;
  1126. $pdfy2 = -$y2;
  1127. $pdfx = $x;
  1128. $pdfy = -$y;
  1129. $this->xbase = $x;
  1130. $this->ybase = -$y;
  1131. }
  1132. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1133. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1134. // mPDF 5.0.040
  1135. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1136. $bx = calc_bezier_bbox($start, $curves);
  1137. $minl = min($minl,$bx[0]);
  1138. $maxr = max($maxr,$bx[2]);
  1139. $mint = min($mint,$bx[1]);
  1140. $maxb = max($maxb,$bx[3]);
  1141. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1142. {
  1143. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1144. }
  1145. else
  1146. {
  1147. $path_cmd .= sprintf('%.3F %.3F %.3F %.3F %.3F %.3F c ', $pdfx1*$this->kp, $pdfy1*$this->kp, $pdfx2*$this->kp, $pdfy2*$this->kp, $pdfx*$this->kp, $pdfy*$this->kp);
  1148. }
  1149. }
  1150. break;
  1151. case 'c': // bezier with second vertex equal second control
  1152. for($i = 0; $i<$ile_argumentow; $i += 6){
  1153. $x1 = ($a[$i][0]);
  1154. $y1 = ($a[$i+1][0]);
  1155. $x2 = ($a[$i+2][0]);
  1156. $y2 = ($a[$i+3][0]);
  1157. $x = ($a[$i+4][0]);
  1158. $y = ($a[$i+5][0]);
  1159. if($relative){
  1160. $pdfx1 = ($this->xbase + $x1);
  1161. $pdfy1 = ($this->ybase - $y1);
  1162. $pdfx2 = ($this->xbase + $x2);
  1163. $pdfy2 = ($this->ybase - $y2);
  1164. $pdfx = ($this->xbase + $x);
  1165. $pdfy = ($this->ybase - $y);
  1166. $this->xbase += $x;
  1167. $this->ybase += -$y;
  1168. }
  1169. else{
  1170. $pdfx1 = $x1;
  1171. $pdfy1 = -$y1;
  1172. $pdfx2 = $x2;
  1173. $pdfy2 = -$y2;
  1174. $pdfx = $x;
  1175. $pdfy = -$y;
  1176. $this->xbase = $x;
  1177. $this->ybase = -$y;
  1178. }
  1179. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1180. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  1181. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  1182. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1183. // mPDF 5.0.040
  1184. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1185. $bx = calc_bezier_bbox($start, $curves);
  1186. $minl = min($minl,$bx[0]);
  1187. $maxr = max($maxr,$bx[2]);
  1188. $mint = min($mint,$bx[1]);
  1189. $maxb = max($maxb,$bx[3]);
  1190. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1191. {
  1192. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1193. }
  1194. else
  1195. {
  1196. $path_cmd .= sprintf('%.3F %.3F %.3F %.3F %.3F %.3F c ', $pdfx1*$this->kp, $pdfy1*$this->kp, $pdfx2*$this->kp, $pdfy2*$this->kp, $pdfx*$this->kp, $pdfy*$this->kp);
  1197. }
  1198. }
  1199. break;
  1200. case 'q': // bezier quadratic avec point de control
  1201. for($i = 0; $i<$ile_argumentow; $i += 4){
  1202. $x1 = ($a[$i][0]);
  1203. $y1 = ($a[$i+1][0]);
  1204. $x = ($a[$i+2][0]);
  1205. $y = ($a[$i+3][0]);
  1206. if($relative){
  1207. $pdfx = ($this->xbase + $x);
  1208. $pdfy = ($this->ybase - $y);
  1209. $pdfx1 = ($this->xbase + ($x1*2/3));
  1210. $pdfy1 = ($this->ybase - ($y1*2/3));
  1211. // mPDF 4.4.003
  1212. $pdfx2 = $pdfx1 + 1/3 *($x);
  1213. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  1214. $this->xbase += $x;
  1215. $this->ybase += -$y;
  1216. }
  1217. else{
  1218. $pdfx = $x;
  1219. $pdfy = -$y;
  1220. $pdfx1 = ($this->xbase+(($x1-$this->xbase)*2/3));
  1221. $pdfy1 = ($this->ybase-(($y1+$this->ybase)*2/3));
  1222. $pdfx2 = ($x+(($x1-$x)*2/3));
  1223. $pdfy2 = (-$y-(($y1-$y)*2/3));
  1224. // mPDF 4.4.003
  1225. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  1226. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  1227. $this->xbase = $x;
  1228. $this->ybase = -$y;
  1229. }
  1230. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1231. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  1232. // mPDF 5.0.040
  1233. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1234. $bx = calc_bezier_bbox($start, $curves);
  1235. $minl = min($minl,$bx[0]);
  1236. $maxr = max($maxr,$bx[2]);
  1237. $mint = min($mint,$bx[1]);
  1238. $maxb = max($maxb,$bx[3]);
  1239. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  1240. {
  1241. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  1242. }
  1243. else
  1244. {
  1245. $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);
  1246. }
  1247. }
  1248. break;
  1249. case 't': // bezier quadratic avec point de control simetrique a lancien point de control
  1250. // mPDF 4.4.003
  1251. if (!($this->lastcommand == 'Q' || $this->lastcommand == 'q' || $this->lastcommand == 'T' || $this->lastcommand == 't')) {
  1252. $this->lastcontrolpoints = array(0,0);
  1253. }
  1254. for($i = 0; $i<$ile_argumentow; $i += 2){
  1255. $x = ($a[$i][0]);
  1256. $y = ($a[$i+1][0]);
  1257. $x1 = $this->lastcontrolpoints[0];
  1258. $y1 = $this->lastcontrolpoints[1];
  1259. if($relative){
  1260. $pdfx = ($this->xbase + $x);
  1261. $pdfy = ($this->ybase - $y);
  1262. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  1263. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  1264. // mPDF 4.4.003
  1265. $pdfx2 = $pdfx1 + 1/3 *($x);
  1266. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  1267. $this->xbase += $x;
  1268. $this->ybase += -$y;
  1269. }
  1270. else{
  1271. $pdfx = $x;
  1272. $pdfy = -$y;
  1273. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  1274. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  1275. // mPDF 4.4.003
  1276. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  1277. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  1278. $this->xbase = $x;
  1279. $this->ybase = -$y;
  1280. }
  1281. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  1282. // mPDF 5.0.040
  1283. $curves = array($pdfx1,-$pdfy1,$pdfx2,-$pdfy2,$pdfx,-$pdfy);
  1284. $bx = calc_bezier_bbox($start, $curves);
  1285. $minl = min($minl,$bx[0]);
  1286. $maxr = max($maxr,$bx[2]);
  1287. $mint = min($mint,$bx[1]);
  1288. $maxb = max($maxb,$bx[3]);
  1289. $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);
  1290. }
  1291. break;
  1292. case 'a': // Elliptical arc
  1293. for($i = 0; $i<$ile_argumentow; $i += 7){
  1294. $rx = ($a[$i][0]);
  1295. $ry = ($a[$i+1][0]);
  1296. $angle = ($a[$i+2][0]); //x-axis-rotation
  1297. $largeArcFlag = ($a[$i+3][0]);
  1298. $sweepFlag = ($a[$i+4][0]);
  1299. $x2 = ($a[$i+5][0]);
  1300. $y2 = ($a[$i+6][0]);
  1301. $x1 = $this->xbase;
  1302. $y1 = -$this->ybase;
  1303. if($relative){
  1304. $x2 = $this->xbase + $x2;
  1305. $y2 = -$this->ybase + $y2;
  1306. $this->xbase += ($a[$i+5][0]);
  1307. $this->ybase += -($a[$i+6][0]);
  1308. }
  1309. else{
  1310. $this->xbase = $x2;
  1311. $this->ybase = -$y2;
  1312. }
  1313. // mPDF 5.0.039 // mPDF 5.0.040
  1314. list($pcmd, $bounds) = $this->Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag);
  1315. $minl = min($minl,$x2,min($bounds[0]));
  1316. $maxr = max($maxr,$x2,max($bounds[0]));
  1317. $mint = min($mint,$y2,min($bounds[1]));
  1318. $maxb = max($maxb,$y2,max($bounds[1]));
  1319. $path_cmd .= $pcmd;
  1320. }
  1321. break;
  1322. case'z':
  1323. $path_cmd .= 'h ';
  1324. // mPDF 4.4.003
  1325. $this->subPathInit = true;
  1326. $newsubpath = true;
  1327. $this->xbase = $this->spxstart;
  1328. $this->ybase = $this->spystart;
  1329. break;
  1330. default:
  1331. break;
  1332. }
  1333. if (!$newsubpath) { $this->subPathInit = false; } // mPDF 4.4.003
  1334. $this->lastcommand = $command;
  1335. // mPDF 5.0.039
  1336. $this->pathBBox[0] = $minl;
  1337. $this->pathBBox[1] = $mint;
  1338. $this->pathBBox[2] = $maxr - $this->pathBBox[0];
  1339. $this->pathBBox[3] = $maxb - $this->pathBBox[1];
  1340. return $path_cmd;
  1341. }
  1342. function Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag) {
  1343. // mPDF 5.0.040
  1344. $bounds = array(0=>array($x1,$x2),1=>array($y1,$y2));
  1345. // 1. Treat out-of-range parameters as described in
  1346. // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  1347. // If the endpoints (x1, y1) and (x2, y2) are identical, then this
  1348. // is equivalent to omitting the elliptical arc segment entirely
  1349. if ($x1 == $x2 && $y1 == $y2) return array('', $bounds); // mPD 5.0.040
  1350. // If rX = 0 or rY = 0 then this arc is treated as a straight line
  1351. // segment (a "lineto") joining the endpoints.
  1352. if ($rx == 0.0 || $ry == 0.0) {
  1353. // return array(Lineto(x2, y2), $bounds); // mPD 5.0.040
  1354. }
  1355. // If rX or rY have negative signs, these are dropped; the absolute
  1356. // value is used instead.
  1357. if ($rx<0.0) $rx = -$rx;
  1358. if ($ry<0.0) $ry = -$ry;
  1359. // 2. convert to center parameterization as shown in
  1360. // http://www.w3.org/TR/SVG/implnote.html
  1361. $sinPhi = sin(deg2rad($angle));
  1362. $cosPhi = cos(deg2rad($angle));
  1363. $x1dash = $cosPhi * ($x1-$x2)/2.0 + $sinPhi * ($y1-$y2)/2.0;
  1364. $y1dash = -$sinPhi * ($x1-$x2)/2.0 + $cosPhi * ($y1-$y2)/2.0;
  1365. $numerator = $rx*$rx*$ry*$ry - $rx*$rx*$y1dash*$y1dash - $ry*$ry*$x1dash*$x1dash;
  1366. if ($numerator < 0.0) {
  1367. // If rX , rY and are such that there is no solution (basically,
  1368. // the ellipse is not big enough to reach from (x1, y1) to (x2,
  1369. // y2)) then the ellipse is scaled up uniformly until there is
  1370. // exactly one solution (until the ellipse is just big enough).
  1371. // -> find factor s, such that numerator' with rx'=s*rx and
  1372. // ry'=s*ry becomes 0 :
  1373. $s = sqrt(1.0 - $numerator/($rx*$rx*$ry*$ry));
  1374. $rx *= $s;
  1375. $ry *= $s;
  1376. $root = 0.0;
  1377. }
  1378. else {
  1379. $root = ($largeArcFlag == $sweepFlag ? -1.0 : 1.0) * sqrt( $numerator/($rx*$rx*$y1dash*$y1dash+$ry*$ry*$x1dash*$x1dash) );
  1380. }
  1381. $cxdash = $root*$rx*$y1dash/$ry;
  1382. $cydash = -$root*$ry*$x1dash/$rx;
  1383. $cx = $cosPhi * $cxdash - $sinPhi * $cydash + ($x1+$x2)/2.0;
  1384. $cy = $sinPhi * $cxdash + $cosPhi * $cydash + ($y1+$y2)/2.0;
  1385. $theta1 = $this->CalcVectorAngle(1.0, 0.0, ($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry);
  1386. $dtheta = $this->CalcVectorAngle(($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry, (-$x1dash-$cxdash)/$rx, (-$y1dash-$cydash)/$ry);
  1387. if (!$sweepFlag && $dtheta>0)
  1388. $dtheta -= 2.0*M_PI;
  1389. else if ($sweepFlag && $dtheta<0)
  1390. $dtheta += 2.0*M_PI;
  1391. // 3. convert into cubic bezier segments <= 90deg
  1392. $segments = ceil(abs($dtheta/(M_PI/2.0)));
  1393. $delta = $dtheta/$segments;
  1394. $t = 8.0/3.0 * sin($delta/4.0) * sin($delta/4.0) / sin($delta/2.0);
  1395. $coords = array();
  1396. for ($i = 0; $i < $segments; $i++) {
  1397. $cosTheta1 = cos($theta1);
  1398. $sinTheta1 = sin($theta1);
  1399. $theta2 = $theta1 + $delta;
  1400. $cosTheta2 = cos($theta2);
  1401. $sinTheta2 = sin($theta2);
  1402. // a) calculate endpoint of the segment:
  1403. $xe = $cosPhi * $rx*$cosTheta2 - $sinPhi * $ry*$sinTheta2 + $cx;
  1404. $ye = $sinPhi * $rx*$cosTheta2 + $cosPhi * $ry*$sinTheta2 + $cy;
  1405. // b) calculate gradients at start/end points of segment:
  1406. $dx1 = $t * ( - $cosPhi * $rx*$sinTheta1 - $sinPhi * $ry*$cosTheta1);
  1407. $dy1 = $t * ( - $sinPhi * $rx*$sinTheta1 + $cosPhi * $ry*$cosTheta1);
  1408. $dxe = $t * ( $cosPhi * $rx*$sinTheta2 + $sinPhi * $ry*$cosTheta2);
  1409. $dye = $t * ( $sinPhi * $rx*$sinTheta2 - $cosPhi * $ry*$cosTheta2);
  1410. // c) draw the cubic bezier:
  1411. $coords[$i] = array(($x1+$dx1), ($y1+$dy1), ($xe+$dxe), ($ye+$dye), $xe, $ye);
  1412. // do next segment
  1413. $theta1 = $theta2;
  1414. $x1 = $xe;
  1415. $y1 = $ye;
  1416. }
  1417. $path = ' ';
  1418. foreach($coords AS $c) {
  1419. $cpx1 = $c[0];
  1420. $cpy1 = $c[1];
  1421. $cpx2 = $c[2];
  1422. $cpy2 = $c[3];
  1423. $x2 = $c[4];
  1424. $y2 = $c[5];
  1425. $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";
  1426. // mPDF 5.0.040
  1427. $bounds[0][] = $c[4];
  1428. $bounds[1][] = $c[5];
  1429. }
  1430. return array($path, $bounds); // mPD 5.0.040
  1431. }
  1432. function CalcVectorAngle($ux, $uy, $vx, $vy) {
  1433. $ta = atan2($uy, $ux);
  1434. $tb = atan2($vy, $vx);
  1435. if ($tb >= $ta)
  1436. return ($tb-$ta);
  1437. return (6.28318530718 - ($ta-$tb));
  1438. }
  1439. // mPDF 4.4.003
  1440. function ConvertSVGSizePixels($size=5,$maxsize='x'){
  1441. // maxsize in pixels (user units) or 'y' or 'x'
  1442. // e.g. $w = $this->ConvertSVGSizePixels($arguments['w'],$this->svg_info['w']*(25.4/$this->mpdf_ref->dpi));
  1443. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1444. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1445. // For text $maxsize = Fontsize
  1446. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1447. if ($maxsize == 'y') { $maxsize = $this->svg_info['h']; }
  1448. else if ($maxsize == 'x') { $maxsize = $this->svg_info['w']; }
  1449. $maxsize *= (25.4/$this->mpdf_ref->dpi); // convert pixels to mm
  1450. $fontsize=$this->mpdf_ref->FontSize;
  1451. //Return as pixels
  1452. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,$fontsize,false) * 1/(25.4/$this->mpdf_ref->dpi);
  1453. return $size;
  1454. }
  1455. // mPDF 4.4.003
  1456. function ConvertSVGSizePts($size=5){
  1457. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1458. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1459. // For text $maxsize = Fontsize
  1460. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1461. $maxsize=$this->mpdf_ref->FontSize;
  1462. //Return as pts
  1463. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,false,true) * 72/25.4;
  1464. return $size;
  1465. }
  1466. //
  1467. // fonction retracant les <rect />
  1468. function svgRect($arguments){
  1469. if ($arguments['h']==0 || $arguments['w']==0) { return ''; } // mPDF 4.4.003
  1470. $x = $this->ConvertSVGSizePixels($arguments['x'],'x'); // mPDF 4.4.003
  1471. $y = $this->ConvertSVGSizePixels($arguments['y'],'y'); // mPDF 4.4.003
  1472. $h = $this->ConvertSVGSizePixels($arguments['h'],'y'); // mPDF 4.4.003
  1473. $w = $this->ConvertSVGSizePixels($arguments['w'],'x'); // mPDF 4.4.003
  1474. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1475. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1476. if ($rx > $w/2) { $rx = $w/2; } // mPDF 4.4.003
  1477. if ($ry > $h/2) { $ry = $h/2; } // mPDF 4.4.003
  1478. if ($rx>0 and $ry == 0){$ry = $rx;}
  1479. if ($ry>0 and $rx == 0){$rx = $ry;}
  1480. if ($rx == 0 and $ry == 0){
  1481. // trace un rectangle sans angle arrondit
  1482. $path_cmd = sprintf('%.3F %.3F m ', ($x*$this->kp), -($y*$this->kp));
  1483. $path_cmd .= sprintf('%.3F %.3F l ', (($x+$w)*$this->kp), -($y*$this->kp));
  1484. $path_cmd .= sprintf('%.3F %.3F l ', (($x+$w)*$this->kp), -(($y+$h)*$this->kp));
  1485. $path_cmd .= sprintf('%.3F %.3F l ', ($x)*$this->kp, -(($y+$h)*$this->kp));
  1486. $path_cmd .= sprintf('%.3F %.3F l h ', ($x*$this->kp), -($y*$this->kp));
  1487. }
  1488. else {
  1489. // trace un rectangle avec les arrondit
  1490. // les points de controle du bezier sont deduis grace a la constante kappa
  1491. $kappa = 4*(sqrt(2)-1)/3;
  1492. $kx = $kappa*$rx;
  1493. $ky = $kappa*$ry;
  1494. $path_cmd = sprintf('%.3F %.3F m ', ($x+$rx)*$this->kp, -$y*$this->kp);
  1495. $path_cmd .= sprintf('%.3F %.3F l ', ($x+($w-$rx))*$this->kp, -$y*$this->kp);
  1496. $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 );
  1497. $path_cmd .= sprintf('%.3F %.3F l ', ($x+$w)*$this->kp, (-$y+(-$h+$ry))*$this->kp);
  1498. $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 );
  1499. $path_cmd .= sprintf('%.3F %.3F l ', ($x+$rx)*$this->kp, (-$y+(-$h))*$this->kp);
  1500. $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 );
  1501. $path_cmd .= sprintf('%.3F %.3F l ', $x*$this->kp, (-$y+(-$ry))*$this->kp);
  1502. $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 );
  1503. }
  1504. return $path_cmd;
  1505. }
  1506. //
  1507. // fonction retracant les <ellipse /> et <circle />
  1508. // le cercle est tracé grave a 4 bezier cubic, les poitn de controles
  1509. // sont deduis grace a la constante kappa * rayon
  1510. function svgEllipse($arguments){
  1511. if ($arguments['rx']==0 || $arguments['ry']==0) { return ''; } // mPDF 4.4.003
  1512. $kappa = 4*(sqrt(2)-1)/3;
  1513. $cx = $this->ConvertSVGSizePixels($arguments['cx'],'x'); // mPDF 4.4.003
  1514. $cy = $this->ConvertSVGSizePixels($arguments['cy'],'y'); // mPDF 4.4.003
  1515. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1516. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1517. $x1 = $cx;
  1518. $y1 = -$cy+$ry;
  1519. $x2 = $cx+$rx;
  1520. $y2 = -$cy;
  1521. $x3 = $cx;
  1522. $y3 = -$cy-$ry;
  1523. $x4 = $cx-$rx;
  1524. $y4 = -$cy;
  1525. $path_cmd = sprintf('%.3F %.3F m ', $x1*$this->kp, $y1*$this->kp);
  1526. $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);
  1527. $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);
  1528. $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);
  1529. $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);
  1530. $path_cmd .= 'h ';
  1531. return $path_cmd;
  1532. }
  1533. //
  1534. // fonction retracant les <polyline /> et les <line />
  1535. function svgPolyline($arguments,$ispolyline=true){
  1536. if ($ispolyline) {
  1537. $xbase = $arguments[0] ;
  1538. $ybase = - $arguments[1] ;
  1539. }
  1540. else {
  1541. if ($arguments[0]==$arguments[2] && $arguments[1]==$arguments[3]) { return ''; } // mPDF 4.4.003 Zero length line
  1542. $xbase = $this->ConvertSVGSizePixels($arguments[0],'x'); // mPDF 4.4.003
  1543. $ybase = - $this->ConvertSVGSizePixels($arguments[1],'y'); // mPDF 4.4.003
  1544. }
  1545. $path_cmd = sprintf('%.3F %.3F m ', $xbase*$this->kp, $ybase*$this->kp);
  1546. for ($i = 2; $i<count($arguments);$i += 2) {
  1547. if ($ispolyline) {
  1548. $tmp_x = $arguments[$i] ;
  1549. $tmp_y = - $arguments[($i+1)] ;
  1550. }
  1551. else {
  1552. $tmp_x = $this->ConvertSVGSizePixels($arguments[$i],'x') ; // mPDF 4.4.003
  1553. $tmp_y = - $this->ConvertSVGSizePixels($arguments[($i+1)],'y') ; // mPDF 4.4.003
  1554. }
  1555. $path_cmd .= sprintf('%.3F %.3F l ', $tmp_x*$this->kp, $tmp_y*$this->kp);
  1556. }
  1557. // $path_cmd .= 'h '; // ?? In error - don't close subpath here
  1558. return $path_cmd;
  1559. }
  1560. //
  1561. // fonction retracant les <polygone />
  1562. function svgPolygon($arguments){
  1563. $xbase = $arguments[0] ;
  1564. $ybase = - $arguments[1] ;
  1565. $path_cmd = sprintf('%.3F %.3F m ', $xbase*$this->kp, $ybase*$this->kp);
  1566. for ($i = 2; $i<count($arguments);$i += 2) {
  1567. $tmp_x = $arguments[$i] ;
  1568. $tmp_y = - $arguments[($i+1)] ;
  1569. $path_cmd .= sprintf('%.3F %.3F l ', $tmp_x*$this->kp, $tmp_y*$this->kp);
  1570. }
  1571. $path_cmd .= sprintf('%.3F %.3F l ', $xbase*$this->kp, $ybase*$this->kp);
  1572. $path_cmd .= 'h ';
  1573. return $path_cmd;
  1574. }
  1575. //
  1576. // write string to image
  1577. function svgText() {
  1578. // $tmp = count($this->txt_style)-1;
  1579. $current_style = array_pop($this->txt_style);
  1580. $style = '';
  1581. $render = -1;
  1582. if(isset($this->txt_data[2]))
  1583. {
  1584. // select font
  1585. $style .= ($current_style['font-weight'] == 'bold')?'B':'';
  1586. $style .= ($current_style['font-style'] == 'italic')?'I':'';
  1587. $size = $current_style['font-size']*$this->kf; // mPDF 5.0.039
  1588. // mPDF 5.0
  1589. $current_style['font-family'] = $this->mpdf_ref->SetFont($current_style['font-family'],$style,$size,false);
  1590. $this->mpdf_ref->CurrentFont['fo'] = true; // mPDF 5.0.039
  1591. // mPDF 5.0.041
  1592. $opacitystr = '';
  1593. $opacity = 1;
  1594. if (isset($current_style['fill-opacity'])) {
  1595. if ($current_style['fill-opacity'] == 0) { $opacity = 0; }
  1596. else if ($current_style['fill-opacity'] > 1) { $opacity = 1; }
  1597. else if ($current_style['fill-opacity'] > 0) { $opacity = $current_style['fill-opacity']; }
  1598. else if ($current_style['fill-opacity'] < 0) { $opacity = 0; }
  1599. }
  1600. $gs = $this->mpdf_ref->AddExtGState(array('ca'=>$opacity, 'BM'=>'/Normal'));
  1601. $this->mpdf_ref->extgstates[$gs]['fo'] = true; // mPDF 5.0.039
  1602. $opacitystr = sprintf(' /GS%d gs ', $gs);
  1603. // mPDF 5.0.051
  1604. $fillstr = '';
  1605. if (isset($current_style['fill']) && $current_style['fill']!='none') {
  1606. $col = $this->mpdf_ref->ConvertColor($current_style['fill']);
  1607. // mPDF 5.0.051
  1608. $fillstr = $this->mpdf_ref->SetFColor($col, true);
  1609. $render = "0"; // Fill (only)
  1610. }
  1611. $strokestr = '';
  1612. if (isset($current_style['stroke-width']) && $current_style['stroke-width']>0 && $current_style['stroke']!='none') {
  1613. $scol = $this->mpdf_ref->ConvertColor($current_style['stroke']);
  1614. if ($scol) {
  1615. $strokestr .= $this->mpdf_ref->SetDColor($scol, true).' '; // mPDF 5.0.051
  1616. }
  1617. $linewidth = $this->ConvertSVGSizePixels($current_style['stroke-width']);
  1618. if ($linewidth > 0) {
  1619. $strokestr .= sprintf('%.3F w 1 J 1 j ',$linewidth*$this->kp);
  1620. if ($render == -1) { $render = "1"; } // stroke only
  1621. else { $render = "2"; } // fill and stroke
  1622. }
  1623. }
  1624. if ($render == -1) { return ''; }
  1625. $x = $this->ConvertSVGSizePixels($this->txt_data[0],'x'); // mPDF 4.4.003
  1626. $y = $this->ConvertSVGSizePixels($this->txt_data[1],'y'); // mPDF 4.4.003
  1627. $txt = $this->txt_data[2];
  1628. // mPDF 4.4.003
  1629. $txt = preg_replace('/\f/','',$txt);
  1630. $txt = preg_replace('/\r/','',$txt);
  1631. $txt = preg_replace('/\n/',' ',$txt);
  1632. $txt = preg_replace('/\t/',' ',$txt);
  1633. $txt = preg_replace("/[ ]+/u",' ',$txt);
  1634. $txt = trim($txt);
  1635. $txt = $this->mpdf_ref->purify_utf8_text($txt);
  1636. if ($this->mpdf_ref->text_input_as_HTML) {
  1637. $txt = $this->mpdf_ref->all_entities_to_utf8($txt);
  1638. }
  1639. // mPDF 5.0
  1640. if ($this->mpdf_ref->usingCoreFont) { $txt = mb_convert_encoding($txt,$this->mpdf_ref->mb_enc,'UTF-8'); }
  1641. if (preg_match("/([".$this->mpdf_ref->pregRTLchars."])/u", $txt)) { $this->mpdf_ref->biDirectional = true; } // mPDF 4.4.003
  1642. $this->mpdf_ref->magic_reverse_dir($txt, true, 'ltr'); // mPDF 5.0.054
  1643. $this->mpdf_ref->ConvertIndic($txt);
  1644. if ($current_style['text-anchor']=='middle') {
  1645. $tw = $this->mpdf_ref->GetStringWidth($txt)*_MPDFK/2; // mPDF 4.4.003 // mPDF 5.4.09
  1646. }
  1647. else if ($current_style['text-anchor']=='end') {
  1648. $tw = $this->mpdf_ref->GetStringWidth($txt)*_MPDFK; // mPDF 4.4.003 // mPDF 5.4.09
  1649. }
  1650. else $tw = 0;
  1651. if (!$this->mpdf_ref->usingCoreFont) {
  1652. $this->mpdf_ref->UTF8StringToArray($txt); // mPDF 5.0 adds chars to subset list
  1653. $txt= $this->mpdf_ref->UTF8ToUTF16BE($txt, false);
  1654. }
  1655. $txt='('.$this->mpdf_ref->_escape($txt).')';
  1656. $this->mpdf_ref->CurrentFont['used']= true;
  1657. $pdfx = $x - $tw/$this->kp; // mPDF 4.4.009
  1658. $pdfy = -$y ;
  1659. $xbase = $x;
  1660. $ybase = -$y;
  1661. // mPDF 5.0.041
  1662. // mPDF 5.0.051
  1663. $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";
  1664. unset($this->txt_data[0], $this->txt_data[1],$this->txt_data[2]);
  1665. // mPDF 5.4.12
  1666. if (isset($current_style['font-size-parent'])) {
  1667. $this->mpdf_ref->SetFontSize($current_style['font-size-parent']);
  1668. }
  1669. }
  1670. else
  1671. {
  1672. return ' ';
  1673. }
  1674. // $path_cmd .= 'h '; // mPDF 5.0
  1675. return $path_cmd;
  1676. }
  1677. function svgDefineTxtStyle($critere_style)
  1678. {
  1679. // get copy of current/default txt style, and modify it with supplied attributes
  1680. $tmp = count($this->txt_style)-1;
  1681. $current_style = $this->txt_style[$tmp];
  1682. if (isset($critere_style['style'])){
  1683. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1684. $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);
  1685. }
  1686. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']);
  1687. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  1688. }
  1689. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1690. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  1691. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1692. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  1693. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1694. $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);
  1695. }
  1696. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1697. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  1698. }
  1699. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1700. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  1701. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1702. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  1703. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1704. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  1705. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1706. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  1707. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1708. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  1709. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  1710. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  1711. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1712. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  1713. // mPDF 5.0.039
  1714. $tmp = preg_replace("/(.*)font-family:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1715. if ($tmp != $critere_style['style']){ $critere_style['font-family'] = $tmp;}
  1716. $tmp = preg_replace("/(.*)font-size:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1717. if ($tmp != $critere_style['style']){ $critere_style['font-size'] = $tmp;}
  1718. $tmp = preg_replace("/(.*)font-weight:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1719. if ($tmp != $critere_style['style']){ $critere_style['font-weight'] = $tmp;}
  1720. $tmp = preg_replace("/(.*)font-style:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1721. if ($tmp != $critere_style['style']){ $critere_style['font-style'] = $tmp;}
  1722. }
  1723. if (isset($critere_style['font'])){
  1724. // [ [ <'font-style'> || <'font-variant'> || <'font-weight'> ]?<'font-size'> [ / <'line-height'> ]? <'font-family'> ]
  1725. $tmp = preg_replace("/(.*)(italic|oblique)(.*)/i","$2",$critere_style['font']);
  1726. if ($tmp != $critere_style['font']){
  1727. if($tmp == 'oblique'){
  1728. $tmp = 'italic';
  1729. }
  1730. $current_style['font-style'] = $tmp;
  1731. }
  1732. $tmp = preg_replace("/(.*)(bold|bolder)(.*)/i","$2",$critere_style['font']);
  1733. if ($tmp != $critere_style['font']){
  1734. if($tmp == 'bolder'){
  1735. $tmp = 'bold';
  1736. }
  1737. $current_style['font-weight'] = $tmp;
  1738. }
  1739. // select digits not followed by percent sign nor preceeded by forward slash
  1740. $tmp = preg_replace("/(.*)\b(\d+)[\b|\/](.*)/i","$2",$critere_style['font']);
  1741. if ($tmp != $critere_style['font']){
  1742. $current_style['font-size'] = $this->ConvertSVGSizePts($tmp);
  1743. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1744. }
  1745. }
  1746. if(isset($critere_style['fill'])){
  1747. $current_style['fill'] = $critere_style['fill'];
  1748. }
  1749. if(isset($critere_style['stroke'])){
  1750. $current_style['stroke'] = $critere_style['stroke'];
  1751. }
  1752. if(isset($critere_style['stroke-width'])){
  1753. $current_style['stroke-width'] = $critere_style['stroke-width'];
  1754. }
  1755. if(isset($critere_style['font-style'])){
  1756. if(strtolower($critere_style['font-style']) == 'oblique')
  1757. {
  1758. $critere_style['font-style'] = 'italic';
  1759. }
  1760. $current_style['font-style'] = $critere_style['font-style'];
  1761. }
  1762. if(isset($critere_style['font-weight'])){
  1763. if(strtolower($critere_style['font-weight']) == 'bolder')
  1764. {
  1765. $critere_style['font-weight'] = 'bold';
  1766. }
  1767. $current_style['font-weight'] = $critere_style['font-weight'];
  1768. }
  1769. if(isset($critere_style['font-size'])){
  1770. // mPDF 5.4.12
  1771. if (strpos($critere_style['font-size'], '%')!==false) {
  1772. $current_style['font-size-parent'] = $current_style['font-size'];
  1773. }
  1774. $current_style['font-size'] = $this->ConvertSVGSizePts($critere_style['font-size']);
  1775. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1776. }
  1777. if(isset($critere_style['font-family'])){
  1778. $v = $critere_style['font-family'];
  1779. $aux_fontlist = explode(",",$v);
  1780. $found = 0;
  1781. foreach($aux_fontlist AS $f) {
  1782. $fonttype = trim($f);
  1783. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  1784. $fonttype = preg_replace('/ /','',$fonttype);
  1785. $v = strtolower(trim($fonttype));
  1786. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) { $v = $this->mpdf_ref->fonttrans[$v]; }
  1787. if ((!$this->mpdf_ref->usingCoreFont && in_array($v,$this->mpdf_ref->available_unifonts)) ||
  1788. ($this->mpdf_ref->usingCoreFont && in_array($v,array('courier','times','helvetica','arial'))) ||
  1789. in_array($v, array('sjis','uhc','big5','gb'))) {
  1790. $current_style['font-family'] = $v;
  1791. $found = 1;
  1792. break;
  1793. }
  1794. }
  1795. if (!$found) {
  1796. foreach($aux_fontlist AS $f) {
  1797. $fonttype = trim($f);
  1798. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  1799. $fonttype = preg_replace('/ /','',$fonttype);
  1800. $v = strtolower(trim($fonttype));
  1801. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) { $v = $this->mpdf_ref->fonttrans[$v]; }
  1802. 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) ) {
  1803. $current_style['font-family'] = $v;
  1804. break;
  1805. }
  1806. }
  1807. }
  1808. }
  1809. if(isset($critere_style['text-anchor'])){
  1810. $current_style['text-anchor'] = $critere_style['text-anchor'];
  1811. }
  1812. // add current style to text style array (will remove it later after writing text to svg_string)
  1813. array_push($this->txt_style,$current_style);
  1814. }
  1815. //
  1816. // fonction ajoutant un gradient
  1817. function svgAddGradient($id,$array_gradient){
  1818. $this->svg_gradient[$id] = $array_gradient;
  1819. }
  1820. //
  1821. // Ajoute une couleur dans le gradient correspondant
  1822. //
  1823. // function ecrivant dans le svgstring
  1824. function svgWriteString($content){
  1825. $this->svg_string .= $content;
  1826. }
  1827. // analise le svg et renvoie aux fonctions precedente our le traitement
  1828. function ImageSVG($data){
  1829. $this->svg_info = array();
  1830. // mPDF 4.4.006
  1831. if (preg_match('/<!ENTITY/si',$data)) {
  1832. // Get User-defined entities
  1833. preg_match_all('/<!ENTITY\s+([a-z]+)\s+\"(.*?)\">/si',$data, $ent);
  1834. // Replace entities
  1835. for ($i=0; $i<count($ent[0]); $i++) {
  1836. $data = preg_replace('/&'.preg_quote($ent[1][$i],'/').';/is', $ent[2][$i], $data);
  1837. }
  1838. }
  1839. // mPDF 4.4.003
  1840. if (preg_match('/xlink:href=/si',$data)) {
  1841. // Get links
  1842. preg_match_all('/(<(linearGradient|radialgradient)[^>]*)xlink:href=["\']#(.*?)["\'](.*?)\/>/si',$data, $links);
  1843. if (count($links[0])) { $links[5] = array(); } // mPDF 4.5.010
  1844. // Delete links from data - keeping in $links
  1845. for ($i=0; $i<count($links[0]); $i++) {
  1846. $links[5][$i] = 'tmpLink'.RAND(100000,9999999); // mPDF 4.5.010
  1847. $data = preg_replace('/'.preg_quote($links[0][$i],'/').'/is', '<MYLINKS'.$links[5][$i].'>' , $data); // mPDF 4.5.010
  1848. }
  1849. // Get targets
  1850. preg_match_all('/<(linearGradient|radialgradient)([^>]*)id=["\'](.*?)["\'](.*?)>(.*?)<\/(linearGradient|radialgradient)>/si',$data, $m);
  1851. $targets = array();
  1852. $stops = array();
  1853. // keeping in $targets
  1854. for ($i=0; $i<count($m[0]); $i++) {
  1855. $stops[$m[3][$i]] = $m[5][$i];
  1856. }
  1857. // Add back links this time as targets (gradients)
  1858. for ($i=0; $i<count($links[0]); $i++) {
  1859. $def = $links[1][$i] .' '.$links[4][$i].'>'. $stops[$links[3][$i]].'</'.$links[2][$i] .'>' ; // mPDF 4.5.010
  1860. $data = preg_replace('/<MYLINKS'.$links[5][$i].'>/is', $def , $data); // mPDF 4.5.010
  1861. }
  1862. }
  1863. // mPDF 4.4.003 - Removes <pattern>
  1864. $data = preg_replace('/<pattern.*?<\/pattern>/is', '', $data);
  1865. // mPDF 4.4.003 - Removes <marker>
  1866. $data = preg_replace('/<marker.*?<\/marker>/is', '', $data);
  1867. $this->svg_info['data'] = $data;
  1868. $this->svg_string = '';
  1869. //
  1870. // chargement unique des fonctions
  1871. if(!function_exists("xml_svg2pdf_start")){ // mPDF 5.3.76
  1872. function xml_svg2pdf_start($parser, $name, $attribs){
  1873. //
  1874. // definition
  1875. global $svg_class, $last_gradid;
  1876. // mPDF 4.4.003
  1877. $svg_class->xbase = 0;
  1878. $svg_class->ybase = 0;
  1879. switch (strtolower($name)){
  1880. // mPDF 5.0.039 - Don't output stuff inside <defs>
  1881. case 'defs':
  1882. $svg_class->inDefs = true;
  1883. return;
  1884. case 'svg':
  1885. $svg_class->svgOffset($attribs);
  1886. break;
  1887. case 'path':
  1888. $path = $attribs['d'];
  1889. // mPDF 4.4.003
  1890. preg_match_all('/([MZLHVCSQTAmzlhvcsqta])([e ,\-.\d]+)*/', $path, $commands, PREG_SET_ORDER);
  1891. $path_cmd = '';
  1892. $svg_class->subPathInit = true; // mPDF 4.4.003
  1893. // mPDF 5.0.039
  1894. $svg_class->pathBBox = array(999999,999999,-999999,-999999);
  1895. foreach($commands as $c){
  1896. if(count($c)==3 || $c[2]==''){
  1897. list($tmp, $command, $arguments) = $c;
  1898. }
  1899. else{
  1900. list($tmp, $command) = $c;
  1901. $arguments = '';
  1902. }
  1903. $path_cmd .= $svg_class->svgPath($command, $arguments);
  1904. }
  1905. // mPDF 5.0.039
  1906. if ($svg_class->pathBBox[2]==-1999998) { $svg_class->pathBBox[2] = 100; }
  1907. if ($svg_class->pathBBox[3]==-1999998) { $svg_class->pathBBox[3] = 100; }
  1908. if ($svg_class->pathBBox[0]==999999) { $svg_class->pathBBox[0] = 0; }
  1909. if ($svg_class->pathBBox[1]==999999) { $svg_class->pathBBox[1] = 0; }
  1910. $critere_style = $attribs;
  1911. unset($critere_style['d']);
  1912. $path_style = $svg_class->svgDefineStyle($critere_style);
  1913. break;
  1914. case 'rect':
  1915. if (!isset($attribs['x'])) {$attribs['x'] = 0;}
  1916. if (!isset($attribs['y'])) {$attribs['y'] = 0;}
  1917. if (!isset($attribs['rx'])) {$attribs['rx'] = 0;}
  1918. if (!isset($attribs['ry'])) {$attribs['ry'] = 0;}
  1919. $arguments = array(
  1920. 'x' => $attribs['x'],
  1921. 'y' => $attribs['y'],
  1922. 'w' => $attribs['width'],
  1923. 'h' => $attribs['height'],
  1924. 'rx' => $attribs['rx'],
  1925. 'ry' => $attribs['ry']
  1926. );
  1927. $path_cmd = $svg_class->svgRect($arguments);
  1928. $critere_style = $attribs;
  1929. unset($critere_style['x'],$critere_style['y'],$critere_style['rx'],$critere_style['ry'],$critere_style['height'],$critere_style['width']);
  1930. $path_style = $svg_class->svgDefineStyle($critere_style);
  1931. break;
  1932. case 'circle':
  1933. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1934. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1935. $arguments = array(
  1936. 'cx' => $attribs['cx'],
  1937. 'cy' => $attribs['cy'],
  1938. 'rx' => $attribs['r'],
  1939. 'ry' => $attribs['r']
  1940. );
  1941. $path_cmd = $svg_class->svgEllipse($arguments);
  1942. $critere_style = $attribs;
  1943. unset($critere_style['cx'],$critere_style['cy'],$critere_style['r']);
  1944. $path_style = $svg_class->svgDefineStyle($critere_style);
  1945. break;
  1946. case 'ellipse':
  1947. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1948. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1949. $arguments = array(
  1950. 'cx' => $attribs['cx'],
  1951. 'cy' => $attribs['cy'],
  1952. 'rx' => $attribs['rx'],
  1953. 'ry' => $attribs['ry']
  1954. );
  1955. $path_cmd = $svg_class->svgEllipse($arguments);
  1956. $critere_style = $attribs;
  1957. unset($critere_style['cx'],$critere_style['cy'],$critere_style['rx'],$critere_style['ry']);
  1958. $path_style = $svg_class->svgDefineStyle($critere_style);
  1959. break;
  1960. case 'line':
  1961. $arguments = array($attribs['x1'],$attribs['y1'],$attribs['x2'],$attribs['y2']);
  1962. $path_cmd = $svg_class->svgPolyline($arguments,false); // mPDF 4.4.003
  1963. $critere_style = $attribs;
  1964. unset($critere_style['x1'],$critere_style['y1'],$critere_style['x2'],$critere_style['y2']);
  1965. $path_style = $svg_class->svgDefineStyle($critere_style);
  1966. break;
  1967. case 'polyline':
  1968. $path = $attribs['points'];
  1969. preg_match_all('/[0-9\-\.]*/',$path, $tmp, PREG_SET_ORDER);
  1970. $arguments = array();
  1971. for ($i=0;$i<count($tmp);$i++){
  1972. if ($tmp[$i][0] !=''){
  1973. array_push($arguments, $tmp[$i][0]);
  1974. }
  1975. }
  1976. $path_cmd = $svg_class->svgPolyline($arguments);
  1977. $critere_style = $attribs;
  1978. unset($critere_style['points']);
  1979. $path_style = $svg_class->svgDefineStyle($critere_style);
  1980. break;
  1981. case 'polygon':
  1982. $path = $attribs['points'];
  1983. preg_match_all('/([\-]*[0-9\.]+)/',$path, $tmp);
  1984. $arguments = array();
  1985. for ($i=0;$i<count($tmp[0]);$i++){
  1986. if ($tmp[0][$i] !=''){
  1987. array_push($arguments, $tmp[0][$i]);
  1988. }
  1989. }
  1990. $path_cmd = $svg_class->svgPolygon($arguments);
  1991. // definition du style de la forme:
  1992. $critere_style = $attribs;
  1993. unset($critere_style['points']);
  1994. $path_style = $svg_class->svgDefineStyle($critere_style);
  1995. break;
  1996. case 'lineargradient':
  1997. $tmp_gradient = array(
  1998. 'type' => 'linear',
  1999. 'info' => array(
  2000. 'x1' => $attribs['x1'],
  2001. 'y1' => $attribs['y1'],
  2002. 'x2' => $attribs['x2'],
  2003. 'y2' => $attribs['y2']
  2004. ),
  2005. 'transform' => $attribs['gradientTransform'],
  2006. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  2007. 'spread' => $attribs['spreadMethod'], /* mPDF 5.0.040 */
  2008. 'color' => array()
  2009. );
  2010. $last_gradid = $attribs['id'];
  2011. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  2012. break;
  2013. case 'radialgradient':
  2014. $tmp_gradient = array(
  2015. 'type' => 'radial',
  2016. 'info' => array(
  2017. 'x0' => $attribs['cx'],
  2018. 'y0' => $attribs['cy'],
  2019. 'x1' => $attribs['fx'],
  2020. 'y1' => $attribs['fy'],
  2021. 'r' => $attribs['r']
  2022. ),
  2023. 'transform' => $attribs['gradientTransform'],
  2024. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  2025. 'spread' => $attribs['spreadMethod'], /* mPDF 5.0.040 */
  2026. 'color' => array()
  2027. );
  2028. $last_gradid = $attribs['id'];
  2029. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  2030. break;
  2031. case 'stop':
  2032. if (!$last_gradid) break;
  2033. // mPDF 4.4.003 // mPDF 5.0.040
  2034. if (isset($attribs['style']) AND preg_match('/stop-color:\s*([^;]*)/i',$attribs['style'],$m)) {
  2035. $color = trim($m[1]);
  2036. } else if (isset($attribs['stop-color'])) {
  2037. $color = $attribs['stop-color'];
  2038. }
  2039. $col = $svg_class->mpdf_ref->ConvertColor($color);
  2040. // mPDF 5.0.051
  2041. // mPDF 5.3.74
  2042. if ($col{0}==3 || $col{0}==5) { // RGB
  2043. $color_final = sprintf('%.3F %.3F %.3F',ord($col{1})/255,ord($col{2})/255,ord($col{3})/255);
  2044. $svg_class->svg_gradient[$last_gradid]['colorspace']='RGB';
  2045. }
  2046. else if ($col{0}==4 || $col{0}==6) { // CMYK
  2047. $color_final = sprintf('%.3F %.3F %.3F %.3F',ord($col{1})/100,ord($col{2})/100,ord($col{3})/100,ord($col{4})/100);
  2048. $svg_class->svg_gradient[$last_gradid]['colorspace']='CMYK';
  2049. }
  2050. else if ($col{0}==1) { // Grayscale
  2051. $color_final = sprintf('%.3F',ord($col{1})/255);
  2052. $svg_class->svg_gradient[$last_gradid]['colorspace']='Gray';
  2053. }
  2054. // mPDF 5.0.020
  2055. $stop_opacity = 1;
  2056. // mPDF 4.4.003
  2057. if (isset($attribs['style']) AND preg_match('/stop-opacity:\s*([0-9.]*)/i',$attribs['style'],$m)) {
  2058. $stop_opacity = $m[1];
  2059. } else if (isset($attribs['stop-opacity'])) {
  2060. $stop_opacity = $attribs['stop-opacity'];
  2061. }
  2062. // mPDF 5.0.051
  2063. // mPDF 5.3.74
  2064. else if ($col{0}==5) { // RGBa
  2065. $stop_opacity = ord($col{4}/100);
  2066. }
  2067. else if ($col{0}==6) { // CMYKa
  2068. $stop_opacity = ord($col{5}/100);
  2069. }
  2070. $tmp_color = array(
  2071. 'color' => $color_final,
  2072. 'offset' => $attribs['offset'],
  2073. 'opacity' => $stop_opacity
  2074. );
  2075. array_push($svg_class->svg_gradient[$last_gradid]['color'],$tmp_color);
  2076. break;
  2077. case 'a':
  2078. if (isset($attribs['xlink:href'])) {
  2079. unset($attribs['xlink:href']); // this should be a hyperlink
  2080. // not handled like a xlink:href in other elements
  2081. } // then continue like a <g>
  2082. case 'g':
  2083. $array_style = $svg_class->svgDefineStyle($attribs);
  2084. if ($array_style['transformations']) {
  2085. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  2086. }
  2087. array_push($svg_class->svg_style,$array_style);
  2088. $svg_class->svgDefineTxtStyle($attribs); // mPDF 4.4.003
  2089. break;
  2090. case 'text':
  2091. // mPDF 4.4.003
  2092. $array_style = $svg_class->svgDefineStyle($attribs);
  2093. if ($array_style['transformations']) {
  2094. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  2095. }
  2096. array_push($svg_class->svg_style,$array_style);
  2097. $svg_class->txt_data = array();
  2098. $svg_class->txt_data[0] = $attribs['x'];
  2099. $svg_class->txt_data[1] = $attribs['y'];
  2100. $critere_style = $attribs;
  2101. unset($critere_style['x'], $critere_style['y']);
  2102. $svg_class->svgDefineTxtStyle($critere_style);
  2103. break;
  2104. }
  2105. //
  2106. //insertion des path et du style dans le flux de donné general.
  2107. if (isset($path_cmd) && $path_cmd) { // mPDF 4.4.003
  2108. // mPDF 5.0
  2109. list($prestyle,$poststyle) = $svg_class->svgStyle($path_style, $attribs, strtolower($name));
  2110. if ($path_style['transformations']) { // transformation on an element
  2111. $svg_class->svgWriteString(" q ".$path_style['transformations']. " $prestyle $path_cmd $poststyle" . " Q\n");
  2112. }
  2113. else {
  2114. $svg_class->svgWriteString("$prestyle $path_cmd $poststyle\n");
  2115. }
  2116. }
  2117. }
  2118. function characterData($parser, $data)
  2119. {
  2120. global $svg_class;
  2121. if(isset($svg_class->txt_data[2])) {
  2122. $svg_class->txt_data[2] .= $data;
  2123. }
  2124. else {
  2125. $svg_class->txt_data[2] = $data;
  2126. }
  2127. }
  2128. function xml_svg2pdf_end($parser, $name){
  2129. global $svg_class;
  2130. switch($name){
  2131. case "g":
  2132. case "a":
  2133. $tmp = count($svg_class->svg_style)-1;
  2134. $current_style = $svg_class->svg_style[$tmp];
  2135. if ($current_style['transformations']) {
  2136. $svg_class->svgWriteString(" Q\n");
  2137. }
  2138. array_pop($svg_class->svg_style);
  2139. array_pop($svg_class->txt_style); // mPDF 4.4.003
  2140. break;
  2141. case 'radialgradient':
  2142. case 'lineargradient':
  2143. $last_gradid = '';
  2144. break;
  2145. case "text":
  2146. $path_cmd = $svg_class->svgText();
  2147. // echo 'path >> '.$path_cmd."<br><br>";
  2148. // echo "style >> ".$get_style[1]."<br><br>";
  2149. $svg_class->svgWriteString($path_cmd);
  2150. // mPDF 4.4.003
  2151. $tmp = count($svg_class->svg_style)-1;
  2152. $current_style = $svg_class->svg_style[$tmp];
  2153. if ($current_style['transformations']) {
  2154. $svg_class->svgWriteString(" Q\n");
  2155. }
  2156. array_pop($svg_class->svg_style);
  2157. break;
  2158. }
  2159. // mPDF 5.0.039 - Don't output stuff inside <defs>
  2160. if ($name == 'defs') {
  2161. $svg_class->inDefs = false;
  2162. }
  2163. }
  2164. }
  2165. $svg2pdf_xml='';
  2166. global $svg_class;
  2167. $svg_class = $this;
  2168. // mPDF 5.0.039 - Don't output stuff inside <defs>
  2169. $svg_class->inDefs = false;
  2170. $svg2pdf_xml_parser = xml_parser_create("utf-8");
  2171. xml_parser_set_option($svg2pdf_xml_parser, XML_OPTION_CASE_FOLDING, false);
  2172. xml_set_element_handler($svg2pdf_xml_parser, "xml_svg2pdf_start", "xml_svg2pdf_end");
  2173. xml_set_character_data_handler($svg2pdf_xml_parser, "characterData");
  2174. xml_parse($svg2pdf_xml_parser, $data);
  2175. // mPDF 4.4.003
  2176. if ($this->svg_error) { return false; }
  2177. else {
  2178. 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);
  2179. }
  2180. }
  2181. }
  2182. // END OF CLASS
  2183. // mPDF 5.0.040
  2184. function calc_bezier_bbox($start, $c) {
  2185. $P0 = array($start[0],$start[1]);
  2186. $P1 = array($c[0],$c[1]);
  2187. $P2 = array($c[2],$c[3]);
  2188. $P3 = array($c[4],$c[5]);
  2189. $bounds = array();
  2190. $bounds[0][] = $P0[0];
  2191. $bounds[1][] = $P0[1];
  2192. $bounds[0][] = $P3[0];
  2193. $bounds[1][] = $P3[1];
  2194. for ($i=0;$i<=1;$i++) {
  2195. $b = 6 * $P0[$i] - 12 * $P1[$i] + 6 * $P2[$i];
  2196. $a = -3 * $P0[$i] + 9 * $P1[$i] - 9 * $P2[$i] + 3 * $P3[$i];
  2197. $c = 3 * $P1[$i] - 3 * $P0[$i];
  2198. if ($a == 0) {
  2199. if ($b == 0) { continue; }
  2200. $t = -$c / $b;
  2201. if ($t>0 && $t<1) {
  2202. $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]);
  2203. }
  2204. continue;
  2205. }
  2206. $b2ac = pow($b, 2) - 4 * $c * $a;
  2207. if ($b2ac < 0) { continue; }
  2208. $t1 = (-$b + sqrt($b2ac))/(2 * $a);
  2209. if ($t1>0 && $t1<1) {
  2210. $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]);
  2211. }
  2212. $t2 = (-$b - sqrt($b2ac))/(2 * $a);
  2213. if ($t2>0 && $t2<1) {
  2214. $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]);
  2215. }
  2216. }
  2217. $x = min($bounds[0]);
  2218. $x2 = max($bounds[0]);
  2219. $y = min($bounds[1]);
  2220. $y2 = max($bounds[1]);
  2221. return array($x, $y, $x2, $y2);
  2222. }
  2223. // mPDF 5.0.040
  2224. function _testIntersectCircle($cx, $cy, $cr) {
  2225. // Tests whether a circle fully encloses a rectangle 0,0,1,1
  2226. // to see if any further radial gradients need adding (SVG)
  2227. // If centre of circle is inside 0,0,1,1 square
  2228. if ($cx >= 0 && $cx <= 1 && $cy >= 0 && $cy <= 1) {
  2229. $maxd = 1.5;
  2230. }
  2231. // distance to four corners
  2232. else {
  2233. $d1 = sqrt(pow(($cy-0),2) + pow(($cx-0),2));
  2234. $d2 = sqrt(pow(($cy-1),2) + pow(($cx-0),2));
  2235. $d3 = sqrt(pow(($cy-0),2) + pow(($cx-1),2));
  2236. $d4 = sqrt(pow(($cy-1),2) + pow(($cx-1),2));
  2237. $maxd = max($d1,$d2,$d3,$d4);
  2238. }
  2239. if ($cr < $maxd) { return true; }
  2240. else { return false; }
  2241. }
  2242. // mPDF 5.0.040
  2243. function _testIntersect($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) {
  2244. // Tests whether line (x1, y1) and (x2, y2) [a gradient axis (perpendicular)]
  2245. // intersects with a specific line segment (x3, y3) and (x4, y4)
  2246. $a1 = $y2-$y1;
  2247. $b1 = $x1-$x2;
  2248. $c1 = $a1*$x1+$b1*$y1;
  2249. $a2 = $y4-$y3;
  2250. $b2 = $x3-$x4;
  2251. $c2 = $a2*$x3+$b2*$y3;
  2252. $det = $a1*$b2 - $a2*$b1;
  2253. if($det == 0){ //Lines are parallel
  2254. return false;
  2255. }
  2256. else{
  2257. $x = ($b2*$c1 - $b1*$c2)/$det;
  2258. $y = ($a1*$c2 - $a2*$c1)/$det;
  2259. if ($x >= $x3 && $x <= $x4 && $y >= $y3 && $y <= $y4) { return true; }
  2260. }
  2261. return false;
  2262. }
  2263. ?>