Getting started with Cairo

Using ghci is a great way of experimenting with Cairo. You might experience problems due to the way ghci and Gtk2Hs both require to be run concurrently. I’ve created a little code snippet that opens a dialog box, displays the given Cairo action and waits for you to click the Close button.

The run function will execute the given Cairo statements and will then continue to run the Gtk main loop until the dialog is actually removed from screen. This function can conveniently be used to test various Cairo commands. For example, try:

run (moveTo 200 200 >> lineTo 0 0 >> stroke)

If straight lines are too boring, you can do this:

run (moveTo 200 200 >> setLineWidth 20 >> curveTo 200 0 100 200 0 0 >> stroke)

To render text, you need to create a layout first:

run (moveTo 20 20 >> rotate (pi/4) >> createLayout "Haskell and Cairo is cool." >>= showLayout)

Note that the return value of createLayout is passed to showLayout.

14 Responses to “Getting started with Cairo”

  1. Cale Gibbard Says:

    Very cool for such a simple thing. :) I was actually thinking of writing something like this just the last couple days. I think it should be extended somewhat and added to the library. Perhaps some stuff to handle animation as easily would be nice (hmm… Time -> Render () ?).

  2. Administrator Says:

    Animation!? Well, that’s a bit trickier, since Cairo is just for drawing and Gtk more for user interfaces. So if you want to animate something, it would be part of the user interface, with adding a timer that changes the content of a widget and all that. It might be easiest to render several PNGs with Cairo and them to build an animated Pixbuf which can be put into an image widget. Alas, we haven’t done the animation functions in Pixbuf yet. Tell us if you want them!

  3. Duncan Says:

    Cale, take a look at the cairo clock demo. It animates!

  4. Leon Says:

    I’ve tried to use this introductory code under windows. I can compile and run demos, but when I try to load a demo in GHCi, I get the following error:

    can’t load .so/.DLL for: glib-2.0 (addDLL: unknown error)

    I have installed:


  5. Duncan Says:

    Hi Leon, this is a known problem. It’s mentioned in the notes about installing on Windows. For the gory details see this email.

    In fact, thanks for reminding me about this because GHC 6.4.2 is now out and includes a feature I added to address exactly this problem. So expect the next release of Gtk2Hs for Windows to have this one fixed! :-)

  6. Hans van Thiel Says:

    How do you compile this with GHC? I’ve tried several variations of main (which GHC wants) but can’t get it right. Thanks,


  7. Duncan Says:

    Hans, it’s designed to use in GHCi, so it doesn’t have a main function. So just download the file and say:
    ghci CairoGhci.hs

  8. Hans van Thiel Says:

    Oops, sorry for being unclear. It runs into the same problem with threads as Gtk2Hs with GHCi version 6.6 . Trying to run the example results in an error message that suggests starting GHCi with a no-threaded flag, but according to the GHC 6.6 documentation there is no such flag. So, I’d like to transform the example so I can compile it, which would sidestep the issue? Alternatively, maybe I should just downgrade to GHC 6.4, which will probably work as expected.


  9. Duncan Says:

    No need to dowgrade to GHC 6.4, the issue with GHCi and threads is fixed in the latest release candidate.

  10. Adam Says:

    I get the following error when using this in GHCi on Mac OS X.
    (ghc 6.6.1, gtk2hs 0.9.11 built with Mac Ports)

    Loading package gtk-0.9.11 … can’t load .so/.DLL for: gthread-2.0 (dlopen(libgthread-2.0.dylib, 10): image not found)

    Any ideas?

  11. Duncan Says:

    Hi Adam. This sounds like a problem with the port file, try taking this up with the maintainer of the gtk2hs or glib package in Mac Ports. It looks like you are missing the gthread component of glib, or it is somehow mis-configured.

  12. pepe Says:

    Hi Duncan & Adam

    I also have the libgthread problem here. Os X Intel with a hand built version of Gtk2hs and a hand built GHC 6.7. GTK installed via MacPorts

    The file libgthread-2.0.dylib is found at /opt/local/lib, and I launched ghci with “-L/opt/local/lib” to make sure it can find it, but still the same.

  13. Axel Says:

    Hi Pepe,

    yes, I can reproduce this. The reason seems to be that we don’t include /opt/local/lib in the package file. Fixing that by hand, I now get a segmentation fault after calling “main” in ghci. GDB says:

    linking … done.

    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_INVALID_ADDRESS at address: 0×83f87795
    [Switching to process 16916 thread 0x1f03]
    0×90002fe0 in strcmp ()
    (gdb) bt
    #0 0×90002fe0 in strcmp ()
    #1 0×00fa5aa1 in FcPatternPosition ()
    Cannot access memory at address 0×3

    I don’t know if we’re at fault here or ghc. The program I tested this with compiles and runs with no problems (even without adding the /opt/local/lib path to the package.conf file). The function names suggest that some arguments passed to Gtk are incorrect. Any thoughts, Duncan?

  14. pepe Says:

    Hi alex

    This is great. Adding /opt/local/lib by hand to package.conf did it. I didn’t see the problems that you report, perhaps you didn’t launch ghci with “-L/opt/local/lib”?

    Don’t forget to include this change in the next release!