svg.php 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  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)*$this->mpdf_ref->k/2; // mPDF 4.4.003
  1646. }
  1647. else if ($current_style['text-anchor']=='end') {
  1648. $tw = $this->mpdf_ref->GetStringWidth($txt)*$this->mpdf_ref->k; // mPDF 4.4.003
  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. }
  1666. else
  1667. {
  1668. return ' ';
  1669. }
  1670. // $path_cmd .= 'h '; // mPDF 5.0
  1671. return $path_cmd;
  1672. }
  1673. function svgDefineTxtStyle($critere_style)
  1674. {
  1675. // get copy of current/default txt style, and modify it with supplied attributes
  1676. $tmp = count($this->txt_style)-1;
  1677. $current_style = $this->txt_style[$tmp];
  1678. if (isset($critere_style['style'])){
  1679. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1680. $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);
  1681. }
  1682. else { $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i","$2",$critere_style['style']);
  1683. if ($tmp != $critere_style['style']){ $current_style['fill'] = $tmp; }
  1684. }
  1685. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1686. if ($tmp != $critere_style['style']){ $current_style['fill-opacity'] = $tmp;}
  1687. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1688. if ($tmp != $critere_style['style']){ $current_style['fill-rule'] = $tmp;}
  1689. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/',$critere_style['style'], $m)) {
  1690. $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);
  1691. }
  1692. else { $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1693. if ($tmp != $critere_style['style']){ $current_style['stroke'] = $tmp; }
  1694. }
  1695. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1696. if ($tmp != $critere_style['style']){ $current_style['stroke-linecap'] = $tmp;}
  1697. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1698. if ($tmp != $critere_style['style']){ $current_style['stroke-linejoin'] = $tmp;}
  1699. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i","$2",$critere_style['style']);
  1700. if ($tmp != $critere_style['style']){ $current_style['stroke-miterlimit'] = $tmp;}
  1701. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1702. if ($tmp != $critere_style['style']){ $current_style['stroke-opacity'] = $tmp; }
  1703. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1704. if ($tmp != $critere_style['style']){ $current_style['stroke-width'] = $tmp;}
  1705. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i","$2",$critere_style['style']);
  1706. if ($tmp != $critere_style['style']){ $current_style['stroke-dasharray'] = $tmp;}
  1707. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1708. if ($tmp != $critere_style['style']){ $current_style['stroke-dashoffset'] = $tmp;}
  1709. // mPDF 5.0.039
  1710. $tmp = preg_replace("/(.*)font-family:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1711. if ($tmp != $critere_style['style']){ $critere_style['font-family'] = $tmp;}
  1712. $tmp = preg_replace("/(.*)font-size:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1713. if ($tmp != $critere_style['style']){ $critere_style['font-size'] = $tmp;}
  1714. $tmp = preg_replace("/(.*)font-weight:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1715. if ($tmp != $critere_style['style']){ $critere_style['font-weight'] = $tmp;}
  1716. $tmp = preg_replace("/(.*)font-style:\s*([a-z0-9.]*|none)(.*)/i","$2",$critere_style['style']);
  1717. if ($tmp != $critere_style['style']){ $critere_style['font-style'] = $tmp;}
  1718. }
  1719. if (isset($critere_style['font'])){
  1720. // [ [ <'font-style'> || <'font-variant'> || <'font-weight'> ]?<'font-size'> [ / <'line-height'> ]? <'font-family'> ]
  1721. $tmp = preg_replace("/(.*)(italic|oblique)(.*)/i","$2",$critere_style['font']);
  1722. if ($tmp != $critere_style['font']){
  1723. if($tmp == 'oblique'){
  1724. $tmp = 'italic';
  1725. }
  1726. $current_style['font-style'] = $tmp;
  1727. }
  1728. $tmp = preg_replace("/(.*)(bold|bolder)(.*)/i","$2",$critere_style['font']);
  1729. if ($tmp != $critere_style['font']){
  1730. if($tmp == 'bolder'){
  1731. $tmp = 'bold';
  1732. }
  1733. $current_style['font-weight'] = $tmp;
  1734. }
  1735. // select digits not followed by percent sign nor preceeded by forward slash
  1736. $tmp = preg_replace("/(.*)\b(\d+)[\b|\/](.*)/i","$2",$critere_style['font']);
  1737. if ($tmp != $critere_style['font']){
  1738. // mPDF 4.4.003
  1739. $current_style['font-size'] = $this->ConvertSVGSizePts($tmp);
  1740. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1741. }
  1742. }
  1743. if(isset($critere_style['fill'])){
  1744. $current_style['fill'] = $critere_style['fill'];
  1745. }
  1746. // mPDF 4.4.003
  1747. if(isset($critere_style['stroke'])){
  1748. $current_style['stroke'] = $critere_style['stroke'];
  1749. }
  1750. if(isset($critere_style['stroke-width'])){
  1751. $current_style['stroke-width'] = $critere_style['stroke-width'];
  1752. }
  1753. if(isset($critere_style['font-style'])){
  1754. if(strtolower($critere_style['font-style']) == 'oblique')
  1755. {
  1756. $critere_style['font-style'] = 'italic';
  1757. }
  1758. $current_style['font-style'] = $critere_style['font-style'];
  1759. }
  1760. if(isset($critere_style['font-weight'])){
  1761. if(strtolower($critere_style['font-weight']) == 'bolder')
  1762. {
  1763. $critere_style['font-weight'] = 'bold';
  1764. }
  1765. $current_style['font-weight'] = $critere_style['font-weight'];
  1766. }
  1767. if(isset($critere_style['font-size'])){
  1768. // mPDF 4.4.003
  1769. $current_style['font-size'] = $this->ConvertSVGSizePts($critere_style['font-size']);
  1770. $this->mpdf_ref->SetFont('','',$current_style['font-size'],false);
  1771. }
  1772. if(isset($critere_style['font-family'])){
  1773. // mPDF 4.4.003
  1774. $v = $critere_style['font-family'];
  1775. $aux_fontlist = explode(",",$v);
  1776. $found = 0;
  1777. foreach($aux_fontlist AS $f) {
  1778. $fonttype = trim($f);
  1779. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  1780. $fonttype = preg_replace('/ /','',$fonttype);
  1781. $v = strtolower(trim($fonttype));
  1782. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) { $v = $this->mpdf_ref->fonttrans[$v]; }
  1783. if ((!$this->mpdf_ref->usingCoreFont && in_array($v,$this->mpdf_ref->available_unifonts)) ||
  1784. ($this->mpdf_ref->usingCoreFont && in_array($v,array('courier','times','helvetica','arial'))) ||
  1785. in_array($v, array('sjis','uhc','big5','gb'))) {
  1786. $current_style['font-family'] = $v;
  1787. $found = 1;
  1788. break;
  1789. }
  1790. }
  1791. if (!$found) {
  1792. foreach($aux_fontlist AS $f) {
  1793. $fonttype = trim($f);
  1794. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  1795. $fonttype = preg_replace('/ /','',$fonttype);
  1796. $v = strtolower(trim($fonttype));
  1797. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) { $v = $this->mpdf_ref->fonttrans[$v]; }
  1798. 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) ) {
  1799. $current_style['font-family'] = $v;
  1800. break;
  1801. }
  1802. }
  1803. }
  1804. }
  1805. if(isset($critere_style['text-anchor'])){
  1806. $current_style['text-anchor'] = $critere_style['text-anchor'];
  1807. }
  1808. // add current style to text style array (will remove it later after writing text to svg_string)
  1809. array_push($this->txt_style,$current_style);
  1810. }
  1811. //
  1812. // fonction ajoutant un gradient
  1813. function svgAddGradient($id,$array_gradient){
  1814. $this->svg_gradient[$id] = $array_gradient;
  1815. }
  1816. //
  1817. // Ajoute une couleur dans le gradient correspondant
  1818. //
  1819. // function ecrivant dans le svgstring
  1820. function svgWriteString($content){
  1821. $this->svg_string .= $content;
  1822. }
  1823. // analise le svg et renvoie aux fonctions precedente our le traitement
  1824. function ImageSVG($data){
  1825. $this->svg_info = array();
  1826. // mPDF 4.4.006
  1827. if (preg_match('/<!ENTITY/si',$data)) {
  1828. // Get User-defined entities
  1829. preg_match_all('/<!ENTITY\s+([a-z]+)\s+\"(.*?)\">/si',$data, $ent);
  1830. // Replace entities
  1831. for ($i=0; $i<count($ent[0]); $i++) {
  1832. $data = preg_replace('/&'.preg_quote($ent[1][$i],'/').';/is', $ent[2][$i], $data);
  1833. }
  1834. }
  1835. // mPDF 4.4.003
  1836. if (preg_match('/xlink:href=/si',$data)) {
  1837. // Get links
  1838. preg_match_all('/(<(linearGradient|radialgradient)[^>]*)xlink:href=["\']#(.*?)["\'](.*?)\/>/si',$data, $links);
  1839. if (count($links[0])) { $links[5] = array(); } // mPDF 4.5.010
  1840. // Delete links from data - keeping in $links
  1841. for ($i=0; $i<count($links[0]); $i++) {
  1842. $links[5][$i] = 'tmpLink'.RAND(100000,9999999); // mPDF 4.5.010
  1843. $data = preg_replace('/'.preg_quote($links[0][$i],'/').'/is', '<MYLINKS'.$links[5][$i].'>' , $data); // mPDF 4.5.010
  1844. }
  1845. // Get targets
  1846. preg_match_all('/<(linearGradient|radialgradient)([^>]*)id=["\'](.*?)["\'](.*?)>(.*?)<\/(linearGradient|radialgradient)>/si',$data, $m);
  1847. $targets = array();
  1848. $stops = array();
  1849. // keeping in $targets
  1850. for ($i=0; $i<count($m[0]); $i++) {
  1851. $stops[$m[3][$i]] = $m[5][$i];
  1852. }
  1853. // Add back links this time as targets (gradients)
  1854. for ($i=0; $i<count($links[0]); $i++) {
  1855. $def = $links[1][$i] .' '.$links[4][$i].'>'. $stops[$links[3][$i]].'</'.$links[2][$i] .'>' ; // mPDF 4.5.010
  1856. $data = preg_replace('/<MYLINKS'.$links[5][$i].'>/is', $def , $data); // mPDF 4.5.010
  1857. }
  1858. }
  1859. // mPDF 4.4.003 - Removes <pattern>
  1860. $data = preg_replace('/<pattern.*?<\/pattern>/is', '', $data);
  1861. // mPDF 4.4.003 - Removes <marker>
  1862. $data = preg_replace('/<marker.*?<\/marker>/is', '', $data);
  1863. $this->svg_info['data'] = $data;
  1864. $this->svg_string = '';
  1865. //
  1866. // chargement unique des fonctions
  1867. if(!function_exists("xml_svg2pdf_start")){ // mPDF 5.3.76
  1868. function xml_svg2pdf_start($parser, $name, $attribs){
  1869. //
  1870. // definition
  1871. global $svg_class, $last_gradid;
  1872. // mPDF 4.4.003
  1873. $svg_class->xbase = 0;
  1874. $svg_class->ybase = 0;
  1875. switch (strtolower($name)){
  1876. // mPDF 5.0.039 - Don't output stuff inside <defs>
  1877. case 'defs':
  1878. $svg_class->inDefs = true;
  1879. return;
  1880. case 'svg':
  1881. $svg_class->svgOffset($attribs);
  1882. break;
  1883. case 'path':
  1884. $path = $attribs['d'];
  1885. // mPDF 4.4.003
  1886. preg_match_all('/([MZLHVCSQTAmzlhvcsqta])([e ,\-.\d]+)*/', $path, $commands, PREG_SET_ORDER);
  1887. $path_cmd = '';
  1888. $svg_class->subPathInit = true; // mPDF 4.4.003
  1889. // mPDF 5.0.039
  1890. $svg_class->pathBBox = array(999999,999999,-999999,-999999);
  1891. foreach($commands as $c){
  1892. if(count($c)==3 || $c[2]==''){
  1893. list($tmp, $command, $arguments) = $c;
  1894. }
  1895. else{
  1896. list($tmp, $command) = $c;
  1897. $arguments = '';
  1898. }
  1899. $path_cmd .= $svg_class->svgPath($command, $arguments);
  1900. }
  1901. // mPDF 5.0.039
  1902. if ($svg_class->pathBBox[2]==-1999998) { $svg_class->pathBBox[2] = 100; }
  1903. if ($svg_class->pathBBox[3]==-1999998) { $svg_class->pathBBox[3] = 100; }
  1904. if ($svg_class->pathBBox[0]==999999) { $svg_class->pathBBox[0] = 0; }
  1905. if ($svg_class->pathBBox[1]==999999) { $svg_class->pathBBox[1] = 0; }
  1906. $critere_style = $attribs;
  1907. unset($critere_style['d']);
  1908. $path_style = $svg_class->svgDefineStyle($critere_style);
  1909. break;
  1910. case 'rect':
  1911. if (!isset($attribs['x'])) {$attribs['x'] = 0;}
  1912. if (!isset($attribs['y'])) {$attribs['y'] = 0;}
  1913. if (!isset($attribs['rx'])) {$attribs['rx'] = 0;}
  1914. if (!isset($attribs['ry'])) {$attribs['ry'] = 0;}
  1915. $arguments = array(
  1916. 'x' => $attribs['x'],
  1917. 'y' => $attribs['y'],
  1918. 'w' => $attribs['width'],
  1919. 'h' => $attribs['height'],
  1920. 'rx' => $attribs['rx'],
  1921. 'ry' => $attribs['ry']
  1922. );
  1923. $path_cmd = $svg_class->svgRect($arguments);
  1924. $critere_style = $attribs;
  1925. unset($critere_style['x'],$critere_style['y'],$critere_style['rx'],$critere_style['ry'],$critere_style['height'],$critere_style['width']);
  1926. $path_style = $svg_class->svgDefineStyle($critere_style);
  1927. break;
  1928. case 'circle':
  1929. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1930. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1931. $arguments = array(
  1932. 'cx' => $attribs['cx'],
  1933. 'cy' => $attribs['cy'],
  1934. 'rx' => $attribs['r'],
  1935. 'ry' => $attribs['r']
  1936. );
  1937. $path_cmd = $svg_class->svgEllipse($arguments);
  1938. $critere_style = $attribs;
  1939. unset($critere_style['cx'],$critere_style['cy'],$critere_style['r']);
  1940. $path_style = $svg_class->svgDefineStyle($critere_style);
  1941. break;
  1942. case 'ellipse':
  1943. if (!isset($attribs['cx'])) {$attribs['cx'] = 0;}
  1944. if (!isset($attribs['cy'])) {$attribs['cy'] = 0;}
  1945. $arguments = array(
  1946. 'cx' => $attribs['cx'],
  1947. 'cy' => $attribs['cy'],
  1948. 'rx' => $attribs['rx'],
  1949. 'ry' => $attribs['ry']
  1950. );
  1951. $path_cmd = $svg_class->svgEllipse($arguments);
  1952. $critere_style = $attribs;
  1953. unset($critere_style['cx'],$critere_style['cy'],$critere_style['rx'],$critere_style['ry']);
  1954. $path_style = $svg_class->svgDefineStyle($critere_style);
  1955. break;
  1956. case 'line':
  1957. $arguments = array($attribs['x1'],$attribs['y1'],$attribs['x2'],$attribs['y2']);
  1958. $path_cmd = $svg_class->svgPolyline($arguments,false); // mPDF 4.4.003
  1959. $critere_style = $attribs;
  1960. unset($critere_style['x1'],$critere_style['y1'],$critere_style['x2'],$critere_style['y2']);
  1961. $path_style = $svg_class->svgDefineStyle($critere_style);
  1962. break;
  1963. case 'polyline':
  1964. $path = $attribs['points'];
  1965. preg_match_all('/[0-9\-\.]*/',$path, $tmp, PREG_SET_ORDER);
  1966. $arguments = array();
  1967. for ($i=0;$i<count($tmp);$i++){
  1968. if ($tmp[$i][0] !=''){
  1969. array_push($arguments, $tmp[$i][0]);
  1970. }
  1971. }
  1972. $path_cmd = $svg_class->svgPolyline($arguments);
  1973. $critere_style = $attribs;
  1974. unset($critere_style['points']);
  1975. $path_style = $svg_class->svgDefineStyle($critere_style);
  1976. break;
  1977. case 'polygon':
  1978. $path = $attribs['points'];
  1979. preg_match_all('/([\-]*[0-9\.]+)/',$path, $tmp);
  1980. $arguments = array();
  1981. for ($i=0;$i<count($tmp[0]);$i++){
  1982. if ($tmp[0][$i] !=''){
  1983. array_push($arguments, $tmp[0][$i]);
  1984. }
  1985. }
  1986. $path_cmd = $svg_class->svgPolygon($arguments);
  1987. // definition du style de la forme:
  1988. $critere_style = $attribs;
  1989. unset($critere_style['points']);
  1990. $path_style = $svg_class->svgDefineStyle($critere_style);
  1991. break;
  1992. case 'lineargradient':
  1993. $tmp_gradient = array(
  1994. 'type' => 'linear',
  1995. 'info' => array(
  1996. 'x1' => $attribs['x1'],
  1997. 'y1' => $attribs['y1'],
  1998. 'x2' => $attribs['x2'],
  1999. 'y2' => $attribs['y2']
  2000. ),
  2001. 'transform' => $attribs['gradientTransform'],
  2002. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  2003. 'spread' => $attribs['spreadMethod'], /* mPDF 5.0.040 */
  2004. 'color' => array()
  2005. );
  2006. $last_gradid = $attribs['id'];
  2007. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  2008. break;
  2009. case 'radialgradient':
  2010. $tmp_gradient = array(
  2011. 'type' => 'radial',
  2012. 'info' => array(
  2013. 'x0' => $attribs['cx'],
  2014. 'y0' => $attribs['cy'],
  2015. 'x1' => $attribs['fx'],
  2016. 'y1' => $attribs['fy'],
  2017. 'r' => $attribs['r']
  2018. ),
  2019. 'transform' => $attribs['gradientTransform'],
  2020. 'units' => $attribs['gradientUnits'], /* mPDF 4.4.003 */
  2021. 'spread' => $attribs['spreadMethod'], /* mPDF 5.0.040 */
  2022. 'color' => array()
  2023. );
  2024. $last_gradid = $attribs['id'];
  2025. $svg_class->svgAddGradient($attribs['id'],$tmp_gradient);
  2026. break;
  2027. case 'stop':
  2028. if (!$last_gradid) break;
  2029. // mPDF 4.4.003 // mPDF 5.0.040
  2030. if (isset($attribs['style']) AND preg_match('/stop-color:\s*([^;]*)/i',$attribs['style'],$m)) {
  2031. $color = trim($m[1]);
  2032. } else if (isset($attribs['stop-color'])) {
  2033. $color = $attribs['stop-color'];
  2034. }
  2035. $col = $svg_class->mpdf_ref->ConvertColor($color);
  2036. // mPDF 5.0.051
  2037. // mPDF 5.3.74
  2038. if ($col{0}==3 || $col{0}==5) { // RGB
  2039. $color_final = sprintf('%.3F %.3F %.3F',ord($col{1})/255,ord($col{2})/255,ord($col{3})/255);
  2040. $svg_class->svg_gradient[$last_gradid]['colorspace']='RGB';
  2041. }
  2042. else if ($col{0}==4 || $col{0}==6) { // CMYK
  2043. $color_final = sprintf('%.3F %.3F %.3F %.3F',ord($col{1})/100,ord($col{2})/100,ord($col{3})/100,ord($col{4})/100);
  2044. $svg_class->svg_gradient[$last_gradid]['colorspace']='CMYK';
  2045. }
  2046. else if ($col{0}==1) { // Grayscale
  2047. $color_final = sprintf('%.3F',ord($col{1})/255);
  2048. $svg_class->svg_gradient[$last_gradid]['colorspace']='Gray';
  2049. }
  2050. // mPDF 5.0.020
  2051. $stop_opacity = 1;
  2052. // mPDF 4.4.003
  2053. if (isset($attribs['style']) AND preg_match('/stop-opacity:\s*([0-9.]*)/i',$attribs['style'],$m)) {
  2054. $stop_opacity = $m[1];
  2055. } else if (isset($attribs['stop-opacity'])) {
  2056. $stop_opacity = $attribs['stop-opacity'];
  2057. }
  2058. // mPDF 5.0.051
  2059. // mPDF 5.3.74
  2060. else if ($col{0}==5) { // RGBa
  2061. $stop_opacity = ord($col{4}/100);
  2062. }
  2063. else if ($col{0}==6) { // CMYKa
  2064. $stop_opacity = ord($col{5}/100);
  2065. }
  2066. $tmp_color = array(
  2067. 'color' => $color_final,
  2068. 'offset' => $attribs['offset'],
  2069. 'opacity' => $stop_opacity
  2070. );
  2071. array_push($svg_class->svg_gradient[$last_gradid]['color'],$tmp_color);
  2072. break;
  2073. case 'a':
  2074. if (isset($attribs['xlink:href'])) {
  2075. unset($attribs['xlink:href']); // this should be a hyperlink
  2076. // not handled like a xlink:href in other elements
  2077. } // then continue like a <g>
  2078. case 'g':
  2079. $array_style = $svg_class->svgDefineStyle($attribs);
  2080. if ($array_style['transformations']) {
  2081. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  2082. }
  2083. array_push($svg_class->svg_style,$array_style);
  2084. $svg_class->svgDefineTxtStyle($attribs); // mPDF 4.4.003
  2085. break;
  2086. case 'text':
  2087. // mPDF 4.4.003
  2088. $array_style = $svg_class->svgDefineStyle($attribs);
  2089. if ($array_style['transformations']) {
  2090. $svg_class->svgWriteString(' q '.$array_style['transformations']);
  2091. }
  2092. array_push($svg_class->svg_style,$array_style);
  2093. $svg_class->txt_data = array();
  2094. $svg_class->txt_data[0] = $attribs['x'];
  2095. $svg_class->txt_data[1] = $attribs['y'];
  2096. $critere_style = $attribs;
  2097. unset($critere_style['x'], $critere_style['y']);
  2098. $svg_class->svgDefineTxtStyle($critere_style);
  2099. break;
  2100. }
  2101. //
  2102. //insertion des path et du style dans le flux de donné general.
  2103. if (isset($path_cmd) && $path_cmd) { // mPDF 4.4.003
  2104. // mPDF 5.0
  2105. list($prestyle,$poststyle) = $svg_class->svgStyle($path_style, $attribs, strtolower($name));
  2106. if ($path_style['transformations']) { // transformation on an element
  2107. $svg_class->svgWriteString(" q ".$path_style['transformations']. " $prestyle $path_cmd $poststyle" . " Q\n");
  2108. }
  2109. else {
  2110. $svg_class->svgWriteString("$prestyle $path_cmd $poststyle\n");
  2111. }
  2112. }
  2113. }
  2114. function characterData($parser, $data)
  2115. {
  2116. global $svg_class;
  2117. if(isset($svg_class->txt_data[2])) {
  2118. $svg_class->txt_data[2] .= $data;
  2119. }
  2120. else {
  2121. $svg_class->txt_data[2] = $data;
  2122. }
  2123. }
  2124. function xml_svg2pdf_end($parser, $name){
  2125. global $svg_class;
  2126. switch($name){
  2127. case "g":
  2128. case "a":
  2129. $tmp = count($svg_class->svg_style)-1;
  2130. $current_style = $svg_class->svg_style[$tmp];
  2131. if ($current_style['transformations']) {
  2132. $svg_class->svgWriteString(" Q\n");
  2133. }
  2134. array_pop($svg_class->svg_style);
  2135. array_pop($svg_class->txt_style); // mPDF 4.4.003
  2136. break;
  2137. case 'radialgradient':
  2138. case 'lineargradient':
  2139. $last_gradid = '';
  2140. break;
  2141. case "text":
  2142. $path_cmd = $svg_class->svgText();
  2143. // echo 'path >> '.$path_cmd."<br><br>";
  2144. // echo "style >> ".$get_style[1]."<br><br>";
  2145. $svg_class->svgWriteString($path_cmd);
  2146. // mPDF 4.4.003
  2147. $tmp = count($svg_class->svg_style)-1;
  2148. $current_style = $svg_class->svg_style[$tmp];
  2149. if ($current_style['transformations']) {
  2150. $svg_class->svgWriteString(" Q\n");
  2151. }
  2152. array_pop($svg_class->svg_style);
  2153. break;
  2154. }
  2155. // mPDF 5.0.039 - Don't output stuff inside <defs>
  2156. if ($name == 'defs') {
  2157. $svg_class->inDefs = false;
  2158. }
  2159. }
  2160. }
  2161. $svg2pdf_xml='';
  2162. global $svg_class;
  2163. $svg_class = $this;
  2164. // mPDF 5.0.039 - Don't output stuff inside <defs>
  2165. $svg_class->inDefs = false;
  2166. $svg2pdf_xml_parser = xml_parser_create("utf-8");
  2167. xml_parser_set_option($svg2pdf_xml_parser, XML_OPTION_CASE_FOLDING, false);
  2168. xml_set_element_handler($svg2pdf_xml_parser, "xml_svg2pdf_start", "xml_svg2pdf_end");
  2169. xml_set_character_data_handler($svg2pdf_xml_parser, "characterData");
  2170. xml_parse($svg2pdf_xml_parser, $data);
  2171. // mPDF 4.4.003
  2172. if ($this->svg_error) { return false; }
  2173. else {
  2174. 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);
  2175. }
  2176. }
  2177. }
  2178. // END OF CLASS
  2179. // mPDF 5.0.040
  2180. function calc_bezier_bbox($start, $c) {
  2181. $P0 = array($start[0],$start[1]);
  2182. $P1 = array($c[0],$c[1]);
  2183. $P2 = array($c[2],$c[3]);
  2184. $P3 = array($c[4],$c[5]);
  2185. $bounds = array();
  2186. $bounds[0][] = $P0[0];
  2187. $bounds[1][] = $P0[1];
  2188. $bounds[0][] = $P3[0];
  2189. $bounds[1][] = $P3[1];
  2190. for ($i=0;$i<=1;$i++) {
  2191. $b = 6 * $P0[$i] - 12 * $P1[$i] + 6 * $P2[$i];
  2192. $a = -3 * $P0[$i] + 9 * $P1[$i] - 9 * $P2[$i] + 3 * $P3[$i];
  2193. $c = 3 * $P1[$i] - 3 * $P0[$i];
  2194. if ($a == 0) {
  2195. if ($b == 0) { continue; }
  2196. $t = -$c / $b;
  2197. if ($t>0 && $t<1) {
  2198. $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]);
  2199. }
  2200. continue;
  2201. }
  2202. $b2ac = pow($b, 2) - 4 * $c * $a;
  2203. if ($b2ac < 0) { continue; }
  2204. $t1 = (-$b + sqrt($b2ac))/(2 * $a);
  2205. if ($t1>0 && $t1<1) {
  2206. $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]);
  2207. }
  2208. $t2 = (-$b - sqrt($b2ac))/(2 * $a);
  2209. if ($t2>0 && $t2<1) {
  2210. $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]);
  2211. }
  2212. }
  2213. $x = min($bounds[0]);
  2214. $x2 = max($bounds[0]);
  2215. $y = min($bounds[1]);
  2216. $y2 = max($bounds[1]);
  2217. return array($x, $y, $x2, $y2);
  2218. }
  2219. // mPDF 5.0.040
  2220. function _testIntersectCircle($cx, $cy, $cr) {
  2221. // Tests whether a circle fully encloses a rectangle 0,0,1,1
  2222. // to see if any further radial gradients need adding (SVG)
  2223. // If centre of circle is inside 0,0,1,1 square
  2224. if ($cx >= 0 && $cx <= 1 && $cy >= 0 && $cy <= 1) {
  2225. $maxd = 1.5;
  2226. }
  2227. // distance to four corners
  2228. else {
  2229. $d1 = sqrt(pow(($cy-0),2) + pow(($cx-0),2));
  2230. $d2 = sqrt(pow(($cy-1),2) + pow(($cx-0),2));
  2231. $d3 = sqrt(pow(($cy-0),2) + pow(($cx-1),2));
  2232. $d4 = sqrt(pow(($cy-1),2) + pow(($cx-1),2));
  2233. $maxd = max($d1,$d2,$d3,$d4);
  2234. }
  2235. if ($cr < $maxd) { return true; }
  2236. else { return false; }
  2237. }
  2238. // mPDF 5.0.040
  2239. function _testIntersect($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) {
  2240. // Tests whether line (x1, y1) and (x2, y2) [a gradient axis (perpendicular)]
  2241. // intersects with a specific line segment (x3, y3) and (x4, y4)
  2242. $a1 = $y2-$y1;
  2243. $b1 = $x1-$x2;
  2244. $c1 = $a1*$x1+$b1*$y1;
  2245. $a2 = $y4-$y3;
  2246. $b2 = $x3-$x4;
  2247. $c2 = $a2*$x3+$b2*$y3;
  2248. $det = $a1*$b2 - $a2*$b1;
  2249. if($det == 0){ //Lines are parallel
  2250. return false;
  2251. }
  2252. else{
  2253. $x = ($b2*$c1 - $b1*$c2)/$det;
  2254. $y = ($a1*$c2 - $a2*$c1)/$det;
  2255. if ($x >= $x3 && $x <= $x4 && $y >= $y3 && $y <= $y4) { return true; }
  2256. }
  2257. return false;
  2258. }
  2259. ?>