我怎样才能“最适合”?任意 cairo (pycairo) 路径?
似乎鉴于 lines_extents()
和 translate(x, y)
和 scale(x, y)
函数中的信息,我应该能够采用任意 cairo (我使用 pycairo)路径并“最适合”它。换句话说,将其居中并展开以填充可用空间。
在绘制路径之前,我已经缩放了画布,使原点为左下角,向上为 y+,向右为 x+,高度和宽度均为 1。考虑到这些条件,这段代码似乎 正确缩放路径:
# 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)
尽管我一生都无法弄清楚翻译。有没有更简单的方法?如何在画布上“最适合”开罗路径?
如果此问题有任何不清楚的地方,请要求澄清。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个我喜欢的解决方案(至少对于我的目的而言)。只需创建一个新表面并将旧表面绘制到新表面上即可。
I have found a solution that I like (at least for my purposes). Just create a new surface and paint the old surface on to the new one.
仅就比例而言,我做了类似的事情,以“最适合”的方法调整框内的图像。至于比例,代码如下:
图像被绘制为与原点对齐(在我的例子中为左上角),因此也许应该执行类似的操作来平移路径。
此外,这种最佳拟合旨在保持纵横比。如果要拉伸图形,请使用每个比率而不是“比例”变量。
希望我有帮助
As for the scale only, I have done a similar thing to adjust an image inside a box with a "best-fit" approach. As about scale, here is the code:
The image gets drawn aligned to the origin (top left in my case), so perhaps a similar thing should be done to translate the path.
Also, this best fit is intended to preserve aspect ratio. If you want to stretch the figure, use each of the ratios instead of the 'scale' variable.
Hope I have helped