diff --git a/src/PhpSpreadsheet/Shared/Font.php b/src/PhpSpreadsheet/Shared/Font.php index 3ee3d5a6..1adf213e 100644 --- a/src/PhpSpreadsheet/Shared/Font.php +++ b/src/PhpSpreadsheet/Shared/Font.php @@ -229,7 +229,8 @@ class Font $cellText = '', $rotation = 0, ?FontStyle $defaultFont = null, - bool $filterAdjustment = false + bool $filterAdjustment = false, + int $indentAdjustment = 0 ): int { // If it is rich text, use plain text if ($cellText instanceof RichText) { @@ -248,12 +249,12 @@ class Font } // Try to get the exact text width in pixels - $approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX; + $approximate = self::$autoSizeMethod === self::AUTOSIZE_METHOD_APPROX; $columnWidth = 0; if (!$approximate) { $columnWidthAdjust = ceil( self::getTextWidthPixelsExact( - str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)), + str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))), $font, 0 ) * 1.07 @@ -270,7 +271,7 @@ class Font if ($approximate) { $columnWidthAdjust = self::getTextWidthPixelsApprox( - str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)), + str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))), $font, 0 ); diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index dd2770d4..4a441a93 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -756,11 +756,11 @@ class Worksheet implements IComparable //By default merged cells should be ignored $isMergedButProceed = false; - //The only exception is if it's a merge range value cell of a 'vertical' randge (1 column wide) + //The only exception is if it's a merge range value cell of a 'vertical' range (1 column wide) if ($isMerged && $cell->isMergeRangeValueCell()) { $range = $cell->getMergeRange(); $rangeBoundaries = Coordinate::rangeDimension($range); - if ($rangeBoundaries[0] == 1) { + if ($rangeBoundaries[0] === 1) { $isMergedButProceed = true; } } @@ -774,6 +774,8 @@ class Worksheet implements IComparable $filterAdjustment = true; } + $indentAdjustment = $cell->getStyle()->getAlignment()->getIndent(); + // Calculated value // To formatted string $cellValue = NumberFormat::toFormattedString( @@ -791,7 +793,8 @@ class Worksheet implements IComparable $this->getParent()->getCellXfByIndex($cell->getXfIndex()) ->getAlignment()->getTextRotation(), $this->getParent()->getDefaultStyle()->getFont(), - $filterAdjustment + $filterAdjustment, + $indentAdjustment ) ); } diff --git a/tests/PhpSpreadsheetTests/Shared/FontTest.php b/tests/PhpSpreadsheetTests/Shared/FontTest.php index 9bc1d18a..c733f8ee 100644 --- a/tests/PhpSpreadsheetTests/Shared/FontTest.php +++ b/tests/PhpSpreadsheetTests/Shared/FontTest.php @@ -99,4 +99,35 @@ class FontTest extends TestCase $width = Font::getTextWidthPixelsApprox('n', $font, -165); self::assertEquals(4, $width); } + + /** + * @dataProvider providerCalculateApproximateColumnWidth + */ + public function testCalculateApproximateColumnWidth( + int $expectedWidth, + StyleFont $font, + string $text, + int $rotation, + StyleFont $defaultFont, + bool $filter, + int $indent + ): void { + $columnWidth = Font::calculateColumnWidth($font, $text, $rotation, $defaultFont, $filter, $indent); + self::assertEquals($expectedWidth, $columnWidth); + } + + public function providerCalculateApproximateColumnWidth(): array + { + return [ + [13, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 0], + [16, new StyleFont(), 'Hello World', 0, new StyleFont(), true, 0], + [16, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 1], + [18, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 2], + [20, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 3], + [6, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), false, 0], + [9, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), true, 0], + [17, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 0], + [19, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 1], + ]; + } }