Skip to content

Commit

Permalink
Improve type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
bsweeney committed Jul 11, 2022
1 parent ab50082 commit e82c746
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 23 deletions.
9 changes: 3 additions & 6 deletions src/Dompdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,13 @@ public function render()
}

$canvas = $this->canvas;
$root = null;

$root_frame = $this->tree->get_root();
$root = Factory::decorate_root($root_frame, $this);
foreach ($this->tree as $frame) {
// Set up the root frame
if (is_null($root)) {
$root = Factory::decorate_root($this->tree->get_root(), $this);
if ($frame === $root_frame) {
continue;
}

// Create the appropriate decorators, reflowers & positioners.
Factory::decorate_frame($frame, $this, $root);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Frame/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ static function decorate_frame(Frame $frame, Dompdf $dompdf, Frame $root = null)

$node = $frame->get_node();
$parent_node = $node->parentNode;

if ($parent_node) {
if ($parent_node && $parent_node instanceof \DOMElement) {
if (!$parent_node->hasAttribute("dompdf-children-count")) {
$xpath = new DOMXPath($xml);
$count = $xpath->query("li", $parent_node)->length;
Expand Down
3 changes: 2 additions & 1 deletion src/FrameDecorator/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ private function isEmptyTextNode(AbstractFrameDecorator $frame): bool
|| in_array($frame->get_style()->display, self::VALID_CHILDREN, true);
};

return $frame->is_text_node() && !$frame->is_pre()
return $frame instanceof Text
&& !$frame->is_pre()
&& preg_match($wsPattern, $frame->get_text())
&& $validChildOrNull($frame->get_prev_sibling())
&& $validChildOrNull($frame->get_next_sibling());
Expand Down
4 changes: 3 additions & 1 deletion src/FrameDecorator/TableRowGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function split(?Frame $child = null, bool $page_break = false, bool $forc
}

// Remove child & all subsequent rows from the cellmap
$cellmap = $this->get_parent()->get_cellmap();
/** @var Table $parent */
$parent = $this->get_parent();
$cellmap = $parent->get_cellmap();
$iter = $child;

while ($iter) {
Expand Down
11 changes: 7 additions & 4 deletions src/FrameReflower/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Dompdf\FrameReflower;

use Dompdf\Frame;
use Dompdf\FrameDecorator\AbstractFrameDecorator;
use Dompdf\FrameDecorator\Block as BlockFrameDecorator;
use Dompdf\FrameDecorator\TableCell as TableCellFrameDecorator;
use Dompdf\FrameDecorator\Text as TextFrameDecorator;
Expand All @@ -25,6 +26,8 @@ class Block extends AbstractFrameReflower
const MIN_JUSTIFY_WIDTH = 0.80;

/**
* Frame for this reflower
*
* @var BlockFrameDecorator
*/
protected $_frame;
Expand Down Expand Up @@ -683,9 +686,9 @@ function vertical_align()
}

/**
* @param Frame $child
* @param AbstractFrameDecorator $child
*/
function process_clear(Frame $child)
function process_clear(AbstractFrameDecorator $child)
{
$child_style = $child->get_style();
$root = $this->_frame->get_root();
Expand Down Expand Up @@ -717,11 +720,11 @@ function process_clear(Frame $child)
}

/**
* @param Frame $child
* @param AbstractFrameDecorator $child
* @param float $cb_x
* @param float $cb_w
*/
function process_float(Frame $child, $cb_x, $cb_w)
function process_float(AbstractFrameDecorator $child, $cb_x, $cb_w)
{
$child_style = $child->get_style();
$root = $this->_frame->get_root();
Expand Down
14 changes: 8 additions & 6 deletions src/FrameReflower/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,20 @@ function apply_page_style(Frame $frame, $page_number)
*/
function reflow(BlockFrameDecorator $block = null)
{
/** @var PageFrameDecorator $frame */
$frame = $this->_frame;
$child = $frame->get_first_child();
$fixed_children = [];
$prev_child = null;
$child = $this->_frame->get_first_child();
$current_page = 0;

while ($child) {
$this->apply_page_style($this->_frame, $current_page + 1);
$this->apply_page_style($frame, $current_page + 1);

$style = $this->_frame->get_style();
$style = $frame->get_style();

// Pages are only concerned with margins
$cb = $this->_frame->get_containing_block();
$cb = $frame->get_containing_block();
$left = (float)$style->length_in_pt($style->margin_left, $cb["w"]);
$right = (float)$style->length_in_pt($style->margin_right, $cb["w"]);
$top = (float)$style->length_in_pt($style->margin_top, $cb["h"]);
Expand Down Expand Up @@ -144,13 +146,13 @@ function reflow(BlockFrameDecorator $block = null)
$this->_check_callbacks("begin_page_render", $child);

// Render the page
$this->_frame->get_renderer()->render($child);
$frame->get_renderer()->render($child);

// Check for end render callback
$this->_check_callbacks("end_page_render", $child);

if ($next_child) {
$this->_frame->next_page();
$frame->next_page();
}

// Wait to dispose of all frames on the previous page
Expand Down
7 changes: 5 additions & 2 deletions src/FrameReflower/TableCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ function reflow(BlockFrameDecorator $block = null)
// Determine our height
$style_height = (float)$style->length_in_pt($style->height, $h);

$this->_frame->set_content_height($this->_calculate_content_height());
/** @var FrameDecorator\TableCell */
$frame = $this->_frame;

$height = max($style_height, (float)$this->_frame->get_content_height());
$frame->set_content_height($this->_calculate_content_height());

$height = max($style_height, (float)$frame->get_content_height());

// Let the cellmap know our height
$cell_height = $height / count($cells["rows"]);
Expand Down
2 changes: 2 additions & 0 deletions src/FrameReflower/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Text extends AbstractFrameReflower
public static $_wordbreak_pattern = '/([^\S\xA0\x{202F}\x{2007}\n]+|\R|\-+|\xAD+)/u';

/**
* Frame for this reflower
*
* @var TextFrameDecorator
*/
protected $_frame;
Expand Down
3 changes: 2 additions & 1 deletion src/Renderer/ListBullet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Dompdf\Helpers;
use Dompdf\Frame;
use Dompdf\FrameDecorator\ListBullet as ListBulletFrameDecorator;
use Dompdf\FrameDecorator\ListBulletImage;
use Dompdf\Image\Cache;

/**
Expand Down Expand Up @@ -146,7 +147,7 @@ function render(Frame $frame)

// 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())) {
if ($frame instanceof ListBulletImage && !Cache::is_broken($img = $frame->get_image_url())) {
[$x, $y] = $frame->get_position();
$w = $frame->get_width();
$h = $frame->get_height();
Expand Down

0 comments on commit e82c746

Please sign in to comment.