Cairo (graphics)


Cairo is an open-source graphics library that provides a vector graphics-based, device-independent API for software developers. It provides primitives for two-dimensional drawing across a number of different backends. Cairo uses hardware acceleration when available.

Software architecture

Language bindings

A library written in one programming language may be used in another language if bindings are written; Cairo has a range of bindings for various languages including C++, C# and other CLI languages, Delphi, Eiffel, Fortran, Factor, Harbour, Haskell, Julia, Lua, Perl, PHP, Python, Ruby, Rust, Scheme, Smalltalk and several others like Gambas.

Toolkit bindings

Since Cairo is only a drawing library, it can be quite useful to integrate it with a graphical user interface toolkit.

Available back-ends

Cairo supports output to a number of different back-ends, known as "surfaces" in its code. Back-ends support includes output to the X Window System, via both Xlib and XCB, Win32 GDI, OS X Quartz Compositor, the BeOS API, OS/2, OpenGL contexts, local image buffers, PNG files, PDF, PostScript, DirectFB and SVG files.
There are other back-ends in development targeting the graphics APIs OpenVG, Qt, Skia, and Microsoft's Direct2D. The BeOS, OS/2, DirectFB and OpenGL backends were dropped in 2022.

Drawing model

The Cairo drawing model relies on a three-layer model.
Any drawing process takes place in three steps:
  1. First a mask is created, which includes one or more vector primitives or forms, i.e., circles, squares, TrueType fonts, Bézier curves, etc.
  2. Then source must be defined, which may be a color, a color gradient, a bitmap or some vector graphics, and from the painted parts of this source a die cut is made with the help of the above defined mask.
  3. Finally the result is transferred to the destination or surface, which is provided by the back-end for the output.
This constitutes a fundamentally different approach from Scalable Vector Graphics, which specifies the color of shapes with Cascading Style Sheets rules. Whereas Cairo would create a mask of a shape, then make a source for it, and then transfer them onto the surface, an SVG file would simply specify the shape with a style attribute. That said, the models are not incompatible; many SVG renderers use Cairo for heavy lifting.

Example

Quite complex "Hello world" graphics can be drawn with the help of Cairo with only a few lines of source code:

  1. include
  2. include
int main

Notable usage

Cairo is popular in the open source community for providing cross-platform support for advanced 2D drawing.

History

Keith Packard and Carl Worth founded the Cairo project for use in the X Window System. It was originally called Xr or Xr/Xc. The name was changed to emphasize the idea of a cross-platform library to access display server, not tied to the X Window System.
The name Cairo derives from the original name Xr, interpreted as the Greek letters chi and rho.

Complex text layout

Cairo handles Latin and CJK based fonts, but does not directly support complex text layout fonts, which require shaping the glyphs. The Cairo developers recommend using Pango, which provides complex text layout and can integrate with Cairo.