meerk40t / svgelements

SVG Parsing for Elements, Paths, and other SVG Objects.
MIT License
132 stars 29 forks source link

QUESTION: I don't understand why bbox is bigger than viewbox #150

Closed Esser50K closed 2 years ago

Esser50K commented 2 years ago

I attached a sample file. This file has only one path tag, but has 2 paths inside it.

If I simply pass the string of the path to the Path object it returns a bbox of (0.0, -0.009686851979103846, 602.9308328427891, 689.713956748954)

But if I parse the entire file with SVG.parse and call svg.bbox() that one returns (134.738410836746, -0.014220565800255212, 1183.1095042701288, 1211.628721269468)

also the SVG viewbox_transform then is 'translate(134.738410836746, 0) scale(1.211621877045, 1.211621877045)'. But that doesn't seem to be written in the file, so where does it come from?

Basically I would like to be able to go through the paths and call .point(x) along it and draw the SVG on a canvas. I'm having success with many files but this one is somewhat tricky.

simplified_skull.svg.zip

tatarize commented 2 years ago

This file is actually not able to be solved. And so it's usually the default height and width of 1000. This is strictly speaking not part of spec and just a value I made up. You need to specify how large your viewPort is. Not your viewbox but the actual window or box which you are viewing this graphics in.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 602.93 825.34">
<g id="Layer_1" data-name="Layer 1">
<path d="...

The svg file tag itself lacks a height or width this means per spec the height and width of the actual graphics is 100% in both dims. This means we scale the paths given to being 100% of our viewport. The physical size of the box we are using to view the graphic itself. This value was not given. So the default size of 1000 was used instead, which is injected in the conditional parameters of parse since it's an unknown with regards to the document.

Though I do see you have a point here. Rather than the fully arbitrary 1000x1000 viewport here it would make sense to say since the viewBox is 0 0 602.93 825.34 we might be better off looking at this and using the viewbox parameters as the missing values rather than using the unknown width and height arbitrarily stated. Then we would only require the purely arbitrary fallback if the viewbox and viewport are missing.

I'll fix that. It would make more sense to set the width and height to the width and height of the viewbox in this case which would natively result in the viewbox transform having no meaning at all rather than meaning scale this up so that it fits a purely arbitrary viewport that was not actually defined.

Esser50K commented 2 years ago

ah yeah didn't know about those parameters in the parse function. Will try it out like this later today. I think the random 1000x1000 viewport also explains why my code simply worked for other SVGs because their viewbox is square as well

Esser50K commented 2 years ago

thanks my math was all twisted adjusting for unnecessary things. If I set the viewport to what I want everything works perfectly

tatarize commented 2 years ago

Fixing this correctly in 1.6.4

tatarize commented 2 years ago

Actually fixed.