From 9b03fc35bfd0e2452c34a3e9d9d0543fe931ab50 Mon Sep 17 00:00:00 2001 From: Mantas Kantautas Date: Sat, 24 Oct 2026 18:31:10 +0300 Subject: [PATCH] Paint line elements with stroke only SVG line elements are never filled. Shape::after() applied the fill, black by default, to lines as well, so a line with stroke="none" still emitted a zero-area fill that PDF viewers draw as a hairline, and a stroked line was filled and stroked. Refs #122 --- src/Svg/Tag/Line.php | 10 +++++ src/Svg/Tag/Shape.php | 10 ++++- tests/Svg/ShapePaintTest.php | 79 ++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tests/Svg/ShapePaintTest.php 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); + } +}