[Cairo] another set of Python bindings

James Henstridge james at daa.com.au
Thu Sep 11 07:23:43 PDT 2003

I have put together some prototype Python bindings for Cairo.  These are 
not based on Maarten Breddels' bindings, which I didn't know about when 
I started mine.

I put up a snapshot of the source code at:

It uses auto* to build, and should be built with the same prefix as 
Python.  Some interesting points about these bindings:

    * provides an OO interface to Cairo, using Python 2.2 new style classes.
    * treat cairo.Matrix objects as immutable.  The wrappers for things
      like cairo_matrix_invert() return a new object, leaving the
      existing matrix unchanged.
    * cairo_status() is called to check the status of cairo_t
      operations, and raise exceptions as appropriate.
    * Can be used as a base for other Python extensions.  Will be
      important when wrapping other libraries that depend on cairo
    * Includes a simple extension with functions for creating
      cairo.Surface objects from GdkWindows or GdkPixbufs.  This makes
      it possible to use the bindings with PyGTK.

There is a very simple example included that does a little bit of 
drawing using Cairo and PyGTK.  I've put a copy of this example here:

It would be quite useful if Cairo exposed on cairo_t and 
cairo_surface_t.  At the moment, the Python wrappers in my binding own 
the context or surface, and destroy them when the wrapper gets destroyed.

If GTK were to support Cairo, it is quite likely that there would be 
functions that return a cairo_t or cairo_surface_t, while not 
transfering ownership.  In this case, it would be bad if the wrapper 
destroyed the context or surface.  I could add a "don't free this object 
on destruction", but then we might run into a similar problem where the 
object context or surface gets destroyed unexpectedly.

A reference count would be the most robust way to handle this, and 
shouldn't be difficult to implement.


James Henstridge.

Email: james at daa.com.au
WWW:   http://www.daa.com.au/~james/

More information about the cairo mailing list