css4j / echosvg

SVG implementation in the Java™ Language, fork of Apache Batik, supporting level 4 selectors and colors.
Apache License 2.0
39 stars 2 forks source link

SVG scale problem while painting #90

Closed Frank-99 closed 10 months ago

Frank-99 commented 10 months ago

Every example I managed to find online (not many out there) for painting an svg with the Batik / EchoSVG library, will suggest something like this

        g.transform(ViewBox.getViewTransform(ref, root, width, height, ctx));
        svgRoot.paint(g);

or this

        g.scale(svgWidth / iconWidth, finalHeight / svgHeight);

Which results in an image that looks as if the vector image got painted with the original dimensions and then got scaled to the final dimensions

image

I then tried to set the width and height manually to the desired final dimension, hoping it would scale the svg before painting it, like this

    document.getRootElement().setAttribute("width", "" + width);
    document.getRootElement().setAttribute("height", "" + height);

With the svgRoot.paint(g); used before it still comes out wrong, but if for example I draw the outlines like this

g.draw(svgRoot.getRoot().getOutline());

Then the scaling of the stroke is correct and as expected

image

I can't find anything about this online and I wonder if there's something already built in to repaint the svg in different scales without loosing the stroke width fedelity...