svg.php 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. <?php
  2. // svg class modified for mPDF version 4.4.003 by Ian Back: based on -
  3. // svg2pdf fpdf class
  4. // sylvain briand (syb@godisaduck.com), modified by rick trevino (rtrevino1@yahoo.com)
  5. // http://www.godisaduck.com/svg2pdf_with_fpdf
  6. // http://rhodopsin.blogspot.com
  7. //
  8. // cette class etendue est open source, toute modification devra cependant etre repertoriée~
  9. // NB UNITS - Works in pixels as main units - converting to PDF units when outputing to PDF string
  10. // and on returning size
  11. class SVG {
  12. var $svg_gradient; // array - contient les infos sur les gradient fill du svg classé par id du svg
  13. var $svg_shadinglist; // array - contient les ids des objet shading
  14. var $svg_info; // array contenant les infos du svg voulue par l'utilisateur
  15. var $svg_attribs; // array - holds all attributes of root <svg> tag
  16. var $svg_style; // array contenant les style de groupes du svg
  17. var $svg_string; // String contenant le tracage du svg en lui même.
  18. var $txt_data; // array - holds string info to write txt to image
  19. var $txt_style; // array - current text style
  20. var $mpdf_ref;
  21. var $xbase; // mPDF 4.4.003
  22. var $ybase; // mPDF 4.4.003
  23. var $svg_error; // mPDF 4.4.003
  24. var $subPathInit; // mPDF 4.4.003
  25. var $spxstart; // mPDF 4.4.003
  26. var $spystart; // mPDF 4.4.003
  27. var $kp; // mPDF 4.4.003 convert pixels to PDF units
  28. function SVG(&$mpdf){
  29. $this->svg_gradient = array();
  30. $this->svg_shadinglist = array();
  31. $this->txt_data = array();
  32. $this->svg_string = '';
  33. $this->svg_info = array();
  34. $this->svg_attribs = array();
  35. $this->xbase = 0;
  36. $this->ybase = 0;
  37. $this->svg_error = false;
  38. $this->subPathInit = false; // mPDF 4.4.003
  39. $this->mpdf_ref =& $mpdf;
  40. $this->kp = 72 / $mpdf->img_dpi; // mPDF 4.4.003 constant To convert pixels to pts/PDF units
  41. $this->svg_style = array(
  42. array(
  43. 'fill' => 'black', // mPDF 4.4.008
  44. 'fill-opacity' => 1, // remplissage opaque par defaut
  45. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  46. 'stroke' => 'none', // pas de trait par defaut
  47. 'stroke-linecap' => 'butt', // style de langle par defaut
  48. 'stroke-linejoin' => 'miter', //
  49. 'stroke-miterlimit' => 4, // limite de langle par defaut
  50. 'stroke-opacity' => 1, // trait opaque par defaut
  51. 'stroke-width' => 1, // mPDF 4.4.011
  52. 'stroke-dasharray' => 0, // mPDF 4.4.003
  53. 'stroke-dashoffset' => 0, // mPDF 4.4.003
  54. 'color' => '' // mPDF 4.4.005
  55. )
  56. );
  57. $this->txt_style = array(
  58. array(
  59. 'fill' => 'black', // pas de remplissage par defaut
  60. 'font-family' => $mpdf->default_font,
  61. 'font-size' => $mpdf->default_font_size, // ****** this is pts
  62. 'font-weight' => 'normal', // normal | bold
  63. 'font-style' => 'normal', // italic | normal
  64. 'text-anchor' => 'start' // alignment: start, middle, end
  65. )
  66. );
  67. }
  68. function svgGradient($gradient_info, $attribs, $element){
  69. $n = count($this->mpdf_ref->gradients)+1;
  70. // Get bounding dimensions of element
  71. $w = 100;
  72. $h = 100;
  73. $x_offset = 0;
  74. $y_offset = 0;
  75. if ($element=='rect') {
  76. $w = $attribs['width'];
  77. $h = $attribs['height'];
  78. $x_offset = $attribs['x'];
  79. $y_offset = $attribs['y'];
  80. }
  81. else if ($element=='ellipse') {
  82. $w = $attribs['rx']*2;
  83. $h = $attribs['ry']*2;
  84. $x_offset = $attribs['cx']-$attribs['rx'];
  85. $y_offset = $attribs['cy']-$attribs['ry'];
  86. }
  87. else if ($element=='circle') {
  88. $w = $attribs['r']*2;
  89. $h = $attribs['r']*2;
  90. $x_offset = $attribs['cx']-$attribs['r'];
  91. $y_offset = $attribs['cy']-$attribs['r'];
  92. }
  93. else if ($element=='polygon') {
  94. $pts = preg_split('/[ ,]+/', trim($attribs['points']));
  95. $maxr=$maxb=0;
  96. $minl=$mint=999999;
  97. for ($i=0;$i<count($pts); $i++) {
  98. if ($i % 2 == 0) { // x values
  99. $minl = min($minl,$pts[$i]);
  100. $maxr = max($maxr,$pts[$i]);
  101. }
  102. else { // y values
  103. $mint = min($mint,$pts[$i]);
  104. $maxb = max($maxb,$pts[$i]);
  105. }
  106. }
  107. $w = $maxr-$minl;
  108. $h = $maxb-$mint;
  109. $x_offset = $minl;
  110. $y_offset = $mint;
  111. }
  112. else if ($element=='path') {
  113. preg_match_all('/([a-z]|[A-Z])([ ,\-.\d]+)*/', $attribs['d'], $commands, PREG_SET_ORDER);
  114. $maxr=$maxb=0;
  115. $minl=$mint=999999;
  116. foreach($commands as $c){
  117. if(count($c)==3){
  118. list($tmp, $cmd, $arg) = $c;
  119. if ($cmd=='M' || $cmd=='L' || $cmd=='C' || $cmd=='S' || $cmd=='Q' || $cmd=='T') {
  120. $pts = preg_split('/[ ,]+/', trim($arg));
  121. for ($i=0;$i<count($pts); $i++) {
  122. if ($i % 2 == 0) { // x values
  123. $minl = min($minl,$pts[$i]);
  124. $maxr = max($maxr,$pts[$i]);
  125. }
  126. else { // y values
  127. $mint = min($mint,$pts[$i]);
  128. $maxb = max($maxb,$pts[$i]);
  129. }
  130. }
  131. }
  132. if ($cmd=='H') { // sets new x
  133. $minl = min($minl,$arg);
  134. $maxr = max($maxr,$arg);
  135. }
  136. if ($cmd=='V') { // sets new y
  137. $mint = min($mint,$arg);
  138. $maxb = max($maxb,$arg);
  139. }
  140. }
  141. }
  142. $w = $maxr-$minl;
  143. $h = $maxb-$mint;
  144. $x_offset = $minl;
  145. $y_offset = $mint;
  146. }
  147. if (!$w) { $w = 100; }
  148. if (!$h) { $h = 100; }
  149. if ($x_offset==999999) { $x_offset = 0; }
  150. if ($y_offset==999999) { $y_offset = 0; }
  151. // mPDF 4.5.010
  152. // TRANSFORMATIONS
  153. $transformations = '';
  154. if (isset($gradient_info['transform'])){
  155. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$gradient_info['transform'],$m);
  156. if (count($m[0])) {
  157. for($i=0; $i<count($m[0]); $i++) {
  158. $c = strtolower($m[1][$i]);
  159. $v = trim($m[2][$i]);
  160. $vv = preg_split('/[ ,]+/',$v);
  161. if ($c=='matrix' && count($vv)==6) {
  162. $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);
  163. }
  164. else if ($c=='translate' && count($vv)) {
  165. $tm[4] = $vv[0];
  166. if (count($vv)==2) { $t_y = -$vv[1]; }
  167. else { $t_y = 0; }
  168. $tm[5] = $t_y;
  169. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  170. }
  171. else if ($c=='scale' && count($vv)) {
  172. if (count($vv)==2) { $s_y = $vv[1]; }
  173. else { $s_y = $vv[0]; }
  174. $tm[0] = $vv[0];
  175. $tm[3] = $s_y;
  176. $transformations .= sprintf(' %.3f 0 0 %.3f 0 0 cm ', $tm[0], $tm[3]);
  177. }
  178. else if ($c=='rotate' && count($vv)) {
  179. $tm[0] = cos(deg2rad(-$vv[0]));
  180. $tm[1] = sin(deg2rad(-$vv[0]));
  181. $tm[2] = -$tm[1];
  182. $tm[3] = $tm[0];
  183. if (count($vv)==3) {
  184. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  185. }
  186. $transformations .= sprintf(' %.3f %.3f %.3f %.3f 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  187. if (count($vv)==3) {
  188. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  189. }
  190. }
  191. else if ($c=='skewx' && count($vv)) {
  192. $tm[2] = tan(deg2rad(-$vv[0]));
  193. $transformations .= sprintf(' 1 0 %.3f 1 0 0 cm ', $tm[2]);
  194. }
  195. else if ($c=='skewy' && count($vv)) {
  196. $tm[1] = tan(deg2rad(-$vv[0]));
  197. $transformations .= sprintf(' 1 %.3f 0 1 0 0 cm ', $tm[1]);
  198. }
  199. }
  200. }
  201. }
  202. $return = "";
  203. // This ought to make it better - but makes it worse!
  204. // if ($transformations) { $return .= $transformations; } // mPDF 4.5.010
  205. if ($gradient_info['type'] == 'linear'){
  206. // mPDF 4.4.003
  207. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  208. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  209. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  210. if (isset($gradient_info['info']['x2'])) { $gradient_info['info']['x2'] = ($gradient_info['info']['x2']-$x_offset) / $w; }
  211. if (isset($gradient_info['info']['y2'])) { $gradient_info['info']['y2'] = ($gradient_info['info']['y2']-$y_offset) / $h; }
  212. }
  213. if (isset($gradient_info['info']['x1'])) { $x1 = $gradient_info['info']['x1']; }
  214. else { $x1 = 0; }
  215. if (isset($gradient_info['info']['y1'])) { $y1 = $gradient_info['info']['y1']; }
  216. else { $y1 = 0; }
  217. if (isset($gradient_info['info']['x2'])) { $x2 = $gradient_info['info']['x2']; }
  218. else { $x2 = 1; }
  219. if (isset($gradient_info['info']['y2'])) { $y2 = $gradient_info['info']['y2']; }
  220. else { $y2 = 0; }
  221. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  222. if (stristr($x2, '%')!== false) { $x2 = ($x2+0)/100; }
  223. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  224. if (stristr($y2, '%')!== false) { $y2 = ($y2+0)/100; }
  225. $a = $w; // width
  226. $b = 0;
  227. $c = 0;
  228. $d = -$h; // height
  229. $e = $x_offset; // x- offset
  230. $f = -$y_offset; // -y-offset
  231. $return .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm ', $a*$this->kp, $b, $c, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  232. // mPDF 4.4.007 Gradient STOPs
  233. $stops = count($gradient_info['color']);
  234. if ($stops < 2) { return ''; }
  235. for ($i=0; $i<($stops-1); $i++) {
  236. $first_stop = $gradient_info['color'][$i]['offset'];
  237. $last_stop = $gradient_info['color'][($i+1)]['offset'];
  238. if (stristr($first_stop, '%')!== false) { $first_stop = ($first_stop+0)/100; }
  239. if (stristr($last_stop, '%')!== false) { $last_stop = ($last_stop+0)/100; }
  240. if ($first_stop < 0) { $first_stop = 0; }
  241. if ($last_stop > 1) { $last_stop = 1; }
  242. if ($last_stop < $first_stop) { $last_stop = $first_stop; }
  243. $grx1 = $x1 + ($x2-$x1)*$first_stop;
  244. $gry1 = $y1 + ($y2-$y1)*$first_stop;
  245. $grx2 = $x1 + ($x2-$x1)*$last_stop;
  246. $gry2 = $y1 + ($y2-$y1)*$last_stop;
  247. $this->mpdf_ref->gradients[$n+$i]['type'] = 2;
  248. $this->mpdf_ref->gradients[$n+$i]['coords']=array($grx1, $gry1, $grx2, $gry2);
  249. if (!$gradient_info['color'][$i]['color']) { $gradient_info['color'][$i]['color'] = '0 0 0'; }
  250. if (!$gradient_info['color'][$i+1]['color']) { $gradient_info['color'][$i+1]['color'] = '0 0 0'; }
  251. $this->mpdf_ref->gradients[$n+$i]['col1'] = $gradient_info['color'][$i]['color'];
  252. $this->mpdf_ref->gradients[$n+$i]['col2'] = $gradient_info['color'][$i+1]['color'];
  253. if ($i == 0 && $i == ($stops-2) )
  254. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','true');
  255. else if ($i==0)
  256. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','false');
  257. else if ($i == ($stops-2) )
  258. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','true');
  259. else
  260. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','false');
  261. $return .= '/Sh'.($n+$i).' sh ';
  262. }
  263. $return .= ' Q ';
  264. }
  265. else if ($gradient_info['type'] == 'radial'){
  266. // mPDF 4.4.003
  267. if (isset($gradient_info['units']) && strtolower($gradient_info['units'])=='userspaceonuse') {
  268. if ($w > $h) { $h = $w; }
  269. else { $w = $h; }
  270. if (isset($gradient_info['info']['x0'])) { $gradient_info['info']['x0'] = ($gradient_info['info']['x0']-$x_offset) / $w; }
  271. if (isset($gradient_info['info']['y0'])) { $gradient_info['info']['y0'] = ($gradient_info['info']['y0']-$y_offset) / $h; }
  272. if (isset($gradient_info['info']['x1'])) { $gradient_info['info']['x1'] = ($gradient_info['info']['x1']-$x_offset) / $w; }
  273. if (isset($gradient_info['info']['y1'])) { $gradient_info['info']['y1'] = ($gradient_info['info']['y1']-$y_offset) / $h; }
  274. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['rx'] = $gradient_info['info']['r'] / $w; }
  275. if (isset($gradient_info['info']['r'])) { $gradient_info['info']['ry'] = $gradient_info['info']['r'] / $h; }
  276. }
  277. if ($gradient_info['info']['x0'] || $gradient_info['info']['x0']===0) { $x0 = $gradient_info['info']['x0']; }
  278. else { $x0 = 0.5; }
  279. if ($gradient_info['info']['y0'] || $gradient_info['info']['y0']===0) { $y0 = $gradient_info['info']['y0']; }
  280. else { $y0 = 0.5; }
  281. if ($gradient_info['info']['rx'] || $gradient_info['info']['rx']===0) { $rx = $gradient_info['info']['rx']; }
  282. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $rx = $gradient_info['info']['r']; }
  283. else { $rx = 0.5; }
  284. if ($gradient_info['info']['ry'] || $gradient_info['info']['ry']===0) { $ry = $gradient_info['info']['ry']; }
  285. else if ($gradient_info['info']['r'] || $gradient_info['info']['r']===0) { $ry = $gradient_info['info']['r']; }
  286. else { $ry = 0.5; }
  287. if ($gradient_info['info']['x1'] || $gradient_info['info']['x1']===0) { $x1 = $gradient_info['info']['x1']; }
  288. else { $x1 = $x0; }
  289. if ($gradient_info['info']['y1'] || $gradient_info['info']['y1']===0) { $y1 = $gradient_info['info']['y1']; }
  290. else { $y1 = $y0; }
  291. if (stristr($x1, '%')!== false) { $x1 = ($x1+0)/100; }
  292. if (stristr($x0, '%')!== false) { $x0 = ($x0+0)/100; }
  293. if (stristr($y1, '%')!== false) { $y1 = ($y1+0)/100; }
  294. if (stristr($y0, '%')!== false) { $y0 = ($y0+0)/100; }
  295. if (stristr($rx, '%')!== false) { $rx = ($rx+0)/100; }
  296. if (stristr($ry, '%')!== false) { $ry = ($ry+0)/100; }
  297. $r = $rx;
  298. $a = $w; // width
  299. $b = 0;
  300. $c = 0;
  301. $d = -$h; // -height
  302. $e = $x_offset; // x- offset
  303. $f = -$y_offset; // -y-offset
  304. $return .= sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm ', $a*$this->kp, $b, $c, $d*$this->kp, $e*$this->kp, $f*$this->kp);
  305. // x1 and y1 (fx, fy) should be inside the circle defined by x0 y0 and r else error in mPDF
  306. while (pow(($x1-$x0),2) + pow(($y1 - $y0),2) >= pow($r,2)) { $r += 0.05; }
  307. // mPDF 4.4.007 Gradient STOPs
  308. $stops = count($gradient_info['color']);
  309. if ($stops < 2) { return ''; }
  310. for ($i=0; $i<($stops-1); $i++) {
  311. $first_stop = $gradient_info['color'][$i]['offset'];
  312. $last_stop = $gradient_info['color'][($i+1)]['offset'];
  313. if (stristr($first_stop, '%')!== false) { $first_stop = ($first_stop+0)/100; }
  314. if (stristr($last_stop, '%')!== false) { $last_stop = ($last_stop+0)/100; }
  315. if ($first_stop < 0) { $first_stop = 0; }
  316. if ($last_stop > 1) { $last_stop = 1; }
  317. if ($last_stop < $first_stop) { $last_stop = $first_stop; }
  318. $grx1 = $x1 + ($x0-$x1)*$first_stop;
  319. $gry1 = $y1 + ($y0-$y1)*$first_stop;
  320. $grx2 = $x1 + ($x0-$x1)*$last_stop;
  321. $gry2 = $y1 + ($y0-$y1)*$last_stop;
  322. $grir = $r*$first_stop;
  323. $grr = $r*$last_stop;
  324. $this->mpdf_ref->gradients[$n+$i]['type'] = 3;
  325. $this->mpdf_ref->gradients[$n+$i]['coords']=array($grx1, $gry1, $grx2, $gry2, abs($grr), abs($grir) );
  326. if (!$gradient_info['color'][$i]['color']) { $gradient_info['color'][$i]['color'] = '0 0 0'; }
  327. if (!$gradient_info['color'][$i+1]['color']) { $gradient_info['color'][$i+1]['color'] = '0 0 0'; }
  328. $this->mpdf_ref->gradients[$n+$i]['col1'] = $gradient_info['color'][$i]['color'];
  329. $this->mpdf_ref->gradients[$n+$i]['col2'] = $gradient_info['color'][$i+1]['color'];
  330. if ($i == 0 && $i == ($stops-2) )
  331. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','true');
  332. else if ($i == 0 )
  333. $this->mpdf_ref->gradients[$n+$i]['extend']=array('true','false');
  334. else if ($i == ($stops-2) )
  335. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','true');
  336. else
  337. $this->mpdf_ref->gradients[$n+$i]['extend']=array('false','false');
  338. $return .= '/Sh'.($n+$i).' sh ';
  339. }
  340. $return .= ' Q ';
  341. }
  342. return $return;
  343. }
  344. function svgOffset ($attribs){
  345. // save all <svg> tag attributes
  346. $this->svg_attribs = $attribs;
  347. if(isset($this->svg_attribs['viewBox'])) {
  348. $vb = preg_split('/\s+/is', trim($this->svg_attribs['viewBox']));
  349. if (count($vb)==4) {
  350. $this->svg_info['x'] = $vb[0];
  351. $this->svg_info['y'] = $vb[1];
  352. $this->svg_info['w'] = $vb[2];
  353. $this->svg_info['h'] = $vb[3];
  354. return;
  355. }
  356. }
  357. $svg_w = $this->mpdf_ref->ConvertSize($attribs['width']); // mm (interprets numbers as pixels)
  358. $svg_h = $this->mpdf_ref->ConvertSize($attribs['height']); // mm
  359. // Added to handle file without height or width specified
  360. if (!$svg_w && !$svg_h) { $svg_w = $svg_h = $this->mpdf_ref->blk[$this->mpdf_ref->blklvl]['inner_width'] ; } // DEFAULT
  361. if (!$svg_w) { $svg_w = $svg_h; }
  362. if (!$svg_h) { $svg_h = $svg_w; }
  363. $this->svg_info['x'] = 0;
  364. $this->svg_info['y'] = 0;
  365. $this->svg_info['w'] = $svg_w/0.2645; // mm->pixels
  366. $this->svg_info['h'] = $svg_h/0.2645; // mm->pixels
  367. }
  368. //
  369. // check if points are within svg, if not, set to max
  370. function svg_overflow($x,$y)
  371. {
  372. $x2 = $x;
  373. $y2 = $y;
  374. if(isset($this->svg_attribs['overflow']))
  375. {
  376. if($this->svg_attribs['overflow'] == 'hidden')
  377. {
  378. // Not sure if this is supposed to strip off units, but since I dont use any I will omlt this step
  379. $svg_w = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['width']);
  380. $svg_h = preg_replace("/([0-9\.]*)(.*)/i","$1",$this->svg_attribs['height']);
  381. // $xmax = floor($this->svg_attribs['width']);
  382. $xmax = floor($svg_w);
  383. $xmin = 0;
  384. // $ymax = floor(($this->svg_attribs['height'] * -1));
  385. $ymax = floor(($svg_h * -1));
  386. $ymin = 0;
  387. if($x > $xmax) $x2 = $xmax; // right edge
  388. if($x < $xmin) $x2 = $xmin; // left edge
  389. if($y < $ymax) $y2 = $ymax; // bottom
  390. if($y > $ymin) $y2 = $ymin; // top
  391. }
  392. }
  393. return array( 'x' => $x2, 'y' => $y2);
  394. }
  395. function svgDefineStyle($critere_style){
  396. $tmp = count($this->svg_style)-1;
  397. $current_style = $this->svg_style[$tmp];
  398. unset($current_style['transformations']);
  399. // TRANSFORM SCALE
  400. $transformations = '';
  401. if (isset($critere_style['transform'])){
  402. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is',$critere_style['transform'],$m);
  403. if (count($m[0])) {
  404. for($i=0; $i<count($m[0]); $i++) {
  405. $c = strtolower($m[1][$i]);
  406. $v = trim($m[2][$i]);
  407. $vv = preg_split('/[ ,]+/',$v);
  408. if ($c=='matrix' && count($vv)==6) {
  409. $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);
  410. }
  411. else if ($c=='translate' && count($vv)) {
  412. $tm[4] = $vv[0];
  413. if (count($vv)==2) { $t_y = -$vv[1]; }
  414. else { $t_y = 0; }
  415. $tm[5] = $t_y;
  416. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $tm[4]*$this->kp, $tm[5]*$this->kp);
  417. }
  418. else if ($c=='scale' && count($vv)) {
  419. if (count($vv)==2) { $s_y = $vv[1]; }
  420. else { $s_y = $vv[0]; }
  421. $tm[0] = $vv[0];
  422. $tm[3] = $s_y;
  423. $transformations .= sprintf(' %.3f 0 0 %.3f 0 0 cm ', $tm[0], $tm[3]);
  424. }
  425. else if ($c=='rotate' && count($vv)) {
  426. $tm[0] = cos(deg2rad(-$vv[0]));
  427. $tm[1] = sin(deg2rad(-$vv[0]));
  428. $tm[2] = -$tm[1];
  429. $tm[3] = $tm[0];
  430. if (count($vv)==3) {
  431. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', $vv[1]*$this->kp, -$vv[2]*$this->kp);
  432. }
  433. $transformations .= sprintf(' %.3f %.3f %.3f %.3f 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  434. if (count($vv)==3) {
  435. $transformations .= sprintf(' 1 0 0 1 %.3f %.3f cm ', -$vv[1]*$this->kp, $vv[2]*$this->kp);
  436. }
  437. }
  438. else if ($c=='skewx' && count($vv)) {
  439. $tm[2] = tan(deg2rad(-$vv[0]));
  440. $transformations .= sprintf(' 1 0 %.3f 1 0 0 cm ', $tm[2]);
  441. }
  442. else if ($c=='skewy' && count($vv)) {
  443. $tm[1] = tan(deg2rad(-$vv[0]));
  444. $transformations .= sprintf(' 1 %.3f 0 1 0 0 cm ', $tm[1]);
  445. }
  446. }
  447. }
  448. $current_style['transformations'] = $transformations;
  449. }
  450. if (isset($critere_style['style'])){
  451. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  452. $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);
  453. }
  454. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']); // mPDF 4.4.003
  455. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  456. }
  457. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  458. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  459. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  460. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  461. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  462. $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);
  463. }
  464. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  465. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  466. }
  467. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  468. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  469. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  470. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  471. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  472. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  473. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  474. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  475. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  476. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  477. // mPDF 4.4.003
  478. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  479. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  480. // mPDF 4.4.003
  481. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  482. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  483. }
  484. if(isset($critere_style['fill'])){
  485. $current_style['fill'] = $critere_style['fill'];
  486. }
  487. if(isset($critere_style['fill-opacity'])){
  488. $current_style['fill-opacity'] = $critere_style['fill-opacity'];
  489. }
  490. if(isset($critere_style['fill-rule'])){
  491. $current_style['fill-rule'] = $critere_style['fill-rule'];
  492. }
  493. if(isset($critere_style['stroke'])){
  494. $current_style['stroke'] = $critere_style['stroke'];
  495. }
  496. if(isset($critere_style['stroke-linecap'])){
  497. $current_style['stroke-linecap'] = $critere_style['stroke-linecap'];
  498. }
  499. if(isset($critere_style['stroke-linejoin'])){
  500. $current_style['stroke-linejoin'] = $critere_style['stroke-linejoin'];
  501. }
  502. if(isset($critere_style['stroke-miterlimit'])){
  503. $current_style['stroke-miterlimit'] = $critere_style['stroke-miterlimit'];
  504. }
  505. if(isset($critere_style['stroke-opacity'])){
  506. $current_style['stroke-opacity'] = $critere_style['stroke-opacity'];
  507. }
  508. if(isset($critere_style['stroke-width'])){
  509. $current_style['stroke-width'] = $critere_style['stroke-width'];
  510. }
  511. // mPDF 4.4.003
  512. if(isset($critere_style['stroke-dasharray'])){
  513. $current_style['stroke-dasharray'] = $critere_style['stroke-dasharray'];
  514. }
  515. if(isset($critere_style['stroke-dashoffset'])){
  516. $current_style['stroke-dashoffset'] = $critere_style['stroke-dashoffset'];
  517. }
  518. // mPDF 4.4.005 Used as indirect setting for currentColor
  519. if(isset($critere_style['color']) && $critere_style['color'] != 'inherit'){
  520. $current_style['color'] = $critere_style['color'];
  521. }
  522. return $current_style;
  523. }
  524. //
  525. // Cette fonction ecrit le style dans le stream svg.
  526. function svgStyle($critere_style, $attribs, $element){
  527. $path_style = '';
  528. if (substr_count($critere_style['fill'],'url')>0){
  529. //
  530. // couleur degradé
  531. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['fill']);
  532. if ($id_gradient != $critere_style['fill']) {
  533. if (isset($this->svg_gradient[$id_gradient])) {
  534. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  535. if ($fill_gradient) { // mPDF 4.4.003
  536. $path_style = "q ";
  537. $w = "W";
  538. $style .= 'N';
  539. }
  540. }
  541. }
  542. }
  543. // mPDF 4.4.005 Used as indirect setting for currentColor
  544. else if (strtolower($critere_style['fill']) == 'currentcolor'){
  545. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  546. if ($col) {
  547. $path_style .= sprintf('%.3f %.3f %.3f rg ',$col['R']/255,$col['G']/255,$col['B']/255);
  548. $style .= 'F';
  549. }
  550. }
  551. else if ($critere_style['fill'] != 'none'){
  552. // fill couleur pleine
  553. $col = $this->mpdf_ref->ConvertColor($critere_style['fill']);
  554. if ($col) {
  555. $path_style .= sprintf('%.3f %.3f %.3f rg ',$col['R']/255,$col['G']/255,$col['B']/255);
  556. $style .= 'F';
  557. }
  558. }
  559. // mPDF 4.4.005 Used as indirect setting for currentColor
  560. if (strtolower($critere_style['stroke']) == 'currentcolor'){
  561. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  562. if ($col) {
  563. $path_style .= sprintf('%.3f %.3f %.3f RG ',$col['R']/255,$col['G']/255,$col['B']/255);
  564. $style .= 'D';
  565. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']);
  566. $path_style .= sprintf('%.3f w ',$lw*$this->kp);
  567. }
  568. }
  569. else if ($critere_style['stroke'] != 'none'){
  570. $col = $this->mpdf_ref->ConvertColor($critere_style['stroke']);
  571. if ($col) {
  572. $path_style .= sprintf('%.3f %.3f %.3f RG ',$col['R']/255,$col['G']/255,$col['B']/255);
  573. $style .= 'D';
  574. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']); // mPDF 4.4.003
  575. $path_style .= sprintf('%.3f w ',$lw*$this->kp);
  576. }
  577. }
  578. if ($critere_style['stroke'] != 'none'){
  579. if ($critere_style['stroke-linejoin'] == 'miter'){
  580. $path_style .= ' 0 j ';
  581. }
  582. else if ($critere_style['stroke-linejoin'] == 'round'){
  583. $path_style .= ' 1 j ';
  584. }
  585. else if ($critere_style['stroke-linejoin'] == 'bevel'){
  586. $path_style .= ' 2 j ';
  587. }
  588. if ($critere_style['stroke-linecap'] == 'butt'){
  589. $path_style .= ' 0 J ';
  590. }
  591. else if ($critere_style['stroke-linecap'] == 'round'){
  592. $path_style .= ' 1 J ';
  593. }
  594. else if ($critere_style['stroke-linecap'] == 'square'){
  595. $path_style .= ' 2 J ';
  596. }
  597. if (isset($critere_style['stroke-miterlimit'])){
  598. if ($critere_style['stroke-miterlimit'] == 'none'){
  599. }
  600. else if (preg_match('/^[\d.]+$/',$critere_style['stroke-miterlimit'])) {
  601. $path_style .= sprintf('%.2f M ',$critere_style['stroke-miterlimit']);
  602. }
  603. }
  604. // mPDF 4.4.003
  605. if (isset($critere_style['stroke-dasharray'])){
  606. $off = 0;
  607. $d = preg_split('/[ ,]/',$critere_style['stroke-dasharray']);
  608. if (count($d) == 1 && $d[0]==0) {
  609. $path_style .= '[] 0 d ';
  610. }
  611. else {
  612. if (count($d) % 2 == 1) { $d = array_merge($d, $d); } // 5, 3, 1 => 5,3,1,5,3,1 OR 3 => 3,3
  613. $arr = '';
  614. for($i=0; $i<count($d); $i+=2) {
  615. $arr .= sprintf('%.3f %.3f ', $d[$i]*$this->kp, $d[$i+1]*$this->kp);
  616. }
  617. if (isset($critere_style['stroke-dashoffset'])){ $off = $critere_style['stroke-dashoffset'] + 0; }
  618. $path_style .= sprintf('[%s] %.3f d ', $arr, $off*$this->kp);
  619. }
  620. }
  621. }
  622. // mPDF 4.4.003
  623. if ($critere_style['fill-rule']=='evenodd') { $fr = '*'; }
  624. else { $fr = ''; }
  625. // mPDF 4.4.003
  626. if (isset($critere_style['fill-opacity'])) {
  627. $opacity = 1;
  628. if ($critere_style['fill-opacity'] == 0) { $opacity = 0; }
  629. else if ($critere_style['fill-opacity'] > 1) { $opacity = 1; }
  630. else if ($critere_style['fill-opacity'] > 0) { $opacity = $critere_style['fill-opacity']; }
  631. else if ($critere_style['fill-opacity'] < 0) { $opacity = 0; }
  632. $gs = $this->mpdf_ref->AddExtGState(array('ca'=>$opacity, 'BM'=>'/Normal'));
  633. $path_style .= sprintf(' /GS%d gs ', $gs);
  634. }
  635. // mPDF 4.4.003
  636. if (isset($critere_style['stroke-opacity'])) {
  637. $opacity = 1;
  638. if ($critere_style['stroke-opacity'] == 0) { $opacity = 0; }
  639. else if ($critere_style['stroke-opacity'] > 1) { $opacity = 1; }
  640. else if ($critere_style['stroke-opacity'] > 0) { $opacity = $critere_style['stroke-opacity']; }
  641. else if ($critere_style['stroke-opacity'] < 0) { $opacity = 0; }
  642. $gs = $this->mpdf_ref->AddExtGState(array('CA'=>$opacity, 'BM'=>'/Normal'));
  643. $path_style .= sprintf(' /GS%d gs ', $gs);
  644. }
  645. switch ($style){
  646. case 'F':
  647. $op = 'f';
  648. break;
  649. case 'FD':
  650. $op = 'B';
  651. break;
  652. case 'ND':
  653. $op = 'S';
  654. break;
  655. case 'D':
  656. $op = 'S';
  657. break;
  658. default:
  659. $op = 'n';
  660. }
  661. $final_style = "$path_style $w $op$fr $fill_gradient \n";
  662. // echo 'svgStyle: '. $final_style .'<br><br>';
  663. return $final_style;
  664. }
  665. //
  666. // fonction retracant les <path />
  667. function svgPath($command, $arguments){
  668. $path_cmd = '';
  669. $newsubpath = false; // mPDF 4.4.003
  670. // mPDF 4.4.003
  671. preg_match_all('/[\-^]?[\d.]+(e[\-]?[\d]+){0,1}/i', $arguments, $a, PREG_SET_ORDER);
  672. // if the command is a capital letter, the coords go absolute, otherwise relative
  673. if(strtolower($command) == $command) $relative = true;
  674. else $relative = false;
  675. $ile_argumentow = count($a);
  676. // each command may have different needs for arguments [1 to 8]
  677. switch(strtolower($command)){
  678. case 'm': // move
  679. for($i = 0; $i<$ile_argumentow; $i+=2){
  680. $x = $a[$i][0];
  681. $y = $a[$i+1][0];
  682. if($relative){
  683. $pdfx = ($this->xbase + $x);
  684. $pdfy = ($this->ybase - $y);
  685. $this->xbase += $x;
  686. $this->ybase += -$y;
  687. }
  688. else{
  689. $pdfx = $x;
  690. $pdfy = -$y ;
  691. $this->xbase = $x;
  692. $this->ybase = -$y;
  693. }
  694. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  695. if($i == 0) $path_cmd .= sprintf('%.3f %.3f m ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  696. else $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  697. // mPDF 4.4.003 Save start points of subpath
  698. if ($this->subPathInit) {
  699. $this->spxstart = $this->xbase;
  700. $this->spystart = $this->ybase;
  701. $this->subPathInit = false;
  702. }
  703. }
  704. break;
  705. case 'l': // a simple line
  706. for($i = 0; $i<$ile_argumentow; $i+=2){
  707. $x = ($a[$i][0]);
  708. $y = ($a[$i+1][0]);
  709. if($relative){
  710. $pdfx = ($this->xbase + $x);
  711. $pdfy = ($this->ybase - $y);
  712. $this->xbase += $x;
  713. $this->ybase += -$y;
  714. }
  715. else{
  716. $pdfx = $x ;
  717. $pdfy = -$y ;
  718. $this->xbase = $x;
  719. $this->ybase = -$y;
  720. }
  721. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  722. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  723. }
  724. break;
  725. case 'h': // a very simple horizontal line
  726. for($i = 0; $i<$ile_argumentow; $i++){
  727. $x = ($a[$i][0]);
  728. if($relative){
  729. $y = 0;
  730. $pdfx = ($this->xbase + $x) ;
  731. $pdfy = ($this->ybase - $y) ;
  732. $this->xbase += $x;
  733. $this->ybase += -$y;
  734. }
  735. else{
  736. $y = -$this->ybase;
  737. $pdfx = $x;
  738. $pdfy = -$y;
  739. $this->xbase = $x;
  740. $this->ybase = -$y;
  741. }
  742. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  743. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  744. }
  745. break;
  746. case 'v': // the simplest line, vertical
  747. for($i = 0; $i<$ile_argumentow; $i++){
  748. $y = ($a[$i][0]);
  749. if($relative){
  750. $x = 0;
  751. $pdfx = ($this->xbase + $x);
  752. $pdfy = ($this->ybase - $y);
  753. $this->xbase += $x;
  754. $this->ybase += -$y;
  755. }
  756. else{
  757. $x = $this->xbase;
  758. $pdfx = $x;
  759. $pdfy = -$y;
  760. $this->xbase = $x;
  761. $this->ybase = -$y;
  762. }
  763. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  764. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  765. }
  766. break;
  767. case 's': // bezier with first vertex equal first control
  768. // mPDF 4.4.003
  769. if (!($this->lastcommand == 'C' || $this->lastcommand == 'c' || $this->lastcommand == 'S' || $this->lastcommand == 's')) {
  770. $this->lastcontrolpoints = array(0,0);
  771. }
  772. for($i = 0; $i<$ile_argumentow; $i += 4){
  773. $x1 = $this->lastcontrolpoints[0];
  774. $y1 = $this->lastcontrolpoints[1];
  775. $x2 = ($a[$i][0]);
  776. $y2 = ($a[$i+1][0]);
  777. $x = ($a[$i+2][0]);
  778. $y = ($a[$i+3][0]);
  779. if($relative){
  780. $pdfx1 = ($this->xbase + $x1);
  781. $pdfy1 = ($this->ybase - $y1);
  782. $pdfx2 = ($this->xbase + $x2);
  783. $pdfy2 = ($this->ybase - $y2);
  784. $pdfx = ($this->xbase + $x);
  785. $pdfy = ($this->ybase - $y);
  786. $this->xbase += $x;
  787. $this->ybase += -$y;
  788. }
  789. else{
  790. $pdfx1 = $this->xbase + $x1;
  791. $pdfy1 = $this->ybase -$y1;
  792. $pdfx2 = $x2;
  793. $pdfy2 = -$y2;
  794. $pdfx = $x;
  795. $pdfy = -$y;
  796. $this->xbase = $x;
  797. $this->ybase = -$y;
  798. }
  799. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  800. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  801. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  802. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  803. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  804. {
  805. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  806. }
  807. else
  808. {
  809. $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);
  810. }
  811. }
  812. break;
  813. case 'c': // bezier with second vertex equal second control
  814. for($i = 0; $i<$ile_argumentow; $i += 6){
  815. $x1 = ($a[$i][0]);
  816. $y1 = ($a[$i+1][0]);
  817. $x2 = ($a[$i+2][0]);
  818. $y2 = ($a[$i+3][0]);
  819. $x = ($a[$i+4][0]);
  820. $y = ($a[$i+5][0]);
  821. if($relative){
  822. $pdfx1 = ($this->xbase + $x1);
  823. $pdfy1 = ($this->ybase - $y1);
  824. $pdfx2 = ($this->xbase + $x2);
  825. $pdfy2 = ($this->ybase - $y2);
  826. $pdfx = ($this->xbase + $x);
  827. $pdfy = ($this->ybase - $y);
  828. $this->xbase += $x;
  829. $this->ybase += -$y;
  830. }
  831. else{
  832. $pdfx1 = $x1;
  833. $pdfy1 = -$y1;
  834. $pdfx2 = $x2;
  835. $pdfy2 = -$y2;
  836. $pdfx = $x;
  837. $pdfy = -$y;
  838. $this->xbase = $x;
  839. $this->ybase = -$y;
  840. }
  841. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  842. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  843. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  844. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  845. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  846. {
  847. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  848. }
  849. else
  850. {
  851. $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);
  852. }
  853. }
  854. break;
  855. case 'q': // bezier quadratic avec point de control
  856. for($i = 0; $i<$ile_argumentow; $i += 4){
  857. $x1 = ($a[$i][0]);
  858. $y1 = ($a[$i+1][0]);
  859. $x = ($a[$i+2][0]);
  860. $y = ($a[$i+3][0]);
  861. if($relative){
  862. $pdfx = ($this->xbase + $x);
  863. $pdfy = ($this->ybase - $y);
  864. $pdfx1 = ($this->xbase + ($x1*2/3));
  865. $pdfy1 = ($this->ybase - ($y1*2/3));
  866. // mPDF 4.4.003
  867. $pdfx2 = $pdfx1 + 1/3 *($x);
  868. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  869. $this->xbase += $x;
  870. $this->ybase += -$y;
  871. }
  872. else{
  873. $pdfx = $x;
  874. $pdfy = -$y;
  875. $pdfx1 = ($this->xbase+(($x1-$this->xbase)*2/3));
  876. $pdfy1 = ($this->ybase-(($y1+$this->ybase)*2/3));
  877. $pdfx2 = ($x+(($x1-$x)*2/3));
  878. $pdfy2 = (-$y-(($y1-$y)*2/3));
  879. // mPDF 4.4.003
  880. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  881. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  882. $this->xbase = $x;
  883. $this->ybase = -$y;
  884. }
  885. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  886. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  887. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  888. $pdf_pt = $this->svg_overflow($pdfx,$pdfy);
  889. if( ($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy) )
  890. {
  891. $path_cmd .= sprintf('%.3f %.3f l ', $pdf_pt['x']*$this->kp, $pdf_pt['y']*$this->kp);
  892. }
  893. else
  894. {
  895. $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);
  896. }
  897. }
  898. break;
  899. case 't': // bezier quadratic avec point de control simetrique a lancien point de control
  900. // mPDF 4.4.003
  901. if (!($this->lastcommand == 'Q' || $this->lastcommand == 'q' || $this->lastcommand == 'T' || $this->lastcommand == 't')) {
  902. $this->lastcontrolpoints = array(0,0);
  903. }
  904. for($i = 0; $i<$ile_argumentow; $i += 2){
  905. $x = ($a[$i][0]);
  906. $y = ($a[$i+1][0]);
  907. $x1 = $this->lastcontrolpoints[0];
  908. $y1 = $this->lastcontrolpoints[1];
  909. if($relative){
  910. $pdfx = ($this->xbase + $x);
  911. $pdfy = ($this->ybase - $y);
  912. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  913. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  914. // mPDF 4.4.003
  915. $pdfx2 = $pdfx1 + 1/3 *($x);
  916. $pdfy2 = $pdfy1 + 1/3 *(-$y) ;
  917. $this->xbase += $x;
  918. $this->ybase += -$y;
  919. }
  920. else{
  921. $pdfx = $x;
  922. $pdfy = -$y;
  923. $pdfx1 = ($this->xbase + ($x1)); // mPDF 4.4.003
  924. $pdfy1 = ($this->ybase - ($y1)); // mPDF 4.4.003
  925. // mPDF 4.4.003
  926. $pdfx2 = $pdfx1 + 1/3 *($x - $this->xbase);
  927. $pdfy2 = $pdfy1 + 1/3 *(-$y - $this->ybase) ;
  928. $this->xbase = $x;
  929. $this->ybase = -$y;
  930. }
  931. $this->lastcontrolpoints = array(($pdfx-$pdfx2),-($pdfy-$pdfy2)); // mPDF 4.4.003 always relative
  932. $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);
  933. }
  934. break;
  935. case 'a': // Elliptical arc
  936. for($i = 0; $i<$ile_argumentow; $i += 7){
  937. $rx = ($a[$i][0]);
  938. $ry = ($a[$i+1][0]);
  939. $angle = ($a[$i+2][0]); //x-axis-rotation
  940. $largeArcFlag = ($a[$i+3][0]);
  941. $sweepFlag = ($a[$i+4][0]);
  942. $x2 = ($a[$i+5][0]);
  943. $y2 = ($a[$i+6][0]);
  944. $x1 = $this->xbase;
  945. $y1 = -$this->ybase;
  946. if($relative){
  947. $x2 = $this->xbase + $x2;
  948. $y2 = -$this->ybase + $y2;
  949. $this->xbase += ($a[$i+5][0]);
  950. $this->ybase += -($a[$i+6][0]);
  951. }
  952. else{
  953. $this->xbase = $x2;
  954. $this->ybase = -$y2;
  955. }
  956. $path_cmd .= $this->Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag);
  957. }
  958. break;
  959. case'z':
  960. $path_cmd .= 'h ';
  961. // mPDF 4.4.003
  962. $this->subPathInit = true;
  963. $newsubpath = true;
  964. $this->xbase = $this->spxstart;
  965. $this->ybase = $this->spystart;
  966. break;
  967. default:
  968. break;
  969. }
  970. if (!$newsubpath) { $this->subPathInit = false; } // mPDF 4.4.003
  971. $this->lastcommand = $command;
  972. return $path_cmd;
  973. }
  974. function Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag) {
  975. // 1. Treat out-of-range parameters as described in
  976. // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  977. // If the endpoints (x1, y1) and (x2, y2) are identical, then this
  978. // is equivalent to omitting the elliptical arc segment entirely
  979. if ($x1 == $x2 && $y1 == $y2) return '';
  980. // If rX = 0 or rY = 0 then this arc is treated as a straight line
  981. // segment (a "lineto") joining the endpoints.
  982. if ($rx == 0.0 || $ry == 0.0) {
  983. // return Lineto(x2, y2); // ****
  984. }
  985. // If rX or rY have negative signs, these are dropped; the absolute
  986. // value is used instead.
  987. if ($rx<0.0) $rx = -$rx;
  988. if ($ry<0.0) $ry = -$ry;
  989. // 2. convert to center parameterization as shown in
  990. // http://www.w3.org/TR/SVG/implnote.html
  991. $sinPhi = sin(deg2rad($angle));
  992. $cosPhi = cos(deg2rad($angle));
  993. $x1dash = $cosPhi * ($x1-$x2)/2.0 + $sinPhi * ($y1-$y2)/2.0;
  994. $y1dash = -$sinPhi * ($x1-$x2)/2.0 + $cosPhi * ($y1-$y2)/2.0;
  995. $numerator = $rx*$rx*$ry*$ry - $rx*$rx*$y1dash*$y1dash - $ry*$ry*$x1dash*$x1dash;
  996. if ($numerator < 0.0) {
  997. // If rX , rY and are such that there is no solution (basically,
  998. // the ellipse is not big enough to reach from (x1, y1) to (x2,
  999. // y2)) then the ellipse is scaled up uniformly until there is
  1000. // exactly one solution (until the ellipse is just big enough).
  1001. // -> find factor s, such that numerator' with rx'=s*rx and
  1002. // ry'=s*ry becomes 0 :
  1003. $s = sqrt(1.0 - $numerator/($rx*$rx*$ry*$ry));
  1004. $rx *= $s;
  1005. $ry *= $s;
  1006. $root = 0.0;
  1007. }
  1008. else {
  1009. $root = ($largeArcFlag == $sweepFlag ? -1.0 : 1.0) * sqrt( $numerator/($rx*$rx*$y1dash*$y1dash+$ry*$ry*$x1dash*$x1dash) );
  1010. }
  1011. $cxdash = $root*$rx*$y1dash/$ry;
  1012. $cydash = -$root*$ry*$x1dash/$rx;
  1013. $cx = $cosPhi * $cxdash - $sinPhi * $cydash + ($x1+$x2)/2.0;
  1014. $cy = $sinPhi * $cxdash + $cosPhi * $cydash + ($y1+$y2)/2.0;
  1015. $theta1 = $this->CalcVectorAngle(1.0, 0.0, ($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry);
  1016. $dtheta = $this->CalcVectorAngle(($x1dash-$cxdash)/$rx, ($y1dash-$cydash)/$ry, (-$x1dash-$cxdash)/$rx, (-$y1dash-$cydash)/$ry);
  1017. if (!$sweepFlag && $dtheta>0)
  1018. $dtheta -= 2.0*M_PI;
  1019. else if ($sweepFlag && $dtheta<0)
  1020. $dtheta += 2.0*M_PI;
  1021. // 3. convert into cubic bezier segments <= 90deg
  1022. $segments = ceil(abs($dtheta/(M_PI/2.0)));
  1023. $delta = $dtheta/$segments;
  1024. $t = 8.0/3.0 * sin($delta/4.0) * sin($delta/4.0) / sin($delta/2.0);
  1025. $coords = array();
  1026. for ($i = 0; $i < $segments; $i++) {
  1027. $cosTheta1 = cos($theta1);
  1028. $sinTheta1 = sin($theta1);
  1029. $theta2 = $theta1 + $delta;
  1030. $cosTheta2 = cos($theta2);
  1031. $sinTheta2 = sin($theta2);
  1032. // a) calculate endpoint of the segment:
  1033. $xe = $cosPhi * $rx*$cosTheta2 - $sinPhi * $ry*$sinTheta2 + $cx;
  1034. $ye = $sinPhi * $rx*$cosTheta2 + $cosPhi * $ry*$sinTheta2 + $cy;
  1035. // b) calculate gradients at start/end points of segment:
  1036. $dx1 = $t * ( - $cosPhi * $rx*$sinTheta1 - $sinPhi * $ry*$cosTheta1);
  1037. $dy1 = $t * ( - $sinPhi * $rx*$sinTheta1 + $cosPhi * $ry*$cosTheta1);
  1038. $dxe = $t * ( $cosPhi * $rx*$sinTheta2 + $sinPhi * $ry*$cosTheta2);
  1039. $dye = $t * ( $sinPhi * $rx*$sinTheta2 - $cosPhi * $ry*$cosTheta2);
  1040. // c) draw the cubic bezier:
  1041. $coords[$i] = array(($x1+$dx1), ($y1+$dy1), ($xe+$dxe), ($ye+$dye), $xe, $ye);
  1042. // do next segment
  1043. $theta1 = $theta2;
  1044. $x1 = $xe;
  1045. $y1 = $ye;
  1046. }
  1047. $path = ' ';
  1048. foreach($coords AS $c) {
  1049. $cpx1 = $c[0];
  1050. $cpy1 = $c[1];
  1051. $cpx2 = $c[2];
  1052. $cpy2 = $c[3];
  1053. $x2 = $c[4];
  1054. $y2 = $c[5];
  1055. $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";
  1056. }
  1057. return $path ;
  1058. }
  1059. function CalcVectorAngle($ux, $uy, $vx, $vy) {
  1060. $ta = atan2($uy, $ux);
  1061. $tb = atan2($vy, $vx);
  1062. if ($tb >= $ta)
  1063. return ($tb-$ta);
  1064. return (6.28318530718 - ($ta-$tb));
  1065. }
  1066. // mPDF 4.4.003
  1067. function ConvertSVGSizePixels($size=5,$maxsize='x'){
  1068. // maxsize in pixels (user units) or 'y' or 'x'
  1069. // e.g. $w = $this->ConvertSVGSizePixels($arguments['w'],$this->svg_info['w']*(25.4/$this->mpdf_ref->dpi));
  1070. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1071. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1072. // For text $maxsize = Fontsize
  1073. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1074. if ($maxsize == 'y') { $maxsize = $this->svg_info['h']; }
  1075. else if ($maxsize == 'x') { $maxsize = $this->svg_info['w']; }
  1076. $maxsize *= (25.4/$this->mpdf_ref->dpi); // convert pixels to mm
  1077. $fontsize=$this->mpdf_ref->FontSize;
  1078. //Return as pixels
  1079. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,$fontsize,false) * 1/(25.4/$this->mpdf_ref->dpi);
  1080. return $size;
  1081. }
  1082. // mPDF 4.4.003
  1083. function ConvertSVGSizePts($size=5){
  1084. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1085. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1086. // For text $maxsize = Fontsize
  1087. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1088. $maxsize=$this->mpdf_ref->FontSize;
  1089. //Return as pts
  1090. $size = $this->mpdf_ref->ConvertSize($size,$maxsize,false,true) * 72/25.4;
  1091. return $size;
  1092. }
  1093. //
  1094. // fonction retracant les <rect />
  1095. function svgRect($arguments){
  1096. if ($arguments['h']==0 || $arguments['w']==0) { return ''; } // mPDF 4.4.003
  1097. $x = $this->ConvertSVGSizePixels($arguments['x'],'x'); // mPDF 4.4.003
  1098. $y = $this->ConvertSVGSizePixels($arguments['y'],'y'); // mPDF 4.4.003
  1099. $h = $this->ConvertSVGSizePixels($arguments['h'],'y'); // mPDF 4.4.003
  1100. $w = $this->ConvertSVGSizePixels($arguments['w'],'x'); // mPDF 4.4.003
  1101. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1102. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1103. if ($rx > $w/2) { $rx = $w/2; } // mPDF 4.4.003
  1104. if ($ry > $h/2) { $ry = $h/2; } // mPDF 4.4.003
  1105. if ($rx>0 and $ry == 0){$ry = $rx;}
  1106. if ($ry>0 and $rx == 0){$rx = $ry;}
  1107. if ($rx == 0 and $ry == 0){
  1108. // trace un rectangle sans angle arrondit
  1109. $path_cmd = sprintf('%.3f %.3f m ', ($x*$this->kp), -($y*$this->kp));
  1110. $path_cmd .= sprintf('%.3f %.3f l ', (($x+$w)*$this->kp), -($y*$this->kp));
  1111. $path_cmd .= sprintf('%.3f %.3f l ', (($x+$w)*$this->kp), -(($y+$h)*$this->kp));
  1112. $path_cmd .= sprintf('%.3f %.3f l ', ($x)*$this->kp, -(($y+$h)*$this->kp));
  1113. $path_cmd .= sprintf('%.3f %.3f l h ', ($x*$this->kp), -($y*$this->kp));
  1114. }
  1115. else {
  1116. // trace un rectangle avec les arrondit
  1117. // les points de controle du bezier sont deduis grace a la constante kappa
  1118. $kappa = 4*(sqrt(2)-1)/3;
  1119. $kx = $kappa*$rx;
  1120. $ky = $kappa*$ry;
  1121. $path_cmd = sprintf('%.3f %.3f m ', ($x+$rx)*$this->kp, -$y*$this->kp);
  1122. $path_cmd .= sprintf('%.3f %.3f l ', ($x+($w-$rx))*$this->kp, -$y*$this->kp);
  1123. $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 );
  1124. $path_cmd .= sprintf('%.3f %.3f l ', ($x+$w)*$this->kp, (-$y+(-$h+$ry))*$this->kp);
  1125. $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 );
  1126. $path_cmd .= sprintf('%.3f %.3f l ', ($x+$rx)*$this->kp, (-$y+(-$h))*$this->kp);
  1127. $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 );
  1128. $path_cmd .= sprintf('%.3f %.3f l ', $x*$this->kp, (-$y+(-$ry))*$this->kp);
  1129. $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 );
  1130. }
  1131. return $path_cmd;
  1132. }
  1133. //
  1134. // fonction retracant les <ellipse /> et <circle />
  1135. // le cercle est tracé grave a 4 bezier cubic, les poitn de controles
  1136. // sont deduis grace a la constante kappa * rayon
  1137. function svgEllipse($arguments){
  1138. if ($arguments['rx']==0 || $arguments['ry']==0) { return ''; } // mPDF 4.4.003
  1139. $kappa = 4*(sqrt(2)-1)/3;
  1140. $cx = $this->ConvertSVGSizePixels($arguments['cx'],'x'); // mPDF 4.4.003
  1141. $cy = $this->ConvertSVGSizePixels($arguments['cy'],'y'); // mPDF 4.4.003
  1142. $rx = $this->ConvertSVGSizePixels($arguments['rx'],'x'); // mPDF 4.4.003
  1143. $ry = $this->ConvertSVGSizePixels($arguments['ry'],'y'); // mPDF 4.4.003
  1144. $x1 = $cx;
  1145. $y1 = -$cy+$ry;
  1146. $x2 = $cx+$rx;
  1147. $y2 = -$cy;
  1148. $x3 = $cx;
  1149. $y3 = -$cy-$ry;
  1150. $x4 = $cx-$rx;
  1151. $y4 = -$cy;
  1152. $path_cmd = sprintf('%.3f %.3f m ', $x1*$this->kp, $y1*$this->kp);
  1153. $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);
  1154. $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);
  1155. $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);
  1156. $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);
  1157. $path_cmd .= 'h ';
  1158. return $path_cmd;
  1159. }
  1160. //
  1161. // fonction retracant les <polyline /> et les <line />
  1162. function svgPolyline($arguments,$ispolyline=true){
  1163. if ($ispolyline) {
  1164. $xbase = $arguments[0] ;
  1165. $ybase = - $arguments[1] ;
  1166. }
  1167. else {
  1168. if ($arguments[0]==$arguments[2] && $arguments[1]==$arguments[3]) { return ''; } // mPDF 4.4.003 Zero length line
  1169. $xbase = $this->ConvertSVGSizePixels($arguments[0],'x'); // mPDF 4.4.003
  1170. $ybase = - $this->ConvertSVGSizePixels($arguments[1],'y'); // mPDF 4.4.003
  1171. }
  1172. $path_cmd = sprintf('%.3f %.3f m ', $xbase*$this->kp, $ybase*$this->kp);
  1173. for ($i = 2; $i<count($arguments);$i += 2) {
  1174. if ($ispolyline) {
  1175. $tmp_x = $arguments[$i] ;
  1176. $tmp_y = - $arguments[($i+1)] ;
  1177. }
  1178. else {
  1179. $tmp_x = $this->ConvertSVGSizePixels($arguments[$i],'x') ; // mPDF 4.4.003
  1180. $tmp_y = - $this->ConvertSVGSizePixels($arguments[($i+1)],'y') ; // mPDF 4.4.003
  1181. }
  1182. $path_cmd .= sprintf('%.3f %.3f l ', $tmp_x*$this->kp, $tmp_y*$this->kp);
  1183. }
  1184. // $path_cmd .= 'h '; // ?? In error - don't close subpath here
  1185. return $path_cmd;
  1186. }
  1187. //
  1188. // fonction retracant les <polygone />
  1189. function svgPolygon($arguments){
  1190. $xbase = $arguments[0] ;
  1191. $ybase = - $arguments[1] ;
  1192. $path_cmd = sprintf('%.3f %.3f m ', $xbase*$this->kp, $ybase*$this->kp);
  1193. for ($i = 2; $i<count($arguments);$i += 2) {
  1194. $tmp_x = $arguments[$i] ;
  1195. $tmp_y = - $arguments[($i+1)] ;
  1196. $path_cmd .= sprintf('%.3f %.3f l ', $tmp_x*$this->kp, $tmp_y*$this->kp);
  1197. }
  1198. $path_cmd .= sprintf('%.3f %.3f l ', $xbase*$this->kp, $ybase*$this->kp);
  1199. $path_cmd .= 'h ';
  1200. return $path_cmd;
  1201. }
  1202. //
  1203. // write string to image
  1204. function svgText() {
  1205. // $tmp = count($this->txt_style)-1;
  1206. $current_style = array_pop($this->txt_style);
  1207. $style = '';
  1208. $render = -1;
  1209. if(isset($this->txt_data[2]))
  1210. {
  1211. // select font
  1212. $style .= ($current_style['font-weight'] == 'bold')?'B':'';
  1213. $style .= ($current_style['font-style'] == 'italic')?'I':'';
  1214. $size = $current_style['font-size']; // mPDF 4.4.003
  1215. // mPDF 4.5.010
  1216. if ($this->mpdf_ref->is_MB && $current_style['font-family'] == 'times') {
  1217. $current_style['font-family'] = $this->mpdf_ref->SetFont('serif',$style,$size,false);
  1218. }
  1219. else if ($this->mpdf_ref->is_MB && $current_style['font-family'] == 'courier') {
  1220. $current_style['font-family'] = $this->mpdf_ref->SetFont('mono',$style,$size,false);
  1221. }
  1222. else {
  1223. $current_style['font-family'] = $this->mpdf_ref->SetFont($current_style['font-family'],$style,$size,false);
  1224. }
  1225. // mPDF 4.4.003
  1226. if (isset($current_style['fill']) && $current_style['fill']!='none') {
  1227. $col = $this->mpdf_ref->ConvertColor($current_style['fill']);
  1228. $render = "0"; // Fill only
  1229. }
  1230. $strokestr = '';
  1231. if (isset($current_style['stroke-width']) && $current_style['stroke-width']>0 && $current_style['stroke']!='none') {
  1232. $scol = $this->mpdf_ref->ConvertColor($current_style['stroke']);
  1233. if ($scol) { $strokestr .= sprintf('%.3f %.3f %.3f RG ',$scol['R']/255,$scol['G']/255,$scol['B']/255); }
  1234. $linewidth = $this->ConvertSVGSizePixels($current_style['stroke-width']);
  1235. if ($linewidth > 0) {
  1236. $strokestr .= sprintf('%.3f w 1 J 1 j ',$linewidth*$this->kp);
  1237. if ($render == -1) { $render = "1"; } // stroke only
  1238. else { $render = "2"; } // fill and stroke
  1239. }
  1240. }
  1241. if ($render == -1) { return ''; }
  1242. $x = $this->ConvertSVGSizePixels($this->txt_data[0],'x'); // mPDF 4.4.003
  1243. $y = $this->ConvertSVGSizePixels($this->txt_data[1],'y'); // mPDF 4.4.003
  1244. $txt = $this->txt_data[2];
  1245. // mPDF 4.4.003
  1246. $txt = preg_replace('/\f/','',$txt);
  1247. $txt = preg_replace('/\r/','',$txt);
  1248. $txt = preg_replace('/\n/',' ',$txt);
  1249. $txt = preg_replace('/\t/',' ',$txt);
  1250. $txt = preg_replace("/[ ]+/u",' ',$txt);
  1251. $txt = trim($txt);
  1252. $txt = $this->mpdf_ref->purify_utf8_text($txt);
  1253. if ($this->mpdf_ref->text_input_as_HTML) {
  1254. $txt = $this->mpdf_ref->all_entities_to_utf8($txt);
  1255. }
  1256. if (!$this->mpdf_ref->is_MB) { $txt = mb_convert_encoding($txt,$this->mpdf_ref->mb_enc,'UTF-8'); }
  1257. $this->mpdf_ref->magic_reverse_dir($txt);
  1258. $this->mpdf_ref->ConvertIndic($txt);
  1259. if (preg_match("/([".$this->mpdf_ref->pregRTLchars."])/u", $txt)) { $this->mpdf_ref->biDirectional = true; } // mPDF 4.4.003
  1260. if ($current_style['text-anchor']=='middle') {
  1261. $tw = $this->mpdf_ref->GetStringWidth($txt)*$this->mpdf_ref->k/2; // mPDF 4.4.003
  1262. }
  1263. else if ($current_style['text-anchor']=='end') {
  1264. $tw = $this->mpdf_ref->GetStringWidth($txt)*$this->mpdf_ref->k; // mPDF 4.4.003
  1265. }
  1266. else $tw = 0;
  1267. if ($this->mpdf_ref->useSubsets && $this->mpdf_ref->CurrentFont['type']=='Type1subset' && !$this->mpdf_ref->isCJK && !$this->mpdf_ref->usingCoreFont) {
  1268. $txt = $this->mpdf_ref->UTF8toSubset($txt);
  1269. }
  1270. else {
  1271. if ($this->mpdf_ref->is_MB && !$this->mpdf_ref->usingCoreFont) {
  1272. $txt= $this->mpdf_ref->UTF8ToUTF16BE($txt, false);
  1273. }
  1274. $txt='('.$this->mpdf_ref->_escape($txt).')';
  1275. }
  1276. $this->mpdf_ref->CurrentFont['used']= true;
  1277. $pdfx = $x - $tw/$this->kp; // mPDF 4.4.009
  1278. $pdfy = -$y ;
  1279. $xbase = $x;
  1280. $ybase = -$y;
  1281. // mPDF 4.4.003
  1282. $path_cmd = sprintf('q BT /F%d %.3f Tf %.3f %.3f Td %s Tr %.3f %.3f %.3f rg %s %s Tj ET Q ',$this->mpdf_ref->CurrentFont['i'],$this->mpdf_ref->FontSizePt,$pdfx*$this->kp,$pdfy*$this->kp,$render,$col['R']/255,$col['G']/255,$col['B']/255,$strokestr,$txt);
  1283. unset($this->txt_data[0], $this->txt_data[1],$this->txt_data[2]);
  1284. }
  1285. else
  1286. {
  1287. return ' '; // mPDF 4.4.010
  1288. }
  1289. $path_cmd .= 'h ';
  1290. return $path_cmd;
  1291. }
  1292. function svgDefineTxtStyle($critere_style)
  1293. {
  1294. // get copy of current/default txt style, and modify it with supplied attributes
  1295. $tmp = count($this->txt_style)-1;
  1296. $current_style = $this->txt_style[$tmp];
  1297. // mPDF 4.5.010
  1298. if (isset($critere_style['style'])){
  1299. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1300. $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);
  1301. }
  1302. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']); // mPDF 4.4.003
  1303. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  1304. }
  1305. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1306. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  1307. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1308. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  1309. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1310. $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);
  1311. }
  1312. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1313. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  1314. }
  1315. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1316. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  1317. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1318. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  1319. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1320. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  1321. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1322. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  1323. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1324. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  1325. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  1326. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  1327. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1328. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  1329. }
  1330. if (isset($critere_style['font'])){
  1331. // [ [ <'font-style'> || <'font-variant'> || <'font-weight'> ]?<'font-size'> [ / <'line-height'> ]? <'font-family'> ]
  1332. $tmp = preg_replace("/(.*)(italic|oblique)(.*)/i","$2",$critere_style['font']);
  1333. if ($tmp != $critere_style['font']){
  1334. if($tmp == 'oblique'){
  1335. $tmp = 'italic';
  1336. }
  1337. $current_style['font-style'] = $tmp;
  1338. }
  1339. $tmp = preg_replace("/(.*)(bold|bolder)(.*)/i","$2",$critere_style['font']);
  1340. if ($tmp != $critere_style['font']){
  1341. if($tmp == 'bolder'){
  1342. $tmp = 'bold';
  1343. }
  1344. $current_style['font-weight'] = $tmp;
  1345. }
  1346. // select digits not followed by percent sign nor preceeded by forward slash
  1347. $tmp = preg_replace("/(.*)\b(\d+)[\b|\/](.*)/i","$2",$critere_style['font']);
  1348. if ($tmp != $critere_style['font']){
  1349. // mPDF 4.4.003
  1350. $current_style['font-size'] = $this->ConvertSVGSizePts($tmp);
  1351. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1352. }
  1353. }
  1354. if(isset($critere_style['fill'])){
  1355. $current_style['fill'] = $critere_style['fill'];
  1356. }
  1357. // mPDF 4.4.003
  1358. if(isset($critere_style['stroke'])){
  1359. $current_style['stroke'] = $critere_style['stroke'];
  1360. }
  1361. if(isset($critere_style['stroke-width'])){
  1362. $current_style['stroke-width'] = $critere_style['stroke-width'];
  1363. }
  1364. if(isset($critere_style['font-style'])){
  1365. if(strtolower($critere_style['font-style']) == 'oblique')
  1366. {
  1367. $critere_style['font-style'] = 'italic';
  1368. }
  1369. $current_style['font-style'] = $critere_style['font-style'];
  1370. }
  1371. if(isset($critere_style['font-weight'])){
  1372. if(strtolower($critere_style['font-weight']) == 'bolder')
  1373. {
  1374. $critere_style['font-weight'] = 'bold';
  1375. }
  1376. $current_style['font-weight'] = $critere_style['font-weight'];
  1377. }
  1378. if(isset($critere_style['font-size'])){
  1379. // mPDF 4.4.003
  1380. $current_style['font-size'] = $this->ConvertSVGSizePts($critere_style['font-size']);
  1381. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1382. }
  1383. if(isset($critere_style['font-family'])){
  1384. // mPDF 4.4.003
  1385. $v = $critere_style['font-family'];
  1386. $aux_fontlist = explode(",",$v);
  1387. $fonttype = trim($aux_fontlist[0]);
  1388. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  1389. $aux_fontlist = explode(" ",$fonttype);
  1390. $fonttype = $aux_fontlist[0];
  1391. $current_style['font-family'] = strtolower(trim($fonttype));
  1392. }
  1393. if(isset($critere_style['text-anchor'])){
  1394. $current_style['text-anchor'] = $critere_style['text-anchor'];
  1395. }
  1396. // add current style to text style array (will remove it later after writing text to svg_string)
  1397. array_push($this->txt_style,$current_style);
  1398. }
  1399. //
  1400. // fonction ajoutant un gradient
  1401. function svgAddGradient($id,$array_gradient){
  1402. $this->svg_gradient[$id] = $array_gradient;
  1403. }
  1404. //
  1405. // Ajoute une couleur dans le gradient correspondant
  1406. //
  1407. // function ecrivant dans le svgstring
  1408. function svgWriteString($content){
  1409. $this->svg_string .= $content;
  1410. }
  1411. // analise le svg et renvoie aux fonctions precedente our le traitement
  1412. function ImageSVG($data){
  1413. $this->svg_info = array();
  1414. // mPDF 4.4.006
  1415. if (preg_match('/<!ENTITY/si',$data)) {
  1416. // Get User-defined entities
  1417. preg_match_all('/<!ENTITY\s+([a-z]+)\s+\"(.*?)\">/si',$data, $ent);
  1418. // Replace entities
  1419. for ($i=0; $i<count($ent[0]); $i++) {
  1420. $data = preg_replace('/&'.preg_quote($ent[1][$i],'/').';/is', $ent[2][$i], $data);
  1421. }
  1422. }
  1423. // mPDF 4.4.003
  1424. if (preg_match('/xlink:href=/si',$data)) {
  1425. // Get links
  1426. preg_match_all('/(<(linearGradient|radialgradient)[^>]*)xlink:href=["\']#(.*?)["\'](.*?)\/>/si',$data, $links);
  1427. if (count($links[0])) { $links[5] = array(); } // mPDF 4.5.010
  1428. // Delete links from data - keeping in $links
  1429. for ($i=0; $i<count($links[0]); $i++) {
  1430. $links[5][$i] = 'tmpLink'.RAND(100000,9999999); // mPDF 4.5.010
  1431. $data = preg_replace('/'.preg_quote($links[0][$i],'/').'/is', '<MYLINKS'.$links[5][$i].'>' , $data); // mPDF 4.5.010
  1432. }
  1433. // Get targets
  1434. preg_match_all('/<(linearGradient|radialgradient)([^>]*)id=["\'](.*?)["\'](.*?)>(.*?)<\/(linearGradient|radialgradient)>/si',$data, $m);
  1435. $targets = array();
  1436. $stops = array();
  1437. // keeping in $targets
  1438. for ($i=0; $i<count($m[0]); $i++) {
  1439. $stops[$m[3][$i]] = $m[5][$i];
  1440. }
  1441. // Add back links this time as targets (gradients)
  1442. for ($i=0; $i<count($links[0]); $i++) {
  1443. $def = $links[1][$i] .' '.$links[4][$i].'>'. $stops[$links[3][$i]].'</'.$links[2][$i] .'>' ; // mPDF 4.5.010
  1444. $data = preg_replace('/<MYLINKS'.$links[5][$i].'>/is', $def , $data); // mPDF 4.5.010
  1445. }
  1446. }
  1447. // mPDF 4.4.003 - Removes <pattern>
  1448. $data = preg_replace('/<pattern.*?<\/pattern>/is', '', $data);
  1449. // mPDF 4.4.003 - Removes <marker>
  1450. $data = preg_replace('/<marker.*?<\/marker>/is', '', $data);
  1451. $this->svg_info['data'] = $data;
  1452. $this->svg_string = '';
  1453. //
  1454. // chargement unique des fonctions
  1455. if(!function_exists(xml_svg2pdf_start)){
  1456. function xml_svg2pdf_start($parser, $name, $attribs){
  1457. //
  1458. // definition
  1459. global $svg_class, $last_gradid;
  1460. // mPDF 4.4.003
  1461. $svg_class->xbase = 0;
  1462. $svg_class->ybase = 0;
  1463. switch (strtolower($name)){
  1464. case 'svg':
  1465. $svg_class->svgOffset($attribs);
  1466. break;
  1467. case 'path':
  1468. $path = $attribs['d'];
  1469. // mPDF 4.4.003
  1470. preg_match_all('/([MZLHVCSQTAmzlhvcsqta])([e ,\-.\d]+)*/', $path, $commands, PREG_SET_ORDER);
  1471. $path_cmd = '';
  1472. $svg_class->subPathInit = true; // mPDF 4.4.003
  1473. foreach($commands as $c){
  1474. if(count($c)==3 || $c[2]==''){
  1475. list($tmp, $command, $arguments) = $c;
  1476. }
  1477. else{
  1478. list($tmp, $command) = $c;
  1479. $arguments = '';
  1480. }
  1481. $path_cmd .= $svg_class->svgPath($command, $arguments);
  1482. }
  1483. $critere_style = $attribs;
  1484. unset($critere_style['d']);
  1485. $path_style = $svg_class->svgDefineStyle($critere_style);
  1486. break;
  1487. case 'rect':
  1488. if (!isset($attribs['x'])) {$attribs['x'] = 0;}
  1489. if (!isset($attribs['y'])) {$attribs['y'] = 0;}
  1490. if (!isset($attribs['rx'])) {$attribs['rx'] = 0;}
  1491. if (!isset($attribs['ry'])) {$attribs['ry'] = 0;}
  1492. $arguments = array(
  1493. 'x' => $attribs['x'],
  1494. 'y' => $attribs['y'],
  1495. 'w' => $attribs['width'],
  1496. 'h' => $attribs['height'],
  1497. 'rx' => $attribs['rx'],
  1498. 'ry' => $attribs['ry']
  1499. );
  1500. $path_cmd = $svg_class->svgRect($arguments);
  1501. $critere_style = $attribs;
  1502. unset($critere_style['x'],$critere_style['y'],$critere_style['rx'],$critere_style['ry'],$critere_style['height'],$critere_style['width']);
  1503. $path_style = $svg_class->svgDefineStyle($critere_style);
  1504. break;
  1505. case 'circle':
  1506. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1507. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1508. $arguments = array(
  1509. 'cx' => $attribs['cx'],
  1510. 'cy' => $attribs['cy'],
  1511. 'rx' => $attribs['r'],
  1512. 'ry' => $attribs['r']
  1513. );
  1514. $path_cmd = $svg_class->svgEllipse($arguments);
  1515. $critere_style = $attribs;
  1516. unset($critere_style['cx'],$critere_style['cy'],$critere_style['r']);
  1517. $path_style = $svg_class->svgDefineStyle($critere_style);
  1518. break;
  1519. case 'ellipse':
  1520. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1521. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1522. $arguments = array(
  1523. 'cx' => $attribs['cx'],
  1524. 'cy' => $attribs['cy'],
  1525. 'rx' => $attribs['rx'],
  1526. 'ry' => $attribs['ry']
  1527. );
  1528. $path_cmd = $svg_class->svgEllipse($arguments);
  1529. $critere_style = $attribs;
  1530. unset($critere_style['cx'],$critere_style['cy'],$critere_style['rx'],$critere_style['ry']);
  1531. $path_style = $svg_class->svgDefineStyle($critere_style);
  1532. break;
  1533. case 'line':
  1534. $arguments = array($attribs['x1'],$attribs['y1'],$attribs['x2'],$attribs['y2']);
  1535. $path_cmd = $svg_class->svgPolyline($arguments,false); // mPDF 4.4.003
  1536. $critere_style = $attribs;
  1537. unset($critere_style['x1'],$critere_style['y1'],$critere_style['x2'],$critere_style['y2']);
  1538. $path_style = $svg_class->svgDefineStyle($critere_style);
  1539. break;
  1540. case 'polyline':
  1541. $path = $attribs['points'];
  1542. preg_match_all('/[0-9\-\.]*/',$path, $tmp, PREG_SET_ORDER);
  1543. $arguments = array();
  1544. for ($i=0;$i<count($tmp);$i++){
  1545. if ($tmp[$i][0] !=''){
  1546. array_push($arguments, $tmp[$i][0]);
  1547. }
  1548. }
  1549. $path_cmd = $svg_class->svgPolyline($arguments);
  1550. $critere_style = $attribs;
  1551. unset($critere_style['points']);
  1552. $path_style = $svg_class->svgDefineStyle($critere_style);
  1553. break;
  1554. case 'polygon':
  1555. $path = $attribs['points'];
  1556. preg_match_all('/([\-]*[0-9\.]+)/',$path, $tmp);
  1557. $arguments = array();
  1558. for ($i=0;$i<count($tmp[0]);$i++){
  1559. if ($tmp[0][$i] !=''){
  1560. array_push($arguments, $tmp[0][$i]);
  1561. }
  1562. }
  1563. $path_cmd = $svg_class->svgPolygon($arguments);
  1564. // definition du style de la forme:
  1565. $critere_style = $attribs;
  1566. unset($critere_style['points']);
  1567. $path_style = $svg_class->svgDefineStyle($critere_style);
  1568. break;
  1569. case 'lineargradient':
  1570. $tmp_gradient = array(
  1571. 'type' => 'linear',
  1572. 'info' => array(
  1573. 'x1' => $attribs['x1'],
  1574. 'y1' => $attribs['y1'],
  1575. 'x2' => $attribs['x2'],
  1576. 'y2' => $attribs['y2']
  1577. ),
  1578. 'transform' => $attribs['gradientTransform'],
  1579. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  1580. 'color' => array()
  1581. );
  1582. $last_gradid = $attribs['id'];
  1583. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  1584. break;
  1585. case 'radialgradient':
  1586. $tmp_gradient = array(
  1587. 'type' => 'radial',
  1588. 'info' => array(
  1589. 'x0' => $attribs['cx'],
  1590. 'y0' => $attribs['cy'],
  1591. 'x1' => $attribs['fx'],
  1592. 'y1' => $attribs['fy'],
  1593. 'r' => $attribs['r']
  1594. ),
  1595. 'transform' => $attribs['gradientTransform'],
  1596. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  1597. 'color' => array()
  1598. );
  1599. $last_gradid = $attribs['id'];
  1600. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  1601. break;
  1602. case 'stop':
  1603. if (!$last_gradid) break;
  1604. // mPDF 4.4.003
  1605. if (isset($attribs['style']) AND preg_match('/stop-color:\s*([0-9a-f#]*)/i',$attribs['style'],$m)) {
  1606. $color = $m[1];
  1607. } else if (isset($attribs['stop-color'])) {
  1608. $color = $attribs['stop-color'];
  1609. }
  1610. $col = $svg_class->mpdf_ref->ConvertColor($color);
  1611. $color_r = $col['R'];
  1612. $color_g = $col['G'];
  1613. $color_b = $col['B'];
  1614. // mPDF 4.4.003
  1615. $color_final = sprintf('%.3f %.3f %.3f',$color_r/255,$color_g/255,$color_b/255);
  1616. // $color_final = $path_style .= sprintf('%.3f %.3f %.3f',$color_r/255,$color_g/255,$color_b/255);
  1617. // mPDF 4.4.003
  1618. if (isset($attribs['style']) AND preg_match('/stop-opacity:\s*([0-9.]*)/i',$attribs['style'],$m)) {
  1619. $stop_opacity = $m[1];
  1620. } else if (isset($attribs['stop-opacity'])) {
  1621. $stop_opacity = $attribs['stop-opacity'];
  1622. }
  1623. $tmp_color = array(
  1624. 'color' => $color_final,
  1625. 'offset' => $attribs['offset'],
  1626. 'opacity' => $stop_opacity
  1627. );
  1628. array_push($svg_class->svg_gradient[$last_gradid]['color'],$tmp_color);
  1629. break;
  1630. case 'g':
  1631. $array_style = $svg_class->svgDefineStyle($attribs);
  1632. if ($array_style['transformations']) {
  1633. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  1634. }
  1635. array_push($svg_class->svg_style,$array_style);
  1636. $svg_class->svgDefineTxtStyle($attribs); // mPDF 4.4.003
  1637. break;
  1638. case 'text':
  1639. // mPDF 4.4.003
  1640. $array_style = $svg_class->svgDefineStyle($attribs);
  1641. if ($array_style['transformations']) {
  1642. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  1643. }
  1644. array_push($svg_class->svg_style,$array_style);
  1645. $svg_class->txt_data = array();
  1646. $svg_class->txt_data[0] = $attribs['x'];
  1647. $svg_class->txt_data[1] = $attribs['y'];
  1648. $critere_style = $attribs;
  1649. unset($critere_style['x'], $critere_style['y']);
  1650. $svg_class->svgDefineTxtStyle($critere_style);
  1651. break;
  1652. }
  1653. //
  1654. //insertion des path et du style dans le flux de donné general.
  1655. if (isset($path_cmd) && $path_cmd) { // mPDF 4.4.003
  1656. $get_style = $svg_class->svgStyle($path_style, $attribs, strtolower($name));
  1657. if ($path_style['transformations']) { // transformation on an element
  1658. $svg_class->svgWriteString(" q ".$path_style['transformations']. "$path_cmd $get_style" . " Q\n");
  1659. }
  1660. else {
  1661. $svg_class->svgWriteString("$path_cmd $get_style\n");
  1662. }
  1663. }
  1664. }
  1665. function characterData($parser, $data)
  1666. {
  1667. global $svg_class;
  1668. if(isset($svg_class->txt_data[2])) {
  1669. $svg_class->txt_data[2] .= $data;
  1670. }
  1671. else {
  1672. $svg_class->txt_data[2] = $data;
  1673. }
  1674. }
  1675. function xml_svg2pdf_end($parser, $name){
  1676. global $svg_class;
  1677. switch($name){
  1678. case "g":
  1679. $tmp = count($svg_class->svg_style)-1;
  1680. $current_style = $svg_class->svg_style[$tmp];
  1681. if ($current_style['transformations']) {
  1682. $svg_class->svgWriteString(" Q ");
  1683. }
  1684. array_pop($svg_class->svg_style);
  1685. array_pop($svg_class->txt_style); // mPDF 4.4.003
  1686. break;
  1687. case 'radialgradient':
  1688. case 'lineargradient':
  1689. $last_gradid = '';
  1690. break;
  1691. case "text":
  1692. $path_cmd = $svg_class->svgText();
  1693. // echo 'path >> '.$path_cmd."<br><br>";
  1694. // echo "style >> ".$get_style[1]."<br><br>";
  1695. $svg_class->svgWriteString($path_cmd);
  1696. // mPDF 4.4.003
  1697. $tmp = count($svg_class->svg_style)-1;
  1698. $current_style = $svg_class->svg_style[$tmp];
  1699. if ($current_style['transformations']) {
  1700. $svg_class->svgWriteString(" Q ");
  1701. }
  1702. array_pop($svg_class->svg_style);
  1703. break;
  1704. }
  1705. }
  1706. }
  1707. $svg2pdf_xml='';
  1708. global $svg_class;
  1709. $svg_class = $this;
  1710. $svg2pdf_xml_parser = xml_parser_create("utf-8");
  1711. xml_parser_set_option($svg2pdf_xml_parser, XML_OPTION_CASE_FOLDING, false);
  1712. xml_set_element_handler($svg2pdf_xml_parser, "xml_svg2pdf_start", "xml_svg2pdf_end");
  1713. xml_set_character_data_handler($svg2pdf_xml_parser, "characterData");
  1714. xml_parse($svg2pdf_xml_parser, $data);
  1715. // mPDF 4.4.003
  1716. if ($this->svg_error) { return false; }
  1717. else {
  1718. 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);
  1719. }
  1720. }
  1721. }
  1722. ?>