Example #1
0
 protected function _layout_line()
 {
     $frame = $this->_frame;
     $style = $frame->get_style();
     $text = $frame->get_text();
     $size = $style->font_size;
     $font = $style->font_family;
     // Determine the text height
     $style->height = $this->getFontMetrics()->getFontHeight($font, $size);
     $split = false;
     $add_line = false;
     // Handle text transform:
     // http://www.w3.org/TR/CSS21/text.html#propdef-text-transform
     switch (strtolower($style->text_transform)) {
         default:
             break;
         case "capitalize":
             $text = mb_convert_case($text, MB_CASE_TITLE);
             break;
         case "uppercase":
             $text = mb_convert_case($text, MB_CASE_UPPER);
             break;
         case "lowercase":
             $text = mb_convert_case($text, MB_CASE_LOWER);
             break;
     }
     // Handle white-space property:
     // http://www.w3.org/TR/CSS21/text.html#propdef-white-space
     switch ($style->white_space) {
         default:
         case "normal":
             $frame->set_text($text = $this->_collapse_white_space($text));
             if ($text == "") {
                 break;
             }
             $split = $this->_line_break($text);
             break;
         case "pre":
             $split = $this->_newline_break($text);
             $add_line = $split !== false;
             break;
         case "nowrap":
             $frame->set_text($text = $this->_collapse_white_space($text));
             break;
         case "pre-wrap":
             $split = $this->_newline_break($text);
             if (($tmp = $this->_line_break($text)) !== false) {
                 $add_line = $split < $tmp;
                 $split = min($tmp, $split);
             } else {
                 $add_line = true;
             }
             break;
         case "pre-line":
             // Collapse white-space except for \n
             $frame->set_text($text = preg_replace("/[ \t]+/u", " ", $text));
             if ($text == "") {
                 break;
             }
             $split = $this->_newline_break($text);
             if (($tmp = $this->_line_break($text)) !== false) {
                 $add_line = $split < $tmp;
                 $split = min($tmp, $split);
             } else {
                 $add_line = true;
             }
             break;
     }
     // Handle degenerate case
     if ($text === "") {
         return;
     }
     if ($split !== false) {
         // Handle edge cases
         if ($split == 0 && $text === " ") {
             $frame->set_text("");
             return;
         }
         if ($split == 0) {
             // Trim newlines from the beginning of the line
             //$this->_frame->set_text(ltrim($text, "\n\r"));
             $this->_block_parent->add_line();
             $frame->position();
             // Layout the new line
             $this->_layout_line();
         } else {
             if ($split < mb_strlen($frame->get_text())) {
                 // split the line if required
                 $frame->split_text($split);
                 $t = $frame->get_text();
                 // Remove any trailing newlines
                 if ($split > 1 && $t[$split - 1] === "\n" && !$frame->is_pre()) {
                     $frame->set_text(mb_substr($t, 0, -1));
                 }
                 // Do we need to trim spaces on wrapped lines? This might be desired, however, we
                 // can't trim the lines here or the layout will be affected if trimming the line
                 // leaves enough space to fit the next word in the text stream (because pdf layout
                 // is performed elsewhere).
                 /*if (!$this->_frame->get_prev_sibling() && !$this->_frame->get_next_sibling()) {
                     $t = $this->_frame->get_text();
                     $this->_frame->set_text( trim($t) );
                   }*/
             }
         }
         if ($add_line) {
             $this->_block_parent->add_line();
             $frame->position();
         }
     } else {
         // Remove empty space from start and end of line, but only where there isn't an inline sibling
         // and the parent node isn't an inline element with siblings
         // FIXME: Include non-breaking spaces?
         $t = $frame->get_text();
         $parent = $frame->get_parent();
         $is_inline_frame = get_class($parent) === 'Inline_Frame_Decorator';
         if (!$is_inline_frame && !$frame->get_next_sibling()) {
             // fails <b>BOLD <u>UNDERLINED</u></b> becomes <b>BOLD<u>UNDERLINED</u></b>
             $t = rtrim($t);
         }
         if (!$is_inline_frame && !$frame->get_prev_sibling()) {
             //  <span><span>A<span>B</span> C</span></span> fails (the whitespace is removed)
             $t = ltrim($t);
         }
         $frame->set_text($t);
     }
     // Set our new width
     $width = $frame->recalculate_width();
 }
