Skip to content

Commit

Permalink
performance optimization, undefined variable break bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalpospiech committed Mar 4, 2019
1 parent d43595e commit 5c42b59
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
.vscode/
vendor/
26 changes: 26 additions & 0 deletions examples/LoremIpsum.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div style="font-family:'DejaVu Sans';font-size:12px;">
Lorem ipsum dolor sit amet ante. Quisque euismod convallis non, porttitor lacus pretium eget, orci. Vestibulum nibh.
Fusce a libero ornare vel, consequat eu, elit. Nullam consequat faucibus, tortor orci luctus nulla orci massa,
feugiat
quam interdum viverra. Mauris tempor diam. Sed tristique pede. Fusce vitae dui tincidunt lorem. Praesent dolor.
Vestibulum ante ipsum sit amet tellus. Donec eros. Sed sed nulla. Sed dignissim porttitor, lacus nulla nisl eros,
sit
amet tempus facilisis, ante ullamcorper eleifend tincidunt, risus velit, rhoncus aliquam tortor. Nunc eleifend
viverra.
Proin cursus et, scelerisque ligula, et libero. Donec mauris sed augue euismod nec, accumsan eget, ante. Proin
ornare
vitae, dictum a, urna. Aenean scelerisque fermentum malesuada. Donec vitae odio fermentum in, vulputate quam. Fusce
fringilla mollis. Etiam dapibus aliquam purus. Phasellus quis arcu. In metus. Maecenas at sagittis vel, consectetuer
adipiscing sollicitudin. Cras id sapien et est congue at, congue eu, odio. Nam ultrices. Nunc accumsan vitae,
tortor.
Class aptent taciti sociosqu ad litora torquent per inceptos hymenaeos. Aenean gravida gravida elit. Nam ut turpis.
Nam
scelerisque mauris pulvinar ligula, semper aliquam ac, augue. Fusce gravida, quam interdum consectetuer dignissim,
sapien leo mollis non, rhoncus eget, aliquam at, tellus. Quisque nec tincidunt vel, varius laoreet. Aenean feugiat
pulvinar. Nulla semper. Morbi molestie, neque in quam nulla, egestas sodales, velit ac quam fermentum sapien sed
lorem
dapibus risus ut orci luctus et gravida mattis, magna vel lorem eget urna. Nullam id odio et libero. Vivamus
imperdiet
convallis. Donec a metus. Curabitur nec tellus. Integer metus imperdiet orci ultricies feugiat, urna ut urna. Nam
lectus
</div>
Binary file added examples/LoremIpsum.pdf
Binary file not shown.
10 changes: 10 additions & 0 deletions examples/LoremIpsum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$loader = require '../vendor/autoload.php';
$files = ['LoremIpsum'];
foreach ($files as $file) {
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml(file_get_contents($file . '.html'));
$pdfFile = $document->render();
file_put_contents($file . '.pdf', $pdfFile);
}
Binary file modified examples/PagesTables.pdf
Binary file not shown.
Binary file added examples/PagesTables2.pdf
Binary file not shown.
Binary file modified examples/RowSpan.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions examples/SimpleColor.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
<div style="display:inline-block;color:#00FF00;">green</div>
<div style="display:inline;color:#0000FF;">blue</div>
</div>
<div style="width:100px;height:100px;background:yellowgreen;color:black;">YellowGreen</div>
Binary file modified examples/SimpleColor.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion examples/SimpleColor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

echo "test";
$loader = require '../vendor/autoload.php';
echo 'before';
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml(file_get_contents('SimpleColor.html'));
$pdfFile = $document->render();
echo 'SimpleColor';
file_put_contents('SimpleColor.pdf', $pdfFile);
Binary file modified examples/helloWorld.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/tempCodeRunnerFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$loader = require '../vendor/autoload.php';
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml(file_get_contents('SimpleColor.html'));
$pdfFile = $document->render();
file_put_contents('SimpleColor.pdf', $pdfFile);
42 changes: 32 additions & 10 deletions lib/Layout/BlockBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ class BlockBox extends ElementBox implements BoxInterface, AppendChildInterface,
*/
protected $sourceLines = [];

/**
* Merged lines.
*
* @var LineBox[]
*/
public $mergedLines = [];

