From c71095438e2bee562f1ebe86c7a69b9361ce19b4 Mon Sep 17 00:00:00 2001 From: Jackson Date: Mon, 21 Oct 2024 21:43:28 +1100 Subject: [PATCH 1/3] Correctly render referenced elements in use tagss --- src/Nodes/Structures/SVGUse.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Nodes/Structures/SVGUse.php b/src/Nodes/Structures/SVGUse.php index a78e308..36ed961 100644 --- a/src/Nodes/Structures/SVGUse.php +++ b/src/Nodes/Structures/SVGUse.php @@ -22,6 +22,20 @@ public function __construct() */ public function rasterize(SVGRasterizer $rasterizer): void { - // Nothing to rasterize. + $element = $this->getAttribute('xlink:href'); + if(empty($element)) return; + + /** @var SVGDocumentFragment $root */ + do { + $root = $this->getParent(); + } while ($root->getParent() != null); + $element = $root->getElementById(substr($element, strpos($element, "#") + 1 ?: 0)); + if(!$element) return; + + TransformParser::parseTransformString($this->getAttribute('transform'), $rasterizer->pushTransform()); + + $element->rasterize($rasterizer); + + $rasterizer->popTransform(); } } From 169b6d37cf8ad1922849ac2cfc373be616686ae3 Mon Sep 17 00:00:00 2001 From: Jackson Date: Mon, 21 Oct 2024 21:54:00 +1100 Subject: [PATCH 2/3] Copy over util methods from SVGImage --- src/Nodes/Structures/SVGUse.php | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Nodes/Structures/SVGUse.php b/src/Nodes/Structures/SVGUse.php index 36ed961..5420194 100644 --- a/src/Nodes/Structures/SVGUse.php +++ b/src/Nodes/Structures/SVGUse.php @@ -2,8 +2,10 @@ namespace SVG\Nodes\Structures; +use SVG\Nodes\Embedded\SVGImage; use SVG\Nodes\SVGNodeContainer; use SVG\Rasterization\SVGRasterizer; +use SVG\Rasterization\Transform\TransformParser; /** * Represents the SVG tag 'use'. @@ -17,14 +19,34 @@ public function __construct() parent::__construct(); } + /** + * @return string|null The referenced element. + */ + public function getHref(): ?string + { + return $this->getAttribute('xlink:href') ?: $this->getAttribute('href'); + } + + /** + * Sets the element reference. + * + * @param string|null $href The new element reference. + * + * @return $this This node instance, for call chaining. + */ + public function setHref(?string $href): SVGUse + { + return $this->setAttribute('xlink:href', $href); + } + /** * @inheritdoc */ public function rasterize(SVGRasterizer $rasterizer): void { - $element = $this->getAttribute('xlink:href'); + $element = $this->getHref(); if(empty($element)) return; - + /** @var SVGDocumentFragment $root */ do { $root = $this->getParent(); @@ -33,9 +55,9 @@ public function rasterize(SVGRasterizer $rasterizer): void if(!$element) return; TransformParser::parseTransformString($this->getAttribute('transform'), $rasterizer->pushTransform()); - + $element->rasterize($rasterizer); - + $rasterizer->popTransform(); } } From 6ce105e2e4ee8a362ad1097d578e7fcc91a493c0 Mon Sep 17 00:00:00 2001 From: Jackson Date: Thu, 7 Nov 2024 09:58:34 +1100 Subject: [PATCH 3/3] Expand null checks as required by linting Co-authored-by: Fabian Meyer <3982806+meyfa@users.noreply.github.com> --- src/Nodes/Structures/SVGUse.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Nodes/Structures/SVGUse.php b/src/Nodes/Structures/SVGUse.php index 5420194..bda50cc 100644 --- a/src/Nodes/Structures/SVGUse.php +++ b/src/Nodes/Structures/SVGUse.php @@ -45,14 +45,18 @@ public function setHref(?string $href): SVGUse public function rasterize(SVGRasterizer $rasterizer): void { $element = $this->getHref(); - if(empty($element)) return; + if (empty($element)) { + return; + } /** @var SVGDocumentFragment $root */ do { $root = $this->getParent(); } while ($root->getParent() != null); $element = $root->getElementById(substr($element, strpos($element, "#") + 1 ?: 0)); - if(!$element) return; + if ($element === null) { + return; + } TransformParser::parseTransformString($this->getAttribute('transform'), $rasterizer->pushTransform());