mgieseki / dvisvgm

A fast DVI, EPS, and PDF to SVG converter
https://dvisvgm.de
GNU General Public License v3.0
295 stars 28 forks source link

move transform from <svg> to parent <g> (PS/PDF file clipping) #179

Closed agrahn closed 2 years ago

agrahn commented 2 years ago

This moves the transform attribute from an <svg> element to a parent <g> element. Fixes #178 .

mgieseki commented 2 years ago

Thanks for digging into the code and for providing a fix for this issue. I'd prefer a variant that fixes the issue locally with the svg element. The following patch wraps this element with a group that gets the transform attribute if necessary. So far it seems to work as expected.

diff -r 77da1159cd08 src/PsSpecialHandler.cpp
--- a/src/PsSpecialHandler.cpp  Wed Feb 23 14:59:06 2022 +0100
+++ b/src/PsSpecialHandler.cpp  Thu Feb 24 17:07:01 2022 +0100
@@ -426,13 +426,15 @@
            node = util::make_unique<SVGElement>("g");
        else {
            // clip image to its bounding box if flag 'clip' is given
-           node = util::make_unique<SVGElement>("svg");
-           node->addAttribute("overflow", "hidden");
-           node->addAttribute("x", bbox.minX());
-           node->addAttribute("y", bbox.minY());
-           node->addAttribute("width", bbox.width());
-           node->addAttribute("height", bbox.height());
-           node->addAttribute("viewBox", bbox.svgViewBoxString());
+           auto svg = util::make_unique<SVGElement>("svg");
+           svg->addAttribute("overflow", "hidden");
+           svg->addAttribute("x", bbox.minX());
+           svg->addAttribute("y", bbox.minY());
+           svg->addAttribute("width", bbox.width());
+           svg->addAttribute("height", bbox.height());
+           svg->addAttribute("viewBox", bbox.svgViewBoxString());
+           node = util::make_unique<SVGElement>("g");
+           node->append(std::move(svg));
        }
        _xmlnode = node.get();
        _psi.execute(
agrahn commented 2 years ago

Thank you @martin, I have to apologise for not having tested enough the new clipping method recently. It was only today that I stumbled across the issue when I accidentally opened a test file in Chromium.

mgieseki commented 2 years ago

Alex, there's no need to apologize. I really appreciate your input on dvisvgm. Maybe I should add some kind of a validity check that detects malformed SVG files automatically.

agrahn commented 2 years ago

Thank you, Martin, for fixing this!