Skip to content

Commit

Permalink
fix: Default x and y to 0 in ImageRenderer (#206)
Browse files Browse the repository at this point in the history
Fixes #189
  • Loading branch information
meyfa authored Apr 12, 2023
1 parent f6d501c commit 3f5dccc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/Rasterization/Renderers/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ public function render(SVGRasterizer $rasterizer, array $options, SVGNode $conte
{
$transform = $rasterizer->getCurrentTransform();

$x = $options['x'];
$y = $options['y'];
$x = $options['x'] ?? 0;
$y = $options['y'] ?? 0;
$transform->map($x, $y);

$width = $options['width'];
$height = $options['height'];
// TODO support "auto" values for width and height

$width = $options['width'] ?? 0;
$height = $options['height'] ?? 0;
if ($width <= 0 || $height <= 0) {
return;
}
$transform->resize($width, $height);

$image = $rasterizer->getImage();
Expand Down
44 changes: 34 additions & 10 deletions tests/Rasterization/Renderers/ImageRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace SVG;

use AssertGD\GDSimilarityConstraint;
use SVG\Rasterization\Renderers\ImageRenderer;
use SVG\Nodes\SVGNode;
use SVG\Rasterization\SVGRasterizer;

class SVGNodeClass extends SVGNode
{
Expand All @@ -27,17 +29,39 @@ class ImageRendererTest extends \PHPUnit\Framework\TestCase
{
public function testRender()
{
$rast = new \SVG\Rasterization\SVGRasterizer(10, 20, null, 100, 200);
$options = [
'href' => __DIR__ . '/../../sample.svg',
'x' => 10.5,
'y' => 10.5,
'width' => 100.5,
'height' => 100.5
];
$svgImageRender = new ImageRenderer();
$obj = new ImageRenderer();

$context = new SVGNodeClass();

$this->assertNull($svgImageRender->render($rast, $options, $context));
$rasterizer = new SVGRasterizer(10, 10, null, 100, 100);
$obj->render($rasterizer, [
'href' => __DIR__ . '/../../squares.svg',
'x' => 1,
'y' => 2,
'width' => 6,
'height' => 6
], $context);
$img = $rasterizer->finish();

$this->assertThat($img, new GDSimilarityConstraint('./tests/images/renderer-image.png'));
}

public function testDefaultsXAndYToZero()
{
$obj = new ImageRenderer();

$context = new SVGNodeClass();

$rasterizer = new SVGRasterizer('10px', '10px', null, 100, 100);
$obj->render($rasterizer, [
'href' => __DIR__ . '/../../squares.svg',
'x' => null,
'y' => null,
'width' => 6,
'height' => 6
], $context);
$img = $rasterizer->finish();

$this->assertThat($img, new GDSimilarityConstraint('./tests/images/renderer-image-x0y0.png'));
}
}
Binary file added tests/images/renderer-image-x0y0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/renderer-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/squares.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3f5dccc

Please sign in to comment.