cairo rsvg and python in windows

librsvg is currenlty part of gnome-python and therefore is not installed with pycairo on Windows. However, 'librsvg.dll' is ported to windows and may be used directly within a pycairo application. This short code snippet recreates the very basics of gnome-python's rsvg module in windows.

An extremely minimal wrapper for rsvg that can be used in Windows as a drop-in replacement:

#some code to give rsvg.render_cairo(ctx) ability
#on windows.
import os
    import rsvg
except ImportError:
    print"Warning, could not import 'rsvg'"
    if == 'nt':
        print "Detected windows, creating rsvg."
        #some workarounds for windows

        from ctypes import *


        class rsvgHandle():
            class RsvgDimensionData(Structure):
                _fields_ = [("width", c_int),
                            ("height", c_int),

            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()
                return (svgDim.width,svgDim.height)

            def render_cairo(self, ctx):
                z = self.PycairoContext.from_address(id(ctx))
                l.rsvg_handle_render_cairo(self.handle, z.ctx)

        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 = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
ctx = cairo.Context(s)