我有一个简单的围绕 rsvg 的 python ctypes 包装器,可与 cairo 一起使用。它似乎可以工作,ubuntu 16
但是当我尝试它时,ubuntu 18
我得到了一个分段错误。我查看了 cairo 文档以了解如何做到这一点,所以没什么特别的。(https://www.cairographics.org/cookbook/librsvgpython/)。
from ctypes import CDLL, Structure, byref, c_byte, c_double, c_int, c_void_p
# CDLL: Instances of this class represent loaded shared libraries.
l = CDLL('librsvg-2.so.2')
class rsvgHandle():
class RsvgDimensionData(Structure):
_fields_ = [("width", c_int),
("height", c_int),
("em", c_double),
("ex", c_double)]
class PycairoContext(Structure):
_fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
("ctx", c_void_p),
("base", c_void_p)]
def __init__(self, path):
self.path = path
error = ''
self.handle = l.rsvg_handle_new_from_file(self.path, error)
def get_dimension_data(self):
svgDim = self.RsvgDimensionData()
l.rsvg_handle_get_dimensions(self.handle, byref(svgDim))
return (svgDim.width, svgDim.height)
def render_cairo(self, ctx):
ctx.save()
z = self.PycairoContext.from_address(id(ctx))
l.rsvg_handle_render_cairo(self.handle, z.ctx)
ctx.restore()
class rsvgClass():
def Handle(self, file):
return rsvgHandle(file)
rsvg = rsvgClass()
svg_handle = rsvg.Handle(fpath)
svg_width, svg_height = svg_handle.get_dimension_data()[:2]
svg_handle.render_cairo(context)
错误发生get_dimension_data
在行内l.rsvg_handle_get_dimensions(self.handle, byref(svgDim))
跟踪不是很有帮助,它说分段错误和一堆这些
/usr/lib/x86_64-linux-gnu/librsvg-2.so.2(rsvg_handle_get_dimensions+0) [0x7ff70b01ed50]
/usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call_unix64+0x4c) [0x7ff71f52adae]
/usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call+0x22f) [0x7ff71f52a71f]
/usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(_ctypes_callproc+0x2a4) [0x7ff71f73dcc4]
/usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so(+0x106c5) [0x7ff71f73d6c5] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+ 0x5bf6) [0x7ff7247eb366] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x8b5b) [0x7ff7247ee2cb] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0( PyEval_EvalCodeEx+0x7d8) [0x7ff72492a908] /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x5bf6) [0x7ff7247eb366]
有谁知道我的ubuntu 18
环境中可能发生什么导致这个问题?或者,我如何在 ubuntu 18 上将 svg 渲染到开放的 cairo 上下文中(如代码的最后一行所示)?(顺便说一句,rsvg 的现成 python 绑定已从 ubuntu 18 中删除,这就是我尝试 ctypes 开始的原因)