Merge pull request #2825 from PHPOffice/Column-Autofit-Indentation
Column autofit indentation
This commit is contained in:
commit
54d82cd023
|
|
@ -229,7 +229,8 @@ class Font
|
||||||
$cellText = '',
|
$cellText = '',
|
||||||
$rotation = 0,
|
$rotation = 0,
|
||||||
?FontStyle $defaultFont = null,
|
?FontStyle $defaultFont = null,
|
||||||
bool $filterAdjustment = false
|
bool $filterAdjustment = false,
|
||||||
|
int $indentAdjustment = 0
|
||||||
): int {
|
): int {
|
||||||
// If it is rich text, use plain text
|
// If it is rich text, use plain text
|
||||||
if ($cellText instanceof RichText) {
|
if ($cellText instanceof RichText) {
|
||||||
|
|
@ -248,12 +249,12 @@ class Font
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to get the exact text width in pixels
|
// 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;
|
$columnWidth = 0;
|
||||||
if (!$approximate) {
|
if (!$approximate) {
|
||||||
$columnWidthAdjust = ceil(
|
$columnWidthAdjust = ceil(
|
||||||
self::getTextWidthPixelsExact(
|
self::getTextWidthPixelsExact(
|
||||||
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
|
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
|
||||||
$font,
|
$font,
|
||||||
0
|
0
|
||||||
) * 1.07
|
) * 1.07
|
||||||
|
|
@ -270,7 +271,7 @@ class Font
|
||||||
|
|
||||||
if ($approximate) {
|
if ($approximate) {
|
||||||
$columnWidthAdjust = self::getTextWidthPixelsApprox(
|
$columnWidthAdjust = self::getTextWidthPixelsApprox(
|
||||||
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
|
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
|
||||||
$font,
|
$font,
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -756,11 +756,11 @@ class Worksheet implements IComparable
|
||||||
//By default merged cells should be ignored
|
//By default merged cells should be ignored
|
||||||
$isMergedButProceed = false;
|
$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()) {
|
if ($isMerged && $cell->isMergeRangeValueCell()) {
|
||||||
$range = $cell->getMergeRange();
|
$range = $cell->getMergeRange();
|
||||||
$rangeBoundaries = Coordinate::rangeDimension($range);
|
$rangeBoundaries = Coordinate::rangeDimension($range);
|
||||||
if ($rangeBoundaries[0] == 1) {
|
if ($rangeBoundaries[0] === 1) {
|
||||||
$isMergedButProceed = true;
|
$isMergedButProceed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -774,6 +774,8 @@ class Worksheet implements IComparable
|
||||||
$filterAdjustment = true;
|
$filterAdjustment = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$indentAdjustment = $cell->getStyle()->getAlignment()->getIndent();
|
||||||
|
|
||||||
// Calculated value
|
// Calculated value
|
||||||
// To formatted string
|
// To formatted string
|
||||||
$cellValue = NumberFormat::toFormattedString(
|
$cellValue = NumberFormat::toFormattedString(
|
||||||
|
|
@ -791,7 +793,8 @@ class Worksheet implements IComparable
|
||||||
$this->getParent()->getCellXfByIndex($cell->getXfIndex())
|
$this->getParent()->getCellXfByIndex($cell->getXfIndex())
|
||||||
->getAlignment()->getTextRotation(),
|
->getAlignment()->getTextRotation(),
|
||||||
$this->getParent()->getDefaultStyle()->getFont(),
|
$this->getParent()->getDefaultStyle()->getFont(),
|
||||||
$filterAdjustment
|
$filterAdjustment,
|
||||||
|
$indentAdjustment
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -99,4 +99,35 @@ class FontTest extends TestCase
|
||||||
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
|
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
|
||||||
self::assertEquals(4, $width);
|
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],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue