Skip to content

Commit

Permalink
Improve symbol element parsing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bsweeney committed Feb 23, 2024
1 parent 3d6b248 commit 964d9a9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Svg/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand All @@ -394,7 +404,6 @@ function _tagEnd($parser, $name)
case 'style':
case 'text':
case 'g':
case 'symbol':
case 'clippath':
case 'use':
case 'a':
Expand Down
34 changes: 34 additions & 0 deletions src/Svg/Tag/Symbol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @package php-svg-lib
* @link http://github.com/PhenX/php-svg-lib
* @author Fabien Ménager <[email protected]>
* @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();
}
}

0 comments on commit 964d9a9

Please sign in to comment.