Ignore Fractional Part of Drawing Shadow Alpha

Fix #4415. We store the rarely-used property Drawing/Shadow/Alpha as an integer representing the percentage. Excel also stores it as an integer, but multiplies it by 1,000, so we divide by 1,000 when we read this value. This can, and in the case of the issue at hand does, leave a fractional portion. Php has deprecated passing a float with a fractional portion to an int argument, so the reporter saw a deprecation message. This is easily fixed.
This commit is contained in:
oleibman
2025-03-20 20:11:11 -07:00
parent 74dca30c89
commit 71b243539a
3 changed files with 61 additions and 2 deletions
+14 -2
View File
@@ -1492,7 +1492,13 @@ class Xlsx extends BaseReader
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn'));
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr;
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val'));
$shadow->setAlpha(self::getArrayItem(self::getAttributes($clr->alpha), 'val') / 1000); // @phpstan-ignore-line
if ($clr->alpha) {
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val'));
if (is_numeric($alpha)) {
$alpha = (int) ($alpha / 1000);
$shadow->setAlpha($alpha);
}
}
}
$this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks);
@@ -1597,7 +1603,13 @@ class Xlsx extends BaseReader
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn'));
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr;
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val'));
$shadow->setAlpha(self::getArrayItem(self::getAttributes($clr->alpha), 'val') / 1000); // @phpstan-ignore-line
if ($clr->alpha) {
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val'));
if (is_numeric($alpha)) {
$alpha = (int) ($alpha / 1000);
$shadow->setAlpha($alpha);
}
}
}
$this->readHyperLinkDrawing($objDrawing, $twoCellAnchor, $hyperlinks);
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PHPUnit\Framework\TestCase;
class Issue4415Test extends TestCase
{
private static string $file = 'tests/data/Reader/XLSX/issue.4415.xlsx';
public function testPreliminaries(): void
{
$file = 'zip://';
$file .= self::$file;
$file .= '#xl/drawings/drawing1.xml';
$data = file_get_contents($file) ?: '';
$expected = '<a:alpha val="72600"/>';
self::assertStringContainsString($expected, $data);
$expected = '<a:alpha val="90100"/>';
self::assertStringContainsString($expected, $data);
self::assertSame(2, substr_count($data, '<a:alpha '), 'number of drawings with alpha');
self::assertSame(2, substr_count($data, '<xdr:oneCellAnchor>'), 'first 2 drawings');
self::assertSame(1, substr_count($data, '<xdr:twoCellAnchor>'), 'third drawings');
}
public function testFractionalAlpha(): void
{
$file = self::$file;
$reader = new XlsxReader();
$spreadsheet = $reader->load($file);
$sheet = $spreadsheet->getActiveSheet();
$drawings = $sheet->getDrawingCollection();
self::assertCount(3, $drawings);
self::assertNotNull($drawings[0]);
self::assertNotNull($drawings[1]);
self::assertNotNull($drawings[2]);
self::assertSame(50, $drawings[0]->getShadow()->getAlpha());
self::assertSame(72, $drawings[1]->getShadow()->getAlpha());
self::assertSame('', $drawings[1]->getCoordinates2(), 'one cell anchor');
self::assertSame(90, $drawings[2]->getShadow()->getAlpha());
self::assertNotEquals('', $drawings[2]->getCoordinates2(), 'two cell anchor');
$spreadsheet->disconnectWorksheets();
}
}
Binary file not shown.