Skip to content

Commit

Permalink
redistribute free space to auto columns by max-content ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalpospiech committed Nov 16, 2018
1 parent bbfeb5c commit 215b110
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 48 deletions.
42 changes: 37 additions & 5 deletions examples/SimpleTable.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@
</tr>
</table>
-->
<table style="border:1px solid red;width:50%;font-size:12px;line-height:1.2;font-family:'PT Sans';">
<tr>
<td style="border:1px solid blue;">Another table inside!!!</td>
<td style="border:1px solid green;">test</td>
</tr>
</table>
<table style="border:1px solid red;font-family:'PT Sans';font-size:12px;line-height:1.2;border-collapse:separate;">
<tr>
<td style="border:1px solid green;">
<table style="border:1px solid red;font-family:'PT Sans';font-size:12px;line-height:1.2;border-collapse:separate;">
<tr>
<td style="border:1px solid green;">
<table style="border:1px solid red;font-family:'PT Sans';font-size:12px;line-height:1.2;border-collapse:separate;">
<tr>
<td style="border:1px solid green;">
<table style="border:1px solid red;font-family:'PT Sans';font-size:12px;line-height:1.2;border-collapse:separate;">
<tr>
<td style="border:1px solid green;">cell</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>

<table style="border:1px solid red;font-family:'PT Sans';font-size:12px;line-height:1.2;border-collapse:collapse;">
<tr>
<td style="border:1px solid green;">cell</td>
Expand Down Expand Up @@ -90,7 +120,8 @@
</tr>
<tr>
<td style="border:1px solid yellow;">and</td>
<td style="border:1px solid gray;">oieto
<td style="border:1px solid gray;">
oieto
<table style="border:1px solid red;">
<tr>
<td style="border:1px solid blue;">Another table inside!!!</td>
Expand All @@ -108,11 +139,11 @@
<tr>
<td style="border:1px solid red;">
This-is-table-cell
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:collapse;">
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:separate;">
<tr>
<td>
table-inside parent table as child
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:collapse;">
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:separate;">
<tr>
<td>
table-inside parent table as child
Expand All @@ -129,7 +160,7 @@
</td>
<td style="border:1px solid red;">
This-is-table-cell
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:collapse;">
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:separate;">
<tr>
<td>
table-inside parent table as child eeeee
Expand All @@ -140,7 +171,7 @@
</td>
<td style="border:1px solid red;">
This-is-table-cell
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:collapse;">
<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid blue;border-collapse:separate;">
<tr>
<td>
table-inside parent table as child
Expand Down Expand Up @@ -173,6 +204,7 @@
</td>
</tr>
</table>

<table style="font-family:'PT Sans';font-size:12px;line-height:1.2;border:1px solid black;border-collapse:collapse;">
<tr>
<td style="border:1px solid red;">
Expand Down
Binary file modified examples/SimpleTable.pdf
Binary file not shown.
Binary file modified examples/WidthsSpecifiedTable.pdf
Binary file not shown.
83 changes: 53 additions & 30 deletions lib/Layout/TableBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ class TableBox extends BlockBox
protected $minWidth = '0';
protected $preferredWidth = '0';
protected $contentWidth = '0';
protected $gridMin = '0';
protected $gridMax = '0';
protected $percentages = [];
protected $intrinsicSum = '0';
protected $cellSpacingWidth = '0';
protected $borderWidth = '0';
protected $usedWidth = '0';
protected $assignableWidth = '0';
protected $percentColumns = [];
protected $pixelColumns = [];
protected $autoColumns = [];
Expand Down Expand Up @@ -198,13 +194,6 @@ public function setUpWidths(array $columnGroups, string $availableSpace)
$spacing = $this->getStyle()->getRules('border-spacing');
$this->cellSpacingWidth = Math::mul((string)(count($columnGroups) + 1), $spacing);
}
//$parentStyle = $this->getParent()->getStyle();
//$parentSpacing = Math::add($parentStyle->getHorizontalBordersWidth(), $parentStyle->getHorizontalPaddingsWidth());
//$availableSpace = Math::sub($availableSpace, $parentSpacing);
$this->gridMin = Math::add($this->minWidth, $this->cellSpacingWidth, $this->borderWidth);
$this->gridMax = Math::add($this->preferredWidth, $this->cellSpacingWidth, $this->borderWidth);
$this->usedWidth = Math::max(Math::min($this->gridMax, $availableSpace), $this->gridMin);
$this->assignableWidth = Math::sub($this->usedWidth, $this->cellSpacingWidth);
}

