Skip to content

Commit

Permalink
performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafał Pośpiech committed Mar 6, 2019
1 parent d43595e commit f415381
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
vendor/
.vscode/
.sonarlint/
14 changes: 14 additions & 0 deletions examples/LoremIpsum.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<table>
<tr>
<td style="width:50%">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean malesuada odio elit, nec vestibulum nunc finibus
et. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse rutrum,
risus id auctor posuere, odio metus condimentum lectus, eu hendrerit nulla ante viverra velit.
</td>
<td>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean malesuada odio elit, nec vestibulum nunc finibus
et. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse rutrum,
risus id auctor posuere, odio metus condimentum lectus, eu hendrerit nulla ante viverra velit.
</td>
</tr>
</table>
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/Pages.pdf
Binary file not shown.
Binary file modified examples/PagesTables.pdf
Binary file not shown.
Binary file added examples/PagesTables2.pdf
Binary file not shown.
Binary file added examples/SpanTable.pdf
Binary file not shown.
Binary file modified examples/WidthsSpecifiedTable.pdf
Binary file not shown.
Binary file modified examples/helloWorld.pdf
Binary file not shown.
Binary file modified examples/percentWidth.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/percentWidth.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$loader = require '../vendor/autoload.php';
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml(file_get_contents('percentWidth.html'));
$pdfFile = $document->render();
echo $pdfFile;
file_put_contents('percentWidth.pdf', $pdfFile);
Binary file modified examples/simpleInline.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/simpleInline.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$loader = require '../vendor/autoload.php';
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml(file_get_contents('simpleInline.html'));
$pdfFile = $document->render();
//echo $pdfFile;
file_put_contents('simpleInline.pdf', $pdfFile);
Binary file modified examples/wordWrap.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/wordWrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$loader = require '../vendor/autoload.php';
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml(file_get_contents('wordWrap.html'));
$pdfFile = $document->render();
echo $pdfFile;
file_put_contents('wordWrap.pdf', $pdfFile);
3 changes: 1 addition & 2 deletions lib/Html/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function cleanUpHtml(string $html)
*
* @return \YetiForcePDF\Html\Parser
*/
public function loadHtml(string $html, string $fromEncoding = ''): \YetiForcePDF\Html\Parser
public function loadHtml(string $html, string $fromEncoding = ''): self
{
$html = htmlspecialchars_decode($html, ENT_HTML5);
$this->html = $this->cleanUpHtml($html);
Expand Down Expand Up @@ -240,7 +240,6 @@ public function parse()
foreach ($this->document->getPages($groupIndex) as $page) {
$page->getBox()->spanAllRows();
}
$pageGroup->getStyle()->fixDomTree();
$this->document->fixPageNumbers();
foreach ($this->document->getPages($groupIndex) as $page) {
$this->document->setCurrentPage($page);
Expand Down
8 changes: 5 additions & 3 deletions lib/Layout/BlockBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,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 @@ -508,6 +508,7 @@ public function divideLines()
}
}
$this->hideEmptyLines();
unset($lines);
return $this;
}

Expand Down Expand Up @@ -632,16 +633,17 @@ 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();
}
}
}
unset($allChildren);
return $this;
}

Expand Down
12 changes: 10 additions & 2 deletions lib/Layout/Dimensions/BoxDimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,19 @@ public function getOuterWidth()
// if some of the children overflows
if ($box->getStyle()->getRules('display') === 'inline') {
foreach ($box->getChildren() as $child) {
$childrenWidth = Math::add($childrenWidth, $child->getDimensions()->getOuterWidth());
if ($childWidth = $child->getDimensions()->getWidth()) {
$childrenWidth = Math::add($childrenWidth, $childWidth);
} else {
$childrenWidth = Math::add($childrenWidth, $child->getDimensions()->getOuterWidth());
}
}
} else {
foreach ($box->getChildren() as $child) {
$childrenWidth = Math::max($childrenWidth, $child->getDimensions()->getOuterWidth());
if ($childWidth = $child->getDimensions()->getWidth()) {
$childrenWidth = Math::max($childrenWidth, $childWidth);
} else {
$childrenWidth = Math::max($childrenWidth, $child->getDimensions()->getOuterWidth());
}
}
}
if ($this->getWidth() !== null) {
Expand Down
7 changes: 6 additions & 1 deletion lib/Layout/LineBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public function divide()
}
$line->forMeasurement = $isForMeasurement;
}
unset($children);
return $lines;
}