Example #2
0
 /**
  * @param BlockFrameDecorator $block
  */
 function reflow(BlockFrameDecorator $block = null)
 {
     // Check if a page break is forced
     $page = $this->_frame->get_root();
     $page->check_forced_page_break($this->_frame);
     // Bail if the page is full
     if ($page->is_full()) {
         return;
     }
     // Generated content
     $this->_set_content();
     // Collapse margins if required
     $this->_collapse_margins();
     $style = $this->_frame->get_style();
     $cb = $this->_frame->get_containing_block();
     if ($style->position === "fixed") {
         $cb = $this->_frame->get_root()->get_containing_block();
     }
     // Determine the constraints imposed by this frame: calculate the width
     // of the content area:
     list($w, $left_margin, $right_margin, $left, $right) = $this->_calculate_restricted_width();
     // Store the calculated properties
     $style->width = $w;
     $style->margin_left = $left_margin;
     $style->margin_right = $right_margin;
     $style->left = $left;
     $style->right = $right;
     // Update the position
     $this->_frame->position();
     list($x, $y) = $this->_frame->get_position();
     // Adjust the first line based on the text-indent property
     $indent = $style->length_in_pt($style->text_indent, $cb["w"]);
     $this->_frame->increase_line_width($indent);
     // Determine the content edge
     $top = $style->length_in_pt(array($style->margin_top, $style->padding_top, $style->border_top_width), $cb["h"]);
     $bottom = $style->length_in_pt(array($style->border_bottom_width, $style->margin_bottom, $style->padding_bottom), $cb["h"]);
     $cb_x = $x + $left_margin + $style->length_in_pt(array($style->border_left_width, $style->padding_left), $cb["w"]);
     $cb_y = $y + $top;
     $cb_h = $cb["h"] + $cb["y"] - $bottom - $cb_y;
     // Set the y position of the first line in this block
     $line_box = $this->_frame->get_current_line_box();
     $line_box->y = $cb_y;
     $line_box->get_float_offsets();
     // Set the containing blocks and reflow each child
     foreach ($this->_frame->get_children() as $child) {
         // Bail out if the page is full
         if ($page->is_full()) {
             break;
         }
         $child->set_containing_block($cb_x, $cb_y, $w, $cb_h);
         $this->process_clear($child);
         $child->reflow($this->_frame);
         // Don't add the child to the line if a page break has occurred
         if ($page->check_page_break($child)) {
             break;
         }
         $this->process_float($child, $cb_x, $w);
     }
     // Determine our height
     list($height, $margin_top, $margin_bottom, $top, $bottom) = $this->_calculate_restricted_height();
     $style->height = $height;
     $style->margin_top = $margin_top;
     $style->margin_bottom = $margin_bottom;
     $style->top = $top;
     $style->bottom = $bottom;
     $orig_style = $this->_frame->get_original_style();
     $needs_reposition = $style->position === "absolute" && ($style->right !== "auto" || $style->bottom !== "auto");
     // Absolute positioning measurement
     if ($needs_reposition) {
         if ($orig_style->width === "auto" && ($orig_style->left === "auto" || $orig_style->right === "auto")) {
             $width = 0;
             foreach ($this->_frame->get_line_boxes() as $line) {
                 $width = max($line->w, $width);
             }
             $style->width = $width;
         }
         $style->left = $orig_style->left;
         $style->right = $orig_style->right;
     }
     // Calculate inline-block / float auto-widths
     if (($style->display === "inline-block" || $style->float !== 'none') && $orig_style->width === 'auto') {
         $width = 0;
         foreach ($this->_frame->get_line_boxes() as $line) {
             $line->recalculate_width();
             $width = max($line->w, $width);
         }
         if ($width === 0) {
             foreach ($this->_frame->get_children() as $child) {
                 $width += $child->calculate_auto_width();
             }
         }
         $style->width = $width;
     }
     $this->_text_align();
     $this->vertical_align();
     // Absolute positioning
     if ($needs_reposition) {
         list($x, $y) = $this->_frame->get_position();
         $this->_frame->position();
         list($new_x, $new_y) = $this->_frame->get_position();
         $this->_frame->move($new_x - $x, $new_y - $y, true);
     }
     if ($block && $this->_frame->is_in_flow()) {
         $block->add_frame_to_line($this->_frame);
         // May be inline-block
         if ($style->display === "block") {
             $block->add_line();
         }
     }
 }
