[cairo-commit] [cairo-www] src/cookbook
Carl Worth
cworth at freedesktop.org
Mon Dec 8 16:16:04 PST 2008
src/cookbook/librsvgpython.mdwn | 67 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+)
New commits:
commit 00d4b492acf6ce84f44cbe83e7e971ae1262a8af
Author: Carl Worth <cworth at freedesktop.org>
Date: Mon Dec 8 16:16:04 2008 -0800
web commit by siefkenj
diff --git a/src/cookbook/librsvgpython.mdwn b/src/cookbook/librsvgpython.mdwn
index cfb5a12..51a4936 100644
--- a/src/cookbook/librsvgpython.mdwn
+++ b/src/cookbook/librsvgpython.mdwn
@@ -85,3 +85,70 @@ Normally it's easier to use librsvg Python bindings from PyGTK, so you can impor
renderSVG(ctx,'ball.svg',0,0)
surface.write_to_png("test.png")
+
+
+An extremely minimal wrapper for rsvg that can be used in windows as a drop-in replacement for rsvg might go as follows:
+
+ #some code to give rsvg.render_cairo(ctx) ability
+ #on windows.
+ import os
+ try:
+ import rsvg
+ WINDOWS=False
+ except ImportError:
+ print"Warning, could not import 'rsvg'"
+ if os.name == 'nt':
+ print "Detected windows, creating rsvg."
+ #some workarounds for windows
+
+ from ctypes import *
+
+ l=CDLL('librsvg-2-2.dll')
+ g=CDLL('libgobject-2.0-0.dll')
+ g.g_type_init()
+
+ 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()
+
+This wrapper may be used to render svg's with the same syntax as rsvg. i.e.:
+
+ h = rsvg.Handle("box.svg")
+ s = cario.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
+ ctx = cairo.Context(s)
+ h.render_cairo(ctx)
+
+
More information about the cairo-commit
mailing list