Cairo (graphics)

From Wikipedia, the free encyclopedia
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.12.16 (August 26, 2013; 10 months ago (2013-08-26)) [±]
Preview release 1.12.16 (August 26, 2013; 10 months ago (2013-08-26)) [±][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 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.

Cairo is free and open-source software subject to the terms of dual licensing, under the GNU Lesser General Public License (LGPL) and the Mozilla Public License (MPL).

Cairo is written in C.

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

Software architecture[edit]

Language bindings[edit]

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

Toolkit bindings[edit]

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

Drawing model[edit]

The Cairo drawing model

The Cairo drawing model is somewhat unorthodox 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 SVG vector graphics.

Example[edit]

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:

  1. #include <cairo-svg.h>
    
  2. #include <stdio.h>
    
  3.  
    
  4. int main(int argc, char **argv) {
    
  5.     cairo_t *cr;
    
  6.     cairo_surface_t *surface;
    
  7.     cairo_pattern_t *pattern;
    
  8.     cairo_text_extents_t text;
    
  9.     int x,y;
    
  10.  
    
  11.     surface = 
    
  12.       (cairo_surface_t *)cairo_svg_surface_create("Cairo_example.svg", 100.0, 100.0);
    
  13.     cr = cairo_create(surface);
    
  14.  
    
  15.     /* Draw the squares in the background */
    
  16.     for (x=0; x<10; x++)
    
  17.        for (y=0; y<10; y++)
    
  18.            cairo_rectangle(cr, x*10.0, y*10.0, 5, 5);
    
  19.  
    
  20.     pattern = cairo_pattern_create_radial(50, 50, 5, 50, 50, 50);
    
  21.     cairo_pattern_add_color_stop_rgb(pattern, 0, 0.75, 0.15, 0.99);
    
  22.     cairo_pattern_add_color_stop_rgb(pattern, 0.9, 1, 1, 1);
    
  23.  
    
  24.     cairo_set_source(cr, pattern);
    
  25.     cairo_fill(cr);
    
  26.  
    
  27.     /* Writing in the foreground */
    
  28.     cairo_set_font_size (cr, 15);
    
  29.     cairo_select_font_face (cr, "Georgia",
    
  30.         CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    
  31.     cairo_set_source_rgb (cr, 0, 0, 0);
    
  32.  
    
  33.     cairo_move_to(cr, 10, 25);
    
  34.     cairo_show_text(cr, "Hallo");
    
  35.  
    
  36.     cairo_move_to(cr, 10, 75);
    
  37.     cairo_show_text(cr, "Wikipedia!");
    
  38.  
    
  39.     cairo_destroy (cr);
    
  40.     cairo_surface_destroy (surface);
    
  41.     return 0;
    
  42. }
    

Available back-ends[edit]

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, Mac OS X Quartz, the BeOS API, OS/2, OpenGL contexts (directly[6] 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,[7] Qt,[8] Skia,[9] and Microsoft's Direct2D.[10]

Notable usage[edit]

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.[11] Since GTK+ version 3, all the rendering is done using Cairo.
  • The Mono Project,[12] including Moonlight,[13] 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,[14] 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.[15]
  • 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.[16]
  • Internet Browser for PlayStation 3 uses cairo since system software update 4.10.

History[edit]

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

See also[edit]

References[edit]

  1. ^ http://cworth.org/~cworth/[dead link]
  2. ^ Wilson, Chris (26 Feb 2013). "cairo 1.12.16 snapshot available". Retrieved 2013-10-01. 
  3. ^ "Cairo homepage". Retrieved 2010-10-30. 
  4. ^ http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3888.pdf.  Missing or empty |title= (help)
  5. ^ "Cairo Language Bindings". Retrieved 2014-04-16. 
  6. ^ Chris Wilson (2009-07-22). "New OpenGL backend merged". Retrieved 2010-02-12. 
  7. ^ Øyvind Kolås (2008-01-24). "Announcing OpenVG backend". Retrieved 2010-02-12. 
  8. ^ Vladimir Vukićević (2008-05-06). "Well Isn’t That Qt". Retrieved 2010-02-12. 
  9. ^ Chris Wilson (2009-08-31). "Cool Stuff". Retrieved 2010-02-12. 
  10. ^ Bas Schouten (2009-11-22). "Direct2D: Hardware Rendering a Browser". Retrieved 2010-02-12. 
  11. ^ "GTK+ to Use Cairo Vector Engine". Retrieved 2009-12-27. 
  12. ^ "Mono - Drawing". Retrieved 2009-12-27. 
  13. ^ "Moonlight Notes". Retrieved 2009-12-27. 
  14. ^ "Gecko 1.9 Roadmap". Retrieved 2009-12-27. 
  15. ^ "ReleaseNotes046". Inkscape Wiki. Retrieved 2008-03-31. 
  16. ^ "Gnuplot version 4.4.0 announcement". Gnuplot homepage. Retrieved 2011-02-22. 
  17. ^ "Xr: Cross-device Rendering for Vector Graphics". Retrieved 2009-06-08. 
  18. ^ "Mailing list thread about the cairo name change". Retrieved 2009-06-08. 
  19. ^ "Mailing list thread about the cairo name change". Retrieved 2006-12-02. 

External links[edit]