From 7bff63f179b6378796cdcf5cdb26756adc943496 Mon Sep 17 00:00:00 2001 From: Mariusz Krzaczkowski Date: Fri, 20 May 2022 13:39:14 +0200 Subject: [PATCH] Added minor improvements --- lib/Layout/TableBox.php | 59 +++++++++++++++-------------- lib/Style/Color.php | 23 ++++++----- lib/Style/Normalizer/MarginLeft.php | 6 ++- lib/Style/Normalizer/Normalizer.php | 16 ++++---- 4 files changed, 57 insertions(+), 47 deletions(-) diff --git a/lib/Layout/TableBox.php b/lib/Layout/TableBox.php index 6226d37..0470cae 100644 --- a/lib/Layout/TableBox.php +++ b/lib/Layout/TableBox.php @@ -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')) { @@ -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; @@ -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) { diff --git a/lib/Style/Color.php b/lib/Style/Color.php index e66234d..ff47c7e 100644 --- a/lib/Style/Color.php +++ b/lib/Style/Color.php @@ -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', @@ -176,6 +172,11 @@ 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. * @@ -183,7 +184,7 @@ class Color * * @return string[] */ - public static function fromName(string $colorName) + public static function fromName(string $colorName): array { $colorName = strtolower($colorName); if (isset(static::$colorNames[$colorName])) { @@ -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)) { @@ -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); @@ -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) { @@ -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]; @@ -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"; diff --git a/lib/Style/Normalizer/MarginLeft.php b/lib/Style/Normalizer/MarginLeft.php index 80db914..a2d27a4 100644 --- a/lib/Style/Normalizer/MarginLeft.php +++ b/lib/Style/Normalizer/MarginLeft.php @@ -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; } diff --git a/lib/Style/Normalizer/Normalizer.php b/lib/Style/Normalizer/Normalizer.php index 4cf3876..22f3427 100644 --- a/lib/Style/Normalizer/Normalizer.php +++ b/lib/Style/Normalizer/Normalizer.php @@ -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])) { @@ -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); }