How can I "best fit" an arbitrary cairo (pycairo) path?
Posted
by Daniel Straight
on Stack Overflow
See other posts from Stack Overflow
or by Daniel Straight
Published on 2010-02-18T21:23:31Z
Indexed on
2010/05/28
3:21 UTC
Read the original article
Hit count: 374
It seems like given the information in stroke_extents()
and the translate(x, y)
and scale(x, y)
functions, I should be able to take any arbitrary cairo (I'm using pycairo) path and "best fit" it. In other words, center it and expand it to fill the available space.
Before drawing the path, I have scaled the canvas such that the origin is the lower left corner, up is y+, right is x+, and the height and width are both 1. Given these conditions, this code seems to correctly scale the path:
# cr is the canvas
extents = cr.stroke_extents()
x_size = abs(extents[0]) + abs(extents[2])
y_size = abs(extents[1]) + abs(extents[3])
cr.scale(1.0 / x_size, 1.0 / y_size)
I cannot for the life of me figure out the translating though. Is there a simpler approach? How can I "best fit" a cairo path on its canvas?
Please ask for clarification if anything is unclear in this question.
© Stack Overflow or respective owner