meerk40t / svgelements

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

calculated bbox is bigger than viewbox #124

Closed Esser50K closed 2 years ago

Esser50K commented 2 years ago

This file when parsed with the svgelements library says that the box of the one path is 542.00.. 610.55... while the viewbox is only 512, 512

That just seems odd to me and is causing some issues as well.

raspberry-pi-4-599381.svg.zip

tatarize commented 2 years ago
(svgelements.Path("""M3250 4634 c-25 -8 -67 -14 -95 -14 -32 0 -62 -7 -83 -20 -19 -12
-50 -20 -76 -20 -32 0 -54 -8 -84 -29 -22 -16 -43 -34 -47 -40 -3 -6 -20 -11
-37 -11 -49 0 -160 -120 -220 -237 l-47 -92 -44 92 c-65 134 -163 237 -228
237 -15 0 -31 5 -34 11 -4 6 -25 24 -47 40 -31 21 -52 29 -85 29 -25 0 -55 8
-70 17 -38 25 -167 38 -313 33 -16 -1 -52 -1 -79 0 -32 2 -57 -3 -72 -14 -13
-9 -42 -16 -64 -16 -23 0 -50 -7 -61 -15 -10 -8 -31 -15 -46 -15 -54 0 -127
-22 -151 -44 -17 -16 -42 -25 -77 -28 -80 -8 -104 -38 -107 -131 -1 -39 -5
-88 -8 -107 -4 -25 0 -46 14 -73 13 -26 18 -49 14 -71 -5 -34 13 -82 41 -105
10 -8 16 -29 16 -54 0 -42 29 -101 55 -111 9 -3 15 -18 15 -36 0 -22 13 -44
50 -85 28 -30 50 -62 50 -69 0 -25 49 -74 93 -92 28 -11 47 -28 57 -48 9 -18
32 -40 55 -51 22 -11 41 -24 43 -28 1 -5 -23 -26 -55 -47 -116 -78 -205 -194
-243 -318 -11 -34 -25 -112 -31 -172 l-12 -110 -92 -92 c-126 -129 -184 -243
-204 -408 -19 -151 19 -304 108 -436 27 -39 52 -87 56 -105 3 -19 20 -78 36
-132 17 -54 41 -141 54 -193 35 -139 83 -222 190 -330 67 -68 108 -100 160
-126 39 -19 99 -56 135 -82 85 -63 178 -116 267 -154 41 -17 101 -55 140 -87
77 -64 199 -128 288 -150 34 -9 104 -15 166 -15 181 0 313 47 459 164 36 29
125 82 200 118 74 37 167 91 205 120 39 29 101 68 138 87 170 85 315 280 356
481 6 30 33 118 60 196 38 112 58 153 94 200 92 116 137 243 137 386 0 104
-19 179 -71 285 -38 80 -94 146 -200 239 -35 30 -36 34 -47 141 -21 192 -71
304 -182 413 -37 36 -90 78 -117 95 l-51 30 52 25 c34 17 56 35 66 56 10 20
29 37 57 48 42 18 93 66 93 89 0 7 22 31 50 55 35 30 53 55 61 85 7 23 23 53
35 67 26 29 44 71 44 105 0 13 10 40 22 61 12 20 23 56 24 79 1 24 10 58 19
76 13 24 15 43 10 73 -4 23 -7 73 -6 112 3 90 -19 122 -89 131 -25 3 -54 13
-65 23 -33 29 -58 38 -123 46 -34 3 -67 13 -73 20 -7 9 -32 14 -66 14 -30 0
-64 5 -76 12 -36 19 -96 26 -140 16 -26 -6 -49 -6 -62 1 -36 17 -60 18 -105 5z
m-1529 -129 c24 -9 61 -27 83 -41 43 -29 61 -20 51 24 -5 22 -5 22 22 8 15 -8
43 -26 62 -40 45 -34 60 -33 68 5 l6 30 46 -45 c26 -25 52 -46 58 -46 6 0 15
12 20 26 l9 27 57 -63 c31 -35 58 -61 60 -59 2 2 7 14 13 27 9 23 11 21 53
-35 108 -144 147 -293 102 -386 -19 -39 -53 -74 -97 -100 -5 -3 -54 27 -108
67 -167 126 -361 228 -609 320 -55 20 -104 35 -109 34 -6 -2 58 -42 143 -88
243 -134 484 -302 578 -404 l24 -25 -27 -55 c-59 -120 -217 -195 -410 -196
-61 0 -68 2 -51 14 31 22 12 46 -37 46 -24 0 -60 3 -81 6 -36 6 -37 7 -17 21
11 8 20 18 20 23 0 4 -28 18 -62 30 -35 11 -79 29 -97 39 l-35 19 32 7 c18 3
32 11 32 16 0 5 -26 22 -57 38 -32 17 -71 39 -86 49 l-28 19 31 7 c18 3 30 10
28 15 -1 6 -31 32 -65 60 l-63 50 36 3 36 3 -62 70 -62 70 39 3 c21 2 42 7 46
12 5 4 -16 26 -47 49 -30 23 -60 50 -66 61 -10 18 -8 20 30 20 38 0 41 2 34
23 -3 12 -17 43 -30 70 l-24 47 96 0 95 0 -20 29 c-12 16 -21 32 -21 36 0 8
114 0 179 -12 32 -5 34 -4 29 21 -3 14 -5 26 -4 26 1 0 31 -9 68 -19 36 -11
69 -17 72 -13 13 12 5 42 -16 56 -28 20 -22 20 33 1z m1731 -8 c-25 -27 -30
-62 -8 -54 8 3 34 11 58 17 24 6 50 13 59 16 13 3 15 -1 11 -21 -5 -30 4 -31
85 -10 32 8 72 12 91 9 l33 -6 -22 -24 -22 -24 91 0 c51 0 92 -2 92 -4 0 -3
-13 -26 -29 -53 -16 -26 -32 -58 -36 -70 -6 -21 -3 -23 35 -23 l41 0 -18 -31
c-10 -17 -35 -46 -55 -64 -49 -41 -48 -55 2 -55 22 0 40 -3 40 -7 0 -4 -25
-33 -56 -64 -56 -57 -62 -79 -22 -79 22 0 17 -5 -86 -92 -18 -15 -19 -19 -6
-28 8 -5 26 -10 40 -10 l25 0 -24 -19 c-14 -11 -56 -34 -93 -52 -37 -18 -68
-37 -68 -41 0 -5 10 -12 21 -17 19 -6 16 -10 -27 -28 -27 -11 -67 -26 -88 -33
l-40 -13 24 -19 c12 -10 20 -21 17 -24 -3 -3 -44 -9 -92 -13 -101 -10 -122
-23 -85 -53 l25 -20 -85 7 c-166 13 -279 52 -348 118 -34 32 -72 95 -72 119 0
17 108 122 185 180 100 76 235 161 399 253 86 48 155 89 153 92 -8 7 -233 -85
-374 -152 -156 -75 -275 -145 -371 -219 l-70 -54 -31 19 c-16 10 -42 37 -57
59 -68 102 -28 267 103 428 19 23 38 42 43 42 4 0 11 -12 15 -27 l7 -28 58 64
c56 63 57 63 64 37 3 -14 11 -26 16 -26 5 0 30 20 54 45 51 50 63 55 68 24 6
-39 32 -46 70 -17 18 14 47 32 64 41 31 16 32 16 27 -3 -3 -11 -3 -27 1 -35 5
-13 12 -12 53 9 40 21 115 52 132 55 3 1 -5 -9 -17 -22z m-800 -1042 c199 -42
365 -198 328 -309 -12 -37 -70 -96 -118 -120 -67 -34 -183 -56 -293 -57 -168
0 -291 40 -377 124 -56 55 -70 101 -47 151 42 89 181 181 315 210 84 19 111
19 192 1z m-643 -81 c17 -4 31 -11 31 -16 0 -22 -69 -107 -159 -196 -176 -174
-401 -340 -467 -345 -28 -2 -29 0 -32 44 -4 63 21 176 55 245 60 123 189 224
329 258 62 15 190 20 243 10z m1321 -30 c128 -36 248 -132 304 -242 34 -67 56
-161 56 -241 l0 -64 -35 6 c-72 12 -263 151 -443 321 -96 93 -185 201 -179
220 9 28 198 27 297 0z m-169 -414 c159 -49 285 -178 346 -352 114 -329 -91
-623 -397 -568 -282 50 -495 348 -440 615 50 246 260 377 491 305z m-918 -34
c72 -35 149 -118 178 -194 27 -71 36 -191 19 -275 -18 -95 -78 -213 -143 -284
-110 -118 -224 -168 -367 -161 -95 5 -160 31 -228 93 -71 64 -122 191 -122
306 0 70 30 191 66 260 54 108 190 223 314 267 91 32 204 27 283 -12z m1571
-267 c141 -101 215 -284 187 -464 -14 -86 -75 -216 -125 -264 -32 -31 -38 -33
-60 -23 -51 23 -111 157 -142 319 -22 111 -24 319 -5 386 13 45 49 87 76 87 6
0 38 -19 69 -41z m-2429 -24 c25 -51 33 -230 15 -360 -28 -202 -104 -385 -160
-385 -22 0 -92 76 -118 127 -71 138 -71 327 -2 463 27 53 86 120 143 163 65
49 94 47 122 -8z m1288 -560 c140 -30 264 -123 321 -244 29 -61 31 -74 31
-171 0 -93 -3 -111 -27 -163 -31 -66 -114 -158 -176 -194 -238 -140 -568 -43
-682 199 -73 155 -34 338 98 459 121 111 269 149 435 114z m1037 -154 c60 -43
103 -241 82 -384 -18 -122 -65 -212 -166 -312 -151 -150 -325 -202 -456 -135
-78 40 -99 142 -60 293 38 152 111 268 254 405 150 144 266 188 346 133z
m-2110 -104 c224 -92 400 -340 400 -563 0 -123 -29 -166 -122 -185 -168 -34
-393 103 -489 296 -72 146 -86 294 -37 393 41 83 136 105 248 59z m1206 -772
c83 -22 179 -70 199 -102 17 -25 17 -28 -1 -62 -46 -85 -228 -198 -368 -228
-90 -19 -180 -11 -278 26 -75 29 -193 115 -232 169 -29 41 -30 45 -15 70 19
36 136 93 233 117 157 36 342 40 462 10z""") * "translate(0.000000,512.000000) scale(0.100000,-0.100000)").bbox()

