Skip to content

Commit

Permalink
Add method to apply element viewBox
Browse files Browse the repository at this point in the history
  • Loading branch information
bsweeney committed Feb 23, 2024
1 parent 092e32c commit 3d6b248
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/Svg/Tag/AbstractTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,97 @@ protected function applyTransform($attributes)
}
}

/**
* Apply a viewBox transform to the element
*
* @param array $attributes
*/
protected function applyViewbox($attributes) {
if (!isset($attributes["viewbox"])) {
return;
}

$surface = $this->document->getSurface();
$viewBox = preg_split('/[\s,]+/is', trim($attributes['viewbox']));
if (count($viewBox) != 4) {
return;
}

// 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;
}

// correct solution is to not render, for now scaling to 0 below
//if ($vbWidth == 0 || $vbHeight == 0) {
//}

// 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

0 comments on commit 3d6b248

Please sign in to comment.