diff --git a/src/Svg/Tag/Line.php b/src/Svg/Tag/Line.php index 4f6a276..04a826c 100644 --- a/src/Svg/Tag/Line.php +++ b/src/Svg/Tag/Line.php @@ -39,4 +39,14 @@ public function start($attributes) $surface->moveTo($this->x1, $this->y1); $surface->lineTo($this->x2, $this->y2); } + + /** + * A line has no interior, so it is never filled. + * + * @see https://www.w3.org/TR/SVG11/shapes.html#LineElement + */ + protected function isFillable(): bool + { + return false; + } } diff --git a/src/Svg/Tag/Shape.php b/src/Svg/Tag/Shape.php index e9342a2..0448a95 100644 --- a/src/Svg/Tag/Shape.php +++ b/src/Svg/Tag/Shape.php @@ -32,7 +32,7 @@ protected function after() if ($this->hasShape) { $style = $surface->getStyle(); - $fill = $style->fill && is_array($style->fill); + $fill = $this->isFillable() && $style->fill && is_array($style->fill); $stroke = $style->stroke && is_array($style->stroke); if ($fill) { @@ -59,4 +59,12 @@ protected function after() $surface->restore(); } + + /** + * Whether the shape has an interior that the fill applies to. + */ + protected function isFillable(): bool + { + return true; + } } \ No newline at end of file diff --git a/tests/Svg/ShapePaintTest.php b/tests/Svg/ShapePaintTest.php new file mode 100644 index 0000000..e28f9a1 --- /dev/null +++ b/tests/Svg/ShapePaintTest.php @@ -0,0 +1,79 @@ + [ + '', + ['endPath'], + ], + 'line with the default style paints nothing' => [ + '', + ['endPath'], + ], + 'line with a fill and no stroke paints nothing' => [ + '', + ['endPath'], + ], + 'stroked line is only stroked' => [ + '', + ['stroke'], + ], + 'filled rect is filled' => [ + '', + ['fill'], + ], + 'filled and stroked polyline is filled and stroked' => [ + '', + ['fillStroke'], + ], + ]; + } + + /** + * @dataProvider paintProvider + * @param string $element + * @param array $expected + */ + #[\PHPUnit\Framework\Attributes\DataProvider('paintProvider')] + public function testPaintOperations(string $element, array $expected) + { + $style = null; + $calls = []; + + $surface = $this->createMock(SurfaceInterface::class); + $surface->method('setStyle')->willReturnCallback(function ($newStyle) use (&$style) { + $style = $newStyle; + }); + $surface->method('getStyle')->willReturnCallback(function () use (&$style) { + return $style; + }); + foreach (['fill', 'stroke', 'fillStroke', 'endPath'] as $method) { + $surface->method($method)->willReturnCallback(function () use (&$calls, $method) { + $calls[] = $method; + }); + } + + $file = tempnam(sys_get_temp_dir(), 'svg'); + file_put_contents($file, '' . $element . ''); + + try { + $document = new Document(); + $document->loadFile($file); + $document->getDimensions(); + $document->render($surface); + } finally { + unlink($file); + } + + $this->assertSame($expected, $calls); + } +}