/**
Expand Down Expand Up @@ -259,7 +248,7 @@ protected function setRowsWidth()
$parentStyle = $this->getParent()->getStyle();
$parentSpacing = Math::add($parentStyle->getHorizontalPaddingsWidth(), $parentStyle->getHorizontalBordersWidth());
$width = Math::add($width, $parentSpacing);
$this->getParent()->getDimensions()->setWidth($width);
//$this->getParent()->getDimensions()->setWidth($width);
return $this;
}

Expand Down Expand Up @@ -353,8 +342,10 @@ protected function addToOthers(string $leftSpace, bool $withPreferred = false)
}
// first redistribute it to auto columns because they are most flexible ones
if ($count = count($this->autoColumns)) {
$add = Math::div($leftSpace, (string)$count);
$autoColumnsMaxWidth = $this->getAutoColumnsMaxWidth();
foreach ($this->autoColumns as $columnIndex => $columns) {
$ratio = Math::div($this->contentWidths[$columnIndex], $autoColumnsMaxWidth);
$add = Math::mul($leftSpace, $ratio);
$colWidth = Math::add($columns[0]->getDimensions()->getWidth(), $add);
foreach ($columns as $column) {
$colDmns = $column->getDimensions();
Expand Down Expand Up @@ -482,7 +473,6 @@ protected function applyPercentage()
}
}
}

return $this;
}

Expand Down Expand Up @@ -606,10 +596,41 @@ protected function getRowInnerWidth()
return $width;
}

protected function getAutoColumnsMaxWidth()
{
$autoColumnsMaxWidth = '0';
foreach ($this->autoColumns as $columnIndex => $columns) {
$autoColumnsMaxWidth = Math::add($autoColumnsMaxWidth, $this->contentWidths[$columnIndex]);
}
return $autoColumnsMaxWidth;
}

protected function getAutoColumnsMinWidth()
{
$autoColumnsMinWidth = '0';
foreach ($this->autoColumns as $columnIndex => $columns) {
$autoColumnsMinWidth = Math::add($autoColumnsMinWidth, $this->minWidths[$columnIndex]);
}
return $autoColumnsMinWidth;
}

protected function getAutoColumnsWidth()
{
$autoColumnsWidth = '0';
foreach ($this->autoColumns as $columnIndex => $columns) {
$autoColumnsWidth = Math::add($autoColumnsWidth, $columns[0]->getDimensions()->getInnerWidth());
}
return $autoColumnsWidth;
}


protected function shrinkToFit(string $availableSpace, int $step)
{
$availableSpace = Math::sub($availableSpace, $this->cellSpacingWidth);
$parentStyle = $this->getParent()->getStyle();
$parentSpacing = Math::add($parentStyle->getHorizontalBordersWidth(), $parentStyle->getHorizontalPaddingsWidth());
$availableSpace = Math::sub($availableSpace, $this->cellSpacingWidth, $parentSpacing);
$currentWidth = Math::sub($this->getRowInnerWidth(), $this->cellSpacingWidth);
//$currentWidth = $this->getRowInnerWidth();
$toRemoveTotal = Math::sub($currentWidth, $availableSpace);
$totalPercentages = '0';
foreach ($this->percentages as $percentage) {
Expand All @@ -621,12 +642,8 @@ protected function shrinkToFit(string $availableSpace, int $step)
$eachPercentagesWidth[$columnIndex] = Math::percent($percent, $availableSpace);
}
$nonPercentageSpace = Math::sub($availableSpace, $percentagesFullWidth);
$autoColumnsMinWidth = '0';
$autoColumnsMaxWidth = '0';
foreach ($this->autoColumns as $columnIndex => $columns) {
$autoColumnsMinWidth = Math::add($autoColumnsMinWidth, $this->minWidths[$columnIndex]);
$autoColumnsMaxWidth = Math::add($autoColumnsMaxWidth, $this->contentWidths[$columnIndex]);
}
$autoColumnsMinWidth = $this->getAutoColumnsMinWidth();
$autoColumnsMaxWidth = $this->getAutoColumnsMaxWidth();
$totalPixelWidth = '0';
foreach ($this->pixelColumns as $columnIndex => $columns) {
$totalPixelWidth = Math::add($totalPixelWidth, $this->preferredWidths[$columnIndex]);
Expand Down Expand Up @@ -693,10 +710,9 @@ protected function shrinkToFit(string $availableSpace, int $step)
return $this->finish();
}

protected function tryPreferred(string $leftSpace)
protected function tryPreferred(string $leftSpace, bool $outerWidthSet)
{
// left space is 100% width that we can use
$leftSpace = Math::sub($leftSpace, $this->cellSpacingWidth);
$totalPercentages = '0';
$totalPercentagesWidth = '0';
foreach ($this->percentages as $columnIndex => $percentage) {
Expand All @@ -715,6 +731,9 @@ protected function tryPreferred(string $leftSpace)
$neededTotal = Math::add($neededTotal, $needed[$columnIndex]);
}
}
if (Math::comp($neededTotal, '0') === 0 && !$outerWidthSet) {
return $this->setRowsWidth();
}
$currentPercentsWidth = '0';
$addToPercents = Math::min($neededTotal, $forPercentages);
foreach ($this->percentColumns as $columnIndex => $columns) {
Expand All @@ -730,11 +749,14 @@ protected function tryPreferred(string $leftSpace)
// we've added space to percentage columns, now we must calculate how much space we need to add (to have 100%)
$leftSpace = Math::sub($leftSpace, $addToPercents);
$leftPercent = Math::sub('100', $totalPercentages);
// if 25% = $currentPercentsWidth
$onePercent = Math::div($currentPercentsWidth, $totalPercentages);
$restMustHave = Math::mul($leftPercent, $onePercent);
$restHave = $this->getCurrentOthersWidth();
$leftSpace = Math::min($leftSpace, Math::sub($restMustHave, $restHave));
// if 25% = $currentPercentsWidth
if (Math::comp($totalPercentages, '0') > 0) {

This comment has been minimized.

Copy link
@mariuszkrzaczkowski

mariuszkrzaczkowski Nov 16, 2018

Member

?

$onePercent = Math::div($currentPercentsWidth, $totalPercentages);
$restMustHave = Math::mul($leftPercent, $onePercent);
$leftSpace = Math::min($leftSpace, Math::sub($restMustHave, $restHave));
}

if (Math::comp($leftSpace, '0') === 0) {
return $this->setRowsWidth();
}
Expand All @@ -757,9 +779,11 @@ public function measureWidth()
$columnGroups = $this->getColumns();
$this->setUpSizingTypes($columnGroups);
$availableSpace = $this->getParent()->getDimensions()->computeAvailableSpace();
$outerWidthSet = false;
if ($this->getParent()->getStyle()->getRules('width') !== 'auto') {
$this->getParent()->applyStyleWidth();
$availableSpace = Math::min($availableSpace, $this->getParent()->getDimensions()->getInnerWidth());
$outerWidthSet = true;
}
$rows = $this->rows = $this->getRows();
$this->setUpWidths($columnGroups, $availableSpace);
Expand All @@ -779,11 +803,10 @@ public function measureWidth()
$this->maxContentGuess($rows);
if (!$this->willFit($availableSpace)) {
return $this->shrinkToFit($availableSpace, $step);
//return $this->finish();
}
$leftSpace = Math::sub($availableSpace, $this->getParent()->getDimensions()->getInnerWidth());
$leftSpace = Math::sub($availableSpace, $this->getDimensions()->getWidth());
if (Math::comp($leftSpace, '0') > 0) {
$this->tryPreferred($leftSpace);
$this->tryPreferred($leftSpace, $outerWidthSet);
}
return $this->finish();
}
Expand Down
13 changes: 0 additions & 13 deletions lib/Layout/TableWrapperBlockBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,10 @@ public function appendTableBox($childDomElement, $element, $style, $parentBlock)
*/
public function measureWidth()
{
$maxWidth = '0';
$this->applyStyleWidth();
$style = $this->getStyle();
foreach ($this->getChildren() as $child) {
$child->measureWidth();
$width = $child->getDimensions()->getWidth();
$spacing = Math::add($style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth());
$maxWidth = Math::max($maxWidth, Math::add($width, $spacing));
}
/*$availableSpace = $this->getDimensions()->computeAvailableSpace();
if (Math::comp($maxWidth, $availableSpace) === 1) {
$maxWidth = $availableSpace;
} else {
$style = $this->getStyle();
$maxWidth = Math::add($maxWidth, $style->getHorizontalBordersWidth(), $style->getHorizontalPaddingsWidth());
}*/
$this->getDimensions()->setWidth($maxWidth);
return $this;
}

Expand Down

0 comments on commit 215b110

Please sign in to comment.