Skip to content

Commit

Permalink
Handle nested definition elements
Browse files Browse the repository at this point in the history
Such as a clipPath or symbol contained within a defs element.
  • Loading branch information
bsweeney committed Apr 8, 2024
1 parent 0e46722 commit 0e9dc9d
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/Svg/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class Document extends AbstractTag
{
protected $filename;
protected $_defs_depth = 0;
public $inDefs = false;

protected $x;
Expand Down Expand Up @@ -81,6 +82,31 @@ public function __construct() {

}

/**
* Increase the nesting level for defs-like elements
*
* @return int
*/
public function enterDefs () {
$this->_defs_depth++;
$this->inDefs = true;
return $this->_defs_depth;
}

/**
* Decrease the nesting level for defs-like elements
*
* @return int
*/
public function exitDefs () {
$this->_defs_depth--;
if ($this->_defs_depth < 0) {
$this->_defs_depth = 0;
}
$this->inDefs = ($this->_defs_depth > 0 ? true : false);
return $this->_defs_depth;
}

/**
* @return SurfaceInterface
*/
Expand Down Expand Up @@ -215,6 +241,7 @@ protected function before($attributes)

public function render(SurfaceInterface $surface)
{
$this->_defs_depth = 0;
$this->inDefs = false;
$this->surface = $surface;

Expand Down Expand Up @@ -258,7 +285,7 @@ private function _tagStart($parser, $name, $attributes)

switch (strtolower($name)) {
case 'defs':
$this->inDefs = true;
$this->enterDefs();
return;

case 'svg':
Expand Down Expand Up @@ -328,7 +355,7 @@ private function _tagStart($parser, $name, $attributes)
break;

case 'symbol':
$this->inDefs = true;
$this->enterDefs();
$tag = new Symbol($this, $name);
break;

Expand Down Expand Up @@ -381,11 +408,11 @@ function _tagEnd($parser, $name)
$tag = null;
switch (strtolower($name)) {
case 'defs':
$this->inDefs = false;
$this->exitDefs();
return;

case 'symbol':
$this->inDefs = false;
$this->exitDefs();
$tag = array_pop($this->stack);
break;

Expand Down

0 comments on commit 0e9dc9d

Please sign in to comment.