cssmgr.php 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. <?php
  2. class cssmgr
  3. {
  4. var $mpdf = null;
  5. var $tablecascadeCSS;
  6. var $cascadeCSS;
  7. var $CSS;
  8. var $tbCSSlvl;
  9. public function __construct(mPDF $mpdf)
  10. {
  11. $this->mpdf = $mpdf;
  12. $this->tablecascadeCSS = array();
  13. $this->CSS = array();
  14. $this->cascadeCSS = array();
  15. $this->tbCSSlvl = 0;
  16. }
  17. function ReadCSS($html)
  18. {
  19. preg_match_all('/<style[^>]*media=["\']([^"\'>]*)["\'].*?<\/style>/is', $html, $m);
  20. for ($i = 0; $i < count($m[0]); $i++) {
  21. if ($this->mpdf->CSSselectMedia && !preg_match('/(' . trim($this->mpdf->CSSselectMedia) . '|all)/i', $m[1][$i])) {
  22. $html = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', '', $html);
  23. }
  24. }
  25. preg_match_all('/<link[^>]*media=["\']([^"\'>]*)["\'].*?>/is', $html, $m);
  26. for ($i = 0; $i < count($m[0]); $i++) {
  27. if ($this->mpdf->CSSselectMedia && !preg_match('/(' . trim($this->mpdf->CSSselectMedia) . '|all)/i', $m[1][$i])) {
  28. $html = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', '', $html);
  29. }
  30. }
  31. // mPDF 5.5.02
  32. // Remove Comment tags <!-- ... --> inside CSS as <style> in HTML document
  33. // Remove Comment tags /* ... */ inside CSS as <style> in HTML document
  34. // But first, we replace upper and mixed case closing style tag with lower
  35. // case so we can use str_replace later.
  36. preg_match_all('/<style.*?>(.*?)<\/style>/si',$html,$m);
  37. if (count($m[1])) {
  38. for ($i = 0; $i < count($m[1]); $i++) {
  39. // Remove comment tags
  40. $sub = preg_replace('/(<\!\-\-|\-\->)/s',' ',$m[1][$i]);
  41. $sub = '>'.preg_replace('|/\*.*?\*/|s',' ',$sub).'</style>';
  42. $html = str_replace('>'.$m[1][$i].'</style>', $sub, $html);
  43. }
  44. }
  45. $html = preg_replace('/<!--mpdf/i', '', $html);
  46. $html = preg_replace('/mpdf-->/i', '', $html);
  47. $html = preg_replace('/<\!\-\-.*?\-\->/s', ' ', $html);
  48. $match = 0; // no match for instance
  49. $regexp = ''; // This helps debugging: showing what is the REAL string being processed
  50. $CSSext = array();
  51. //CSS inside external files
  52. $regexp = '/<link[^>]*rel=["\']stylesheet["\'][^>]*href=["\']([^>"\']*)["\'].*?>/si';
  53. $x = preg_match_all($regexp, $html, $cxt);
  54. if ($x) {
  55. $match += $x;
  56. $CSSext = $cxt[1];
  57. }
  58. $regexp = '/<link[^>]*href=["\']([^>"\']*)["\'][^>]*?rel=["\']stylesheet["\'].*?>/si';
  59. $x = preg_match_all($regexp, $html, $cxt);
  60. if ($x) {
  61. $match += $x;
  62. $CSSext = array_merge($CSSext, $cxt[1]);
  63. }
  64. // look for @import stylesheets
  65. //$regexp = '/@import url\([\'\"]{0,1}([^\)]*?\.css)[\'\"]{0,1}\)/si';
  66. $regexp = '/@import url\([\'\"]{0,1}([^\)]*?\.css(\?\S+)?)[\'\"]{0,1}\)/si';
  67. $x = preg_match_all($regexp, $html, $cxt);
  68. if ($x) {
  69. $match += $x;
  70. $CSSext = array_merge($CSSext, $cxt[1]);
  71. }
  72. // look for @import without the url()
  73. //$regexp = '/@import [\'\"]{0,1}([^;]*?\.css)[\'\"]{0,1}/si';
  74. $regexp = '/@import [\'\"]{0,1}([^;]*?\.css(\?\S+)?)[\'\"]{0,1}/si';
  75. $x = preg_match_all($regexp, $html, $cxt);
  76. if ($x) {
  77. $match += $x;
  78. $CSSext = array_merge($CSSext, $cxt[1]);
  79. }
  80. $ind = 0;
  81. $CSSstr = '';
  82. if (!is_array($this->cascadeCSS))
  83. $this->cascadeCSS = array();
  84. while ($match) {
  85. $path = $CSSext[$ind];
  86. $path = htmlspecialchars_decode($path); // mPDF 6
  87. $this->mpdf->GetFullPath($path);
  88. $CSSextblock = $this->mpdf->_get_file($path);
  89. if ($CSSextblock) {
  90. // look for embedded @import stylesheets in other stylesheets
  91. // and fix url paths (including background-images) relative to stylesheet
  92. //$regexpem = '/@import url\([\'\"]{0,1}(.*?\.css)[\'\"]{0,1}\)/si';
  93. $regexpem = '/@import url\([\'\"]{0,1}(.*?\.css(\?\S+)?)[\'\"]{0,1}\)/si';
  94. $xem = preg_match_all($regexpem, $CSSextblock, $cxtem);
  95. $cssBasePath = preg_replace('/\/[^\/]*$/', '', $path) . '/';
  96. if ($xem) {
  97. foreach ($cxtem[1] AS $cxtembedded) {
  98. // path is relative to original stlyesheet!!
  99. $this->mpdf->GetFullPath($cxtembedded, $cssBasePath);
  100. $match++;
  101. $CSSext[] = $cxtembedded;
  102. }
  103. }
  104. $regexpem = '/(background[^;]*url\s*\(\s*[\'\"]{0,1})([^\)\'\"]*)([\'\"]{0,1}\s*\))/si';
  105. $xem = preg_match_all($regexpem, $CSSextblock, $cxtem);
  106. if ($xem) {
  107. for ($i = 0; $i < count($cxtem[0]); $i++) {
  108. // path is relative to original stlyesheet!!
  109. $embedded = $cxtem[2][$i];
  110. if (!preg_match('/^data:image/i', $embedded)) { // mPDF 5.5.13
  111. $this->mpdf->GetFullPath($embedded, $cssBasePath);
  112. $CSSextblock = preg_replace('/' . preg_quote($cxtem[0][$i], '/') . '/', ($cxtem[1][$i] . $embedded . $cxtem[3][$i]), $CSSextblock);
  113. }
  114. }
  115. }
  116. $CSSstr .= ' ' . $CSSextblock;
  117. }
  118. $match--;
  119. $ind++;
  120. } //end of match
  121. $match = 0; // reset value, if needed
  122. // CSS as <style> in HTML document
  123. $regexp = '/<style.*?>(.*?)<\/style>/si';
  124. $match = preg_match_all($regexp, $html, $CSSblock);
  125. if ($match) {
  126. $tmpCSSstr = implode(' ', $CSSblock[1]);
  127. $regexpem = '/(background[^;]*url\s*\(\s*[\'\"]{0,1})([^\)\'\"]*)([\'\"]{0,1}\s*\))/si';
  128. $xem = preg_match_all($regexpem, $tmpCSSstr, $cxtem);
  129. if ($xem) {
  130. for ($i = 0; $i < count($cxtem[0]); $i++) {
  131. $embedded = $cxtem[2][$i];
  132. if (!preg_match('/^data:image/i', $embedded)) { // mPDF 5.5.13
  133. $this->mpdf->GetFullPath($embedded);
  134. $tmpCSSstr = preg_replace('/' . preg_quote($cxtem[0][$i], '/') . '/', ($cxtem[1][$i] . $embedded . $cxtem[3][$i]), $tmpCSSstr);
  135. }
  136. }
  137. }
  138. $CSSstr .= ' ' . $tmpCSSstr;
  139. }
  140. // Remove comments
  141. $CSSstr = preg_replace('|/\*.*?\*/|s', ' ', $CSSstr);
  142. $CSSstr = preg_replace('/[\s\n\r\t\f]/s', ' ', $CSSstr);
  143. if (preg_match('/@media/', $CSSstr)) {
  144. preg_match_all('/@media(.*?)\{(([^\{\}]*\{[^\{\}]*\})+)\s*\}/is', $CSSstr, $m);
  145. for ($i = 0; $i < count($m[0]); $i++) {
  146. if ($this->mpdf->CSSselectMedia && !preg_match('/(' . trim($this->mpdf->CSSselectMedia) . '|all)/i', $m[1][$i])) {
  147. $CSSstr = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', '', $CSSstr);
  148. } else {
  149. $CSSstr = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', ' ' . $m[2][$i] . ' ', $CSSstr);
  150. }
  151. }
  152. }
  153. // Replace any background: url(data:image... with temporary image file reference
  154. preg_match_all("/(url\(data:image\/(jpeg|gif|png);base64,(.*?)\))/si", $CSSstr, $idata); // mPDF 5.7.2
  155. if (count($idata[0])) {
  156. for ($i = 0; $i < count($idata[0]); $i++) {
  157. $file = _MPDF_TEMP_PATH . '_tempCSSidata' . RAND(1, 10000) . '_' . $i . '.' . $idata[2][$i];
  158. //Save to local file
  159. file_put_contents($file, base64_decode($idata[3][$i]));
  160. // $this->mpdf->GetFullPath($file); // ? is this needed - NO mPDF 5.6.03
  161. $CSSstr = str_replace($idata[0][$i], 'url("' . $file . '")', $CSSstr); // mPDF 5.5.17
  162. }
  163. }
  164. $CSSstr = preg_replace('/(<\!\-\-|\-\->)/s', ' ', $CSSstr);
  165. // mPDF 5.7.4 URLs
  166. // Characters "(" ")" and ";" in url() e.g. background-image, cause problems parsing the CSS string
  167. // URLencode ( and ), but change ";" to a code which can be converted back after parsing (so as not to confuse ;
  168. // with a segment delimiter in the URI)
  169. $tempmarker = '%ZZ';
  170. if (strpos($CSSstr, 'url(') !== false) {
  171. preg_match_all('/url\(\"(.*?)\"\)/', $CSSstr, $m);
  172. for ($i = 0; $i < count($m[1]); $i++) {
  173. $tmp = str_replace(array('(', ')', ';'), array('%28', '%29', $tempmarker), $m[1][$i]);
  174. $CSSstr = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', 'url(\'' . $tmp . '\')', $CSSstr);
  175. }
  176. preg_match_all('/url\(\'(.*?)\'\)/', $CSSstr, $m);
  177. for ($i = 0; $i < count($m[1]); $i++) {
  178. $tmp = str_replace(array('(', ')', ';'), array('%28', '%29', $tempmarker), $m[1][$i]);
  179. $CSSstr = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', 'url(\'' . $tmp . '\')', $CSSstr);
  180. }
  181. preg_match_all('/url\(([^\'\"].*?[^\'\"])\)/', $CSSstr, $m);
  182. for ($i = 0; $i < count($m[1]); $i++) {
  183. $tmp = str_replace(array('(', ')', ';'), array('%28', '%29', $tempmarker), $m[1][$i]);
  184. $CSSstr = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', 'url(\'' . $tmp . '\')', $CSSstr);
  185. }
  186. }
  187. if ($CSSstr) {
  188. $classproperties = array(); // mPDF 6
  189. preg_match_all('/(.*?)\{(.*?)\}/', $CSSstr, $styles);
  190. for ($i = 0; $i < count($styles[1]); $i++) {
  191. // SET array e.g. $classproperties['COLOR'] = '#ffffff';
  192. $stylestr = trim($styles[2][$i]);
  193. $stylearr = explode(';', $stylestr);
  194. foreach ($stylearr AS $sta) {
  195. if (trim($sta)) {
  196. // Changed to allow style="background: url('http://www.bpm1.com/bg.jpg')"
  197. $tmp = explode(':', $sta, 2);
  198. $property = $tmp[0];
  199. if (isset($tmp[1])) {
  200. $value = $tmp[1];
  201. } else {
  202. $value = '';
  203. }
  204. $value = str_replace($tempmarker, ';', $value); // mPDF 5.7.4 URLs
  205. $property = trim($property);
  206. $value = preg_replace('/\s*!important/i', '', $value);
  207. $value = trim($value);
  208. if ($property && ($value || $value === '0')) {
  209. // Ignores -webkit-gradient so doesn't override -moz-
  210. if ((strtoupper($property) == 'BACKGROUND-IMAGE' || strtoupper($property) == 'BACKGROUND') && preg_match('/-webkit-gradient/i', $value)) {
  211. continue;
  212. }
  213. $classproperties[strtoupper($property)] = $value;
  214. }
  215. }
  216. }
  217. $classproperties = $this->fixCSS($classproperties);
  218. $tagstr = strtoupper(trim($styles[1][$i]));
  219. $tagarr = explode(',', $tagstr);
  220. $pageselectors = false; // used to turn on $this->mpdf->mirrorMargins
  221. foreach ($tagarr AS $tg) {
  222. // mPDF 5.7.4
  223. if (preg_match('/NTH-CHILD\((\s*(([\-+]?\d*)N(\s*[\-+]\s*\d+)?|[\-+]?\d+|ODD|EVEN)\s*)\)/', $tg, $m)) {
  224. $tg = preg_replace('/NTH-CHILD\(.*\)/', 'NTH-CHILD(' . str_replace(' ', '', $m[1]) . ')', $tg);
  225. }
  226. $tags = preg_split('/\s+/', trim($tg));
  227. $level = count($tags);
  228. $t = '';
  229. $t2 = '';
  230. $t3 = '';
  231. if (trim($tags[0]) == '@PAGE') {
  232. if (isset($tags[0])) {
  233. $t = trim($tags[0]);
  234. }
  235. if (isset($tags[1])) {
  236. $t2 = trim($tags[1]);
  237. }
  238. if (isset($tags[2])) {
  239. $t3 = trim($tags[2]);
  240. }
  241. $tag = '';
  242. if ($level == 1) {
  243. $tag = $t;
  244. } else if ($level == 2 && preg_match('/^[:](.*)$/', $t2, $m)) {
  245. $tag = $t . '>>PSEUDO>>' . $m[1];
  246. if ($m[1] == 'LEFT' || $m[1] == 'RIGHT') {
  247. $pageselectors = true;
  248. } // used to turn on $this->mpdf->mirrorMargins
  249. } else if ($level == 2) {
  250. $tag = $t . '>>NAMED>>' . $t2;
  251. } else if ($level == 3 && preg_match('/^[:](.*)$/', $t3, $m)) {
  252. $tag = $t . '>>NAMED>>' . $t2 . '>>PSEUDO>>' . $m[1];
  253. if ($m[1] == 'LEFT' || $m[1] == 'RIGHT') {
  254. $pageselectors = true;
  255. } // used to turn on $this->mpdf->mirrorMargins
  256. }
  257. if (isset($this->CSS[$tag]) && $tag) {
  258. $this->CSS[$tag] = $this->array_merge_recursive_unique($this->CSS[$tag], $classproperties);
  259. } else if ($tag) {
  260. $this->CSS[$tag] = $classproperties;
  261. }
  262. } else if ($level == 1) { // e.g. p or .class or #id or p.class or p#id
  263. if (isset($tags[0])) {
  264. $t = trim($tags[0]);
  265. }
  266. if ($t) {
  267. $tag = '';
  268. if (preg_match('/^[.](.*)$/', $t, $m)) {
  269. $tag = 'CLASS>>' . $m[1];
  270. } else if (preg_match('/^[#](.*)$/', $t, $m)) {
  271. $tag = 'ID>>' . $m[1];
  272. } else if (preg_match('/^\[LANG=[\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\]$/', $t, $m)) {
  273. $tag = 'LANG>>' . strtolower($m[1]);
  274. } // mPDF 6 Special case for lang as attribute selector
  275. else if (preg_match('/^:LANG\([\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\)$/', $t, $m)) {
  276. $tag = 'LANG>>' . strtolower($m[1]);
  277. } // mPDF 6 Special case for lang as attribute selector
  278. else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')[.](.*)$/', $t, $m)) {
  279. $tag = $m[1] . '>>CLASS>>' . $m[2];
  280. } else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')\s*:NTH-CHILD\((.*)\)$/', $t, $m)) {
  281. $tag = $m[1] . '>>SELECTORNTHCHILD>>' . $m[2];
  282. } else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')[#](.*)$/', $t, $m)) {
  283. $tag = $m[1] . '>>ID>>' . $m[2];
  284. } else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')\[LANG=[\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\]$/', $t, $m)) {
  285. $tag = $m[1] . '>>LANG>>' . strtolower($m[2]);
  286. } // mPDF 6 Special case for lang as attribute selector
  287. else if (preg_match('/^(' . $this->mpdf->allowedCSStags . '):LANG\([\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\)$/', $t, $m)) {
  288. $tag = $m[1] . '>>LANG>>' . strtolower($m[2]);
  289. } // mPDF 6 Special case for lang as attribute selector
  290. else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')$/', $t)) {
  291. $tag = $t;
  292. }
  293. if (isset($this->CSS[$tag]) && $tag) {
  294. $this->CSS[$tag] = $this->array_merge_recursive_unique($this->CSS[$tag], $classproperties);
  295. } else if ($tag) {
  296. $this->CSS[$tag] = $classproperties;
  297. }
  298. }
  299. } else {
  300. $tmp = array();
  301. for ($n = 0; $n < $level; $n++) {
  302. if (isset($tags[$n])) {
  303. $t = trim($tags[$n]);
  304. } else {
  305. $t = '';
  306. }
  307. if ($t) {
  308. $tag = '';
  309. if (preg_match('/^[.](.*)$/', $t, $m)) {
  310. $tag = 'CLASS>>' . $m[1];
  311. } else if (preg_match('/^[#](.*)$/', $t, $m)) {
  312. $tag = 'ID>>' . $m[1];
  313. } else if (preg_match('/^\[LANG=[\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\]$/', $t, $m)) {
  314. $tag = 'LANG>>' . strtolower($m[1]);
  315. } // mPDF 6 Special case for lang as attribute selector
  316. else if (preg_match('/^:LANG\([\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\)$/', $t, $m)) {
  317. $tag = 'LANG>>' . strtolower($m[1]);
  318. } // mPDF 6 Special case for lang as attribute selector
  319. else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')[.](.*)$/', $t, $m)) {
  320. $tag = $m[1] . '>>CLASS>>' . $m[2];
  321. } else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')\s*:NTH-CHILD\((.*)\)$/', $t, $m)) {
  322. $tag = $m[1] . '>>SELECTORNTHCHILD>>' . $m[2];
  323. } else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')[#](.*)$/', $t, $m)) {
  324. $tag = $m[1] . '>>ID>>' . $m[2];
  325. } else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')\[LANG=[\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\]$/', $t, $m)) {
  326. $tag = $m[1] . '>>LANG>>' . strtolower($m[2]);
  327. } // mPDF 6 Special case for lang as attribute selector
  328. else if (preg_match('/^(' . $this->mpdf->allowedCSStags . '):LANG\([\'\"]{0,1}([A-Z\-]{2,11})[\'\"]{0,1}\)$/', $t, $m)) {
  329. $tag = $m[1] . '>>LANG>>' . strtolower($m[2]);
  330. } // mPDF 6 Special case for lang as attribute selector
  331. else if (preg_match('/^(' . $this->mpdf->allowedCSStags . ')$/', $t)) {
  332. $tag = $t;
  333. }
  334. if ($tag)
  335. $tmp[] = $tag;
  336. else {
  337. break;
  338. }
  339. }
  340. }
  341. if ($tag) {
  342. $x = &$this->cascadeCSS;
  343. foreach ($tmp AS $tp) {
  344. $x = &$x[$tp];
  345. }
  346. $x = $this->array_merge_recursive_unique($x, $classproperties);
  347. $x['depth'] = $level;
  348. }
  349. }
  350. }
  351. if ($pageselectors) {
  352. $this->mpdf->mirrorMargins = true;
  353. }
  354. $properties = array();
  355. $values = array();
  356. $classproperties = array();
  357. }
  358. } // end of if
  359. //Remove CSS (tags and content), if any
  360. $regexp = '/<style.*?>(.*?)<\/style>/si'; // it can be <style> or <style type="txt/css">
  361. $html = preg_replace($regexp, '', $html);
  362. //print_r($this->CSS); exit;
  363. //print_r($this->cascadeCSS); exit;
  364. return $html;
  365. }
  366. function readInlineCSS($html)
  367. {
  368. $html = htmlspecialchars_decode($html); // mPDF 5.7.4 URLs
  369. // mPDF 5.7.4 URLs
  370. // Characters "(" ")" and ";" in url() e.g. background-image, cause probems parsing the CSS string
  371. // URLencode ( and ), but change ";" to a code which can be converted back after parsing (so as not to confuse ;
  372. // with a segment delimiter in the URI)
  373. $tempmarker = '%ZZ';
  374. if (strpos($html, 'url(') !== false) {
  375. preg_match_all('/url\(\"(.*?)\"\)/', $html, $m);
  376. for ($i = 0; $i < count($m[1]); $i++) {
  377. $tmp = str_replace(array('(', ')', ';'), array('%28', '%29', $tempmarker), $m[1][$i]);
  378. $html = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', 'url(\'' . $tmp . '\')', $html);
  379. }
  380. preg_match_all('/url\(\'(.*?)\'\)/', $html, $m);
  381. for ($i = 0; $i < count($m[1]); $i++) {
  382. $tmp = str_replace(array('(', ')', ';'), array('%28', '%29', $tempmarker), $m[1][$i]);
  383. $html = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', 'url(\'' . $tmp . '\')', $html);
  384. }
  385. preg_match_all('/url\(([^\'\"].*?[^\'\"])\)/', $html, $m);
  386. for ($i = 0; $i < count($m[1]); $i++) {
  387. $tmp = str_replace(array('(', ')', ';'), array('%28', '%29', $tempmarker), $m[1][$i]);
  388. $html = preg_replace('/' . preg_quote($m[0][$i], '/') . '/', 'url(\'' . $tmp . '\')', $html);
  389. }
  390. }
  391. //Fix incomplete CSS code
  392. $size = strlen($html) - 1;
  393. if (substr($html, $size, 1) != ';')
  394. $html .= ';';
  395. //Make CSS[Name-of-the-class] = array(key => value)
  396. $regexp = '|\\s*?(\\S+?):(.+?);|i';
  397. preg_match_all($regexp, $html, $styleinfo);
  398. $properties = $styleinfo[1];
  399. $values = $styleinfo[2];
  400. //Array-properties and Array-values must have the SAME SIZE!
  401. $classproperties = array();
  402. for ($i = 0; $i < count($properties); $i++) {
  403. // Ignores -webkit-gradient so doesn't override -moz-
  404. if ((strtoupper($properties[$i]) == 'BACKGROUND-IMAGE' || strtoupper($properties[$i]) == 'BACKGROUND') && preg_match('/-webkit-gradient/i', $values[$i])) {
  405. continue;
  406. }
  407. $values[$i] = str_replace($tempmarker, ';', $values[$i]); // mPDF 5.7.4 URLs
  408. $classproperties[strtoupper($properties[$i])] = trim($values[$i]);
  409. }
  410. return $this->fixCSS($classproperties);
  411. }
  412. function _fix_borderStr($bd)
  413. {
  414. preg_match_all("/\((.*?)\)/", $bd, $m);
  415. if (count($m[1])) {
  416. for ($i = 0; $i < count($m[1]); $i++) {
  417. $sub = preg_replace("/ /", "", $m[1][$i]);
  418. $bd = preg_replace('/' . preg_quote($m[1][$i], '/') . '/si', $sub, $bd);
  419. }
  420. }
  421. $prop = preg_split('/\s+/', trim($bd));
  422. $w = 'medium';
  423. $c = '#000000';
  424. $s = 'none';
  425. if (count($prop) == 1) {
  426. // solid
  427. if (in_array($prop[0], $this->mpdf->borderstyles) || $prop[0] == 'none' || $prop[0] == 'hidden') {
  428. $s = $prop[0];
  429. }
  430. // #000000
  431. else if (is_array($this->mpdf->ConvertColor($prop[0]))) {
  432. $c = $prop[0];
  433. }
  434. // 1px
  435. else {
  436. $w = $prop[0];
  437. }
  438. } else if (count($prop) == 2) {
  439. // 1px solid
  440. if (in_array($prop[1], $this->mpdf->borderstyles) || $prop[1] == 'none' || $prop[1] == 'hidden') {
  441. $w = $prop[0];
  442. $s = $prop[1];
  443. }
  444. // solid #000000
  445. else if (in_array($prop[0], $this->mpdf->borderstyles) || $prop[0] == 'none' || $prop[0] == 'hidden') {
  446. $s = $prop[0];
  447. $c = $prop[1];
  448. }
  449. // 1px #000000
  450. else {
  451. $w = $prop[0];
  452. $c = $prop[1];
  453. }
  454. } else if (count($prop) == 3) {
  455. // Change #000000 1px solid to 1px solid #000000 (proper)
  456. if (substr($prop[0], 0, 1) == '#') {
  457. $c = $prop[0];
  458. $w = $prop[1];
  459. $s = $prop[2];
  460. }
  461. // Change solid #000000 1px to 1px solid #000000 (proper)
  462. else if (substr($prop[0], 1, 1) == '#') {
  463. $s = $prop[0];
  464. $c = $prop[1];
  465. $w = $prop[2];
  466. }
  467. // Change solid 1px #000000 to 1px solid #000000 (proper)
  468. else if (in_array($prop[0], $this->mpdf->borderstyles) || $prop[0] == 'none' || $prop[0] == 'hidden') {
  469. $s = $prop[0];
  470. $w = $prop[1];
  471. $c = $prop[2];
  472. } else {
  473. $w = $prop[0];
  474. $s = $prop[1];
  475. $c = $prop[2];
  476. }
  477. } else {
  478. return '';
  479. }
  480. $s = strtolower($s);
  481. return $w . ' ' . $s . ' ' . $c;
  482. }
  483. function fixCSS($prop)
  484. {
  485. if (!is_array($prop) || (count($prop) == 0))
  486. return array();
  487. $newprop = array();
  488. foreach ($prop AS $k => $v) {
  489. if ($k != 'BACKGROUND-IMAGE' && $k != 'BACKGROUND' && $k != 'ODD-HEADER-NAME' && $k != 'EVEN-HEADER-NAME' && $k != 'ODD-FOOTER-NAME' && $k != 'EVEN-FOOTER-NAME' && $k != 'HEADER' && $k != 'FOOTER') {
  490. $v = strtolower($v);
  491. }
  492. if ($k == 'FONT') {
  493. $s = trim($v);
  494. preg_match_all('/\"(.*?)\"/', $s, $ff);
  495. if (count($ff[1])) {
  496. foreach ($ff[1] AS $ffp) {
  497. $w = preg_split('/\s+/', $ffp);
  498. $s = preg_replace('/\"' . $ffp . '\"/', $w[0], $s);
  499. }
  500. }
  501. preg_match_all('/\'(.*?)\'/', $s, $ff);
  502. if (count($ff[1])) {
  503. foreach ($ff[1] AS $ffp) {
  504. $w = preg_split('/\s+/', $ffp);
  505. $s = preg_replace('/\'' . $ffp . '\'/', $w[0], $s);
  506. }
  507. }
  508. $s = preg_replace('/\s*,\s*/', ',', $s);
  509. $bits = preg_split('/\s+/', $s);
  510. if (count($bits) > 1) {
  511. $k = 'FONT-FAMILY';
  512. $v = $bits[(count($bits) - 1)];
  513. $fs = $bits[(count($bits) - 2)];
  514. if (preg_match('/(.*?)\/(.*)/', $fs, $fsp)) {
  515. $newprop['FONT-SIZE'] = $fsp[1];
  516. $newprop['LINE-HEIGHT'] = $fsp[2];
  517. } else {
  518. $newprop['FONT-SIZE'] = $fs;
  519. }
  520. if (preg_match('/(italic|oblique)/i', $s)) {
  521. $newprop['FONT-STYLE'] = 'italic';
  522. } else {
  523. $newprop['FONT-STYLE'] = 'normal';
  524. }
  525. if (preg_match('/bold/i', $s)) {
  526. $newprop['FONT-WEIGHT'] = 'bold';
  527. } else {
  528. $newprop['FONT-WEIGHT'] = 'normal';
  529. }
  530. if (preg_match('/small-caps/i', $s)) {
  531. $newprop['TEXT-TRANSFORM'] = 'uppercase';
  532. }
  533. }
  534. } else if ($k == 'FONT-FAMILY') {
  535. $aux_fontlist = explode(",", $v);
  536. $found = 0;
  537. foreach ($aux_fontlist AS $f) {
  538. $fonttype = trim($f);
  539. $fonttype = preg_replace('/["\']*(.*?)["\']*/', '\\1', $fonttype);
  540. $fonttype = preg_replace('/ /', '', $fonttype);
  541. $v = strtolower(trim($fonttype));
  542. if (isset($this->mpdf->fonttrans[$v]) && $this->mpdf->fonttrans[$v]) {
  543. $v = $this->mpdf->fonttrans[$v];
  544. }
  545. if ((!$this->mpdf->onlyCoreFonts && in_array($v, $this->mpdf->available_unifonts)) ||
  546. in_array($v, array('ccourier', 'ctimes', 'chelvetica')) ||
  547. ($this->mpdf->onlyCoreFonts && in_array($v, array('courier', 'times', 'helvetica', 'arial'))) ||
  548. in_array($v, array('sjis', 'uhc', 'big5', 'gb'))) {
  549. $newprop[$k] = $v;
  550. $found = 1;
  551. break;
  552. }
  553. }
  554. if (!$found) {
  555. foreach ($aux_fontlist AS $f) {
  556. $fonttype = trim($f);
  557. $fonttype = preg_replace('/["\']*(.*?)["\']*/', '\\1', $fonttype);
  558. $fonttype = preg_replace('/ /', '', $fonttype);
  559. $v = strtolower(trim($fonttype));
  560. if (isset($this->mpdf->fonttrans[$v]) && $this->mpdf->fonttrans[$v]) {
  561. $v = $this->mpdf->fonttrans[$v];
  562. }
  563. if (in_array($v, $this->mpdf->sans_fonts) || in_array($v, $this->mpdf->serif_fonts) || in_array($v, $this->mpdf->mono_fonts)) {
  564. $newprop[$k] = $v;
  565. break;
  566. }
  567. }
  568. }
  569. }
  570. // mPDF 5.7.1
  571. else if ($k == 'FONT-VARIANT') {
  572. if (preg_match('/(normal|none)/', $v, $m)) { // mPDF 6
  573. $newprop['FONT-VARIANT-LIGATURES'] = $m[1];
  574. $newprop['FONT-VARIANT-CAPS'] = $m[1];
  575. $newprop['FONT-VARIANT-NUMERIC'] = $m[1];
  576. $newprop['FONT-VARIANT-ALTERNATES'] = $m[1];
  577. } else {
  578. if (preg_match_all('/(no-common-ligatures|\bcommon-ligatures|no-discretionary-ligatures|\bdiscretionary-ligatures|no-historical-ligatures|\bhistorical-ligatures|no-contextual|\bcontextual)/i', $v, $m)) {
  579. $newprop['FONT-VARIANT-LIGATURES'] = implode(' ', $m[1]);
  580. }
  581. if (preg_match('/(all-small-caps|\bsmall-caps|all-petite-caps|\bpetite-caps|unicase|titling-caps)/i', $v, $m)) {
  582. $newprop['FONT-VARIANT-CAPS'] = $m[1];
  583. }
  584. if (preg_match_all('/(lining-nums|oldstyle-nums|proportional-nums|tabular-nums|diagonal-fractions|stacked-fractions)/i', $v, $m)) {
  585. $newprop['FONT-VARIANT-NUMERIC'] = implode(' ', $m[1]);
  586. }
  587. if (preg_match('/(historical-forms)/i', $v, $m)) {
  588. $newprop['FONT-VARIANT-ALTERNATES'] = $m[1];
  589. }
  590. }
  591. } else if ($k == 'MARGIN') {
  592. $tmp = $this->expand24($v);
  593. $newprop['MARGIN-TOP'] = $tmp['T'];
  594. $newprop['MARGIN-RIGHT'] = $tmp['R'];
  595. $newprop['MARGIN-BOTTOM'] = $tmp['B'];
  596. $newprop['MARGIN-LEFT'] = $tmp['L'];
  597. }
  598. /* -- BORDER-RADIUS -- */ else if ($k == 'BORDER-RADIUS' || $k == 'BORDER-TOP-LEFT-RADIUS' || $k == 'BORDER-TOP-RIGHT-RADIUS' || $k == 'BORDER-BOTTOM-LEFT-RADIUS' || $k == 'BORDER-BOTTOM-RIGHT-RADIUS') {
  599. $tmp = $this->border_radius_expand($v, $k);
  600. if (isset($tmp['TL-H']))
  601. $newprop['BORDER-TOP-LEFT-RADIUS-H'] = $tmp['TL-H'];
  602. if (isset($tmp['TL-V']))
  603. $newprop['BORDER-TOP-LEFT-RADIUS-V'] = $tmp['TL-V'];
  604. if (isset($tmp['TR-H']))
  605. $newprop['BORDER-TOP-RIGHT-RADIUS-H'] = $tmp['TR-H'];
  606. if (isset($tmp['TR-V']))
  607. $newprop['BORDER-TOP-RIGHT-RADIUS-V'] = $tmp['TR-V'];
  608. if (isset($tmp['BL-H']))
  609. $newprop['BORDER-BOTTOM-LEFT-RADIUS-H'] = $tmp['BL-H'];
  610. if (isset($tmp['BL-V']))
  611. $newprop['BORDER-BOTTOM-LEFT-RADIUS-V'] = $tmp['BL-V'];
  612. if (isset($tmp['BR-H']))
  613. $newprop['BORDER-BOTTOM-RIGHT-RADIUS-H'] = $tmp['BR-H'];
  614. if (isset($tmp['BR-V']))
  615. $newprop['BORDER-BOTTOM-RIGHT-RADIUS-V'] = $tmp['BR-V'];
  616. }
  617. /* -- END BORDER-RADIUS -- */
  618. else if ($k == 'PADDING') {
  619. $tmp = $this->expand24($v);
  620. $newprop['PADDING-TOP'] = $tmp['T'];
  621. $newprop['PADDING-RIGHT'] = $tmp['R'];
  622. $newprop['PADDING-BOTTOM'] = $tmp['B'];
  623. $newprop['PADDING-LEFT'] = $tmp['L'];
  624. } else if ($k == 'BORDER') {
  625. if ($v == '1') {
  626. $v = '1px solid #000000';
  627. } else {
  628. $v = $this->_fix_borderStr($v);
  629. }
  630. $newprop['BORDER-TOP'] = $v;
  631. $newprop['BORDER-RIGHT'] = $v;
  632. $newprop['BORDER-BOTTOM'] = $v;
  633. $newprop['BORDER-LEFT'] = $v;
  634. } else if ($k == 'BORDER-TOP') {
  635. $newprop['BORDER-TOP'] = $this->_fix_borderStr($v);
  636. } else if ($k == 'BORDER-RIGHT') {
  637. $newprop['BORDER-RIGHT'] = $this->_fix_borderStr($v);
  638. } else if ($k == 'BORDER-BOTTOM') {
  639. $newprop['BORDER-BOTTOM'] = $this->_fix_borderStr($v);
  640. } else if ($k == 'BORDER-LEFT') {
  641. $newprop['BORDER-LEFT'] = $this->_fix_borderStr($v);
  642. } else if ($k == 'BORDER-STYLE') {
  643. $e = $this->expand24($v);
  644. if (!empty($e)) {
  645. $newprop['BORDER-TOP-STYLE'] = $e['T'];
  646. $newprop['BORDER-RIGHT-STYLE'] = $e['R'];
  647. $newprop['BORDER-BOTTOM-STYLE'] = $e['B'];
  648. $newprop['BORDER-LEFT-STYLE'] = $e['L'];
  649. }
  650. } else if ($k == 'BORDER-WIDTH') {
  651. $e = $this->expand24($v);
  652. if (!empty($e)) {
  653. $newprop['BORDER-TOP-WIDTH'] = $e['T'];
  654. $newprop['BORDER-RIGHT-WIDTH'] = $e['R'];
  655. $newprop['BORDER-BOTTOM-WIDTH'] = $e['B'];
  656. $newprop['BORDER-LEFT-WIDTH'] = $e['L'];
  657. }
  658. } else if ($k == 'BORDER-COLOR') {
  659. $e = $this->expand24($v);
  660. if (!empty($e)) {
  661. $newprop['BORDER-TOP-COLOR'] = $e['T'];
  662. $newprop['BORDER-RIGHT-COLOR'] = $e['R'];
  663. $newprop['BORDER-BOTTOM-COLOR'] = $e['B'];
  664. $newprop['BORDER-LEFT-COLOR'] = $e['L'];
  665. }
  666. } else if ($k == 'BORDER-SPACING') {
  667. $prop = preg_split('/\s+/', trim($v));
  668. if (count($prop) == 1) {
  669. $newprop['BORDER-SPACING-H'] = $prop[0];
  670. $newprop['BORDER-SPACING-V'] = $prop[0];
  671. } else if (count($prop) == 2) {
  672. $newprop['BORDER-SPACING-H'] = $prop[0];
  673. $newprop['BORDER-SPACING-V'] = $prop[1];
  674. }
  675. } else if ($k == 'TEXT-OUTLINE') { // mPDF 5.6.07
  676. $prop = preg_split('/\s+/', trim($v));
  677. if (trim(strtolower($v)) == 'none') {
  678. $newprop['TEXT-OUTLINE'] = 'none';
  679. } else if (count($prop) == 2) {
  680. $newprop['TEXT-OUTLINE-WIDTH'] = $prop[0];
  681. $newprop['TEXT-OUTLINE-COLOR'] = $prop[1];
  682. } else if (count($prop) == 3) {
  683. $newprop['TEXT-OUTLINE-WIDTH'] = $prop[0];
  684. $newprop['TEXT-OUTLINE-COLOR'] = $prop[2];
  685. }
  686. } else if ($k == 'SIZE') {
  687. $prop = preg_split('/\s+/', trim($v));
  688. if (preg_match('/(auto|portrait|landscape)/', $prop[0])) {
  689. $newprop['SIZE'] = strtoupper($prop[0]);
  690. } else if (count($prop) == 1) {
  691. $newprop['SIZE']['W'] = $this->mpdf->ConvertSize($prop[0]);
  692. $newprop['SIZE']['H'] = $this->mpdf->ConvertSize($prop[0]);
  693. } else if (count($prop) == 2) {
  694. $newprop['SIZE']['W'] = $this->mpdf->ConvertSize($prop[0]);
  695. $newprop['SIZE']['H'] = $this->mpdf->ConvertSize($prop[1]);
  696. }
  697. } else if ($k == 'SHEET-SIZE') {
  698. $prop = preg_split('/\s+/', trim($v));
  699. if (count($prop) == 2) {
  700. $newprop['SHEET-SIZE'] = array($this->mpdf->ConvertSize($prop[0]), $this->mpdf->ConvertSize($prop[1]));
  701. } else {
  702. if (preg_match('/([0-9a-zA-Z]*)-L/i', $v, $m)) { // e.g. A4-L = A$ landscape
  703. $ft = $this->mpdf->_getPageFormat($m[1]);
  704. $format = array($ft[1], $ft[0]);
  705. } else {
  706. $format = $this->mpdf->_getPageFormat($v);
  707. }
  708. if ($format) {
  709. $newprop['SHEET-SIZE'] = array($format[0] / _MPDFK, $format[1] / _MPDFK);
  710. }
  711. }
  712. } else if ($k == 'BACKGROUND') {
  713. $bg = $this->parseCSSbackground($v);
  714. if ($bg['c']) {
  715. $newprop['BACKGROUND-COLOR'] = $bg['c'];
  716. } else {
  717. $newprop['BACKGROUND-COLOR'] = 'transparent';
  718. }
  719. /* -- BACKGROUNDS -- */
  720. if ($bg['i']) {
  721. $newprop['BACKGROUND-IMAGE'] = $bg['i'];
  722. if ($bg['r']) {
  723. $newprop['BACKGROUND-REPEAT'] = $bg['r'];
  724. }
  725. if ($bg['p']) {
  726. $newprop['BACKGROUND-POSITION'] = $bg['p'];
  727. }
  728. } else {
  729. $newprop['BACKGROUND-IMAGE'] = '';
  730. }
  731. /* -- END BACKGROUNDS -- */
  732. }
  733. /* -- BACKGROUNDS -- */ else if ($k == 'BACKGROUND-IMAGE') {
  734. if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $v, $m)) {
  735. $newprop['BACKGROUND-IMAGE'] = $m[0];
  736. continue;
  737. }
  738. if (preg_match('/url\([\'\"]{0,1}(.*?)[\'\"]{0,1}\)/i', $v, $m)) {
  739. $newprop['BACKGROUND-IMAGE'] = $m[1];
  740. } else if (strtolower($v) == 'none') {
  741. $newprop['BACKGROUND-IMAGE'] = '';
  742. }
  743. } else if ($k == 'BACKGROUND-REPEAT') {
  744. if (preg_match('/(repeat-x|repeat-y|no-repeat|repeat)/i', $v, $m)) {
  745. $newprop['BACKGROUND-REPEAT'] = strtolower($m[1]);
  746. }
  747. } else if ($k == 'BACKGROUND-POSITION') {
  748. $s = $v;
  749. $bits = preg_split('/\s+/', trim($s));
  750. // These should be Position x1 or x2
  751. if (count($bits) == 1) {
  752. if (preg_match('/bottom/', $bits[0])) {
  753. $bg['p'] = '50% 100%';
  754. } else if (preg_match('/top/', $bits[0])) {
  755. $bg['p'] = '50% 0%';
  756. } else {
  757. $bg['p'] = $bits[0] . ' 50%';
  758. }
  759. } else if (count($bits) == 2) {
  760. // Can be either right center or center right
  761. if (preg_match('/(top|bottom)/', $bits[0]) || preg_match('/(left|right)/', $bits[1])) {
  762. $bg['p'] = $bits[1] . ' ' . $bits[0];
  763. } else {
  764. $bg['p'] = $bits[0] . ' ' . $bits[1];
  765. }
  766. }
  767. if ($bg['p']) {
  768. $bg['p'] = preg_replace('/(left|top)/', '0%', $bg['p']);
  769. $bg['p'] = preg_replace('/(right|bottom)/', '100%', $bg['p']);
  770. $bg['p'] = preg_replace('/(center)/', '50%', $bg['p']);
  771. if (!preg_match('/[\-]{0,1}\d+(in|cm|mm|pt|pc|em|ex|px|%)* [\-]{0,1}\d+(in|cm|mm|pt|pc|em|ex|px|%)*/', $bg['p'])) {
  772. $bg['p'] = false;
  773. }
  774. }
  775. if ($bg['p']) {
  776. $newprop['BACKGROUND-POSITION'] = $bg['p'];
  777. }
  778. }
  779. /* -- END BACKGROUNDS -- */ else if ($k == 'IMAGE-ORIENTATION') {
  780. if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad)/i', $v, $m)) {
  781. $angle = $m[1] + 0;
  782. if (strtolower($m[2]) == 'deg') {
  783. $angle = $angle;
  784. } else if (strtolower($m[2]) == 'grad') {
  785. $angle *= (360 / 400);
  786. } else if (strtolower($m[2]) == 'rad') {
  787. $angle = rad2deg($angle);
  788. }
  789. while ($angle < 0) {
  790. $angle += 360;
  791. }
  792. $angle = ($angle % 360);
  793. $angle /= 90;
  794. $angle = round($angle) * 90;
  795. $newprop['IMAGE-ORIENTATION'] = $angle;
  796. }
  797. } else if ($k == 'TEXT-ALIGN') {
  798. if (preg_match('/["\'](.){1}["\']/i', $v, $m)) {
  799. $d = array_search($m[1], $this->mpdf->decimal_align);
  800. if ($d !== false) {
  801. $newprop['TEXT-ALIGN'] = $d;
  802. }
  803. if (preg_match('/(center|left|right)/i', $v, $m)) {
  804. $newprop['TEXT-ALIGN'] .= strtoupper(substr($m[1], 0, 1));
  805. } else {
  806. $newprop['TEXT-ALIGN'] .= 'R';
  807. } // default = R
  808. } else if (preg_match('/["\'](\\\[a-fA-F0-9]{1,6})["\']/i', $v, $m)) {
  809. $utf8 = codeHex2utf(substr($m[1], 1, 6));
  810. $d = array_search($utf8, $this->mpdf->decimal_align);
  811. if ($d !== false) {
  812. $newprop['TEXT-ALIGN'] = $d;
  813. }
  814. if (preg_match('/(center|left|right)/i', $v, $m)) {
  815. $newprop['TEXT-ALIGN'] .= strtoupper(substr($m[1], 0, 1));
  816. } else {
  817. $newprop['TEXT-ALIGN'] .= 'R';
  818. } // default = R
  819. } else {
  820. $newprop[$k] = $v;
  821. }
  822. }
  823. // mpDF 6 Lists
  824. else if ($k == 'LIST-STYLE') {
  825. if (preg_match('/none/i', $v, $m)) {
  826. $newprop['LIST-STYLE-TYPE'] = 'none';
  827. $newprop['LIST-STYLE-IMAGE'] = 'none';
  828. }
  829. if (preg_match('/(lower-roman|upper-roman|lower-latin|lower-alpha|upper-latin|upper-alpha|decimal|disc|circle|square|arabic-indic|bengali|devanagari|gujarati|gurmukhi|kannada|malayalam|oriya|persian|tamil|telugu|thai|urdu|cambodian|khmer|lao|cjk-decimal|hebrew)/i', $v, $m)) {
  830. $newprop['LIST-STYLE-TYPE'] = strtolower(trim($m[1]));
  831. } else if (preg_match('/U\+([a-fA-F0-9]+)/i', $v, $m)) {
  832. $newprop['LIST-STYLE-TYPE'] = strtolower(trim($m[1]));
  833. }
  834. if (preg_match('/url\([\'\"]{0,1}(.*?)[\'\"]{0,1}\)/i', $v, $m)) {
  835. $newprop['LIST-STYLE-IMAGE'] = strtolower(trim($m[1]));
  836. }
  837. if (preg_match('/(inside|outside)/i', $v, $m)) {
  838. $newprop['LIST-STYLE-POSITION'] = strtolower(trim($m[1]));
  839. }
  840. } else {
  841. $newprop[$k] = $v;
  842. }
  843. }
  844. return $newprop;
  845. }
  846. function setCSSboxshadow($v)
  847. {
  848. $sh = array();
  849. $c = preg_match_all('/(rgba|rgb|device-cmyka|cmyka|device-cmyk|cmyk|hsla|hsl)\(.*?\)/', $v, $x); // mPDF 5.6.05
  850. for ($i = 0; $i < $c; $i++) {
  851. $col = preg_replace('/,/', '*', $x[0][$i]);
  852. $v = preg_replace('/' . preg_quote($x[0][$i], '/') . '/', $col, $v);
  853. }
  854. $ss = explode(',', $v);
  855. foreach ($ss AS $s) {
  856. $new = array('inset' => false, 'blur' => 0, 'spread' => 0);
  857. if (preg_match('/inset/i', $s)) {
  858. $new['inset'] = true;
  859. $s = preg_replace('/\s*inset\s*/', '', $s);
  860. }
  861. $p = explode(' ', trim($s));
  862. if (isset($p[0])) {
  863. $new['x'] = $this->mpdf->ConvertSize(trim($p[0]), $this->mpdf->blk[$this->mpdf->blklvl - 1]['inner_width'], $this->mpdf->FontSize, false);
  864. }
  865. if (isset($p[1])) {
  866. $new['y'] = $this->mpdf->ConvertSize(trim($p[1]), $this->mpdf->blk[$this->mpdf->blklvl - 1]['inner_width'], $this->mpdf->FontSize, false);
  867. }
  868. if (isset($p[2])) {
  869. if (preg_match('/^\s*[\.\-0-9]/', $p[2])) {
  870. $new['blur'] = $this->mpdf->ConvertSize(trim($p[2]), $this->mpdf->blk[$this->mpdf->blklvl - 1]['inner_width'], $this->mpdf->FontSize, false);
  871. } else {
  872. $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/', ',', $p[2]));
  873. }
  874. if (isset($p[3])) {
  875. if (preg_match('/^\s*[\.\-0-9]/', $p[3])) {
  876. $new['spread'] = $this->mpdf->ConvertSize(trim($p[3]), $this->mpdf->blk[$this->mpdf->blklvl - 1]['inner_width'], $this->mpdf->FontSize, false);
  877. } else {
  878. $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/', ',', $p[3]));
  879. }
  880. if (isset($p[4])) {
  881. $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/', ',', $p[4]));
  882. }
  883. }
  884. }
  885. if (!$new['col']) {
  886. $new['col'] = $this->mpdf->ConvertColor('#888888');
  887. }
  888. if (isset($new['y'])) {
  889. array_unshift($sh, $new);
  890. }
  891. }
  892. return $sh;
  893. }
  894. function setCSStextshadow($v)
  895. {
  896. $sh = array();
  897. $c = preg_match_all('/(rgba|rgb|device-cmyka|cmyka|device-cmyk|cmyk|hsla|hsl)\(.*?\)/', $v, $x); // mPDF 5.6.05
  898. for ($i = 0; $i < $c; $i++) {
  899. $col = preg_replace('/,/', '*', $x[0][$i]);
  900. $v = preg_replace('/' . preg_quote($x[0][$i], '/') . '/', $col, $v);
  901. }
  902. $ss = explode(',', $v);
  903. foreach ($ss AS $s) {
  904. $new = array('blur' => 0);
  905. $p = explode(' ', trim($s));
  906. if (isset($p[0])) {
  907. $new['x'] = $this->mpdf->ConvertSize(trim($p[0]), $this->mpdf->FontSize, $this->mpdf->FontSize, false);
  908. }
  909. if (isset($p[1])) {
  910. $new['y'] = $this->mpdf->ConvertSize(trim($p[1]), $this->mpdf->FontSize, $this->mpdf->FontSize, false);
  911. }
  912. if (isset($p[2])) {
  913. if (preg_match('/^\s*[\.\-0-9]/', $p[2])) {
  914. $new['blur'] = $this->mpdf->ConvertSize(trim($p[2]), $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'], $this->mpdf->FontSize, false);
  915. } else {
  916. $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/', ',', $p[2]));
  917. }
  918. if (isset($p[3])) {
  919. $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/', ',', $p[3]));
  920. }
  921. }
  922. if (!isset($new['col']) || !$new['col']) {
  923. $new['col'] = $this->mpdf->ConvertColor('#888888');
  924. }
  925. if (isset($new['y'])) {
  926. array_unshift($sh, $new);
  927. }
  928. }
  929. return $sh;
  930. }
  931. function parseCSSbackground($s)
  932. {
  933. $bg = array('c' => false, 'i' => false, 'r' => false, 'p' => false,);
  934. /* -- BACKGROUNDS -- */
  935. if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i', $s, $m)) {
  936. $bg['i'] = $m[0];
  937. } else
  938. /* -- END BACKGROUNDS -- */
  939. if (preg_match('/url\(/i', $s)) {
  940. // If color, set and strip it off
  941. // mPDF 5.6.05
  942. if (preg_match('/^\s*(#[0-9a-fA-F]{3,6}|(rgba|rgb|device-cmyka|cmyka|device-cmyk|cmyk|hsla|hsl|spot)\(.*?\)|[a-zA-Z]{3,})\s+(url\(.*)/i', $s, $m)) {
  943. $bg['c'] = strtolower($m[1]);
  944. $s = $m[3];
  945. }
  946. /* -- BACKGROUNDS -- */
  947. if (preg_match('/url\([\'\"]{0,1}(.*?)[\'\"]{0,1}\)\s*(.*)/i', $s, $m)) {
  948. $bg['i'] = $m[1];
  949. $s = strtolower($m[2]);
  950. if (preg_match('/(repeat-x|repeat-y|no-repeat|repeat)/', $s, $m)) {
  951. $bg['r'] = $m[1];
  952. }
  953. // Remove repeat, attachment (discarded) and also any inherit
  954. $s = preg_replace('/(repeat-x|repeat-y|no-repeat|repeat|scroll|fixed|inherit)/', '', $s);
  955. $bits = preg_split('/\s+/', trim($s));
  956. // These should be Position x1 or x2
  957. if (count($bits) == 1) {
  958. if (preg_match('/bottom/', $bits[0])) {
  959. $bg['p'] = '50% 100%';
  960. } else if (preg_match('/top/', $bits[0])) {
  961. $bg['p'] = '50% 0%';
  962. } else {
  963. $bg['p'] = $bits[0] . ' 50%';
  964. }
  965. } else if (count($bits) == 2) {
  966. // Can be either right center or center right
  967. if (preg_match('/(top|bottom)/', $bits[0]) || preg_match('/(left|right)/', $bits[1])) {
  968. $bg['p'] = $bits[1] . ' ' . $bits[0];
  969. } else {
  970. $bg['p'] = $bits[0] . ' ' . $bits[1];
  971. }
  972. }
  973. if ($bg['p']) {
  974. $bg['p'] = preg_replace('/(left|top)/', '0%', $bg['p']);
  975. $bg['p'] = preg_replace('/(right|bottom)/', '100%', $bg['p']);
  976. $bg['p'] = preg_replace('/(center)/', '50%', $bg['p']);
  977. if (!preg_match('/[\-]{0,1}\d+(in|cm|mm|pt|pc|em|ex|px|%)* [\-]{0,1}\d+(in|cm|mm|pt|pc|em|ex|px|%)*/', $bg['p'])) {
  978. $bg['p'] = false;
  979. }
  980. }
  981. }
  982. /* -- END BACKGROUNDS -- */
  983. } else if (preg_match('/^\s*(#[0-9a-fA-F]{3,6}|(rgba|rgb|device-cmyka|cmyka|device-cmyk|cmyk|hsla|hsl|spot)\(.*?\)|[a-zA-Z]{3,})/i', $s, $m)) {
  984. $bg['c'] = strtolower($m[1]);
  985. } // mPDF 5.6.05
  986. return ($bg);
  987. }
  988. function expand24($mp)
  989. {
  990. $prop = preg_split('/\s+/', trim($mp));
  991. if (count($prop) == 1) {
  992. return array('T' => $prop[0], 'R' => $prop[0], 'B' => $prop[0], 'L' => $prop[0]);
  993. }
  994. if (count($prop) == 2) {
  995. return array('T' => $prop[0], 'R' => $prop[1], 'B' => $prop[0], 'L' => $prop[1]);
  996. }
  997. if (count($prop) == 3) {
  998. return array('T' => $prop[0], 'R' => $prop[1], 'B' => $prop[2], 'L' => $prop[1]);
  999. }
  1000. if (count($prop) == 4) {
  1001. return array('T' => $prop[0], 'R' => $prop[1], 'B' => $prop[2], 'L' => $prop[3]);
  1002. }
  1003. return array();
  1004. }
  1005. /* -- BORDER-RADIUS -- */
  1006. function border_radius_expand($val, $k)
  1007. {
  1008. $b = array();
  1009. if ($k == 'BORDER-RADIUS') {
  1010. $hv = explode('/', trim($val));
  1011. $prop = preg_split('/\s+/', trim($hv[0]));
  1012. if (count($prop) == 1) {
  1013. $b['TL-H'] = $b['TR-H'] = $b['BR-H'] = $b['BL-H'] = $prop[0];
  1014. } else if (count($prop) == 2) {
  1015. $b['TL-H'] = $b['BR-H'] = $prop[0];
  1016. $b['TR-H'] = $b['BL-H'] = $prop[1];
  1017. } else if (count($prop) == 3) {
  1018. $b['TL-H'] = $prop[0];
  1019. $b['TR-H'] = $b['BL-H'] = $prop[1];
  1020. $b['BR-H'] = $prop[2];
  1021. } else if (count($prop) == 4) {
  1022. $b['TL-H'] = $prop[0];
  1023. $b['TR-H'] = $prop[1];
  1024. $b['BR-H'] = $prop[2];
  1025. $b['BL-H'] = $prop[3];
  1026. }
  1027. if (count($hv) == 2) {
  1028. $prop = preg_split('/\s+/', trim($hv[1]));
  1029. if (count($prop) == 1) {
  1030. $b['TL-V'] = $b['TR-V'] = $b['BR-V'] = $b['BL-V'] = $prop[0];
  1031. } else if (count($prop) == 2) {
  1032. $b['TL-V'] = $b['BR-V'] = $prop[0];
  1033. $b['TR-V'] = $b['BL-V'] = $prop[1];
  1034. } else if (count($prop) == 3) {
  1035. $b['TL-V'] = $prop[0];
  1036. $b['TR-V'] = $b['BL-V'] = $prop[1];
  1037. $b['BR-V'] = $prop[2];
  1038. } else if (count($prop) == 4) {
  1039. $b['TL-V'] = $prop[0];
  1040. $b['TR-V'] = $prop[1];
  1041. $b['BR-V'] = $prop[2];
  1042. $b['BL-V'] = $prop[3];
  1043. }
  1044. } else {
  1045. $b['TL-V'] = $b['TL-H'];
  1046. $b['TR-V'] = $b['TR-H'];
  1047. $b['BL-V'] = $b['BL-H'];
  1048. $b['BR-V'] = $b['BR-H'];
  1049. }
  1050. return $b;
  1051. }
  1052. // Parse 2
  1053. $h = 0;
  1054. $v = 0;
  1055. $prop = preg_split('/\s+/', trim($val));
  1056. if (count($prop) == 1) {
  1057. $h = $v = $val;
  1058. } else {
  1059. $h = $prop[0];
  1060. $v = $prop[1];
  1061. }
  1062. if ($h == 0 || $v == 0) {
  1063. $h = $v = 0;
  1064. }
  1065. if ($k == 'BORDER-TOP-LEFT-RADIUS') {
  1066. $b['TL-H'] = $h;
  1067. $b['TL-V'] = $v;
  1068. } else if ($k == 'BORDER-TOP-RIGHT-RADIUS') {
  1069. $b['TR-H'] = $h;
  1070. $b['TR-V'] = $v;
  1071. } else if ($k == 'BORDER-BOTTOM-LEFT-RADIUS') {
  1072. $b['BL-H'] = $h;
  1073. $b['BL-V'] = $v;
  1074. } else if ($k == 'BORDER-BOTTOM-RIGHT-RADIUS') {
  1075. $b['BR-H'] = $h;
  1076. $b['BR-V'] = $v;
  1077. }
  1078. return $b;
  1079. }
  1080. /* -- END BORDER-RADIUS -- */
  1081. function _mergeCSS($p, &$t)
  1082. {
  1083. // Save Cascading CSS e.g. "div.topic p" at this block level
  1084. if (isset($p) && $p) {
  1085. if ($t) {
  1086. $t = $this->array_merge_recursive_unique($t, $p);
  1087. } else {
  1088. $t = $p;
  1089. }
  1090. }
  1091. }
  1092. // for CSS handling
  1093. function array_merge_recursive_unique($array1, $array2)
  1094. {
  1095. $arrays = func_get_args();
  1096. $narrays = count($arrays);
  1097. $ret = $arrays[0];
  1098. for ($i = 1; $i < $narrays; $i ++) {
  1099. foreach ($arrays[$i] as $key => $value) {
  1100. if (((string) $key) === ((string) intval($key))) { // integer or string as integer key - append
  1101. $ret[] = $value;
  1102. } else { // string key - merge
  1103. if (is_array($value) && isset($ret[$key])) {
  1104. $ret[$key] = $this->array_merge_recursive_unique($ret[$key], $value);
  1105. } else {
  1106. $ret[$key] = $value;
  1107. }
  1108. }
  1109. }
  1110. }
  1111. return $ret;
  1112. }
  1113. function _mergeFullCSS($p, &$t, $tag, $classes, $id, $lang)
  1114. { // mPDF 6
  1115. if (isset($p[$tag])) {
  1116. $this->_mergeCSS($p[$tag], $t);
  1117. }
  1118. // STYLESHEET CLASS e.g. .smallone{} .redletter{}
  1119. foreach ($classes AS $class) {
  1120. if (isset($p['CLASS>>' . $class])) {
  1121. $this->_mergeCSS($p['CLASS>>' . $class], $t);
  1122. }
  1123. }
  1124. // STYLESHEET nth-child SELECTOR e.g. tr:nth-child(odd) td:nth-child(2n+1)
  1125. if ($tag == 'TR' && isset($p) && $p) {
  1126. foreach ($p AS $k => $val) {
  1127. if (preg_match('/' . $tag . '>>SELECTORNTHCHILD>>(.*)/', $k, $m)) {
  1128. $select = false;
  1129. if ($tag == 'TR') {
  1130. $row = $this->mpdf->row;
  1131. $thnr = (isset($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_thead']) ? count($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_thead']) : 0);
  1132. $tfnr = (isset($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_tfoot']) ? count($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_tfoot']) : 0);
  1133. if ($this->mpdf->tabletfoot) {
  1134. $row -= $thnr;
  1135. } else if (!$this->mpdf->tablethead) {
  1136. $row -= ($thnr + $tfnr);
  1137. }
  1138. if (preg_match('/(([\-+]?\d*)?N([\-+]\d+)?|[\-+]?\d+|ODD|EVEN)/', $m[1], $a)) { // mPDF 5.7.4
  1139. $select = $this->_nthchild($a, $row);
  1140. }
  1141. } else if ($tag == 'TD' || $tag == 'TH') {
  1142. if (preg_match('/(([\-+]?\d*)?N([\-+]\d+)?|[\-+]?\d+|ODD|EVEN)/', $m[1], $a)) { // mPDF 5.7.4
  1143. $select = $this->_nthchild($a, $this->mpdf->col);
  1144. }
  1145. }
  1146. if ($select) {
  1147. $this->_mergeCSS($p[$tag . '>>SELECTORNTHCHILD>>' . $m[1]], $t);
  1148. }
  1149. }
  1150. }
  1151. }
  1152. // STYLESHEET CLASS e.g. [lang=fr]{} or :lang(fr)
  1153. if (isset($lang) && isset($p['LANG>>' . $lang])) {
  1154. $this->_mergeCSS($p['LANG>>' . $lang], $t);
  1155. }
  1156. // STYLESHEET CLASS e.g. #smallone{} #redletter{}
  1157. if (isset($id) && isset($p['ID>>' . $id])) {
  1158. $this->_mergeCSS($p['ID>>' . $id], $t);
  1159. }
  1160. // STYLESHEET CLASS e.g. .smallone{} .redletter{}
  1161. foreach ($classes AS $class) {
  1162. if (isset($p[$tag . '>>CLASS>>' . $class])) {
  1163. $this->_mergeCSS($p[$tag . '>>CLASS>>' . $class], $t);
  1164. }
  1165. }
  1166. // STYLESHEET CLASS e.g. [lang=fr]{} or :lang(fr)
  1167. if (isset($lang) && isset($p[$tag . '>>LANG>>' . $lang])) {
  1168. $this->_mergeCSS($p[$tag . '>>LANG>>' . $lang], $t);
  1169. }
  1170. // STYLESHEET CLASS e.g. #smallone{} #redletter{}
  1171. if (isset($id) && isset($p[$tag . '>>ID>>' . $id])) {
  1172. $this->_mergeCSS($p[$tag . '>>ID>>' . $id], $t);
  1173. }
  1174. }
  1175. function setBorderDominance($prop, $val)
  1176. {
  1177. if (isset($prop['BORDER-LEFT']) && $prop['BORDER-LEFT']) {
  1178. $this->cell_border_dominance_L = $val;
  1179. }
  1180. if (isset($prop['BORDER-RIGHT']) && $prop['BORDER-RIGHT']) {
  1181. $this->cell_border_dominance_R = $val;
  1182. }
  1183. if (isset($prop['BORDER-TOP']) && $prop['BORDER-TOP']) {
  1184. $this->cell_border_dominance_T = $val;
  1185. }
  1186. if (isset($prop['BORDER-BOTTOM']) && $prop['BORDER-BOTTOM']) {
  1187. $this->cell_border_dominance_B = $val;
  1188. }
  1189. }
  1190. function _set_mergedCSS(&$m, &$p, $d = true, $bd = false)
  1191. {
  1192. if (isset($m)) {
  1193. if ((isset($m['depth']) && $m['depth'] > 1) || $d == false) { // include check for 'depth'
  1194. if ($bd) {
  1195. $this->setBorderDominance($m, $bd);
  1196. } // *TABLES*
  1197. if (is_array($m)) {
  1198. $p = array_merge($p, $m);
  1199. $this->_mergeBorders($p, $m);
  1200. }
  1201. }
  1202. }
  1203. }
  1204. function _mergeBorders(&$b, &$a)
  1205. { // Merges $a['BORDER-TOP-STYLE'] to $b['BORDER-TOP'] etc.
  1206. foreach (array('TOP', 'RIGHT', 'BOTTOM', 'LEFT') AS $side) {
  1207. foreach (array('STYLE', 'WIDTH', 'COLOR') AS $el) {
  1208. if (isset($a['BORDER-' . $side . '-' . $el])) { // e.g. $b['BORDER-TOP-STYLE']
  1209. $s = trim($a['BORDER-' . $side . '-' . $el]);
  1210. if (isset($b['BORDER-' . $side])) { // e.g. $b['BORDER-TOP']
  1211. $p = trim($b['BORDER-' . $side]);
  1212. } else {
  1213. $p = '';
  1214. }
  1215. if ($el == 'STYLE') {
  1216. if ($p) {
  1217. $b['BORDER-' . $side] = preg_replace('/(\S+)\s+(\S+)\s+(\S+)/', '\\1 ' . $s . ' \\3', $p);
  1218. } else {
  1219. $b['BORDER-' . $side] = '0px ' . $s . ' #000000';
  1220. }
  1221. } else if ($el == 'WIDTH') {
  1222. if ($p) {
  1223. $b['BORDER-' . $side] = preg_replace('/(\S+)\s+(\S+)\s+(\S+)/', $s . ' \\2 \\3', $p);
  1224. } else {
  1225. $b['BORDER-' . $side] = $s . ' none #000000';
  1226. }
  1227. } else if ($el == 'COLOR') {
  1228. if ($p) {
  1229. $b['BORDER-' . $side] = preg_replace('/(\S+)\s+(\S+)\s+(\S+)/', '\\1 \\2 ' . $s, $p);
  1230. } else {
  1231. $b['BORDER-' . $side] = '0px none ' . $s;
  1232. }
  1233. }
  1234. }
  1235. }
  1236. }
  1237. }
  1238. function MergeCSS($inherit, $tag, $attr)
  1239. {
  1240. $p = array();
  1241. $zp = array();
  1242. $classes = array();
  1243. if (isset($attr['CLASS'])) {
  1244. $classes = preg_split('/\s+/', $attr['CLASS']);
  1245. }
  1246. if (!isset($attr['ID'])) {
  1247. $attr['ID'] = '';
  1248. }
  1249. // mPDF 6
  1250. $shortlang = '';
  1251. if (!isset($attr['LANG'])) {
  1252. $attr['LANG'] = '';
  1253. } else {
  1254. $attr['LANG'] = strtolower($attr['LANG']);
  1255. if (strlen($attr['LANG']) == 5) {
  1256. $shortlang = substr($attr['LANG'], 0, 2);
  1257. }
  1258. }
  1259. //===============================================
  1260. /* -- TABLES -- */
  1261. // Set Inherited properties
  1262. if ($inherit == 'TOPTABLE') { // $tag = TABLE
  1263. //===============================================
  1264. // Save Cascading CSS e.g. "div.topic p" at this block level
  1265. if (isset($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'])) {
  1266. $this->tablecascadeCSS[0] = $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'];
  1267. } else {
  1268. $this->tablecascadeCSS[0] = $this->cascadeCSS;
  1269. }
  1270. }
  1271. //===============================================
  1272. // Set Inherited properties
  1273. if ($inherit == 'TOPTABLE' || $inherit == 'TABLE') {
  1274. //Cascade everything from last level that is not an actual property, or defined by current tag/attributes
  1275. if (isset($this->tablecascadeCSS[$this->tbCSSlvl - 1]) && is_array($this->tablecascadeCSS[$this->tbCSSlvl - 1])) {
  1276. foreach ($this->tablecascadeCSS[$this->tbCSSlvl - 1] AS $k => $v) {
  1277. $this->tablecascadeCSS[$this->tbCSSlvl][$k] = $v;
  1278. }
  1279. }
  1280. $this->_mergeFullCSS($this->cascadeCSS, $this->tablecascadeCSS[$this->tbCSSlvl], $tag, $classes, $attr['ID'], $attr['LANG']);
  1281. //===============================================
  1282. // Cascading forward CSS e.g. "table.topic td" for this table in $this->tablecascadeCSS
  1283. //===============================================
  1284. // STYLESHEET TAG e.g. table
  1285. $this->_mergeFullCSS($this->tablecascadeCSS[$this->tbCSSlvl - 1], $this->tablecascadeCSS[$this->tbCSSlvl], $tag, $classes, $attr['ID'], $attr['LANG']);
  1286. //===============================================
  1287. }
  1288. /* -- END TABLES -- */
  1289. //===============================================
  1290. // Set Inherited properties
  1291. if ($inherit == 'BLOCK') {
  1292. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS']) && is_array($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS'])) {
  1293. foreach ($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS'] AS $k => $v) {
  1294. $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'][$k] = $v;
  1295. }
  1296. }
  1297. //===============================================
  1298. // Save Cascading CSS e.g. "div.topic p" at this block level
  1299. $this->_mergeFullCSS($this->cascadeCSS, $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'], $tag, $classes, $attr['ID'], $attr['LANG']);
  1300. //===============================================
  1301. // Cascading forward CSS
  1302. //===============================================
  1303. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1])) {
  1304. $this->_mergeFullCSS($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS'], $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'], $tag, $classes, $attr['ID'], $attr['LANG']);
  1305. }
  1306. //===============================================
  1307. // Block properties which are inherited
  1308. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['margin_collapse']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['margin_collapse']) {
  1309. $p['MARGIN-COLLAPSE'] = 'COLLAPSE';
  1310. } // custom tag, but follows CSS principle that border-collapse is inherited
  1311. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['line_height']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['line_height']) {
  1312. $p['LINE-HEIGHT'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['line_height'];
  1313. }
  1314. // mPDF 6
  1315. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['line_stacking_strategy']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['line_stacking_strategy']) {
  1316. $p['LINE-STACKING-STRATEGY'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['line_stacking_strategy'];
  1317. }
  1318. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['line_stacking_shift']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['line_stacking_shift']) {
  1319. $p['LINE-STACKING-SHIFT'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['line_stacking_shift'];
  1320. }
  1321. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['direction']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['direction']) {
  1322. $p['DIRECTION'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['direction'];
  1323. }
  1324. // mPDF 6 Lists
  1325. if ($tag == 'LI') {
  1326. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_type']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_type']) {
  1327. $p['LIST-STYLE-TYPE'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_type'];
  1328. }
  1329. }
  1330. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_image']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_image']) {
  1331. $p['LIST-STYLE-IMAGE'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_image'];
  1332. }
  1333. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_position']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_position']) {
  1334. $p['LIST-STYLE-POSITION'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['list_style_position'];
  1335. }
  1336. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['align']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['align']) {
  1337. if ($this->mpdf->blk[$this->mpdf->blklvl - 1]['align'] == 'L') {
  1338. $p['TEXT-ALIGN'] = 'left';
  1339. } else if ($this->mpdf->blk[$this->mpdf->blklvl - 1]['align'] == 'J') {
  1340. $p['TEXT-ALIGN'] = 'justify';
  1341. } else if ($this->mpdf->blk[$this->mpdf->blklvl - 1]['align'] == 'R') {
  1342. $p['TEXT-ALIGN'] = 'right';
  1343. } else if ($this->mpdf->blk[$this->mpdf->blklvl - 1]['align'] == 'C') {
  1344. $p['TEXT-ALIGN'] = 'center';
  1345. }
  1346. }
  1347. if ($this->mpdf->ColActive || $this->mpdf->keep_block_together) {
  1348. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['bgcolor']) && $this->mpdf->blk[$this->mpdf->blklvl - 1]['bgcolor']) { // Doesn't officially inherit, but default value is transparent (?=inherited)
  1349. $cor = $this->mpdf->blk[$this->mpdf->blklvl - 1]['bgcolorarray'];
  1350. $p['BACKGROUND-COLOR'] = $this->mpdf->_colAtoString($cor);
  1351. }
  1352. }
  1353. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['text_indent']) && ($this->mpdf->blk[$this->mpdf->blklvl - 1]['text_indent'] || $this->mpdf->blk[$this->mpdf->blklvl - 1]['text_indent'] === 0)) {
  1354. $p['TEXT-INDENT'] = $this->mpdf->blk[$this->mpdf->blklvl - 1]['text_indent'];
  1355. }
  1356. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1]['InlineProperties'])) {
  1357. $biilp = $this->mpdf->blk[$this->mpdf->blklvl - 1]['InlineProperties'];
  1358. $this->inlinePropsToCSS($biilp, $p); // mPDF 5.7.1
  1359. } else {
  1360. $biilp = null;
  1361. }
  1362. }
  1363. //===============================================
  1364. //===============================================
  1365. // INLINE HTML ATTRIBUTES e.g. .. ALIGN="CENTER">
  1366. // mPDF 6 (added)
  1367. if (isset($attr['DIR']) and $attr['DIR'] != '') {
  1368. $p['DIRECTION'] = $attr['DIR'];
  1369. }
  1370. // mPDF 6 (moved)
  1371. if (isset($attr['LANG']) and $attr['LANG'] != '') {
  1372. $p['LANG'] = $attr['LANG'];
  1373. }
  1374. if (isset($attr['COLOR']) and $attr['COLOR'] != '') {
  1375. $p['COLOR'] = $attr['COLOR'];
  1376. }
  1377. if ($tag != 'INPUT') {
  1378. if (isset($attr['WIDTH']) and $attr['WIDTH'] != '') {
  1379. $p['WIDTH'] = $attr['WIDTH'];
  1380. }
  1381. if (isset($attr['HEIGHT']) and $attr['HEIGHT'] != '') {
  1382. $p['HEIGHT'] = $attr['HEIGHT'];
  1383. }
  1384. }
  1385. if ($tag == 'FONT') {
  1386. if (isset($attr['FACE'])) {
  1387. $p['FONT-FAMILY'] = $attr['FACE'];
  1388. }
  1389. if (isset($attr['SIZE']) and $attr['SIZE'] != '') {
  1390. $s = '';
  1391. if ($attr['SIZE'] === '+1') {
  1392. $s = '120%';
  1393. } else if ($attr['SIZE'] === '-1') {
  1394. $s = '86%';
  1395. } else if ($attr['SIZE'] === '1') {
  1396. $s = 'XX-SMALL';
  1397. } else if ($attr['SIZE'] == '2') {
  1398. $s = 'X-SMALL';
  1399. } else if ($attr['SIZE'] == '3') {
  1400. $s = 'SMALL';
  1401. } else if ($attr['SIZE'] == '4') {
  1402. $s = 'MEDIUM';
  1403. } else if ($attr['SIZE'] == '5') {
  1404. $s = 'LARGE';
  1405. } else if ($attr['SIZE'] == '6') {
  1406. $s = 'X-LARGE';
  1407. } else if ($attr['SIZE'] == '7') {
  1408. $s = 'XX-LARGE';
  1409. }
  1410. if ($s)
  1411. $p['FONT-SIZE'] = $s;
  1412. }
  1413. }
  1414. if (isset($attr['VALIGN']) and $attr['VALIGN'] != '') {
  1415. $p['VERTICAL-ALIGN'] = $attr['VALIGN'];
  1416. }
  1417. if (isset($attr['VSPACE']) and $attr['VSPACE'] != '') {
  1418. $p['MARGIN-TOP'] = $attr['VSPACE'];
  1419. $p['MARGIN-BOTTOM'] = $attr['VSPACE'];
  1420. }
  1421. if (isset($attr['HSPACE']) and $attr['HSPACE'] != '') {
  1422. $p['MARGIN-LEFT'] = $attr['HSPACE'];
  1423. $p['MARGIN-RIGHT'] = $attr['HSPACE'];
  1424. }
  1425. //===============================================
  1426. //===============================================
  1427. // DEFAULT for this TAG set in DefaultCSS
  1428. if (isset($this->mpdf->defaultCSS[$tag])) {
  1429. $zp = $this->fixCSS($this->mpdf->defaultCSS[$tag]);
  1430. if (is_array($zp)) { // Default overwrites Inherited
  1431. $p = array_merge($p, $zp); // !! Note other way round !!
  1432. $this->_mergeBorders($p, $zp);
  1433. }
  1434. }
  1435. //===============================================
  1436. /* -- TABLES -- */
  1437. // mPDF 5.7.3
  1438. // cellSpacing overwrites TABLE default but not specific CSS set on table
  1439. if ($tag == 'TABLE' && isset($attr['CELLSPACING'])) {
  1440. $p['BORDER-SPACING-H'] = $p['BORDER-SPACING-V'] = $attr['CELLSPACING'];
  1441. }
  1442. // cellPadding overwrites TD/TH default but not specific CSS set on cell
  1443. if (($tag == 'TD' || $tag == 'TH') && isset($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cell_padding']) && ($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cell_padding'] || $this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cell_padding'] === '0')) { // mPDF 5.7.3
  1444. $p['PADDING-LEFT'] = $this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cell_padding'];
  1445. $p['PADDING-RIGHT'] = $this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cell_padding'];
  1446. $p['PADDING-TOP'] = $this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cell_padding'];
  1447. $p['PADDING-BOTTOM'] = $this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['cell_padding'];
  1448. }
  1449. /* -- END TABLES -- */
  1450. //===============================================
  1451. // STYLESHEET TAG e.g. h1 p div table
  1452. if (isset($this->CSS[$tag]) && $this->CSS[$tag]) {
  1453. $zp = $this->CSS[$tag];
  1454. if ($tag == 'TD' || $tag == 'TH') {
  1455. $this->setBorderDominance($zp, 9);
  1456. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1457. if (is_array($zp)) {
  1458. $p = array_merge($p, $zp);
  1459. $this->_mergeBorders($p, $zp);
  1460. }
  1461. }
  1462. //===============================================
  1463. // STYLESHEET CLASS e.g. .smallone{} .redletter{}
  1464. foreach ($classes AS $class) {
  1465. $zp = array();
  1466. if (isset($this->CSS['CLASS>>' . $class]) && $this->CSS['CLASS>>' . $class]) {
  1467. $zp = $this->CSS['CLASS>>' . $class];
  1468. }
  1469. if ($tag == 'TD' || $tag == 'TH') {
  1470. $this->setBorderDominance($zp, 9);
  1471. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1472. if (is_array($zp)) {
  1473. $p = array_merge($p, $zp);
  1474. $this->_mergeBorders($p, $zp);
  1475. }
  1476. }
  1477. //===============================================
  1478. /* -- TABLES -- */
  1479. // STYLESHEET nth-child SELECTOR e.g. tr:nth-child(odd) td:nth-child(2n+1)
  1480. if ($tag == 'TR' || $tag == 'TD' || $tag == 'TH') {
  1481. foreach ($this->CSS AS $k => $val) {
  1482. if (preg_match('/' . $tag . '>>SELECTORNTHCHILD>>(.*)/', $k, $m)) {
  1483. $select = false;
  1484. if ($tag == 'TR') {
  1485. $row = $this->mpdf->row;
  1486. $thnr = (isset($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_thead']) ? count($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_thead']) : 0);
  1487. $tfnr = (isset($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_tfoot']) ? count($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_tfoot']) : 0);
  1488. if ($this->mpdf->tabletfoot) {
  1489. $row -= $thnr;
  1490. } else if (!$this->mpdf->tablethead) {
  1491. $row -= ($thnr + $tfnr);
  1492. }
  1493. if (preg_match('/(([\-+]?\d*)?N([\-+]\d+)?|[\-+]?\d+|ODD|EVEN)/', $m[1], $a)) { // mPDF 5.7.4
  1494. $select = $this->_nthchild($a, $row);
  1495. }
  1496. } else if ($tag == 'TD' || $tag == 'TH') {
  1497. if (preg_match('/(([\-+]?\d*)?N([\-+]\d+)?|[\-+]?\d+|ODD|EVEN)/', $m[1], $a)) { // mPDF 5.7.4
  1498. $select = $this->_nthchild($a, $this->mpdf->col);
  1499. }
  1500. }
  1501. if ($select) {
  1502. $zp = $this->CSS[$tag . '>>SELECTORNTHCHILD>>' . $m[1]];
  1503. if ($tag == 'TD' || $tag == 'TH') {
  1504. $this->setBorderDominance($zp, 9);
  1505. }
  1506. if (is_array($zp)) {
  1507. $p = array_merge($p, $zp);
  1508. $this->_mergeBorders($p, $zp);
  1509. }
  1510. }
  1511. }
  1512. }
  1513. }
  1514. /* -- END TABLES -- */
  1515. //===============================================
  1516. // STYLESHEET LANG e.g. [lang=fr]{} or :lang(fr)
  1517. if (isset($attr['LANG'])) {
  1518. if (isset($this->CSS['LANG>>' . $attr['LANG']]) && $this->CSS['LANG>>' . $attr['LANG']]) {
  1519. $zp = $this->CSS['LANG>>' . $attr['LANG']];
  1520. if ($tag == 'TD' || $tag == 'TH') {
  1521. $this->setBorderDominance($zp, 9);
  1522. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1523. if (is_array($zp)) {
  1524. $p = array_merge($p, $zp);
  1525. $this->_mergeBorders($p, $zp);
  1526. }
  1527. } else if (isset($this->CSS['LANG>>' . $shortlang]) && $this->CSS['LANG>>' . $shortlang]) {
  1528. $zp = $this->CSS['LANG>>' . $shortlang];
  1529. if ($tag == 'TD' || $tag == 'TH') {
  1530. $this->setBorderDominance($zp, 9);
  1531. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1532. if (is_array($zp)) {
  1533. $p = array_merge($p, $zp);
  1534. $this->_mergeBorders($p, $zp);
  1535. }
  1536. }
  1537. }
  1538. //===============================================
  1539. // STYLESHEET ID e.g. #smallone{} #redletter{}
  1540. if (isset($attr['ID']) && isset($this->CSS['ID>>' . $attr['ID']]) && $this->CSS['ID>>' . $attr['ID']]) {
  1541. $zp = $this->CSS['ID>>' . $attr['ID']];
  1542. if ($tag == 'TD' || $tag == 'TH') {
  1543. $this->setBorderDominance($zp, 9);
  1544. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1545. if (is_array($zp)) {
  1546. $p = array_merge($p, $zp);
  1547. $this->_mergeBorders($p, $zp);
  1548. }
  1549. }
  1550. //===============================================
  1551. // STYLESHEET CLASS e.g. p.smallone{} div.redletter{}
  1552. foreach ($classes AS $class) {
  1553. $zp = array();
  1554. if (isset($this->CSS[$tag . '>>CLASS>>' . $class]) && $this->CSS[$tag . '>>CLASS>>' . $class]) {
  1555. $zp = $this->CSS[$tag . '>>CLASS>>' . $class];
  1556. }
  1557. if ($tag == 'TD' || $tag == 'TH') {
  1558. $this->setBorderDominance($zp, 9);
  1559. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1560. if (is_array($zp)) {
  1561. $p = array_merge($p, $zp);
  1562. $this->_mergeBorders($p, $zp);
  1563. }
  1564. }
  1565. //===============================================
  1566. // STYLESHEET LANG e.g. [lang=fr]{} or :lang(fr)
  1567. if (isset($attr['LANG'])) {
  1568. if (isset($this->CSS[$tag . '>>LANG>>' . $attr['LANG']]) && $this->CSS[$tag . '>>LANG>>' . $attr['LANG']]) {
  1569. $zp = $this->CSS[$tag . '>>LANG>>' . $attr['LANG']];
  1570. if ($tag == 'TD' || $tag == 'TH') {
  1571. $this->setBorderDominance($zp, 9);
  1572. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1573. if (is_array($zp)) {
  1574. $p = array_merge($p, $zp);
  1575. $this->_mergeBorders($p, $zp);
  1576. }
  1577. } else if (isset($this->CSS[$tag . '>>LANG>>' . $shortlang]) && $this->CSS[$tag . '>>LANG>>' . $shortlang]) {
  1578. $zp = $this->CSS[$tag . '>>LANG>>' . $shortlang];
  1579. if ($tag == 'TD' || $tag == 'TH') {
  1580. $this->setBorderDominance($zp, 9);
  1581. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1582. if (is_array($zp)) {
  1583. $p = array_merge($p, $zp);
  1584. $this->_mergeBorders($p, $zp);
  1585. }
  1586. }
  1587. }
  1588. //===============================================
  1589. // STYLESHEET CLASS e.g. p#smallone{} div#redletter{}
  1590. if (isset($attr['ID']) && isset($this->CSS[$tag . '>>ID>>' . $attr['ID']]) && $this->CSS[$tag . '>>ID>>' . $attr['ID']]) {
  1591. $zp = $this->CSS[$tag . '>>ID>>' . $attr['ID']];
  1592. if ($tag == 'TD' || $tag == 'TH') {
  1593. $this->setBorderDominance($zp, 9);
  1594. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1595. if (is_array($zp)) {
  1596. $p = array_merge($p, $zp);
  1597. $this->_mergeBorders($p, $zp);
  1598. }
  1599. }
  1600. //===============================================
  1601. // Cascaded e.g. div.class p only works for block level
  1602. if ($inherit == 'BLOCK') {
  1603. if (isset($this->mpdf->blk[$this->mpdf->blklvl - 1])) { // mPDF 6
  1604. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS'][$tag], $p);
  1605. foreach ($classes AS $class) {
  1606. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS']['CLASS>>' . $class], $p);
  1607. }
  1608. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS']['ID>>' . $attr['ID']], $p);
  1609. foreach ($classes AS $class) {
  1610. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS'][$tag . '>>CLASS>>' . $class], $p);
  1611. }
  1612. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl - 1]['cascadeCSS'][$tag . '>>ID>>' . $attr['ID']], $p);
  1613. }
  1614. } else if ($inherit == 'INLINE') {
  1615. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'][$tag], $p);
  1616. foreach ($classes AS $class) {
  1617. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS']['CLASS>>' . $class], $p);
  1618. }
  1619. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS']['ID>>' . $attr['ID']], $p);
  1620. foreach ($classes AS $class) {
  1621. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'][$tag . '>>CLASS>>' . $class], $p);
  1622. }
  1623. $this->_set_mergedCSS($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'][$tag . '>>ID>>' . $attr['ID']], $p);
  1624. }
  1625. /* -- TABLES -- */ else if ($inherit == 'TOPTABLE' || $inherit == 'TABLE') { // NB looks at $this->tablecascadeCSS-1 for cascading CSS
  1626. if (isset($this->tablecascadeCSS[$this->tbCSSlvl - 1])) { // mPDF 6
  1627. // false, 9 = don't check for 'depth' and do set border dominance
  1628. $this->_set_mergedCSS($this->tablecascadeCSS[$this->tbCSSlvl - 1][$tag], $p, false, 9);
  1629. foreach ($classes AS $class) {
  1630. $this->_set_mergedCSS($this->tablecascadeCSS[$this->tbCSSlvl - 1]['CLASS>>' . $class], $p, false, 9);
  1631. }
  1632. // STYLESHEET nth-child SELECTOR e.g. tr:nth-child(odd) td:nth-child(2n+1)
  1633. if ($tag == 'TR' || $tag == 'TD' || $tag == 'TH') {
  1634. foreach ($this->tablecascadeCSS[$this->tbCSSlvl - 1] AS $k => $val) {
  1635. if (preg_match('/' . $tag . '>>SELECTORNTHCHILD>>(.*)/', $k, $m)) {
  1636. $select = false;
  1637. if ($tag == 'TR') {
  1638. $row = $this->mpdf->row;
  1639. $thnr = (isset($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_thead']) ? count($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_thead']) : 0);
  1640. $tfnr = (isset($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_tfoot']) ? count($this->mpdf->table[$this->mpdf->tableLevel][$this->mpdf->tbctr[$this->mpdf->tableLevel]]['is_tfoot']) : 0);
  1641. if ($this->mpdf->tabletfoot) {
  1642. $row -= $thnr;
  1643. } else if (!$this->mpdf->tablethead) {
  1644. $row -= ($thnr + $tfnr);
  1645. }
  1646. if (preg_match('/(([\-+]?\d*)?N([\-+]\d+)?|[\-+]?\d+|ODD|EVEN)/', $m[1], $a)) { // mPDF 5.7.4
  1647. $select = $this->_nthchild($a, $row);
  1648. }
  1649. } else if ($tag == 'TD' || $tag == 'TH') {
  1650. if (preg_match('/(([\-+]?\d*)?N([\-+]\d+)?|[\-+]?\d+|ODD|EVEN)/', $m[1], $a)) { // mPDF 5.7.4
  1651. $select = $this->_nthchild($a, $this->mpdf->col);
  1652. }
  1653. }
  1654. if ($select) {
  1655. $this->_set_mergedCSS($this->tablecascadeCSS[$this->tbCSSlvl - 1][$tag . '>>SELECTORNTHCHILD>>' . $m[1]], $p, false, 9);
  1656. }
  1657. }
  1658. }
  1659. }
  1660. }
  1661. $this->_set_mergedCSS($this->tablecascadeCSS[$this->tbCSSlvl - 1]['ID>>' . $attr['ID']], $p, false, 9);
  1662. foreach ($classes AS $class) {
  1663. $this->_set_mergedCSS($this->tablecascadeCSS[$this->tbCSSlvl - 1][$tag . '>>CLASS>>' . $class], $p, false, 9);
  1664. }
  1665. $this->_set_mergedCSS($this->tablecascadeCSS[$this->tbCSSlvl - 1][$tag . '>>ID>>' . $attr['ID']], $p, false, 9);
  1666. }
  1667. /* -- END TABLES -- */
  1668. //===============================================
  1669. //===============================================
  1670. // INLINE STYLE e.g. style="CSS:property"
  1671. if (isset($attr['STYLE'])) {
  1672. $zp = $this->readInlineCSS($attr['STYLE']);
  1673. if ($tag == 'TD' || $tag == 'TH') {
  1674. $this->setBorderDominance($zp, 9);
  1675. } // *TABLES* // *TABLES-ADVANCED-BORDERS*
  1676. if (is_array($zp)) {
  1677. $p = array_merge($p, $zp);
  1678. $this->_mergeBorders($p, $zp);
  1679. }
  1680. }
  1681. //===============================================
  1682. //===============================================
  1683. return $p;
  1684. }
  1685. // Convert inline Properties back to CSS
  1686. function inlinePropsToCSS($bilp, &$p)
  1687. {
  1688. if (isset($bilp['family']) && $bilp['family']) {
  1689. $p['FONT-FAMILY'] = $bilp['family'];
  1690. }
  1691. if (isset($bilp['I']) && $bilp['I']) {
  1692. $p['FONT-STYLE'] = 'italic';
  1693. }
  1694. if (isset($bilp['sizePt']) && $bilp['sizePt']) {
  1695. $p['FONT-SIZE'] = $bilp['sizePt'] . 'pt';
  1696. }
  1697. if (isset($bilp['B']) && $bilp['B']) {
  1698. $p['FONT-WEIGHT'] = 'bold';
  1699. }
  1700. if (isset($bilp['colorarray']) && $bilp['colorarray']) {
  1701. $cor = $bilp['colorarray'];
  1702. $p['COLOR'] = $this->mpdf->_colAtoString($cor);
  1703. }
  1704. if (isset($bilp['lSpacingCSS']) && $bilp['lSpacingCSS']) {
  1705. $p['LETTER-SPACING'] = $bilp['lSpacingCSS'];
  1706. }
  1707. if (isset($bilp['wSpacingCSS']) && $bilp['wSpacingCSS']) {
  1708. $p['WORD-SPACING'] = $bilp['wSpacingCSS'];
  1709. }
  1710. if (isset($bilp['textparam']) && $bilp['textparam']) {
  1711. if (isset($bilp['textparam']['hyphens'])) {
  1712. if ($bilp['textparam']['hyphens'] == 2) {
  1713. $p['HYPHENS'] = 'none';
  1714. }
  1715. if ($bilp['textparam']['hyphens'] == 1) {
  1716. $p['HYPHENS'] = 'auto';
  1717. }
  1718. if ($bilp['textparam']['hyphens'] == 0) {
  1719. $p['HYPHENS'] = 'manual';
  1720. }
  1721. }
  1722. if (isset($bilp['textparam']['outline-s']) && !$bilp['textparam']['outline-s']) {
  1723. $p['TEXT-OUTLINE'] = 'none';
  1724. }
  1725. if (isset($bilp['textparam']['outline-COLOR']) && $bilp['textparam']['outline-COLOR']) {
  1726. $p['TEXT-OUTLINE-COLOR'] = $this->mpdf->_colAtoString($bilp['textparam']['outline-COLOR']);
  1727. }
  1728. if (isset($bilp['textparam']['outline-WIDTH']) && $bilp['textparam']['outline-WIDTH']) {
  1729. $p['TEXT-OUTLINE-WIDTH'] = $bilp['textparam']['outline-WIDTH'] . 'mm';
  1730. }
  1731. }
  1732. if (isset($bilp['textvar']) && $bilp['textvar']) {
  1733. // CSS says text-decoration is not inherited, but IE7 does??
  1734. if ($bilp['textvar'] & FD_LINETHROUGH) {
  1735. if ($bilp['textvar'] & FD_UNDERLINE) {
  1736. $p['TEXT-DECORATION'] = 'underline line-through';
  1737. } else {
  1738. $p['TEXT-DECORATION'] = 'line-through';
  1739. }
  1740. } else if ($bilp['textvar'] & FD_UNDERLINE) {
  1741. $p['TEXT-DECORATION'] = 'underline';
  1742. } else {
  1743. $p['TEXT-DECORATION'] = 'none';
  1744. }
  1745. if ($bilp['textvar'] & FA_SUPERSCRIPT) {
  1746. $p['VERTICAL-ALIGN'] = 'super';
  1747. } else if ($bilp['textvar'] & FA_SUBSCRIPT) {
  1748. $p['VERTICAL-ALIGN'] = 'sub';
  1749. } else {
  1750. $p['VERTICAL-ALIGN'] = 'baseline';
  1751. }
  1752. if ($bilp['textvar'] & FT_CAPITALIZE) {
  1753. $p['TEXT-TRANSFORM'] = 'capitalize';
  1754. } else if ($bilp['textvar'] & FT_UPPERCASE) {
  1755. $p['TEXT-TRANSFORM'] = 'uppercase';
  1756. } else if ($bilp['textvar'] & FT_LOWERCASE) {
  1757. $p['TEXT-TRANSFORM'] = 'lowercase';
  1758. } else {
  1759. $p['TEXT-TRANSFORM'] = 'none';
  1760. }
  1761. if ($bilp['textvar'] & FC_KERNING) {
  1762. $p['FONT-KERNING'] = 'normal';
  1763. } // ignore 'auto' as default already applied
  1764. //if (isset($bilp[ 'OTLtags' ]) && $bilp[ 'OTLtags' ]['Plus'] contains 'kern'
  1765. else {
  1766. $p['FONT-KERNING'] = 'none';
  1767. }
  1768. if ($bilp['textvar'] & FA_SUPERSCRIPT) {
  1769. $p['FONT-VARIANT-POSITION'] = 'super';
  1770. }
  1771. //if (isset($bilp[ 'OTLtags' ]) && $bilp[ 'OTLtags' ]['Plus'] contains 'sups' / 'subs'
  1772. else if ($bilp['textvar'] & FA_SUBSCRIPT) {
  1773. $p['FONT-VARIANT-POSITION'] = 'sub';
  1774. } else {
  1775. $p['FONT-VARIANT-POSITION'] = 'normal';
  1776. }
  1777. if ($bilp['textvar'] & FC_SMALLCAPS) {
  1778. $p['FONT-VARIANT-CAPS'] = 'small-caps';
  1779. }
  1780. }
  1781. if (isset($bilp['fontLanguageOverride'])) {
  1782. if ($bilp['fontLanguageOverride']) {
  1783. $p['FONT-LANGUAGE-OVERRIDE'] = $bilp['fontLanguageOverride'];
  1784. } else {
  1785. $p['FONT-LANGUAGE-OVERRIDE'] = 'normal';
  1786. }
  1787. }
  1788. // All the variations of font-variant-* we are going to set as font-feature-settings...
  1789. if (isset($bilp['OTLtags']) && $bilp['OTLtags']) {
  1790. $ffs = array();
  1791. if (isset($bilp['OTLtags']['Minus']) && $bilp['OTLtags']['Minus']) {
  1792. $f = preg_split('/\s+/', trim($bilp['OTLtags']['Minus']));
  1793. foreach ($f AS $ff) {
  1794. $ffs[] = "'" . $ff . "' 0";
  1795. }
  1796. }
  1797. if (isset($bilp['OTLtags']['FFMinus']) && $bilp['OTLtags']['FFMinus']) {
  1798. $f = preg_split('/\s+/', trim($bilp['OTLtags']['FFMinus']));
  1799. foreach ($f AS $ff) {
  1800. $ffs[] = "'" . $ff . "' 0";
  1801. }
  1802. }
  1803. if (isset($bilp['OTLtags']['Plus']) && $bilp['OTLtags']['Plus']) {
  1804. $f = preg_split('/\s+/', trim($bilp['OTLtags']['Plus']));
  1805. foreach ($f AS $ff) {
  1806. $ffs[] = "'" . $ff . "' 1";
  1807. }
  1808. }
  1809. if (isset($bilp['OTLtags']['FFPlus']) && $bilp['OTLtags']['FFPlus']) { // May contain numeric value e.g. salt4
  1810. $f = preg_split('/\s+/', trim($bilp['OTLtags']['FFPlus']));
  1811. foreach ($f AS $ff) {
  1812. if (strlen($ff) > 4) {
  1813. $ffs[] = "'" . substr($ff, 0, 4) . "' " . substr($ff, 4);
  1814. } else {
  1815. $ffs[] = "'" . $ff . "' 1";
  1816. }
  1817. }
  1818. }
  1819. $p['FONT-FEATURE-SETTINGS'] = implode(', ', $ffs);
  1820. }
  1821. }
  1822. function PreviewBlockCSS($tag, $attr)
  1823. {
  1824. // Looks ahead from current block level to a new level
  1825. $p = array();
  1826. $zp = array();
  1827. $oldcascadeCSS = $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'];
  1828. $classes = array();
  1829. if (isset($attr['CLASS'])) {
  1830. $classes = preg_split('/\s+/', $attr['CLASS']);
  1831. }
  1832. //===============================================
  1833. // DEFAULT for this TAG set in DefaultCSS
  1834. if (isset($this->mpdf->defaultCSS[$tag])) {
  1835. $zp = $this->fixCSS($this->mpdf->defaultCSS[$tag]);
  1836. if (is_array($zp)) {
  1837. $p = array_merge($zp, $p);
  1838. } // Inherited overwrites default
  1839. }
  1840. // STYLESHEET TAG e.g. h1 p div table
  1841. if (isset($this->CSS[$tag])) {
  1842. $zp = $this->CSS[$tag];
  1843. if (is_array($zp)) {
  1844. $p = array_merge($p, $zp);
  1845. }
  1846. }
  1847. // STYLESHEET CLASS e.g. .smallone{} .redletter{}
  1848. foreach ($classes AS $class) {
  1849. $zp = array();
  1850. if (isset($this->CSS['CLASS>>' . $class])) {
  1851. $zp = $this->CSS['CLASS>>' . $class];
  1852. }
  1853. if (is_array($zp)) {
  1854. $p = array_merge($p, $zp);
  1855. }
  1856. }
  1857. // STYLESHEET ID e.g. #smallone{} #redletter{}
  1858. if (isset($attr['ID']) && isset($this->CSS['ID>>' . $attr['ID']])) {
  1859. $zp = $this->CSS['ID>>' . $attr['ID']];
  1860. if (is_array($zp)) {
  1861. $p = array_merge($p, $zp);
  1862. }
  1863. }
  1864. // STYLESHEET CLASS e.g. p.smallone{} div.redletter{}
  1865. foreach ($classes AS $class) {
  1866. $zp = array();
  1867. if (isset($this->CSS[$tag . '>>CLASS>>' . $class])) {
  1868. $zp = $this->CSS[$tag . '>>CLASS>>' . $class];
  1869. }
  1870. if (is_array($zp)) {
  1871. $p = array_merge($p, $zp);
  1872. }
  1873. }
  1874. // STYLESHEET CLASS e.g. p#smallone{} div#redletter{}
  1875. if (isset($attr['ID']) && isset($this->CSS[$tag . '>>ID>>' . $attr['ID']])) {
  1876. $zp = $this->CSS[$tag . '>>ID>>' . $attr['ID']];
  1877. if (is_array($zp)) {
  1878. $p = array_merge($p, $zp);
  1879. }
  1880. }
  1881. //===============================================
  1882. // STYLESHEET TAG e.g. div h1 div p
  1883. $this->_set_mergedCSS($oldcascadeCSS[$tag], $p);
  1884. // STYLESHEET CLASS e.g. .smallone{} .redletter{}
  1885. foreach ($classes AS $class) {
  1886. $this->_set_mergedCSS($oldcascadeCSS['CLASS>>' . $class], $p);
  1887. }
  1888. // STYLESHEET CLASS e.g. #smallone{} #redletter{}
  1889. if (isset($attr['ID'])) {
  1890. $this->_set_mergedCSS($oldcascadeCSS['ID>>' . $attr['ID']], $p);
  1891. }
  1892. // STYLESHEET CLASS e.g. div.smallone{} p.redletter{}
  1893. foreach ($classes AS $class) {
  1894. $this->_set_mergedCSS($oldcascadeCSS[$tag . '>>CLASS>>' . $class], $p);
  1895. }
  1896. // STYLESHEET CLASS e.g. div#smallone{} p#redletter{}
  1897. if (isset($attr['ID'])) {
  1898. $this->_set_mergedCSS($oldcascadeCSS[$tag . '>>ID>>' . $attr['ID']], $p);
  1899. }
  1900. //===============================================
  1901. // INLINE STYLE e.g. style="CSS:property"
  1902. if (isset($attr['STYLE'])) {
  1903. $zp = $this->readInlineCSS($attr['STYLE']);
  1904. if (is_array($zp)) {
  1905. $p = array_merge($p, $zp);
  1906. }
  1907. }
  1908. //===============================================
  1909. return $p;
  1910. }
  1911. // mPDF 5.7.4 nth-child
  1912. function _nthchild($f, $c)
  1913. {
  1914. // $f is formual e.g. 2N+1 spilt into a preg_match array
  1915. // $c is the comparator value e.g row or column number
  1916. $c += 1;
  1917. $select = false;
  1918. $a = 1;
  1919. $b = 1;
  1920. if ($f[0] == 'ODD') {
  1921. $a = 2;
  1922. $b = 1;
  1923. } else if ($f[0] == 'EVEN') {
  1924. $a = 2;
  1925. $b = 0;
  1926. } else if (count($f) == 2) {
  1927. $a = 0;
  1928. $b = $f[1] + 0;
  1929. } // e.g. (+6)
  1930. else if (count($f) == 3) { // e.g. (2N)
  1931. if ($f[2] == '') {
  1932. $a = 1;
  1933. } else if ($f[2] == '-') {
  1934. $a = -1;
  1935. } else {
  1936. $a = $f[2] + 0;
  1937. }
  1938. $b = 0;
  1939. } else if (count($f) == 4) { // e.g. (2N+6)
  1940. if ($f[2] == '') {
  1941. $a = 1;
  1942. } else if ($f[2] == '-') {
  1943. $a = -1;
  1944. } else {
  1945. $a = $f[2] + 0;
  1946. }
  1947. $b = $f[3] + 0;
  1948. } else {
  1949. return false;
  1950. }
  1951. if ($a > 0) {
  1952. if (((($c % $a) - $b) % $a) == 0 && $c >= $b) {
  1953. $select = true;
  1954. }
  1955. } else if ($a == 0) {
  1956. if ($c == $b) {
  1957. $select = true;
  1958. }
  1959. } else { // if ($a<0)
  1960. if (((($c % $a) - $b) % $a) == 0 && $c <= $b) {
  1961. $select = true;
  1962. }
  1963. }
  1964. return $select;
  1965. }
  1966. }