From 1026ed0c2a2a667ce86c2c130141b136adf7285a Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Sun, 22 Jan 2023 14:52:29 +0100 Subject: [PATCH] fix: Set a default font-size Fixes #195 --- src/Nodes/Structures/SVGDocumentFragment.php | 1 + src/Nodes/Texts/SVGText.php | 3 +- .../Structures/SVGDocumentFragmentTest.php | 1 + tests/Nodes/Texts/SVGTextTest.php | 38 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/Nodes/Texts/SVGTextTest.php diff --git a/src/Nodes/Structures/SVGDocumentFragment.php b/src/Nodes/Structures/SVGDocumentFragment.php index 41ee174..ac14fed 100644 --- a/src/Nodes/Structures/SVGDocumentFragment.php +++ b/src/Nodes/Structures/SVGDocumentFragment.php @@ -21,6 +21,7 @@ class SVGDocumentFragment extends SVGNodeContainer 'stroke' => 'none', 'stroke-width' => '1', 'opacity' => '1', + 'font-size' => '16px', ]; /** diff --git a/src/Nodes/Texts/SVGText.php b/src/Nodes/Texts/SVGText.php index c063453..1b1200c 100644 --- a/src/Nodes/Texts/SVGText.php +++ b/src/Nodes/Texts/SVGText.php @@ -85,7 +85,8 @@ public function rasterize(SVGRasterizer $rasterizer): void // https://www.w3.org/TR/SVG11/text.html#FontSizeProperty // "Percentages: refer to parent element's font size" // For now, assume the standard font size of 16px as reference size - $fontSize = Length::convert($this->getComputedStyle('font-size'), 16); + // Default to 16px if font size could not be parsed + $fontSize = Length::convert($this->getComputedStyle('font-size'), 16) ?? 16; $rasterizer->render('text', [ 'x' => Length::convert($this->getAttribute('x'), $rasterizer->getDocumentWidth()), diff --git a/tests/Nodes/Structures/SVGDocumentFragmentTest.php b/tests/Nodes/Structures/SVGDocumentFragmentTest.php index e130a74..7deba0a 100644 --- a/tests/Nodes/Structures/SVGDocumentFragmentTest.php +++ b/tests/Nodes/Structures/SVGDocumentFragmentTest.php @@ -112,6 +112,7 @@ public function testGetComputedStyle() $this->assertSame('none', $obj->getComputedStyle('stroke')); $this->assertSame('1', $obj->getComputedStyle('stroke-width')); $this->assertSame('1', $obj->getComputedStyle('opacity')); + $this->assertSame('16px', $obj->getComputedStyle('font-size')); // should return null for non-defined styles $this->assertNull($obj->getComputedStyle('undefined-test-style')); diff --git a/tests/Nodes/Texts/SVGTextTest.php b/tests/Nodes/Texts/SVGTextTest.php new file mode 100644 index 0000000..b0f38b1 --- /dev/null +++ b/tests/Nodes/Texts/SVGTextTest.php @@ -0,0 +1,38 @@ + + * + * @SuppressWarnings(PHPMD) + */ +class SVGTextTest extends \PHPUnit\Framework\TestCase +{ + /** + * @covers ::rasterize + */ + public function testRasterizeShouldHaveDefaultFontSize() + { + // Test for https://github.com/meyfa/php-svg/issues/195 + + $obj = new SVGText('foo', 10, 10); + + $rast = $this->getMockBuilder('\SVG\Rasterization\SVGRasterizer') + ->disableOriginalConstructor() + ->getMock(); + + // should call image renderer with correct options + $rast->expects($this->once())->method('render')->with( + $this->identicalTo('text'), + $this->callback(function ($options) { + return isset($options['fontSize']) && $options['fontSize'] === 16; + }), + $this->identicalTo($obj) + ); + $obj->rasterize($rast); + } +}