Cairo with python Ctypes

Uploaded some experiments with python and ctypes to here:


This is a really rough proof of concept that the pycairo API can be implemented with ctypes + metclasses.



So far only ImageSurface is supported on the SVG backend, along with Contexts.   The nice thing about this is that you can use a pycairo like API on pypy, where things should be faster (in theory).


There’s a test that can be run to show the outputs the same for pycairo and cairo ctypes.

8 thoughts on “Cairo with python Ctypes

  1. Hi Maxim,
    Your library looks useful, it’s slightly different to this one at the moment – yours provides the cairo C.

    What I’m interested in is providing the pycairo API where you get classes for things like Context, Surface, ImageSurface etc.

    This is achieved by importing a ctypes style binding then using metaclasses to bind the functions to classes using their names (e.g. a prefix of cairo_image_surface_ goes to ImageSurface).

    I could save a lot of the nastier code by just building on your library.

    I haven’t worked on this for a little while though so feel free to take the ideas/code. A pycairo compatible library might be useful for running pycairo style code on pypy for instance.

  2. Thank you for your reply.
    I understand about classes, see my pyIrrlicht project, but should be noted that, Cairo purely functional (cairo-ctypes auto-generated, respectively), and object approach requires more time to develop and direct (individual) developer, but my main project pyIrrlicht.

    I also use Cairo for rendering SVG in irrlicht_c.dll and I’m very important analogue of Cairo in Python, for study and testing.

    Thanks for the tips on googlecode, I made some corrections.

  3. Hi Stuart.
    Thanks for bug reports and patches again, I will work under these.
    Consider load Cairo library – I made some changes after ctypesgen was generated, first change is on 595 line of, now it have _CAIRO_LIBRARY_ variable and may be used in users py file
    (or as first line of code before import cairo “import os; os.environ['_CAIRO_LIBRARY_'] = ‘full_path_name_to_custom_cairo_library”.

    Sorry for my English.

  4. Hi Maxim,
    Your Enlish is fine :)
    I’ll have a look at the changes when I get a chance. The issue I was having on 64 bit ubuntu seems in ctypesgen svn.
    Can you try regenerating with svn ctypesgen, that should be the same as the when I generate it in ubuntu and ‘just work’.

    The subversion is a bit simpler: cairo.h > seems to be enough to get working bindings.

    It’s probably an idea to not modify, so that it can be regenerated without breaking anything.

  5. I still need to try on ctypesgen in windows vs linux to see if thats the reason for the difference in ctypesgen.

    Do you which .h files, it might be possible to just pass those to ctypesgen.

    I noticed that M_PI is included from math.h an used in the tests, I’ve successfully replaced it with math.pi in testing.

    The ctypesgen loader doesn’t seem to understand ubuntu style files containing wildcards, so this is what I’ve been looking at, hopefully can get this working and upstream next.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>