mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-30 12:07:56 +00:00
Let column width calculation return a float
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user