Skip to content

Commit 611122c

Browse files
author
Rafał Pośpiech
committed
more accurate calculations when needed
1 parent ebddb28 commit 611122c

9 files changed

+60
-2
lines changed

examples/DefaultStyles.pdf

0 Bytes
Binary file not shown.

examples/Invoice.pdf

0 Bytes
Binary file not shown.

examples/RowSpan.pdf

0 Bytes
Binary file not shown.

examples/TableClassNames.pdf

0 Bytes
Binary file not shown.

examples/helloWorld.pdf

-4 Bytes
Binary file not shown.

lib/Layout/ImageBox.php

+4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public function measureWidth()
3535
$width = $this->getParent()->getDimensions()->computeAvailableSpace();
3636
}
3737
if ($style->getRules('height') !== 'auto') {
38+
Math::setAccurate(true);
3839
$ratio = $img->getRatio();
3940
$width = Math::mul($ratio, $this->getDimensions()->getStyleHeight());
41+
Math::setAccurate(false);
4042
}
4143
$width = Math::add($width, $style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth());
4244
$this->getDimensions()->setWidth($width);
@@ -57,8 +59,10 @@ public function measureHeight(bool $afterPageDividing = false)
5759
{
5860
$style = $this->getStyle();
5961
$img = $style->getBackgroundImageStream();
62+
Math::setAccurate(true);
6063
$ratio = $img->getRatio();
6164
$height = Math::div($this->getDimensions()->getInnerWidth(), $ratio);
65+
Math::setAccurate(false);
6266
$height = Math::add($height, $style->getVerticalBordersWidth(), $style->getVerticalPaddingsWidth());
6367
$this->getDimensions()->setHeight($height);
6468
return $this;

lib/Layout/TableBox.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,19 @@ protected function addToPreferredOthers(string $leftSpace)
459459
$totalNeeded = Math::add($autoNeededTotal, $pixelNeededTotal);
460460
$totalToAdd = Math::min($leftSpace, $totalNeeded);
461461
// we know how much we can distribute
462+
Math::setAccurate(true);
462463
$autoTotalRatio = Math::div($autoNeededTotal, $totalNeeded);
463464
$addToAutoTotal = Math::mul($autoTotalRatio, $totalToAdd);
465+
Math::setAccurate(false);
464466
$addToPixelTotal = Math::sub($totalToAdd, $addToAutoTotal);
465467
// we know how much space we can add to each column type (auto and pixel)
466468
// now we must distribute this space according to concrete column needs
467469
foreach ($this->autoColumns as $columnIndex => $columns) {
468470
if (isset($autoNeeded[$columnIndex])) {
471+
Math::setAccurate(true);
469472
$neededRatio = Math::div($autoNeeded[$columnIndex], $autoNeededTotal);
470473
$add = Math::mul($neededRatio, $addToAutoTotal);
474+
Math::setAccurate(false);
471475
$columnWidth = Math::add($columns[0]->getDimensions()->getWidth(), $add);
472476
foreach ($columns as $column) {
473477
$colDmns = $column->getDimensions();
@@ -478,8 +482,10 @@ protected function addToPreferredOthers(string $leftSpace)
478482
}
479483
foreach ($this->pixelColumns as $columnIndex => $columns) {
480484
if (isset($pixelNeeded[$columnIndex])) {
485+
Math::setAccurate(true);
481486
$neededRatio = Math::div($pixelNeeded[$columnIndex], $pixelNeededTotal);
482487
$add = Math::mul($neededRatio, $addToPixelTotal);
488+
Math::setAccurate(false);
483489
$columnWidth = Math::add($columns[0]->getDimensions()->getWidth(), $add);
484490
foreach ($columns as $column) {
485491
$colDmns = $column->getDimensions();
@@ -500,7 +506,6 @@ protected function addToPreferredOthers(string $leftSpace)
500506
protected function getCurrentOthersWidth()
501507
{
502508
$currentOthersWidth = '0';
503-
504509
foreach ($this->autoColumns as $columns) {
505510
$currentOthersWidth = Math::add($currentOthersWidth, $columns[0]->getDimensions()->getInnerWidth());
506511
}
@@ -555,7 +560,9 @@ protected function expandPercentsToMin(string $availableSpace)
555560
$maxPercentRatioIndex = 0;
556561
$ratioPercent = '0';
557562
foreach ($this->percentages as $columnIndex => $percent) {
563+
Math::setAccurate(true);
558564
$ratio = Math::div($this->minWidths[$columnIndex], $percent);
565+
Math::setAccurate(false);
559566
if (Math::comp($ratio, $maxPercentRatio) > 0) {
560567
$maxPercentRatio = $ratio;
561568
$maxPercentRatioIndex = $columnIndex;
@@ -975,8 +982,10 @@ protected function shrinkToFit(string $availableSpace, int $step)
975982
// minimal stays minimal, decreasing pixels
976983
$toPixelDisposition = Math::sub($nonPercentageSpace, $autoColumnsMinWidth);
977984
foreach ($this->pixelColumns as $columnIndex => $columns) {
985+
Math::setAccurate(true);
978986
$ratio = Math::div($this->preferredWidths[$columnIndex], $totalPixelWidth);
979987
$columnWidth = Math::mul($toPixelDisposition, $ratio);
988+
Math::setAccurate(false);
980989
foreach ($columns as $column) {
981990
$columnDimensions = $column->getDimensions();
982991
$columnDimensions->setWidth(Math::add($columnWidth, $column->getStyle()->getHorizontalPaddingsWidth()));
@@ -998,8 +1007,10 @@ protected function shrinkToFit(string $availableSpace, int $step)
9981007
$toAutoDisposition = Math::sub($nonPercentageSpace, $totalPixelWidth);
9991008
$nonMinWidthColumns = [];
10001009
foreach ($this->autoColumns as $columnIndex => $columns) {
1010+
Math::setAccurate(true);
10011011
$ratio = Math::div($this->contentWidths[$columnIndex], $autoColumnsMaxWidth);
10021012
$columnWidth = Math::mul($toAutoDisposition, $ratio);
1013+
Math::setAccurate(false);
10031014
if (Math::comp($this->minWidths[$columnIndex], $columnWidth) > 0) {
10041015
$toAutoDisposition = Math::sub($toAutoDisposition, Math::sub($this->minWidths[$columnIndex], $columnWidth));
10051016
$columnWidth = $this->minWidths[$columnIndex];
@@ -1013,8 +1024,10 @@ protected function shrinkToFit(string $availableSpace, int $step)
10131024
}
10141025
}
10151026
foreach ($nonMinWidthColumns as $columnIndex => $columns) {
1027+
Math::setAccurate(true);
10161028
$ratio = Math::div($this->contentWidths[$columnIndex], $autoColumnsMaxWidth);
10171029
$columnWidth = Math::mul($toAutoDisposition, $ratio);
1030+
Math::setAccurate(false);
10181031
foreach ($columns as $column) {
10191032
$columnDimensions = $column->getDimensions();
10201033
$columnDimensions->setWidth(Math::add($columnWidth, $column->getStyle()->getHorizontalPaddingsWidth()));
@@ -1061,8 +1074,10 @@ protected function addToOthers(string $leftSpace, bool $withPreferred = false)
10611074
if (!empty($this->autoColumns)) {
10621075
$autoColumnsMaxWidth = $this->getAutoColumnsMaxWidth();
10631076
foreach ($this->autoColumns as $columnIndex => $columns) {
1077+
Math::setAccurate(true);
10641078
$ratio = Math::div($this->contentWidths[$columnIndex], $autoColumnsMaxWidth);
10651079
$add = Math::mul($leftSpace, $ratio);
1080+
Math::setAccurate(false);
10661081
$colWidth = Math::add($columns[0]->getDimensions()->getWidth(), $add);
10671082
foreach ($columns as $column) {
10681083
$colDmns = $column->getDimensions();
@@ -1130,8 +1145,10 @@ protected function tryPreferred(string $leftSpace, bool $outerWidthSet)
11301145
$addToPercents = Math::min($neededTotal, $forPercentages);
11311146
foreach ($this->percentColumns as $columnIndex => $columns) {
11321147
if (Math::comp($addToPercents, $neededTotal) < 0) {
1148+
Math::setAccurate(true);
11331149
$ratio = Math::div($this->percentages[$columnIndex], $totalPercentages);
11341150
$add = Math::mul($ratio, $addToPercents);
1151+
Math::setAccurate(false);
11351152
} else {
11361153
if (isset($needed[$columnIndex])) {
11371154
$add = $needed[$columnIndex];

lib/Math.php

+33
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,41 @@
1818
*/
1919
class Math
2020
{
21+
/**
22+
* Current scale = precision.
23+
*
24+
* @var int
25+
*/
2126
public static $scale = 2;
2227

28+
/**
29+
* Standard scale = precision.
30+
*
31+
* @var int
32+
*/
33+
public static $standardScale = 2;
34+
35+
/**
36+
* High scale for more accurate calculations when needed (ratio for example).
37+
*
38+
* @var int
39+
*/
40+
public static $highScale = 12;
41+
42+
/**
43+
* Set more accurate calculation.
44+
*
45+
* @param bool $accurate
46+
*/
47+
public static function setAccurate(bool $accurate)
48+
{
49+
if ($accurate) {
50+
static::$scale = static::$highScale;
51+
} else {
52+
static::$scale = static::$standardScale;
53+
}
54+
}
55+
2356
/**
2457
* Add two numbers.
2558
*

lib/Objects/ImageStream.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace YetiForcePDF\Objects;
1515

16+
use YetiForcePDF\Math;
17+
1618
/**
1719
* Class ImageStream.
1820
*/
@@ -120,7 +122,9 @@ public function loadImage(string $fileName)
120122
$this->height = (string) $info[1];
121123
$this->displayWidth = $this->width;
122124
$this->displayHeight = $this->height;
123-
$this->ratio = \YetiForcePDF\Math::div($this->getWidth(), $this->getHeight());
125+
Math::setAccurate(true);
126+
$this->ratio = Math::div($this->getWidth(), $this->getHeight());
127+
Math::setAccurate(false);
124128
}
125129
return $this;
126130
}

0 commit comments

Comments
 (0)