Cairo (graphics)

Cairo (graphics)
Original author(s) Keith Packard, Carl Worth[1]
Developer(s) Carl Worth, Behdad Esfahbod
Stable release 1.15.2 (December 10, 2015 (2015-12-10)) [±]
Preview release 1.12.18 (November 7, 2014 (2014-11-07)) [±][2]
Written in C
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 that provides a vector graphics-based, device-independent API for software developers. It provides primitives for 2-dimensional drawing across a number of different back ends. Cairo uses hardware acceleration[3] when available.

There is a formal proposal to standardize 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++, C# and other CLS languages, 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.

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 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.

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:

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

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.[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

Wikimedia Commons has media related to Cairo (graphics).

References

  1. "Carl's boring web pages". cworth.org. 2013. Retrieved 11 July 2014.
  2. Harrington, Bryce (2014-11-07). "cairo 1.12.18 snapshot available". Retrieved 2014-12-31.
  3. "Cairo homepage". Retrieved 2010-10-30.
  4. McLaughlin, Michael B.; Sutter, Herb; Zink, Jason (2014-07-03). "A Proposal to Add 2D Graphics Rendering and Display to C++ Revision 2" (PDF). Open Standards. Retrieved 2014-09-12.
  5. "Cairo Language Bindings". Retrieved 2014-04-16.
  6. Fedor, Adam (2011-09-29). "User Defaults Summary for GNUstep Backend". Retrieved 2014-11-03.
  7. "Cairo - SDL". 2009-02-17. Retrieved 2014-11-03.
  8. Chris Wilson (2009-07-22). "New OpenGL backend merged". Retrieved 2010-02-12.
  9. Øyvind Kolås (2008-01-24). "Announcing OpenVG backend". Retrieved 2010-02-12.
  10. Vladimir Vukićević (2008-05-06). "Well Isn’t That Qt". Retrieved 2010-02-12.
  11. Chris Wilson (2009-08-31). "Cool Stuff". Retrieved 2010-02-12.
  12. Bas Schouten (2009-11-22). "Direct2D: Hardware Rendering a Browser". Retrieved 2010-02-12.
  13. "GTK+ to Use Cairo Vector Engine". Retrieved 2009-12-27.
  14. "Mono - Drawing". Retrieved 2009-12-27.
  15. "Moonlight Notes". Retrieved 2009-12-27.
  16. "Gecko 1.9 Roadmap". Retrieved 2009-12-27.
  17. "ReleaseNotes046". Inkscape Wiki. Retrieved 2008-03-31.
  18. "Gnuplot version 4.4.0 announcement". Gnuplot homepage. Retrieved 2011-02-22.
  19. "Xr: Cross-device Rendering for Vector Graphics". Retrieved 2009-06-08.
  20. "Mailing list thread about the cairo name change". Retrieved 2009-06-08.
  21. "Mailing list thread about the cairo name change". Retrieved 2006-12-02.

External links

This article is issued from Wikipedia - version of the Friday, April 15, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.