From 964d9a9809a1a0ef730117f0e4fd0d5c331ff33c Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Wed, 10 Jan 2024 08:50:15 -0500 Subject: [PATCH] Improve symbol element parsing This change adds a separate Symbol tag class since the element should be treated slightly different from a Group, namely in that a vewBox can be applied. Additionally, a Symbol is like a def in that it should not be rendered until referenced by a Use element. fixes #58 --- src/Svg/Document.php | 13 +++++++++++-- src/Svg/Tag/Symbol.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/Svg/Tag/Symbol.php diff --git a/src/Svg/Document.php b/src/Svg/Document.php index 768408f..e5c2c24 100644 --- a/src/Svg/Document.php +++ b/src/Svg/Document.php @@ -23,6 +23,7 @@ use Svg\Tag\Polyline; use Svg\Tag\Rect; use Svg\Tag\Stop; +use Svg\Tag\Symbol; use Svg\Tag\Text; use Svg\Tag\StyleTag; use Svg\Tag\UseTag; @@ -323,10 +324,14 @@ private function _tagStart($parser, $name, $attributes) break; case 'g': - case 'symbol': $tag = new Group($this, $name); break; + case 'symbol': + $this->inDefs = true; + $tag = new Symbol($this, $name); + break; + case 'clippath': $tag = new ClipPath($this, $name); break; @@ -379,6 +384,11 @@ function _tagEnd($parser, $name) $this->inDefs = false; return; + case 'symbol': + $this->inDefs = false; + $tag = array_pop($this->stack); + break; + case 'svg': case 'path': case 'rect': @@ -394,7 +404,6 @@ function _tagEnd($parser, $name) case 'style': case 'text': case 'g': - case 'symbol': case 'clippath': case 'use': case 'a': diff --git a/src/Svg/Tag/Symbol.php b/src/Svg/Tag/Symbol.php new file mode 100644 index 0000000..d00e7ab --- /dev/null +++ b/src/Svg/Tag/Symbol.php @@ -0,0 +1,34 @@ + + * @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html + */ + +namespace Svg\Tag; + +use Svg\Style; + +class Symbol extends AbstractTag +{ + protected function before($attributes) + { + $surface = $this->document->getSurface(); + + $surface->save(); + + $style = $this->makeStyle($attributes); + + $this->setStyle($style); + $surface->setStyle($style); + + $this->applyViewbox($attributes); + $this->applyTransform($attributes); + } + + protected function after() + { + $this->document->getSurface()->restore(); + } +} \ No newline at end of file