cppfw / svgren

:camera: SVG rendering library in C++
MIT License
206 stars 41 forks source link

lib can not render svg without explicitly set size #45

Closed Papirosnik closed 6 years ago

Papirosnik commented 6 years ago

A simple svg from wiki can not be rendered by svgren, while others tools spit it out easily. A brief investigation shows that that svg doesn't contain explicit definition of size. I do realize that we should know the size of memory buffer before render and nevertheless want to find a way to render such simple svg by default. Can you take a short glance on this and produce an idea? test.svg.zip

igagis commented 6 years ago

Well, yes, your assumption is right. We need to know image dimensions before rendering. I don't know how other programs render it, perhaps they do a dry-run of render just to find out the image dimensions where all primitives fit and then do a normal render run. Or perhaps they just pick some large image size as a guess (say 2000x2000) and then try to render and if it does not fit then try to re-render with bigger size. And then trim white areas... I don't know which approach would be good here. To me it looks reasonable to require image dimensions to be specified in SVG and render nothing if those are not anyhow specified. Do you have any other ideas how this could be implemented?

Papirosnik commented 6 years ago

Nope, no reasonable ideas. I've thought about approach you called dry-run of render and afraid it might be costly for svgs with big and complex content. And at a first glance implementation also does not look trivial. At the other hand for such simple images similar to the attached one it might be pretty cheap )

The second approach sounds hardly acceptable.

So, making some restrictions or assumptions at app-level looks really a bit preferable than calculating of dimensions in the lib. But as an option...

igagis commented 6 years ago

Here is the spec to read https://www.w3.org/TR/SVG/coords.html#ViewportSpace and it looks like it does not specify what to do in this case. Also, not all programs detect the image size in this case, for example inkscape does not.

The third solution would be to select some fixed dimensions, say 300x200 and render the image in these dimensions. Not the ideal solution but at least it gives something.

igagis commented 6 years ago

looks like browsers have a default size of 300x150 to be applied to elements of unknown size, same can be done here I think

igagis commented 6 years ago

In svgdom version 0.3.9 it uses 300x150 dimensions by default.