/**
* Line groups.
*
* @var LineBox[]
*/
public $lineGroups=[];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -120,7 +134,7 @@ public function getNewLineBox($before = null)
* Close line box.
*
* @param \YetiForcePDF\Layout\LineBox|null $lineBox
* @param bool $createNew
* @param bool $createNew
*
* @return \YetiForcePDF\Layout\LineBox
*/
Expand Down Expand Up @@ -405,18 +419,21 @@ public function measureWidth()
*/
public function groupLines()
{
$lineGroups = [];
if (!empty($this->lineGroups)) {
return $this->lineGroups;
}
$this->lineGroups = [];
$currentGroup = 0;
foreach ($this->getChildren() as $child) {
if ($child instanceof LineBox) {
$lineGroups[$currentGroup][] = $child;
$this->lineGroups[$currentGroup][] = $child;
} else {
if (isset($lineGroups[$currentGroup])) {
if (isset($this->lineGroups[$currentGroup])) {
$currentGroup++;
}
}
}
return $lineGroups;
return $this->lineGroups;
}

/**
Expand All @@ -426,7 +443,9 @@ public function groupLines()
*/
public function mergeLineGroups(array $lineGroups)
{
$lines = [];
if (!empty($this->mergedLines)) {
return $this->mergedLines;
}
foreach ($lineGroups as $lines) {
if (!empty($lines)) {
$currentLine = $this->getNewLineBox($lines[0]);
Expand All @@ -437,10 +456,10 @@ public function mergeLineGroups(array $lineGroups)
}
$this->removeChild($line);
}
$lines[] = $currentLine;
$this->mergedLines[] = $currentLine;
}
}
return $lines;
return $this->mergedLines;
}