(93.62218501786387, 47.71372487416009, 419.0, 464.0)

Seems to give me a rather standard size well within the viewbox.

tatarize commented 2 years ago

Though admittedly:

list(svgelements.SVG().parse("raspberry-pi-4-599381.svg").elements())[0][1].bbox()

(124.8264592843179, 63.616709374717644, 558.6527, 618.6512)

I think the issue is that the width and height are in pt. (Points.)

1pt ≅ 1.3333px or user units (1px = 0.75pt)

This converts the size from the 512 pt to px. Which is why the values are at 4/3rds the expected size.

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="512.000000pt" height="512.000000pt" viewBox="0 0 512.000000 512.000000"
 preserveAspectRatio="xMidYMid meet">

My conclusion is that this looks weird but is actually correct. The matrix and the viewbox being in px and width being in pt is what accounts for the difference there.

See: https://oreillymedia.github.io/Using_SVG/guide/units.html

tatarize commented 2 years ago

It seems weird but width="512.000000pt" height="512.000000pt" viewBox="0 0 512.000000 512.000000" is an automatic scale(1.3333,1.3333) appended as a transform. As 1pt == 1.3333px then 512pt == 682.6496px. The viewbox being 512 pixels wide means everything is being viewed at scale, so the scale gets appended and shapes are made bigger accordingly.

Esser50K commented 2 years ago

that really is weird 🤔 but I also suspected it was because of the pt I managed to work around it by checking if the bbox is greater than the viewbox and calculate how to scale it. Since it's not a bug I'll close this issue

tatarize commented 2 years ago

If you're just getting those values from the potrace you could just get rid of the transformations. Since potrace will just apply the modifiers itself a bit.

list(svgelements.SVG().parse("raspberry-pi-4-599381.svg", reify=False).elements())[0][1][0].d(transformed=False)

'M 3250,4634 c -25,-8 -67,-14 -95,-14 c -32,0 -62,-7 -83,-20 ...

It depends a bit on what you want. Also during the parsing it'll give you the viewbox and you can use that to check if something multiplied it weirdly. If there's some specific info you want extracted I'd be more than happy to make sure svgelements either can already do that or would do that shortly.

Also, if you want I could add some options to the pure python potrace port I wrote if something like that would make things considerably easier.

https://github.com/tatarize/potrace

I had a rough time getting the ported in C version to work really well, so I spent a few days porting it and getting permissions etc.