Skip to content

Commit

Permalink
little bit more optimizations and bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafał Pośpiech committed Mar 7, 2019
1 parent 803670b commit a9568a3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 19 deletions.
32 changes: 24 additions & 8 deletions lib/Layout/BlockBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class BlockBox extends ElementBox implements BoxInterface, AppendChildInterface,
* @var \YetiForcePDF\Layout\LineBox
*/
protected $currentLineBox;
/**
* Parent width cache.
*
* @var string
*/
protected $parentWidth = '0';

/**
* @var LineBox[]
Expand Down Expand Up @@ -364,13 +370,20 @@ public function measureWidth()
$dimensions = $this->getDimensions();
$parent = $this->getParent();
if ($parent) {
if ($this->parentWidth === $parent->getDimensions()->getWidth()) {
return $this;
}
$this->parentWidth = $parent->getDimensions()->getWidth();
$oldWidth = $this->getDimensions()->getWidth();
if ($parent->getDimensions()->getWidth() !== null) {
$dimensions->setWidth(Math::sub($parent->getDimensions()->getInnerWidth(), $this->getStyle()->getHorizontalMarginsWidth()));
$this->applyStyleWidth();
foreach ($this->getChildren() as $child) {
$child->measureWidth();
if ($oldWidth !== $this->getDimensions()->getWidth()) {
foreach ($this->getChildren() as $child) {
$child->measureWidth();
}
$this->divideLines();
}
$this->divideLines();
return $this;
}
// if parent doesn't have a width specified
Expand All @@ -389,12 +402,15 @@ public function measureWidth()
$this->applyStyleWidth();
return $this;
}
$dimensions->setWidth($this->document->getCurrentPage()->getDimensions()->getWidth());
$this->applyStyleWidth();
foreach ($this->getChildren() as $child) {
$child->measureWidth();
$width = $this->document->getCurrentPage()->getDimensions()->getWidth();
if ($this->getDimensions()->getWidth()!==$width) {
$dimensions->setWidth($width);
$this->applyStyleWidth();
foreach ($this->getChildren() as $child) {
$child->measureWidth();
}
$this->divideLines();
}
$this->divideLines();
return $this;
}

Expand Down
19 changes: 9 additions & 10 deletions lib/Layout/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function setDisplayable(bool $displayable)
* Set style.
*
* @param \YetiForcePDF\Style\Style $style
* @param bool $init
* @param bool $init
*
* @return $this
*/
Expand Down Expand Up @@ -523,9 +523,6 @@ public function isRenderable()
*/
public function appendChild(self $box)
{
if ($this instanceof LineBox && $box instanceof LineBox) {
throw new \InvalidArgumentException('LineBox cannot append another LineBox as child.');
}
$box->setParent($this);
$childrenCount = count($this->children);
if ($childrenCount > 0) {
Expand Down Expand Up @@ -623,16 +620,18 @@ public function insertBefore(self $child, self $before)

/**
* Get children.
*
* @param bool $onlyRenderable
* @param bool $onlyForMeasurment
*
* @return Box[]
*/
public function getChildren(bool $onlyRenderable = false, bool $onlyForMeasurment = false): array
{
if (!$onlyRenderable && !$onlyForMeasurment) {
return $this->children;
}
return array_filter($this->children, function (Box $box) use ($onlyRenderable, $onlyForMeasurment) {
return array_filter($this->children, function (self $box) use ($onlyRenderable, $onlyForMeasurment) {
if ($onlyRenderable && $onlyForMeasurment) {
return $box->isRenderable() && $box->isForMeasurement();
}
Expand All @@ -647,7 +646,7 @@ public function getChildren(bool $onlyRenderable = false, bool $onlyForMeasurmen
* Get all children.
*
* @param Box[] $allChildren
* @param bool $withCurrent
* @param bool $withCurrent
*
* @return Box[]
*/
Expand All @@ -666,8 +665,8 @@ public function getAllChildren(&$allChildren = [], bool $withCurrent = true)
* Iterate all children.
*
* @param callable $fn
* @param bool $reverse
* @param bool $deep
* @param bool $reverse
* @param bool $deep
*
* @return $this
*/
Expand All @@ -693,7 +692,7 @@ public function iterateChildren(callable $fn, bool $reverse = false, bool $deep
/**
* Get boxes by type.
*
* @param string $shortClassName
* @param string $shortClassName
* @param string|null $until
*
* @return array
Expand Down Expand Up @@ -1027,7 +1026,7 @@ public function alignText()
/**
* Add border instructions.
*
* @param array $element
* @param array $element
* @param string $pdfX
* @param string $pdfY
* @param string $width
Expand Down
24 changes: 23 additions & 1 deletion lib/Layout/InlineBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ class InlineBox extends ElementBox implements BoxInterface, BuildTreeInterface,
* @var \YetiForcePDF\Layout\TextBox
*/
protected $previousTextBox;
/**
* Parent width cache.
*
* @var string
*/
protected $parentWidth ='0';
/**
* Parent height cache.
*
* @var string
*/
protected $parentHeight ='0';

/**
* Go up to Line box and clone and wrap element.
Expand Down Expand Up @@ -242,14 +254,21 @@ public function appendText($childDomElement, $element = null, $style = null, $pa
*/
public function measureWidth()
{
$style = $this->getStyle();
if ($this->parentWidth === $this->getParent()->getDimensions()->getWidth()) {
if (!$this->isForMeasurement()) {
$this->getDimensions()->setWidth(Math::add($style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth()));
}
return $this;
}
$this->parentWidth = $this->getParent()->getDimensions()->getWidth();
$width = '0';
if ($this->isForMeasurement()) {
foreach ($this->getChildren() as $child) {
$child->measureWidth();
$width = Math::add($width, $child->getDimensions()->getOuterWidth());
}
}
$style = $this->getStyle();
$width = Math::add($width, $style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth());
$this->getDimensions()->setWidth($width);
$this->applyStyleWidth();
Expand All @@ -263,6 +282,9 @@ public function measureWidth()
*/
public function measureHeight()
{
if ($this->getDimensions()->getHeight()!==null) {
return $this;
}
foreach ($this->getChildren() as $child) {
$child->measureHeight();
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Layout/LineBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public function willFit(Box $box)
$childrenWidth = $this->getChildrenWidth();
$availableSpace = $this->getDimensions()->computeAvailableSpace();
$boxWidth = $box->getDimensions()->getWidth();
if (!$boxWidth) {
$boxWidth = $box->getDimensions()->getOuterWidth();
}
return Math::comp(Math::sub($availableSpace, $childrenWidth), $boxWidth) >= 0;
}

Expand Down
10 changes: 10 additions & 0 deletions lib/Layout/TableBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class TableBox extends BlockBox
* @var TableRowGroupBox|null
*/
protected $anonymousRowGroup;
/**
* Parent width cache.
*
* @var string
*/
protected $parentWidth = '0';

/**
* We shouldn't append block box here.
Expand Down Expand Up @@ -1116,6 +1122,10 @@ public function getMinWidth()
*/
public function measureWidth()
{
if ($this->parentWidth === $this->getParent()->getParent()->getDimensions()->getWidth()) {
return $this;
}
$this->parentWidth = $this->getParent()->getParent()->getDimensions()->getWidth();
foreach ($this->getCells() as $cell) {
$cell->measureWidth();
}
Expand Down
11 changes: 11 additions & 0 deletions lib/Layout/TableCellBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class TableCellBox extends BlockBox
*/
protected $spanned = false;

/**
* Parent width cache.
*
* @var string
*/
protected $parentWidth = '0';

/**
* Set column spanned.
*
Expand Down Expand Up @@ -55,6 +62,10 @@ public function isSpanned()
*/
public function measureWidth()
{
if ($this->parentWidth === $this->getClosestByType('TableBox')->getDimensions()->getWidth()) {
return $this;
}
$this->parentWidth = $this->getClosestByType('TableBox')->getDimensions()->getWidth();
foreach ($this->getChildren() as $child) {
$child->measureWidth();
}
Expand Down

0 comments on commit a9568a3

Please sign in to comment.