deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
307 stars 79 forks source link

Implementation error in rendering of paths with shorthand command for quadratic Bézier curves #364

Closed stefanw closed 1 year ago

stefanw commented 1 year ago

I noticed weird SVG curves coming out of the internal conversion from quadratic Bézier curves to cubic ones.

Here's an example of what the mistake can looks like in practice.

I tracked this down to this code in convertPath. The definition of the t/T commands is:

Draws a quadratic Bézier curve from the current point to (x,y). The control point is assumed to be the reflection of the control point on the previous command relative to the current point. (If there is no previous command or if the previous command was not a Q, q, T or t, assume the control point is coincident with the current point.)

So where does the control point from the previous command come from? svglib takes the previously stored last two points and assumes the first one to be the control point of the previous quadratic Bézier curve. However, the point is actually the second control point of a cubic Bézier curve (because a previous Q/q command has been converted to cubic).

The solution would be to store the control point of the last quadratic Bézier curve in a variable and use that when the next t/T command comes along. I will prepare a PR for this.

stefanw commented 1 year ago

@claudep Happy new year and thanks for the merge. Would you consider a patch release for this?

claudep commented 1 year ago

Happy new year, and happy new release :smile:

https://pypi.org/project/svglib/

deeplook commented 1 year ago

Thanks, and Happy New Year! I'm about to post the first bug of the year... ;)

stefanw commented 1 year ago

Thanks! Embarrassingly, I made a small mistake in the fix 😞 . New PR is at #372 this time with a test.

claudep commented 1 year ago

Thanks for the quick fix. I just released 1.5.1.