Cairo graphics tutorial
Introduction
This is Cairo graphics tutorial. The tutorial will teach you the basics of graphics programming in Cairo with the C programming language. This tutorial is for beginners and intermediate developers.
Cairo
Cairo is a library for creating 2D vector graphics. It is written in the C programming language. There are bindings for other computer languages. Python, Perl, C++, C# or Java. Cairo is a multiplatform library. It works on Linux, BSDs, OSX.
Cairo supports various backends.
- X Window System
- Win32 GDI
- Mac OS X Quartz
- PNG
- PostScript
- SVG
This means that we can use the library to draw on windows on Linux/BSDs, Windows, Mac OS and we can use the library to create PNG images, PDF files, PostScript files, and SVG files.
We can compare the cairo library to the GDI+ library on Windows OS and the Quartz 2D on Mac OS. Cairo is an open source software library. From version 2.8, the cairo library is part of the GTK+ system.
The Cairo graphics library
Welcome to the Cairo graphics tutorial. This tutorial will teach you basics and some advanced topics of the Cairo 2D vector drawing library. In most examples we will use the GTK+ programming library. This tutorial is done in C programming language.
2D Vector Graphics
There are two different computer graphics. Vector and raster graphics. Raster graphics represents images as a collection of pixels. Vector graphics is the use of geometrical primitives such as points, lines, curves or polygons to represent images. These primitives are created using mathematical equations.
Both types of computer graphics have advantages and disadvantages. The advantages of vector graphics over raster are:
- smaller size
- ability to zoom indefinitely
- moving, scaling, filling or rotating does not degrade the quality of an image
Compiling examples
The examples are created in the C programming language. We use the GNU C compiler to compile them.
gcc example.c -o example `pkg-config --cflags --libs gtk+-3.0`
Note that the order of compiling options is important.
Cairo definitions
In this part of the Cairo graphics tutorial, we will provide some useful definitions for the Cairo graphics library. This will help us better understand the Cairo drawing model.
Context
Drawing in Cairo is done via the Context. The Cairo context holds all of the graphics state parameters that describe how drawing is to be done. This includes information such as line width, colour, the surface to draw to, and many other things. This allows the actual drawing functions to take fewer arguments to simplify the interface.
All drawing with Cairo is always done to a cairo_t object. A Cairo context is tied to a specific surface. A PDF, SVG, PNG, GtkWindow etc.
Path
A path is made up of one or more lines. These lines are connected by two or more anchor points. Paths can be made up of straight lines and curves. There are two kinds of paths. Open and closed paths. In a closed path, starting and ending points meet. In an open path, starting and ending point do not meet.
In Cairo, we start with an empty path. First we define a path and then we make them visible by stroking and filling them. One important note. After each cairo_stroke() or cairo_fill() function calls, the path is emptied. We have to define a new path.
A path is made of subpaths.
Source
The source is the paint we use in drawing. We can compare the source to a pen or ink that we will use to draw the outlines and fill the shapes. There are four kinds of basic sources: colors, gradients, patterns, and images.
Surface
A surface is a destination that we are drawing to. We can render documents using the PDF or PostScript surfaces, directly draw to a platform via the Xlib and Win32 surfaces.
The documentation mentions the following surfaces:
typedef enum _cairo_surface_type {
CAIRO_SURFACE_TYPE_IMAGE,
CAIRO_SURFACE_TYPE_PDF,
CAIRO_SURFACE_TYPE_PS,
CAIRO_SURFACE_TYPE_XLIB,
CAIRO_SURFACE_TYPE_XCB,
CAIRO_SURFACE_TYPE_GLITZ,
CAIRO_SURFACE_TYPE_QUARTZ,
CAIRO_SURFACE_TYPE_WIN32,
CAIRO_SURFACE_TYPE_BEOS,
CAIRO_SURFACE_TYPE_DIRECTFB,
CAIRO_SURFACE_TYPE_SVG,
CAIRO_SURFACE_TYPE_OS2
} cairo_surface_type_t;
Mask
Before the source is applied to the surface, it is filtered first. The mask is used as a filter. The mask determines, where the source is applied and where not. Opaque parts of the mask allow to copy the source. Transparent parts do not let to copy the source to the surface.
Pattern
A cairo pattern represents a source when drawing onto a surface. In cairo, a pattern is something that you can read from that is used as the source or mask of a drawing operation. Patterns in cairo can be solid, surface-based or gradients patterns.
In this chapter of the Cairo tutorial, we have given some basic definitions.
Cairo backends
The Cairo library supports various backends. In this section of the Cairo graphics tutorial, we will use Cairo to create a PNG image, PDF file, SVG file and we will draw on a GTK window.
PNG image
In the first example, we will create a PNG image.
#include <cairo.h>
int main(void)
{
cairo_surface_t *surface;
cairo_t *cr;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 390, 60);
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 40.0);
cairo_move_to(cr, 10.0, 50.0);
cairo_show_text(cr, "Disziplin ist Macht.");
cairo_surface_write_to_png(surface, "image.png");
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}