svg.php 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795
  1. <?php
  2. // svg class modified for mPDF version 6.0 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. // If you wish to use Automatic Font selection within SVG's. change this definition to true.
  10. // This selects different fonts for different scripts used in text.
  11. // This can be enabled/disabled independently of the use of Automatic Font selection within mPDF generally.
  12. // Choice of font is determined by the config_script2lang.php and config_lang2fonts.php files, the same as for mPDF generally.
  13. if (!defined("_SVG_AUTOFONT")) {
  14. define("_SVG_AUTOFONT", false);
  15. }
  16. // Enable a limited use of classes within SVG <text> elements by setting this to true.
  17. // This allows recognition of a "class" attribute on a <text> element.
  18. // The CSS style for that class should be outside the SVG, and cannot use any other selectors (i.e. only .class {} can be defined)
  19. // <style> definitions within the SVG code will be recognised if the SVG is included as an inline item within the HTML code passed to mPDF.
  20. // The style property should be pertinent to SVG e.g. use fill:red rather than color:red
  21. // Only the properties currently supported for SVG text can be specified:
  22. // fill, fill-opacity, stroke, stroke-opacity, stroke-linecap, stroke-linejoin, stroke-width, stroke-dasharray, stroke-dashoffset
  23. // font-family, font-size, font-weight, font-variant, font-style, opacity, text-anchor
  24. if (!defined("_SVG_CLASSES")) {
  25. define("_SVG_CLASSES", false);
  26. }
  27. // NB UNITS - Works in pixels as main units - converting to PDF units when outputing to PDF string
  28. // and on returning size
  29. class SVG
  30. {
  31. var $svg_font; // array - holds content of SVG fonts defined in image // mPDF 6
  32. var $svg_gradient; // array - contient les infos sur les gradient fill du svg classé par id du svg
  33. var $svg_shadinglist; // array - contient les ids des objet shading
  34. var $svg_info; // array contenant les infos du svg voulue par l'utilisateur
  35. var $svg_attribs; // array - holds all attributes of root <svg> tag
  36. var $svg_style; // array contenant les style de groupes du svg
  37. var $svg_string; // String contenant le tracage du svg en lui même.
  38. var $txt_data; // array - holds string info to write txt to image
  39. var $txt_style; // array - current text style
  40. var $mpdf_ref;
  41. var $xbase;
  42. var $ybase;
  43. var $svg_error;
  44. var $subPathInit;
  45. var $spxstart;
  46. var $spystart;
  47. var $kp; // convert pixels to PDF units
  48. var $pathBBox;
  49. var $script2lang;
  50. var $viet;
  51. var $pashto;
  52. var $urdu;
  53. var $persian;
  54. var $sindhi;
  55. var $textlength; // mPDF 5.7.4
  56. var $texttotallength; // mPDF 5.7.4
  57. var $textoutput; // mPDF 5.7.4
  58. var $textanchor; // mPDF 5.7.4
  59. var $textXorigin; // mPDF 5.7.4
  60. var $textYorigin; // mPDF 5.7.4
  61. var $textjuststarted; // mPDF 5.7.4
  62. var $intext; // mPDF 5.7.4
  63. public function __construct(mPDF $mpdf)
  64. {
  65. $this->svg_font = array(); // mPDF 6
  66. $this->svg_gradient = array();
  67. $this->svg_shadinglist = array();
  68. $this->txt_data = array();
  69. $this->svg_string = '';
  70. $this->svg_info = array();
  71. $this->svg_attribs = array();
  72. $this->xbase = 0;
  73. $this->ybase = 0;
  74. $this->svg_error = false;
  75. $this->subPathInit = false;
  76. $this->dashesUsed = false;
  77. $this->mpdf_ref = & $mpdf;
  78. $this->textlength = 0; // mPDF 5.7.4
  79. $this->texttotallength = 0; // mPDF 5.7.4
  80. $this->textoutput = ''; // mPDF 5.7.4
  81. $this->textanchor = 'start'; // mPDF 5.7.4
  82. $this->textXorigin = 0; // mPDF 5.7.4
  83. $this->textYorigin = 0; // mPDF 5.7.4
  84. $this->textjuststarted = false; // mPDF 5.7.4
  85. $this->intext = false; // mPDF 5.7.4
  86. $this->kp = 72 / $mpdf->img_dpi; // constant To convert pixels to pts/PDF units
  87. $this->kf = 1; // constant To convert font size if re-mapped
  88. $this->pathBBox = array();
  89. $this->svg_style = array(
  90. array(
  91. 'fill' => 'black',
  92. 'fill-opacity' => 1, // remplissage opaque par defaut
  93. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  94. 'stroke' => 'none', // pas de trait par defaut
  95. 'stroke-linecap' => 'butt', // style de langle par defaut
  96. 'stroke-linejoin' => 'miter',
  97. 'stroke-miterlimit' => 4, // limite de langle par defaut
  98. 'stroke-opacity' => 1, // trait opaque par defaut
  99. 'stroke-width' => 1,
  100. 'stroke-dasharray' => 0,
  101. 'stroke-dashoffset' => 0,
  102. 'color' => ''
  103. )
  104. );
  105. $this->txt_style = array(
  106. array(
  107. 'fill' => 'black', // pas de remplissage par defaut
  108. 'font-family' => $mpdf->default_font,
  109. 'font-size' => $mpdf->default_font_size, // ****** this is pts
  110. 'font-weight' => 'normal', // normal | bold
  111. 'font-style' => 'normal', // italic | normal
  112. 'text-anchor' => 'start', // alignment: start, middle, end
  113. 'fill-opacity' => 1, // remplissage opaque par defaut
  114. 'fill-rule' => 'nonzero', // mode de remplissage par defaut
  115. 'stroke' => 'none', // pas de trait par defaut
  116. 'stroke-opacity' => 1, // trait opaque par defaut
  117. 'stroke-width' => 1,
  118. 'color' => ''
  119. )
  120. );
  121. }
  122. // mPDF 5.7.4 Embedded image
  123. function svgImage($attribs)
  124. {
  125. // x and y are coordinates
  126. $x = (isset($attribs['x']) ? $attribs['x'] : 0);
  127. $y = (isset($attribs['y']) ? $attribs['y'] : 0);
  128. // preserveAspectRatio
  129. $par = (isset($attribs['preserveAspectRatio']) ? $attribs['preserveAspectRatio'] : 'xMidYMid meet');
  130. // width and height are <lengths> - Required attributes
  131. $wset = (isset($attribs['width']) ? $attribs['width'] : 0);
  132. $hset = (isset($attribs['height']) ? $attribs['height'] : 0);
  133. $w = $this->mpdf_ref->ConvertSize($wset, $this->svg_info['w'] * (25.4 / $this->mpdf_ref->dpi), $this->mpdf_ref->FontSize, false);
  134. $h = $this->mpdf_ref->ConvertSize($hset, $this->svg_info['h'] * (25.4 / $this->mpdf_ref->dpi), $this->mpdf_ref->FontSize, false);
  135. if ($w == 0 || $h == 0) {
  136. return;
  137. }
  138. // Convert to pixels = SVG units
  139. $w *= 1 / (25.4 / $this->mpdf_ref->dpi);
  140. $h *= 1 / (25.4 / $this->mpdf_ref->dpi);
  141. $srcpath = $attribs['xlink:href'];
  142. $orig_srcpath = '';
  143. if (trim($srcpath) != '' && substr($srcpath, 0, 4) == 'var:') {
  144. $orig_srcpath = $srcpath;
  145. $this->mpdf_ref->GetFullPath($srcpath);
  146. }
  147. // Image file (does not allow vector images i.e. WMF/SVG)
  148. // mPDF 6 Added $this->mpdf_ref->interpolateImages
  149. $info = $this->mpdf_ref->_getImage($srcpath, true, false, $orig_srcpath, $this->mpdf_ref->interpolateImages);
  150. if (!$info)
  151. return;
  152. // x,y,w,h define the reference rectangle
  153. $img_h = $h;
  154. $img_w = $w;
  155. $img_x = $x;
  156. $img_y = $y;
  157. $meetOrSlice = 'meet';
  158. // preserveAspectRatio
  159. $ar = preg_split('/\s+/', strtolower($par));
  160. if ($ar[0] != 'none') { // If "none" need to do nothing
  161. // Force uniform scaling
  162. if (isset($ar[1]) && $ar[1] == 'slice') {
  163. $meetOrSlice = 'slice';
  164. } else {
  165. $meetOrSlice = 'meet';
  166. }
  167. if ($info['h'] / $info['w'] > $h / $w) {
  168. if ($meetOrSlice == 'meet') { // the entire viewBox is visible within the viewport
  169. $img_w = $img_h * $info['w'] / $info['h'];
  170. } else { // the entire viewport is covered by the viewBox
  171. $img_h = $img_w * $info['h'] / $info['w'];
  172. }
  173. } else if ($info['h'] / $info['w'] < $h / $w) {
  174. if ($meetOrSlice == 'meet') { // the entire viewBox is visible within the viewport
  175. $img_h = $img_w * $info['h'] / $info['w'];
  176. } else { // the entire viewport is covered by the viewBox
  177. $img_w = $img_h * $info['w'] / $info['h'];
  178. }
  179. }
  180. if ($ar[0] == 'xminymin') {
  181. // do nothing to x
  182. // do nothing to y
  183. } else if ($ar[0] == 'xmidymin') {
  184. $img_x += $w / 2 - $img_w / 2; // xMid
  185. // do nothing to y
  186. } else if ($ar[0] == 'xmaxymin') {
  187. $img_x += $w - $img_w; // xMax
  188. // do nothing to y
  189. } else if ($ar[0] == 'xminymid') {
  190. // do nothing to x
  191. $img_y += $h / 2 - $img_h / 2; // yMid
  192. } else if ($ar[0] == 'xmaxymid') {
  193. $img_x += $w - $img_w; // xMax
  194. $img_y += $h / 2 - $img_h / 2; // yMid
  195. } else if ($ar[0] == 'xminymax') {
  196. // do nothing to x
  197. $img_y += $h - $img_h; // yMax
  198. } else if ($ar[0] == 'xmidymax') {
  199. $img_x += $w / 2 - $img_w / 2; // xMid
  200. $img_y += $h - $img_h; // yMax
  201. } else if ($ar[0] == 'xmaxymax') {
  202. $img_x += $w - $img_w; // xMax
  203. $img_y += $h - $img_h; // yMax
  204. } else { // xMidYMid (the default)
  205. $img_x += $w / 2 - $img_w / 2; // xMid
  206. $img_y += $h / 2 - $img_h / 2; // yMid
  207. }
  208. }
  209. // Output
  210. if ($meetOrSlice == 'slice') { // need to add a clipping path to reference rectangle
  211. $s = ' q 0 w '; // Line width=0
  212. $s .= sprintf('%.3F %.3F m ', ($x) * $this->kp, (-($y + $h)) * $this->kp); // start point TL before the arc
  213. $s .= sprintf('%.3F %.3F l ', ($x) * $this->kp, (-($y)) * $this->kp); // line to BL
  214. $s .= sprintf('%.3F %.3F l ', ($x + $w) * $this->kp, (-($y)) * $this->kp); // line to BR
  215. $s .= sprintf('%.3F %.3F l ', ($x + $w) * $this->kp, (-($y + $h)) * $this->kp); // line to TR
  216. $s .= sprintf('%.3F %.3F l ', ($x) * $this->kp, (-($y + $h)) * $this->kp); // line to TL
  217. $s .= ' W n '; // Ends path no-op & Sets the clipping path
  218. $this->svgWriteString($s);
  219. }
  220. $outstring = sprintf(" q %.3F 0 0 %.3F %.3F %.3F cm /I%d Do Q ", $img_w * $this->kp, $img_h * $this->kp, $img_x * $this->kp, -($img_y + $img_h) * $this->kp, $info['i']);
  221. $this->svgWriteString($outstring);
  222. if ($meetOrSlice == 'slice') { // need to end clipping path
  223. $this->svgWriteString(' Q ');
  224. }
  225. }
  226. function svgGradient($gradient_info, $attribs, $element)
  227. {
  228. $n = count($this->mpdf_ref->gradients) + 1;
  229. // Get bounding dimensions of element
  230. $w = 100;
  231. $h = 100;
  232. $x_offset = 0;
  233. $y_offset = 0;
  234. if ($element == 'rect') {
  235. $w = $attribs['width'];
  236. $h = $attribs['height'];
  237. $x_offset = $attribs['x'];
  238. $y_offset = $attribs['y'];
  239. } else if ($element == 'ellipse') {
  240. $w = $attribs['rx'] * 2;
  241. $h = $attribs['ry'] * 2;
  242. $x_offset = $attribs['cx'] - $attribs['rx'];
  243. $y_offset = $attribs['cy'] - $attribs['ry'];
  244. } else if ($element == 'circle') {
  245. $w = $attribs['r'] * 2;
  246. $h = $attribs['r'] * 2;
  247. $x_offset = $attribs['cx'] - $attribs['r'];
  248. $y_offset = $attribs['cy'] - $attribs['r'];
  249. } else if ($element == 'polygon') {
  250. $pts = preg_split('/[ ,]+/', trim($attribs['points']));
  251. $maxr = $maxb = 0;
  252. $minl = $mint = 999999;
  253. for ($i = 0; $i < count($pts); $i++) {
  254. if ($i % 2 == 0) { // x values
  255. $minl = min($minl, $pts[$i]);
  256. $maxr = max($maxr, $pts[$i]);
  257. } else { // y values
  258. $mint = min($mint, $pts[$i]);
  259. $maxb = max($maxb, $pts[$i]);
  260. }
  261. }
  262. $w = $maxr - $minl;
  263. $h = $maxb - $mint;
  264. $x_offset = $minl;
  265. $y_offset = $mint;
  266. } else if ($element == 'path') {
  267. if (is_array($this->pathBBox) && $this->pathBBox[2] > 0) {
  268. $w = $this->pathBBox[2];
  269. $h = $this->pathBBox[3];
  270. $x_offset = $this->pathBBox[0];
  271. $y_offset = $this->pathBBox[1];
  272. } else {
  273. preg_match_all('/([a-z]|[A-Z])([ ,\-.\d]+)*/', $attribs['d'], $commands, PREG_SET_ORDER);
  274. $maxr = $maxb = 0;
  275. $minl = $mint = 999999;
  276. foreach ($commands as $c) {
  277. if (count($c) == 3) {
  278. list($tmp, $cmd, $arg) = $c;
  279. if ($cmd == 'M' || $cmd == 'L' || $cmd == 'C' || $cmd == 'S' || $cmd == 'Q' || $cmd == 'T') {
  280. $pts = preg_split('/[ ,]+/', trim($arg));
  281. for ($i = 0; $i < count($pts); $i++) {
  282. if ($i % 2 == 0) { // x values
  283. $minl = min($minl, $pts[$i]);
  284. $maxr = max($maxr, $pts[$i]);
  285. } else { // y values
  286. $mint = min($mint, $pts[$i]);
  287. $maxb = max($maxb, $pts[$i]);
  288. }
  289. }
  290. }
  291. if ($cmd == 'H') { // sets new x
  292. $minl = min($minl, $arg);
  293. $maxr = max($maxr, $arg);
  294. }
  295. if ($cmd == 'V') { // sets new y
  296. $mint = min($mint, $arg);
  297. $maxb = max($maxb, $arg);
  298. }
  299. }
  300. }
  301. $w = $maxr - $minl;
  302. $h = $maxb - $mint;
  303. $x_offset = $minl;
  304. $y_offset = $mint;
  305. }
  306. }
  307. if (!$w || $w == -999999) {
  308. $w = 100;
  309. }
  310. if (!$h || $h == -999999) {
  311. $h = 100;
  312. }
  313. if ($x_offset == 999999) {
  314. $x_offset = 0;
  315. }
  316. if ($y_offset == 999999) {
  317. $y_offset = 0;
  318. }
  319. // TRANSFORMATIONS
  320. $transformations = '';
  321. if (isset($gradient_info['transform'])) {
  322. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is', $gradient_info['transform'], $m);
  323. if (count($m[0])) {
  324. for ($i = 0; $i < count($m[0]); $i++) {
  325. $c = strtolower($m[1][$i]);
  326. $v = trim($m[2][$i]);
  327. $vv = preg_split('/[ ,]+/', $v);
  328. if ($c == 'matrix' && count($vv) == 6) {
  329. // Note angle of rotation is reversed (from SVG to PDF), so vv[1] and vv[2] are negated
  330. // cf svgDefineStyle()
  331. $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);
  332. } else if ($c == 'translate' && count($vv)) {
  333. $tm[4] = $vv[0];
  334. if (count($vv) == 2) {
  335. $t_y = -$vv[1];
  336. } else {
  337. $t_y = 0;
  338. }
  339. $tm[5] = $t_y;
  340. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $tm[4] * $this->kp, $tm[5] * $this->kp);
  341. } else if ($c == 'scale' && count($vv)) {
  342. if (count($vv) == 2) {
  343. $s_y = $vv[1];
  344. } else {
  345. $s_y = $vv[0];
  346. }
  347. $tm[0] = $vv[0];
  348. $tm[3] = $s_y;
  349. $transformations .= sprintf(' %.3F 0 0 %.3F 0 0 cm ', $tm[0], $tm[3]);
  350. } else if ($c == 'rotate' && count($vv)) {
  351. $tm[0] = cos(deg2rad(-$vv[0]));
  352. $tm[1] = sin(deg2rad(-$vv[0]));
  353. $tm[2] = -$tm[1];
  354. $tm[3] = $tm[0];
  355. if (count($vv) == 3) {
  356. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $vv[1] * $this->kp, -$vv[2] * $this->kp);
  357. }
  358. $transformations .= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  359. if (count($vv) == 3) {
  360. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', -$vv[1] * $this->kp, $vv[2] * $this->kp);
  361. }
  362. } else if ($c == 'skewx' && count($vv)) {
  363. $tm[2] = tan(deg2rad(-$vv[0]));
  364. $transformations .= sprintf(' 1 0 %.3F 1 0 0 cm ', $tm[2]);
  365. } else if ($c == 'skewy' && count($vv)) {
  366. $tm[1] = tan(deg2rad(-$vv[0]));
  367. $transformations .= sprintf(' 1 %.3F 0 1 0 0 cm ', $tm[1]);
  368. }
  369. }
  370. }
  371. }
  372. $return = "";
  373. if (isset($gradient_info['units']) && strtolower($gradient_info['units']) == 'userspaceonuse') {
  374. if ($transformations) {
  375. $return .= $transformations;
  376. }
  377. }
  378. $spread = 'P'; // pad
  379. if (isset($gradient_info['spread'])) {
  380. if (strtolower($gradient_info['spread']) == 'reflect') {
  381. $spread = 'F';
  382. } // reflect
  383. else if (strtolower($gradient_info['spread']) == 'repeat') {
  384. $spread = 'R';
  385. } // repeat
  386. }
  387. for ($i = 0; $i < (count($gradient_info['color'])); $i++) {
  388. if (stristr($gradient_info['color'][$i]['offset'], '%') !== false) {
  389. $gradient_info['color'][$i]['offset'] = ($gradient_info['color'][$i]['offset'] + 0) / 100;
  390. }
  391. if (isset($gradient_info['color'][($i + 1)]['offset']) && stristr($gradient_info['color'][($i + 1)]['offset'], '%') !== false) {
  392. $gradient_info['color'][($i + 1)]['offset'] = ($gradient_info['color'][($i + 1)]['offset'] + 0) / 100;
  393. }
  394. if ($gradient_info['color'][$i]['offset'] < 0) {
  395. $gradient_info['color'][$i]['offset'] = 0;
  396. }
  397. if ($gradient_info['color'][$i]['offset'] > 1) {
  398. $gradient_info['color'][$i]['offset'] = 1;
  399. }
  400. if ($i > 0) {
  401. if ($gradient_info['color'][$i]['offset'] < $gradient_info['color'][($i - 1)]['offset']) {
  402. $gradient_info['color'][$i]['offset'] = $gradient_info['color'][($i - 1)]['offset'];
  403. }
  404. }
  405. }
  406. if (isset($gradient_info['color'][0]['offset']) && $gradient_info['color'][0]['offset'] > 0) {
  407. array_unshift($gradient_info['color'], $gradient_info['color'][0]);
  408. $gradient_info['color'][0]['offset'] = 0;
  409. }
  410. $ns = count($gradient_info['color']);
  411. if (isset($gradient_info['color'][($ns - 1)]['offset']) && $gradient_info['color'][($ns - 1)]['offset'] < 1) {
  412. $gradient_info['color'][] = $gradient_info['color'][($ns - 1)];
  413. $gradient_info['color'][($ns)]['offset'] = 1;
  414. }
  415. $ns = count($gradient_info['color']);
  416. if ($gradient_info['type'] == 'linear') {
  417. // mPDF 4.4.003
  418. if (isset($gradient_info['units']) && strtolower($gradient_info['units']) == 'userspaceonuse') {
  419. if (isset($gradient_info['info']['x1'])) {
  420. $gradient_info['info']['x1'] = ($gradient_info['info']['x1'] - $x_offset) / $w;
  421. }
  422. if (isset($gradient_info['info']['y1'])) {
  423. $gradient_info['info']['y1'] = ($gradient_info['info']['y1'] - $y_offset) / $h;
  424. }
  425. if (isset($gradient_info['info']['x2'])) {
  426. $gradient_info['info']['x2'] = ($gradient_info['info']['x2'] - $x_offset) / $w;
  427. }
  428. if (isset($gradient_info['info']['y2'])) {
  429. $gradient_info['info']['y2'] = ($gradient_info['info']['y2'] - $y_offset) / $h;
  430. }
  431. }
  432. if (isset($gradient_info['info']['x1'])) {
  433. $x1 = $gradient_info['info']['x1'];
  434. } else {
  435. $x1 = 0;
  436. }
  437. if (isset($gradient_info['info']['y1'])) {
  438. $y1 = $gradient_info['info']['y1'];
  439. } else {
  440. $y1 = 0;
  441. }
  442. if (isset($gradient_info['info']['x2'])) {
  443. $x2 = $gradient_info['info']['x2'];
  444. } else {
  445. $x2 = 1;
  446. }
  447. if (isset($gradient_info['info']['y2'])) {
  448. $y2 = $gradient_info['info']['y2'];
  449. } else {
  450. $y2 = 0;
  451. } // mPDF 6
  452. if (stristr($x1, '%') !== false) {
  453. $x1 = ($x1 + 0) / 100;
  454. }
  455. if (stristr($x2, '%') !== false) {
  456. $x2 = ($x2 + 0) / 100;
  457. }
  458. if (stristr($y1, '%') !== false) {
  459. $y1 = ($y1 + 0) / 100;
  460. }
  461. if (stristr($y2, '%') !== false) {
  462. $y2 = ($y2 + 0) / 100;
  463. }
  464. // mPDF 5.0.042
  465. $bboxw = $w;
  466. $bboxh = $h;
  467. $usex = $x_offset;
  468. $usey = $y_offset;
  469. $usew = $bboxw;
  470. $useh = $bboxh;
  471. if (isset($gradient_info['units']) && strtolower($gradient_info['units']) == 'userspaceonuse') {
  472. $angle = rad2deg(atan2(($gradient_info['info']['y2'] - $gradient_info['info']['y1']), ($gradient_info['info']['x2'] - $gradient_info['info']['x1'])));
  473. if ($angle < 0) {
  474. $angle += 360;
  475. } else if ($angle > 360) {
  476. $angle -= 360;
  477. }
  478. if ($angle != 0 && $angle != 360 && $angle != 90 && $angle != 180 && $angle != 270) {
  479. if ($w >= $h) {
  480. $y1 *= $h / $w;
  481. $y2 *= $h / $w;
  482. $usew = $useh = $bboxw;
  483. } else {
  484. $x1 *= $w / $h;
  485. $x2 *= $w / $h;
  486. $usew = $useh = $bboxh;
  487. }
  488. }
  489. }
  490. $a = $usew; // width
  491. $d = -$useh; // height
  492. $e = $usex; // x- offset
  493. $f = -$usey; // -y-offset
  494. $return .= sprintf('%.3F 0 0 %.3F %.3F %.3F cm ', $a * $this->kp, $d * $this->kp, $e * $this->kp, $f * $this->kp);
  495. if (isset($gradient_info['units']) && strtolower($gradient_info['units']) == 'objectboundingbox') {
  496. if ($transformations) {
  497. $return .= $transformations;
  498. }
  499. }
  500. $trans = false;
  501. if ($spread == 'R' || $spread == 'F') { // Repeat / Reflect
  502. $offs = array();
  503. for ($i = 0; $i < $ns; $i++) {
  504. $offs[$i] = $gradient_info['color'][$i]['offset'];
  505. }
  506. $gp = 0;
  507. $inside = true;
  508. while ($inside) {
  509. $gp++;
  510. for ($i = 0; $i < $ns; $i++) {
  511. if ($spread == 'F' && ($gp % 2) == 1) { // Reflect
  512. $gradient_info['color'][(($ns * $gp) + $i)] = $gradient_info['color'][(($ns * ($gp - 1)) + ($ns - $i - 1))];
  513. $tmp = $gp + (1 - $offs[($ns - $i - 1)]);
  514. $gradient_info['color'][(($ns * $gp) + $i)]['offset'] = $tmp;
  515. } else { // Reflect
  516. $gradient_info['color'][(($ns * $gp) + $i)] = $gradient_info['color'][$i];
  517. $tmp = $gp + $offs[$i];
  518. $gradient_info['color'][(($ns * $gp) + $i)]['offset'] = $tmp;
  519. }
  520. // IF STILL INSIDE BOX OR STILL VALID
  521. // Point on axis to test
  522. $px1 = $x1 + ($x2 - $x1) * $tmp;
  523. $py1 = $y1 + ($y2 - $y1) * $tmp;
  524. // Get perpendicular axis
  525. $alpha = atan2($y2 - $y1, $x2 - $x1);
  526. $alpha += M_PI / 2; // rotate 90 degrees
  527. // Get arbitrary point to define line perpendicular to axis
  528. $px2 = $px1 + cos($alpha);
  529. $py2 = $py1 + sin($alpha);
  530. $res1 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 0, 1); // $x=0 vert axis
  531. $res2 = _testIntersect($px1, $py1, $px2, $py2, 1, 0, 1, 1); // $x=1 vert axis
  532. $res3 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 1, 0); // $y=0 horiz axis
  533. $res4 = _testIntersect($px1, $py1, $px2, $py2, 0, 1, 1, 1); // $y=1 horiz axis
  534. if (!$res1 && !$res2 && !$res3 && !$res4) {
  535. $inside = false;
  536. }
  537. }
  538. }
  539. $inside = true;
  540. $gp = 0;
  541. while ($inside) {
  542. $gp++;
  543. $newarr = array();
  544. for ($i = 0; $i < $ns; $i++) {
  545. if ($spread == 'F') { // Reflect
  546. $newarr[$i] = $gradient_info['color'][($ns - $i - 1)];
  547. if (($gp % 2) == 1) {
  548. $tmp = -$gp + (1 - $offs[($ns - $i - 1)]);
  549. $newarr[$i]['offset'] = $tmp;
  550. } else {
  551. $tmp = -$gp + $offs[$i];
  552. $newarr[$i]['offset'] = $tmp;
  553. }
  554. } else { // Reflect
  555. $newarr[$i] = $gradient_info['color'][$i];
  556. $tmp = -$gp + $offs[$i];
  557. $newarr[$i]['offset'] = $tmp;
  558. }
  559. // IF STILL INSIDE BOX OR STILL VALID
  560. // Point on axis to test
  561. $px1 = $x1 + ($x2 - $x1) * $tmp;
  562. $py1 = $y1 + ($y2 - $y1) * $tmp;
  563. // Get perpendicular axis
  564. $alpha = atan2($y2 - $y1, $x2 - $x1);
  565. $alpha += M_PI / 2; // rotate 90 degrees
  566. // Get arbitrary point to define line perpendicular to axis
  567. $px2 = $px1 + cos($alpha);
  568. $py2 = $py1 + sin($alpha);
  569. $res1 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 0, 1); // $x=0 vert axis
  570. $res2 = _testIntersect($px1, $py1, $px2, $py2, 1, 0, 1, 1); // $x=1 vert axis
  571. $res3 = _testIntersect($px1, $py1, $px2, $py2, 0, 0, 1, 0); // $y=0 horiz axis
  572. $res4 = _testIntersect($px1, $py1, $px2, $py2, 0, 1, 1, 1); // $y=1 horiz axis
  573. if (!$res1 && !$res2 && !$res3 && !$res4) {
  574. $inside = false;
  575. }
  576. }
  577. for ($i = ($ns - 1); $i >= 0; $i--) {
  578. if (isset($newarr[$i]['offset']))
  579. array_unshift($gradient_info['color'], $newarr[$i]);
  580. }
  581. }
  582. }
  583. // Gradient STOPs
  584. $stops = count($gradient_info['color']);
  585. if ($stops < 2) {
  586. return '';
  587. }
  588. $range = $gradient_info['color'][count($gradient_info['color']) - 1]['offset'] - $gradient_info['color'][0]['offset'];
  589. $min = $gradient_info['color'][0]['offset'];
  590. for ($i = 0; $i < ($stops); $i++) {
  591. if (!$gradient_info['color'][$i]['color']) {
  592. if ($gradient_info['colorspace'] == 'RGB')
  593. $gradient_info['color'][$i]['color'] = '0 0 0';
  594. else if ($gradient_info['colorspace'] == 'Gray')
  595. $gradient_info['color'][$i]['color'] = '0';
  596. else if ($gradient_info['colorspace'] == 'CMYK')
  597. $gradient_info['color'][$i]['color'] = '1 1 1 1';
  598. }
  599. $offset = ($gradient_info['color'][$i]['offset'] - $min) / $range;
  600. $this->mpdf_ref->gradients[$n]['stops'][] = array(
  601. 'col' => $gradient_info['color'][$i]['color'],
  602. 'opacity' => $gradient_info['color'][$i]['opacity'],
  603. 'offset' => $offset);
  604. if ($gradient_info['color'][$i]['opacity'] < 1) {
  605. $trans = true;
  606. }
  607. }
  608. $grx1 = $x1 + ($x2 - $x1) * $gradient_info['color'][0]['offset'];
  609. $gry1 = $y1 + ($y2 - $y1) * $gradient_info['color'][0]['offset'];
  610. $grx2 = $x1 + ($x2 - $x1) * $gradient_info['color'][count($gradient_info['color']) - 1]['offset'];
  611. $gry2 = $y1 + ($y2 - $y1) * $gradient_info['color'][count($gradient_info['color']) - 1]['offset'];
  612. $this->mpdf_ref->gradients[$n]['coords'] = array($grx1, $gry1, $grx2, $gry2);
  613. $this->mpdf_ref->gradients[$n]['colorspace'] = $gradient_info['colorspace'];
  614. $this->mpdf_ref->gradients[$n]['type'] = 2;
  615. $this->mpdf_ref->gradients[$n]['fo'] = true;
  616. $this->mpdf_ref->gradients[$n]['extend'] = array('true', 'true');
  617. if ($trans) {
  618. $this->mpdf_ref->gradients[$n]['trans'] = true;
  619. $return .= ' /TGS' . ($n) . ' gs ';
  620. }
  621. $return .= ' /Sh' . ($n) . ' sh ';
  622. $return .= " Q\n";
  623. } else if ($gradient_info['type'] == 'radial') {
  624. if (isset($gradient_info['units']) && strtolower($gradient_info['units']) == 'userspaceonuse') {
  625. if ($w > $h) {
  626. $h = $w;
  627. } else {
  628. $w = $h;
  629. }
  630. if (isset($gradient_info['info']['x0'])) {
  631. $gradient_info['info']['x0'] = ($gradient_info['info']['x0'] - $x_offset) / $w;
  632. }
  633. if (isset($gradient_info['info']['y0'])) {
  634. $gradient_info['info']['y0'] = ($gradient_info['info']['y0'] - $y_offset) / $h;
  635. }
  636. if (isset($gradient_info['info']['x1'])) {
  637. $gradient_info['info']['x1'] = ($gradient_info['info']['x1'] - $x_offset) / $w;
  638. }
  639. if (isset($gradient_info['info']['y1'])) {
  640. $gradient_info['info']['y1'] = ($gradient_info['info']['y1'] - $y_offset) / $h;
  641. }
  642. if (isset($gradient_info['info']['r'])) {
  643. $gradient_info['info']['rx'] = $gradient_info['info']['r'] / $w;
  644. }
  645. if (isset($gradient_info['info']['r'])) {
  646. $gradient_info['info']['ry'] = $gradient_info['info']['r'] / $h;
  647. }
  648. }
  649. if (isset($gradient_info['info']['x0'])) {
  650. $x0 = $gradient_info['info']['x0'];
  651. } else {
  652. $x0 = 0.5;
  653. }
  654. if (isset($gradient_info['info']['y0'])) {
  655. $y0 = $gradient_info['info']['y0'];
  656. } else {
  657. $y0 = 0.5;
  658. }
  659. if (isset($gradient_info['info']['rx'])) {
  660. $rx = $gradient_info['info']['rx'];
  661. } else if (isset($gradient_info['info']['r'])) {
  662. $rx = $gradient_info['info']['r'];
  663. } else {
  664. $rx = 0.5;
  665. }
  666. if (isset($gradient_info['info']['ry'])) {
  667. $ry = $gradient_info['info']['ry'];
  668. } else if (isset($gradient_info['info']['r'])) {
  669. $ry = $gradient_info['info']['r'];
  670. } else {
  671. $ry = 0.5;
  672. }
  673. if (isset($gradient_info['info']['x1'])) {
  674. $x1 = $gradient_info['info']['x1'];
  675. } else {
  676. $x1 = $x0;
  677. }
  678. if (isset($gradient_info['info']['y1'])) {
  679. $y1 = $gradient_info['info']['y1'];
  680. } else {
  681. $y1 = $y0;
  682. }
  683. if (stristr($x1, '%') !== false) {
  684. $x1 = ($x1 + 0) / 100;
  685. }
  686. if (stristr($x0, '%') !== false) {
  687. $x0 = ($x0 + 0) / 100;
  688. }
  689. if (stristr($y1, '%') !== false) {
  690. $y1 = ($y1 + 0) / 100;
  691. }
  692. if (stristr($y0, '%') !== false) {
  693. $y0 = ($y0 + 0) / 100;
  694. }
  695. if (stristr($rx, '%') !== false) {
  696. $rx = ($rx + 0) / 100;
  697. }
  698. if (stristr($ry, '%') !== false) {
  699. $ry = ($ry + 0) / 100;
  700. }
  701. $bboxw = $w;
  702. $bboxh = $h;
  703. $usex = $x_offset;
  704. $usey = $y_offset;
  705. $usew = $bboxw;
  706. $useh = $bboxh;
  707. if (isset($gradient_info['units']) && strtolower($gradient_info['units']) == 'userspaceonuse') {
  708. $angle = rad2deg(atan2(($gradient_info['info']['y0'] - $gradient_info['info']['y1']), ($gradient_info['info']['x0'] - $gradient_info['info']['x1'])));
  709. if ($angle < 0) {
  710. $angle += 360;
  711. } else if ($angle > 360) {
  712. $angle -= 360;
  713. }
  714. if ($angle != 0 && $angle != 360 && $angle != 90 && $angle != 180 && $angle != 270) {
  715. if ($w >= $h) {
  716. $y1 *= $h / $w;
  717. $y0 *= $h / $w;
  718. $rx *= $h / $w;
  719. $ry *= $h / $w;
  720. $usew = $useh = $bboxw;
  721. } else {
  722. $x1 *= $w / $h;
  723. $x0 *= $w / $h;
  724. $rx *= $w / $h;
  725. $ry *= $w / $h;
  726. $usew = $useh = $bboxh;
  727. }
  728. }
  729. }
  730. $a = $usew; // width
  731. $d = -$useh; // height
  732. $e = $usex; // x- offset
  733. $f = -$usey; // -y-offset
  734. $r = $rx;
  735. $return .= sprintf('%.3F 0 0 %.3F %.3F %.3F cm ', $a * $this->kp, $d * $this->kp, $e * $this->kp, $f * $this->kp);
  736. // mPDF 5.0.039
  737. if (isset($gradient_info['units']) && strtolower($gradient_info['units']) == 'objectboundingbox') {
  738. if ($transformations) {
  739. $return .= $transformations;
  740. }
  741. }
  742. // mPDF 5.7.4
  743. // x1 and y1 (fx, fy) should be inside the circle defined by x0 y0 (cx, cy)
  744. // "If the point defined by fx and fy lies outside the circle defined by cx, cy and r, then the user agent shall set
  745. // the focal point to the intersection of the line from (cx, cy) to (fx, fy) with the circle defined by cx, cy and r."
  746. while (pow(($x1 - $x0), 2) + pow(($y1 - $y0), 2) >= pow($r, 2)) {
  747. // Gradually move along fx,fy towards cx,cy in 100'ths until meets criteria
  748. $x1 -= ($x1 - $x0) / 100;
  749. $y1 -= ($y1 - $y0) / 100;
  750. }
  751. if ($spread == 'R' || $spread == 'F') { // Repeat / Reflect
  752. $offs = array();
  753. for ($i = 0; $i < $ns; $i++) {
  754. $offs[$i] = $gradient_info['color'][$i]['offset'];
  755. }
  756. $gp = 0;
  757. $inside = true;
  758. while ($inside) {
  759. $gp++;
  760. for ($i = 0; $i < $ns; $i++) {
  761. if ($spread == 'F' && ($gp % 2) == 1) { // Reflect
  762. $gradient_info['color'][(($ns * $gp) + $i)] = $gradient_info['color'][(($ns * ($gp - 1)) + ($ns - $i - 1))];
  763. $tmp = $gp + (1 - $offs[($ns - $i - 1)]);
  764. $gradient_info['color'][(($ns * $gp) + $i)]['offset'] = $tmp;
  765. } else { // Reflect
  766. $gradient_info['color'][(($ns * $gp) + $i)] = $gradient_info['color'][$i];
  767. $tmp = $gp + $offs[$i];
  768. $gradient_info['color'][(($ns * $gp) + $i)]['offset'] = $tmp;
  769. }
  770. // IF STILL INSIDE BOX OR STILL VALID
  771. // TEST IF circle (perimeter) intersects with
  772. // or is enclosed
  773. // Point on axis to test
  774. $px = $x1 + ($x0 - $x1) * $tmp;
  775. $py = $y1 + ($y0 - $y1) * $tmp;
  776. $pr = $r * $tmp;
  777. $res = _testIntersectCircle($px, $py, $pr);
  778. if (!$res) {
  779. $inside = false;
  780. }
  781. }
  782. }
  783. }
  784. // Gradient STOPs
  785. $stops = count($gradient_info['color']);
  786. if ($stops < 2) {
  787. return '';
  788. }
  789. $range = $gradient_info['color'][count($gradient_info['color']) - 1]['offset'] - $gradient_info['color'][0]['offset'];
  790. $min = $gradient_info['color'][0]['offset'];
  791. for ($i = 0; $i < ($stops); $i++) {
  792. if (!$gradient_info['color'][$i]['color']) {
  793. if ($gradient_info['colorspace'] == 'RGB')
  794. $gradient_info['color'][$i]['color'] = '0 0 0';
  795. else if ($gradient_info['colorspace'] == 'Gray')
  796. $gradient_info['color'][$i]['color'] = '0';
  797. else if ($gradient_info['colorspace'] == 'CMYK')
  798. $gradient_info['color'][$i]['color'] = '1 1 1 1';
  799. }
  800. $offset = ($gradient_info['color'][$i]['offset'] - $min) / $range;
  801. $this->mpdf_ref->gradients[$n]['stops'][] = array(
  802. 'col' => $gradient_info['color'][$i]['color'],
  803. 'opacity' => $gradient_info['color'][$i]['opacity'],
  804. 'offset' => $offset);
  805. if ($gradient_info['color'][$i]['opacity'] < 1) {
  806. $trans = true;
  807. }
  808. }
  809. $grx1 = $x1 + ($x0 - $x1) * $gradient_info['color'][0]['offset'];
  810. $gry1 = $y1 + ($y0 - $y1) * $gradient_info['color'][0]['offset'];
  811. $grx2 = $x1 + ($x0 - $x1) * $gradient_info['color'][count($gradient_info['color']) - 1]['offset'];
  812. $gry2 = $y1 + ($y0 - $y1) * $gradient_info['color'][count($gradient_info['color']) - 1]['offset'];
  813. $grir = $r * $gradient_info['color'][0]['offset'];
  814. $grr = $r * $gradient_info['color'][count($gradient_info['color']) - 1]['offset'];
  815. $this->mpdf_ref->gradients[$n]['coords'] = array($grx1, $gry1, $grx2, $gry2, abs($grr), abs($grir));
  816. $this->mpdf_ref->gradients[$n]['colorspace'] = $gradient_info['colorspace'];
  817. $this->mpdf_ref->gradients[$n]['type'] = 3;
  818. $this->mpdf_ref->gradients[$n]['fo'] = true;
  819. $this->mpdf_ref->gradients[$n]['extend'] = array('true', 'true');
  820. if (isset($trans) && $trans) {
  821. $this->mpdf_ref->gradients[$n]['trans'] = true;
  822. $return .= ' /TGS' . ($n) . ' gs ';
  823. }
  824. $return .= ' /Sh' . ($n) . ' sh ';
  825. $return .= " Q\n";
  826. }
  827. return $return;
  828. }
  829. function svgOffset($attribs)
  830. {
  831. // save all <svg> tag attributes
  832. $this->svg_attribs = $attribs;
  833. if (isset($this->svg_attribs['viewBox'])) {
  834. $vb = preg_split('/\s+/is', trim($this->svg_attribs['viewBox']));
  835. if (count($vb) == 4) {
  836. $this->svg_info['x'] = $vb[0];
  837. $this->svg_info['y'] = $vb[1];
  838. $this->svg_info['w'] = $vb[2];
  839. $this->svg_info['h'] = $vb[3];
  840. // return;
  841. }
  842. }
  843. $svg_w = 0;
  844. $svg_h = 0;
  845. if (isset($attribs['width']) && $attribs['width'])
  846. $svg_w = $this->mpdf_ref->ConvertSize($attribs['width']); // mm (interprets numbers as pixels)
  847. if (isset($attribs['height']) && $attribs['height'])
  848. $svg_h = $this->mpdf_ref->ConvertSize($attribs['height']); // mm
  849. ///*
  850. // mPDF 5.0.005
  851. if (isset($this->svg_info['w']) && $this->svg_info['w']) { // if 'w' set by viewBox
  852. if ($svg_w) { // if width also set, use these values to determine to set size of "pixel"
  853. $this->kp *= ($svg_w / 0.2645) / $this->svg_info['w'];
  854. $this->kf = ($svg_w / 0.2645) / $this->svg_info['w'];
  855. } else if ($svg_h) {
  856. $this->kp *= ($svg_h / 0.2645) / $this->svg_info['h'];
  857. $this->kf = ($svg_h / 0.2645) / $this->svg_info['h'];
  858. }
  859. return;
  860. }
  861. //*/
  862. // Added to handle file without height or width specified
  863. if (!$svg_w && !$svg_h) {
  864. $svg_w = $svg_h = $this->mpdf_ref->blk[$this->mpdf_ref->blklvl]['inner_width'];
  865. } // DEFAULT
  866. if (!$svg_w) {
  867. $svg_w = $svg_h;
  868. }
  869. if (!$svg_h) {
  870. $svg_h = $svg_w;
  871. }
  872. $this->svg_info['x'] = 0;
  873. $this->svg_info['y'] = 0;
  874. $this->svg_info['w'] = $svg_w / 0.2645; // mm->pixels
  875. $this->svg_info['h'] = $svg_h / 0.2645; // mm->pixels
  876. }
  877. //
  878. // check if points are within svg, if not, set to max
  879. function svg_overflow($x, $y)
  880. {
  881. $x2 = $x;
  882. $y2 = $y;
  883. if (isset($this->svg_attribs['overflow'])) {
  884. if ($this->svg_attribs['overflow'] == 'hidden') {
  885. // Not sure if this is supposed to strip off units, but since I dont use any I will omlt this step
  886. $svg_w = preg_replace("/([0-9\.]*)(.*)/i", "$1", $this->svg_attribs['width']);
  887. $svg_h = preg_replace("/([0-9\.]*)(.*)/i", "$1", $this->svg_attribs['height']);
  888. // $xmax = floor($this->svg_attribs['width']);
  889. $xmax = floor($svg_w);
  890. $xmin = 0;
  891. // $ymax = floor(($this->svg_attribs['height'] * -1));
  892. $ymax = floor(($svg_h * -1));
  893. $ymin = 0;
  894. if ($x > $xmax)
  895. $x2 = $xmax; // right edge
  896. if ($x < $xmin)
  897. $x2 = $xmin; // left edge
  898. if ($y < $ymax)
  899. $y2 = $ymax; // bottom
  900. if ($y > $ymin)
  901. $y2 = $ymin; // top
  902. }
  903. }
  904. return array('x' => $x2, 'y' => $y2);
  905. }
  906. function svgDefineStyle($critere_style)
  907. {
  908. $tmp = count($this->svg_style) - 1;
  909. $current_style = $this->svg_style[$tmp];
  910. unset($current_style['transformations']);
  911. // TRANSFORM SCALE
  912. $transformations = '';
  913. if (isset($critere_style['transform'])) {
  914. preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)\((.*?)\)/is', $critere_style['transform'], $m);
  915. if (count($m[0])) {
  916. for ($i = 0; $i < count($m[0]); $i++) {
  917. $c = strtolower($m[1][$i]);
  918. $v = trim($m[2][$i]);
  919. $vv = preg_split('/[ ,]+/', $v);
  920. if ($c == 'matrix' && count($vv) == 6) {
  921. // mPDF 5.0.039
  922. // Note angle of rotation is reversed (from SVG to PDF), so vv[1] and vv[2] are negated
  923. $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);
  924. /*
  925. // The long way of doing this??
  926. // need to reverse angle of rotation from SVG to PDF
  927. $sx=sqrt(pow($vv[0],2)+pow($vv[2],2));
  928. if ($vv[0] < 0) { $sx *= -1; } // change sign
  929. $sy=sqrt(pow($vv[1],2)+pow($vv[3],2));
  930. if ($vv[3] < 0) { $sy *= -1; } // change sign
  931. // rotation angle is
  932. $t=atan2($vv[1],$vv[3]);
  933. $t=atan2(-$vv[2],$vv[0]); // Should be the same value or skew has been applied
  934. // Reverse angle
  935. $t *= -1;
  936. // Rebuild matrix
  937. $ma = $sx * cos($t);
  938. $mb = $sy * sin($t);
  939. $mc = -$sx * sin($t);
  940. $md = $sy * cos($t);
  941. // $transformations .= sprintf(' %.3F %.3F %.3F %.3F %.3F %.3F cm ', $ma, $mb, $mc, $md, $vv[4]*$this->kp, -$vv[5]*$this->kp);
  942. */
  943. } else if ($c == 'translate' && count($vv)) {
  944. $tm[4] = $vv[0];
  945. if (count($vv) == 2) {
  946. $t_y = -$vv[1];
  947. } else {
  948. $t_y = 0;
  949. }
  950. $tm[5] = $t_y;
  951. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $tm[4] * $this->kp, $tm[5] * $this->kp);
  952. } else if ($c == 'scale' && count($vv)) {
  953. if (count($vv) == 2) {
  954. $s_y = $vv[1];
  955. } else {
  956. $s_y = $vv[0];
  957. }
  958. $tm[0] = $vv[0];
  959. $tm[3] = $s_y;
  960. $transformations .= sprintf(' %.3F 0 0 %.3F 0 0 cm ', $tm[0], $tm[3]);
  961. } else if ($c == 'rotate' && count($vv)) {
  962. $tm[0] = cos(deg2rad(-$vv[0]));
  963. $tm[1] = sin(deg2rad(-$vv[0]));
  964. $tm[2] = -$tm[1];
  965. $tm[3] = $tm[0];
  966. if (count($vv) == 3) {
  967. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', $vv[1] * $this->kp, -$vv[2] * $this->kp);
  968. }
  969. $transformations .= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm ', $tm[0], $tm[1], $tm[2], $tm[3]);
  970. if (count($vv) == 3) {
  971. $transformations .= sprintf(' 1 0 0 1 %.3F %.3F cm ', -$vv[1] * $this->kp, $vv[2] * $this->kp);
  972. }
  973. } else if ($c == 'skewx' && count($vv)) {
  974. $tm[2] = tan(deg2rad(-$vv[0]));
  975. $transformations .= sprintf(' 1 0 %.3F 1 0 0 cm ', $tm[2]);
  976. } else if ($c == 'skewy' && count($vv)) {
  977. $tm[1] = tan(deg2rad(-$vv[0]));
  978. $transformations .= sprintf(' 1 %.3F 0 1 0 0 cm ', $tm[1]);
  979. }
  980. }
  981. }
  982. $current_style['transformations'] = $transformations;
  983. }
  984. if (isset($critere_style['style'])) {
  985. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/i', $critere_style['style'], $m)) { // mPDF 5.7.2
  986. $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);
  987. } else {
  988. $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i", "$2", $critere_style['style']);
  989. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  990. $current_style['fill'] = $tmp;
  991. }
  992. }
  993. // mPDF 5.7.2
  994. if ((preg_match("/[^-]opacity:\s*([a-z0-9.]*|none)/i", $critere_style['style'], $m) ||
  995. preg_match("/^opacity:\s*([a-z0-9.]*|none)/i", $critere_style['style'], $m)) && $m[1] != 'inherit') {
  996. $current_style['fill-opacity'] = $m[1];
  997. $current_style['stroke-opacity'] = $m[1];
  998. }
  999. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  1000. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1001. $current_style['fill-opacity'] = $tmp;
  1002. }
  1003. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  1004. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1005. $current_style['fill-rule'] = $tmp;
  1006. }
  1007. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/', $critere_style['style'], $m)) {
  1008. $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);
  1009. } else {
  1010. $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  1011. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1012. $current_style['stroke'] = $tmp;
  1013. }
  1014. }
  1015. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  1016. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1017. $current_style['stroke-linecap'] = $tmp;
  1018. }
  1019. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  1020. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1021. $current_style['stroke-linejoin'] = $tmp;
  1022. }
  1023. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  1024. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1025. $current_style['stroke-miterlimit'] = $tmp;
  1026. }
  1027. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  1028. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1029. $current_style['stroke-opacity'] = $tmp;
  1030. }
  1031. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  1032. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1033. $current_style['stroke-width'] = $tmp;
  1034. }
  1035. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i", "$2", $critere_style['style']);
  1036. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1037. $current_style['stroke-dasharray'] = $tmp;
  1038. }
  1039. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  1040. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  1041. $current_style['stroke-dashoffset'] = $tmp;
  1042. }
  1043. }
  1044. // mPDF 5.7.2
  1045. if (isset($critere_style['opacity']) && $critere_style['opacity'] != 'inherit') {
  1046. $current_style['fill-opacity'] = $critere_style['opacity'];
  1047. $current_style['stroke-opacity'] = $critere_style['opacity'];
  1048. }
  1049. if (isset($critere_style['fill']) && $critere_style['fill'] != 'inherit') {
  1050. $current_style['fill'] = $critere_style['fill'];
  1051. }
  1052. if (isset($critere_style['fill-opacity']) && $critere_style['fill-opacity'] != 'inherit') {
  1053. $current_style['fill-opacity'] = $critere_style['fill-opacity'];
  1054. }
  1055. if (isset($critere_style['fill-rule']) && $critere_style['fill-rule'] != 'inherit') {
  1056. $current_style['fill-rule'] = $critere_style['fill-rule'];
  1057. }
  1058. if (isset($critere_style['stroke']) && $critere_style['stroke'] != 'inherit') {
  1059. $current_style['stroke'] = $critere_style['stroke'];
  1060. }
  1061. if (isset($critere_style['stroke-linecap']) && $critere_style['stroke-linecap'] != 'inherit') {
  1062. $current_style['stroke-linecap'] = $critere_style['stroke-linecap'];
  1063. }
  1064. if (isset($critere_style['stroke-linejoin']) && $critere_style['stroke-linejoin'] != 'inherit') {
  1065. $current_style['stroke-linejoin'] = $critere_style['stroke-linejoin'];
  1066. }
  1067. if (isset($critere_style['stroke-miterlimit']) && $critere_style['stroke-miterlimit'] != 'inherit') {
  1068. $current_style['stroke-miterlimit'] = $critere_style['stroke-miterlimit'];
  1069. }
  1070. if (isset($critere_style['stroke-opacity']) && $critere_style['stroke-opacity'] != 'inherit') {
  1071. $current_style['stroke-opacity'] = $critere_style['stroke-opacity'];
  1072. }
  1073. if (isset($critere_style['stroke-width']) && $critere_style['stroke-width'] != 'inherit') {
  1074. $current_style['stroke-width'] = $critere_style['stroke-width'];
  1075. }
  1076. if (isset($critere_style['stroke-dasharray']) && $critere_style['stroke-dasharray'] != 'inherit') {
  1077. $current_style['stroke-dasharray'] = $critere_style['stroke-dasharray'];
  1078. }
  1079. if (isset($critere_style['stroke-dashoffset']) && $critere_style['stroke-dashoffset'] != 'inherit') {
  1080. $current_style['stroke-dashoffset'] = $critere_style['stroke-dashoffset'];
  1081. }
  1082. // Used as indirect setting for currentColor
  1083. if (isset($critere_style['color']) && $critere_style['color'] != 'inherit') {
  1084. $current_style['color'] = $critere_style['color'];
  1085. }
  1086. return $current_style;
  1087. }
  1088. //
  1089. // Cette fonction ecrit le style dans le stream svg.
  1090. function svgStyle($critere_style, $attribs, $element)
  1091. {
  1092. $path_style = '';
  1093. $fill_gradient = '';
  1094. $w = '';
  1095. $style = '';
  1096. if (substr_count($critere_style['fill'], 'url') > 0 && $element != 'line') {
  1097. //
  1098. // couleur degradé
  1099. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i", "$1", $critere_style['fill']);
  1100. if ($id_gradient != $critere_style['fill']) {
  1101. if (isset($this->svg_gradient[$id_gradient])) {
  1102. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  1103. if ($fill_gradient) {
  1104. $path_style = "q ";
  1105. $w = "W";
  1106. $style .= 'N';
  1107. }
  1108. }
  1109. }
  1110. }
  1111. // Used as indirect setting for currentColor
  1112. else if (strtolower($critere_style['fill']) == 'currentcolor' && $element != 'line') {
  1113. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  1114. if ($col) {
  1115. if ($col{0} == 5) {
  1116. $critere_style['fill-opacity'] = ord($col{4} / 100);
  1117. } // RGBa
  1118. if ($col{0} == 6) {
  1119. $critere_style['fill-opacity'] = ord($col{5} / 100);
  1120. } // CMYKa
  1121. $path_style .= $this->mpdf_ref->SetFColor($col, true) . ' ';
  1122. $style .= 'F';
  1123. }
  1124. } else if ($critere_style['fill'] != 'none' && $element != 'line') {
  1125. $col = $this->mpdf_ref->ConvertColor($critere_style['fill']);
  1126. if ($col) {
  1127. if ($col{0} == 5) {
  1128. $critere_style['fill-opacity'] = ord($col{4} / 100);
  1129. } // RGBa
  1130. if ($col{0} == 6) {
  1131. $critere_style['fill-opacity'] = ord($col{5} / 100);
  1132. } // CMYKa
  1133. $path_style .= $this->mpdf_ref->SetFColor($col, true) . ' ';
  1134. $style .= 'F';
  1135. }
  1136. }
  1137. if (substr_count($critere_style['stroke'], 'url') > 0) {
  1138. /*
  1139. // Cannot put a gradient on a "stroke" in PDF?
  1140. $id_gradient = preg_replace("/url\(#([\w_]*)\)/i","$1",$critere_style['stroke']);
  1141. if ($id_gradient != $critere_style['stroke']) {
  1142. if (isset($this->svg_gradient[$id_gradient])) {
  1143. $fill_gradient = $this->svgGradient($this->svg_gradient[$id_gradient], $attribs, $element);
  1144. if ($fill_gradient) {
  1145. $path_style = "q ";
  1146. $w = "W";
  1147. $style .= 'D';
  1148. }
  1149. }
  1150. }
  1151. */
  1152. }
  1153. // Used as indirect setting for currentColor
  1154. else if (strtolower($critere_style['stroke']) == 'currentcolor') {
  1155. $col = $this->mpdf_ref->ConvertColor($critere_style['color']);
  1156. if ($col) {
  1157. if ($col{0} == 5) {
  1158. $critere_style['stroke-opacity'] = ord($col{4} / 100);
  1159. } // RGBa
  1160. if ($col{0} == 6) {
  1161. $critere_style['stroke-opacity'] = ord($col{5} / 100);
  1162. } // CMYKa
  1163. $path_style .= $this->mpdf_ref->SetDColor($col, true) . ' ';
  1164. $style .= 'D';
  1165. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']);
  1166. $path_style .= sprintf('%.3F w ', $lw * $this->kp);
  1167. }
  1168. } else if ($critere_style['stroke'] != 'none') {
  1169. $col = $this->mpdf_ref->ConvertColor($critere_style['stroke']);
  1170. if ($col) {
  1171. // mPDF 5.0.051
  1172. // mPDF 5.3.74
  1173. if ($col{0} == 5) {
  1174. $critere_style['stroke-opacity'] = ord($col{4} / 100);
  1175. } // RGBa
  1176. if ($col{0} == 6) {
  1177. $critere_style['stroke-opacity'] = ord($col{5} / 100);
  1178. } // CMYKa
  1179. $path_style .= $this->mpdf_ref->SetDColor($col, true) . ' ';
  1180. $style .= 'D';
  1181. $lw = $this->ConvertSVGSizePixels($critere_style['stroke-width']);
  1182. $path_style .= sprintf('%.3F w ', $lw * $this->kp);
  1183. }
  1184. }
  1185. if ($critere_style['stroke'] != 'none') {
  1186. if ($critere_style['stroke-linejoin'] == 'miter') {
  1187. $path_style .= ' 0 j ';
  1188. } else if ($critere_style['stroke-linejoin'] == 'round') {
  1189. $path_style .= ' 1 j ';
  1190. } else if ($critere_style['stroke-linejoin'] == 'bevel') {
  1191. $path_style .= ' 2 j ';
  1192. }
  1193. if ($critere_style['stroke-linecap'] == 'butt') {
  1194. $path_style .= ' 0 J ';
  1195. } else if ($critere_style['stroke-linecap'] == 'round') {
  1196. $path_style .= ' 1 J ';
  1197. } else if ($critere_style['stroke-linecap'] == 'square') {
  1198. $path_style .= ' 2 J ';
  1199. }
  1200. if (isset($critere_style['stroke-miterlimit'])) {
  1201. if ($critere_style['stroke-miterlimit'] == 'none') {
  1202. } else if (preg_match('/^[\d.]+$/', $critere_style['stroke-miterlimit'])) {
  1203. $path_style .= sprintf('%.2F M ', $critere_style['stroke-miterlimit']);
  1204. }
  1205. }
  1206. if (isset($critere_style['stroke-dasharray'])) {
  1207. $off = 0;
  1208. $d = preg_split('/[ ,]/', $critere_style['stroke-dasharray']);
  1209. if (count($d) == 1 && $d[0] == 0) {
  1210. $path_style .= '[] 0 d ';
  1211. } else {
  1212. if (count($d) % 2 == 1) {
  1213. $d = array_merge($d, $d);
  1214. } // 5, 3, 1 => 5,3,1,5,3,1 OR 3 => 3,3
  1215. $arr = '';
  1216. for ($i = 0; $i < count($d); $i+=2) {
  1217. $arr .= sprintf('%.3F %.3F ', $d[$i] * $this->kp, $d[$i + 1] * $this->kp);
  1218. }
  1219. if (isset($critere_style['stroke-dashoffset'])) {
  1220. $off = $critere_style['stroke-dashoffset'] + 0;
  1221. }
  1222. $path_style .= sprintf('[%s] %.3F d ', $arr, $off * $this->kp);
  1223. }
  1224. }
  1225. }
  1226. if ($critere_style['fill-rule'] == 'evenodd') {
  1227. $fr = '*';
  1228. } else {
  1229. $fr = '';
  1230. }
  1231. if (isset($critere_style['fill-opacity'])) {
  1232. $opacity = 1;
  1233. if ($critere_style['fill-opacity'] == 0) {
  1234. $opacity = 0;
  1235. } else if ($critere_style['fill-opacity'] > 1) {
  1236. $opacity = 1;
  1237. } else if ($critere_style['fill-opacity'] > 0) {
  1238. $opacity = $critere_style['fill-opacity'];
  1239. } else if ($critere_style['fill-opacity'] < 0) {
  1240. $opacity = 0;
  1241. }
  1242. $gs = $this->mpdf_ref->AddExtGState(array('ca' => $opacity, 'BM' => '/Normal'));
  1243. $this->mpdf_ref->extgstates[$gs]['fo'] = true;
  1244. $path_style .= sprintf(' /GS%d gs ', $gs);
  1245. }
  1246. if (isset($critere_style['stroke-opacity'])) {
  1247. $opacity = 1;
  1248. if ($critere_style['stroke-opacity'] == 0) {
  1249. $opacity = 0;
  1250. } else if ($critere_style['stroke-opacity'] > 1) {
  1251. $opacity = 1;
  1252. } else if ($critere_style['stroke-opacity'] > 0) {
  1253. $opacity = $critere_style['stroke-opacity'];
  1254. } else if ($critere_style['stroke-opacity'] < 0) {
  1255. $opacity = 0;
  1256. }
  1257. $gs = $this->mpdf_ref->AddExtGState(array('CA' => $opacity, 'BM' => '/Normal'));
  1258. $this->mpdf_ref->extgstates[$gs]['fo'] = true;
  1259. $path_style .= sprintf(' /GS%d gs ', $gs);
  1260. }
  1261. switch ($style) {
  1262. case 'F':
  1263. $op = 'f';
  1264. break;
  1265. case 'FD':
  1266. $op = 'B';
  1267. break;
  1268. case 'ND':
  1269. $op = 'S';
  1270. break;
  1271. case 'D':
  1272. $op = 'S';
  1273. break;
  1274. default:
  1275. $op = 'n';
  1276. }
  1277. $prestyle = $path_style . ' ';
  1278. $poststyle = $w . ' ' . $op . $fr . ' ' . $fill_gradient . "\n";
  1279. return array($prestyle, $poststyle);
  1280. }
  1281. // fonction retracant les <path />
  1282. function svgPath($command, $arguments)
  1283. {
  1284. $path_cmd = '';
  1285. $newsubpath = false;
  1286. // mPDF 5.0.039
  1287. $minl = $this->pathBBox[0];
  1288. $mint = $this->pathBBox[1];
  1289. $maxr = $this->pathBBox[2] + $this->pathBBox[0];
  1290. $maxb = $this->pathBBox[3] + $this->pathBBox[1];
  1291. $start = array($this->xbase, -$this->ybase);
  1292. preg_match_all('/[\-^]?[\d.]+(e[\-]?[\d]+){0,1}/i', $arguments, $a, PREG_SET_ORDER);
  1293. // if the command is a capital letter, the coords go absolute, otherwise relative
  1294. if (strtolower($command) == $command)
  1295. $relative = true;
  1296. else
  1297. $relative = false;
  1298. $ile_argumentow = count($a);
  1299. // each command may have different needs for arguments [1 to 8]
  1300. switch (strtolower($command)) {
  1301. case 'm': // move
  1302. for ($i = 0; $i < $ile_argumentow; $i+=2) {
  1303. $x = $a[$i][0];
  1304. $y = $a[$i + 1][0];
  1305. if ($relative) {
  1306. $pdfx = ($this->xbase + $x);
  1307. $pdfy = ($this->ybase - $y);
  1308. $this->xbase += $x;
  1309. $this->ybase += -$y;
  1310. } else {
  1311. $pdfx = $x;
  1312. $pdfy = -$y;
  1313. $this->xbase = $x;
  1314. $this->ybase = -$y;
  1315. }
  1316. $pdf_pt = $this->svg_overflow($pdfx, $pdfy);
  1317. $minl = min($minl, $pdf_pt['x']);
  1318. $maxr = max($maxr, $pdf_pt['x']);
  1319. $mint = min($mint, -$pdf_pt['y']);
  1320. $maxb = max($maxb, -$pdf_pt['y']);
  1321. if ($i == 0)
  1322. $path_cmd .= sprintf('%.3F %.3F m ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1323. else
  1324. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1325. // mPDF 4.4.003 Save start points of subpath
  1326. if ($this->subPathInit) {
  1327. $this->spxstart = $this->xbase;
  1328. $this->spystart = $this->ybase;
  1329. $this->subPathInit = false;
  1330. }
  1331. }
  1332. break;
  1333. case 'l': // a simple line
  1334. for ($i = 0; $i < $ile_argumentow; $i+=2) {
  1335. $x = ($a[$i][0]);
  1336. $y = ($a[$i + 1][0]);
  1337. if ($relative) {
  1338. $pdfx = ($this->xbase + $x);
  1339. $pdfy = ($this->ybase - $y);
  1340. $this->xbase += $x;
  1341. $this->ybase += -$y;
  1342. } else {
  1343. $pdfx = $x;
  1344. $pdfy = -$y;
  1345. $this->xbase = $x;
  1346. $this->ybase = -$y;
  1347. }
  1348. $pdf_pt = $this->svg_overflow($pdfx, $pdfy);
  1349. $minl = min($minl, $pdf_pt['x']);
  1350. $maxr = max($maxr, $pdf_pt['x']);
  1351. $mint = min($mint, -$pdf_pt['y']);
  1352. $maxb = max($maxb, -$pdf_pt['y']);
  1353. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1354. }
  1355. break;
  1356. case 'h': // a very simple horizontal line
  1357. for ($i = 0; $i < $ile_argumentow; $i++) {
  1358. $x = ($a[$i][0]);
  1359. if ($relative) {
  1360. $y = 0;
  1361. $pdfx = ($this->xbase + $x);
  1362. $pdfy = ($this->ybase - $y);
  1363. $this->xbase += $x;
  1364. $this->ybase += -$y;
  1365. } else {
  1366. $y = -$this->ybase;
  1367. $pdfx = $x;
  1368. $pdfy = -$y;
  1369. $this->xbase = $x;
  1370. $this->ybase = -$y;
  1371. }
  1372. $pdf_pt = $this->svg_overflow($pdfx, $pdfy);
  1373. $minl = min($minl, $pdf_pt['x']);
  1374. $maxr = max($maxr, $pdf_pt['x']);
  1375. $mint = min($mint, -$pdf_pt['y']);
  1376. $maxb = max($maxb, -$pdf_pt['y']);
  1377. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1378. }
  1379. break;
  1380. case 'v': // the simplest line, vertical
  1381. for ($i = 0; $i < $ile_argumentow; $i++) {
  1382. $y = ($a[$i][0]);
  1383. if ($relative) {
  1384. $x = 0;
  1385. $pdfx = ($this->xbase + $x);
  1386. $pdfy = ($this->ybase - $y);
  1387. $this->xbase += $x;
  1388. $this->ybase += -$y;
  1389. } else {
  1390. $x = $this->xbase;
  1391. $pdfx = $x;
  1392. $pdfy = -$y;
  1393. $this->xbase = $x;
  1394. $this->ybase = -$y;
  1395. }
  1396. $pdf_pt = $this->svg_overflow($pdfx, $pdfy);
  1397. $minl = min($minl, $pdf_pt['x']);
  1398. $maxr = max($maxr, $pdf_pt['x']);
  1399. $mint = min($mint, -$pdf_pt['y']);
  1400. $maxb = max($maxb, -$pdf_pt['y']);
  1401. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1402. }
  1403. break;
  1404. case 's': // bezier with first vertex equal first control
  1405. // mPDF 4.4.003
  1406. if (!($this->lastcommand == 'C' || $this->lastcommand == 'c' || $this->lastcommand == 'S' || $this->lastcommand == 's')) {
  1407. $this->lastcontrolpoints = array(0, 0);
  1408. }
  1409. for ($i = 0; $i < $ile_argumentow; $i += 4) {
  1410. $x1 = $this->lastcontrolpoints[0];
  1411. $y1 = $this->lastcontrolpoints[1];
  1412. $x2 = ($a[$i][0]);
  1413. $y2 = ($a[$i + 1][0]);
  1414. $x = ($a[$i + 2][0]);
  1415. $y = ($a[$i + 3][0]);
  1416. if ($relative) {
  1417. $pdfx1 = ($this->xbase + $x1);
  1418. $pdfy1 = ($this->ybase - $y1);
  1419. $pdfx2 = ($this->xbase + $x2);
  1420. $pdfy2 = ($this->ybase - $y2);
  1421. $pdfx = ($this->xbase + $x);
  1422. $pdfy = ($this->ybase - $y);
  1423. $this->xbase += $x;
  1424. $this->ybase += -$y;
  1425. } else {
  1426. $pdfx1 = $this->xbase + $x1;
  1427. $pdfy1 = $this->ybase - $y1;
  1428. $pdfx2 = $x2;
  1429. $pdfy2 = -$y2;
  1430. $pdfx = $x;
  1431. $pdfy = -$y;
  1432. $this->xbase = $x;
  1433. $this->ybase = -$y;
  1434. }
  1435. $this->lastcontrolpoints = array(($pdfx - $pdfx2), -($pdfy - $pdfy2)); // mPDF 4.4.003 always relative
  1436. $pdf_pt = $this->svg_overflow($pdfx, $pdfy);
  1437. $curves = array($pdfx1, -$pdfy1, $pdfx2, -$pdfy2, $pdfx, -$pdfy);
  1438. $bx = calc_bezier_bbox($start, $curves);
  1439. $minl = min($minl, $bx[0]);
  1440. $maxr = max($maxr, $bx[2]);
  1441. $mint = min($mint, $bx[1]);
  1442. $maxb = max($maxb, $bx[3]);
  1443. if (($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy)) {
  1444. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1445. } else {
  1446. $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);
  1447. }
  1448. }
  1449. break;
  1450. case 'c': // bezier with second vertex equal second control
  1451. for ($i = 0; $i < $ile_argumentow; $i += 6) {
  1452. $x1 = ($a[$i][0]);
  1453. $y1 = ($a[$i + 1][0]);
  1454. $x2 = ($a[$i + 2][0]);
  1455. $y2 = ($a[$i + 3][0]);
  1456. $x = ($a[$i + 4][0]);
  1457. $y = ($a[$i + 5][0]);
  1458. if ($relative) {
  1459. $pdfx1 = ($this->xbase + $x1);
  1460. $pdfy1 = ($this->ybase - $y1);
  1461. $pdfx2 = ($this->xbase + $x2);
  1462. $pdfy2 = ($this->ybase - $y2);
  1463. $pdfx = ($this->xbase + $x);
  1464. $pdfy = ($this->ybase - $y);
  1465. $this->xbase += $x;
  1466. $this->ybase += -$y;
  1467. } else {
  1468. $pdfx1 = $x1;
  1469. $pdfy1 = -$y1;
  1470. $pdfx2 = $x2;
  1471. $pdfy2 = -$y2;
  1472. $pdfx = $x;
  1473. $pdfy = -$y;
  1474. $this->xbase = $x;
  1475. $this->ybase = -$y;
  1476. }
  1477. $this->lastcontrolpoints = array(($pdfx - $pdfx2), -($pdfy - $pdfy2)); // mPDF 4.4.003 always relative
  1478. // $pdf_pt2 = $this->svg_overflow($pdfx2,$pdfy2);
  1479. // $pdf_pt1 = $this->svg_overflow($pdfx1,$pdfy1);
  1480. $pdf_pt = $this->svg_overflow($pdfx, $pdfy);
  1481. $curves = array($pdfx1, -$pdfy1, $pdfx2, -$pdfy2, $pdfx, -$pdfy);
  1482. $bx = calc_bezier_bbox($start, $curves);
  1483. $minl = min($minl, $bx[0]);
  1484. $maxr = max($maxr, $bx[2]);
  1485. $mint = min($mint, $bx[1]);
  1486. $maxb = max($maxb, $bx[3]);
  1487. if (($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy)) {
  1488. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1489. } else {
  1490. $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);
  1491. }
  1492. }
  1493. break;
  1494. case 'q': // bezier quadratic avec point de control
  1495. for ($i = 0; $i < $ile_argumentow; $i += 4) {
  1496. $x1 = ($a[$i][0]);
  1497. $y1 = ($a[$i + 1][0]);
  1498. $x = ($a[$i + 2][0]);
  1499. $y = ($a[$i + 3][0]);
  1500. if ($relative) {
  1501. $pdfx = ($this->xbase + $x);
  1502. $pdfy = ($this->ybase - $y);
  1503. $pdfx1 = ($this->xbase + ($x1 * 2 / 3));
  1504. $pdfy1 = ($this->ybase - ($y1 * 2 / 3));
  1505. // mPDF 4.4.003
  1506. $pdfx2 = $pdfx1 + 1 / 3 * ($x);
  1507. $pdfy2 = $pdfy1 + 1 / 3 * (-$y);
  1508. $this->xbase += $x;
  1509. $this->ybase += -$y;
  1510. } else {
  1511. $pdfx = $x;
  1512. $pdfy = -$y;
  1513. $pdfx1 = ($this->xbase + (($x1 - $this->xbase) * 2 / 3));
  1514. $pdfy1 = ($this->ybase - (($y1 + $this->ybase) * 2 / 3));
  1515. $pdfx2 = ($x + (($x1 - $x) * 2 / 3));
  1516. $pdfy2 = (-$y - (($y1 - $y) * 2 / 3));
  1517. // mPDF 4.4.003
  1518. $pdfx2 = $pdfx1 + 1 / 3 * ($x - $this->xbase);
  1519. $pdfy2 = $pdfy1 + 1 / 3 * (-$y - $this->ybase);
  1520. $this->xbase = $x;
  1521. $this->ybase = -$y;
  1522. }
  1523. $this->lastcontrolpoints = array(($pdfx - $pdfx2), -($pdfy - $pdfy2)); // mPDF 4.4.003 always relative
  1524. $pdf_pt = $this->svg_overflow($pdfx, $pdfy);
  1525. $curves = array($pdfx1, -$pdfy1, $pdfx2, -$pdfy2, $pdfx, -$pdfy);
  1526. $bx = calc_bezier_bbox($start, $curves);
  1527. $minl = min($minl, $bx[0]);
  1528. $maxr = max($maxr, $bx[2]);
  1529. $mint = min($mint, $bx[1]);
  1530. $maxb = max($maxb, $bx[3]);
  1531. if (($pdf_pt['x'] != $pdfx) || ($pdf_pt['y'] != $pdfy)) {
  1532. $path_cmd .= sprintf('%.3F %.3F l ', $pdf_pt['x'] * $this->kp, $pdf_pt['y'] * $this->kp);
  1533. } else {
  1534. $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);
  1535. }
  1536. }
  1537. break;
  1538. case 't': // bezier quadratic avec point de control simetrique a lancien point de control
  1539. if (!($this->lastcommand == 'Q' || $this->lastcommand == 'q' || $this->lastcommand == 'T' || $this->lastcommand == 't')) {
  1540. $this->lastcontrolpoints = array(0, 0);
  1541. }
  1542. for ($i = 0; $i < $ile_argumentow; $i += 2) {
  1543. $x = ($a[$i][0]);
  1544. $y = ($a[$i + 1][0]);
  1545. $x1 = $this->lastcontrolpoints[0];
  1546. $y1 = $this->lastcontrolpoints[1];
  1547. if ($relative) {
  1548. $pdfx = ($this->xbase + $x);
  1549. $pdfy = ($this->ybase - $y);
  1550. $pdfx1 = ($this->xbase + ($x1));
  1551. $pdfy1 = ($this->ybase - ($y1));
  1552. // mPDF 4.4.003
  1553. $pdfx2 = $pdfx1 + 1 / 3 * ($x);
  1554. $pdfy2 = $pdfy1 + 1 / 3 * (-$y);
  1555. $this->xbase += $x;
  1556. $this->ybase += -$y;
  1557. } else {
  1558. $pdfx = $x;
  1559. $pdfy = -$y;
  1560. $pdfx1 = ($this->xbase + ($x1));
  1561. $pdfy1 = ($this->ybase - ($y1));
  1562. // mPDF 4.4.003
  1563. $pdfx2 = $pdfx1 + 1 / 3 * ($x - $this->xbase);
  1564. $pdfy2 = $pdfy1 + 1 / 3 * (-$y - $this->ybase);
  1565. $this->xbase = $x;
  1566. $this->ybase = -$y;
  1567. }
  1568. $this->lastcontrolpoints = array(($pdfx - $pdfx2), -($pdfy - $pdfy2)); // mPDF 4.4.003 always relative
  1569. $curves = array($pdfx1, -$pdfy1, $pdfx2, -$pdfy2, $pdfx, -$pdfy);
  1570. $bx = calc_bezier_bbox($start, $curves);
  1571. $minl = min($minl, $bx[0]);
  1572. $maxr = max($maxr, $bx[2]);
  1573. $mint = min($mint, $bx[1]);
  1574. $maxb = max($maxb, $bx[3]);
  1575. $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);
  1576. }
  1577. break;
  1578. case 'a': // Elliptical arc
  1579. for ($i = 0; $i < $ile_argumentow; $i += 7) {
  1580. $rx = ($a[$i][0]);
  1581. $ry = ($a[$i + 1][0]);
  1582. $angle = ($a[$i + 2][0]); //x-axis-rotation
  1583. $largeArcFlag = ($a[$i + 3][0]);
  1584. $sweepFlag = ($a[$i + 4][0]);
  1585. $x2 = ($a[$i + 5][0]);
  1586. $y2 = ($a[$i + 6][0]);
  1587. $x1 = $this->xbase;
  1588. $y1 = -$this->ybase;
  1589. if ($relative) {
  1590. $x2 = $this->xbase + $x2;
  1591. $y2 = -$this->ybase + $y2;
  1592. $this->xbase += ($a[$i + 5][0]);
  1593. $this->ybase += -($a[$i + 6][0]);
  1594. } else {
  1595. $this->xbase = $x2;
  1596. $this->ybase = -$y2;
  1597. }
  1598. list($pcmd, $bounds) = $this->Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag);
  1599. $minl = min($minl, $x2, min($bounds[0]));
  1600. $maxr = max($maxr, $x2, max($bounds[0]));
  1601. $mint = min($mint, $y2, min($bounds[1]));
  1602. $maxb = max($maxb, $y2, max($bounds[1]));
  1603. $path_cmd .= $pcmd;
  1604. }
  1605. break;
  1606. case'z':
  1607. $path_cmd .= 'h ';
  1608. $this->subPathInit = true;
  1609. $newsubpath = true;
  1610. $this->xbase = $this->spxstart;
  1611. $this->ybase = $this->spystart;
  1612. break;
  1613. default:
  1614. break;
  1615. }
  1616. if (!$newsubpath) {
  1617. $this->subPathInit = false;
  1618. }
  1619. $this->lastcommand = $command;
  1620. // mPDF 5.0.039
  1621. $this->pathBBox[0] = $minl;
  1622. $this->pathBBox[1] = $mint;
  1623. $this->pathBBox[2] = $maxr - $this->pathBBox[0];
  1624. $this->pathBBox[3] = $maxb - $this->pathBBox[1];
  1625. return $path_cmd;
  1626. }
  1627. function Arcto($x1, $y1, $x2, $y2, $rx, $ry, $angle, $largeArcFlag, $sweepFlag)
  1628. {
  1629. $bounds = array(0 => array($x1, $x2), 1 => array($y1, $y2));
  1630. // 1. Treat out-of-range parameters as described in
  1631. // http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  1632. // If the endpoints (x1, y1) and (x2, y2) are identical, then this
  1633. // is equivalent to omitting the elliptical arc segment entirely
  1634. if ($x1 == $x2 && $y1 == $y2)
  1635. return array('', $bounds); // mPD 5.0.040
  1636. // If rX = 0 or rY = 0 then this arc is treated as a straight line
  1637. // segment (a "lineto") joining the endpoints.
  1638. if ($rx == 0.0 || $ry == 0.0) {
  1639. // return array(Lineto(x2, y2), $bounds);
  1640. }
  1641. // If rX or rY have negative signs, these are dropped; the absolute
  1642. // value is used instead.
  1643. if ($rx < 0.0)
  1644. $rx = -$rx;
  1645. if ($ry < 0.0)
  1646. $ry = -$ry;
  1647. // 2. convert to center parameterization as shown in
  1648. // http://www.w3.org/TR/SVG/implnote.html
  1649. $sinPhi = sin(deg2rad($angle));
  1650. $cosPhi = cos(deg2rad($angle));
  1651. $x1dash = $cosPhi * ($x1 - $x2) / 2.0 + $sinPhi * ($y1 - $y2) / 2.0;
  1652. $y1dash = -$sinPhi * ($x1 - $x2) / 2.0 + $cosPhi * ($y1 - $y2) / 2.0;
  1653. $numerator = $rx * $rx * $ry * $ry - $rx * $rx * $y1dash * $y1dash - $ry * $ry * $x1dash * $x1dash;
  1654. if ($numerator < 0.0) {
  1655. // If rX , rY and are such that there is no solution (basically,
  1656. // the ellipse is not big enough to reach from (x1, y1) to (x2,
  1657. // y2)) then the ellipse is scaled up uniformly until there is
  1658. // exactly one solution (until the ellipse is just big enough).
  1659. // -> find factor s, such that numerator' with rx'=s*rx and
  1660. // ry'=s*ry becomes 0 :
  1661. $s = sqrt(1.0 - $numerator / ($rx * $rx * $ry * $ry));
  1662. $rx *= $s;
  1663. $ry *= $s;
  1664. $root = 0.0;
  1665. } else {
  1666. $root = ($largeArcFlag == $sweepFlag ? -1.0 : 1.0) * sqrt($numerator / ($rx * $rx * $y1dash * $y1dash + $ry * $ry * $x1dash * $x1dash));
  1667. }
  1668. $cxdash = $root * $rx * $y1dash / $ry;
  1669. $cydash = -$root * $ry * $x1dash / $rx;
  1670. $cx = $cosPhi * $cxdash - $sinPhi * $cydash + ($x1 + $x2) / 2.0;
  1671. $cy = $sinPhi * $cxdash + $cosPhi * $cydash + ($y1 + $y2) / 2.0;
  1672. $theta1 = $this->CalcVectorAngle(1.0, 0.0, ($x1dash - $cxdash) / $rx, ($y1dash - $cydash) / $ry);
  1673. $dtheta = $this->CalcVectorAngle(($x1dash - $cxdash) / $rx, ($y1dash - $cydash) / $ry, (-$x1dash - $cxdash) / $rx, (-$y1dash - $cydash) / $ry);
  1674. if (!$sweepFlag && $dtheta > 0)
  1675. $dtheta -= 2.0 * M_PI;
  1676. else if ($sweepFlag && $dtheta < 0)
  1677. $dtheta += 2.0 * M_PI;
  1678. // 3. convert into cubic bezier segments <= 90deg
  1679. $segments = ceil(abs($dtheta / (M_PI / 2.0)));
  1680. $delta = $dtheta / $segments;
  1681. $t = 8.0 / 3.0 * sin($delta / 4.0) * sin($delta / 4.0) / sin($delta / 2.0);
  1682. $coords = array();
  1683. for ($i = 0; $i < $segments; $i++) {
  1684. $cosTheta1 = cos($theta1);
  1685. $sinTheta1 = sin($theta1);
  1686. $theta2 = $theta1 + $delta;
  1687. $cosTheta2 = cos($theta2);
  1688. $sinTheta2 = sin($theta2);
  1689. // a) calculate endpoint of the segment:
  1690. $xe = $cosPhi * $rx * $cosTheta2 - $sinPhi * $ry * $sinTheta2 + $cx;
  1691. $ye = $sinPhi * $rx * $cosTheta2 + $cosPhi * $ry * $sinTheta2 + $cy;
  1692. // b) calculate gradients at start/end points of segment:
  1693. $dx1 = $t * ( - $cosPhi * $rx * $sinTheta1 - $sinPhi * $ry * $cosTheta1);
  1694. $dy1 = $t * ( - $sinPhi * $rx * $sinTheta1 + $cosPhi * $ry * $cosTheta1);
  1695. $dxe = $t * ( $cosPhi * $rx * $sinTheta2 + $sinPhi * $ry * $cosTheta2);
  1696. $dye = $t * ( $sinPhi * $rx * $sinTheta2 - $cosPhi * $ry * $cosTheta2);
  1697. // c) draw the cubic bezier:
  1698. $coords[$i] = array(($x1 + $dx1), ($y1 + $dy1), ($xe + $dxe), ($ye + $dye), $xe, $ye);
  1699. // do next segment
  1700. $theta1 = $theta2;
  1701. $x1 = $xe;
  1702. $y1 = $ye;
  1703. }
  1704. $path = ' ';
  1705. foreach ($coords AS $c) {
  1706. $cpx1 = $c[0];
  1707. $cpy1 = $c[1];
  1708. $cpx2 = $c[2];
  1709. $cpy2 = $c[3];
  1710. $x2 = $c[4];
  1711. $y2 = $c[5];
  1712. $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";
  1713. // mPDF 5.0.040
  1714. $bounds[0][] = $c[4];
  1715. $bounds[1][] = $c[5];
  1716. }
  1717. return array($path, $bounds); // mPD 5.0.040
  1718. }
  1719. function CalcVectorAngle($ux, $uy, $vx, $vy)
  1720. {
  1721. $ta = atan2($uy, $ux);
  1722. $tb = atan2($vy, $vx);
  1723. if ($tb >= $ta)
  1724. return ($tb - $ta);
  1725. return (6.28318530718 - ($ta - $tb));
  1726. }
  1727. function ConvertSVGSizePixels($size = 5, $maxsize = 'x')
  1728. {
  1729. // maxsize in pixels (user units) or 'y' or 'x'
  1730. // e.g. $w = $this->ConvertSVGSizePixels($arguments['w'],$this->svg_info['w']*(25.4/$this->mpdf_ref->dpi));
  1731. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1732. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1733. // For text $maxsize = Fontsize
  1734. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1735. if ($maxsize == 'y') {
  1736. $maxsize = $this->svg_info['h'];
  1737. } else if ($maxsize == 'x') {
  1738. $maxsize = $this->svg_info['w'];
  1739. }
  1740. $maxsize *= (25.4 / $this->mpdf_ref->dpi); // convert pixels to mm
  1741. $fontsize = $this->mpdf_ref->FontSize / $this->kf;
  1742. //Return as pixels
  1743. $size = $this->mpdf_ref->ConvertSize($size, $maxsize, $fontsize, false) * 1 / (25.4 / $this->mpdf_ref->dpi);
  1744. return $size;
  1745. }
  1746. function ConvertSVGSizePts($size = 5)
  1747. {
  1748. // usefontsize - setfalse for e.g. margins - will ignore fontsize for % values
  1749. // Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
  1750. // For text $maxsize = Fontsize
  1751. // Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
  1752. $maxsize = $this->mpdf_ref->FontSize;
  1753. //Return as pts
  1754. $size = $this->mpdf_ref->ConvertSize($size, $maxsize, false, true) * 72 / 25.4;
  1755. return $size;
  1756. }
  1757. //
  1758. // fonction retracant les <rect />
  1759. function svgRect($arguments)
  1760. {
  1761. if ($arguments['h'] == 0 || $arguments['w'] == 0) {
  1762. return '';
  1763. }
  1764. $x = $this->ConvertSVGSizePixels($arguments['x'], 'x'); // mPDF 4.4.003
  1765. $y = $this->ConvertSVGSizePixels($arguments['y'], 'y'); // mPDF 4.4.003
  1766. $h = $this->ConvertSVGSizePixels($arguments['h'], 'y'); // mPDF 4.4.003
  1767. $w = $this->ConvertSVGSizePixels($arguments['w'], 'x'); // mPDF 4.4.003
  1768. $rx = $this->ConvertSVGSizePixels($arguments['rx'], 'x'); // mPDF 4.4.003
  1769. $ry = $this->ConvertSVGSizePixels($arguments['ry'], 'y'); // mPDF 4.4.003
  1770. if ($rx > $w / 2) {
  1771. $rx = $w / 2;
  1772. } // mPDF 4.4.003
  1773. if ($ry > $h / 2) {
  1774. $ry = $h / 2;
  1775. } // mPDF 4.4.003
  1776. if ($rx > 0 and $ry == 0) {
  1777. $ry = $rx;
  1778. }
  1779. if ($ry > 0 and $rx == 0) {
  1780. $rx = $ry;
  1781. }
  1782. if ($rx == 0 and $ry == 0) {
  1783. // trace un rectangle sans angle arrondit
  1784. $path_cmd = sprintf('%.3F %.3F m ', ($x * $this->kp), -($y * $this->kp));
  1785. $path_cmd .= sprintf('%.3F %.3F l ', (($x + $w) * $this->kp), -($y * $this->kp));
  1786. $path_cmd .= sprintf('%.3F %.3F l ', (($x + $w) * $this->kp), -(($y + $h) * $this->kp));
  1787. $path_cmd .= sprintf('%.3F %.3F l ', ($x) * $this->kp, -(($y + $h) * $this->kp));
  1788. $path_cmd .= sprintf('%.3F %.3F l h ', ($x * $this->kp), -($y * $this->kp));
  1789. } else {
  1790. // trace un rectangle avec les arrondit
  1791. // les points de controle du bezier sont deduis grace a la constante kappa
  1792. $kappa = 4 * (sqrt(2) - 1) / 3;
  1793. $kx = $kappa * $rx;
  1794. $ky = $kappa * $ry;
  1795. $path_cmd = sprintf('%.3F %.3F m ', ($x + $rx) * $this->kp, -$y * $this->kp);
  1796. $path_cmd .= sprintf('%.3F %.3F l ', ($x + ($w - $rx)) * $this->kp, -$y * $this->kp);
  1797. $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);
  1798. $path_cmd .= sprintf('%.3F %.3F l ', ($x + $w) * $this->kp, (-$y + (-$h + $ry)) * $this->kp);
  1799. $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);
  1800. $path_cmd .= sprintf('%.3F %.3F l ', ($x + $rx) * $this->kp, (-$y + (-$h)) * $this->kp);
  1801. $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);
  1802. $path_cmd .= sprintf('%.3F %.3F l ', $x * $this->kp, (-$y + (-$ry)) * $this->kp);
  1803. $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);
  1804. }
  1805. return $path_cmd;
  1806. }
  1807. //
  1808. // fonction retracant les <ellipse /> et <circle />
  1809. // le cercle est tracé grave a 4 bezier cubic, les poitn de controles
  1810. // sont deduis grace a la constante kappa * rayon
  1811. function svgEllipse($arguments)
  1812. {
  1813. if ($arguments['rx'] == 0 || $arguments['ry'] == 0) {
  1814. return '';
  1815. }
  1816. $kappa = 4 * (sqrt(2) - 1) / 3;
  1817. $cx = $this->ConvertSVGSizePixels($arguments['cx'], 'x');
  1818. $cy = $this->ConvertSVGSizePixels($arguments['cy'], 'y');
  1819. $rx = $this->ConvertSVGSizePixels($arguments['rx'], 'x');
  1820. $ry = $this->ConvertSVGSizePixels($arguments['ry'], 'y');
  1821. $x1 = $cx;
  1822. $y1 = -$cy + $ry;
  1823. $x2 = $cx + $rx;
  1824. $y2 = -$cy;
  1825. $x3 = $cx;
  1826. $y3 = -$cy - $ry;
  1827. $x4 = $cx - $rx;
  1828. $y4 = -$cy;
  1829. $path_cmd = sprintf('%.3F %.3F m ', $x1 * $this->kp, $y1 * $this->kp);
  1830. $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);
  1831. $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);
  1832. $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);
  1833. $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);
  1834. $path_cmd .= 'h ';
  1835. return $path_cmd;
  1836. }
  1837. //
  1838. // fonction retracant les <polyline /> et les <line />
  1839. function svgPolyline($arguments, $ispolyline = true)
  1840. {
  1841. if ($ispolyline) {
  1842. $xbase = $arguments[0];
  1843. $ybase = - $arguments[1];
  1844. } else {
  1845. if ($arguments[0] == $arguments[2] && $arguments[1] == $arguments[3]) {
  1846. return '';
  1847. } // Zero length line
  1848. $xbase = $this->ConvertSVGSizePixels($arguments[0], 'x');
  1849. $ybase = - $this->ConvertSVGSizePixels($arguments[1], 'y');
  1850. }
  1851. $path_cmd = sprintf('%.3F %.3F m ', $xbase * $this->kp, $ybase * $this->kp);
  1852. for ($i = 2; $i < count($arguments); $i += 2) {
  1853. if ($ispolyline) {
  1854. $tmp_x = $arguments[$i];
  1855. $tmp_y = - $arguments[($i + 1)];
  1856. } else {
  1857. $tmp_x = $this->ConvertSVGSizePixels($arguments[$i], 'x');
  1858. $tmp_y = - $this->ConvertSVGSizePixels($arguments[($i + 1)], 'y');
  1859. }
  1860. $path_cmd .= sprintf('%.3F %.3F l ', $tmp_x * $this->kp, $tmp_y * $this->kp);
  1861. }
  1862. // $path_cmd .= 'h '; // ?? In error - don't close subpath here
  1863. return $path_cmd;
  1864. }
  1865. //
  1866. // fonction retracant les <polygone />
  1867. function svgPolygon($arguments)
  1868. {
  1869. $xbase = $arguments[0];
  1870. $ybase = - $arguments[1];
  1871. $path_cmd = sprintf('%.3F %.3F m ', $xbase * $this->kp, $ybase * $this->kp);
  1872. for ($i = 2; $i < count($arguments); $i += 2) {
  1873. $tmp_x = $arguments[$i];
  1874. $tmp_y = - $arguments[($i + 1)];
  1875. $path_cmd .= sprintf('%.3F %.3F l ', $tmp_x * $this->kp, $tmp_y * $this->kp);
  1876. }
  1877. $path_cmd .= sprintf('%.3F %.3F l ', $xbase * $this->kp, $ybase * $this->kp);
  1878. $path_cmd .= 'h ';
  1879. return $path_cmd;
  1880. }
  1881. //
  1882. // write string to image
  1883. function svgText()
  1884. {
  1885. // $tmp = count($this->txt_style)-1;
  1886. $current_style = $this->txt_style[count($this->txt_style) - 1]; // mPDF 5.7.4
  1887. $style = '';
  1888. $op = '';
  1889. $render = -1;
  1890. if (isset($this->txt_data[2])) {
  1891. // mPDF 6
  1892. // If using SVG Font
  1893. if (isset($this->svg_font[$current_style['font-family']])) {
  1894. // select font
  1895. $style = 'R';
  1896. $style .= (isset($current_style['font-weight']) && $current_style['font-weight'] == 'bold') ? 'B' : '';
  1897. $style .= (isset($current_style['font-style']) && $current_style['font-style'] == 'italic') ? 'I' : '';
  1898. $style .= (isset($current_style['font-variant']) && $current_style['font-variant'] == 'small-caps') ? 'S' : '';
  1899. $fontsize = $current_style['font-size'] * $this->mpdf_ref->dpi / 72;
  1900. if (isset($this->svg_font[$current_style['font-family']][$style])) {
  1901. $svg_font = $this->svg_font[$current_style['font-family']][$style];
  1902. } else if (isset($this->svg_font[$current_style['font-family']]['R'])) {
  1903. $svg_font = $this->svg_font[$current_style['font-family']]['R'];
  1904. }
  1905. if (!isset($svg_font['units-per-em']) || $svg_font['units-per-em'] < 1) {
  1906. $svg_font['units-per-em'] = 1000;
  1907. }
  1908. $units_per_em = $svg_font['units-per-em'];
  1909. $scale = $fontsize / $units_per_em;
  1910. $stroke_width = $current_style['stroke-width'];
  1911. $stroke_width /= $scale;
  1912. $opacitystr = '';
  1913. $fopacity = 1;
  1914. if (isset($current_style['fill-opacity'])) {
  1915. if ($current_style['fill-opacity'] == 0) {
  1916. $fopacity = 0;
  1917. } else if ($current_style['fill-opacity'] > 1) {
  1918. $fopacity = 1;
  1919. } else if ($current_style['fill-opacity'] > 0) {
  1920. $fopacity = $current_style['fill-opacity'];
  1921. } else if ($current_style['fill-opacity'] < 0) {
  1922. $fopacity = 0;
  1923. }
  1924. }
  1925. $sopacity = 1;
  1926. if (isset($current_style['stroke-opacity'])) {
  1927. if ($current_style['stroke-opacity'] == 0) {
  1928. $sopacity = 0;
  1929. } else if ($current_style['stroke-opacity'] > 1) {
  1930. $sopacity = 1;
  1931. } else if ($current_style['stroke-opacity'] > 0) {
  1932. $sopacity = $current_style['stroke-opacity'];
  1933. } else if ($current_style['stroke-opacity'] < 0) {
  1934. $sopacity = 0;
  1935. }
  1936. }
  1937. $gs = $this->mpdf_ref->AddExtGState(array('ca' => $fopacity, 'CA' => $sopacity, 'BM' => '/Normal'));
  1938. $this->mpdf_ref->extgstates[$gs]['fo'] = true;
  1939. $opacitystr = sprintf(' /GS%d gs ', $gs);
  1940. $fillstr = '';
  1941. if (isset($current_style['fill']) && $current_style['fill'] != 'none') {
  1942. $col = $this->mpdf_ref->ConvertColor($current_style['fill']);
  1943. $fillstr = $this->mpdf_ref->SetFColor($col, true);
  1944. $render = "0"; // Fill (only)
  1945. $op = 'f';
  1946. }
  1947. $strokestr = '';
  1948. if ($stroke_width > 0 && $current_style['stroke'] != 'none') {
  1949. $scol = $this->mpdf_ref->ConvertColor($current_style['stroke']);
  1950. if ($scol) {
  1951. $strokestr .= $this->mpdf_ref->SetDColor($scol, true) . ' ';
  1952. }
  1953. $linewidth = $this->ConvertSVGSizePixels($stroke_width);
  1954. if ($linewidth > 0) {
  1955. $strokestr .= sprintf('%.3F w 0 J 0 j ', $linewidth * $this->kp);
  1956. if ($render == -1) {
  1957. $render = "1";
  1958. } // stroke only
  1959. else {
  1960. $render = "2";
  1961. } // fill and stroke
  1962. $op .= 'S';
  1963. }
  1964. }
  1965. if ($render == -1) {
  1966. return '';
  1967. }
  1968. if ($op == 'fS') {
  1969. $op = 'B';
  1970. }
  1971. $x = $this->txt_data[0]; // mPDF 5.7.4
  1972. $y = $this->txt_data[1]; // mPDF 5.7.4
  1973. $txt = $this->txt_data[2];
  1974. $txt = preg_replace('/\f/', '', $txt);
  1975. $txt = preg_replace('/\r/', '', $txt);
  1976. $txt = preg_replace('/\n/', ' ', $txt);
  1977. $txt = preg_replace('/\t/', ' ', $txt);
  1978. $txt = preg_replace("/[ ]+/u", ' ', $txt);
  1979. if ($this->textjuststarted) {
  1980. $txt = ltrim($txt);
  1981. } // mPDF 5.7.4
  1982. $this->textjuststarted = false; // mPDF 5.7.4
  1983. $txt = $this->mpdf_ref->purify_utf8_text($txt);
  1984. if ($this->mpdf_ref->text_input_as_HTML) {
  1985. $txt = $this->mpdf_ref->all_entities_to_utf8($txt);
  1986. }
  1987. $nb = mb_strlen($txt, 'UTF-8');
  1988. $i = 0;
  1989. $sw = 0;
  1990. $subpath_cmd = '';
  1991. while ($i < $nb) {
  1992. //Get next character
  1993. $char = mb_substr($txt, $i, 1, 'UTF-8');
  1994. if (isset($svg_font['glyphs'][$char])) {
  1995. $d = $svg_font['glyphs'][$char]['d'];
  1996. if (isset($svg_font['glyphs'][$char]['horiz-adv-x'])) {
  1997. $horiz_adv_x = $svg_font['glyphs'][$char]['horiz-adv-x'];
  1998. } else {
  1999. $horiz_adv_x = $svg_font['horiz-adv-x'];
  2000. } // missing glyph width
  2001. } else {
  2002. $d = $svg_font['d'];
  2003. $horiz_adv_x = $svg_font['horiz-adv-x']; // missing glyph width
  2004. }
  2005. preg_match_all('/([MZLHVCSQTAmzlhvcsqta])([eE ,\-.\d]+)*/', $d, $commands, PREG_SET_ORDER);
  2006. $subpath_cmd .= sprintf('q %.4F 0 0 %.4F mPDF-AXS(%.4F) %.4F cm ', $scale, -$scale, ($x + $sw * $scale) * $this->kp, -$y * $this->kp);
  2007. $this->subPathInit = true;
  2008. $this->pathBBox = array(999999, 999999, -999999, -999999);
  2009. foreach ($commands as $cmd) {
  2010. if (count($cmd) == 3 || (isset($cmd[2]) && $cmd[2] == '')) {
  2011. list($tmp, $command, $arguments) = $cmd;
  2012. } else {
  2013. list($tmp, $command) = $cmd;
  2014. $arguments = '';
  2015. }
  2016. $subpath_cmd .= $this->svgPath($command, $arguments);
  2017. }
  2018. $subpath_cmd .= $op . ' Q ';
  2019. if ($this->pathBBox[2] == -1999998) {
  2020. $this->pathBBox[2] = 100;
  2021. }
  2022. if ($this->pathBBox[3] == -1999998) {
  2023. $this->pathBBox[3] = 100;
  2024. }
  2025. if ($this->pathBBox[0] == 999999) {
  2026. $this->pathBBox[0] = 0;
  2027. }
  2028. if ($this->pathBBox[1] == 999999) {
  2029. $this->pathBBox[1] = 0;
  2030. }
  2031. $sw += $horiz_adv_x;
  2032. $i++;
  2033. }
  2034. $sw *= $scale; // convert stringwidth to units
  2035. // mPDF 5.7.4
  2036. $this->textlength = $sw;
  2037. $this->texttotallength += $this->textlength;
  2038. $path_cmd = sprintf('q %s %s Tr %s %s ', $opacitystr, $render, $fillstr, $strokestr);
  2039. $path_cmd .= $subpath_cmd;
  2040. $path_cmd .= 'Q ';
  2041. unset($this->txt_data[0], $this->txt_data[1], $this->txt_data[2]);
  2042. return $path_cmd;
  2043. }
  2044. // select font
  2045. $style .= ($current_style['font-weight'] == 'bold') ? 'B' : '';
  2046. $style .= ($current_style['font-style'] == 'italic') ? 'I' : '';
  2047. $size = $current_style['font-size'] * $this->kf;
  2048. $current_style['font-family'] = $this->mpdf_ref->SetFont($current_style['font-family'], $style, $size, false);
  2049. $this->mpdf_ref->CurrentFont['fo'] = true;
  2050. $opacitystr = '';
  2051. // mPDF 6
  2052. $fopacity = 1;
  2053. if (isset($current_style['fill-opacity'])) {
  2054. if ($current_style['fill-opacity'] == 0) {
  2055. $fopacity = 0;
  2056. } else if ($current_style['fill-opacity'] > 1) {
  2057. $fopacity = 1;
  2058. } else if ($current_style['fill-opacity'] > 0) {
  2059. $fopacity = $current_style['fill-opacity'];
  2060. } else if ($current_style['fill-opacity'] < 0) {
  2061. $fopacity = 0;
  2062. }
  2063. }
  2064. $sopacity = 1;
  2065. if (isset($current_style['stroke-opacity'])) {
  2066. if ($current_style['stroke-opacity'] == 0) {
  2067. $sopacity = 0;
  2068. } else if ($current_style['stroke-opacity'] > 1) {
  2069. $sopacity = 1;
  2070. } else if ($current_style['stroke-opacity'] > 0) {
  2071. $sopacity = $current_style['stroke-opacity'];
  2072. } else if ($current_style['stroke-opacity'] < 0) {
  2073. $sopacity = 0;
  2074. }
  2075. }
  2076. $gs = $this->mpdf_ref->AddExtGState(array('ca' => $fopacity, 'CA' => $sopacity, 'BM' => '/Normal'));
  2077. $this->mpdf_ref->extgstates[$gs]['fo'] = true;
  2078. $opacitystr = sprintf(' /GS%d gs ', $gs);
  2079. $fillstr = '';
  2080. if (isset($current_style['fill']) && $current_style['fill'] != 'none') {
  2081. $col = $this->mpdf_ref->ConvertColor($current_style['fill']);
  2082. $fillstr = $this->mpdf_ref->SetFColor($col, true);
  2083. $render = "0"; // Fill (only)
  2084. }
  2085. $strokestr = '';
  2086. if (isset($current_style['stroke-width']) && $current_style['stroke-width'] > 0 && $current_style['stroke'] != 'none') {
  2087. $scol = $this->mpdf_ref->ConvertColor($current_style['stroke']);
  2088. if ($scol) {
  2089. $strokestr .= $this->mpdf_ref->SetDColor($scol, true) . ' ';
  2090. }
  2091. $linewidth = $this->ConvertSVGSizePixels($current_style['stroke-width']);
  2092. if ($linewidth > 0) {
  2093. $strokestr .= sprintf('%.3F w 1 J 1 j ', $linewidth * $this->kp);
  2094. if ($render == -1) {
  2095. $render = "1";
  2096. } // stroke only
  2097. else {
  2098. $render = "2";
  2099. } // fill and stroke
  2100. }
  2101. }
  2102. if ($render == -1) {
  2103. return '';
  2104. }
  2105. $x = $this->txt_data[0]; // mPDF 5.7.4
  2106. $y = $this->txt_data[1]; // mPDF 5.7.4
  2107. $txt = $this->txt_data[2];
  2108. $txt = preg_replace('/\f/', '', $txt);
  2109. $txt = preg_replace('/\r/', '', $txt);
  2110. $txt = preg_replace('/\n/', ' ', $txt);
  2111. $txt = preg_replace('/\t/', ' ', $txt);
  2112. $txt = preg_replace("/[ ]+/u", ' ', $txt);
  2113. if ($this->textjuststarted) {
  2114. $txt = ltrim($txt);
  2115. } // mPDF 5.7.4
  2116. $this->textjuststarted = false; // mPDF 5.7.4
  2117. $txt = $this->mpdf_ref->purify_utf8_text($txt);
  2118. if ($this->mpdf_ref->text_input_as_HTML) {
  2119. $txt = $this->mpdf_ref->all_entities_to_utf8($txt);
  2120. }
  2121. if ($this->mpdf_ref->usingCoreFont) {
  2122. $txt = mb_convert_encoding($txt, $this->mpdf_ref->mb_enc, 'UTF-8');
  2123. }
  2124. if (preg_match("/([" . $this->mpdf_ref->pregRTLchars . "])/u", $txt)) {
  2125. $this->mpdf_ref->biDirectional = true;
  2126. }
  2127. $textvar = 0;
  2128. $save_OTLtags = $this->mpdf_ref->OTLtags;
  2129. $this->mpdf_ref->OTLtags = array();
  2130. if ($this->mpdf_ref->useKerning) {
  2131. if ($this->mpdf_ref->CurrentFont['haskernGPOS']) {
  2132. if (isset($this->mpdf_ref->OTLtags['Plus'])) {
  2133. $this->mpdf_ref->OTLtags['Plus'] .= ' kern';
  2134. } else {
  2135. $this->mpdf_ref->OTLtags['Plus'] = ' kern';
  2136. }
  2137. } else {
  2138. $textvar = ($textvar | FC_KERNING);
  2139. }
  2140. }
  2141. // Use OTL OpenType Table Layout - GSUB & GPOS
  2142. if (isset($this->mpdf_ref->CurrentFont['useOTL']) && $this->mpdf_ref->CurrentFont['useOTL']) {
  2143. $txt = $this->mpdf_ref->otl->applyOTL($txt, $this->mpdf_ref->CurrentFont['useOTL']);
  2144. $OTLdata = $this->mpdf_ref->otl->OTLdata;
  2145. }
  2146. $this->mpdf_ref->OTLtags = $save_OTLtags;
  2147. $this->mpdf_ref->magic_reverse_dir($txt, $this->mpdf_ref->directionality, $OTLdata);
  2148. $this->mpdf_ref->CurrentFont['used'] = true;
  2149. $sw = $this->mpdf_ref->GetStringWidth($txt, true, $OTLdata, $textvar); // also adds characters to subset
  2150. // mPDF 5.7.4
  2151. $this->textlength = $sw * 1 / (25.4 / $this->mpdf_ref->dpi);
  2152. $this->texttotallength += $this->textlength;
  2153. $pdfx = $x * $this->kp;
  2154. $pdfy = -$y * $this->kp;
  2155. $aixextra = sprintf(' /F%d %.3F Tf %s %s Tr %s %s ', $this->mpdf_ref->CurrentFont['i'], $this->mpdf_ref->FontSizePt, $opacitystr, $render, $fillstr, $strokestr);
  2156. $path_cmd = 'q 1 0 0 1 mPDF-AXS(0.00) 0 cm '; // Align X-shift
  2157. $path_cmd .= $this->mpdf_ref->Text($pdfx, $pdfy, $txt, $OTLdata, $textvar, $aixextra, 'SVG', true);
  2158. $path_cmd .= " Q\n";
  2159. unset($this->txt_data[0], $this->txt_data[1], $this->txt_data[2]);
  2160. if (isset($current_style['font-size-parent'])) {
  2161. $this->mpdf_ref->SetFontSize($current_style['font-size-parent']);
  2162. }
  2163. } else {
  2164. return ' ';
  2165. }
  2166. // Reset font // mPDF 5.7.4
  2167. $prev_style = $this->txt_style[count($this->txt_style) - 1];
  2168. $style = '';
  2169. $style .= ($prev_style['font-weight'] == 'bold') ? 'B' : '';
  2170. $style .= ($prev_style['font-style'] == 'italic') ? 'I' : '';
  2171. $size = $prev_style['font-size'] * $this->kf;
  2172. $this->mpdf_ref->SetFont($prev_style['font-family'], $style, $size, false);
  2173. return $path_cmd;
  2174. }
  2175. function svgDefineTxtStyle($critere_style)
  2176. {
  2177. // get copy of current/default txt style, and modify it with supplied attributes
  2178. $tmp = count($this->txt_style) - 1;
  2179. $current_style = $this->txt_style[$tmp];
  2180. if (isset($critere_style['style'])) {
  2181. if (preg_match('/fill:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/', $critere_style['style'], $m)) {
  2182. $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);
  2183. } else {
  2184. $tmp = preg_replace("/(.*)fill:\s*([a-z0-9#_()]*|none)(.*)/i", "$2", $critere_style['style']);
  2185. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2186. $current_style['fill'] = $tmp;
  2187. }
  2188. }
  2189. // mPDF 6
  2190. if (preg_match("/[^-]opacity:\s*([a-z0-9.]*|none)/i", $critere_style['style'], $m) ||
  2191. preg_match("/^opacity:\s*([a-z0-9.]*|none)/i", $critere_style['style'], $m)) {
  2192. $current_style['fill-opacity'] = $m[1];
  2193. $current_style['stroke-opacity'] = $m[1];
  2194. }
  2195. $tmp = preg_replace("/(.*)fill-opacity:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  2196. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2197. $current_style['fill-opacity'] = $tmp;
  2198. }
  2199. $tmp = preg_replace("/(.*)fill-rule:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  2200. if ($tmp != $critere_style['style'] && $tmp != $critere_style['style']) {
  2201. $current_style['fill-rule'] = $tmp;
  2202. }
  2203. if (preg_match('/stroke:\s*rgb\((\d+),\s*(\d+),\s*(\d+)\)/', $critere_style['style'], $m)) {
  2204. $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);
  2205. } else {
  2206. $tmp = preg_replace("/(.*)stroke:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  2207. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2208. $current_style['stroke'] = $tmp;
  2209. }
  2210. }
  2211. $tmp = preg_replace("/(.*)stroke-linecap:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  2212. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2213. $current_style['stroke-linecap'] = $tmp;
  2214. }
  2215. $tmp = preg_replace("/(.*)stroke-linejoin:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  2216. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2217. $current_style['stroke-linejoin'] = $tmp;
  2218. }
  2219. $tmp = preg_replace("/(.*)stroke-miterlimit:\s*([a-z0-9#]*|none)(.*)/i", "$2", $critere_style['style']);
  2220. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2221. $current_style['stroke-miterlimit'] = $tmp;
  2222. }
  2223. $tmp = preg_replace("/(.*)stroke-opacity:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  2224. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2225. $current_style['stroke-opacity'] = $tmp;
  2226. }
  2227. $tmp = preg_replace("/(.*)stroke-width:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  2228. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2229. $current_style['stroke-width'] = $tmp;
  2230. }
  2231. $tmp = preg_replace("/(.*)stroke-dasharray:\s*([a-z0-9., ]*|none)(.*)/i", "$2", $critere_style['style']);
  2232. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2233. $current_style['stroke-dasharray'] = $tmp;
  2234. }
  2235. $tmp = preg_replace("/(.*)stroke-dashoffset:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  2236. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2237. $current_style['stroke-dashoffset'] = $tmp;
  2238. }
  2239. $tmp = preg_replace("/(.*)font-family:\s*([a-z0-9.\"' ,\-]*|none)(.*)/i", "$2", $critere_style['style']);
  2240. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2241. $critere_style['font-family'] = $tmp;
  2242. }
  2243. $tmp = preg_replace("/(.*)font-size:\s*([a-z0-9.]*|none)(.*)/i", "$2", $critere_style['style']);
  2244. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2245. $critere_style['font-size'] = $tmp;
  2246. }
  2247. $tmp = preg_replace("/(.*)font-weight:\s*([a-z0-9.]*|normal)(.*)/i", "$2", $critere_style['style']);
  2248. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2249. $critere_style['font-weight'] = $tmp;
  2250. }
  2251. $tmp = preg_replace("/(.*)font-style:\s*([a-z0-9.]*|normal)(.*)/i", "$2", $critere_style['style']);
  2252. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2253. $critere_style['font-style'] = $tmp;
  2254. }
  2255. $tmp = preg_replace("/(.*)font-variant:\s*([a-z0-9.]*|normal)(.*)/i", "$2", $critere_style['style']);
  2256. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2257. $critere_style['font-variant'] = $tmp;
  2258. }
  2259. $tmp = preg_replace("/(.*)text-anchor:\s*(start|middle|end)(.*)/i", "$2", $critere_style['style']);
  2260. if ($tmp && $tmp != 'inherit' && $tmp != $critere_style['style']) {
  2261. $critere_style['text-anchor'] = $tmp;
  2262. }
  2263. }
  2264. if (isset($critere_style['font'])) {
  2265. // [ [ <'font-style'> || <'font-variant'> || <'font-weight'> ]?<'font-size'> [ / <'line-height'> ]? <'font-family'> ]
  2266. $tmp = preg_replace("/(.*)(italic|oblique)(.*)/i", "$2", $critere_style['font']);
  2267. if ($tmp != $critere_style['font']) {
  2268. if ($tmp == 'oblique') {
  2269. $tmp = 'italic';
  2270. }
  2271. $current_style['font-style'] = $tmp;
  2272. }
  2273. $tmp = preg_replace("/(.*)(bold|bolder)(.*)/i", "$2", $critere_style['font']);
  2274. if ($tmp != $critere_style['font']) {
  2275. if ($tmp == 'bolder') {
  2276. $tmp = 'bold';
  2277. }
  2278. $current_style['font-weight'] = $tmp;
  2279. }
  2280. $tmp = preg_replace("/(.*)(small\-caps)(.*)/i", "$2", $critere_style['font']);
  2281. if ($tmp != $critere_style['font']) {
  2282. $current_style['font-variant'] = $tmp;
  2283. }
  2284. // select digits not followed by percent sign nor preceeded by forward slash
  2285. $tmp = preg_replace("/(.*)\b(\d+)[\b|\/](.*)/i", "$2", $critere_style['font']);
  2286. if ($tmp != $critere_style['font']) {
  2287. $current_style['font-size'] = $this->ConvertSVGSizePts($tmp);
  2288. $this->mpdf_ref->SetFont('', '', $current_style['font-size'], false);
  2289. }
  2290. }
  2291. // mPDF 6
  2292. if (isset($critere_style['opacity']) && $critere_style['opacity'] != 'inherit') {
  2293. $current_style['fill-opacity'] = $critere_style['opacity'];
  2294. $current_style['stroke-opacity'] = $critere_style['opacity'];
  2295. }
  2296. // mPDF 6
  2297. if (isset($critere_style['stroke-opacity']) && $critere_style['stroke-opacity'] != 'inherit') {
  2298. $current_style['stroke-opacity'] = $critere_style['stroke-opacity'];
  2299. }
  2300. // mPDF 6
  2301. if (isset($critere_style['fill-opacity']) && $critere_style['fill-opacity'] != 'inherit') {
  2302. $current_style['fill-opacity'] = $critere_style['fill-opacity'];
  2303. }
  2304. if (isset($critere_style['fill']) && $critere_style['fill'] != 'inherit') {
  2305. $current_style['fill'] = $critere_style['fill'];
  2306. }
  2307. if (isset($critere_style['stroke']) && $critere_style['stroke'] != 'inherit') {
  2308. $current_style['stroke'] = $critere_style['stroke'];
  2309. }
  2310. if (isset($critere_style['stroke-width']) && $critere_style['stroke-width'] != 'inherit') {
  2311. $current_style['stroke-width'] = $critere_style['stroke-width'];
  2312. }
  2313. if (isset($critere_style['font-style']) && $critere_style['font-style'] != 'inherit') {
  2314. if (strtolower($critere_style['font-style']) == 'oblique') {
  2315. $critere_style['font-style'] = 'italic';
  2316. }
  2317. $current_style['font-style'] = $critere_style['font-style'];
  2318. }
  2319. if (isset($critere_style['font-weight']) && $critere_style['font-weight'] != 'inherit') {
  2320. if (strtolower($critere_style['font-weight']) == 'bolder') {
  2321. $critere_style['font-weight'] = 'bold';
  2322. }
  2323. $current_style['font-weight'] = $critere_style['font-weight'];
  2324. }
  2325. if (isset($critere_style['font-variant']) && $critere_style['font-variant'] != 'inherit') {
  2326. $current_style['font-variant'] = $critere_style['font-variant'];
  2327. }
  2328. if (isset($critere_style['font-size']) && $critere_style['font-size'] != 'inherit') {
  2329. if (strpos($critere_style['font-size'], '%') !== false) {
  2330. $current_style['font-size-parent'] = $current_style['font-size'];
  2331. }
  2332. $current_style['font-size'] = $this->ConvertSVGSizePts($critere_style['font-size']);
  2333. $this->mpdf_ref->SetFont('', '', $current_style['font-size'], false);
  2334. }
  2335. if (isset($critere_style['font-family']) && $critere_style['font-family'] != 'inherit') {
  2336. $v = $critere_style['font-family'];
  2337. $aux_fontlist = explode(",", $v);
  2338. $found = 0;
  2339. $svgfontstyle = 'R';
  2340. $svgfontstyle .= (isset($current_style['font-weight']) && $current_style['font-weight'] == 'bold') ? 'B' : '';
  2341. $svgfontstyle .= (isset($current_style['font-style']) && $current_style['font-style'] == 'italic') ? 'I' : '';
  2342. $svgfontstyle .= (isset($current_style['font-variant']) && $current_style['font-variant'] == 'small-caps') ? 'S' : '';
  2343. foreach ($aux_fontlist AS $f) {
  2344. $fonttype = trim($f);
  2345. $fonttype = preg_replace('/["\']*(.*?)["\']*/', '\\1', $fonttype);
  2346. $fonttype = preg_replace('/ /', '', $fonttype);
  2347. $v = strtolower(trim($fonttype));
  2348. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) {
  2349. $v = $this->mpdf_ref->fonttrans[$v];
  2350. }
  2351. if ((!$this->mpdf_ref->usingCoreFont && in_array($v, $this->mpdf_ref->available_unifonts)) ||
  2352. ($this->mpdf_ref->usingCoreFont && in_array($v, array('courier', 'times', 'helvetica', 'arial'))) ||
  2353. in_array($v, array('sjis', 'uhc', 'big5', 'gb')) || isset($this->svg_font[$v][$svgfontstyle])) { // mPDF 6
  2354. $current_style['font-family'] = $v;
  2355. $found = 1;
  2356. break;
  2357. }
  2358. }
  2359. if (!$found) {
  2360. foreach ($aux_fontlist AS $f) {
  2361. $fonttype = trim($f);
  2362. $fonttype = preg_replace('/["\']*(.*?)["\']*/', '\\1', $fonttype);
  2363. $fonttype = preg_replace('/ /', '', $fonttype);
  2364. $v = strtolower(trim($fonttype));
  2365. if (isset($this->mpdf_ref->fonttrans[$v]) && $this->mpdf_ref->fonttrans[$v]) {
  2366. $v = $this->mpdf_ref->fonttrans[$v];
  2367. }
  2368. 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) || isset($this->svg_font[$v][$svgfontstyle])) { // mPDF 6
  2369. $current_style['font-family'] = $v;
  2370. break;
  2371. }
  2372. }
  2373. }
  2374. }
  2375. if (isset($critere_style['text-anchor']) && $critere_style['text-anchor'] != 'inherit') {
  2376. $current_style['text-anchor'] = $critere_style['text-anchor'];
  2377. }
  2378. // add current style to text style array (will remove it later after writing text to svg_string)
  2379. array_push($this->txt_style, $current_style);
  2380. }
  2381. //
  2382. // fonction ajoutant un gradient
  2383. function svgAddGradient($id, $array_gradient)
  2384. {
  2385. $this->svg_gradient[$id] = $array_gradient;
  2386. }
  2387. //
  2388. // Ajoute une couleur dans le gradient correspondant
  2389. //
  2390. // function ecrivant dans le svgstring
  2391. function svgWriteString($content)
  2392. {
  2393. $this->svg_string .= $content;
  2394. }
  2395. // analise le svg et renvoie aux fonctions precedente our le traitement
  2396. function ImageSVG($data)
  2397. {
  2398. // Try to clean up the start of the file
  2399. // removing DOCTYPE fails with this:
  2400. /*
  2401. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd"
  2402. [
  2403. <!ELEMENT Paragraph (#PCDATA)>
  2404. ]>
  2405. */
  2406. //$data = preg_replace('/<!DOCTYPE.*? >/is', '', $data);
  2407. //$data = preg_replace('/<\?xml.*? >/is', '', $data);
  2408. $data = preg_replace('/^.*?<svg([> ])/is', '<svg\\1', $data); // mPDF 5.7.4
  2409. $data = preg_replace('/<!--.*?-->/is', '', $data); // mPDF 5.7.4
  2410. // Converts < to &lt; when not a tag
  2411. $data = preg_replace('/<([^!?\/a-zA-Z_:])/i', '&lt;\\1', $data); // mPDF 5.7.4
  2412. if (_SVG_AUTOFONT) {
  2413. $data = $this->markScriptToLang($data);
  2414. }
  2415. $this->svg_info = array();
  2416. $last_gradid = ''; // mPDF 6
  2417. $last_svg_fontid = ''; // mPDF 6
  2418. $last_svg_fontdefw = ''; // mPDF 6
  2419. $last_svg_fontstyle = ''; // mPDF 6
  2420. if (preg_match('/<!ENTITY/si', $data)) {
  2421. // Get User-defined entities
  2422. preg_match_all('/<!ENTITY\s+([a-z]+)\s+\"(.*?)\">/si', $data, $ent);
  2423. // Replace entities
  2424. for ($i = 0; $i < count($ent[0]); $i++) {
  2425. $data = preg_replace('/&' . preg_quote($ent[1][$i], '/') . ';/is', $ent[2][$i], $data);
  2426. }
  2427. }
  2428. if (preg_match('/xlink:href\s*=/si', $data)) {
  2429. // GRADIENTS
  2430. // Get links
  2431. preg_match_all('/(<(linearGradient|radialgradient)[^>]*)xlink:href\s*=\s*["\']#(.*?)["\'](.*?)\/>/si', $data, $links);
  2432. if (count($links[0])) {
  2433. $links[5] = array();
  2434. }
  2435. // Delete links from data - keeping in $links
  2436. for ($i = 0; $i < count($links[0]); $i++) {
  2437. $links[5][$i] = 'tmpLink' . RAND(100000, 9999999);
  2438. $data = preg_replace('/' . preg_quote($links[0][$i], '/') . '/is', '<MYLINKS' . $links[5][$i] . '>', $data);
  2439. }
  2440. // Get targets
  2441. preg_match_all('/<(linearGradient|radialgradient)([^>]*)id\s*=\s*["\'](.*?)["\'](.*?)>(.*?)<\/(linearGradient|radialgradient)>/si', $data, $m);
  2442. $targets = array();
  2443. $stops = array();
  2444. // keeping in $targets
  2445. for ($i = 0; $i < count($m[0]); $i++) {
  2446. $stops[$m[3][$i]] = $m[5][$i];
  2447. }
  2448. // Add back links this time as targets (gradients)
  2449. for ($i = 0; $i < count($links[0]); $i++) {
  2450. $def = $links[1][$i] . ' ' . $links[4][$i] . '>' . $stops[$links[3][$i]] . '</' . $links[2][$i] . '>';
  2451. $data = preg_replace('/<MYLINKS' . $links[5][$i] . '>/is', $def, $data);
  2452. }
  2453. // mPDF 5.7.4
  2454. // <TREF>
  2455. preg_match_all('/<tref ([^>]*)xlink:href\s*=\s*["\']#([^>]*?)["\']([^>]*)\/>/si', $data, $links);
  2456. for ($i = 0; $i < count($links[0]); $i++) {
  2457. // Get the item to use from defs
  2458. $insert = '';
  2459. if (preg_match('/<text [^>]*id\s*=\s*["\']' . $links[2][$i] . '["\'][^>]*>(.*?)<\/text>/si', $data, $m)) {
  2460. $insert = $m[1];
  2461. }
  2462. if ($insert) {
  2463. $data = preg_replace('/' . preg_quote($links[0][$i], '/') . '/is', $insert, $data);
  2464. }
  2465. }
  2466. // mPDF 5.7.2
  2467. // <USE>
  2468. preg_match_all('/<use ([^>]*)xlink:href\s*=\s*["\']#([^>]*?)["\']([^>]*)\/>/si', $data, $links);
  2469. for ($i = 0; $i < count($links[0]); $i++) {
  2470. // Get the item to use from defs
  2471. $insert = '';
  2472. if (preg_match('/<([a-zA-Z]*) [^>]*id\s*=\s*["\']' . $links[2][$i] . '["\'][^>]*\/>/si', $data, $m)) {
  2473. $insert = $m[0];
  2474. }
  2475. if (!$insert && preg_match('/<([a-zA-Z]*) [^>]*id\s*=\s*["\']' . $links[2][$i] . '["\']/si', $data, $m)) {
  2476. if (preg_match('/<' . $m[1] . '[^>]*id\s*=\s*["\']' . $links[2][$i] . '["\'][^>]*>.*?<\/' . $m[1] . '>/si', $data, $m)) {
  2477. $insert = $m[0];
  2478. }
  2479. }
  2480. if ($insert) {
  2481. $inners = $links[1][$i] . ' ' . $links[3][$i];
  2482. // Change x,y coords to translate()
  2483. if (preg_match('/y\s*=\s*["\']([^>]*?)["\']/', $inners, $m)) {
  2484. $y = $m[1];
  2485. } else {
  2486. $y = 0;
  2487. }
  2488. if (preg_match('/x\s*=\s*["\']([^>]*?)["\']/', $inners, $m)) {
  2489. $x = $m[1];
  2490. } else {
  2491. $x = 0;
  2492. }
  2493. if ($x || $y) {
  2494. $inners = preg_replace('/(y|x)\s*=\s*["\']([^>]*?)["\']/', '', $inners);
  2495. if (preg_match('/transform\s*=\s*["\']([^>]*?)["\']/', $inners, $m)) {
  2496. if (preg_match('/translate\(\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*\)/', $m[1], $mm)) {
  2497. $transform = $m[1]; // transform="...."
  2498. $x += $mm[1];
  2499. $y += $mm[2];
  2500. $transform = preg_replace('/' . preg_quote($mm[0], '/') . '/', '', $transform);
  2501. $transform = 'transform="' . $transform . ' translate(' . $x . ', ' . $y . ')"';
  2502. $inners = preg_replace('/' . preg_quote($m[0], '/') . '/is', $transform, $inners);
  2503. } else {
  2504. $inners = preg_replace('/' . preg_quote($m[0], '/') . '/is', 'transform="' . $m[1] . ' translate(' . $x . ', ' . $y . ')"', $inners);
  2505. }
  2506. } else {
  2507. $inners .= ' transform="translate(' . $x . ', ' . $y . ')"';
  2508. }
  2509. }
  2510. }
  2511. $replacement = '<g ' . $inners . '>' . $insert . '</g>';
  2512. $data = preg_replace('/' . preg_quote($links[0][$i], '/') . '/is', $replacement, $data);
  2513. }
  2514. preg_match_all('/<use ([^>]*)xlink:href\s*=\s*["\']#([^>]*?)["\']([^>]*)>\s*<\/use>/si', $data, $links);
  2515. for ($i = 0; $i < count($links[0]); $i++) {
  2516. // Get the item to use from defs
  2517. $insert = '';
  2518. if (preg_match('/<([a-zA-Z]*) [^>]*id\s*=\s*["\']' . $links[2][$i] . '["\'][^>]*\/>/si', $data, $m)) {
  2519. $insert = $m[0];
  2520. }
  2521. if (!$insert && preg_match('/<([a-zA-Z]*) [^>]*id\s*=\s*["\']' . $links[2][$i] . '["\']/si', $data, $m)) {
  2522. if (preg_match('/<' . $m[1] . '[^>]*id\s*=\s*["\']' . $links[2][$i] . '["\'][^>]*>.*?<\/' . $m[1] . '>/si', $data, $m)) {
  2523. $insert = $m[0];
  2524. }
  2525. }
  2526. if ($insert) {
  2527. $inners = $links[1][$i] . ' ' . $links[3][$i];
  2528. // Change x,y coords to translate()
  2529. if (preg_match('/y\s*=\s*["\']([^>]*?)["\']/', $inners, $m)) {
  2530. $y = $m[1];
  2531. } else {
  2532. $y = 0;
  2533. }
  2534. if (preg_match('/x\s*=\s*["\']([^>]*?)["\']/', $inners, $m)) {
  2535. $x = $m[1];
  2536. } else {
  2537. $x = 0;
  2538. }
  2539. if ($x || $y) {
  2540. $inners = preg_replace('/(y|x)\s*=\s*["\']([^>]*?)["\']/', '', $inners);
  2541. if (preg_match('/transform\s*=\s*["\']([^>]*?)["\']/', $inners, $m)) {
  2542. if (preg_match('/translate\(\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*\)/', $m[1], $mm)) {
  2543. $transform = $m[1]; // transform="...."
  2544. $x += $mm[1];
  2545. $y += $mm[2];
  2546. $transform = preg_replace('/' . preg_quote($mm[0], '/') . '/', '', $transform);
  2547. $transform = 'transform="' . $transform . ' translate(' . $x . ', ' . $y . ')"';
  2548. $inners = preg_replace('/' . preg_quote($m[0], '/') . '/is', $transform, $inners);
  2549. } else {
  2550. $inners = preg_replace('/' . preg_quote($m[0], '/') . '/is', 'transform="' . $m[1] . ' translate(' . $x . ', ' . $y . ')"', $inners);
  2551. }
  2552. } else {
  2553. $inners .= ' transform="translate(' . $x . ', ' . $y . ')"';
  2554. }
  2555. }
  2556. $replacement = '<g ' . $inners . '>' . $insert . '</g>';
  2557. $data = preg_replace('/' . preg_quote($links[0][$i], '/') . '/is', $replacement, $data);
  2558. }
  2559. }
  2560. }
  2561. // Removes <pattern>
  2562. $data = preg_replace('/<pattern.*?<\/pattern>/is', '', $data);
  2563. // Removes <marker>
  2564. $data = preg_replace('/<marker.*?<\/marker>/is', '', $data);
  2565. $this->svg_info['data'] = $data;
  2566. $this->svg_string = '';
  2567. //
  2568. // chargement unique des fonctions
  2569. if (!function_exists("xml_svg2pdf_start")) {
  2570. function xml_svg2pdf_start($parser, $name, $attribs)
  2571. {
  2572. //
  2573. // definition
  2574. global $svg_class, $last_gradid, $last_svg_fontid, $last_svg_fontdefw, $last_svg_fontstyle; // mPDF 6
  2575. // mPDF 6
  2576. if (strtolower($name) == 'font') {
  2577. $last_svg_fontid = '';
  2578. if (isset($attribs['horiz-adv-x']) && $attribs['horiz-adv-x']) {
  2579. $last_svg_fontdefw = $attribs['horiz-adv-x'];
  2580. }
  2581. return;
  2582. }
  2583. // mPDF 6
  2584. else if (strtolower($name) == 'font-face') {
  2585. $last_svg_fontstyle = 'R';
  2586. $last_svg_fontstyle .= (isset($attribs['font-weight']) && $attribs['font-weight'] == 'bold') ? 'B' : '';
  2587. $last_svg_fontstyle .= (isset($attribs['font-style']) && $attribs['font-style'] == 'italic') ? 'I' : '';
  2588. $last_svg_fontstyle .= (isset($attribs['font-variant']) && $attribs['font-variant'] == 'small-caps') ? 'S' : '';
  2589. if (isset($attribs['font-family']) && $attribs['font-family']) {
  2590. $tmp_svg_font = array(
  2591. 'units-per-em' => (isset($attribs['units-per-em']) ? $attribs['units-per-em'] : ''),
  2592. 'd' => '',
  2593. 'glyphs' => array()
  2594. );
  2595. $last_svg_fontid = strtolower($attribs['font-family']);
  2596. if ($last_svg_fontdefw) {
  2597. $tmp_svg_font['horiz-adv-x'] = $last_svg_fontdefw;
  2598. } else {
  2599. $tmp_svg_font['horiz-adv-x'] = 500;
  2600. }
  2601. $svg_class->svg_font[$last_svg_fontid][$last_svg_fontstyle] = $tmp_svg_font;
  2602. }
  2603. return;
  2604. }
  2605. // mPDF 6
  2606. else if (strtolower($name) == 'missing-glyph') {
  2607. if ($last_svg_fontid && isset($attribs['horiz-adv-x'])) {
  2608. $svg_class->svg_font[$last_svg_fontid][$last_svg_fontstyle]['horiz-adv-x'] = (isset($attribs['horiz-adv-x']) ? $attribs['horiz-adv-x'] : '');
  2609. $svg_class->svg_font[$last_svg_fontid][$last_svg_fontstyle]['d'] = (isset($attribs['d']) ? $attribs['d'] : '');
  2610. }
  2611. return;
  2612. }
  2613. // mPDF 6
  2614. else if (strtolower($name) == 'glyph') {
  2615. if ($last_svg_fontid && isset($attribs['unicode'])) {
  2616. $svg_class->svg_font[$last_svg_fontid][$last_svg_fontstyle]['glyphs'][$attribs['unicode']] = array(
  2617. 'horiz-adv-x' => (isset($attribs['horiz-adv-x']) ? $attribs['horiz-adv-x'] : $last_svg_fontdefw),
  2618. 'd' => (isset($attribs['d']) ? $attribs['d'] : ''),
  2619. );
  2620. }
  2621. return;
  2622. }
  2623. // mPDF 5.7.2
  2624. else if (strtolower($name) == 'lineargradient') {
  2625. $tmp_gradient = array(
  2626. 'type' => 'linear',
  2627. 'transform' => (isset($attribs['gradientTransform']) ? $attribs['gradientTransform'] : ''),
  2628. 'units' => (isset($attribs['gradientUnits']) ? $attribs['gradientUnits'] : ''),
  2629. 'spread' => (isset($attribs['spreadMethod']) ? $attribs['spreadMethod'] : ''),
  2630. 'color' => array()
  2631. );
  2632. if (isset($attribs['x1']))
  2633. $tmp_gradient['info']['x1'] = $attribs['x1'];
  2634. if (isset($attribs['y1']))
  2635. $tmp_gradient['info']['y1'] = $attribs['y1'];
  2636. if (isset($attribs['x2']))
  2637. $tmp_gradient['info']['x2'] = $attribs['x2'];
  2638. if (isset($attribs['y2']))
  2639. $tmp_gradient['info']['y2'] = $attribs['y2'];
  2640. $last_gradid = $attribs['id'];
  2641. $svg_class->svgAddGradient($attribs['id'], $tmp_gradient);
  2642. return;
  2643. }
  2644. else if (strtolower($name) == 'radialgradient') {
  2645. $tmp_gradient = array(
  2646. 'type' => 'radial',
  2647. 'transform' => (isset($attribs['gradientTransform']) ? $attribs['gradientTransform'] : ''),
  2648. 'units' => (isset($attribs['gradientUnits']) ? $attribs['gradientUnits'] : ''),
  2649. 'spread' => (isset($attribs['spreadMethod']) ? $attribs['spreadMethod'] : ''),
  2650. 'color' => array()
  2651. );
  2652. if (isset($attribs['cx']))
  2653. $tmp_gradient['info']['x0'] = $attribs['cx'];
  2654. if (isset($attribs['cy']))
  2655. $tmp_gradient['info']['y0'] = $attribs['cy'];
  2656. if (isset($attribs['fx']))
  2657. $tmp_gradient['info']['x1'] = $attribs['fx'];
  2658. if (isset($attribs['fy']))
  2659. $tmp_gradient['info']['y1'] = $attribs['fy'];
  2660. if (isset($attribs['r']))
  2661. $tmp_gradient['info']['r'] = $attribs['r'];
  2662. $last_gradid = $attribs['id'];
  2663. $svg_class->svgAddGradient($attribs['id'], $tmp_gradient);
  2664. return;
  2665. }
  2666. else if (strtolower($name) == 'stop') {
  2667. if (!$last_gradid)
  2668. return;
  2669. $color = '#000000';
  2670. if (isset($attribs['style']) AND preg_match('/stop-color:\s*([^;]*)/i', $attribs['style'], $m)) {
  2671. $color = trim($m[1]);
  2672. } else if (isset($attribs['stop-color']) && $attribs['stop-color']) {
  2673. $color = $attribs['stop-color'];
  2674. }
  2675. $col = $svg_class->mpdf_ref->ConvertColor($color);
  2676. if (!$col) {
  2677. $col = $svg_class->mpdf_ref->ConvertColor('#000000');
  2678. } // In case "transparent" or "inherit" returned
  2679. if ($col{0} == 3 || $col{0} == 5) { // RGB
  2680. $color_final = sprintf('%.3F %.3F %.3F', ord($col{1}) / 255, ord($col{2}) / 255, ord($col{3}) / 255);
  2681. $svg_class->svg_gradient[$last_gradid]['colorspace'] = 'RGB';
  2682. } else if ($col{0} == 4 || $col{0} == 6) { // CMYK
  2683. $color_final = sprintf('%.3F %.3F %.3F %.3F', ord($col{1}) / 100, ord($col{2}) / 100, ord($col{3}) / 100, ord($col{4}) / 100);
  2684. $svg_class->svg_gradient[$last_gradid]['colorspace'] = 'CMYK';
  2685. } else if ($col{0} == 1) { // Grayscale
  2686. $color_final = sprintf('%.3F', ord($col{1}) / 255);
  2687. $svg_class->svg_gradient[$last_gradid]['colorspace'] = 'Gray';
  2688. }
  2689. $stop_opacity = 1;
  2690. if (isset($attribs['style']) AND preg_match('/stop-opacity:\s*([0-9.]*)/i', $attribs['style'], $m)) {
  2691. $stop_opacity = $m[1];
  2692. } else if (isset($attribs['stop-opacity'])) {
  2693. $stop_opacity = $attribs['stop-opacity'];
  2694. } else if ($col{0} == 5) { // RGBa
  2695. $stop_opacity = ord($col{4} / 100);
  2696. } else if ($col{0} == 6) { // CMYKa
  2697. $stop_opacity = ord($col{5} / 100);
  2698. }
  2699. $tmp_color = array(
  2700. 'color' => $color_final,
  2701. 'offset' => (isset($attribs['offset']) ? $attribs['offset'] : ''),
  2702. 'opacity' => $stop_opacity
  2703. );
  2704. array_push($svg_class->svg_gradient[$last_gradid]['color'], $tmp_color);
  2705. return;
  2706. }
  2707. if ($svg_class->inDefs) {
  2708. return;
  2709. }
  2710. $svg_class->xbase = 0;
  2711. $svg_class->ybase = 0;
  2712. switch (strtolower($name)) {
  2713. // Don't output stuff inside <defs>
  2714. case 'defs':
  2715. $svg_class->inDefs = true;
  2716. return;
  2717. case 'svg':
  2718. $svg_class->svgOffset($attribs);
  2719. break;
  2720. case 'path':
  2721. $path = $attribs['d'];
  2722. preg_match_all('/([MZLHVCSQTAmzlhvcsqta])([eE ,\-.\d]+)*/', $path, $commands, PREG_SET_ORDER);
  2723. $path_cmd = '';
  2724. $svg_class->subPathInit = true;
  2725. $svg_class->pathBBox = array(999999, 999999, -999999, -999999);
  2726. foreach ($commands as $c) {
  2727. if ((isset($c) && count($c) == 3) || (isset($c[2]) && $c[2] == '')) {
  2728. list($tmp, $command, $arguments) = $c;
  2729. } else {
  2730. list($tmp, $command) = $c;
  2731. $arguments = '';
  2732. }
  2733. $path_cmd .= $svg_class->svgPath($command, $arguments);
  2734. }
  2735. if ($svg_class->pathBBox[2] == -1999998) {
  2736. $svg_class->pathBBox[2] = 100;
  2737. }
  2738. if ($svg_class->pathBBox[3] == -1999998) {
  2739. $svg_class->pathBBox[3] = 100;
  2740. }
  2741. if ($svg_class->pathBBox[0] == 999999) {
  2742. $svg_class->pathBBox[0] = 0;
  2743. }
  2744. if ($svg_class->pathBBox[1] == 999999) {
  2745. $svg_class->pathBBox[1] = 0;
  2746. }
  2747. $critere_style = $attribs;
  2748. unset($critere_style['d']);
  2749. $path_style = $svg_class->svgDefineStyle($critere_style);
  2750. break;
  2751. case 'rect':
  2752. if (!isset($attribs['x'])) {
  2753. $attribs['x'] = 0;
  2754. }
  2755. if (!isset($attribs['y'])) {
  2756. $attribs['y'] = 0;
  2757. }
  2758. if (!isset($attribs['rx'])) {
  2759. $attribs['rx'] = 0;
  2760. }
  2761. if (!isset($attribs['ry'])) {
  2762. $attribs['ry'] = 0;
  2763. }
  2764. $arguments = array();
  2765. if (isset($attribs['x']))
  2766. $arguments['x'] = $attribs['x'];
  2767. if (isset($attribs['y']))
  2768. $arguments['y'] = $attribs['y'];
  2769. if (isset($attribs['width']))
  2770. $arguments['w'] = $attribs['width'];
  2771. if (isset($attribs['height']))
  2772. $arguments['h'] = $attribs['height'];
  2773. if (isset($attribs['rx']))
  2774. $arguments['rx'] = $attribs['rx'];
  2775. if (isset($attribs['ry']))
  2776. $arguments['ry'] = $attribs['ry'];
  2777. $path_cmd = $svg_class->svgRect($arguments);
  2778. $critere_style = $attribs;
  2779. unset($critere_style['x'], $critere_style['y'], $critere_style['rx'], $critere_style['ry'], $critere_style['height'], $critere_style['width']);
  2780. $path_style = $svg_class->svgDefineStyle($critere_style);
  2781. break;
  2782. case 'circle':
  2783. if (!isset($attribs['cx'])) {
  2784. $attribs['cx'] = 0;
  2785. }
  2786. if (!isset($attribs['cy'])) {
  2787. $attribs['cy'] = 0;
  2788. }
  2789. $arguments = array();
  2790. if (isset($attribs['cx']))
  2791. $arguments['cx'] = $attribs['cx'];
  2792. if (isset($attribs['cy']))
  2793. $arguments['cy'] = $attribs['cy'];
  2794. if (isset($attribs['r']))
  2795. $arguments['rx'] = $attribs['r'];
  2796. if (isset($attribs['r']))
  2797. $arguments['ry'] = $attribs['r'];
  2798. $path_cmd = $svg_class->svgEllipse($arguments);
  2799. $critere_style = $attribs;
  2800. unset($critere_style['cx'], $critere_style['cy'], $critere_style['r']);
  2801. $path_style = $svg_class->svgDefineStyle($critere_style);
  2802. break;
  2803. case 'ellipse':
  2804. if (!isset($attribs['cx'])) {
  2805. $attribs['cx'] = 0;
  2806. }
  2807. if (!isset($attribs['cy'])) {
  2808. $attribs['cy'] = 0;
  2809. }
  2810. $arguments = array();
  2811. if (isset($attribs['cx']))
  2812. $arguments['cx'] = $attribs['cx'];
  2813. if (isset($attribs['cy']))
  2814. $arguments['cy'] = $attribs['cy'];
  2815. if (isset($attribs['rx']))
  2816. $arguments['rx'] = $attribs['rx'];
  2817. if (isset($attribs['ry']))
  2818. $arguments['ry'] = $attribs['ry'];
  2819. $path_cmd = $svg_class->svgEllipse($arguments);
  2820. $critere_style = $attribs;
  2821. unset($critere_style['cx'], $critere_style['cy'], $critere_style['rx'], $critere_style['ry']);
  2822. $path_style = $svg_class->svgDefineStyle($critere_style);
  2823. break;
  2824. case 'line':
  2825. $arguments = array();
  2826. $arguments[0] = (isset($attribs['x1']) ? $attribs['x1'] : '');
  2827. $arguments[1] = (isset($attribs['y1']) ? $attribs['y1'] : '');
  2828. $arguments[2] = (isset($attribs['x2']) ? $attribs['x2'] : '');
  2829. $arguments[3] = (isset($attribs['y2']) ? $attribs['y2'] : '');
  2830. $path_cmd = $svg_class->svgPolyline($arguments, false);
  2831. $critere_style = $attribs;
  2832. unset($critere_style['x1'], $critere_style['y1'], $critere_style['x2'], $critere_style['y2']);
  2833. $path_style = $svg_class->svgDefineStyle($critere_style);
  2834. break;
  2835. case 'polyline':
  2836. $path = $attribs['points'];
  2837. preg_match_all('/[0-9\-\.]*/', $path, $tmp, PREG_SET_ORDER);
  2838. $arguments = array();
  2839. for ($i = 0; $i < count($tmp); $i++) {
  2840. if ($tmp[$i][0] != '') {
  2841. array_push($arguments, $tmp[$i][0]);
  2842. }
  2843. }
  2844. $path_cmd = $svg_class->svgPolyline($arguments);
  2845. $critere_style = $attribs;
  2846. unset($critere_style['points']);
  2847. $path_style = $svg_class->svgDefineStyle($critere_style);
  2848. break;
  2849. case 'polygon':
  2850. $path = $attribs['points'];
  2851. preg_match_all('/([\-]*[0-9\.]+)/', $path, $tmp);
  2852. $arguments = array();
  2853. for ($i = 0; $i < count($tmp[0]); $i++) {
  2854. if ($tmp[0][$i] != '') {
  2855. array_push($arguments, $tmp[0][$i]);
  2856. }
  2857. }
  2858. $path_cmd = $svg_class->svgPolygon($arguments);
  2859. // definition du style de la forme:
  2860. $critere_style = $attribs;
  2861. unset($critere_style['points']);
  2862. $path_style = $svg_class->svgDefineStyle($critere_style);
  2863. break;
  2864. // mPDF 5.7.4 Embedded image
  2865. case 'image':
  2866. if (isset($attribs['xlink:href']) && $attribs['xlink:href']) {
  2867. $svg_class->svgImage($attribs);
  2868. }
  2869. break;
  2870. case 'a':
  2871. if (isset($attribs['xlink:href'])) {
  2872. unset($attribs['xlink:href']); // this should be a hyperlink
  2873. // not handled like a xlink:href in other elements
  2874. } // then continue like a <g>
  2875. case 'g':
  2876. $array_style = $svg_class->svgDefineStyle($attribs);
  2877. if (!empty($array_style['transformations'])) {
  2878. // If in the middle of <text> element, add to textoutput, else WriteString
  2879. if ($svg_class->intext) {
  2880. $svg_class->textoutput .= ' q ' . $array_style['transformations'];
  2881. } // mPDF 5.7.4
  2882. else {
  2883. $svg_class->svgWriteString(' q ' . $array_style['transformations']);
  2884. }
  2885. }
  2886. array_push($svg_class->svg_style, $array_style);
  2887. $svg_class->svgDefineTxtStyle($attribs);
  2888. break;
  2889. case 'text':
  2890. $svg_class->textlength = 0; // mPDF 5.7.4
  2891. $svg_class->texttotallength = 0; // mPDF 5.7.4
  2892. $svg_class->textoutput = ''; // mPDF 5.7.4
  2893. $svg_class->textanchor = 'start'; // mPDF 5.7.4
  2894. $svg_class->textXorigin = 0; // mPDF 5.7.4
  2895. $svg_class->textYorigin = 0; // mPDF 5.7.4
  2896. $svg_class->intext = true; // mPDF 5.7.4
  2897. $styl = '';
  2898. if (_SVG_CLASSES && isset($attribs['class']) && $attribs['class']) {
  2899. $classes = preg_split('/\s+/', trim($attribs['class']));
  2900. foreach ($classes AS $class) {
  2901. if (isset($svg_class->mpdf_ref->cssmgr->CSS['CLASS>>' . strtoupper($class)])) {
  2902. $c = $svg_class->mpdf_ref->cssmgr->CSS['CLASS>>' . strtoupper($class)];
  2903. foreach ($c AS $prop => $val) {
  2904. $styl .= strtolower($prop) . ':' . $val . ';';
  2905. }
  2906. }
  2907. }
  2908. }
  2909. if (_SVG_AUTOFONT && isset($attribs['lang']) && $attribs['lang']) {
  2910. if (!$svg_class->mpdf_ref->usingCoreFont) {
  2911. if ($attribs['lang'] != $svg_class->mpdf_ref->default_lang) {
  2912. list ($coreSuitable, $mpdf_unifont) = GetLangOpts($attribs['lang'], $svg_class->mpdf_ref->useAdobeCJK, $svg_class->mpdf_ref->fontdata);
  2913. if ($mpdf_unifont) {
  2914. $styl .= 'font-family:' . $mpdf_unifont . ';';
  2915. }
  2916. }
  2917. }
  2918. }
  2919. if ($styl) {
  2920. if (isset($attribs['style'])) {
  2921. $attribs['style'] = $styl . $attribs['style'];
  2922. } else {
  2923. $attribs['style'] = $styl;
  2924. }
  2925. }
  2926. $array_style = $svg_class->svgDefineStyle($attribs);
  2927. if (!empty($array_style['transformations'])) {
  2928. $svg_class->textoutput .= ' q ' . $array_style['transformations']; // mPDF 5.7.4
  2929. }
  2930. array_push($svg_class->svg_style, $array_style);
  2931. $svg_class->txt_data = array();
  2932. $x = isset($attribs['x']) ? $svg_class->ConvertSVGSizePixels($attribs['x'], 'x') : 0; // mPDF 5.7.4
  2933. $y = isset($attribs['y']) ? $svg_class->ConvertSVGSizePixels($attribs['y'], 'y') : 0; // mPDF 5.7.4
  2934. $x += isset($attribs['dx']) ? $svg_class->ConvertSVGSizePixels($attribs['dx'], 'x') : 0; // mPDF 5.7.4
  2935. $y += isset($attribs['dy']) ? $svg_class->ConvertSVGSizePixels($attribs['dy'], 'y') : 0; // mPDF 5.7.4
  2936. $svg_class->txt_data[0] = $x; // mPDF 5.7.4
  2937. $svg_class->txt_data[1] = $y; // mPDF 5.7.4
  2938. $critere_style = $attribs;
  2939. unset($critere_style['x'], $critere_style['y']);
  2940. $svg_class->svgDefineTxtStyle($critere_style);
  2941. $svg_class->textanchor = $svg_class->txt_style[count($svg_class->txt_style) - 1]['text-anchor']; // mPDF 5.7.4
  2942. $svg_class->textXorigin = $svg_class->txt_data[0]; // mPDF 5.7.4
  2943. $svg_class->textYorigin = $svg_class->txt_data[1]; // mPDF 5.7.4
  2944. $svg_class->textjuststarted = true; // mPDF 5.7.4
  2945. break;
  2946. // mPDF 5.7.4
  2947. case 'tspan':
  2948. // OUTPUT CHUNK(s) UP To NOW (svgText updates $svg_class->textlength)
  2949. $p_cmd = $svg_class->svgText();
  2950. $svg_class->textoutput .= $p_cmd;
  2951. $tmp = count($svg_class->svg_style) - 1;
  2952. $current_style = $svg_class->svg_style[$tmp];
  2953. $styl = '';
  2954. if (_SVG_CLASSES && isset($attribs['class']) && $attribs['class']) {
  2955. $classes = preg_split('/\s+/', trim($attribs['class']));
  2956. foreach ($classes AS $class) {
  2957. if (isset($svg_class->mpdf_ref->cssmgr->CSS['CLASS>>' . strtoupper($class)])) {
  2958. $c = $svg_class->mpdf_ref->cssmgr->CSS['CLASS>>' . strtoupper($class)];
  2959. foreach ($c AS $prop => $val) {
  2960. $styl .= strtolower($prop) . ':' . $val . ';';
  2961. }
  2962. }
  2963. }
  2964. }
  2965. if (_SVG_AUTOFONT && isset($attribs['lang']) && $attribs['lang']) {
  2966. if (!$svg_class->mpdf_ref->usingCoreFont) {
  2967. if ($attribs['lang'] != $svg_class->mpdf_ref->default_lang) {
  2968. list ($coreSuitable, $mpdf_unifont) = GetLangOpts($attribs['lang'], $svg_class->mpdf_ref->useAdobeCJK, $svg_class->mpdf_ref->fontdata);
  2969. if ($mpdf_unifont) {
  2970. $styl .= 'font-family:' . $mpdf_unifont . ';';
  2971. }
  2972. }
  2973. }
  2974. }
  2975. if ($styl) {
  2976. if (isset($attribs['style'])) {
  2977. $attribs['style'] = $styl . $attribs['style'];
  2978. } else {
  2979. $attribs['style'] = $styl;
  2980. }
  2981. }
  2982. $array_style = $svg_class->svgDefineStyle($attribs);
  2983. $svg_class->txt_data = array();
  2984. // If absolute position adjustment (x or y), creates new block of text for text-alignment
  2985. if (isset($attribs['x']) || isset($attribs['y'])) {
  2986. // If text-anchor middle|end, adjust
  2987. if ($svg_class->textanchor == 'end') {
  2988. $tx = -$svg_class->texttotallength;
  2989. } else if ($svg_class->textanchor == 'middle') {
  2990. $tx = -$svg_class->texttotallength / 2;
  2991. } else {
  2992. $tx = 0;
  2993. }
  2994. while (preg_match('/mPDF-AXS\((.*?)\)/', $svg_class->textoutput, $m)) {
  2995. if ($tx) {
  2996. $txk = $m[1] + ($tx * $svg_class->kp);
  2997. $svg_class->textoutput = preg_replace('/mPDF-AXS\((.*?)\)/', sprintf('%.4F', $txk), $svg_class->textoutput, 1);
  2998. } else {
  2999. $svg_class->textoutput = preg_replace('/mPDF-AXS\((.*?)\)/', '\\1', $svg_class->textoutput, 1);
  3000. }
  3001. }
  3002. $svg_class->svgWriteString($svg_class->textoutput);
  3003. $svg_class->textXorigin += $svg_class->textlength;
  3004. $currentX = $svg_class->textXorigin;
  3005. $currentY = $svg_class->textYorigin;
  3006. $svg_class->textlength = 0;
  3007. $svg_class->texttotallength = 0;
  3008. $svg_class->textoutput = '';
  3009. $x = isset($attribs['x']) ? $svg_class->ConvertSVGSizePixels($attribs['x'], 'x') : $currentX;
  3010. $y = isset($attribs['y']) ? $svg_class->ConvertSVGSizePixels($attribs['y'], 'y') : $currentY;
  3011. $svg_class->txt_data[0] = $x;
  3012. $svg_class->txt_data[1] = $y;
  3013. $critere_style = $attribs;
  3014. unset($critere_style['x'], $critere_style['y']);
  3015. $svg_class->svgDefineTxtStyle($critere_style);
  3016. $svg_class->textanchor = $svg_class->txt_style[count($svg_class->txt_style) - 1]['text-anchor'];
  3017. $svg_class->textXorigin = $x;
  3018. $svg_class->textYorigin = $y;
  3019. } else {
  3020. $svg_class->textXorigin += $svg_class->textlength;
  3021. $currentX = $svg_class->textXorigin;
  3022. $currentY = $svg_class->textYorigin;
  3023. $currentX += isset($attribs['dx']) ? $svg_class->ConvertSVGSizePixels($attribs['dx'], 'x') : 0;
  3024. $currentY += isset($attribs['dy']) ? $svg_class->ConvertSVGSizePixels($attribs['dy'], 'y') : 0;
  3025. $svg_class->txt_data[0] = $currentX;
  3026. $svg_class->txt_data[1] = $currentY;
  3027. $critere_style = $attribs;
  3028. unset($critere_style['x'], $critere_style['y']);
  3029. $svg_class->svgDefineTxtStyle($critere_style);
  3030. $svg_class->textXorigin = $currentX;
  3031. $svg_class->textYorigin = $currentY;
  3032. }
  3033. if (!empty($array_style['transformations'])) {
  3034. $svg_class->textoutput .= ' q ' . $array_style['transformations'];
  3035. }
  3036. array_push($svg_class->svg_style, $array_style);
  3037. break;
  3038. }
  3039. //
  3040. //insertion des path et du style dans le flux de donné general.
  3041. if (isset($path_cmd) && $path_cmd) {
  3042. // mPDF 5.0
  3043. list($prestyle, $poststyle) = $svg_class->svgStyle($path_style, $attribs, strtolower($name));
  3044. if (isset($path_style['transformations']) && $path_style['transformations']) { // transformation on an element
  3045. $svg_class->svgWriteString(" q " . $path_style['transformations'] . $prestyle . $path_cmd . $poststyle . " Q\n");
  3046. } else {
  3047. $svg_class->svgWriteString(" q " . $prestyle . $path_cmd . $poststyle . " Q\n"); // mPDF 5.7.4
  3048. }
  3049. }
  3050. }
  3051. function characterData($parser, $data)
  3052. {
  3053. global $svg_class;
  3054. if ($svg_class->inDefs) {
  3055. return;
  3056. } // mPDF 5.7.2
  3057. if (isset($svg_class->txt_data[2])) {
  3058. $svg_class->txt_data[2] .= $data;
  3059. } else {
  3060. $svg_class->txt_data[2] = $data;
  3061. $svg_class->txt_data[0] = $svg_class->textXorigin;
  3062. $svg_class->txt_data[1] = $svg_class->textYorigin;
  3063. }
  3064. }
  3065. function xml_svg2pdf_end($parser, $name)
  3066. {
  3067. global $svg_class;
  3068. // mPDF 5.7.2
  3069. // Don't output stuff inside <defs>
  3070. if ($name == 'defs') {
  3071. $svg_class->inDefs = false;
  3072. return;
  3073. }
  3074. if ($svg_class->inDefs) {
  3075. return;
  3076. }
  3077. switch ($name) {
  3078. case "g":
  3079. case "a":
  3080. if ($svg_class->intext) {
  3081. $p_cmd = $svg_class->svgText();
  3082. $svg_class->textoutput .= $p_cmd;
  3083. }
  3084. $tmp = count($svg_class->svg_style) - 1;
  3085. $current_style = $svg_class->svg_style[$tmp];
  3086. if (!empty($current_style['transformations'])) {
  3087. // If in the middle of <text> element, add to textoutput, else WriteString
  3088. if ($svg_class->intext) {
  3089. $svg_class->textoutput .= " Q\n";
  3090. } // mPDF 5.7.4
  3091. else {
  3092. $svg_class->svgWriteString(" Q\n");
  3093. }
  3094. }
  3095. array_pop($svg_class->svg_style);
  3096. array_pop($svg_class->txt_style);
  3097. if ($svg_class->intext) {
  3098. $svg_class->textXorigin += $svg_class->textlength;
  3099. $svg_class->textlength = 0;
  3100. }
  3101. break;
  3102. case 'font':
  3103. $last_svg_fontdefw = '';
  3104. break;
  3105. case 'font-face':
  3106. $last_svg_fontid = '';
  3107. $last_svg_fontstyle = '';
  3108. break;
  3109. case 'radialgradient':
  3110. case 'lineargradient':
  3111. $last_gradid = '';
  3112. break;
  3113. case "text":
  3114. $svg_class->txt_data[2] = rtrim($svg_class->txt_data[2]); // mPDF 5.7.4
  3115. $path_cmd = $svg_class->svgText();
  3116. $svg_class->textoutput .= $path_cmd; // mPDF 5.7.4
  3117. $tmp = count($svg_class->svg_style) - 1;
  3118. $current_style = $svg_class->svg_style[$tmp];
  3119. if (!empty($current_style['transformations'])) {
  3120. $svg_class->textoutput .= " Q\n"; // mPDF 5.7.4
  3121. }
  3122. array_pop($svg_class->svg_style);
  3123. array_pop($svg_class->txt_style); // mPDF 5.7.4
  3124. // mPDF 5.7.4
  3125. // If text-anchor middle|end, adjust
  3126. if ($svg_class->textanchor == 'end') {
  3127. $tx = -$svg_class->texttotallength;
  3128. } else if ($svg_class->textanchor == 'middle') {
  3129. $tx = -$svg_class->texttotallength / 2;
  3130. } else {
  3131. $tx = 0;
  3132. }
  3133. while (preg_match('/mPDF-AXS\((.*?)\)/', $svg_class->textoutput, $m)) {
  3134. if ($tx) {
  3135. $txk = $m[1] + ($tx * $svg_class->kp);
  3136. $svg_class->textoutput = preg_replace('/mPDF-AXS\((.*?)\)/', sprintf('%.4F', $txk), $svg_class->textoutput, 1);
  3137. } else {
  3138. $svg_class->textoutput = preg_replace('/mPDF-AXS\((.*?)\)/', '\\1', $svg_class->textoutput, 1);
  3139. }
  3140. }
  3141. $svg_class->svgWriteString($svg_class->textoutput);
  3142. $svg_class->textlength = 0;
  3143. $svg_class->texttotallength = 0;
  3144. $svg_class->textoutput = '';
  3145. $svg_class->intext = false; // mPDF 5.7.4
  3146. break;
  3147. // mPDF 5.7.4
  3148. case "tspan":
  3149. $p_cmd = $svg_class->svgText();
  3150. $svg_class->textoutput .= $p_cmd;
  3151. $tmp = count($svg_class->svg_style) - 1;
  3152. $current_style = $svg_class->svg_style[$tmp];
  3153. if ($current_style['transformations']) {
  3154. $svg_class->textoutput .= " Q\n";
  3155. }
  3156. array_pop($svg_class->svg_style);
  3157. array_pop($svg_class->txt_style);
  3158. $svg_class->textXorigin += $svg_class->textlength;
  3159. $svg_class->textlength = 0;
  3160. break;
  3161. }
  3162. }
  3163. }
  3164. $svg2pdf_xml = '';
  3165. global $svg_class;
  3166. $svg_class = $this;
  3167. // Don't output stuff inside <defs>
  3168. $svg_class->inDefs = false;
  3169. $svg2pdf_xml_parser = xml_parser_create("utf-8");
  3170. xml_parser_set_option($svg2pdf_xml_parser, XML_OPTION_CASE_FOLDING, false);
  3171. xml_set_element_handler($svg2pdf_xml_parser, "xml_svg2pdf_start", "xml_svg2pdf_end");
  3172. xml_set_character_data_handler($svg2pdf_xml_parser, "characterData");
  3173. xml_parse($svg2pdf_xml_parser, $data);
  3174. if ($this->svg_error) {
  3175. return false;
  3176. } else {
  3177. 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);
  3178. }
  3179. }
  3180. // AUTOFONT =========================
  3181. function markScriptToLang($html)
  3182. {
  3183. if ($this->mpdf_ref->onlyCoreFonts) {
  3184. return $html;
  3185. }
  3186. if (empty($this->script2lang)) {
  3187. if (!empty($this->mpdf_ref->script2lang)) {
  3188. $this->script2lang = $this->mpdf_ref->script2lang;
  3189. $this->viet = $this->mpdf_ref->viet;
  3190. $this->pashto = $this->mpdf_ref->pashto;
  3191. $this->urdu = $this->mpdf_ref->urdu;
  3192. $this->persian = $this->mpdf_ref->persian;
  3193. $this->sindhi = $this->mpdf_ref->sindhi;
  3194. } else {
  3195. include(_MPDF_PATH . 'config_script2lang.php');
  3196. }
  3197. }
  3198. $n = '';
  3199. $a = preg_split('/<(.*?)>/ms', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
  3200. foreach ($a as $i => $e) {
  3201. if ($i % 2 == 0) {
  3202. $e = strcode2utf($e);
  3203. $e = $this->mpdf_ref->lesser_entity_decode($e);
  3204. $earr = $this->mpdf_ref->UTF8StringToArray($e, false);
  3205. $scriptblock = 0;
  3206. $scriptblocks = array();
  3207. $scriptblocks[0] = 0;
  3208. $chardata = array();
  3209. $subchunk = 0;
  3210. $charctr = 0;
  3211. foreach ($earr as $char) {
  3212. $ucd_record = UCDN::get_ucd_record($char);
  3213. $sbl = $ucd_record[6];
  3214. if ($sbl && $sbl != 40 && $sbl != 102) {
  3215. if ($scriptblock == 0) {
  3216. $scriptblock = $sbl;
  3217. $scriptblocks[$subchunk] = $scriptblock;
  3218. } else if ($scriptblock > 0 && $scriptblock != $sbl) {
  3219. // NEW (non-common) Script encountered in this chunk.
  3220. // Start a new subchunk
  3221. $subchunk++;
  3222. $scriptblock = $sbl;
  3223. $charctr = 0;
  3224. $scriptblocks[$subchunk] = $scriptblock;
  3225. }
  3226. }
  3227. $chardata[$subchunk][$charctr]['script'] = $sbl;
  3228. $chardata[$subchunk][$charctr]['uni'] = $char;
  3229. $charctr++;
  3230. }
  3231. // If scriptblock[x] = common & non-baseScript
  3232. // and scriptblock[x+1] = baseScript
  3233. // Move common script from end of x to start of x+1
  3234. for ($sch = 0; $sch < $subchunk; $sch++) {
  3235. if ($scriptblocks[$sch] > 0 && $scriptblocks[$sch] != $this->mpdf_ref->baseScript && $scriptblocks[$sch + 1] == $this->mpdf_ref->baseScript) {
  3236. $end = count($chardata[$sch]) - 1;
  3237. while ($chardata[$sch][$end]['script'] == 0 && $end > 1) { // common script
  3238. $tmp = array_pop($chardata[$sch]);
  3239. array_unshift($chardata[$sch + 1], $tmp);
  3240. $end--;
  3241. }
  3242. }
  3243. }
  3244. $o = '';
  3245. for ($sch = 0; $sch <= $subchunk; $sch++) {
  3246. if (isset($chardata[$sch])) {
  3247. $s = '';
  3248. for ($j = 0; $j < count($chardata[$sch]); $j++) {
  3249. $s.=code2utf($chardata[$sch][$j]['uni']);
  3250. }
  3251. // ZZZ99 Undo lesser_entity_decode as above - but only for <>&
  3252. $s = str_replace("&", "&amp;", $s);
  3253. $s = str_replace("<", "&lt;", $s);
  3254. $s = str_replace(">", "&gt;", $s);
  3255. if (substr($a[$i - 1], 0, 5) != '<text' && substr($a[$i - 1], 0, 5) != '<tspa') {
  3256. continue;
  3257. } // <tspan> or <text> only
  3258. $lang = '';
  3259. // Check Vietnamese if Latin script - even if Basescript
  3260. if ($scriptblocks[$sch] == UCDN::SCRIPT_LATIN && $this->mpdf_ref->autoVietnamese && preg_match("/([" . $this->viet . "])/u", $s)) {
  3261. $lang = "vi";
  3262. }
  3263. // Check Arabic for different languages if Arabic script - even if Basescript
  3264. else if ($scriptblocks[$sch] == UCDN::SCRIPT_ARABIC && $this->mpdf_ref->autoArabic) {
  3265. if (preg_match("/[" . $this->sindhi . "]/u", $s)) {
  3266. $lang = "sd";
  3267. } else if (preg_match("/[" . $this->urdu . "]/u", $s)) {
  3268. $lang = "ur";
  3269. } else if (preg_match("/[" . $this->pashto . "]/u", $s)) {
  3270. $lang = "ps";
  3271. } else if (preg_match("/[" . $this->persian . "]/u", $s)) {
  3272. $lang = "fa";
  3273. } else if ($this->mpdf_ref->baseScript != UCDN::SCRIPT_ARABIC && isset($this->script2lang[$scriptblocks[$sch]])) {
  3274. $lang = "'.$this->script2lang[$scriptblocks[$sch]].'";
  3275. }
  3276. }
  3277. // Identify Script block if not Basescript, and mark up as language
  3278. else if ($scriptblocks[$sch] > 0 && $scriptblocks[$sch] != $this->mpdf_ref->baseScript && isset($this->script2lang[$scriptblocks[$sch]])) {
  3279. $lang = $this->script2lang[$scriptblocks[$sch]];
  3280. }
  3281. if ($lang) {
  3282. $o .= '<tspan lang="' . $lang . '">' . $s . '</tspan>';
  3283. } else {
  3284. $o .= $s;
  3285. }
  3286. }
  3287. }
  3288. $a[$i] = $o;
  3289. } else {
  3290. $a[$i] = '<' . $e . '>';
  3291. }
  3292. }
  3293. $n = implode('', $a);
  3294. return $n;
  3295. }
  3296. }
  3297. // END OF CLASS
  3298. // END OF CLASS
  3299. // END OF CLASS
  3300. function calc_bezier_bbox($start, $c)
  3301. {
  3302. $P0 = array($start[0], $start[1]);
  3303. $P1 = array($c[0], $c[1]);
  3304. $P2 = array($c[2], $c[3]);
  3305. $P3 = array($c[4], $c[5]);
  3306. $bounds = array();
  3307. $bounds[0][] = $P0[0];
  3308. $bounds[1][] = $P0[1];
  3309. $bounds[0][] = $P3[0];
  3310. $bounds[1][] = $P3[1];
  3311. for ($i = 0; $i <= 1; $i++) {
  3312. $b = 6 * $P0[$i] - 12 * $P1[$i] + 6 * $P2[$i];
  3313. $a = -3 * $P0[$i] + 9 * $P1[$i] - 9 * $P2[$i] + 3 * $P3[$i];
  3314. $c = 3 * $P1[$i] - 3 * $P0[$i];
  3315. if ($a == 0) {
  3316. if ($b == 0) {
  3317. continue;
  3318. }
  3319. $t = -$c / $b;
  3320. if ($t > 0 && $t < 1) {
  3321. $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]);
  3322. }
  3323. continue;
  3324. }
  3325. $b2ac = pow($b, 2) - 4 * $c * $a;
  3326. if ($b2ac < 0) {
  3327. continue;
  3328. }
  3329. $t1 = (-$b + sqrt($b2ac)) / (2 * $a);
  3330. if ($t1 > 0 && $t1 < 1) {
  3331. $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]);
  3332. }
  3333. $t2 = (-$b - sqrt($b2ac)) / (2 * $a);
  3334. if ($t2 > 0 && $t2 < 1) {
  3335. $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]);
  3336. }
  3337. }
  3338. $x = min($bounds[0]);
  3339. $x2 = max($bounds[0]);
  3340. $y = min($bounds[1]);
  3341. $y2 = max($bounds[1]);
  3342. return array($x, $y, $x2, $y2);
  3343. }
  3344. function _testIntersectCircle($cx, $cy, $cr)
  3345. {
  3346. // Tests whether a circle fully encloses a rectangle 0,0,1,1
  3347. // to see if any further radial gradients need adding (SVG)
  3348. // If centre of circle is inside 0,0,1,1 square
  3349. if ($cx >= 0 && $cx <= 1 && $cy >= 0 && $cy <= 1) {
  3350. $maxd = 1.5;
  3351. }
  3352. // distance to four corners
  3353. else {
  3354. $d1 = sqrt(pow(($cy - 0), 2) + pow(($cx - 0), 2));
  3355. $d2 = sqrt(pow(($cy - 1), 2) + pow(($cx - 0), 2));
  3356. $d3 = sqrt(pow(($cy - 0), 2) + pow(($cx - 1), 2));
  3357. $d4 = sqrt(pow(($cy - 1), 2) + pow(($cx - 1), 2));
  3358. $maxd = max($d1, $d2, $d3, $d4);
  3359. }
  3360. if ($cr < $maxd) {
  3361. return true;
  3362. } else {
  3363. return false;
  3364. }
  3365. }
  3366. function _testIntersect($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4)
  3367. {
  3368. // Tests whether line (x1, y1) and (x2, y2) [a gradient axis (perpendicular)]
  3369. // intersects with a specific line segment (x3, y3) and (x4, y4)
  3370. $a1 = $y2 - $y1;
  3371. $b1 = $x1 - $x2;
  3372. $c1 = $a1 * $x1 + $b1 * $y1;
  3373. $a2 = $y4 - $y3;
  3374. $b2 = $x3 - $x4;
  3375. $c2 = $a2 * $x3 + $b2 * $y3;
  3376. $det = $a1 * $b2 - $a2 * $b1;
  3377. if ($det == 0) { //Lines are parallel
  3378. return false;
  3379. } else {
  3380. $x = ($b2 * $c1 - $b1 * $c2) / $det;
  3381. $y = ($a1 * $c2 - $a2 * $c1) / $det;
  3382. if ($x >= $x3 && $x <= $x4 && $y >= $y3 && $y <= $y4) {
  3383. return true;
  3384. }
  3385. }
  3386. return false;
  3387. }