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

/myCore/lib/mpdf/classes/cssmgr.php

https://gitlab.com/fabian.morales/Spencial
PHP | 1229 lines | 1042 code | 75 blank | 112 comment | 632 complexity | bd0cd401e6c8dc87f4e85a76e21a5d86 MD5 | raw file
  1. <?php
  2. class cssmgr {
  3. var $mpdf = null;
  4. var $tablecascadeCSS;
  5. var $listcascadeCSS;
  6. var $cascadeCSS;
  7. var $CSS;
  8. var $tbCSSlvl;
  9. var $listCSSlvl;
  10. function cssmgr(&$mpdf) {
  11. $this->mpdf = $mpdf;
  12. $this->tablecascadeCSS = array();
  13. $this->listcascadeCSS = array();
  14. $this->CSS=array();
  15. $this->cascadeCSS = array();
  16. $this->tbCSSlvl = 0;
  17. $this->listCSSlvl = 0;
  18. }
  19. function ReadDefaultCSS($CSSstr) {
  20. $CSS = array();
  21. $CSSstr = preg_replace('|/\*.*?\*/|s',' ',$CSSstr);
  22. $CSSstr = preg_replace('/[\s\n\r\t\f]/s',' ',$CSSstr);
  23. $CSSstr = preg_replace('/(<\!\-\-|\-\->)/s',' ',$CSSstr);
  24. if ($CSSstr ) {
  25. preg_match_all('/(.*?)\{(.*?)\}/',$CSSstr,$styles);
  26. for($i=0; $i < count($styles[1]) ; $i++) {
  27. $stylestr= trim($styles[2][$i]);
  28. $stylearr = explode(';',$stylestr);
  29. foreach($stylearr AS $sta) {
  30. if (trim($sta)) {
  31. // Changed to allow style="background: url('http://www.bpm1.com/bg.jpg')"
  32. list($property,$value) = explode(':',$sta,2);
  33. $property = trim($property);
  34. $value = preg_replace('/\s*!important/i','',$value);
  35. $value = trim($value);
  36. if ($property && ($value || $value==='0')) {
  37. $classproperties[strtoupper($property)] = $value;
  38. }
  39. }
  40. }
  41. $classproperties = $this->fixCSS($classproperties);
  42. $tagstr = strtoupper(trim($styles[1][$i]));
  43. $tagarr = explode(',',$tagstr);
  44. foreach($tagarr AS $tg) {
  45. $tags = preg_split('/\s+/',trim($tg));
  46. $level = count($tags);
  47. if ($level == 1) { // e.g. p or .class or #id or p.class or p#id
  48. $t = trim($tags[0]);
  49. if ($t) {
  50. $tag = '';
  51. if (preg_match('/^('.$this->mpdf->allowedCSStags.')$/',$t)) { $tag= $t; }
  52. if ($this->CSS[$tag] && $tag) { $CSS[$tag] = $this->array_merge_recursive_unique($CSS[$tag], $classproperties); }
  53. else if ($tag) { $CSS[$tag] = $classproperties; }
  54. }
  55. }
  56. }
  57. $properties = array();
  58. $values = array();
  59. $classproperties = array();
  60. }
  61. } // end of if
  62. return $CSS;
  63. }
  64. function ReadCSS($html) {
  65. preg_match_all('/<style[^>]*media=["\']([^"\'>]*)["\'].*?<\/style>/is',$html,$m);
  66. for($i=0; $i<count($m[0]); $i++) {
  67. if ($this->mpdf->CSSselectMedia && !preg_match('/('.trim($this->mpdf->CSSselectMedia).'|all)/i',$m[1][$i])) {
  68. $html = preg_replace('/'.preg_quote($m[0][$i],'/').'/','',$html);
  69. }
  70. }
  71. preg_match_all('/<link[^>]*media=["\']([^"\'>]*)["\'].*?>/is',$html,$m);
  72. for($i=0; $i<count($m[0]); $i++) {
  73. if ($this->mpdf->CSSselectMedia && !preg_match('/('.trim($this->mpdf->CSSselectMedia).'|all)/i',$m[1][$i])) {
  74. $html = preg_replace('/'.preg_quote($m[0][$i],'/').'/','',$html);
  75. }
  76. }
  77. // mPDF 5.5.02
  78. // Remove Comment tags <!-- ... --> inside CSS as <style> in HTML document
  79. // Remove Comment tags /* ... */ inside CSS as <style> in HTML document
  80. // But first, we replace upper and mixed case closing style tag with lower
  81. // case so we can use str_replace later.
  82. preg_replace('/<\/style>/i', '</style>', $html);
  83. preg_match_all('/<style.*?>(.*?)<\/style>/si',$html,$m);
  84. if (count($m[1])) {
  85. for($i=0;$i<count($m[1]);$i++) {
  86. // Remove comment tags
  87. $sub = preg_replace('/(<\!\-\-|\-\->)/s',' ',$m[1][$i]);
  88. $sub = '>'.preg_replace('|/\*.*?\*/|s',' ',$sub).'</style>';
  89. $html = str_replace('>'.$m[1][$i].'</style>', $sub, $html);
  90. }
  91. }
  92. $html = preg_replace('/<!--mpdf/i','',$html);
  93. $html = preg_replace('/mpdf-->/i','',$html);
  94. $html = preg_replace('/<\!\-\-.*?\-\->/s',' ',$html);
  95. $match = 0; // no match for instance
  96. $regexp = ''; // This helps debugging: showing what is the REAL string being processed
  97. $CSSext = array();
  98. //CSS inside external files
  99. $regexp = '/<link[^>]*rel=["\']stylesheet["\'][^>]*href=["\']([^>"\']*)["\'].*?>/si';
  100. $x = preg_match_all($regexp,$html,$cxt);
  101. if ($x) {
  102. $match += $x;
  103. $CSSext = $cxt[1];
  104. }
  105. $regexp = '/<link[^>]*href=["\']([^>"\']*)["\'][^>]*?rel=["\']stylesheet["\'].*?>/si';
  106. $x = preg_match_all($regexp,$html,$cxt);
  107. if ($x) {
  108. $match += $x;
  109. $CSSext = array_merge($CSSext,$cxt[1]);
  110. }
  111. // look for @import stylesheets
  112. //$regexp = '/@import url\([\'\"]{0,1}([^\)]*?\.css)[\'\"]{0,1}\)/si';
  113. $regexp = '/@import url\([\'\"]{0,1}([^\)]*?\.css(\?\S+)?)[\'\"]{0,1}\)/si';
  114. $x = preg_match_all($regexp,$html,$cxt);
  115. if ($x) {
  116. $match += $x;
  117. $CSSext = array_merge($CSSext,$cxt[1]);
  118. }
  119. // look for @import without the url()
  120. //$regexp = '/@import [\'\"]{0,1}([^;]*?\.css)[\'\"]{0,1}/si';
  121. $regexp = '/@import [\'\"]{0,1}([^;]*?\.css(\?\S+)?)[\'\"]{0,1}/si';
  122. $x = preg_match_all($regexp,$html,$cxt);
  123. if ($x) {
  124. $match += $x;
  125. $CSSext = array_merge($CSSext,$cxt[1]);
  126. }
  127. $ind = 0;
  128. $CSSstr = '';
  129. if (!is_array($this->cascadeCSS)) $this->cascadeCSS = array();
  130. while($match){
  131. $path = $CSSext[$ind];
  132. $this->mpdf->GetFullPath($path);
  133. $CSSextblock = $this->mpdf->_get_file($path);
  134. if ($CSSextblock) {
  135. // look for embedded @import stylesheets in other stylesheets
  136. // and fix url paths (including background-images) relative to stylesheet
  137. //$regexpem = '/@import url\([\'\"]{0,1}(.*?\.css)[\'\"]{0,1}\)/si';
  138. $regexpem = '/@import url\([\'\"]{0,1}(.*?\.css(\?\S+)?)[\'\"]{0,1}\)/si';
  139. $xem = preg_match_all($regexpem,$CSSextblock,$cxtem);
  140. $cssBasePath = preg_replace('/\/[^\/]*$/','',$path) . '/';
  141. if ($xem) {
  142. foreach($cxtem[1] AS $cxtembedded) {
  143. // path is relative to original stlyesheet!!
  144. $this->mpdf->GetFullPath($cxtembedded, $cssBasePath );
  145. $match++;
  146. $CSSext[] = $cxtembedded;
  147. }
  148. }
  149. $regexpem = '/(background[^;]*url\s*\(\s*[\'\"]{0,1})([^\)\'\"]*)([\'\"]{0,1}\s*\))/si';
  150. $xem = preg_match_all($regexpem,$CSSextblock,$cxtem);
  151. if ($xem) {
  152. for ($i=0;$i<count($cxtem[0]);$i++) {
  153. // path is relative to original stlyesheet!!
  154. $embedded = $cxtem[2][$i];
  155. if (!preg_match('/^data:image/i', $embedded)) { // mPDF 5.5.13
  156. $this->mpdf->GetFullPath($embedded, $cssBasePath );
  157. $CSSextblock = preg_replace('/'.preg_quote($cxtem[0][$i],'/').'/', ($cxtem[1][$i].$embedded.$cxtem[3][$i]), $CSSextblock);
  158. }
  159. }
  160. }
  161. $CSSstr .= ' '.$CSSextblock;
  162. }
  163. $match--;
  164. $ind++;
  165. } //end of match
  166. $match = 0; // reset value, if needed
  167. // CSS as <style> in HTML document
  168. $regexp = '/<style.*?>(.*?)<\/style>/si';
  169. $match = preg_match_all($regexp,$html,$CSSblock);
  170. if ($match) {
  171. $tmpCSSstr = implode(' ',$CSSblock[1]);
  172. $regexpem = '/(background[^;]*url\s*\(\s*[\'\"]{0,1})([^\)\'\"]*)([\'\"]{0,1}\s*\))/si';
  173. $xem = preg_match_all($regexpem,$tmpCSSstr ,$cxtem);
  174. if ($xem) {
  175. for ($i=0;$i<count($cxtem[0]);$i++) {
  176. $embedded = $cxtem[2][$i];
  177. if (!preg_match('/^data:image/i', $embedded)) { // mPDF 5.5.13
  178. $this->mpdf->GetFullPath($embedded);
  179. $tmpCSSstr = preg_replace('/'.preg_quote($cxtem[0][$i],'/').'/', ($cxtem[1][$i].$embedded.$cxtem[3][$i]), $tmpCSSstr );
  180. }
  181. }
  182. }
  183. $CSSstr .= ' '.$tmpCSSstr;
  184. }
  185. // Remove comments
  186. $CSSstr = preg_replace('|/\*.*?\*/|s',' ',$CSSstr);
  187. $CSSstr = preg_replace('/[\s\n\r\t\f]/s',' ',$CSSstr);
  188. if (preg_match('/@media/',$CSSstr)) {
  189. preg_match_all('/@media(.*?)\{(([^\{\}]*\{[^\{\}]*\})+)\s*\}/is',$CSSstr,$m);
  190. for($i=0; $i<count($m[0]); $i++) {
  191. if ($this->mpdf->CSSselectMedia && !preg_match('/('.trim($this->mpdf->CSSselectMedia).'|all)/i',$m[1][$i])) {
  192. $CSSstr = preg_replace('/'.preg_quote($m[0][$i],'/').'/','',$CSSstr);
  193. }
  194. else {
  195. $CSSstr = preg_replace('/'.preg_quote($m[0][$i],'/').'/',' '.$m[2][$i].' ',$CSSstr);
  196. }
  197. }
  198. }
  199. // Replace any background: url(data:image... with temporary image file reference
  200. preg_match_all("/(url\(data:image\/(jpeg|gif|png);base64,(.*?)\))/si", $CSSstr, $idata); // mPDF 5.7.2
  201. if (count($idata[0])) {
  202. for($i=0;$i<count($idata[0]);$i++) {
  203. $file = _MPDF_TEMP_PATH.'_tempCSSidata'.RAND(1,10000).'_'.$i.'.'.$idata[2][$i];
  204. //Save to local file
  205. file_put_contents($file, base64_decode($idata[3][$i]));
  206. // $this->mpdf->GetFullPath($file); // ? is this needed - NO mPDF 5.6.03
  207. $CSSstr = str_replace($idata[0][$i], 'url("'.$file.'")', $CSSstr); // mPDF 5.5.17
  208. }
  209. }
  210. $CSSstr = preg_replace('/(<\!\-\-|\-\->)/s',' ',$CSSstr);
  211. if ($CSSstr ) {
  212. preg_match_all('/(.*?)\{(.*?)\}/',$CSSstr,$styles);
  213. for($i=0; $i < count($styles[1]) ; $i++) {
  214. // SET array e.g. $classproperties['COLOR'] = '#ffffff';
  215. $stylestr= trim($styles[2][$i]);
  216. $stylearr = explode(';',$stylestr);
  217. foreach($stylearr AS $sta) {
  218. if (trim($sta)) {
  219. // Changed to allow style="background: url('http://www.bpm1.com/bg.jpg')"
  220. list($property,$value) = explode(':',$sta,2);
  221. $property = trim($property);
  222. $value = preg_replace('/\s*!important/i','',$value);
  223. $value = trim($value);
  224. if ($property && ($value || $value==='0')) {
  225. // Ignores -webkit-gradient so doesn't override -moz-
  226. if ((strtoupper($property)=='BACKGROUND-IMAGE' || strtoupper($property)=='BACKGROUND') && preg_match('/-webkit-gradient/i',$value)) {
  227. continue;
  228. }
  229. $classproperties[strtoupper($property)] = $value;
  230. }
  231. }
  232. }
  233. $classproperties = $this->fixCSS($classproperties);
  234. $tagstr = strtoupper(trim($styles[1][$i]));
  235. $tagarr = explode(',',$tagstr);
  236. $pageselectors = false; // used to turn on $this->mpdf->mirrorMargins
  237. foreach($tagarr AS $tg) {
  238. $tags = preg_split('/\s+/',trim($tg));
  239. $level = count($tags);
  240. $t = '';
  241. $t2 = '';
  242. $t3 = '';
  243. if (trim($tags[0])=='@PAGE') {
  244. if (isset($tags[0])) { $t = trim($tags[0]); }
  245. if (isset($tags[1])) { $t2 = trim($tags[1]); }
  246. if (isset($tags[2])) { $t3 = trim($tags[2]); }
  247. $tag = '';
  248. if ($level==1) { $tag = $t; }
  249. else if ($level==2 && preg_match('/^[:](.*)$/',$t2,$m)) {
  250. $tag = $t.'>>PSEUDO>>'.$m[1];
  251. if ($m[1]=='LEFT' || $m[1]=='RIGHT') { $pageselectors = true; } // used to turn on $this->mpdf->mirrorMargins
  252. }
  253. else if ($level==2) { $tag = $t.'>>NAMED>>'.$t2; }
  254. else if ($level==3 && preg_match('/^[:](.*)$/',$t3,$m)) {
  255. $tag = $t.'>>NAMED>>'.$t2.'>>PSEUDO>>'.$m[1];
  256. if ($m[1]=='LEFT' || $m[1]=='RIGHT') { $pageselectors = true; } // used to turn on $this->mpdf->mirrorMargins
  257. }
  258. if (isset($this->CSS[$tag]) && $tag) { $this->CSS[$tag] = $this->array_merge_recursive_unique($this->CSS[$tag], $classproperties); }
  259. else if ($tag) { $this->CSS[$tag] = $classproperties; }
  260. }
  261. else if ($level == 1) { // e.g. p or .class or #id or p.class or p#id
  262. if (isset($tags[0])) { $t = trim($tags[0]); }
  263. if ($t) {
  264. $tag = '';
  265. if (preg_match('/^[.](.*)$/',$t,$m)) { $tag = 'CLASS>>'.$m[1]; }
  266. else if (preg_match('/^[#](.*)$/',$t,$m)) { $tag = 'ID>>'.$m[1]; }
  267. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')[.](.*)$/',$t,$m)) { $tag = $m[1].'>>CLASS>>'.$m[2]; }
  268. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')\s*:NTH-CHILD\((.*)\)$/',$t,$m)) { $tag = $m[1].'>>SELECTORNTHCHILD>>'.$m[2]; }
  269. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')[#](.*)$/',$t,$m)) { $tag = $m[1].'>>ID>>'.$m[2]; }
  270. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')$/',$t)) { $tag= $t; }
  271. if (isset($this->CSS[$tag]) && $tag) { $this->CSS[$tag] = $this->array_merge_recursive_unique($this->CSS[$tag], $classproperties); }
  272. else if ($tag) { $this->CSS[$tag] = $classproperties; }
  273. }
  274. }
  275. else {
  276. $tmp = array();
  277. for($n=0;$n<$level;$n++) {
  278. if (isset($tags[$n])) { $t = trim($tags[$n]); }
  279. else { $t = ''; }
  280. if ($t) {
  281. $tag = '';
  282. if (preg_match('/^[.](.*)$/',$t,$m)) { $tag = 'CLASS>>'.$m[1]; }
  283. else if (preg_match('/^[#](.*)$/',$t,$m)) { $tag = 'ID>>'.$m[1]; }
  284. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')[.](.*)$/',$t,$m)) { $tag = $m[1].'>>CLASS>>'.$m[2]; }
  285. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')\s*:NTH-CHILD\((.*)\)$/',$t,$m)) { $tag = $m[1].'>>SELECTORNTHCHILD>>'.$m[2]; }
  286. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')[#](.*)$/',$t,$m)) { $tag = $m[1].'>>ID>>'.$m[2]; }
  287. else if (preg_match('/^('.$this->mpdf->allowedCSStags.')$/',$t)) { $tag= $t; }
  288. if ($tag) $tmp[] = $tag;
  289. else { break; }
  290. }
  291. }
  292. if ($tag) {
  293. $x = &$this->cascadeCSS;
  294. foreach($tmp AS $tp) { $x = &$x[$tp]; }
  295. $x = $this->array_merge_recursive_unique($x, $classproperties);
  296. $x['depth'] = $level;
  297. }
  298. }
  299. }
  300. if ($pageselectors) { $this->mpdf->mirrorMargins = true; }
  301. $properties = array();
  302. $values = array();
  303. $classproperties = array();
  304. }
  305. } // end of if
  306. //Remove CSS (tags and content), if any
  307. $regexp = '/<style.*?>(.*?)<\/style>/si'; // it can be <style> or <style type="txt/css">
  308. $html = preg_replace($regexp,'',$html);
  309. //print_r($this->CSS); exit;
  310. //print_r($this->cascadeCSS); exit;
  311. return $html;
  312. }
  313. function readInlineCSS($html) {
  314. //Fix incomplete CSS code
  315. $size = strlen($html)-1;
  316. if (substr($html,$size,1) != ';') $html .= ';';
  317. //Make CSS[Name-of-the-class] = array(key => value)
  318. $regexp = '|\\s*?(\\S+?):(.+?);|i';
  319. preg_match_all( $regexp, $html, $styleinfo);
  320. $properties = $styleinfo[1];
  321. $values = $styleinfo[2];
  322. //Array-properties and Array-values must have the SAME SIZE!
  323. $classproperties = array();
  324. for($i = 0; $i < count($properties) ; $i++) {
  325. // Ignores -webkit-gradient so doesn't override -moz-
  326. if ((strtoupper($properties[$i])=='BACKGROUND-IMAGE' || strtoupper($properties[$i])=='BACKGROUND') && preg_match('/-webkit-gradient/i',$values[$i])) {
  327. continue;
  328. }
  329. $classproperties[strtoupper($properties[$i])] = trim($values[$i]);
  330. }
  331. return $this->fixCSS($classproperties);
  332. }
  333. function _fix_borderStr($bd) {
  334. preg_match_all("/\((.*?)\)/", $bd, $m);
  335. if (count($m[1])) {
  336. for($i=0;$i<count($m[1]);$i++) {
  337. $sub = preg_replace("/ /", "", $m[1][$i]);
  338. $bd = preg_replace('/'.preg_quote($m[1][$i], '/').'/si', $sub, $bd);
  339. }
  340. }
  341. $prop = preg_split('/\s+/',trim($bd));
  342. $w = 'medium';
  343. $c = '#000000';
  344. $s = 'none';
  345. if ( count($prop) == 1 ) {
  346. // solid
  347. if (in_array($prop[0],$this->mpdf->borderstyles) || $prop[0] == 'none' || $prop[0] == 'hidden' ) { $s = $prop[0]; }
  348. // #000000
  349. else if (is_array($this->mpdf->ConvertColor($prop[0]))) { $c = $prop[0]; }
  350. // 1px
  351. else { $w = $prop[0]; }
  352. }
  353. else if (count($prop) == 2 ) {
  354. // 1px solid
  355. if (in_array($prop[1],$this->mpdf->borderstyles) || $prop[1] == 'none' || $prop[1] == 'hidden' ) { $w = $prop[0]; $s = $prop[1]; }
  356. // solid #000000
  357. else if (in_array($prop[0],$this->mpdf->borderstyles) || $prop[0] == 'none' || $prop[0] == 'hidden' ) { $s = $prop[0]; $c = $prop[1]; }
  358. // 1px #000000
  359. else { $w = $prop[0]; $c = $prop[1]; }
  360. }
  361. else if ( count($prop) == 3 ) {
  362. // Change #000000 1px solid to 1px solid #000000 (proper)
  363. if (substr($prop[0],0,1) == '#') { $c = $prop[0]; $w = $prop[1]; $s = $prop[2]; }
  364. // Change solid #000000 1px to 1px solid #000000 (proper)
  365. else if (substr($prop[0],1,1) == '#') { $s = $prop[0]; $c = $prop[1]; $w = $prop[2]; }
  366. // Change solid 1px #000000 to 1px solid #000000 (proper)
  367. else if (in_array($prop[0],$this->mpdf->borderstyles) || $prop[0] == 'none' || $prop[0] == 'hidden' ) {
  368. $s = $prop[0]; $w = $prop[1]; $c = $prop[2];
  369. }
  370. else { $w = $prop[0]; $s = $prop[1]; $c = $prop[2]; }
  371. }
  372. else { return ''; }
  373. $s = strtolower($s);
  374. return $w.' '.$s.' '.$c;
  375. }
  376. function fixCSS($prop) {
  377. if (!is_array($prop) || (count($prop)==0)) return array();
  378. $newprop = array();
  379. foreach($prop AS $k => $v) {
  380. 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') {
  381. $v = strtolower($v);
  382. }
  383. if ($k == 'FONT') {
  384. $s = trim($v);
  385. preg_match_all('/\"(.*?)\"/',$s,$ff);
  386. if (count($ff[1])) {
  387. foreach($ff[1] AS $ffp) {
  388. $w = preg_split('/\s+/',$ffp);
  389. $s = preg_replace('/\"'.$ffp.'\"/',$w[0],$s);
  390. }
  391. }
  392. preg_match_all('/\'(.*?)\'/',$s,$ff);
  393. if (count($ff[1])) {
  394. foreach($ff[1] AS $ffp) {
  395. $w = preg_split('/\s+/',$ffp);
  396. $s = preg_replace('/\''.$ffp.'\'/',$w[0],$s);
  397. }
  398. }
  399. $s = preg_replace('/\s*,\s*/',',',$s);
  400. $bits = preg_split('/\s+/',$s);
  401. if (count($bits)>1) {
  402. $k = 'FONT-FAMILY'; $v = $bits[(count($bits)-1)];
  403. $fs = $bits[(count($bits)-2)];
  404. if (preg_match('/(.*?)\/(.*)/',$fs, $fsp)) {
  405. $newprop['FONT-SIZE'] = $fsp[1];
  406. $newprop['LINE-HEIGHT'] = $fsp[2];
  407. }
  408. else { $newprop['FONT-SIZE'] = $fs; }
  409. if (preg_match('/(italic|oblique)/i',$s)) { $newprop['FONT-STYLE'] = 'italic'; }
  410. else { $newprop['FONT-STYLE'] = 'normal'; }
  411. if (preg_match('/bold/i',$s)) { $newprop['FONT-WEIGHT'] = 'bold'; }
  412. else { $newprop['FONT-WEIGHT'] = 'normal'; }
  413. if (preg_match('/small-caps/i',$s)) { $newprop['TEXT-TRANSFORM'] = 'uppercase'; }
  414. }
  415. }
  416. if ($k == 'FONT-FAMILY') {
  417. $aux_fontlist = explode(",",$v);
  418. $found = 0;
  419. foreach($aux_fontlist AS $f) {
  420. $fonttype = trim($f);
  421. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  422. $fonttype = preg_replace('/ /','',$fonttype);
  423. $v = strtolower(trim($fonttype));
  424. if (isset($this->mpdf->fonttrans[$v]) && $this->mpdf->fonttrans[$v]) { $v = $this->mpdf->fonttrans[$v]; }
  425. if ((!$this->mpdf->onlyCoreFonts && in_array($v,$this->mpdf->available_unifonts)) ||
  426. in_array($v,array('ccourier','ctimes','chelvetica')) ||
  427. ($this->mpdf->onlyCoreFonts && in_array($v,array('courier','times','helvetica','arial'))) ||
  428. in_array($v, array('sjis','uhc','big5','gb'))) {
  429. $newprop[$k] = $v;
  430. $found = 1;
  431. break;
  432. }
  433. }
  434. if (!$found) {
  435. foreach($aux_fontlist AS $f) {
  436. $fonttype = trim($f);
  437. $fonttype = preg_replace('/["\']*(.*?)["\']*/','\\1',$fonttype);
  438. $fonttype = preg_replace('/ /','',$fonttype);
  439. $v = strtolower(trim($fonttype));
  440. if (isset($this->mpdf->fonttrans[$v]) && $this->mpdf->fonttrans[$v]) { $v = $this->mpdf->fonttrans[$v]; }
  441. if (in_array($v,$this->mpdf->sans_fonts) || in_array($v,$this->mpdf->serif_fonts) || in_array($v,$this->mpdf->mono_fonts) ) {
  442. $newprop[$k] = $v;
  443. break;
  444. }
  445. }
  446. }
  447. }
  448. else if ($k == 'MARGIN') {
  449. $tmp = $this->expand24($v);
  450. $newprop['MARGIN-TOP'] = $tmp['T'];
  451. $newprop['MARGIN-RIGHT'] = $tmp['R'];
  452. $newprop['MARGIN-BOTTOM'] = $tmp['B'];
  453. $newprop['MARGIN-LEFT'] = $tmp['L'];
  454. }
  455. /*-- BORDER-RADIUS --*/
  456. 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') {
  457. $tmp = $this->border_radius_expand($v,$k);
  458. if (isset($tmp['TL-H'])) $newprop['BORDER-TOP-LEFT-RADIUS-H'] = $tmp['TL-H'];
  459. if (isset($tmp['TL-V'])) $newprop['BORDER-TOP-LEFT-RADIUS-V'] = $tmp['TL-V'];
  460. if (isset($tmp['TR-H'])) $newprop['BORDER-TOP-RIGHT-RADIUS-H'] = $tmp['TR-H'];
  461. if (isset($tmp['TR-V'])) $newprop['BORDER-TOP-RIGHT-RADIUS-V'] = $tmp['TR-V'];
  462. if (isset($tmp['BL-H'])) $newprop['BORDER-BOTTOM-LEFT-RADIUS-H'] = $tmp['BL-H'];
  463. if (isset($tmp['BL-V'])) $newprop['BORDER-BOTTOM-LEFT-RADIUS-V'] = $tmp['BL-V'];
  464. if (isset($tmp['BR-H'])) $newprop['BORDER-BOTTOM-RIGHT-RADIUS-H'] = $tmp['BR-H'];
  465. if (isset($tmp['BR-V'])) $newprop['BORDER-BOTTOM-RIGHT-RADIUS-V'] = $tmp['BR-V'];
  466. }
  467. /*-- END BORDER-RADIUS --*/
  468. else if ($k == 'PADDING') {
  469. $tmp = $this->expand24($v);
  470. $newprop['PADDING-TOP'] = $tmp['T'];
  471. $newprop['PADDING-RIGHT'] = $tmp['R'];
  472. $newprop['PADDING-BOTTOM'] = $tmp['B'];
  473. $newprop['PADDING-LEFT'] = $tmp['L'];
  474. }
  475. else if ($k == 'BORDER') {
  476. if ($v == '1') { $v = '1px solid #000000'; }
  477. else { $v = $this->_fix_borderStr($v); }
  478. $newprop['BORDER-TOP'] = $v;
  479. $newprop['BORDER-RIGHT'] = $v;
  480. $newprop['BORDER-BOTTOM'] = $v;
  481. $newprop['BORDER-LEFT'] = $v;
  482. }
  483. else if ($k == 'BORDER-TOP') {
  484. $newprop['BORDER-TOP'] = $this->_fix_borderStr($v);
  485. }
  486. else if ($k == 'BORDER-RIGHT') {
  487. $newprop['BORDER-RIGHT'] = $this->_fix_borderStr($v);
  488. }
  489. else if ($k == 'BORDER-BOTTOM') {
  490. $newprop['BORDER-BOTTOM'] = $this->_fix_borderStr($v);
  491. }
  492. else if ($k == 'BORDER-LEFT') {
  493. $newprop['BORDER-LEFT'] = $this->_fix_borderStr($v);
  494. }
  495. else if ($k == 'BORDER-STYLE') {
  496. $e = $this->expand24($v);
  497. $newprop['BORDER-TOP-STYLE'] = $e['T'];
  498. $newprop['BORDER-RIGHT-STYLE'] = $e['R'];
  499. $newprop['BORDER-BOTTOM-STYLE'] = $e['B'];
  500. $newprop['BORDER-LEFT-STYLE'] = $e['L'];
  501. }
  502. else if ($k == 'BORDER-WIDTH') {
  503. $e = $this->expand24($v);
  504. $newprop['BORDER-TOP-WIDTH'] = $e['T'];
  505. $newprop['BORDER-RIGHT-WIDTH'] = $e['R'];
  506. $newprop['BORDER-BOTTOM-WIDTH'] = $e['B'];
  507. $newprop['BORDER-LEFT-WIDTH'] = $e['L'];
  508. }
  509. else if ($k == 'BORDER-COLOR') {
  510. $e = $this->expand24($v);
  511. $newprop['BORDER-TOP-COLOR'] = $e['T'];
  512. $newprop['BORDER-RIGHT-COLOR'] = $e['R'];
  513. $newprop['BORDER-BOTTOM-COLOR'] = $e['B'];
  514. $newprop['BORDER-LEFT-COLOR'] = $e['L'];
  515. }
  516. else if ($k == 'BORDER-SPACING') {
  517. $prop = preg_split('/\s+/',trim($v));
  518. if (count($prop) == 1 ) {
  519. $newprop['BORDER-SPACING-H'] = $prop[0];
  520. $newprop['BORDER-SPACING-V'] = $prop[0];
  521. }
  522. else if (count($prop) == 2 ) {
  523. $newprop['BORDER-SPACING-H'] = $prop[0];
  524. $newprop['BORDER-SPACING-V'] = $prop[1];
  525. }
  526. }
  527. else if ($k == 'TEXT-OUTLINE') { // mPDF 5.6.07
  528. $prop = preg_split('/\s+/',trim($v));
  529. if (trim(strtolower($v)) == 'none' ) {
  530. $newprop['TEXT-OUTLINE'] = 'none';
  531. }
  532. else if (count($prop) == 2 ) {
  533. $newprop['TEXT-OUTLINE-WIDTH'] = $prop[0];
  534. $newprop['TEXT-OUTLINE-COLOR'] = $prop[1];
  535. }
  536. else if (count($prop) == 3 ) {
  537. $newprop['TEXT-OUTLINE-WIDTH'] = $prop[0];
  538. $newprop['TEXT-OUTLINE-COLOR'] = $prop[2];
  539. }
  540. }
  541. else if ($k == 'SIZE') {
  542. $prop = preg_split('/\s+/',trim($v));
  543. if (preg_match('/(auto|portrait|landscape)/',$prop[0])) {
  544. $newprop['SIZE'] = strtoupper($prop[0]);
  545. }
  546. else if (count($prop) == 1 ) {
  547. $newprop['SIZE']['W'] = $this->mpdf->ConvertSize($prop[0]);
  548. $newprop['SIZE']['H'] = $this->mpdf->ConvertSize($prop[0]);
  549. }
  550. else if (count($prop) == 2 ) {
  551. $newprop['SIZE']['W'] = $this->mpdf->ConvertSize($prop[0]);
  552. $newprop['SIZE']['H'] = $this->mpdf->ConvertSize($prop[1]);
  553. }
  554. }
  555. else if ($k == 'SHEET-SIZE') {
  556. $prop = preg_split('/\s+/',trim($v));
  557. if (count($prop) == 2 ) {
  558. $newprop['SHEET-SIZE'] = array($this->mpdf->ConvertSize($prop[0]), $this->mpdf->ConvertSize($prop[1]));
  559. }
  560. else {
  561. if(preg_match('/([0-9a-zA-Z]*)-L/i',$v,$m)) { // e.g. A4-L = A$ landscape
  562. $ft = $this->mpdf->_getPageFormat($m[1]);
  563. $format = array($ft[1],$ft[0]);
  564. }
  565. else { $format = $this->mpdf->_getPageFormat($v); }
  566. if ($format) { $newprop['SHEET-SIZE'] = array($format[0]/_MPDFK, $format[1]/_MPDFK); }
  567. }
  568. }
  569. else if ($k == 'BACKGROUND') {
  570. $bg = $this->parseCSSbackground($v);
  571. if ($bg['c']) { $newprop['BACKGROUND-COLOR'] = $bg['c']; }
  572. else { $newprop['BACKGROUND-COLOR'] = 'transparent'; }
  573. /*-- BACKGROUNDS --*/
  574. if ($bg['i']) {
  575. $newprop['BACKGROUND-IMAGE'] = $bg['i'];
  576. if ($bg['r']) { $newprop['BACKGROUND-REPEAT'] = $bg['r']; }
  577. if ($bg['p']) { $newprop['BACKGROUND-POSITION'] = $bg['p']; }
  578. }
  579. else { $newprop['BACKGROUND-IMAGE'] = ''; }
  580. /*-- END BACKGROUNDS --*/
  581. }
  582. /*-- BACKGROUNDS --*/
  583. else if ($k == 'BACKGROUND-IMAGE') {
  584. if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i',$v,$m)) {
  585. $newprop['BACKGROUND-IMAGE'] = $m[0];
  586. continue;
  587. }
  588. if (preg_match('/url\([\'\"]{0,1}(.*?)[\'\"]{0,1}\)/i',$v,$m)) {
  589. $newprop['BACKGROUND-IMAGE'] = $m[1];
  590. }
  591. else if (strtolower($v)=='none') { $newprop['BACKGROUND-IMAGE'] = ''; }
  592. }
  593. else if ($k == 'BACKGROUND-REPEAT') {
  594. if (preg_match('/(repeat-x|repeat-y|no-repeat|repeat)/i',$v,$m)) {
  595. $newprop['BACKGROUND-REPEAT'] = strtolower($m[1]);
  596. }
  597. }
  598. else if ($k == 'BACKGROUND-POSITION') {
  599. $s = $v;
  600. $bits = preg_split('/\s+/',trim($s));
  601. // These should be Position x1 or x2
  602. if (count($bits)==1) {
  603. if (preg_match('/bottom/',$bits[0])) { $bg['p'] = '50% 100%'; }
  604. else if (preg_match('/top/',$bits[0])) { $bg['p'] = '50% 0%'; }
  605. else { $bg['p'] = $bits[0] . ' 50%'; }
  606. }
  607. else if (count($bits)==2) {
  608. // Can be either right center or center right
  609. if (preg_match('/(top|bottom)/',$bits[0]) || preg_match('/(left|right)/',$bits[1])) {
  610. $bg['p'] = $bits[1] . ' '.$bits[0];
  611. }
  612. else {
  613. $bg['p'] = $bits[0] . ' '.$bits[1];
  614. }
  615. }
  616. if ($bg['p']) {
  617. $bg['p'] = preg_replace('/(left|top)/','0%',$bg['p']);
  618. $bg['p'] = preg_replace('/(right|bottom)/','100%',$bg['p']);
  619. $bg['p'] = preg_replace('/(center)/','50%',$bg['p']);
  620. 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'])) {
  621. $bg['p'] = false;
  622. }
  623. }
  624. if ($bg['p']) { $newprop['BACKGROUND-POSITION'] = $bg['p']; }
  625. }
  626. /*-- END BACKGROUNDS --*/
  627. else if ($k == 'IMAGE-ORIENTATION') {
  628. if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad)/i',$v,$m)) {
  629. $angle = $m[1] + 0;
  630. if (strtolower($m[2])=='deg') { $angle = $angle; }
  631. else if (strtolower($m[2])=='grad') { $angle *= (360/400); }
  632. else if (strtolower($m[2])=='rad') { $angle = rad2deg($angle); }
  633. while($angle < 0) { $angle += 360; }
  634. $angle = ($angle % 360);
  635. $angle /= 90;
  636. $angle = round($angle) * 90;
  637. $newprop['IMAGE-ORIENTATION'] = $angle;
  638. }
  639. }
  640. // mPDF 5.6.13
  641. else if ($k == 'TEXT-ALIGN') {
  642. if (preg_match('/["\'](.){1}["\']/i',$v,$m)) {
  643. $d = array_search($m[1],$this->mpdf->decimal_align);
  644. if ($d !== false) { $newprop['TEXT-ALIGN'] = $d; }
  645. if (preg_match('/(center|left|right)/i',$v,$m)) { $newprop['TEXT-ALIGN'] .= strtoupper(substr($m[1],0,1)); }
  646. else { $newprop['TEXT-ALIGN'] .= 'R'; } // default = R
  647. }
  648. else if (preg_match('/["\'](\\\[a-fA-F0-9]{1,6})["\']/i',$v,$m)) {
  649. $utf8 = codeHex2utf(substr($m[1],1,6));
  650. $d = array_search($utf8,$this->mpdf->decimal_align);
  651. if ($d !== false) { $newprop['TEXT-ALIGN'] = $d; }
  652. if (preg_match('/(center|left|right)/i',$v,$m)) { $newprop['TEXT-ALIGN'] .= strtoupper(substr($m[1],0,1)); }
  653. else { $newprop['TEXT-ALIGN'] .= 'R'; } // default = R
  654. }
  655. else { $newprop[$k] = $v; }
  656. }
  657. else if ($k == 'LIST-STYLE') { // mPDF 5.7.2
  658. if (preg_match('/(lower-roman|upper-roman|lower-latin|lower-alpha|upper-latin|upper-alpha|none|decimal|disc|circle|square|arabic-indic|bengali|devanagari|gujarati|gurmukhi|kannada|malayalam|oriya|persian|tamil|telugu|thai|urdu|cambodian|khmer|lao)/i',$v,$m)
  659. || preg_match('/U\+([a-fA-F0-9]+)/i',$v,$m)) {
  660. $newprop['LIST-STYLE-TYPE'] = strtolower(trim($m[1]));
  661. }
  662. }
  663. else {
  664. $newprop[$k] = $v;
  665. }
  666. }
  667. return $newprop;
  668. }
  669. function setCSSboxshadow($v) {
  670. $sh = array();
  671. $c = preg_match_all('/(rgba|rgb|device-cmyka|cmyka|device-cmyk|cmyk|hsla|hsl)\(.*?\)/',$v,$x); // mPDF 5.6.05
  672. for($i=0; $i<$c; $i++) {
  673. $col = preg_replace('/,/','*',$x[0][$i]);
  674. $v = preg_replace('/'.preg_quote($x[0][$i],'/').'/',$col,$v);
  675. }
  676. $ss = explode(',',$v);
  677. foreach ($ss AS $s) {
  678. $new = array('inset'=>false, 'blur'=>0, 'spread'=>0);
  679. if (preg_match('/inset/i',$s)) { $new['inset'] = true; $s = preg_replace('/\s*inset\s*/','',$s); }
  680. $p = explode(' ',trim($s));
  681. if (isset($p[0])) { $new['x'] = $this->mpdf->ConvertSize(trim($p[0]),$this->mpdf->blk[$this->mpdf->blklvl-1]['inner_width'],$this->mpdf->FontSize,false); }
  682. if (isset($p[1])) { $new['y'] = $this->mpdf->ConvertSize(trim($p[1]),$this->mpdf->blk[$this->mpdf->blklvl-1]['inner_width'],$this->mpdf->FontSize,false); }
  683. if (isset($p[2])) {
  684. if (preg_match('/^\s*[\.\-0-9]/',$p[2])) {
  685. $new['blur'] = $this->mpdf->ConvertSize(trim($p[2]),$this->mpdf->blk[$this->mpdf->blklvl-1]['inner_width'],$this->mpdf->FontSize,false);
  686. }
  687. else { $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/',',',$p[2])); }
  688. if (isset($p[3])) {
  689. if (preg_match('/^\s*[\.\-0-9]/',$p[3])) {
  690. $new['spread'] = $this->mpdf->ConvertSize(trim($p[3]),$this->mpdf->blk[$this->mpdf->blklvl-1]['inner_width'],$this->mpdf->FontSize,false);
  691. }
  692. else { $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/',',',$p[3])); }
  693. if (isset($p[4])) {
  694. $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/',',',$p[4]));
  695. }
  696. }
  697. }
  698. if (!$new['col']) { $new['col'] = $this->mpdf->ConvertColor('#888888'); }
  699. if (isset($new['y'])) { array_unshift($sh, $new); }
  700. }
  701. return $sh;
  702. }
  703. function setCSStextshadow($v) {
  704. $sh = array();
  705. $c = preg_match_all('/(rgba|rgb|device-cmyka|cmyka|device-cmyk|cmyk|hsla|hsl)\(.*?\)/',$v,$x); // mPDF 5.6.05
  706. for($i=0; $i<$c; $i++) {
  707. $col = preg_replace('/,/','*',$x[0][$i]);
  708. $v = preg_replace('/'.preg_quote($x[0][$i],'/').'/',$col,$v);
  709. }
  710. $ss = explode(',',$v);
  711. foreach ($ss AS $s) {
  712. $new = array('blur'=>0);
  713. $p = explode(' ',trim($s));
  714. if (isset($p[0])) { $new['x'] = $this->mpdf->ConvertSize(trim($p[0]),$this->mpdf->blk[$this->mpdf->blklvl-1]['inner_width'],$this->mpdf->FontSize,false); }
  715. if (isset($p[1])) { $new['y'] = $this->mpdf->ConvertSize(trim($p[1]),$this->mpdf->blk[$this->mpdf->blklvl-1]['inner_width'],$this->mpdf->FontSize,false); }
  716. if (isset($p[2])) {
  717. if (preg_match('/^\s*[\.\-0-9]/',$p[2])) {
  718. $new['blur'] = $this->mpdf->ConvertSize(trim($p[2]),$this->mpdf->blk[$this->mpdf->blklvl-1]['inner_width'],$this->mpdf->FontSize,false);
  719. }
  720. else { $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/',',',$p[2])); }
  721. if (isset($p[3])) {
  722. $new['col'] = $this->mpdf->ConvertColor(preg_replace('/\*/',',',$p[3]));
  723. }
  724. }
  725. if (!$new['col']) { $new['col'] = $this->mpdf->ConvertColor('#888888'); }
  726. if (isset($new['y'])) { array_unshift($sh, $new); }
  727. }
  728. return $sh;
  729. }
  730. function parseCSSbackground($s) {
  731. $bg = array('c'=>false, 'i'=>false, 'r'=>false, 'p'=>false, );
  732. /*-- BACKGROUNDS --*/
  733. if (preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient\(.*\)/i',$s,$m)) {
  734. $bg['i'] = $m[0];
  735. }
  736. else
  737. /*-- END BACKGROUNDS --*/
  738. if (preg_match('/url\(/i',$s)) {
  739. // If color, set and strip it off
  740. // mPDF 5.6.05
  741. 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)) {
  742. $bg['c'] = strtolower($m[1]);
  743. $s = $m[3];
  744. }
  745. /*-- BACKGROUNDS --*/
  746. if (preg_match('/url\([\'\"]{0,1}(.*?)[\'\"]{0,1}\)\s*(.*)/i',$s,$m)) {
  747. $bg['i'] = $m[1];
  748. $s = strtolower($m[2]);
  749. if (preg_match('/(repeat-x|repeat-y|no-repeat|repeat)/',$s,$m)) {
  750. $bg['r'] = $m[1];
  751. }
  752. // Remove repeat, attachment (discarded) and also any inherit
  753. $s = preg_replace('/(repeat-x|repeat-y|no-repeat|repeat|scroll|fixed|inherit)/','',$s);
  754. $bits = preg_split('/\s+/',trim($s));
  755. // These should be Position x1 or x2
  756. if (count($bits)==1) {
  757. if (preg_match('/bottom/',$bits[0])) { $bg['p'] = '50% 100%'; }
  758. else if (preg_match('/top/',$bits[0])) { $bg['p'] = '50% 0%'; }
  759. else { $bg['p'] = $bits[0] . ' 50%'; }
  760. }
  761. else if (count($bits)==2) {
  762. // Can be either right center or center right
  763. if (preg_match('/(top|bottom)/',$bits[0]) || preg_match('/(left|right)/',$bits[1])) {
  764. $bg['p'] = $bits[1] . ' '.$bits[0];
  765. }
  766. else {
  767. $bg['p'] = $bits[0] . ' '.$bits[1];
  768. }
  769. }
  770. if ($bg['p']) {
  771. $bg['p'] = preg_replace('/(left|top)/','0%',$bg['p']);
  772. $bg['p'] = preg_replace('/(right|bottom)/','100%',$bg['p']);
  773. $bg['p'] = preg_replace('/(center)/','50%',$bg['p']);
  774. 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'])) {
  775. $bg['p'] = false;
  776. }
  777. }
  778. }
  779. /*-- END BACKGROUNDS --*/
  780. }
  781. 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)) { $bg['c'] = strtolower($m[1]); } // mPDF 5.6.05
  782. return ($bg);
  783. }
  784. function expand24($mp) {
  785. $prop = preg_split('/\s+/',trim($mp));
  786. if (count($prop) == 1 ) {
  787. return array('T' => $prop[0], 'R' => $prop[0], 'B' => $prop[0], 'L'=> $prop[0]);
  788. }
  789. if (count($prop) == 2 ) {
  790. return array('T' => $prop[0], 'R' => $prop[1], 'B' => $prop[0], 'L'=> $prop[1]);
  791. }
  792. if (count($prop) == 3 ) {
  793. return array('T' => $prop[0], 'R' => $prop[1], 'B' => $prop[2], 'L'=> $prop[1]);
  794. }
  795. if (count($prop) == 4 ) {
  796. return array('T' => $prop[0], 'R' => $prop[1], 'B' => $prop[2], 'L'=> $prop[3]);
  797. }
  798. return array();
  799. }
  800. /*-- BORDER-RADIUS --*/
  801. function border_radius_expand($val,$k) {
  802. $b = array();
  803. if ($k == 'BORDER-RADIUS') {
  804. $hv = explode('/',trim($val));
  805. $prop = preg_split('/\s+/',trim($hv[0]));
  806. if (count($prop)==1) {
  807. $b['TL-H'] = $b['TR-H'] = $b['BR-H'] = $b['BL-H'] = $prop[0];
  808. }
  809. else if (count($prop)==2) {
  810. $b['TL-H'] = $b['BR-H'] = $prop[0];
  811. $b['TR-H'] = $b['BL-H'] = $prop[1];
  812. }
  813. else if (count($prop)==3) {
  814. $b['TL-H'] = $prop[0];
  815. $b['TR-H'] = $b['BL-H'] = $prop[1];
  816. $b['BR-H'] = $prop[2];
  817. }
  818. else if (count($prop)==4) {
  819. $b['TL-H'] = $prop[0];
  820. $b['TR-H'] = $prop[1];
  821. $b['BR-H'] = $prop[2];
  822. $b['BL-H'] = $prop[3];
  823. }
  824. if (count($hv)==2) {
  825. $prop = preg_split('/\s+/',trim($hv[1]));
  826. if (count($prop)==1) {
  827. $b['TL-V'] = $b['TR-V'] = $b['BR-V'] = $b['BL-V'] = $prop[0];
  828. }
  829. else if (count($prop)==2) {
  830. $b['TL-V'] = $b['BR-V'] = $prop[0];
  831. $b['TR-V'] = $b['BL-V'] = $prop[1];
  832. }
  833. else if (count($prop)==3) {
  834. $b['TL-V'] = $prop[0];
  835. $b['TR-V'] = $b['BL-V'] = $prop[1];
  836. $b['BR-V'] = $prop[2];
  837. }
  838. else if (count($prop)==4) {
  839. $b['TL-V'] = $prop[0];
  840. $b['TR-V'] = $prop[1];
  841. $b['BR-V'] = $prop[2];
  842. $b['BL-V'] = $prop[3];
  843. }
  844. }
  845. else {
  846. $b['TL-V'] = $b['TL-H'];
  847. $b['TR-V'] = $b['TR-H'];
  848. $b['BL-V'] = $b['BL-H'];
  849. $b['BR-V'] = $b['BR-H'];
  850. }
  851. return $b;
  852. }
  853. // Parse 2
  854. $h = 0;
  855. $v = 0;
  856. $prop = preg_split('/\s+/',trim($val));
  857. if (count($prop)==1) { $h = $v = $val; }
  858. else { $h = $prop[0]; $v = $prop[1]; }
  859. if ($h==0 || $v==0) { $h = $v = 0; }
  860. if ($k == 'BORDER-TOP-LEFT-RADIUS') {
  861. $b['TL-H'] = $h;
  862. $b['TL-V'] = $v;
  863. }
  864. else if ($k == 'BORDER-TOP-RIGHT-RADIUS') {
  865. $b['TR-H'] = $h;
  866. $b['TR-V'] = $v;
  867. }
  868. else if ($k == 'BORDER-BOTTOM-LEFT-RADIUS') {
  869. $b['BL-H'] = $h;
  870. $b['BL-V'] = $v;
  871. }
  872. else if ($k == 'BORDER-BOTTOM-RIGHT-RADIUS') {
  873. $b['BR-H'] = $h;
  874. $b['BR-V'] = $v;
  875. }
  876. return $b;
  877. }
  878. /*-- END BORDER-RADIUS --*/
  879. function _mergeCSS($p, &$t) {
  880. // Save Cascading CSS e.g. "div.topic p" at this block level
  881. if (isset($p) && $p) {
  882. if ($t) {
  883. $t = $this->array_merge_recursive_unique($t, $p);
  884. }
  885. else { $t = $p; }
  886. }
  887. }
  888. // for CSS handling
  889. function array_merge_recursive_unique($array1, $array2) {
  890. $arrays = func_get_args();
  891. $narrays = count($arrays);
  892. $ret = $arrays[0];
  893. for ($i = 1; $i < $narrays; $i ++) {
  894. foreach ($arrays[$i] as $key => $value) {
  895. if (((string) $key) === ((string) intval($key))) { // integer or string as integer key - append
  896. $ret[] = $value;
  897. }
  898. else { // string key - merge
  899. if (is_array($value) && isset($ret[$key])) {
  900. $ret[$key] = $this->array_merge_recursive_unique($ret[$key], $value);
  901. }
  902. else {
  903. $ret[$key] = $value;
  904. }
  905. }
  906. }
  907. }
  908. return $ret;
  909. }
  910. function _mergeFullCSS($p, &$t, $tag, $classes, $id) {
  911. $this->_mergeCSS($p[$tag], $t);
  912. // STYLESHEET CLASS e.g. .smallone{} .redletter{}
  913. foreach($classes AS $class) {
  914. $this->_mergeCSS($p['CLASS>>'.$class], $t);
  915. }
  916. // STYLESHEET nth-child SELECTOR e.g. tr:nth-child(odd) td:nth-child(2n+1)
  917. if ($tag=='TR' && isset($p) && $p) {
  918. foreach($p AS $k=>$val) {
  919. if (preg_match('/'.$tag.'>>SELECTORNTHCHILD>>(.*)/',$k, $m)) {
  920. $select = false;
  921. if ($tag=='TR') {
  922. $row = $this->mpdf->row;
  923. $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);
  924. $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);
  925. if ($this->mpdf->tabletfoot) { $row -= $thnr; }
  926. else if (!$this->mpdf->tablethead) { $row -= ($thnr + $tfnr); }
  927. if ($m[1]=='ODD' && ($row % 2) == 0) { $select = true; }
  928. else if ($m[1]=='EVEN' && ($row % 2) == 1) { $select = true; }
  929. else if (preg_match('/(\d+)N\+(\d+)/',$m[1],$a)) {
  930. if ((($row + 1) % $a[1]) == $a[2]) { $select = true; }
  931. }
  932. }
  933. else if ($tag=='TD' || $tag=='TH') {
  934. if ($m[1]=='ODD' && ($this->mpdf->col % 2) == 0) { $select = true; }
  935. else if ($m[1]=='EVEN' && ($this->mpdf->col % 2) == 1) { $select = true; }
  936. else if (preg_match('/(\d+)N\+(\d+)/',$m[1],$a)) {
  937. if ((($this->mpdf->col + 1) % $a[1]) == $a[2]) { $select = true; }
  938. }
  939. }
  940. if ($select) {
  941. $this->_mergeCSS($p[$tag.'>>SELECTORNTHCHILD>>'.$m[1]], $t);
  942. }
  943. }
  944. }
  945. }
  946. // STYLESHEET CLASS e.g. #smallone{} #redletter{}
  947. if (isset($id) && $id) {
  948. $this->_mergeCSS($p['ID>>'.$id], $t);
  949. }
  950. // STYLESHEET CLASS e.g. .smallone{} .redletter{}
  951. foreach($classes AS $class) {
  952. $this->_mergeCSS($p[$tag.'>>CLASS>>'.$class], $t);
  953. }
  954. // STYLESHEET CLASS e.g. #smallone{} #redletter{}
  955. if (isset($id)) {
  956. $this->_mergeCSS($p[$tag.'>>ID>>'.$id], $t);
  957. }
  958. }
  959. function setBorderDominance($prop, $val) {
  960. if (isset($prop['BORDER-LEFT']) && $prop['BORDER-LEFT']) { $this->cell_border_dominance_L = $val; }
  961. if (isset($prop['BORDER-RIGHT']) && $prop['BORDER-RIGHT']) { $this->cell_border_dominance_R = $val; }
  962. if (isset($prop['BORDER-TOP']) && $prop['BORDER-TOP']) { $this->cell_border_dominance_T = $val; }
  963. if (isset($prop['BORDER-BOTTOM']) && $prop['BORDER-BOTTOM']) { $this->cell_border_dominance_B = $val; }
  964. }
  965. function _set_mergedCSS(&$m, &$p, $d=true, $bd=false) {
  966. if (isset($m)) {
  967. if ((isset($m['depth']) && $m['depth']>1) || $d==false) { // include check for 'depth'
  968. if ($bd) { $this->setBorderDominance($m, $bd); } // *TABLES*
  969. if (is_array($m)) {
  970. $p = array_merge($p,$m);
  971. $this->_mergeBorders($p,$m);
  972. }
  973. }
  974. }
  975. }
  976. function _mergeBorders(&$b, &$a) { // Merges $a['BORDER-TOP-STYLE'] to $b['BORDER-TOP'] etc.
  977. foreach(array('TOP','RIGHT','BOTTOM','LEFT') AS $side) {
  978. foreach(array('STYLE','WIDTH','COLOR') AS $el) {
  979. if (isset($a['BORDER-'.$side.'-'.$el])) { // e.g. $b['BORDER-TOP-STYLE']
  980. $s = trim($a['BORDER-'.$side.'-'.$el]);
  981. if (isset($b['BORDER-'.$side])) { // e.g. $b['BORDER-TOP']
  982. $p = trim($b['BORDER-'.$side]);
  983. }
  984. else { $p = ''; }
  985. if ($el=='STYLE') {
  986. if ($p) { $b['BORDER-'.$side] = preg_replace('/(\S+)\s+(\S+)\s+(\S+)/', '\\1 '.$s.' \\3', $p); }
  987. else { $b['BORDER-'.$side] = '0px '.$s.' #000000'; }
  988. }
  989. else if ($el=='WIDTH') {
  990. if ($p) { $b['BORDER-'.$side] = preg_replace('/(\S+)\s+(\S+)\s+(\S+)/', $s.' \\2 \\3', $p); }
  991. else { $b['BORDER-'.$side] = $s.' none #000000'; }
  992. }
  993. else if ($el=='COLOR') {
  994. if ($p) { $b['BORDER-'.$side] = preg_replace('/(\S+)\s+(\S+)\s+(\S+)/', '\\1 \\2 '.$s, $p); }
  995. else { $b['BORDER-'.$side] = '0px none '.$s; }
  996. }
  997. }
  998. }
  999. }
  1000. }
  1001. function MergeCSS($inherit,$tag,$attr) {
  1002. $p = array();
  1003. $zp = array();
  1004. $classes = array();
  1005. if (isset($attr['CLASS'])) {
  1006. $classes = preg_split('/\s+/',$attr['CLASS']);
  1007. }
  1008. if (!isset($attr['ID'])) { $attr['ID']=''; }
  1009. //===============================================
  1010. /*-- TABLES --*/
  1011. // Set Inherited properties
  1012. if ($inherit == 'TOPTABLE') { // $tag = TABLE
  1013. //===============================================
  1014. // Save Cascading CSS e.g. "div.topic p" at this block level
  1015. if (isset($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'])) {
  1016. $this->tablecascadeCSS[0] = $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'];
  1017. }
  1018. else {
  1019. $this->tablecascadeCSS[0] = $this->cascadeCSS;
  1020. }
  1021. }
  1022. //===============================================
  1023. // Set Inherited properties
  1024. if ($inherit == 'TOPTABLE' || $inherit == 'TABLE') {
  1025. //Cascade everything from last level that is not an actual property, or defined by current tag/attributes
  1026. if (isset($this->tablecascadeCSS[$this->tbCSSlvl-1]) && is_array($this->tablecascadeCSS[$this->tbCSSlvl-1])) {
  1027. foreach($this->tablecascadeCSS[$this->tbCSSlvl-1] AS $k=>$v) {
  1028. $this->tablecascadeCSS[$this->tbCSSlvl][$k] = $v;
  1029. }
  1030. }
  1031. $this->_mergeFullCSS($this->cascadeCSS, $this->tablecascadeCSS[$this->tbCSSlvl], $tag, $classes, $attr['ID']);
  1032. //===============================================
  1033. // Cascading forward CSS e.g. "table.topic td" for this table in $this->tablecascadeCSS
  1034. //===============================================
  1035. // STYLESHEET TAG e.g. table
  1036. $this->_mergeFullCSS($this->tablecascadeCSS[$this->tbCSSlvl-1], $this->tablecascadeCSS[$this->tbCSSlvl], $tag, $classes, $attr['ID']);
  1037. //===============================================
  1038. }
  1039. /*-- END TABLES --*/
  1040. //===============================================
  1041. /*-- LISTS --*/
  1042. // Set Inherited properties
  1043. if ($inherit == 'TOPLIST') { // $tag = UL,OL
  1044. //===============================================
  1045. // Save Cascading CSS e.g. "div.topic p" at this block level
  1046. if (isset($this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'])) {
  1047. $this->listcascadeCSS[0] = $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'];
  1048. }
  1049. else {
  1050. $this->listcascadeCSS[0] = $this->cascadeCSS;
  1051. }
  1052. }
  1053. //===============================================
  1054. // Set Inherited properties
  1055. if ($inherit == 'TOPLIST' || $inherit == 'LIST') {
  1056. //Cascade everything from last level that is not an actual property, or defined by current tag/attributes
  1057. if (isset($this->listcascadeCSS[$this->listCSSlvl-1]) && is_array($this->listcascadeCSS[$this->listCSSlvl-1])) {
  1058. foreach($this->listcascadeCSS[$this->listCSSlvl-1] AS $k=>$v) {
  1059. $this->listcascadeCSS[$this->listCSSlvl][$k] = $v;
  1060. }
  1061. }
  1062. $this->_mergeFullCSS($this->cascadeCSS, $this->listcascadeCSS[$this->listCSSlvl], $tag, $classes, $attr['ID']);
  1063. //===============================================
  1064. // Cascading forward CSS e.g. "table.topic td" for this list in $this->listcascadeCSS
  1065. //===============================================
  1066. // STYLESHEET TAG e.g. table
  1067. $this->_mergeFullCSS($this->listcascadeCSS[$this->listCSSlvl-1], $this->listcascadeCSS[$this->listCSSlvl], $tag, $classes, $attr['ID']);
  1068. //===============================================
  1069. }
  1070. /*-- END LISTS --*/
  1071. //===============================================
  1072. // Set Inherited properties
  1073. if ($inherit == 'BLOCK') {
  1074. if (isset($this->mpdf->blk[$this->mpdf->blklvl-1]['cascadeCSS']) && is_array($this->mpdf->blk[$this->mpdf->blklvl-1]['cascadeCSS'])) {
  1075. foreach($this->mpdf->blk[$this->mpdf->blklvl-1]['cascadeCSS'] AS $k=>$v) {
  1076. $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'][$k] = $v;
  1077. }
  1078. }
  1079. //===============================================
  1080. // Save Cascading CSS e.g. "div.topic p" at this block level
  1081. $this->_mergeFullCSS($this->cascadeCSS, $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'], $tag, $classes, $attr['ID']);
  1082. //===============================================
  1083. // Cascading forward CSS
  1084. //===============================================
  1085. $this->_mergeFullCSS($this->mpdf->blk[$this->mpdf->blklvl-1]['cascadeCSS'], $this->mpdf->blk[$this->mpdf->blklvl]['cascadeCSS'], $tag, $classes, $attr['ID']);
  1086. //===============================================
  1087. // Block properties
  1088. if (isset($this->mpdf->blk[$this->mpdf->blklvl-1]['margin_collapse']) && $this->mpdf->blk[$this->mpdf->blklvl-1]['margin_collapse']) { $p['MARGIN-COLLAPSE'] = 'COLLAPSE'; } // custom tag, but follows CSS principle that border-collapse is inherited
  1089. if (isset($this->mpdf->blk[$this->mpdf->blklvl-1]['line_height']) && $this->mpdf->blk[$this->mpdf->blklvl-1]['line_height']) { $p['LINE-HEIGHT'] = $this->mpdf->blk[$this->mpdf->blklvl-1]['line_height']; }
  1090. if (isset($this->mpdf->blk[$this->mpdf->blklvl-1]['direction']) && $this->mpdf->blk[$this->mpdf->blklvl-1]['direction']) { $p['DIRECTION'] = $this->mpdf->blk[$this->mpdf->blklvl-1]['direction']; }
  1091. if (isset($this->mpdf->blk[$this->mpdf->blklvl-1]['align']) && $this->mpdf->blk[$this->mpdf->blklvl-1]['align']) {
  1092. if ($this->mpdf->blk[$this->mpdf->blklvl-1]['align'] == 'L') { $p['TEXT-ALIGN'] = 'left'; }
  1093. else if ($this->mpdf->blk[$this->mpdf->blklvl-1]['align'] == 'J') { $p['TEXT-ALIGN'] = 'justify'; }
  1094. else if ($this->mpdf->blk[$this->mpdf->blklvl-1]['align'] == 'R') { $p['TEXT-ALIGN'] = 'right'; }
  1095. else if ($this->mpdf->blk[$this->mpdf->blklvl-1]['align'] == 'C') { $p['TEXT-ALIGN'] = 'center'; }
  1096. }
  1097. if ($this->mpdf->ColActive || $this->mpdf->keep_block_together) {
  1098. 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)
  1099. $cor = $this->mpdf->blk[$this->mpdf->blklvl-1]['bgcolorarray' ];
  1100. $p['BACKGROUND-COLOR'] = $this->mpdf->_colAtoString($cor);
  1101. }
  1102. }
  1103. 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)) { $p['TEXT-INDENT'] = $this->mpdf->blk[$this->mpdf->blklvl-1]['text_indent']; }
  1104. if (isset($this->mpdf->blk[$this->mpdf->blklvl-1]['InlineProperties'])) {
  1105. $biilp = $this->mpdf->blk[$this->mpdf->blklvl-1]['InlineProperties'];
  1106. }
  1107. else { $biilp = null; }
  1108. if (isset($biilp[ 'family' ]) && $biilp[ 'family' ]) { $p['FONT-FAMILY'] = $biilp[ 'family' ]; }
  1109. if (isset($biilp[ 'I' ]) && $biilp[ 'I' ]) { $p['FONT-STYLE'] = 'italic'; }
  1110. if (isset($biilp[ 'sizePt' ]) && $biilp[ 'sizePt' ]) { $p['FONT-SIZE'] = $biilp[ 'sizePt' ] . 'pt'; }
  1111. if (isset($biilp[ 'B' ]) && $biilp[ 'B' ]) { $p['FONT-WEIGHT'] = 'bold'; }
  1112. if (isset($biilp[ 'colorarray' ]) && $biilp[ 'colorarray' ]) {
  1113. $cor = $biilp[ 'colorarray' ];
  1114. $p['COLOR'] = $this->mpdf->_colAtoString($cor);
  1115. }
  1116. if (isset($biilp[ 'fontkerning' ])) {
  1117. if ($biilp[ 'fontkerning' ]) { $p['FONT-KERNING'] = 'normal'; }
  1118. else { $p['FONT-KERNING'] = 'none'; }
  1119. }
  1120. if (isset($biilp[ 'lSpacingCSS' ]) && $biilp[ 'lSpacingCSS' ]) { $p['LETTER-SPACING'] = $biilp[ 'lSpacingCSS' ]; }
  1121. if (isset($biilp[ 'wSpacingCSS' ]) && $biilp[ 'wSpacingCSS' ]) { $p['WORD-SPACING'] = $biilp[ 'wSpacingCSS' ]; }
  1122. if (isset($biilp[ 'toupper' ]) && $biilp[ 'toupper' ]) { $p['TEXT-TRANSFORM'] = 'uppercase'; }
  1123. else if (isset($biilp[ 'tolower' ]) && $biilp[ 'tolower' ]) { $p['TEXT-TRANSFORM'] = 'lowercase'; }
  1124. else if (isset($biilp[ 'capitalize' ]) && $biilp[ 'capitalize' ]) { $p['TEXT-TRANSFORM'] = 'capitalize'; }
  1125. // CSS says text-decoration is not inherited, but IE7 does??
  1126. if (isset($biilp[ 'underline' ]) && $biilp[ 'underline' ]) { $p['TEXT-DECORATION'] = 'underline'; }
  1127. if (isset($biilp[ 'smCaps' ]) && $biilp[ 'smCaps' ]) { $p['FONT-VARIANT'] = 'small-caps'; }
  1128. }
  1129. //===============================================
  1130. //===============================================
  1131. /*-- LISTS --*/
  1132. // Set Inherited properties
  1133. if ($inherit == 'TOPLIST') {
  1134. if ($this->listCSSlvl == 1) {
  1135. $bilp = $this->mpdf->blk[$this->mpdf->blklvl]['InlineProperties'];
  1136. if (isset($bilp[ 'family' ]) && $bilp[ 'family' ]) { $p['FONT-FAMILY'] = $bilp[ 'family' ]; }
  1137. if (isset($bilp[ 'I' ]) && $bilp[ 'I' ]) { $p['FONT-STYLE'] = 'italic'; }
  1138. if (isset($bilp[ 'sizePt' ]) && $bilp[ 'sizePt' ]) { $p['FONT-SIZE'] = $bilp[ 'sizePt' ] . 'pt'; }
  1139. if (isset($bilp[ 'B' ]) && $bilp[ 'B' ]) { $p['FONT-WEIGHT'] = 'bold'; }
  1140. if (isset($bilp[ 'colorarray' ]) && $bilp[ 'colorarray' ]) {
  1141. $cor = $bilp[ 'colorarray' ];
  1142. $p['COLOR'] = $this->mpdf->_colAtoString($cor);
  1143. }
  1144. if (isset($bilp[ 'toupper' ]) && $bilp[ 'toupper' ]) { $p['TEXT-TRANSFORM'] = 'uppercase'; }
  1145. else if (isset($bilp[ 'tolower' ]) && $bilp[ 'tolower' ]) { $p['TEXT-TRANSFORM'] = 'lowercase'; }
  1146. else if (isset($bilp[ 'capitalize' ]) && $bilp[ 'capitalize' ]) { $p['TEXT-TRANSFORM'] = 'capitalize'; }
  1147. if (isset($bilp[ 'fontkerning' ])) {
  1148. if ($bilp[ 'fontkerning' ]) { $p['FONT-KERNING'] = 'normal'; }
  1149. else { $p['FONT-KERNING'] = 'none'; }
  1150. }
  1151. if (isset($bilp[ 'lSpacingCSS' ]) && $bilp[ 'lSpacingCSS' ]) { $p['LETTER-SPACING'] = $bilp[ 'lSpacingCSS' ]; }
  1152. if (isset($bilp[ 'wSpacingCSS' ]) && $bilp[ 'wSpacingCSS' ]) { $p['WORD-SPACING'] = $bilp[ 'wSpacingCSS' ]; }
  1153. // CSS says text-decoration is not inherited, but IE7 does??
  1154. if (isset($bilp[ 'underline' ]) && $bilp[ 'underline' ]) { $p[