Example #3
0
 /**
  * @param BlockFrameDecorator $block
  */
 function reflow(BlockFrameDecorator $block = null)
 {
     /** @var TableFrameDecorator */
     $frame = $this->_frame;
     // Check if a page break is forced
     $page = $frame->get_root();
     $page->check_forced_page_break($frame);
     // Bail if the page is full
     if ($page->is_full()) {
         return;
     }
     // Let the page know that we're reflowing a table so that splits
     // are suppressed (simply setting page-break-inside: avoid won't
     // work because we may have an arbitrary number of block elements
     // inside tds.)
     $page->table_reflow_start();
     // Collapse vertical margins, if required
     $this->_collapse_margins();
     $frame->position();
     // Table layout algorithm:
     // http://www.w3.org/TR/CSS21/tables.html#auto-table-layout
     if (is_null($this->_state)) {
         $this->get_min_max_width();
     }
     $cb = $frame->get_containing_block();
     $style = $frame->get_style();
     // This is slightly inexact, but should be okay.  Add half the
     // border-spacing to the table as padding.  The other half is added to
     // the cells themselves.
     if ($style->border_collapse === "separate") {
         list($h, $v) = $style->border_spacing;
         $v = $style->length_in_pt($v) / 2;
         $h = $style->length_in_pt($h) / 2;
         $style->padding_left = $style->length_in_pt($style->padding_left, $cb["w"]) + $h;
         $style->padding_right = $style->length_in_pt($style->padding_right, $cb["w"]) + $h;
         $style->padding_top = $style->length_in_pt($style->padding_top, $cb["h"]) + $v;
         $style->padding_bottom = $style->length_in_pt($style->padding_bottom, $cb["h"]) + $v;
     }
     $this->_assign_widths();
     // Adjust left & right margins, if they are auto
     $width = $style->width;
     $left = $style->margin_left;
     $right = $style->margin_right;
     $diff = $cb["w"] - $width;
     if ($left === "auto" && $right === "auto") {
         if ($diff < 0) {
             $left = 0;
             $right = $diff;
         } else {
             $left = $right = $diff / 2;
         }
         $style->margin_left = "{$left} pt";
         $style->margin_right = "{$right} pt";
     } else {
         if ($left === "auto") {
             $left = $style->length_in_pt($cb["w"] - $right - $width, $cb["w"]);
         }
         if ($right === "auto") {
             $left = $style->length_in_pt($left, $cb["w"]);
         }
     }
     list($x, $y) = $frame->get_position();
     // Determine the content edge
     $content_x = $x + $left + $style->length_in_pt(array($style->padding_left, $style->border_left_width), $cb["w"]);
     $content_y = $y + $style->length_in_pt(array($style->margin_top, $style->border_top_width, $style->padding_top), $cb["h"]);
     if (isset($cb["h"])) {
         $h = $cb["h"];
     } else {
         $h = null;
     }
     $cellmap = $frame->get_cellmap();
     $col =& $cellmap->get_column(0);
     $col["x"] = $content_x;
     $row =& $cellmap->get_row(0);
     $row["y"] = $content_y;
     $cellmap->assign_x_positions();
     // Set the containing block of each child & reflow
     foreach ($frame->get_children() as $child) {
         // Bail if the page is full
         if (!$page->in_nested_table() && $page->is_full()) {
             break;
         }
         $child->set_containing_block($content_x, $content_y, $width, $h);
         $child->reflow();
         if (!$page->in_nested_table()) {
             // Check if a split has occured
             $page->check_page_break($child);
         }
     }
     // Assign heights to our cells:
     $style->height = $this->_calculate_height();
     if ($style->border_collapse === "collapse") {
         // Unset our borders because our cells are now using them
         $style->border_style = "none";
     }
     $page->table_reflow_end();
     // Debugging:
     //echo ($this->_frame->get_cellmap());
     if ($block && $style->float === "none" && $frame->is_in_flow()) {
         $block->add_frame_to_line($frame);
         $block->add_line();
     }
 }