Expand Down Expand Up @@ -420,7 +421,11 @@ public function getChildrenWidth()
$width = '0';
foreach ($this->getChildren() as $childBox) {
if ($childBox->isForMeasurement()) {
$width = Math::add($width, $childBox->getDimensions()->getOuterWidth());
if ($childWidth = $childBox->getDimensions()->getWidth()) {
$width = Math::add($width, $childWidth);
} else {
$width = Math::add($width, $childBox->getDimensions()->getOuterWidth());
}
}
}
return $width;
Expand Down
54 changes: 20 additions & 34 deletions 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 All @@ -29,11 +29,9 @@ class Math
*/
public static function add(string ...$numbers)
{
$numbers = array_reverse($numbers);
$result = array_pop($numbers);
$numbers = array_reverse($numbers);
foreach ($numbers as $number) {
$result = bcadd($result, $number, static::$scale);
$result = $numbers[0];
for ($i=1,$len=count($numbers); $i<$len; $i++) {
$result = bcadd($result, $numbers[$i], static::$scale);
}
return $result;
}
Expand All @@ -47,11 +45,9 @@ public static function add(string ...$numbers)
*/
public static function sub(string ...$numbers)
{
$numbers = array_reverse($numbers);
$result = array_pop($numbers);
$numbers = array_reverse($numbers);
foreach ($numbers as $number) {
$result = bcsub($result, $number, static::$scale);
$result = $numbers[0];
for ($i=1,$len=count($numbers); $i<$len; $i++) {
$result = bcsub($result, $numbers[$i], static::$scale);
}
return $result;
}
Expand All @@ -65,11 +61,9 @@ public static function sub(string ...$numbers)
*/
public static function mul(string ...$numbers)
{
$numbers = array_reverse($numbers);
$result = array_pop($numbers);
$numbers = array_reverse($numbers);
foreach ($numbers as $number) {
$result = bcmul($result, $number, static::$scale);
$result = $numbers[0];
for ($i=1,$len=count($numbers); $i<$len; $i++) {
$result = bcmul($result, $numbers[$i], static::$scale);
}
return $result;
}
Expand All @@ -83,16 +77,12 @@ public static function mul(string ...$numbers)
*/
public static function div(string ...$numbers)
{
foreach ($numbers as $number) {
if (static::comp($number, '0') === 0) {
$result = $numbers[0];
for ($i=1,$len=count($numbers); $i<$len; $i++) {
if ((float) $numbers[$i]===(float) 0) {
return '0';
}
}
$numbers = array_reverse($numbers);
$result = array_pop($numbers);
$numbers = array_reverse($numbers);
foreach ($numbers as $number) {
$result = bcdiv($result, $number, static::$scale);
$result = bcdiv($result, $numbers[$i], static::$scale);
}
return $result;
}
Expand All @@ -119,11 +109,9 @@ public static function comp(string $left, string $right)
*/
public static function max(string ...$numbers)
{
$numbers = array_reverse($numbers);
$result = array_pop($numbers);
$numbers = array_reverse($numbers);
foreach ($numbers as $number) {
$result = static::comp($number, $result) >= 0 ? $number : $result;
$result = $numbers[0];
for ($i=1,$len=count($numbers); $i<$len; $i++) {
$result = bccomp($numbers[$i], $result, static::$scale) >= 0 ? $numbers[$i] : $result;
}
return $result;
}
Expand All @@ -137,11 +125,9 @@ public static function max(string ...$numbers)
*/
public static function min(string ...$numbers)
{
$numbers = array_reverse($numbers);
$result = array_pop($numbers);
$numbers = array_reverse($numbers);
foreach ($numbers as $number) {
$result = static::comp($result, $number) > 0 ? $number : $result;
$result = $numbers[0];
for ($i=1,$len=count($numbers); $i<$len; $i++) {
$result = bccomp($result, $numbers[$i], static::$scale) > 0 ? $numbers[$i] : $result;
}
return $result;
}
Expand Down
35 changes: 18 additions & 17 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 @@ -1211,6 +1212,7 @@ public function breakAfter(Box $box)
}
$newBox->layout(true);
$this->document->setCurrentPage($newPage);
unset($contentBoxes);
return $this;
}

Expand All @@ -1221,7 +1223,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 All @@ -1238,9 +1240,8 @@ public function breakOverflow()
$this->document->setCurrentPage($newPage);
if (Math::comp($newBox->getDimensions()->getHeight(), $this->getDimensions()->getHeight()) > 0) {
$newPage->breakOverflow();
} else {
$newPage->getBox()->getStyle()->fixDomTree();
}
unset($clonedBoxes);
return $this;
}

Expand Down
Loading

0 comments on commit f415381

Please sign in to comment.