Skip to content

Commit c54d55d

Browse files
authored
Merge pull request #4417 from oleibman/issue4415
Ignore Fractional Part of Drawing Shadow Alpha
2 parents 4c350a6 + 7fc2a3a commit c54d55d

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3434

3535
### Fixed
3636

37+
- Ignore fractional part of Drawing Shadow Alpha. [Issue #4415](https://github.com/PHPOffice/PhpSpreadsheet/issues/4415) [PR #4417](https://github.com/PHPOffice/PhpSpreadsheet/pull/4417)
3738
- BIN2DEC, OCT2DEC, and HEX2DEC return numbers rather than strings. [Issue #4383](https://github.com/PHPOffice/PhpSpreadsheet/issues/4383) [PR #4389](https://github.com/PHPOffice/PhpSpreadsheet/pull/4389)
3839
- Fix TREND_BEST_FIT_NO_POLY. [Issue #4400](https://github.com/PHPOffice/PhpSpreadsheet/issues/4400) [PR #4339](https://github.com/PHPOffice/PhpSpreadsheet/pull/4339)
3940
- Better handling of Chart DisplayBlanksAs. [Issue #4411](https://github.com/PHPOffice/PhpSpreadsheet/issues/4411) [PR #4414](https://github.com/PHPOffice/PhpSpreadsheet/pull/4414)

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
14921492
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn'));
14931493
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr;
14941494
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val'));
1495-
$shadow->setAlpha(self::getArrayItem(self::getAttributes($clr->alpha), 'val') / 1000); // @phpstan-ignore-line
1495+
if ($clr->alpha) {
1496+
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val'));
1497+
if (is_numeric($alpha)) {
1498+
$alpha = (int) ($alpha / 1000);
1499+
$shadow->setAlpha($alpha);
1500+
}
1501+
}
14961502
}
14971503

14981504
$this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks);
@@ -1597,7 +1603,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
15971603
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn'));
15981604
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr;
15991605
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val'));
1600-
$shadow->setAlpha(self::getArrayItem(self::getAttributes($clr->alpha), 'val') / 1000); // @phpstan-ignore-line
1606+
if ($clr->alpha) {
1607+
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val'));
1608+
if (is_numeric($alpha)) {
1609+
$alpha = (int) ($alpha / 1000);
1610+
$shadow->setAlpha($alpha);
1611+
}
1612+
}
16011613
}
16021614

16031615
$this->readHyperLinkDrawing($objDrawing, $twoCellAnchor, $hyperlinks);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4415Test extends TestCase
11+
{
12+
private static string $file = 'tests/data/Reader/XLSX/issue.4415.xlsx';
13+
14+
public function testPreliminaries(): void
15+
{
16+
$file = 'zip://';
17+
$file .= self::$file;
18+
$file .= '#xl/drawings/drawing1.xml';
19+
$data = file_get_contents($file) ?: '';
20+
$expected = '<a:alpha val="72600"/>';
21+
self::assertStringContainsString($expected, $data);
22+
$expected = '<a:alpha val="90100"/>';
23+
self::assertStringContainsString($expected, $data);
24+
self::assertSame(2, substr_count($data, '<a:alpha '), 'number of drawings with alpha');
25+
self::assertSame(2, substr_count($data, '<xdr:oneCellAnchor>'), 'first 2 drawings');
26+
self::assertSame(1, substr_count($data, '<xdr:twoCellAnchor>'), 'third drawings');
27+
}
28+
29+
public function testFractionalAlpha(): void
30+
{
31+
$file = self::$file;
32+
$reader = new XlsxReader();
33+
$spreadsheet = $reader->load($file);
34+
$sheet = $spreadsheet->getActiveSheet();
35+
$drawings = $sheet->getDrawingCollection();
36+
self::assertCount(3, $drawings);
37+
self::assertNotNull($drawings[0]);
38+
self::assertNotNull($drawings[1]);
39+
self::assertNotNull($drawings[2]);
40+
self::assertSame(50, $drawings[0]->getShadow()->getAlpha());
41+
self::assertSame(72, $drawings[1]->getShadow()->getAlpha());
42+
self::assertSame('', $drawings[1]->getCoordinates2(), 'one cell anchor');
43+
self::assertSame(90, $drawings[2]->getShadow()->getAlpha());
44+
self::assertNotEquals('', $drawings[2]->getCoordinates2(), 'two cell anchor');
45+
$spreadsheet->disconnectWorksheets();
46+
}
47+
}
28.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)