Skip to content

Commit

Permalink
Merge branch 'use-improvements' into qa
Browse files Browse the repository at this point in the history
  • Loading branch information
bsweeney committed Jan 10, 2024
2 parents 33d6390 + 7916e2f commit a3d5617
Show file tree
Hide file tree
Showing 3 changed files with 132 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
87 changes: 87 additions & 0 deletions src/Svg/Tag/AbstractTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 a3d5617

Please sign in to comment.