diff --git a/src/Svg/Document.php b/src/Svg/Document.php index 0985d28..27c9944 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/AbstractTag.php b/src/Svg/Tag/AbstractTag.php index 708fad0..9d4e0c1 100644 --- a/src/Svg/Tag/AbstractTag.php +++ b/src/Svg/Tag/AbstractTag.php @@ -189,6 +189,93 @@ protected function applyTransform($attributes) } } + /** + * Apply a viewBox transform to the element + * + * @param array $attributes + */ + protected function applyViewbox($attributes) { + if (isset($attributes["viewbox"])) { + $surface = $this->document->getSurface(); + $viewBox = preg_split('/[\s,]+/is', trim($attributes['viewbox'])); + if (count($viewBox) == 4) { + // Computing the equivalent transform of an SVG viewport + // https://svgwg.org/svg2-draft/coords.html#ComputingAViewportsTransform + + // 1. Let vb-x, vb-y, vb-width, vb-height be the min-x, min-y, width and height values of the viewBox attribute respectively. + [$vbX, $vbY, $vbWidth, $vbHeight] = $viewBox; + + if ($vbWidth < 0 || $vbHeight < 0) { + return; + } + + if ($vbWidth == 0 || $vbHeight == 0) { + //TODO: do not render, for now scaling to 0 below + } + + // 2. Let e-x, e-y, e-width, e-height be the position and size of the element respectively. + $eX = $attributes["x"] ?? 0; + $eY = $attributes["y"] ?? 0; + $eWidth = $attributes["width"] ?? $this->document->getWidth(); + $eHeight = $attributes["height"] ?? $this->document->getHeight(); + + // 3. Let align be the align value of preserveAspectRatio, or 'xMidYMid' if preserveAspectRatio is not defined. + $preserveAspectRatio = explode(" ", $attributes["preserveAspectRatio"] ?? "xMidYMid meet"); + $align = $preserveAspectRatio[0]; + + // 4. Let meetOrSlice be the meetOrSlice value of preserveAspectRatio, or 'meet' if preserveAspectRatio is not defined or if meetOrSlice is missing from this value. + $meetOrSlice = $meetOrSlice ?? "meet"; + + // 5. Initialize scale-x to e-width/vb-width. + $scaleX = $vbWidth == 0 ? 0 : ($eWidth / $vbWidth); + + // 6. Initialize scale-y to e-height/vb-height. + $scaleY = $vbHeight == 0 ? 0 : ($eHeight / $vbHeight); + + // 7. If align is not 'none' and meetOrSlice is 'meet', set the larger of scale-x and scale-y to the smaller. + if ($align !== "none" && $meetOrSlice === "meet") { + $scaleX = min($scaleX, $scaleY); + $scaleY = min($scaleX, $scaleY); + } + + // 8. Otherwise, if align is not 'none' and meetOrSlice is 'slice', set the smaller of scale-x and scale-y to the larger. + elseif ($align !== "none" && $meetOrSlice === "slice") { + $scaleX = max($scaleX, $scaleY); + $scaleY = max($scaleX, $scaleY); + } + + // 9. Initialize translate-x to e-x - (vb-x * scale-x). + $translateX = $eX - ($vbX * $scaleX); + + // 10. Initialize translate-y to e-y - (vb-y * scale-y) + $translateY = $eY - ($vbY * $scaleY); + + // 11. If align contains 'xMid', add (e-width - vb-width * scale-x) / 2 to translate-x. + if (strpos($align, "xMid") !== false) { + $translateX += ($eWidth - $vbWidth * $scaleX) / 2; + } + + // 12. If align contains 'xMax', add (e-width - vb-width * scale-x) to translate-x. + if (strpos($align, "xMax") !== false) { + $translateX += ($eWidth - $vbWidth * $scaleX); + } + + // 13. If align contains 'yMid', add (e-height - vb-height * scale-y) / 2 to translate-y. + if (strpos($align, "yMid") !== false) { + $translateX += ($eHeight - $vbHeight * $scaleY) / 2; + } + + // 14. If align contains 'yMax', add (e-height - vb-height * scale-y) to translate-y. + if (strpos($align, "yMid") !== false) { + $translateX += ($eHeight - $vbHeight * $scaleY); + } + + $surface->translate($translateX, $translateY); + $surface->scale($scaleX, $scaleY); + } + } + } + /** * Convert the given size for the context of this current tag. * Takes a pixel-based reference, which is usually specific to the context of the size, 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