sammycage / lunasvg

SVG rendering and manipulation library in C++
MIT License
866 stars 124 forks source link

Consistency in Scaling #3

Closed Reputeless closed 4 years ago

Reputeless commented 4 years ago

Hi, @sammycage.

When I changed documentWidth and documentHeight in the svg2png example as follows, the image content was cropped in some cases.

        double documentWidth = document.documentWidth() * 0.5;
        double documentHeight = document.documentHeight() * 0.5;

scaled:

image

cropped:

image

Is there a consistent way to scale the content to fit the lunasvg::Bitmap size without cropping?

svgren has a option to request a size, and it works as I expect. https://github.com/igagis/svgren/blob/ca45437ab6b89430c8336404889b00bc5f0147ef/src/svgren/render.hpp#L12-L27

Here are the test files.

sammycage commented 4 years ago

Added but not tested : https://github.com/sammycage/lunasvg/commit/7c0ddbf547285d06935571f883e505ac3ac28982.

Reputeless commented 4 years ago

I tried the latest code d60fbc7 with the following modification:

double documentWidth = document.documentWidth() * 0.5;
double documentHeight = document.documentHeight() * 0.5;

It works fine for car.svg, but now the content of the 3.072.svg is scaled down incorrectly.

car.svg

svg_1

3.072.svg

svg_2

sammycage commented 4 years ago

The problem is that viewBox is not specified in these file. But i am currently working on a way to specify the viewBox without modifying the content of the file. Like this.

...

SVGView view(document.getBbox());

document.render(bitmap, view, dpi, bgColor);
...
sammycage commented 4 years ago

Added : https://github.com/sammycage/lunasvg/commit/aed8047ef9aed0cae09e774706b539a43f472c64

Reputeless commented 4 years ago

Now it works fine! Thank you.

image

image

sammycage commented 4 years ago

Added renderToBitmap : https://github.com/sammycage/lunasvg/commit/fc6eaff2785a550aaed34550a095accbf00829bf

SVGDocument document;
document.loadFromFile("filename.svg");
Bitmap bitmap = document.renderToBitmap();