Original author(s) | Keith Packard, Carl Worth[1] |
---|---|
Developer(s) | Carl Worth, Behdad Esfahbod |
Initial release | Before 2003[2] |
Stable release | 1.16.0 (October 19, 2018[3]) [±] |
Repository | gitlab |
Written in | C |
Type | Graphics library |
License | GNU Lesser General Public License version 2.1 (only) or Mozilla Public License 1.1 |
Website | www |
Cairo (stylized as 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 back ends. Cairo uses hardware acceleration[4] when available.
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 (Visual Basic like).[5]
Since Cairo is only a drawing library, it can be quite useful to integrate it with a graphical user interface toolkit.
--enable-cairo
compile switch).Cairo supports output (including rasterisation) 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 (directly[7] and via glitz), local image buffers, PNG files, PDF, PostScript, DirectFB and SVG files.
There are other back-ends in development targeting the graphics APIs OpenVG,[8] Qt,[9] Skia,[10] and Microsoft's Direct2D.[11] The BeOS, OS/2 and DirectFB backends were dropped in 2022.[12]
The Cairo drawing model relies on a three-layer model.
Any drawing process takes place in three steps:
This constitutes a fundamentally different approach from Scalable Vector Graphics (SVG), which specifies the color of shapes with Cascading Style Sheets (CSS) rules.[citation needed] 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.[13]
Quite complex "Hello world" graphics can be drawn with the help of Cairo with only a few lines of source code:
#include <cairo-svg.h>
#include <stdio.h>
int main(int argc, char **argv) {
cairo_surface_t *surface = cairo_svg_surface_create("Cairo_example.svg", 100.0, 100.0);
cairo_t *cr = cairo_create(surface);
/* Draw the squares in the background */
for (int x = 0; x < 10; ++x)
for (int y = 0; y < 10; ++y)
cairo_rectangle(cr, x * 10.0, y * 10.0, 5, 5);
cairo_pattern_t *pattern = cairo_pattern_create_radial(50, 50, 5, 50, 50, 50);
cairo_pattern_add_color_stop_rgb(pattern, 0, 0.75, 0.15, 0.99);
cairo_pattern_add_color_stop_rgb(pattern, 0.9, 1, 1, 1);
cairo_set_source(cr, pattern);
cairo_fill(cr);
/* Writing in the foreground */
cairo_set_font_size (cr, 15);
cairo_select_font_face (cr, "Georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_move_to(cr, 10, 25);
cairo_show_text(cr, "Hallo");
cairo_move_to(cr, 10, 75);
cairo_show_text(cr, "Wikipedia!");
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
Cairo is popular in the open source community for providing cross-platform support for advanced 2D drawing.
Keith Packard and Carl Worth founded the Cairo project for use in the X Window System.[2] It was originally (until at least 2003) 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.[22] The name Cairo derives from the original name Xr, interpreted as the Greek letters chi and rho.[23]
Cairo handles Latin and CJK based fonts, but does not support complex text layout fonts, which require shaping the glyphs.[24]