Let column width calculation return a float

This commit is contained in:
MarkBaker
2023-02-11 21:23:22 +01:00
parent 57424102f1
commit 6b1538f9f0
3 changed files with 26 additions and 23 deletions
+4 -4
View File
@@ -358,7 +358,7 @@ class Font
?FontStyle $defaultFont = null,
bool $filterAdjustment = false,
int $indentAdjustment = 0
): int {
): float {
// If it is rich text, use plain text
if ($cellText instanceof RichText) {
$cellText = $cellText->getPlainText();
@@ -412,13 +412,13 @@ class Font
$columnWidth = Drawing::pixelsToCellDimension((int) $columnWidth, $defaultFont ?? new FontStyle());
// Return
return (int) round($columnWidth, 6);
return round($columnWidth, 4);
}
/**
* Get GD text width in pixels for a string of text in a certain font at a certain rotation angle.
*/
public static function getTextWidthPixelsExact(string $text, FontStyle $font, int $rotation = 0): int
public static function getTextWidthPixelsExact(string $text, FontStyle $font, int $rotation = 0): float
{
// font size should really be supplied in pixels in GD2,
// but since GD2 seems to assume 72dpi, pixels and points are the same
@@ -437,7 +437,7 @@ class Font
$upperLeftCornerX = $textBox[6];
// Consider the rotation when calculating the width
return max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX);
return round(max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX), 4);
}
/**
+12 -9
View File
@@ -782,15 +782,18 @@ class Worksheet implements IComparable
if ($cellValue !== null && $cellValue !== '') {
$autoSizes[$this->cellCollection->getCurrentColumn()] = max(
(float) $autoSizes[$this->cellCollection->getCurrentColumn()],
(float) Shared\Font::calculateColumnWidth(
$this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont(),
$cellValue,
(int) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())
->getAlignment()->getTextRotation(),
$this->getParentOrThrow()->getDefaultStyle()->getFont(),
$filterAdjustment,
$indentAdjustment
$autoSizes[$this->cellCollection->getCurrentColumn()],
round(
Shared\Font::calculateColumnWidth(
$this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont(),
$cellValue,
(int) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())
->getAlignment()->getTextRotation(),
$this->getParentOrThrow()->getDefaultStyle()->getFont(),
$filterAdjustment,
$indentAdjustment
),
3
)
);
}
+10 -10
View File
@@ -106,7 +106,7 @@ class FontTest extends TestCase
* @dataProvider providerCalculateApproximateColumnWidth
*/
public function testCalculateApproximateColumnWidth(
int $expectedWidth,
float $expectedWidth,
StyleFont $font,
string $text,
int $rotation,
@@ -121,15 +121,15 @@ class FontTest extends TestCase
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],
[13.9966, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 0],
[16.2817, new StyleFont(), 'Hello World', 0, new StyleFont(), true, 0],
[16.2817, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 1],
[18.7097, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 2],
[20.9949, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 3],
[6.9983, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), false, 0],
[9.2834, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), true, 0],
[17.5671, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 0],
[19.8523, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 1],
];
}
}