Portal Secure Messaging Audits
[openemr.git] / library / html2pdf / _class / parsingCss.class.php
bloba17abf9f7c901003e218ec64f018b9bd8840c03e
1 <?php
2 /**
3 * HTML2PDF Library - parsingCss class
5 * HTML => PDF convertor
6 * distributed under the LGPL License
8 * @package Html2pdf
9 * @author Laurent MINGUET <webmaster@html2pdf.fr>
10 * @copyright 2016 Laurent MINGUET
12 class HTML2PDF_parsingCss
14 /**
15 * reference to the pdf object
16 * @var TCPDF
18 protected $_pdf = null;
20 protected $_htmlColor = array(); // list of the HTML colors
21 protected $_onlyLeft = false; // flag if we are in a sub html => only "text-align:left" is used
22 protected $_defaultFont = null; // default font to use if the asked font does not exist
24 public $value = array(); // current values
25 public $css = array(); // css values
26 public $cssKeys = array(); // css key, for the execution order
27 public $table = array(); // level history
29 /**
30 * Constructor
32 * @param &HTML2PDF_myPdf reference to the PDF $object
33 * @access public
35 public function __construct(&$pdf)
37 $this->_init();
38 $this->setPdfParent($pdf);
41 /**
42 * Set the HTML2PDF parent object
44 * @param &HTML2PDF reference to the HTML2PDF parent $object
45 * @access public
47 public function setPdfParent(&$pdf)
49 $this->_pdf = &$pdf;
52 /**
53 * Inform that we want only "test-align:left" because we are in a sub HTML
55 * @access public
57 public function setOnlyLeft()
59 $this->value['text-align'] = 'left';
60 $this->_onlyLeft = true;
63 /**
64 * Get the vales of the parent, if exist
66 * @return array CSS values
67 * @access public
69 public function getOldValues()
71 return isset($this->table[count($this->table)-1]) ? $this->table[count($this->table)-1] : $this->value;
74 /**
75 * define the Default Font to use, if the font does not exist, or if no font asked
77 * @param string default font-family. If null : Arial for no font asked, and error fot ont does not exist
78 * @return string old default font-family
79 * @access public
81 public function setDefaultFont($default = null)
83 $old = $this->_defaultFont;
84 $this->_defaultFont = $default;
85 if ($default) $this->value['font-family'] = $default;
86 return $old;
89 /**
90 * Init the object
92 * @access protected
94 protected function _init()
96 // get the Web Colors from TCPDF
97 $this->_htmlColor = TCPDF_COLORS::$webcolor;
99 // init the Style
100 $this->table = array();
101 $this->value = array();
102 $this->initStyle();
104 // Init the styles without legacy
105 $this->resetStyle();
109 * Init the CSS Style
111 * @access public
113 public function initStyle()
115 $this->value['id_tag'] = 'body'; // tag name
116 $this->value['id_name'] = null; // tag - attribute name
117 $this->value['id_id'] = null; // tag - attribute id
118 $this->value['id_class'] = null; // tag - attribute class
119 $this->value['id_lst'] = array('*'); // tag - list of legacy
120 $this->value['mini-size'] = 1.; // specific size report for sup, sub
121 $this->value['mini-decal'] = 0; // specific position report for sup, sub
122 $this->value['font-family'] = defined('PDF_FONT_NAME_MAIN') ? PDF_FONT_NAME_MAIN : 'Arial';
123 $this->value['font-bold'] = false;
124 $this->value['font-italic'] = false;
125 $this->value['font-underline'] = false;
126 $this->value['font-overline'] = false;
127 $this->value['font-linethrough'] = false;
128 $this->value['text-transform'] = 'none';
129 $this->value['font-size'] = $this->convertToMM('10pt');
130 $this->value['text-indent'] = 0;
131 $this->value['text-align'] = 'left';
132 $this->value['vertical-align'] = 'middle';
133 $this->value['line-height'] = 'normal';
135 $this->value['position'] = null;
136 $this->value['x'] = null;
137 $this->value['y'] = null;
138 $this->value['width'] = 0;
139 $this->value['height'] = 0;
140 $this->value['top'] = null;
141 $this->value['right'] = null;
142 $this->value['bottom'] = null;
143 $this->value['left'] = null;
144 $this->value['float'] = null;
145 $this->value['display'] = null;
146 $this->value['rotate'] = null;
147 $this->value['overflow'] = 'visible';
149 $this->value['color'] = array(0, 0, 0);
150 $this->value['background'] = array('color' => null, 'image' => null, 'position' => null, 'repeat' => null);
151 $this->value['border'] = array();
152 $this->value['padding'] = array();
153 $this->value['margin'] = array();
154 $this->value['margin-auto'] = false;
156 $this->value['list-style-type'] = '';
157 $this->value['list-style-image'] = '';
159 $this->value['xc'] = null;
160 $this->value['yc'] = null;
164 * Init the CSS Style without legacy
166 * @param string tag name
167 * @access public
169 public function resetStyle($tagName = '')
171 // prepare somme values
172 $border = $this->readBorder('solid 1px #000000');
173 $units = array(
174 '1px' => $this->convertToMM('1px'),
175 '5px' => $this->convertToMM('5px'),
179 // prepare the Collapse attribute
180 $collapse = isset($this->value['border']['collapse']) ? $this->value['border']['collapse'] : false;
181 if (!in_array($tagName, array('tr', 'td', 'th', 'thead', 'tbody', 'tfoot'))) $collapse = false;
183 // set the global css values
184 $this->value['position'] = null;
185 $this->value['x'] = null;
186 $this->value['y'] = null;
187 $this->value['width'] = 0;
188 $this->value['height'] = 0;
189 $this->value['top'] = null;
190 $this->value['right'] = null;
191 $this->value['bottom'] = null;
192 $this->value['left'] = null;
193 $this->value['float'] = null;
194 $this->value['display'] = null;
195 $this->value['rotate'] = null;
196 $this->value['overflow'] = 'visible';
197 $this->value['background'] = array('color' => null, 'image' => null, 'position' => null, 'repeat' => null);
198 $this->value['border'] = array(
199 't' => $this->readBorder('none'),
200 'r' => $this->readBorder('none'),
201 'b' => $this->readBorder('none'),
202 'l' => $this->readBorder('none'),
203 'radius' => array(
204 'tl' => array(0, 0),
205 'tr' => array(0, 0),
206 'br' => array(0, 0),
207 'bl' => array(0, 0)
209 'collapse' => $collapse,
212 // specific values for some tags
213 if (!in_array($tagName, array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) {
214 $this->value['margin'] = array('t'=>0,'r'=>0,'b'=>0,'l'=>0);
217 if (in_array($tagName, array('input', 'select', 'textarea'))) {
218 $this->value['border']['t'] = null;
219 $this->value['border']['r'] = null;
220 $this->value['border']['b'] = null;
221 $this->value['border']['l'] = null;
224 if ($tagName=='p') {
225 $this->value['margin']['t'] = null;
226 $this->value['margin']['b'] = null;
228 if ($tagName=='blockquote') {
229 $this->value['margin']['t'] = 3;
230 $this->value['margin']['r'] = 3;
231 $this->value['margin']['b'] = 3;
232 $this->value['margin']['l'] = 6;
234 $this->value['margin-auto'] = false;
236 if (in_array($tagName, array('blockquote', 'div', 'fieldset'))) {
237 $this->value['vertical-align'] = 'top';
240 if (in_array($tagName, array('fieldset', 'legend'))) {
241 $this->value['border'] = array(
242 't' => $border,
243 'r' => $border,
244 'b' => $border,
245 'l' => $border,
246 'radius' => array(
247 'tl' => array($units['5px'], $units['5px']),
248 'tr' => array($units['5px'], $units['5px']),
249 'br' => array($units['5px'], $units['5px']),
250 'bl' => array($units['5px'], $units['5px'])
252 'collapse' => false,
256 if (in_array($tagName, array('ul', 'li'))) {
257 $this->value['list-style-type'] = '';
258 $this->value['list-style-image'] = '';
261 if (!in_array($tagName, array('tr', 'td'))) {
262 $this->value['padding'] = array(
263 't' => 0,
264 'r' => 0,
265 'b' => 0,
266 'l' => 0
268 } else {
269 $this->value['padding'] = array(
270 't' => $units['1px'],
271 'r' => $units['1px'],
272 'b' => $units['1px'],
273 'l' => $units['1px']
277 if ($tagName=='hr') {
278 $this->value['border'] = array(
279 't' => $border,
280 'r' => $border,
281 'b' => $border,
282 'l' => $border,
283 'radius' => array(
284 'tl' => array(0, 0),
285 'tr' => array(0, 0),
286 'br' => array(0, 0),
287 'bl' => array(0, 0)
289 'collapse' => false,
291 $this->convertBackground('#FFFFFF', $this->value['background']);
294 $this->value['xc'] = null;
295 $this->value['yc'] = null;
299 * Init the PDF Font
301 * @access public
303 public function fontSet()
305 $family = strtolower($this->value['font-family']);
307 $b = ($this->value['font-bold'] ? 'B' : '');
308 $i = ($this->value['font-italic'] ? 'I' : '');
309 $u = ($this->value['font-underline'] ? 'U' : '');
310 $d = ($this->value['font-linethrough'] ? 'D' : '');
311 $o = ($this->value['font-overline'] ? 'O' : '');
313 // font style
314 $style = $b.$i;
316 if ($this->_defaultFont) {
317 if($family=='arial')
318 $family='helvetica';
319 elseif($family=='symbol' || $family=='zapfdingbats')
320 $style='';
322 $fontkey = $family.$style;
323 if (!$this->_pdf->isLoadedFont($fontkey))
324 $family = $this->_defaultFont;
327 if($family=='arial')
328 $family='helvetica';
329 elseif($family=='symbol' || $family=='zapfdingbats')
330 $style='';
332 // complete style
333 $style.= $u.$d.$o;
335 // size : mm => pt
336 $size = $this->value['font-size'];
337 $size = 72 * $size / 25.4;
339 // apply the font
340 $this->_pdf->SetFont($family, $style, $this->value['mini-size']*$size);
341 $this->_pdf->setTextColorArray($this->value['color']);
342 if ($this->value['background']['color'])
343 $this->_pdf->setFillColorArray($this->value['background']['color']);
344 else
345 $this->_pdf->setFillColor(255);
349 * add a level in the CSS history
351 * @access public
353 public function save()
355 array_push($this->table, $this->value);
359 * remove a level in the CSS history
361 * @access public
363 public function load()
365 if (count($this->table)) {
366 $this->value = array_pop($this->table);
371 * restore the Y positiony (used after a span)
373 * @access public
375 public function restorePosition()
377 if ($this->value['y']==$this->_pdf->getY()) $this->_pdf->setY($this->value['yc'], false);
381 * set the New position for the current Tag
383 * @access public
385 public function setPosition()
387 // get the current position
388 $currentX = $this->_pdf->getX();
389 $currentY = $this->_pdf->getY();
391 // save it
392 $this->value['xc'] = $currentX;
393 $this->value['yc'] = $currentY;
395 if ($this->value['position']=='relative' || $this->value['position']=='absolute') {
396 if ($this->value['right']!==null) {
397 $x = $this->getLastWidth(true) - $this->value['right'] - $this->value['width'];
398 if ($this->value['margin']['r']) $x-= $this->value['margin']['r'];
399 } else {
400 $x = $this->value['left'];
401 if ($this->value['margin']['l']) $x+= $this->value['margin']['l'];
404 if ($this->value['bottom']!==null) {
405 $y = $this->getLastHeight(true) - $this->value['bottom'] - $this->value['height'];
406 if ($this->value['margin']['b']) $y-= $this->value['margin']['b'];
407 } else {
408 $y = $this->value['top'];
409 if ($this->value['margin']['t']) $y+= $this->value['margin']['t'];
412 if ($this->value['position']=='relative') {
413 $this->value['x'] = $currentX + $x;
414 $this->value['y'] = $currentY + $y;
415 } else {
416 $this->value['x'] = $this->_getLastAbsoluteX()+$x;
417 $this->value['y'] = $this->_getLastAbsoluteY()+$y;
419 } else {
420 $this->value['x'] = $currentX;
421 $this->value['y'] = $currentY;
422 if ($this->value['margin']['l']) $this->value['x']+= $this->value['margin']['l'];
423 if ($this->value['margin']['t']) $this->value['y']+= $this->value['margin']['t'];
426 // save the new position
427 $this->_pdf->setXY($this->value['x'], $this->value['y']);
431 * Analise the CSS style to convert it into Form style
433 * @access public
434 * @param array styles
436 public function getFormStyle()
438 $prop = array();
440 $prop['alignment'] = $this->value['text-align'];
442 if (isset($this->value['background']['color']) && is_array($this->value['background']['color'])) {
443 $prop['fillColor'] = $this->value['background']['color'];
446 if (isset($this->value['border']['t']['color'])) {
447 $prop['strokeColor'] = $this->value['border']['t']['color'];
450 if (isset($this->value['border']['t']['width'])) {
451 $prop['lineWidth'] = $this->value['border']['t']['width'];
454 if (isset($this->value['border']['t']['type'])) {
455 $prop['borderStyle'] = $this->value['border']['t']['type'];
458 if (!empty($this->value['color'])) {
459 $prop['textColor'] = $this->value['color'];
462 if (!empty($this->value['font-size'])) {
463 $prop['textSize'] = $this->value['font-size'];
466 return $prop;
470 * Analise the CSS style to convert it into SVG style
472 * @access public
473 * @param string tag name
474 * @param array styles
476 public function getSvgStyle($tagName, &$param)
478 // prepare
479 $tagName = strtolower($tagName);
480 $id = isset($param['id']) ? strtolower(trim($param['id'])) : null; if (!$id) $id = null;
481 $name = isset($param['name']) ? strtolower(trim($param['name'])) : null; if (!$name) $name = null;
483 // read the class attribute
484 $class = array();
485 $tmp = isset($param['class']) ? strtolower(trim($param['class'])) : '';
486 $tmp = explode(' ', $tmp);
487 foreach ($tmp as $k => $v) {
488 $v = trim($v);
489 if ($v) $class[] = $v;
492 // identify the tag, and the direct styles
493 $this->value['id_tag'] = $tagName;
494 $this->value['id_name'] = $name;
495 $this->value['id_id'] = $id;
496 $this->value['id_class'] = $class;
497 $this->value['id_lst'] = array();
498 $this->value['id_lst'][] = '*';
499 $this->value['id_lst'][] = $tagName;
500 if (!isset($this->value['svg'])) {
501 $this->value['svg'] = array(
502 'stroke' => null,
503 'stroke-width' => $this->convertToMM('1pt'),
504 'fill' => null,
505 'fill-opacity' => null,
509 if (count($class)) {
510 foreach ($class as $v) {
511 $this->value['id_lst'][] = '*.'.$v;
512 $this->value['id_lst'][] = '.'.$v;
513 $this->value['id_lst'][] = $tagName.'.'.$v;
516 if ($id) {
517 $this->value['id_lst'][] = '*#'.$id;
518 $this->value['id_lst'][] = '#'.$id;
519 $this->value['id_lst'][] = $tagName.'#'.$id;
522 // CSS style
523 $styles = $this->_getFromCSS();
525 // adding the style from the tag
526 $styles = array_merge($styles, $param['style']);
528 if (isset($styles['stroke'])) $this->value['svg']['stroke'] = $this->convertToColor($styles['stroke'], $res);
529 if (isset($styles['stroke-width'])) $this->value['svg']['stroke-width'] = $this->convertToMM($styles['stroke-width']);
530 if (isset($styles['fill'])) $this->value['svg']['fill'] = $this->convertToColor($styles['fill'], $res);
531 if (isset($styles['fill-opacity'])) $this->value['svg']['fill-opacity'] = 1.*$styles['fill-opacity'];
533 return $this->value['svg'];
537 * analyse the css properties from the HTML parsing
539 * @access public
540 * @param string $tagName
541 * @param array $param
542 * @param array $legacy
544 public function analyse($tagName, &$param, $legacy = null)
546 // prepare the informations
547 $tagName = strtolower($tagName);
548 $id = isset($param['id']) ? strtolower(trim($param['id'])) : null; if (!$id) $id = null;
549 $name = isset($param['name']) ? strtolower(trim($param['name'])) : null; if (!$name) $name = null;
551 // get the class names to use
552 $class = array();
553 $tmp = isset($param['class']) ? strtolower(trim($param['class'])) : '';
554 $tmp = explode(' ', $tmp);
555 foreach ($tmp as $k => $v) {
556 $v = trim($v);
557 if ($v) $class[] = $v;
560 // prepare the values, and the list of css tags to identify
561 $this->value['id_tag'] = $tagName;
562 $this->value['id_name'] = $name;
563 $this->value['id_id'] = $id;
564 $this->value['id_class'] = $class;
565 $this->value['id_lst'] = array();
566 $this->value['id_lst'][] = '*';
567 $this->value['id_lst'][] = $tagName;
568 if (count($class)) {
569 foreach ($class as $v) {
570 $this->value['id_lst'][] = '*.'.$v;
571 $this->value['id_lst'][] = '.'.$v;
572 $this->value['id_lst'][] = $tagName.'.'.$v;
575 if ($id) {
576 $this->value['id_lst'][] = '*#'.$id;
577 $this->value['id_lst'][] = '#'.$id;
578 $this->value['id_lst'][] = $tagName.'#'.$id;
581 // get the css styles from class
582 $styles = $this->_getFromCSS();
584 // merge with the css styles from tag
585 $styles = array_merge($styles, $param['style']);
586 if (isset($param['allwidth']) && !isset($styles['width'])) $styles['width'] = '100%';
588 // reset some styles, depending on the tag name
589 $this->resetStyle($tagName);
591 // add the legacy values
592 if ($legacy) {
593 foreach ($legacy as $legacyName => $legacyValue) {
594 if (is_array($legacyValue)) {
595 foreach($legacyValue as $legacy2Name => $legacy2Value)
596 $this->value[$legacyName][$legacy2Name] = $legacy2Value;
597 } else {
598 $this->value[$legacyName] = $legacyValue;
603 // some flags
604 $correctWidth = false;
605 $noWidth = true;
607 // read all the css styles
608 foreach ($styles as $nom => $val) {
609 switch($nom)
611 case 'font-family':
612 $val = explode(',', $val);
613 $val = trim($val[0]);
614 if ($val) $this->value['font-family'] = $val;
615 break;
617 case 'font-weight':
618 $this->value['font-bold'] = ($val=='bold');
619 break;
621 case 'font-style':
622 $this->value['font-italic'] = ($val=='italic');
623 break;
625 case 'text-decoration':
626 $val = explode(' ', $val);
627 $this->value['font-underline'] = (in_array('underline', $val));
628 $this->value['font-overline'] = (in_array('overline', $val));
629 $this->value['font-linethrough'] = (in_array('line-through', $val));
630 break;
632 case 'text-indent':
633 $this->value['text-indent'] = $this->convertToMM($val);
634 break;
636 case 'text-transform':
637 if (!in_array($val, array('none', 'capitalize', 'uppercase', 'lowercase'))) $val = 'none';
638 $this->value['text-transform'] = $val;
639 break;
641 case 'font-size':
642 $val = $this->convertToMM($val, $this->value['font-size']);
643 if ($val) $this->value['font-size'] = $val;
644 break;
646 case 'color':
647 $res = null;
648 $this->value['color'] = $this->convertToColor($val, $res);
649 if ($tagName=='hr') {
650 $this->value['border']['l']['color'] = $this->value['color'];
651 $this->value['border']['t']['color'] = $this->value['color'];
652 $this->value['border']['r']['color'] = $this->value['color'];
653 $this->value['border']['b']['color'] = $this->value['color'];
655 break;
657 case 'text-align':
658 $val = strtolower($val);
659 if (!in_array($val, array('left', 'right', 'center', 'justify', 'li_right'))) $val = 'left';
660 $this->value['text-align'] = $val;
661 break;
663 case 'vertical-align':
664 $this->value['vertical-align'] = $val;
665 break;
667 case 'width':
668 $this->value['width'] = $this->convertToMM($val, $this->getLastWidth());
669 if ($this->value['width'] && substr($val, -1)=='%') $correctWidth=true;
670 $noWidth = false;
671 break;
673 case 'height':
674 $this->value['height'] = $this->convertToMM($val, $this->getLastHeight());
675 break;
677 case 'line-height':
678 if (preg_match('/^[0-9\.]+$/isU', $val)) $val = floor($val*100).'%';
679 $this->value['line-height'] = $val;
680 break;
682 case 'rotate':
683 if (!in_array($val, array(0, -90, 90, 180, 270, -180, -270))) $val = null;
684 if ($val<0) $val+= 360;
685 $this->value['rotate'] = $val;
686 break;
688 case 'overflow':
689 if (!in_array($val, array('visible', 'hidden'))) $val = 'visible';
690 $this->value['overflow'] = $val;
691 break;
693 case 'padding':
694 $val = explode(' ', $val);
695 foreach ($val as $k => $v) {
696 $v = trim($v);
697 if ($v!='') {
698 $val[$k] = $v;
699 } else {
700 unset($val[$k]);
703 $val = array_values($val);
704 $this->_duplicateBorder($val);
705 $this->value['padding']['t'] = $this->convertToMM($val[0], 0);
706 $this->value['padding']['r'] = $this->convertToMM($val[1], 0);
707 $this->value['padding']['b'] = $this->convertToMM($val[2], 0);
708 $this->value['padding']['l'] = $this->convertToMM($val[3], 0);
709 break;
711 case 'padding-top':
712 $this->value['padding']['t'] = $this->convertToMM($val, 0);
713 break;
715 case 'padding-right':
716 $this->value['padding']['r'] = $this->convertToMM($val, 0);
717 break;
719 case 'padding-bottom':
720 $this->value['padding']['b'] = $this->convertToMM($val, 0);
721 break;
723 case 'padding-left':
724 $this->value['padding']['l'] = $this->convertToMM($val, 0);
725 break;
727 case 'margin':
728 if ($val=='auto') {
729 $this->value['margin-auto'] = true;
730 break;
732 $val = explode(' ', $val);
733 foreach ($val as $k => $v) {
734 $v = trim($v);
735 if ($v!='') {
736 $val[$k] = $v;
737 } else {
738 unset($val[$k]);
741 $val = array_values($val);
742 $this->_duplicateBorder($val);
743 $this->value['margin']['t'] = $this->convertToMM($val[0], 0);
744 $this->value['margin']['r'] = $this->convertToMM($val[1], 0);
745 $this->value['margin']['b'] = $this->convertToMM($val[2], 0);
746 $this->value['margin']['l'] = $this->convertToMM($val[3], 0);
747 break;
749 case 'margin-top':
750 $this->value['margin']['t'] = $this->convertToMM($val, 0);
751 break;
753 case 'margin-right':
754 $this->value['margin']['r'] = $this->convertToMM($val, 0);
755 break;
757 case 'margin-bottom':
758 $this->value['margin']['b'] = $this->convertToMM($val, 0);
759 break;
761 case 'margin-left':
762 $this->value['margin']['l'] = $this->convertToMM($val, 0);
763 break;
765 case 'border':
766 $val = $this->readBorder($val);
767 $this->value['border']['t'] = $val;
768 $this->value['border']['r'] = $val;
769 $this->value['border']['b'] = $val;
770 $this->value['border']['l'] = $val;
771 break;
773 case 'border-style':
774 $val = explode(' ', $val);
775 foreach ($val as $valK => $valV) {
776 if (!in_array($valV, array('solid', 'dotted', 'dashed'))) {
777 $val[$valK] = null;
780 $this->_duplicateBorder($val);
781 if ($val[0]) $this->value['border']['t']['type'] = $val[0];
782 if ($val[1]) $this->value['border']['r']['type'] = $val[1];
783 if ($val[2]) $this->value['border']['b']['type'] = $val[2];
784 if ($val[3]) $this->value['border']['l']['type'] = $val[3];
785 break;
787 case 'border-top-style':
788 if (in_array($val, array('solid', 'dotted', 'dashed'))) {
789 $this->value['border']['t']['type'] = $val;
791 break;
793 case 'border-right-style':
794 if (in_array($val, array('solid', 'dotted', 'dashed'))) {
795 $this->value['border']['r']['type'] = $val;
797 break;
799 case 'border-bottom-style':
800 if (in_array($val, array('solid', 'dotted', 'dashed'))) {
801 $this->value['border']['b']['type'] = $val;
803 break;
805 case 'border-left-style':
806 if (in_array($val, array('solid', 'dotted', 'dashed')))
807 $this->value['border']['l']['type'] = $val;
808 break;
810 case 'border-color':
811 $res = false;
812 $val = preg_replace('/,[\s]+/', ',', $val);
813 $val = explode(' ', $val);
814 foreach ($val as $valK => $valV) {
815 $val[$valK] = $this->convertToColor($valV, $res);
816 if (!$res) {
817 $val[$valK] = null;
820 $this->_duplicateBorder($val);
821 if (is_array($val[0])) $this->value['border']['t']['color'] = $val[0];
822 if (is_array($val[1])) $this->value['border']['r']['color'] = $val[1];
823 if (is_array($val[2])) $this->value['border']['b']['color'] = $val[2];
824 if (is_array($val[3])) $this->value['border']['l']['color'] = $val[3];
826 break;
828 case 'border-top-color':
829 $res = false;
830 $val = $this->convertToColor($val, $res);
831 if ($res) $this->value['border']['t']['color'] = $val;
832 break;
834 case 'border-right-color':
835 $res = false;
836 $val = $this->convertToColor($val, $res);
837 if ($res) $this->value['border']['r']['color'] = $val;
838 break;
840 case 'border-bottom-color':
841 $res = false;
842 $val = $this->convertToColor($val, $res);
843 if ($res) $this->value['border']['b']['color'] = $val;
844 break;
846 case 'border-left-color':
847 $res = false;
848 $val = $this->convertToColor($val, $res);
849 if ($res) $this->value['border']['l']['color'] = $val;
850 break;
852 case 'border-width':
853 $val = explode(' ', $val);
854 foreach ($val as $valK => $valV) {
855 $val[$valK] = $this->convertToMM($valV, 0);
857 $this->_duplicateBorder($val);
858 if ($val[0]) $this->value['border']['t']['width'] = $val[0];
859 if ($val[1]) $this->value['border']['r']['width'] = $val[1];
860 if ($val[2]) $this->value['border']['b']['width'] = $val[2];
861 if ($val[3]) $this->value['border']['l']['width'] = $val[3];
862 break;
864 case 'border-top-width':
865 $val = $this->convertToMM($val, 0);
866 if ($val) $this->value['border']['t']['width'] = $val;
867 break;
869 case 'border-right-width':
870 $val = $this->convertToMM($val, 0);
871 if ($val) $this->value['border']['r']['width'] = $val;
872 break;
874 case 'border-bottom-width':
875 $val = $this->convertToMM($val, 0);
876 if ($val) $this->value['border']['b']['width'] = $val;
877 break;
879 case 'border-left-width':
880 $val = $this->convertToMM($val, 0);
881 if ($val) $this->value['border']['l']['width'] = $val;
882 break;
884 case 'border-collapse':
885 if ($tagName=='table') $this->value['border']['collapse'] = ($val=='collapse');
886 break;
888 case 'border-radius':
889 $val = explode('/', $val);
890 if (count($val)>2) {
891 break;
893 $valH = $this->convertToRadius(trim($val[0]));
894 if (count($valH)<1 || count($valH)>4) {
895 break;
897 if (!isset($valH[1])) $valH[1] = $valH[0];
898 if (!isset($valH[2])) $valH = array($valH[0], $valH[0], $valH[1], $valH[1]);
899 if (!isset($valH[3])) $valH[3] = $valH[1];
900 if (isset($val[1])) {
901 $valV = $this->convertToRadius(trim($val[1]));
902 if (count($valV)<1 || count($valV)>4) {
903 break;
905 if (!isset($valV[1])) $valV[1] = $valV[0];
906 if (!isset($valV[2])) $valV = array($valV[0], $valV[0], $valV[1], $valV[1]);
907 if (!isset($valV[3])) $valV[3] = $valV[1];
908 } else {
909 $valV = $valH;
911 $this->value['border']['radius'] = array(
912 'tl' => array($valH[0], $valV[0]),
913 'tr' => array($valH[1], $valV[1]),
914 'br' => array($valH[2], $valV[2]),
915 'bl' => array($valH[3], $valV[3])
917 break;
919 case 'border-top-left-radius':
920 $val = $this->convertToRadius($val);
921 if (count($val)<1 || count($val)>2) {
922 break;
924 $this->value['border']['radius']['tl'] = array($val[0], isset($val[1]) ? $val[1] : $val[0]);
925 break;
927 case 'border-top-right-radius':
928 $val = $this->convertToRadius($val);
929 if (count($val)<1 || count($val)>2) {
930 break;
932 $this->value['border']['radius']['tr'] = array($val[0], isset($val[1]) ? $val[1] : $val[0]);
933 break;
935 case 'border-bottom-right-radius':
936 $val = $this->convertToRadius($val);
937 if (count($val)<1 || count($val)>2) {
938 break;
940 $this->value['border']['radius']['br'] = array($val[0], isset($val[1]) ? $val[1] : $val[0]);
941 break;
943 case 'border-bottom-left-radius':
944 $val = $this->convertToRadius($val);
945 if (count($val)<1 || count($val)>2) {
946 break;
948 $this->value['border']['radius']['bl'] = array($val[0], isset($val[1]) ? $val[1] : $val[0]);
949 break;
951 case 'border-top':
952 $this->value['border']['t'] = $this->readBorder($val);
953 break;
955 case 'border-right':
956 $this->value['border']['r'] = $this->readBorder($val);
957 break;
959 case 'border-bottom':
960 $this->value['border']['b'] = $this->readBorder($val);
961 break;
963 case 'border-left':
964 $this->value['border']['l'] = $this->readBorder($val);
965 break;
967 case 'background-color':
968 $this->value['background']['color'] = $this->convertBackgroundColor($val);
969 break;
971 case 'background-image':
972 $this->value['background']['image'] = $this->convertBackgroundImage($val);
973 break;
975 case 'background-position':
976 $res = null;
977 $this->value['background']['position'] = $this->convertBackgroundPosition($val, $res);
978 break;
980 case 'background-repeat':
981 $this->value['background']['repeat'] = $this->convertBackgroundRepeat($val);
982 break;
984 case 'background':
985 $this->convertBackground($val, $this->value['background']);
986 break;
988 case 'position':
989 if ($val=='absolute') $this->value['position'] = 'absolute';
990 else if ($val=='relative') $this->value['position'] = 'relative';
991 else $this->value['position'] = null;
992 break;
994 case 'float':
995 if ($val=='left') $this->value['float'] = 'left';
996 else if ($val=='right') $this->value['float'] = 'right';
997 else $this->value['float'] = null;
998 break;
1000 case 'display':
1001 if ($val=='inline') $this->value['display'] = 'inline';
1002 else if ($val=='block') $this->value['display'] = 'block';
1003 else if ($val=='none') $this->value['display'] = 'none';
1004 else $this->value['display'] = null;
1005 break;
1007 case 'top':
1008 case 'bottom':
1009 case 'left':
1010 case 'right':
1011 $this->value[$nom] = $val;
1012 break;
1014 case 'list-style':
1015 case 'list-style-type':
1016 case 'list-style-image':
1017 if ($nom=='list-style') $nom = 'list-style-type';
1018 $this->value[$nom] = $val;
1019 break;
1021 default:
1022 break;
1026 $return = true;
1028 // only for P tag
1029 if ($this->value['margin']['t']===null) $this->value['margin']['t'] = $this->value['font-size'];
1030 if ($this->value['margin']['b']===null) $this->value['margin']['b'] = $this->value['font-size'];
1032 // force the text align to left, if asked by html2pdf
1033 if ($this->_onlyLeft) $this->value['text-align'] = 'left';
1035 // correction on the width (quick box)
1036 if ($noWidth && in_array($tagName, array('div', 'blockquote', 'fieldset')) && $this->value['position']!='absolute') {
1037 $this->value['width'] = $this->getLastWidth();
1038 $this->value['width']-= $this->value['margin']['l'] + $this->value['margin']['r'];
1039 } else {
1040 if ($correctWidth) {
1041 if (!in_array($tagName, array('table', 'div', 'blockquote', 'fieldset', 'hr'))) {
1042 $this->value['width']-= $this->value['padding']['l'] + $this->value['padding']['r'];
1043 $this->value['width']-= $this->value['border']['l']['width'] + $this->value['border']['r']['width'];
1045 if (in_array($tagName, array('th', 'td'))) {
1046 $this->value['width']-= $this->convertToMM(isset($param['cellspacing']) ? $param['cellspacing'] : '2px');
1047 $return = false;
1049 if ($this->value['width']<0) $this->value['width']=0;
1050 } else {
1051 if ($this->value['width']) {
1052 if ($this->value['border']['l']['width']) $this->value['width'] += $this->value['border']['l']['width'];
1053 if ($this->value['border']['r']['width']) $this->value['width'] += $this->value['border']['r']['width'];
1054 if ($this->value['padding']['l']) $this->value['width'] += $this->value['padding']['l'];
1055 if ($this->value['padding']['r']) $this->value['width'] += $this->value['padding']['r'];
1059 if ($this->value['height']) {
1060 if ($this->value['border']['b']['width']) $this->value['height'] += $this->value['border']['b']['width'];
1061 if ($this->value['border']['t']['width']) $this->value['height'] += $this->value['border']['t']['width'];
1062 if ($this->value['padding']['b']) $this->value['height'] += $this->value['padding']['b'];
1063 if ($this->value['padding']['t']) $this->value['height'] += $this->value['padding']['t'];
1066 if ($this->value['top']!=null) $this->value['top'] = $this->convertToMM($this->value['top'], $this->getLastHeight(true));
1067 if ($this->value['bottom']!=null) $this->value['bottom'] = $this->convertToMM($this->value['bottom'], $this->getLastHeight(true));
1068 if ($this->value['left']!=null) $this->value['left'] = $this->convertToMM($this->value['left'], $this->getLastWidth(true));
1069 if ($this->value['right']!=null) $this->value['right'] = $this->convertToMM($this->value['right'], $this->getLastWidth(true));
1071 if ($this->value['top'] && $this->value['bottom'] && $this->value['height']) $this->value['bottom'] = null;
1072 if ($this->value['left'] && $this->value['right'] && $this->value['width']) $this->value['right'] = null;
1074 return $return;
1078 * get the height of the current line
1080 * @access public
1081 * @return float $height in mm
1083 public function getLineHeight()
1085 $val = $this->value['line-height'];
1086 if ($val=='normal') $val = '108%';
1087 return $this->convertToMM($val, $this->value['font-size']);
1091 * get the width of the parent
1093 * @access public
1094 * @param boolean $mode true => adding padding and border
1095 * @return float $width in mm
1097 public function getLastWidth($mode = false)
1099 for ($k=count($this->table)-1; $k>=0; $k--) {
1100 if ($this->table[$k]['width']) {
1101 $w = $this->table[$k]['width'];
1102 if ($mode) {
1103 $w+= $this->table[$k]['border']['l']['width'] + $this->table[$k]['padding']['l'] + 0.02;
1104 $w+= $this->table[$k]['border']['r']['width'] + $this->table[$k]['padding']['r'] + 0.02;
1106 return $w;
1109 return $this->_pdf->getW() - $this->_pdf->getlMargin() - $this->_pdf->getrMargin();
1113 * get the height of the parent
1115 * @access public
1116 * @param boolean $mode true => adding padding and border
1117 * @return float $height in mm
1119 public function getLastHeight($mode = false)
1121 for ($k=count($this->table)-1; $k>=0; $k--) {
1122 if ($this->table[$k]['height']) {
1123 $h = $this->table[$k]['height'];
1124 if ($mode) {
1125 $h+= $this->table[$k]['border']['t']['width'] + $this->table[$k]['padding']['t'] + 0.02;
1126 $h+= $this->table[$k]['border']['b']['width'] + $this->table[$k]['padding']['b'] + 0.02;
1128 return $h;
1131 return $this->_pdf->getH() - $this->_pdf->gettMargin() - $this->_pdf->getbMargin();
1135 * get the value of the float property
1137 * @access public
1138 * @return $float left/right
1140 public function getFloat()
1142 if ($this->value['float']=='left') return 'left';
1143 if ($this->value['float']=='right') return 'right';
1144 return null;
1148 * get the last value for a specific key
1150 * @access public
1151 * @param string $key
1152 * @return mixed
1154 public function getLastValue($key)
1156 $nb = count($this->table);
1157 if ($nb>0) {
1158 return $this->table[$nb-1][$key];
1159 } else {
1160 return null;
1165 * get the last absolute X
1167 * @access protected
1168 * @return float $x
1170 protected function _getLastAbsoluteX()
1172 for ($k=count($this->table)-1; $k>=0; $k--) {
1173 if ($this->table[$k]['x'] && $this->table[$k]['position']) return $this->table[$k]['x'];
1175 return $this->_pdf->getlMargin();
1179 * get the last absolute Y
1181 * @access protected
1182 * @return float $y
1184 protected function _getLastAbsoluteY()
1186 for ($k=count($this->table)-1; $k>=0; $k--) {
1187 if ($this->table[$k]['y'] && $this->table[$k]['position']) return $this->table[$k]['y'];
1189 return $this->_pdf->gettMargin();
1193 * get the CSS properties of the current tag
1195 * @access protected
1196 * @return array $styles
1198 protected function _getFromCSS()
1200 // styles to apply
1201 $styles = array();
1203 // list of the selectors to get in the CSS files
1204 $getit = array();
1206 // get the list of the selectors of each tags
1207 $lst = array();
1208 $lst[] = $this->value['id_lst'];
1209 for ($i=count($this->table)-1; $i>=0; $i--) {
1210 $lst[] = $this->table[$i]['id_lst'];
1213 // foreach selectors in the CSS files, verify if it match with the list of selectors
1214 foreach ($this->cssKeys as $key => $num) {
1215 if ($this->_getReccursiveStyle($key, $lst)) {
1216 $getit[$key] = $num;
1220 // if we have selectors
1221 if (count($getit)) {
1222 // get them, but in the definition order, because of priority
1223 asort($getit);
1224 foreach ($getit as $key => $val) $styles = array_merge($styles, $this->css[$key]);
1227 return $styles;
1231 * identify if the selector $key match with the list of tag selectors
1233 * @access protected
1234 * @param string $key CSS selector to analyse
1235 * @param array $lst list of the selectors of each tags
1236 * @param string $next next step of parsing the selector
1237 * @return boolean
1239 protected function _getReccursiveStyle($key, $lst, $next = null)
1241 // if next step
1242 if ($next!==null) {
1243 // we remove this step
1244 if ($next) $key = trim(substr($key, 0, -strlen($next)));
1245 array_shift($lst);
1247 // if no more step to identify => return false
1248 if (!count($lst)) {
1249 return false;
1253 // for each selector of the current step
1254 foreach ($lst[0] as $name) {
1255 // if selector = key => ok
1256 if ($key==$name) {
1257 return true;
1260 // if the end of the key = the selector and the next step is ok => ok
1261 if (substr($key, -strlen(' '.$name))==' '.$name && $this->_getReccursiveStyle($key, $lst, $name)) {
1262 return true;
1266 // if we are not in the first step, we analyse the sub steps (the pareng tag of the current tag)
1267 if ($next!==null && $this->_getReccursiveStyle($key, $lst, '')) {
1268 return true;
1271 // no corresponding found
1272 return false;
1276 * Analyse a border
1278 * @access public
1279 * @param string $css css border properties
1280 * @return array border properties
1282 public function readBorder($css)
1284 // border none
1285 $none = array('type' => 'none', 'width' => 0, 'color' => array(0, 0, 0));
1287 // default value
1288 $type = 'solid';
1289 $width = $this->convertToMM('1pt');
1290 $color = array(0, 0, 0);
1292 // clean up the values
1293 $css = explode(' ', $css);
1294 foreach ($css as $k => $v) {
1295 $v = trim($v);
1296 if ($v) $css[$k] = $v;
1297 else unset($css[$k]);
1299 $css = array_values($css);
1301 // read the values
1302 $res = null;
1303 foreach ($css as $value) {
1305 // if no border => return none
1306 if ($value=='none' || $value=='hidden') {
1307 return $none;
1310 // try to convert the value as a distance
1311 $tmp = $this->convertToMM($value);
1313 // if the convert is ok => it is a width
1314 if ($tmp!==null) {
1315 $width = $tmp;
1316 // else, it could be the type
1317 } else if (in_array($value, array('solid', 'dotted', 'dashed', 'double'))) {
1318 $type = $value;
1319 // else, it could be the color
1320 } else {
1321 $tmp = $this->convertToColor($value, $res);
1322 if ($res) $color = $tmp;
1326 // if no witdh => return none
1327 if (!$width) return $none;
1329 // return the border properties
1330 return array('type' => $type, 'width' => $width, 'color' => $color);
1334 * duplicate the borders if needed
1336 * @access protected
1337 * @param &array $val
1339 protected function _duplicateBorder(&$val)
1341 // 1 value => L => RTB
1342 if (count($val)==1) {
1343 $val[1] = $val[0];
1344 $val[2] = $val[0];
1345 $val[3] = $val[0];
1346 // 2 values => L => R & T => B
1347 } else if (count($val)==2) {
1348 $val[2] = $val[0];
1349 $val[3] = $val[1];
1350 // 3 values => T => B
1351 } else if (count($val)==3) {
1352 $val[3] = $val[1];
1357 * Analyse a background
1359 * @access public
1360 * @param string $css css background properties
1361 * @param &array $value parsed values (by reference, because, ther is a legacy of the parent CSS properties)
1363 public function convertBackground($css, &$value)
1365 // is there a image ?
1366 $text = '/url\(([^)]*)\)/isU';
1367 if (preg_match($text, $css, $match)) {
1368 // get the image
1369 $value['image'] = $this->convertBackgroundImage($match[0]);
1371 // remove if from the css properties
1372 $css = preg_replace($text, '', $css);
1373 $css = preg_replace('/[\s]+/', ' ', $css);
1376 // protect some spaces
1377 $css = preg_replace('/,[\s]+/', ',', $css);
1379 // explode the values
1380 $css = explode(' ', $css);
1382 // background position to parse
1383 $pos = '';
1385 // foreach value
1386 foreach ($css as $val) {
1387 // try to parse the value as a color
1388 $ok = false;
1389 $color = $this->convertToColor($val, $ok);
1391 // if ok => it is a color
1392 if ($ok) {
1393 $value['color'] = $color;
1394 // else if transparent => no coloàr
1395 } else if ($val=='transparent') {
1396 $value['color'] = null;
1397 // else
1398 } else {
1399 // try to parse the value as a repeat
1400 $repeat = $this->convertBackgroundRepeat($val);
1402 // if ok => it is repeat
1403 if ($repeat) {
1404 $value['repeat'] = $repeat;
1405 // else => it could only be a position
1406 } else {
1407 $pos.= ($pos ? ' ' : '').$val;
1412 // if we have a position to parse
1413 if ($pos) {
1414 // try to read it
1415 $pos = $this->convertBackgroundPosition($pos, $ok);
1416 if ($ok) $value['position'] = $pos;
1421 * parse a background color
1423 * @access public
1424 * @param string $css
1425 * @return string $value
1427 public function convertBackgroundColor($css)
1429 $res = null;
1430 if ($css=='transparent') return null;
1431 else return $this->convertToColor($css, $res);
1435 * parse a background image
1437 * @access public
1438 * @param string $css
1439 * @return string $value
1441 public function convertBackgroundImage($css)
1443 if ($css=='none')
1444 return null;
1445 else if (preg_match('/^url\(([^)]*)\)$/isU', $css, $match))
1446 return $match[1];
1447 else
1448 return null;
1452 * parse a background position
1454 * @access public
1455 * @param string $css
1456 * @param &boolean $res flag if conver is ok or not
1457 * @return array ($x, $y)
1459 public function convertBackgroundPosition($css, &$res)
1461 // init the res
1462 $res = false;
1464 // explode the value
1465 $css = explode(' ', $css);
1467 // we must have 2 values. if 0 or >2 : error. if 1 => put center for 2
1468 if (count($css)<2) {
1469 if (!$css[0]) return null;
1470 $css[1] = 'center';
1472 if (count($css)>2) return null;
1474 // prepare the values
1475 $x = 0;
1476 $y = 0;
1477 $res = true;
1479 // convert the first value
1480 if ($css[0]=='left') $x = '0%';
1481 else if ($css[0]=='center') $x = '50%';
1482 else if ($css[0]=='right') $x = '100%';
1483 else if ($css[0]=='top') $y = '0%';
1484 else if ($css[0]=='bottom') $y = '100%';
1485 else if (preg_match('/^[-]?[0-9\.]+%$/isU', $css[0])) $x = $css[0];
1486 else if ($this->convertToMM($css[0])) $x = $this->convertToMM($css[0]);
1487 else $res = false;
1489 // convert the second value
1490 if ($css[1]=='left') $x = '0%';
1491 else if ($css[1]=='right') $x = '100%';
1492 else if ($css[1]=='top') $y = '0%';
1493 else if ($css[1]=='center') $y = '50%';
1494 else if ($css[1]=='bottom') $y = '100%';
1495 else if (preg_match('/^[-]?[0-9\.]+%$/isU', $css[1])) $y = $css[1];
1496 else if ($this->convertToMM($css[1])) $y = $this->convertToMM($css[1]);
1497 else $res = false;
1499 // return the values
1500 return array($x, $y);
1504 * parse a background repeat
1506 * @access public
1507 * @param string $css
1508 * @return string $value
1510 public function convertBackgroundRepeat($css)
1512 switch($css)
1514 case 'repeat':
1515 return array(true, true);
1516 case 'repeat-x':
1517 return array(true, false);
1518 case 'repeat-y':
1519 return array(false, true);
1520 case 'no-repeat':
1521 return array(false, false);
1523 return null;
1527 * convert a distance to mm
1529 * @access public
1530 * @param string $css distance to convert
1531 * @param float $old parent distance
1532 * @return float $value
1534 public function convertToMM($css, $old=0.)
1536 $css = trim($css);
1537 if (preg_match('/^[0-9\.\-]+$/isU', $css)) $css.= 'px';
1538 if (preg_match('/^[0-9\.\-]+px$/isU', $css)) $css = 25.4/96. * str_replace('px', '', $css);
1539 else if (preg_match('/^[0-9\.\-]+pt$/isU', $css)) $css = 25.4/72. * str_replace('pt', '', $css);
1540 else if (preg_match('/^[0-9\.\-]+in$/isU', $css)) $css = 25.4 * str_replace('in', '', $css);
1541 else if (preg_match('/^[0-9\.\-]+mm$/isU', $css)) $css = 1.*str_replace('mm', '', $css);
1542 else if (preg_match('/^[0-9\.\-]+%$/isU', $css)) $css = 1.*$old*str_replace('%', '', $css)/100.;
1543 else $css = null;
1545 return $css;
1549 * convert a css radius
1551 * @access public
1552 * @param string $css
1553 * @return float $value
1555 public function convertToRadius($css)
1557 // explode the value
1558 $css = explode(' ', $css);
1560 foreach ($css as $k => $v) {
1561 $v = trim($v);
1562 if ($v) {
1563 $v = $this->convertToMM($v, 0);
1564 if ($v!==null) {
1565 $css[$k] = $v;
1566 } else {
1567 unset($css[$k]);
1569 } else {
1570 unset($css[$k]);
1574 return array_values($css);
1578 * convert a css color
1580 * @access public
1581 * @param string $css
1582 * @param &boolean $res
1583 * @return array (r,g, b)
1585 public function convertToColor($css, &$res)
1587 // prepare the value
1588 $css = trim($css);
1589 $res = true;
1591 // if transparent => return null
1592 if (strtolower($css)=='transparent') return array(null, null, null);
1594 // HTML color
1595 if (isset($this->_htmlColor[strtolower($css)])) {
1596 $css = $this->_htmlColor[strtolower($css)];
1597 $r = floatVal(hexdec(substr($css, 0, 2)));
1598 $v = floatVal(hexdec(substr($css, 2, 2)));
1599 $b = floatVal(hexdec(substr($css, 4, 2)));
1600 return array($r, $v, $b);
1603 // like #FFFFFF
1604 if (preg_match('/^#[0-9A-Fa-f]{6}$/isU', $css)) {
1605 $r = floatVal(hexdec(substr($css, 1, 2)));
1606 $v = floatVal(hexdec(substr($css, 3, 2)));
1607 $b = floatVal(hexdec(substr($css, 5, 2)));
1608 return array($r, $v, $b);
1611 // like #FFF
1612 if (preg_match('/^#[0-9A-F]{3}$/isU', $css)) {
1613 $r = floatVal(hexdec(substr($css, 1, 1).substr($css, 1, 1)));
1614 $v = floatVal(hexdec(substr($css, 2, 1).substr($css, 2, 1)));
1615 $b = floatVal(hexdec(substr($css, 3, 1).substr($css, 3, 1)));
1616 return array($r, $v, $b);
1619 // like rgb(100, 100, 100)
1620 if (preg_match('/rgb\([\s]*([0-9%\.]+)[\s]*,[\s]*([0-9%\.]+)[\s]*,[\s]*([0-9%\.]+)[\s]*\)/isU', $css, $match)) {
1621 $r = $this->_convertSubColor($match[1]);
1622 $v = $this->_convertSubColor($match[2]);
1623 $b = $this->_convertSubColor($match[3]);
1624 return array($r*255., $v*255., $b*255.);
1627 // like cmyk(100, 100, 100, 100)
1628 if (preg_match('/cmyk\([\s]*([0-9%\.]+)[\s]*,[\s]*([0-9%\.]+)[\s]*,[\s]*([0-9%\.]+)[\s]*,[\s]*([0-9%\.]+)[\s]*\)/isU', $css, $match)) {
1629 $c = $this->_convertSubColor($match[1]);
1630 $m = $this->_convertSubColor($match[2]);
1631 $y = $this->_convertSubColor($match[3]);
1632 $k = $this->_convertSubColor($match[4]);
1633 return array($c*100., $m*100., $y*100., $k*100.);
1636 $res = false;
1637 return array(0., 0., 0.);
1641 * color value to convert
1643 * @access protected
1644 * @param string $c
1645 * @return float $c 0.->1.
1647 protected function _convertSubColor($c)
1649 if (substr($c, -1)=='%') {
1650 $c = floatVal(substr($c, 0, -1))/100.;
1651 } else {
1652 $c = floatVal($c);
1653 if ($c>1) $c = $c/255.;
1656 return $c;
1660 * read a css content
1662 * @access protected
1663 * @param &string $code
1665 protected function _analyseStyle(&$code)
1667 // clean the spaces
1668 $code = preg_replace('/[\s]+/', ' ', $code);
1670 // remove the comments
1671 $code = preg_replace('/\/\*.*?\*\//s', '', $code);
1673 // split each CSS code "selector { value }"
1674 preg_match_all('/([^{}]+){([^}]*)}/isU', $code, $match);
1676 // for each CSS code
1677 for ($k=0; $k<count($match[0]); $k++) {
1679 // selectors
1680 $names = strtolower(trim($match[1][$k]));
1682 // css style
1683 $styles = trim($match[2][$k]);
1685 // explode each value
1686 $styles = explode(';', $styles);
1688 // parse each value
1689 $css = array();
1690 foreach ($styles as $style) {
1691 $tmp = explode(':', $style);
1692 if (count($tmp)>1) {
1693 $cod = $tmp[0]; unset($tmp[0]); $tmp = implode(':', $tmp);
1694 $css[trim(strtolower($cod))] = trim($tmp);
1698 // explode the names
1699 $names = explode(',', $names);
1701 // save the values for each names
1702 foreach ($names as $name) {
1703 // clean the name
1704 $name = trim($name);
1706 // if a selector with somethink lige :hover => continue
1707 if (strpos($name, ':')!==false) continue;
1709 // save the value
1710 if (!isset($this->css[$name]))
1711 $this->css[$name] = $css;
1712 else
1713 $this->css[$name] = array_merge($this->css[$name], $css);
1718 // get he list of the keys
1719 $this->cssKeys = array_flip(array_keys($this->css));
1723 * Extract the css files from a html code
1725 * @access public
1726 * @param string &$html
1728 public function readStyle(&$html)
1730 // the CSS content
1731 $style = ' ';
1733 // extract the link tags, and remove them in the html code
1734 preg_match_all('/<link([^>]*)>/isU', $html, $match);
1735 $html = preg_replace('/<link[^>]*>/isU', '', $html);
1736 $html = preg_replace('/<\/link[^>]*>/isU', '', $html);
1738 // analyse each link tag
1739 foreach ($match[1] as $code) {
1740 $tmp = array();
1742 // read the attributes name=value
1743 $prop = '([a-zA-Z0-9_]+)=([^"\'\s>]+)';
1744 preg_match_all('/'.$prop.'/is', $code, $match);
1745 for ($k=0; $k<count($match[0]); $k++) {
1746 $tmp[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
1749 // read the attributes name="value"
1750 $prop = '([a-zA-Z0-9_]+)=["]([^"]*)["]';
1751 preg_match_all('/'.$prop.'/is', $code, $match);
1752 for ($k=0; $k<count($match[0]); $k++) {
1753 $tmp[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
1756 // read the attributes name='value'
1757 $prop = "([a-zA-Z0-9_]+)=[']([^']*)[']";
1758 preg_match_all('/'.$prop.'/is', $code, $match);
1759 for ($k=0; $k<count($match[0]); $k++) {
1760 $tmp[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
1763 // if type text/css => we keep it
1764 if (isset($tmp['type']) && strtolower($tmp['type'])=='text/css' && isset($tmp['href'])) {
1766 // get the href
1767 $url = $tmp['href'];
1769 // get the content of the css file
1770 $content = @file_get_contents($url);
1772 // if "http://" in the url
1773 if (strpos($url, 'http://')!==false) {
1775 // get the domain "http://xxx/"
1776 $url = str_replace('http://', '', $url);
1777 $url = explode('/', $url);
1778 $urlMain = 'http://'.$url[0].'/';
1780 // get the absolute url of the path
1781 $urlSelf = $url; unset($urlSelf[count($urlSelf)-1]); $urlSelf = 'http://'.implode('/', $urlSelf).'/';
1783 // adapt the url in the css content
1784 $content = preg_replace('/url\(([^\\\\][^)]*)\)/isU', 'url('.$urlSelf.'$1)', $content);
1785 $content = preg_replace('/url\((\\\\[^)]*)\)/isU', 'url('.$urlMain.'$1)', $content);
1786 } else {
1787 // @TODO correction on url in absolute on a local css content
1788 // $content = preg_replace('/url\(([^)]*)\)/isU', 'url('.dirname($url).'/$1)', $content);
1791 // add to the CSS content
1792 $style.= $content."\n";
1796 // extract the style tags des tags style, and remove them in the html code
1797 preg_match_all('/<style[^>]*>(.*)<\/style[^>]*>/isU', $html, $match);
1798 $html = preg_replace('/<style[^>]*>(.*)<\/style[^>]*>/isU', '', $html);
1800 // analyse each style tags
1801 foreach ($match[1] as $code) {
1802 // add to the CSS content
1803 $code = str_replace('<!--', '', $code);
1804 $code = str_replace('-->', '', $code);
1805 $style.= $code."\n";
1808 //analyse the css content
1809 $this->_analyseStyle($style);