Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Svg/Tag/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 9 additions & 1 deletion src/Svg/Tag/Shape.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
79 changes: 79 additions & 0 deletions tests/Svg/ShapePaintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php declare(strict_types=1);

namespace Svg\Tests;

use PHPUnit\Framework\TestCase;
use Svg\Document;
use Svg\Surface\SurfaceInterface;

final class ShapePaintTest extends TestCase
{
public static function paintProvider(): array
{
return [
'line without stroke paints nothing' => [
'<line x1="0" y1="20" x2="200" y2="20" stroke="none"/>',
['endPath'],
],
'line with the default style paints nothing' => [
'<line x1="0" y1="20" x2="200" y2="20"/>',
['endPath'],
],
'line with a fill and no stroke paints nothing' => [
'<line x1="0" y1="20" x2="200" y2="20" fill="red"/>',
['endPath'],
],
'stroked line is only stroked' => [
'<line x1="0" y1="20" x2="200" y2="20" stroke="#3366cc" stroke-width="4"/>',
['stroke'],
],
'filled rect is filled' => [
'<rect x="0" y="0" width="200" height="40" fill="#eeeeee"/>',
['fill'],
],
'filled and stroked polyline is filled and stroked' => [
'<polyline points="0,0 100,40 200,0" fill="#eeeeee" stroke="#000000"/>',
['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, '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="40">' . $element . '</svg>');

try {
$document = new Document();
$document->loadFile($file);
$document->getDimensions();
$document->render($surface);
} finally {
unlink($file);
}

$this->assertSame($expected, $calls);
}
}
Loading