PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/php/codeigniter/pdf/application/third_party/mpdf/classes/cssmgr.php

https://github.com/4vconnect/api
PHP | 1566 lines | 1310 code | 86 blank | 170 comment | 810 complexity | c5acdd1048db0bbea8e12fec47aec171 MD5 | raw file
Possible License(s): GPL-2.0

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

Large files files are truncated, but you can click here to view the full file