Skip to content

Commit 964d9a9

Browse files
committed
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
1 parent 3d6b248 commit 964d9a9

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Svg/Document.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Svg\Tag\Polyline;
2424
use Svg\Tag\Rect;
2525
use Svg\Tag\Stop;
26+
use Svg\Tag\Symbol;
2627
use Svg\Tag\Text;
2728
use Svg\Tag\StyleTag;
2829
use Svg\Tag\UseTag;
@@ -323,10 +324,14 @@ private function _tagStart($parser, $name, $attributes)
323324
break;
324325

325326
case 'g':
326-
case 'symbol':
327327
$tag = new Group($this, $name);
328328
break;
329329

330+
case 'symbol':
331+
$this->inDefs = true;
332+
$tag = new Symbol($this, $name);
333+
break;
334+
330335
case 'clippath':
331336
$tag = new ClipPath($this, $name);
332337
break;
@@ -379,6 +384,11 @@ function _tagEnd($parser, $name)
379384
$this->inDefs = false;
380385
return;
381386

387+
case 'symbol':
388+
$this->inDefs = false;
389+
$tag = array_pop($this->stack);
390+
break;
391+
382392
case 'svg':
383393
case 'path':
384394
case 'rect':
@@ -394,7 +404,6 @@ function _tagEnd($parser, $name)
394404
case 'style':
395405
case 'text':
396406
case 'g':
397-
case 'symbol':
398407
case 'clippath':
399408
case 'use':
400409
case 'a':

src/Svg/Tag/Symbol.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* @package php-svg-lib
4+
* @link http://github.com/PhenX/php-svg-lib
5+
* @author Fabien Ménager <[email protected]>
6+
* @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html
7+
*/
8+
9+
namespace Svg\Tag;
10+
11+
use Svg\Style;
12+
13+
class Symbol extends AbstractTag
14+
{
15+
protected function before($attributes)
16+
{
17+
$surface = $this->document->getSurface();
18+
19+
$surface->save();
20+
21+
$style = $this->makeStyle($attributes);
22+
23+
$this->setStyle($style);
24+
$surface->setStyle($style);
25+
26+
$this->applyViewbox($attributes);
27+
$this->applyTransform($attributes);
28+
}
29+
30+
protected function after()
31+
{
32+
$this->document->getSurface()->restore();
33+
}
34+
}

0 commit comments

Comments
 (0)