Cairo (graphics library)

from Wikipedia, the free encyclopedia
Cairo

Cairo banner 1.svg
The Cairo banner
Basic data

developer The Cairo team
Current  version 1.16.0
(October 19, 2018)
Current preliminary version 1.15.12
(April 11, 2018)
operating system Unix , Linux , Windows , macOS , BeOS
programming language C.
category Graphics library
License LGPL / MPL
German speaking No
cairographics.org

Cairo is a free 2D - graphics library . It enables software developers to output to various so-called backends via a device-independent , vector-based API . Currently, the output on the X Window System , on Win32 , on Quartz , in local image buffers , and in the file formats PNG , PostScript , PDF and SVG is considered stable. Back-ends for output under OpenGL (via Glitz ), XCB , DirectFB , BeOS and OS / 2 are in development.

Cairo was designed in such a way that - if available - the output back-ends can be accelerated by the graphics processor , for example for smoothing edges . However, this possibility is z. B. currently not used under Win32.

The Cairo development was originally started under the name Xr and Xr / Xc by Keith Packard and Carl Worth for use with the X Window System. The name is derived from the Greek letters Chi and Rho .

Competing technologies are WPF and GDI + from Microsoft and Quartz from Apple .

Character model

Cairos drawing model

Cairo pursues an unusual drawing technique based on a three-part layer model (see picture "Cairos drawing model"):

A drawing process takes place by creating a mask which contains one or more vector primitives or forms, i.e. H. Circles, squares, TTF fonts, Bézier curves, etc. Then a source must be defined which, for example, contains B. can consist of a color, a gradient, a bitmap or vector graphic. Now the painted part is punched out of the source with the mask like a cookie cutter . The result is finally transferred to the destination or surface , which is provided by the output backend.

This is a fundamentally different approach than z. B. SVG vector graphics.

Programming example

The SVG image generated by this program

Cairo was written in C , there are also language bindings to many other programming languages , including Haskell , Java , Perl , Ruby , Python , Scheme , Smalltalk .

Even more complex " Hello World " graphics can be drawn with Cairo with just a few lines of source text :

 1 #include <cairo/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     /* Quadrate im Hintergrund zeichnen */
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     /* Schrift im Vordergrund */
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 }

First, in lines 11 to 13 in conjunction with the declarations in lines five and six, an SVG surface and a Cairo object are created. This can then be used for drawing by drawing shapes and defining a source ( cairo_set_source _... ). In this way, a 10 × 10 field of regular squares is generated in line 16f, which is then drawn with a radial color gradient. From line 28 a simple possibility is shown to draw texts with Cairo.

use

Cairo has found widespread use in the open source community thanks to its ability to create cross-platform, high quality 2D drawings. Various GUI toolkits (e.g. GTK + ), browser engines and graphics programs use Cairo, among others .

Use in GUI toolkits

Example: The drawing of a transparent clock realized with Cairo

GTK + supports Cairo from version 2.8.0 . This makes it possible to obtain a Cairo context ( cairo_t , see line five in the example) from GTK + widgets and then to draw it with Cairo functions. As of this version, GTK + uses the same methods to draw most of its widgets internally with Cairo and can thus benefit from hardware-accelerated rendering.

The developers of the C ++ connection to GTK +, gtkmm , already classify drawing with the GDK as out of date ( deprecated ), because the drawing functions of GDK are complicated to use and comparatively inefficient. Instead, we recommend using Cairo without exception.

Cairo can also use the FLTK to draw the entire surface.

Thanks to its platform independence, Cairo is also used in wxWidgets .

The Mono project has also been using Cairo since the first versions to draw the GDI + surface.

The Standard Widget Toolkit uses Cairo from version 3.1 on Linux for its drawing API.

Use in browser engines

In Gecko -based web browsers , Cairo is used from Gecko version 1.8 (e.g. used from Firefox 1.5) to display SVG files and from Gecko version 1.9 (Firefox 3) also to display the entire graphical user interface ( XUL ) including websites.

Also WebKit Cairo used to render all graphical elements in its GTK + - Port . SVG and <canvas> elements are also rendered using Cairo.

Use in graphics software

From version 0.46 the vector graphics program Inkscape Cairo uses for the outline display mode as well as for writing PDF and PostScript documents.

Cairo also uses the PDF rendering engine Poppler to render PDF documents. As a spin-off from Xpdf , Poppler is used in many popular PDF viewers such as Evince and Okular .

Cairo can be used in the Office package OpenOffice.org from version 2 for hardware-accelerated presentation effects.

Even programs like rsvg use Cairo for rendering. In this way, bitmap graphics are created from SVG files, a technique that Wikipedia also makes use of, for example .

Connection to other programming languages

Thanks to its implementation in the C programming language , links to other programming languages ​​( language bindings ) are easy to implement. There are bindings for C ++ ( cairomm , analogous to gtkmm ), Java , Haskell , Mono / .NET , Perl , PHP , Python , Ruby , Rust , Objective Caml , Scheme , Lua , Vala and many other programming languages.

Web links

Commons : Cairo effects with GTK + and hardware acceleration  - collection of images, videos and audio files

Individual evidence

  1. a b Latest cairo news
  2. Cairo Interaction ( Memento of the original from April 15, 2008 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. in the GDK Reference Manual  @1@ 2Template: Webachiv / IABot / library.gnome.org
  3. Julien Boulnois has an approach to this with his cairo-gtk-engine ( memento of the original of July 24, 2008 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. developed @1@ 2Template: Webachiv / IABot / webcvs.cairographics.org
  4. Programming with gtkmm , Chapter 15. The Drawing Area Widget or Appendix F. Drawing With GDK ( Memento from June 4, 2009 in the Internet Archive )
  5. On the FLTK project homepage: Article # 622: New Poll: FLTK2 and Cairo
  6. wxcairo - Combining Cairo with WxWidgets - Google Project Hosting
  7. http://weblogs.mozillazine.org/tor/archives/2006/04/why_cairo.html ( Memento from January 26, 2010 in the Internet Archive )
  8. Cairo is being switched on for Windows nightly builds ( Memento of October 15, 2008 in the Internet Archive ), February 22, 2006
  9. Release Notes for Version 0.46 from the Inkscape Wiki
  10. https://github.com/gtk-rs/cairo
  11. Project homepage: Language bindings