Cairo (graphics)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search
Cairo (graphics)
The cairo graphics library logo.
Original author(s) Keith Packard, Carl Worth[1]
Developer(s) Carl Worth, Behdad Esfahbod
Stable release 1.14.4 (October 28, 2015; 8 years ago (2015-10-28)) [±]
Preview release 1.12.18 (November 7, 2014; 9 years ago (2014-11-07)) [±][2]
Written in C
Operating system Cross-platform
Type Graphics library
License GNU Lesser General Public License version 2.1 (only) or Mozilla Public License 1.1
Website cairographics.org

Cairo (stylized as cairo) is a programming library used to provide a vector graphics-based, device-independent API for software developers. It is designed to provide primitives for 2-dimensional drawing across a number of different backends. Cairo is designed to use hardware acceleration[3] when available.

There is a formal proposal on standardizing C++ 2D graphic API based on a mechanical transformation of Cairo.[4]

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++, Delphi, Factor, Haskell, Lua, Perl, PHP, Python, Ruby, Scheme, Smalltalk and several others.[5]

Toolkit bindings

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

  • FLTK has full cairo support (through "--enable-cairo" compile switch).
  • GNUstep currently uses cairo as its primary backend on non-Windows systems.[6]
  • GTK+ has not only full support for cairo, but starting in 2005 with version 2.8, uses cairo to render the majority of its graphical control elements, and since 3.0 all rendering is done through cairo.
  • The cairo development team maintains up-to-date instructions for rendering surfaces to SDL.[7]

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 (directly[8] 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,[9] Qt,[10] Skia,[11] and Microsoft's Direct2D.[12]

Drawing model

The Cairo drawing model

The Cairo drawing model is somewhat unorthodox[why?] and 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.[further explanation needed]

Example

SVG-picture generated by this example

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_t *cr;
    cairo_surface_t *surface;
    cairo_pattern_t *pattern;
    int x,y;

    surface = 
      (cairo_surface_t *)cairo_svg_surface_create("Cairo_example.svg", 100.0, 100.0);
    cr = cairo_create(surface);

    /* Draw the squares in the background */
    for (x=0; x<10; x++)
       for (y=0; y<10; y++)
           cairo_rectangle(cr, x*10.0, y*10.0, 5, 5);

    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, "Hello");

    cairo_move_to(cr, 10, 75);
    cairo_show_text(cr, "Wikipedia!");

    cairo_destroy (cr);
    cairo_surface_destroy (surface);
    return 0;
}

Notable usage

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

  • GTK+, starting in 2005 with version 2.8, uses Cairo to render the majority of its graphical control elements.[13] Since GTK+ version 3, all the rendering is done using Cairo.
  • The Mono Project,[14] including Moonlight,[15] has been using cairo since very early in conception to power the backends of its GDI+ (libgdiplus) and System.Drawing namespaces.
  • The Mozilla project has made use of cairo in recent versions of its Gecko layout engine, used for rendering the graphical output of Mozilla products. Gecko 1.8, the layout engine for Mozilla Firefox 2.0 and SeaMonkey 1.0, used cairo to render SVG and <canvas> content. Gecko 1.9,[16] the release of Gecko that serves as the basis of Firefox 3, uses cairo as the graphics backend for rendering both web page content and the user interface (or "chrome").
  • The WebKit framework uses cairo for all rendering in the GTK+ and EFL ports. Support has also been added for SVG and <canvas> content using cairo.
  • The Poppler library uses cairo to render PDF documents. Cairo enables the drawing of antialiased vector graphics and transparent objects.
  • The vector graphics application Inkscape uses the cairo library for its outline mode display, as well as for PDF and PostScript export since release 0.46.[17]
  • MorphOS 2.5 features a shared library implementation of cairo, which was available as stand-alone release for earlier MorphOS versions.
  • AmigaOS 4.1 supports a shared object library of cairo (libcairo.so) in its default installation.
  • FontForge enabled cairo by default for rendering in mid-October 2008.
  • R can output plots in PDF, PostScript and SVG formats using cairo if available.
  • Gnuplot 4.4 now uses cairo for rendering PDF and PNG output.[18]
  • Internet Browser for PlayStation 3 uses cairo since system software update 4.10.
  • Synfig 0.64 now supports optional cairo rendering
  • On-demand graphing of time series data in Graphite

History

Keith Packard and Carl Worth founded the cairo project for use in the X Window System.[19] 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.[20] The name cairo derives from the original name Xr, interpreted as the Greek letters chi and rho.[21]

See also

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. Lua error in package.lua at line 80: module 'strict' not found.
  13. Lua error in package.lua at line 80: module 'strict' not found.
  14. Lua error in package.lua at line 80: module 'strict' not found.
  15. Lua error in package.lua at line 80: module 'strict' not found.
  16. Lua error in package.lua at line 80: module 'strict' not found.
  17. Lua error in package.lua at line 80: module 'strict' not found.
  18. Lua error in package.lua at line 80: module 'strict' not found.
  19. Lua error in package.lua at line 80: module 'strict' not found.
  20. Lua error in package.lua at line 80: module 'strict' not found.
  21. Lua error in package.lua at line 80: module 'strict' not found.

External links

  • Official website
  • cairocffi on GitHub - CFFI-based cairo bindings for Python.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.