function _fix_tag_display($default_display, &$state, &$pipeline) { // In some cases 'display' CSS property should be ignored for element-generated boxes // Here we will use the $default_display stored above // Note that "display: none" should _never_ be changed // $handler =& CSS::get_handler(CSS_DISPLAY); if ($handler->get($state->getState()) === "none") { return; } switch ($default_display) { case 'table-cell': // TD will always have 'display: table-cell' $handler->css('table-cell', $pipeline); break; case '-button': // INPUT buttons will always have 'display: -button' (in latter case if display = 'block', we'll use a wrapper box) $css_state =& $pipeline->getCurrentCSSState(); if ($handler->get($css_state->getState()) === 'block') { $need_block_wrapper = true; } $handler->css('-button', $pipeline); break; } }
function parse($value) { if ($value == 'inherit') { return CSS_PROPERTY_INHERIT; } // Remove spaces between color values in rgb() color definition; this will allow us to tread // this declaration as a single value $value = preg_replace("/\\s*,\\s*/", ",", $value); // Remove spaces before and after parens in rgb color definition $value = preg_replace("/rgb\\s*\\(\\s*(.*?)\\s*\\)/", 'rgb(\\1)', $value); $subvalues = explode(" ", $value); $border = CSS::getDefaultValue(CSS_BORDER); foreach ($subvalues as $subvalue) { $subvalue = trim(strtolower($subvalue)); switch (CSSBorder::detect_border_value_type($subvalue)) { case BORDER_VALUE_COLOR: $color_handler = CSS::get_handler(CSS_BORDER_COLOR); $border_color = $color_handler->parse($subvalue); $color_handler->setValue($border, $border_color); break; case BORDER_VALUE_WIDTH: $width_handler = CSS::get_handler(CSS_BORDER_WIDTH); $border_width = $width_handler->parse($subvalue); $width_handler->setValue($border, $border_width); break; case BORDER_VALUE_STYLE: $style_handler = CSS::get_handler(CSS_BORDER_STYLE); $border_style = $style_handler->parse($subvalue); $style_handler->setValue($border, $border_style); break; } } return $border; }
function apply(&$root, &$state, &$pipeline) { $local_css = array(); if (isset($this->tag_filtered[strtolower($root->tagname())])) { $local_css = $this->tag_filtered[strtolower($root->tagname())]; } if (isset($this->tag_filtered['*'])) { $local_css = array_merge($local_css, $this->tag_filtered['*']); } $applicable = array(); foreach ($local_css as $rule) { if ($rule->match($root)) { $applicable[] = $rule; } } usort($applicable, 'cmp_rule_objs'); foreach ($applicable as $rule) { switch ($rule->get_pseudoelement()) { case SELECTOR_PSEUDOELEMENT_BEFORE: $handler =& CSS::get_handler(CSS_HTML2PS_PSEUDOELEMENTS); $handler->replace($handler->get($state->getState()) | CSS_HTML2PS_PSEUDOELEMENTS_BEFORE, $state); break; case SELECTOR_PSEUDOELEMENT_AFTER: $handler =& CSS::get_handler(CSS_HTML2PS_PSEUDOELEMENTS); $handler->replace($handler->get($state->getState()) | CSS_HTML2PS_PSEUDOELEMENTS_AFTER, $state); break; default: $rule->apply($root, $state, $pipeline); break; } } }
function parse($value) { if ($value == 'inherit') { return CSS_PROPERTY_INHERIT; } $width_handler = CSS::get_handler(CSS_BORDER_WIDTH); $width = $width_handler->parse_value($value); return $width; }
/** * Optimization: this function is called very often, * so even a slight overhead for CSS::get_handler call * accumulates in a significiant processing delay. */ function &getCSSProperty($code) { static $cache = array(); if (!isset($cache[$code])) { $cache[$code] =& CSS::get_handler($code); } $value =& $cache[$code]->get($this->_css); return $value; }
function apply(&$state) { $properties = $this->getPropertiesRaw(); foreach ($properties as $property) { $key = $property->getCode(); $value = $property->getValue(); $handler =& CSS::get_handler($key); $handler->replace($value, $state); } }
function &create(&$root, &$pipeline) { $box =& new TableBox(); $box->readCSS($pipeline->getCurrentCSSState()); // This row should not inherit any table specific properties! // 'overflow' for example // $css_state =& $pipeline->getCurrentCSSState(); $css_state->pushDefaultState(); $row =& new TableRowBox($root); $row->readCSS($css_state); $box->add_child($row); $css_state->popState(); // Setup cellspacing / cellpadding values if ($box->getCSSProperty(CSS_BORDER_COLLAPSE) == BORDER_COLLAPSE) { $handler =& CSS::get_handler(CSS_PADDING); $box->setCSSProperty(CSS_PADDING, $handler->default_value()); } // Set text-align to 'left'; all browsers I've ever seen prevent inheriting of // 'text-align' property by the tables. // Say, in the following example the text inside the table cell will be aligned left, // instead of inheriting 'center' value. // // <div style="text-align: center; background-color: green;"> // <table width="100" bgcolor="red"> // <tr><td>TEST // </table> // </div> $handler =& CSS::get_handler(CSS_TEXT_ALIGN); $handler->css('left', $pipeline); // Parse table contents $child = $root->first_child(); $col_index = 0; while ($child) { if ($child->node_type() === XML_ELEMENT_NODE) { if ($child->tagname() === 'colgroup') { // COLGROUP tags do not generate boxes; they contain information on the columns // $col_index = $box->parse_colgroup_tag($child, $col_index); } else { $child_box =& create_pdf_box($child, $pipeline); $box->add_child($child_box); } } $child = $child->next_sibling(); } $box->normalize($pipeline); $box->normalize_cwc(); $box->normalize_rhc(); $box->normalize_parent(); return $box; }
function &create($code, $value, $pipeline) { $handler =& CSS::get_handler($code); if (is_null($handler)) { $null = null; return $null; } $declaration =& new CSSPropertyDeclaration(); $declaration->_code = $code; if (preg_match("/^(.*)!\\s*important\\s*\$/", $value, $matches)) { $value = $matches[1]; $declaration->_important = true; } else { $declaration->_important = false; } $declaration->_value = $handler->parse($value, $pipeline); return $declaration; }
function &create_pdf_pseudoelement($root, $pe_type, &$pipeline) { // Store initial values to CSS stack $css_state =& $pipeline->get_current_css_state(); $css_state->pushDefaultState(); // Initially generated boxes do not require block wrappers // Block wrappers are required in following cases: // - float property is specified for non-block box which cannot be directly converted to block box // (a button, for example) // - display set to block for such box $need_block_wrapper = false; $css =& $pipeline->get_current_css(); $css->apply_pseudoelement($pe_type, $root, $css_state, $pipeline); // Now, if no content found, just return // $content_obj = $css_state->get_property(CSS_CONTENT); if ($content_obj === CSS_PROPERTY_INHERIT) { $content_obj = $css_state->getInheritedProperty(CSS_CONTENT); } $content = $content_obj->render($pipeline->get_counters()); if ($content === '') { $css_state->popState(); $dummy = null; return $dummy; } // CSS 2.1: // 9.7 Relationships between 'display', 'position', and 'float' // The three properties that affect box generation and layout // 'display', 'position', and 'float' interact as follows: // 1. If 'display' has the value 'none', then 'position' and 'float' do not apply. // In this case, the element generates no box. // 2. Otherwise, if 'position' has the value 'absolute' or 'fixed', the box is absolutely positioned, // the computed value of 'float' is 'none', and display is set according to the table below. // The position of the box will be determined by the 'top', 'right', 'bottom' and 'left' properties and // the box's containing block. $position_handler =& CSS::get_handler(CSS_POSITION); $float_handler =& CSS::get_handler(CSS_FLOAT); $position = $position_handler->get($css_state->getState()); if ($position === CSS_PROPERTY_INHERIT) { $position = $css_state->getInheritedProperty(CSS_POSITION); } if ($position === POSITION_ABSOLUTE || $position === POSITION_FIXED) { $float_handler->replace(FLOAT_NONE); $need_block_wrapper |= _fix_display_position_float($css_state); } // 3. Otherwise, if 'float' has a value other than 'none', the box is floated and 'display' is set // according to the table below. $float = $float_handler->get($css_state->getState()); if ($float != FLOAT_NONE) { $need_block_wrapper |= _fix_display_position_float($css_state); } // 4. Otherwise, if the element is the root element, 'display' is set according to the table below. // 5. Otherwise, the remaining 'display' property values apply as specified. (see _fix_display_position_float) // Note that pseudoelements may get only standard display values $display_handler =& CSS::get_handler(CSS_DISPLAY); $display = $display_handler->get($css_state->getState()); switch ($display) { case 'block': $box =& BlockBox::create_from_text($content, $pipeline); break; case 'inline': $ws_handler =& CSS::get_handler(CSS_WHITE_SPACE); $box =& InlineBox::create_from_text($content, $ws_handler->get($css_state->getState()), $pipeline); break; default: die('Unsupported "display" value: ' . $display_handler->get($css_state->getState())); } // Check if this box needs a block wrapper (for example, floating button) // Note that to keep float/position information, we clear the CSS stack only // AFTER the wrapper box have been created; BUT we should clear the following CSS properties // to avoid the fake wrapper box actually affect the layout: // - margin // - border // - padding // - background // if ($need_block_wrapper) { $handler =& CSS::get_handler(CSS_MARGIN); $handler->css("0", $pipeline); pop_border(); push_border(default_border()); pop_padding(); push_padding(default_padding()); $handler =& CSS::get_handler(CSS_BACKGROUND); $handler->css('transparent', $pipeline); // Create "clean" block box $wrapper =& new BlockBox(); $wrapper->readCSS($pipeline->get_current_css_state()); $wrapper->add_child($box); $css_state->popState(); return $wrapper; } else { $css_state->popState(); return $box; } }
function _getCSSDefaults($selector) { $text_align_handler =& CSS::get_handler(CSS_TEXT_ALIGN); $vertical_align_handler =& CSS::get_handler(CSS_VERTICAL_ALIGN); switch ($selector) { case CSS_MARGIN_BOX_SELECTOR_TOP: return 'text-align: left; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_TOP_LEFT_CORNER: return 'text-align: right; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_TOP_LEFT: return 'text-align: left; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_TOP_CENTER: return 'text-align: center; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT: return 'text-align: right; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_TOP_RIGHT_CORNER: return 'text-align: left; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_BOTTOM: return 'text-align: left; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT_CORNER: return 'text-align: right; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_BOTTOM_LEFT: return 'text-align: left; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_BOTTOM_CENTER: return 'text-align: center; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT: return 'text-align: right; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_BOTTOM_RIGHT_CORNER: return 'text-align: left; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_LEFT_TOP: return 'text-align: center; vertical-align: top'; case CSS_MARGIN_BOX_SELECTOR_LEFT_MIDDLE: return 'text-align: center; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_LEFT_BOTTOM: return 'text-align: center; vertical-align: bottom'; case CSS_MARGIN_BOX_SELECTOR_RIGHT_TOP: return 'text-align: center; vertical-align: top'; case CSS_MARGIN_BOX_SELECTOR_RIGHT_MIDDLE: return 'text-align: center; vertical-align: middle'; case CSS_MARGIN_BOX_SELECTOR_RIGHT_BOTTOM: return 'text-align: center; vertical-align: bottom'; } }
function import_declaration($declaration) { $syntax_property = $declaration->get_property(); $code = CSS::name2code($syntax_property->get_name()); if (is_null($code)) { return null; } $handler = CSS::get_handler($code); $property = new CSSPropertyDeclaration(); $property->set_code($code); $expr = $declaration->get_expr(); $property->set_value($handler->parse($expr->to_string(), $this->get_pipeline(), $expr)); $property->set_important($declaration->get_important()); return $property; }
/** * see get_property for optimization description */ function set_property($code, $value) { $this->set_propertyDefault($code, $value); static $cache = array(); if (!isset($cache[$code])) { $cache[$code] =& CSS::get_handler($code); } $cache[$code]->clearDefaultFlags($this); }
function applicable($css_state) { $handler =& CSS::get_handler(CSS_DISPLAY); $display = $handler->get($css_state->getState()); return $display === 'table-cell' || $display === 'table-row' || is_inline_element($display); }
function alpha(&$psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) { // Generate an unique image id $id = $this->generate_id(); // Determine image size $size_x = imagesx($src_img); $size_y = imagesy($src_img); // write stread header to the postscript file $psdata->write("/image-{$id}-init { image-{$id}-data 0 setfileposition } def\n"); $psdata->write("/image-{$id}-data currentfile << /Filter /ASCIIHexDecode >> /ReusableStreamDecode filter\n"); // initialize line length counter $ctr = 0; // Save visible background color $handler =& CSS::get_handler(CSS_BACKGROUND_COLOR); $bg = $handler->get_visible_background_color(); for ($y = 0; $y < $size_y; $y++) { for ($x = 0; $x < $size_x; $x++) { // Check color/alpha of current pixels $colors = imagecolorsforindex($src_img, imagecolorat($src_img, $x, $y)); $a = $colors['alpha']; $r = $colors['red']; $g = $colors['green']; $b = $colors['blue']; // Calculate approximate color $r = (int) ($r + ($bg[0] - $r) * $a / 127); $g = (int) ($g + ($bg[1] - $g) * $a / 127); $b = (int) ($b + ($bg[2] - $b) * $a / 127); // Save image pixel to the stream data $psdata->write(sprintf("%02X%02X%02X", $r, $g, $b)); // Increate the line length counter; check if stream line needs to be terminated $ctr += 6; if ($ctr > MAX_LINE_LENGTH) { $psdata->write("\n"); $ctr = 0; } } } // terminate the stream data $psdata->write(">\ndef\n"); // return image and mask data references $image = "image-{$id}-data"; $mask = ""; return $id; }
function alpha($psdata, $src_img, &$size_x, &$size_y, &$image, &$mask) { $id = $this->generate_id(); $size_x = imagesx($src_img); $size_y = imagesy($src_img); $ps_image_data = ""; $ps_mask_data = 0xff; $ctr = 1; $row = 1; for ($y = 0; $y < $size_y; $y++) { for ($x = 0; $x < $size_x; $x++) { // Mask pixel $colors = imagecolorsforindex($src_img, imagecolorat($src_img, $x, $y)); $a = $colors['alpha']; $r = $colors['red']; $g = $colors['green']; $b = $colors['blue']; $handler =& CSS::get_handler(CSS_BACKGROUND_COLOR); $bg = $handler->get_visible_background_color(); $r = (int) ($r + ($bg[0] - $r) * $a / 127); $g = (int) ($g + ($bg[1] - $g) * $a / 127); $b = (int) ($b + ($bg[2] - $b) * $a / 127); $ps_image_data .= sprintf("\\%03o\\%03o\\%03o", $r, $g, $b); // Write mask and image rows $ctr++; if ($ctr > MAX_IMAGE_ROW_LEN || $x + 1 == $size_x) { $row_next = $size_x - $x - 1 + $size_x * ($size_y - $y - 1) == 0 ? 1 : $row + 1; $psdata->write("/row-{$id}-{$row} { /image-{$id}-data { row-{$id}-{$row_next} } def ({$ps_image_data}) } def\n"); $ps_image_data = ""; $ctr = 1; $row += 1; } } } if ($ps_image_data) { $psdata->write("/row-{$id}-{$row} { /image-{$id}-data { row-{$id}-{$row_next} } def ({$ps_image_data}) } def\n"); } $psdata->write("/image-{$id}-data { row-{$id}-1 } def\n"); $psdata->write("/image-{$id}-init { } def\n"); // return image and mask data references $image = "{image-{$id}-data}"; $mask = ""; return $id; }
function parse($value) { $font = CSS::getDefaultValue(CSS_FONT); if ($value === 'inherit') { $font->style = CSS_PROPERTY_INHERIT; $font->weight = CSS_PROPERTY_INHERIT; $font->size = CSS_PROPERTY_INHERIT; $font->family = CSS_PROPERTY_INHERIT; $font->line_height = CSS_PROPERTY_INHERIT; return $font; } // according to CSS 2.1 standard, // value of 'font' CSS property can be represented as follows: // [ <'font-style'> || <'font-variant'> || <'font-weight'> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'> ] | // caption | icon | menu | message-box | small-caption | status-bar | inherit // Note that font-family value, unlike other values, can contain spaces (in this case it should be quoted) // Breaking value by spaces, we'll break such multi-word families. // Replace all white space sequences with only one space; // Remove spaces after commas; it will allow us // to split value correctly using look-backward expressions $value = preg_replace("/\\s+/", " ", $value); $value = preg_replace("/,\\s+/", ",", $value); $value = preg_replace("#\\s*/\\s*#", "/", $value); // Split value to subvalues by all whitespaces NOT preceeded by comma; // thus, we'll keep all alternative font-families together instead of breaking them. // Still we have a problem with multi-word family names. $subvalues = preg_split("/ /", $value); // Let's scan subvalues we've received and join values containing multiword family names $family_start = 0; $family_running = false; $family_double_quote = false; for ($i = 0, $num_subvalues = count($subvalues); $i < $num_subvalues; $i++) { $current_value = $subvalues[$i]; if ($family_running) { $subvalues[$family_start] .= " " . $subvalues[$i]; // Remove this subvalues from the subvalue list at all array_splice($subvalues, $i, 1); $num_subvalues--; $i--; } // Check if current subvalue contains beginning of multi-word family name // We can detect it by searching for single or double quote without pair if ($family_running && $family_double_quote && !preg_match('/^[^"]*("[^"]*")*[^"]*$/', $current_value)) { $family_running = false; } elseif ($family_running && !$family_double_quote && !preg_match("/^[^']*('[^']*')*[^']*\$/", $current_value)) { $family_running = false; } elseif (!$family_running && !preg_match("/^[^']*('[^']*')*[^']*\$/", $current_value)) { $family_running = true; $family_start = $i; $family_double_quote = false; } elseif (!$family_running && !preg_match('/^[^"]*("[^"]*")*[^"]*$/', $current_value)) { $family_running = true; $family_start = $i; $family_double_quote = true; } } // Now process subvalues one-by-one. foreach ($subvalues as $subvalue) { $subvalue = trim(strtolower($subvalue)); $subvalue_type = detect_font_value_type($subvalue); switch ($subvalue_type) { case FONT_VALUE_STYLE: $font->style = CSSFontStyle::parse($subvalue); break; case FONT_VALUE_WEIGHT: $font->weight = CSSFontWeight::parse($subvalue); break; case FONT_VALUE_SIZE: $size_subvalues = explode('/', $subvalue); $font->size = CSSFontSize::parse($size_subvalues[0]); if (isset($size_subvalues[1])) { $handler =& CSS::get_handler(CSS_LINE_HEIGHT); $font->line_height = $handler->parse($size_subvalues[1]); } break; case FONT_VALUE_FAMILY: $font->family = CSSFontFamily::parse($subvalue); break; } } return $font; }
function &create(&$root, &$pipeline) { $css_state = $pipeline->getCurrentCSSState(); $box =& new TableCellBox(); $box->readCSS($css_state); // Use cellspacing / cellpadding values from the containing table $cellspacing = $box->getCSSProperty(CSS_HTML2PS_CELLSPACING); $cellpadding = $box->getCSSProperty(CSS_HTML2PS_CELLPADDING); // FIXME: I'll need to resolve that issue with COLLAPSING border model. Now borders // are rendered separated // if not border set explicitly, inherit value set via border attribute of TABLE tag $border_handler = CSS::get_handler(CSS_BORDER); if ($border_handler->is_default($box->getCSSProperty(CSS_BORDER))) { $table_border = $box->getCSSProperty(CSS_HTML2PS_TABLE_BORDER); $box->setCSSProperty(CSS_BORDER, $table_border); } $margin =& CSS::get_handler(CSS_MARGIN); $box->setCSSProperty(CSS_MARGIN, $margin->default_value()); $h_padding =& CSS::get_handler(CSS_PADDING); $padding = $box->getCSSProperty(CSS_PADDING); if ($h_padding->is_default($padding)) { $padding->left->_units = $cellpadding; $padding->left->auto = false; $padding->left->percentage = null; $padding->right->_units = $cellpadding; $padding->right->auto = false; $padding->right->percentage = null; $padding->top->_units = $cellpadding; $padding->top->auto = false; $padding->top->percentage = null; $padding->bottom->_units = $cellpadding; $padding->bottom->auto = false; $padding->bottom->percentage = null; /** * Note that cellpadding/cellspacing values never use font-size based units * ('em' and 'ex'), so we may pass 0 as base_font_size parameter - it * will not be used anyway */ $padding->units2pt(0); $box->setCSSProperty(CSS_PADDING, $padding); } if ($box->getCSSProperty(CSS_BORDER_COLLAPSE) != BORDER_COLLAPSE) { $margin_value = $box->getCSSProperty(CSS_MARGIN); if ($margin->is_default($margin_value)) { $length = $cellspacing->copy(); $length->scale(0.5); $margin_value->left->_units = $length; $margin_value->left->auto = false; $margin_value->left->percentage = null; $margin_value->right->_units = $length; $margin_value->right->auto = false; $margin_value->right->percentage = null; $margin_value->top->_units = $length; $margin_value->top->auto = false; $margin_value->top->percentage = null; $margin_value->bottom->_units = $length; $margin_value->bottom->auto = false; $margin_value->bottom->percentage = null; /** * Note that cellpadding/cellspacing values never use font-size based units * ('em' and 'ex'), so we may pass 0 as base_font_size parameter - it * will not be used anyway */ $margin_value->units2pt(0); $box->setCSSProperty(CSS_MARGIN, $margin_value); } } // Save colspan and rowspan information $box->colspan = max(1, (int) $root->get_attribute('colspan')); $box->rowspan = max(1, (int) $root->get_attribute('rowspan')); // Create content // 'vertical-align' CSS value is not inherited from the table cells $css_state->pushState(); $handler =& CSS::get_handler(CSS_VERTICAL_ALIGN); $handler->replace($handler->default_value(), $css_state); $box->create_content($root, $pipeline); global $g_config; if ($g_config['mode'] == "quirks") { // QUIRKS MODE: // H1-H6 and P elements should have their top/bottom margin suppressed if they occur as the first/last table cell child // correspondingly; note that we cannot do it usung CSS rules, as there's no selectors for the last child. // $child = $root->first_child(); if ($child) { while ($child && $child->node_type() != XML_ELEMENT_NODE) { $child = $child->next_sibling(); } if ($child) { if (array_search(strtolower($child->tagname()), array("h1", "h2", "h3", "h4", "h5", "h6", "p"))) { $box->_suppress_first = true; } } } $child = $root->last_child(); if ($child) { while ($child && $child->node_type() != XML_ELEMENT_NODE) { $child = $child->previous_sibling(); } if ($child) { if (array_search(strtolower($child->tagname()), array("h1", "h2", "h3", "h4", "h5", "h6", "p"))) { $box->_suppress_last = true; } } } } // pop the default vertical-align value $css_state->popState(); return $box; }
/** * HR-specific attributes */ function attr_hr_color_before(&$root, &$pipeline) { $handler =& CSS::get_handler(CSS_BORDER_COLOR); $handler->css($root->get_attribute('color'), $pipeline); }