Exemplo n.º 1
0
 function render(Frame $frame)
 {
     $style = $frame->get_style();
     $font_size = $style->get_font_size();
     $line_height = $style->length_in_pt($style->line_height, $frame->get_containing_block("w"));
     $this->_set_opacity($frame->get_opacity($style->opacity));
     $li = $frame->get_parent();
     // Don't render bullets twice if if was split
     if ($li->_splitted) {
         return;
     }
     // Handle list-style-image
     // If list style image is requested but missing, fall back to predefined types
     if ($style->list_style_image !== "none" && !Cache::is_broken($img = $frame->get_image_url())) {
         list($x, $y) = $frame->get_position();
         //For expected size and aspect, instead of box size, use image natural size scaled to DPI.
         // Resample the bullet image to be consistent with 'auto' sized images
         // See also Image::get_min_max_width
         // Tested php ver: value measured in px, suffix "px" not in value: rtrim unnecessary.
         //$w = $frame->get_width();
         //$h = $frame->get_height();
         list($width, $height) = Helpers::dompdf_getimagesize($img, $this->_dompdf->getHttpContext());
         $dpi = $this->_dompdf->getOptions()->getDpi();
         $w = (double) rtrim($width, "px") * 72 / $dpi;
         $h = (double) rtrim($height, "px") * 72 / $dpi;
         $x -= $w;
         $y -= ($line_height - $font_size) / 2;
         //Reverse hinting of list_bullet_positioner
         $this->_canvas->image($img, $x, $y, $w, $h);
     } else {
         $bullet_style = $style->list_style_type;
         $fill = false;
         switch ($bullet_style) {
             default:
             case "disc":
                 $fill = true;
             case "circle":
                 list($x, $y) = $frame->get_position();
                 $r = $font_size * ListBulletFrameDecorator::BULLET_SIZE / 2;
                 $x -= $font_size * (ListBulletFrameDecorator::BULLET_SIZE / 2);
                 $y += $font_size * (1 - ListBulletFrameDecorator::BULLET_DESCENT) / 2;
                 $o = $font_size * ListBulletFrameDecorator::BULLET_THICKNESS;
                 $this->_canvas->circle($x, $y, $r, $style->color, $o, null, $fill);
                 break;
             case "square":
                 list($x, $y) = $frame->get_position();
                 $w = $font_size * ListBulletFrameDecorator::BULLET_SIZE;
                 $x -= $w;
                 $y += $font_size * (1 - ListBulletFrameDecorator::BULLET_DESCENT - ListBulletFrameDecorator::BULLET_SIZE) / 2;
                 $this->_canvas->filled_rectangle($x, $y, $w, $w, $style->color);
                 break;
             case "decimal-leading-zero":
             case "decimal":
             case "lower-alpha":
             case "lower-latin":
             case "lower-roman":
             case "lower-greek":
             case "upper-alpha":
             case "upper-latin":
             case "upper-roman":
             case "1":
                 // HTML 4.0 compatibility
             // HTML 4.0 compatibility
             case "a":
             case "i":
             case "A":
             case "I":
                 $pad = null;
                 if ($bullet_style === "decimal-leading-zero") {
                     $pad = strlen($li->get_parent()->get_node()->getAttribute("dompdf-children-count"));
                 }
                 $node = $frame->get_node();
                 if (!$node->hasAttribute("dompdf-counter")) {
                     return;
                 }
                 $index = $node->getAttribute("dompdf-counter");
                 $text = $this->make_counter($index, $bullet_style, $pad);
                 if (trim($text) == "") {
                     return;
                 }
                 $spacing = 0;
                 $font_family = $style->font_family;
                 $line = $li->get_containing_line();
                 list($x, $y) = array($frame->get_position("x"), $line->y);
                 $x -= $this->_dompdf->getFontMetrics()->getTextWidth($text, $font_family, $font_size, $spacing);
                 // Take line-height into account
                 $line_height = $style->line_height;
                 $y += ($line_height - $font_size) / 4;
                 // FIXME I thought it should be 2, but 4 gives better results
                 $this->_canvas->text($x, $y, $text, $font_family, $font_size, $style->color, $spacing);
             case "none":
                 break;
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Check if $frame will fit on the page.  If the frame does not fit,
  * the frame tree is modified so that a page break occurs in the
  * correct location.
  *
  * @param Frame $frame the frame to check
  *
  * @return Frame the frame following the page break
  */
 function check_page_break(Frame $frame)
 {
     // Do not split if we have already or if the frame was already
     // pushed to the next page (prevents infinite loops)
     if ($this->_page_full || $frame->_already_pushed) {
         return false;
     }
     // If the frame is absolute of fixed it shouldn't break
     $p = $frame;
     do {
         if ($p->is_absolute()) {
             return false;
         }
     } while ($p = $p->get_parent());
     $margin_height = $frame->get_margin_height();
     // FIXME If the row is taller than the page and
     // if it the first of the page, we don't break
     if ($frame->get_style()->display === "table-row" && !$frame->get_prev_sibling() && $margin_height > $this->get_margin_height()) {
         return false;
     }
     // Determine the frame's maximum y value
     $max_y = $frame->get_position("y") + $margin_height;
     // If a split is to occur here, then the bottom margins & paddings of all
     // parents of $frame must fit on the page as well:
     $p = $frame->get_parent();
     while ($p) {
         $max_y += $p->get_style()->computed_bottom_spacing();
         $p = $p->get_parent();
     }
     // Check if $frame flows off the page
     if ($max_y <= $this->_bottom_page_margin) {
         // no: do nothing
         return false;
     }
     Helpers::dompdf_debug("page-break", "check_page_break");
     Helpers::dompdf_debug("page-break", "in_table: " . $this->_in_table);
     // yes: determine page break location
     $iter = $frame;
     $flg = false;
     $in_table = $this->_in_table;
     Helpers::dompdf_debug("page-break", "Starting search");
     while ($iter) {
         // echo "\nbacktrack: " .$iter->get_node()->nodeName ." ".spl_object_hash($iter->get_node()). "";
         if ($iter === $this) {
             Helpers::dompdf_debug("page-break", "reached root.");
             // We've reached the root in our search.  Just split at $frame.
             break;
         }
         if ($this->_page_break_allowed($iter)) {
             Helpers::dompdf_debug("page-break", "break allowed, splitting.");
             $iter->split(null, true);
             $this->_page_full = true;
             $this->_in_table = $in_table;
             $frame->_already_pushed = true;
             return true;
         }
         if (!$flg && ($next = $iter->get_last_child())) {
             Helpers::dompdf_debug("page-break", "following last child.");
             if ($next->is_table()) {
                 $this->_in_table++;
             }
             $iter = $next;
             continue;
         }
         if ($next = $iter->get_prev_sibling()) {
             Helpers::dompdf_debug("page-break", "following prev sibling.");
             if ($next->is_table() && !$iter->is_table()) {
                 $this->_in_table++;
             } else {
                 if (!$next->is_table() && $iter->is_table()) {
                     $this->_in_table--;
                 }
             }
             $iter = $next;
             $flg = false;
             continue;
         }
         if ($next = $iter->get_parent()) {
             Helpers::dompdf_debug("page-break", "following parent.");
             if ($iter->is_table()) {
                 $this->_in_table--;
             }
             $iter = $next;
             $flg = true;
             continue;
         }
         break;
     }
     $this->_in_table = $in_table;
     // No valid page break found.  Just break at $frame.
     Helpers::dompdf_debug("page-break", "no valid break found, just splitting.");
     // If we are in a table, backtrack to the nearest top-level table row
     if ($this->_in_table) {
         $iter = $frame;
         while ($iter && $iter->get_style()->display !== "table-row" && $iter->get_style()->display !== 'table-row-group') {
             $iter = $iter->get_parent();
         }
         $iter->split(null, true);
     } else {
         $frame->split(null, true);
     }
     $this->_page_full = true;
     $frame->_already_pushed = true;
     return true;
 }
 /**
  * @param integer $i
  *
  * @return array|float
  */
 function get_position($i = null)
 {
     return $this->_frame->get_position($i);
 }
Exemplo n.º 4
0
 /**
  * @param \Dompdf\FrameDecorator\Text $frame
  */
 function render(Frame $frame)
 {
     $text = $frame->get_text();
     if (trim($text) === "") {
         return;
     }
     $style = $frame->get_style();
     list($x, $y) = $frame->get_position();
     $cb = $frame->get_containing_block();
     if (($ml = $style->margin_left) === "auto" || $ml === "none") {
         $ml = 0;
     }
     if (($pl = $style->padding_left) === "auto" || $pl === "none") {
         $pl = 0;
     }
     if (($bl = $style->border_left_width) === "auto" || $bl === "none") {
         $bl = 0;
     }
     $x += $style->length_in_pt(array($ml, $pl, $bl), $cb["w"]);
     $font = $style->font_family;
     $size = $frame_font_size = $style->font_size;
     $height = $style->height;
     $word_spacing = $frame->get_text_spacing() + $style->length_in_pt($style->word_spacing);
     $char_spacing = $style->length_in_pt($style->letter_spacing);
     $width = $style->width;
     /*$text = str_replace(
         array("{PAGE_NUM}"),
         array($this->_canvas->get_page_number()),
         $text
       );*/
     $this->_canvas->text($x, $y, $text, $font, $size, $style->color, $word_spacing, $char_spacing);
     $line = $frame->get_containing_line();
     // FIXME Instead of using the tallest frame to position,
     // the decoration, the text should be well placed
     if (false && $line->tallest_frame) {
         $base_frame = $line->tallest_frame;
         $style = $base_frame->get_style();
         $size = $style->font_size;
         $height = $line->h * ($size / $style->line_height);
     }
     $line_thickness = $size * self::DECO_THICKNESS;
     $underline_offset = $size * self::UNDERLINE_OFFSET;
     $overline_offset = $size * self::OVERLINE_OFFSET;
     $linethrough_offset = $size * self::LINETHROUGH_OFFSET;
     $underline_position = -0.08;
     if ($this->_canvas instanceof CPDF) {
         $cpdf_font = $this->_canvas->get_cpdf()->fonts[$style->font_family];
         if (isset($cpdf_font["UnderlinePosition"])) {
             $underline_position = $cpdf_font["UnderlinePosition"] / 1000;
         }
         if (isset($cpdf_font["UnderlineThickness"])) {
             $line_thickness = $size * ($cpdf_font["UnderlineThickness"] / 1000);
         }
     }
     $descent = $size * $underline_position;
     $base = $size;
     // Handle text decoration:
     // http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration
     // Draw all applicable text-decorations.  Start with the root and work our way down.
     $p = $frame;
     $stack = array();
     while ($p = $p->get_parent()) {
         $stack[] = $p;
     }
     while (isset($stack[0])) {
         $f = array_pop($stack);
         if (($text_deco = $f->get_style()->text_decoration) === "none") {
             continue;
         }
         $deco_y = $y;
         //$line->y;
         $color = $f->get_style()->color;
         switch ($text_deco) {
             default:
                 continue;
             case "underline":
                 $deco_y += $base - $descent + $underline_offset + $line_thickness / 2;
                 break;
             case "overline":
                 $deco_y += $overline_offset + $line_thickness / 2;
                 break;
             case "line-through":
                 $deco_y += $base * 0.7 + $linethrough_offset;
                 break;
         }
         $dx = 0;
         $x1 = $x - self::DECO_EXTENSION;
         $x2 = $x + $width + $dx + self::DECO_EXTENSION;
         $this->_canvas->line($x1, $deco_y, $x2, $deco_y, $color, $line_thickness);
     }
     if ($this->_dompdf->getOptions()->getDebugLayout() && $this->_dompdf->getOptions()->getDebugLayoutLines()) {
         $text_width = $this->_dompdf->getFontMetrics()->getTextWidth($text, $font, $frame_font_size);
         $this->_debug_layout(array($x, $y, $text_width + ($line->wc - 1) * $word_spacing, $frame_font_size), "orange", array(0.5, 0.5));
     }
 }