/**
Expand Down Expand Up @@ -496,6 +515,9 @@ public function divideLines()
foreach ($this->getChildren() as $child) {
if ($child instanceof LineBox) {
$lines = $child->divide();
if (count($lines)===1 && $child===$lines[0]) {
continue;
}
foreach ($lines as $line) {
$this->insertBefore($line, $child);
$line->getStyle()->init();
Expand Down Expand Up @@ -632,11 +654,11 @@ public function replacePageNumbers()
if ($child instanceof TextBox) {
if (mb_stripos($child->getTextContent(), '{p}') !== false) {
$pageNumber = $this->document->getCurrentPage()->getPageNumber();
$child->setText(preg_replace('/{p}/ui', (string)$pageNumber, $child->getTextContent()));
$child->setText(preg_replace('/{p}/ui', (string) $pageNumber, $child->getTextContent()));
$child->getClosestByType('BlockBox')->layout();
}
if (mb_stripos($child->getTextContent(), '{a}') !== false) {
$pages = (string)$this->document->getCurrentPage()->getPageCount();
$pages = (string) $this->document->getCurrentPage()->getPageCount();
$child->setText(preg_replace('/{a}/ui', $pages, $child->getTextContent()));
$child->getClosestByType('BlockBox')->layout();
}
Expand Down
19 changes: 9 additions & 10 deletions lib/Layout/Dimensions/BoxDimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,20 @@ public function getOuterWidth()
if (!$box instanceof LineBox) {
$style = $this->getBox()->getStyle();
$childrenWidth = '0';
// if some of the children overflows
if ($box->getStyle()->getRules('display') === 'inline') {
foreach ($box->getChildren() as $child) {
$childrenWidth = Math::add($childrenWidth, $child->getDimensions()->getOuterWidth());
}
} else {
foreach ($box->getChildren() as $child) {
$childrenWidth = Math::max($childrenWidth, $child->getDimensions()->getOuterWidth());
}
}
if ($this->getWidth() !== null) {
$childrenWidth = Math::add($childrenWidth, $style->getHorizontalMarginsWidth(), $style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth());
$width = Math::add($this->getWidth(), $style->getHorizontalMarginsWidth());
return Math::max($width, $childrenWidth);
} else {
if ($box->getStyle()->getRules('display') === 'inline') {
foreach ($box->getChildren() as $child) {
$childrenWidth = Math::add($childrenWidth, $child->getDimensions()->getOuterWidth());
}
} else {
foreach ($box->getChildren() as $child) {
$childrenWidth = Math::max($childrenWidth, $child->getDimensions()->getOuterWidth());
}
}
return Math::add($childrenWidth, $style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth());
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions lib/Layout/LineBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ public function removeWhiteSpaces()
*/
public function divide()
{
$width = $this->getDimensions()->getWidth();
$availableSpace = $this->getParent()->getDimensions()->computeAvailableSpace();
if (!empty($width) && !empty($availableSpace) && Math::comp($width, $availableSpace) <= 0) {
return [$this];
}
$lines = [];
$line = (new self())
->setDocument($this->document)
Expand Down
2 changes: 1 addition & 1 deletion lib/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class Math
{
public static $scale = 8;
public static $scale = 2;

/**
* Add two numbers.
Expand Down
31 changes: 16 additions & 15 deletions lib/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class Page extends \YetiForcePDF\Objects\Basic\DictionaryObject
*/
const ORIENTATION_LANDSCAPE = 'L';
/**
* After page breaking box was cut below
* After page breaking box was cut below.
*/
const CUT_BELOW = 1;
/**
* After page breaking box was cut above
* After page breaking box was cut above.
*/
const CUT_ABOVE = 2;
/**
Expand Down Expand Up @@ -550,16 +550,16 @@ public function setFormat(string $format)
->setDocument($this->document)
->init();
$this->dimensions
->setWidth(Math::sub((string)$dimensions[0], Math::add((string)$this->margins['left'], (string)$this->margins['right'])))
->setHeight(Math::sub((string)$dimensions[1], Math::add((string)$this->margins['top'], (string)$this->margins['bottom'])));
->setWidth(Math::sub((string) $dimensions[0], Math::add((string) $this->margins['left'], (string) $this->margins['right'])))
->setHeight(Math::sub((string) $dimensions[1], Math::add((string) $this->margins['top'], (string) $this->margins['bottom'])));
$this->outerDimensions = (new Dimensions())
->setDocument($this->document)
->init();
$this->outerDimensions->setWidth((string)$dimensions[0])->setHeight((string)$dimensions[1]);
$this->outerDimensions->setWidth((string) $dimensions[0])->setHeight((string) $dimensions[1]);
$this->coordinates = (new Coordinates())
->setDocument($this->document)
->init();
$this->coordinates->setX((string)$this->margins['left'])->setY((string)$this->margins['top'])->init();
$this->coordinates->setX((string) $this->margins['left'])->setY((string) $this->margins['top'])->init();
return $this;
}

Expand All @@ -580,7 +580,7 @@ public function getFormat()
*
* @return \YetiForcePDF\Page
*/
public function setOrientation(string $orientation): \YetiForcePDF\Page
public function setOrientation(string $orientation): self
{
$this->orientation = $orientation;
return $this;
Expand Down Expand Up @@ -716,7 +716,7 @@ public function getBox()
*
* @return \YetiForcePDF\Page
*/
public function setUserUnit(float $userUnit): \YetiForcePDF\Page
public function setUserUnit(float $userUnit): self
{
$this->userUnit = $userUnit;
return $this;
Expand All @@ -725,13 +725,13 @@ public function setUserUnit(float $userUnit): \YetiForcePDF\Page
/**
* Add page resource.
*
* @param string $groupName
* @param string $resourceName
* @param string $groupName
* @param string $resourceName
* @param \YetiForcePDF\Objects\PdfObject $resource
*
* @return \YetiForcePDF\Page
*/
public function addResource(string $groupName, string $resourceName, \YetiForcePDF\Objects\PdfObject $resource): \YetiForcePDF\Page
public function addResource(string $groupName, string $resourceName, \YetiForcePDF\Objects\PdfObject $resource): self
{
if (!isset($this->resources[$groupName])) {
$this->resources[$groupName] = [];
Expand Down Expand Up @@ -960,7 +960,7 @@ public function getRootChildsAfterY(string $yPos)
/**
* Cut box above specified position.
*
* @param Box $child
* @param Box $child
* @param string $yPos
*
* @return $this
Expand All @@ -980,7 +980,7 @@ public function cutAbove(Box $child, string $yPos)
/**
* Cut box below specified position.
*
* @param Box $child
* @param Box $child
* @param string $yPos
*
* @return $this
Expand Down Expand Up @@ -1066,7 +1066,7 @@ public function cloneAndDivideChildrenAfterY(string $yPos, array $boxes = null)
protected function divideTable(Box $tableChild)
{
$tableWrapperBox = $tableChild->getClosestByType('TableWrapperBox');
$pageEnd = Math::add($this->getDimensions()->getHeight(), (string)$this->margins['top']);
$pageEnd = Math::add($this->getDimensions()->getHeight(), (string) $this->margins['top']);
if (Math::comp($tableWrapperBox->getCoordinates()->getY(), $pageEnd) >= 0) {
// if table is below page do nothing - it will be moved to the next page and then again checked
return $tableWrapperBox;
Expand Down Expand Up @@ -1178,6 +1178,7 @@ public function breakAfter(Box $box)
return $this;
}
$contentBoxes = [];
$break = false;
foreach ($box->getParent()->getChildren() as $child) {
if ($child === $box) {
$break = true;
Expand Down Expand Up @@ -1221,7 +1222,7 @@ public function breakAfter(Box $box)
*/
public function breakOverflow()
{
$atYPos = Math::add($this->getDimensions()->getHeight(), (string)$this->margins['top']);
$atYPos = Math::add($this->getDimensions()->getHeight(), (string) $this->margins['top']);
$clonedBoxes = $this->cloneAndDivideChildrenAfterY($atYPos);
if (empty($clonedBoxes)) {
return $this;
Expand Down

0 comments on commit 5c42b59

Please sign in to comment.