Skip to content

Commit

Permalink
Added minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszkrzaczkowski committed May 20, 2022
1 parent 1e99826 commit 7bff63f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
59 changes: 30 additions & 29 deletions lib/Layout/TableBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,36 +294,37 @@ public function setUpWidths(string $availableSpace)
{
foreach ($this->getChildren() as $rowGroup) {
foreach ($rowGroup->getChildren() as $row) {
$columns = $row->getChildren();
foreach ($columns as $columnIndex => $column) {
$cell = $column->getFirstChild();
$cellStyle = $cell->getStyle();
$columnInnerWidth = $cell->getDimensions()->getMaxWidth();
$styleWidth = $column->getStyle()->getRules('width');
$this->contentWidths[$columnIndex] = Math::max($this->contentWidths[$columnIndex] ?? '0', $columnInnerWidth);
$minColumnWidth = $cell->getDimensions()->getMinWidth();
if ($column->getColSpan() > 1) {
$minColumnWidth = Math::div($minColumnWidth, (string) $column->getColSpan());
}
$this->minWidths[$columnIndex] = Math::max($this->minWidths[$columnIndex] ?? '0', $minColumnWidth);
if ('auto' !== $styleWidth && false === strpos($styleWidth, '%')) {
if ($columns = $row->getChildren()) {
foreach ($columns as $columnIndex => $column) {
$cell = $column->getFirstChild();
$cellStyle = $cell->getStyle();
$columnInnerWidth = $cell->getDimensions()->getMaxWidth();
$styleWidth = $column->getStyle()->getRules('width');
$this->contentWidths[$columnIndex] = Math::max($this->contentWidths[$columnIndex] ?? '0', $columnInnerWidth);
$minColumnWidth = $cell->getDimensions()->getMinWidth();
if ($column->getColSpan() > 1) {
$styleWidth = Math::div($styleWidth, (string) $column->getColSpan());
$minColumnWidth = Math::div($minColumnWidth, (string) $column->getColSpan());
}
$preferred = Math::max($styleWidth, $minColumnWidth);
$this->minWidths[$columnIndex] = $preferred;
} elseif (strpos($styleWidth, '%') > 0) {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
$this->percentages[$columnIndex] = Math::max($this->percentages[$columnIndex] ?? '0', trim($styleWidth, '%'));
} else {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
$this->minWidths[$columnIndex] = Math::max($this->minWidths[$columnIndex] ?? '0', $minColumnWidth);
if ('auto' !== $styleWidth && false === strpos($styleWidth, '%')) {
if ($column->getColSpan() > 1) {
$styleWidth = Math::div($styleWidth, (string) $column->getColSpan());
}
$preferred = Math::max($styleWidth, $minColumnWidth);
$this->minWidths[$columnIndex] = $preferred;
} elseif (strpos($styleWidth, '%') > 0) {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
$this->percentages[$columnIndex] = Math::max($this->percentages[$columnIndex] ?? '0', trim($styleWidth, '%'));
} else {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
}
$this->preferredWidths[$columnIndex] = $preferred;
}
$this->preferredWidths[$columnIndex] = $preferred;
$this->borderWidth = Math::add($this->borderWidth, $cellStyle->getHorizontalBordersWidth());
$this->minWidth = Math::add($this->minWidth, $this->minWidths[$columnIndex]);
$this->contentWidth = Math::add($this->contentWidth, $this->contentWidths[$columnIndex]);
$this->preferredWidth = Math::add($this->preferredWidth, $this->preferredWidths[$columnIndex]);
}
$this->borderWidth = Math::add($this->borderWidth, $cellStyle->getHorizontalBordersWidth());
$this->minWidth = Math::add($this->minWidth, $this->minWidths[$columnIndex]);
$this->contentWidth = Math::add($this->contentWidth, $this->contentWidths[$columnIndex]);
$this->preferredWidth = Math::add($this->preferredWidth, $this->preferredWidths[$columnIndex]);
}
}
if ('collapse' !== $this->getParent()->getStyle()->getRules('border-collapse')) {
Expand Down Expand Up @@ -358,9 +359,9 @@ protected function setUpSizingTypes()
foreach ($this->getChildren() as $rowGroup) {
foreach ($rowGroup->getChildren() as $row) {
foreach ($row->getChildren() as $columnIndex => $column) {
if ('percent' === $columnSizingTypes[$columnIndex]) {
if (isset($columnSizingTypes[$columnIndex]) && 'percent' === $columnSizingTypes[$columnIndex]) {
$this->percentColumns[$columnIndex][] = $column;
} elseif ('pixel' === $columnSizingTypes[$columnIndex]) {
} elseif (isset($columnSizingTypes[$columnIndex]) && 'pixel' === $columnSizingTypes[$columnIndex]) {
$this->pixelColumns[$columnIndex][] = $column;
} else {
$this->autoColumns[$columnIndex][] = $column;
Expand Down Expand Up @@ -1287,7 +1288,7 @@ public function measureHeight(bool $afterPageDividing = false)
}
// column that is spanned with more than 1 row must have height that is equal to all spanned rows height
foreach ($rows as $rowIndex => $row) {
$currentRowMax = $maxRowHeights[$rowGroupIndex][$rowIndex];
$currentRowMax = $maxRowHeights[$rowGroupIndex][$rowIndex] ?? '0';
foreach ($row->getChildren() as $column) {
$rowSpan = $column->getRowSpan();
if ($rowSpan > 1) {
Expand Down
23 changes: 13 additions & 10 deletions lib/Style/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
*/
class Color
{
/**
* Color names hash values.
*
* @var array
*/
/** @var string[] Color names hash values. */
protected static $colorNames = [
'aliceblue' => '#f0f8ff',
'antiquewhite' => '#faebd7',
Expand Down Expand Up @@ -176,14 +172,19 @@ class Color
'yellowgreen' => '#9acd32',
];

/** @var array Color names rgba values. */
protected static $colorCustomNames = [
'transparent' => [0, 0, 0, 0.1]
];

/**
* Get rgba array from color name.
*
* @param string $colorName
*
* @return string[]
*/
public static function fromName(string $colorName)
public static function fromName(string $colorName): array
{
$colorName = strtolower($colorName);
if (isset(static::$colorNames[$colorName])) {
Expand All @@ -199,7 +200,7 @@ public static function fromName(string $colorName)
*
* @return string[]
*/
public static function fromHash(string $hashColor)
public static function fromHash(string $hashColor): array
{
$color = substr($hashColor, 1);
if (3 === \strlen($color)) {
Expand Down Expand Up @@ -233,7 +234,7 @@ public static function fromHash(string $hashColor)
*
* @return string[] rgb/a
*/
public static function fromRGBA(string $rgbColor)
public static function fromRGBA(string $rgbColor): array
{
$matches = [];
preg_match_all('/rgb\(([0-9]+)\s?\,\s?([0-9]+)\s?\,\s?([0-9]+)\s?([0-9]+)?\s?\)/ui', str_replace("\n\t\r ", '', $rgbColor), $matches);
Expand All @@ -253,7 +254,7 @@ public static function fromRGBA(string $rgbColor)
*
* @return int[]
*/
public static function toRGBA($colorInput, bool $inPDFColorSpace = false)
public static function toRGBA($colorInput, bool $inPDFColorSpace = false): array
{
$colorInput = trim(strtolower($colorInput));
if ($colorInput) {
Expand All @@ -263,6 +264,8 @@ public static function toRGBA($colorInput, bool $inPDFColorSpace = false)
$color = static::fromRGBA($colorInput);
} elseif (\array_key_exists($colorInput, static::$colorNames)) {
$color = static::fromName($colorInput);
} elseif (isset(static::$colorCustomNames[$colorInput])) {
return static::$colorCustomNames[$colorInput];
}
$r = $inPDFColorSpace ? Math::div($color[0], '255') : $color[0];
$g = $inPDFColorSpace ? Math::div($color[1], '255') : $color[1];
Expand All @@ -280,7 +283,7 @@ public static function toRGBA($colorInput, bool $inPDFColorSpace = false)
*
* @return string
*/
public static function toPdfString(string $colorInput)
public static function toPdfString(string $colorInput): string
{
$color = static::toRGBA($colorInput);
return "{$color[0]} {$color[1]} {$color[2]} RG";
Expand Down
6 changes: 5 additions & 1 deletion lib/Style/Normalizer/MarginLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class MarginLeft extends Normalizer
public function normalize($ruleValue, string $ruleName = ''): array
{
if (null === $this->normalized) {
return $this->normalized = ['margin-left' => $this->getNumberValues($ruleValue)[0]];
$css = [];
if ($number = $this->getNumberValues($ruleValue)) {
$css = ['margin-left' => $number[0]];
}
$this->normalized = $css;
}
return $this->normalized;
}
Expand Down
16 changes: 9 additions & 7 deletions lib/Style/Normalizer/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ public function getNumberValues($ruleValue, bool $isFont = false)
if ($ruleValue instanceof NumericValue) {
return $ruleValue;
}
$matches = [];
preg_match_all(static::$numericRegex, $ruleValue, $matches, PREG_SET_ORDER);
if (!$matches) {
$matches = [['0', '0', '0']];
}
$originalSize = $matches[0][2];
$originalUnit = 'em';
if (isset($matches[0][3])) {
Expand All @@ -97,25 +99,25 @@ public function getNumberValues($ruleValue, bool $isFont = false)
$matchesCount = \count($matches);
if ($matchesCount >= 2) {
$multi[] = (new NumericValue())
->setUnit($matches[1][3])
->setUnit($matches[1][3] ?? $originalUnit)
->setValue($matches[1][2])
->setOriginal($matches[1][2] . $matches[1][3])
->setOriginal($matches[1][2] . ($matches[1][3] ?? $originalUnit))
->setIsFont($isFont)
->convert($this->style);
}
if ($matchesCount >= 3) {
$multi[] = (new NumericValue())
->setUnit($matches[2][3])
->setUnit($matches[2][3] ?? $originalUnit)
->setValue($matches[2][2])
->setOriginal($matches[2][2] . $matches[2][3])
->setOriginal($matches[2][2] . ($matches[2][3] ?? $originalUnit))
->setIsFont($isFont)
->convert($this->style);
}
if (4 === $matchesCount) {
$multi[] = (new NumericValue())
->setUnit($matches[3][3])
->setUnit($matches[3][3] ?? $originalUnit)
->setValue($matches[3][2])
->setOriginal($matches[3][2] . $matches[3][3])
->setOriginal($matches[3][2] . ($matches[3][3] ?? $originalUnit))
->setIsFont($isFont)
->convert($this->style);
}
Expand Down

0 comments on commit 7bff63f

Please sign in to comment.