Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set a default font-size #198

Merged
merged 1 commit into from
Jan 22, 2023
Merged
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
1 change: 1 addition & 0 deletions src/Nodes/Structures/SVGDocumentFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SVGDocumentFragment extends SVGNodeContainer
'stroke' => 'none',
'stroke-width' => '1',
'opacity' => '1',
'font-size' => '16px',
];

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Nodes/Texts/SVGText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
1 change: 1 addition & 0 deletions tests/Nodes/Structures/SVGDocumentFragmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
38 changes: 38 additions & 0 deletions tests/Nodes/Texts/SVGTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace SVG;

use SVG\Nodes\Texts\SVGText;

/**
* @coversDefaultClass \SVG\Nodes\Texts\SVGText
* @covers ::<!public>
*
* @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);
}
}