From 3d6b248f9f3ec5bcfe03136c2f0555dce67d6198 Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Wed, 10 Jan 2024 08:46:59 -0500 Subject: [PATCH] Add method to apply element viewBox --- src/Svg/Tag/AbstractTag.php | 91 +++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/src/Svg/Tag/AbstractTag.php b/src/Svg/Tag/AbstractTag.php index c58c671..c74e0eb 100644 --- a/src/Svg/Tag/AbstractTag.php +++ b/src/Svg/Tag/AbstractTag.php @@ -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,