Binding to Cairo, a 2D Vector Graphics Library.
OCaml C Other
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Travis Build Status AppVeyor Build status

OCaml interface to Cairo

This is an OCaml binding for the Cairo library, a 2D graphics library with support for multiple output devices.

You can read the API of Cairo and Cairo_gtk online.


You need the development files of Cairo (e.g. on Debian, you must install the package libcairo2-dev, on Mageia libcairo-devel, and on Fedora cairo-devel) and the OCaml package lablgtk2 (in the OPAM package lablgtk).

Compilation & Installation

The easier way to install this library — once the prerequisites are set up — is to use opam:

opam install cairo2

If you would like to compile from the sources, install OASIS:

opam install oasis

and do:

oasis setup
ocaml -configure
ocaml -build

You can then install it with:

ocaml -install


You can read a version of the Cairo tutorial using this module. The code of this tutorial is available in the examples/ directory. To compile it, you must configure the project with

ocaml -configure --enable-examples

All the examples below are available (with some comments) by clicking on images in the tutorial.

Basic examples

  • shows how to draw (stroke) a simple rectangle on a PNG surface.

  • shows how to fill a simple rectangle on a PNG surface.

  • illustrates how to select a font and draw some text on a PNG surface.

  • shows how to paint the current source everywhere within the current clip region.

  • shows how to apply a radial transparency mask on top of a linear gradient.

  • produces

    Source RGBA

  • shows how to use radial and linear patterns. It generates:


  • shows how to draw a closed path. It produces the PNG:

    close path

  • displays graphically the various dimensions one can request about text. It generates the PNG:


  • exemplifies drawing consecutive UTF-8 strings in a PDF file. Some helping lines are also added to show the text extents.

  • shows the action of dilation on the line width and how to properly draw ellipses. It generates the PNG:


  • illustrates the wrong way of centering characters based on their individual extents:


    Instead, one should combine them with the font extents as shown in to have:


Examples generating the images of the tutorial