Cairo (graphics)

Last updated

Original author(s) Keith Packard, Carl Worth [1]
Developer(s) Carl Worth, Behdad Esfahbod
Initial releaseBefore 2003;21 years ago (2003) [2]
Stable release
1.18.2 (September 1, 2024;2 months ago (2024-09-01) [3] ) [±]
Repository gitlab.freedesktop.org/cairo/cairo
Written in C
Type Graphics library
License GNU Lesser General Public License version 2.1 (only) or Mozilla Public License 1.1
Website www.cairographics.org

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 backends. Cairo uses hardware acceleration [4] when available.

Contents

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

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 (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]

Drawing model

The Cairo drawing model Cairo's drawing model.svg
The Cairo drawing model

The Cairo drawing model 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 (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]

Example

Quite complex "Hello world" graphics can be drawn with the help of Cairo with only a few lines of source code:

SVG picture generated by this example Cairo example.svg
SVG picture generated by this example
#include<cairo-svg.h>#include<stdio.h>intmain(intargc,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(intx=0;x<10;++x)for(inty=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);}

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

Complex text layout

Cairo handles Latin and CJK based fonts, but does not directly support complex text layout fonts, which require shaping the glyphs. The Cairo developers recommend using Pango, which provides complex text layout and can integrate with Cairo. [24]

See also

Related Research Articles

Scalable Vector Graphics (SVG) is an XML-based vector image format for defining two-dimensional graphics, having support for interactivity and animation. The SVG specification is an open standard developed by the World Wide Web Consortium since 1999.

<span class="mw-page-title-main">Vector graphics</span> Computer graphics images defined by points, lines and curves

Vector graphics are a form of computer graphics in which visual images are created directly from geometric shapes defined on a Cartesian plane, such as points, lines, curves and polygons. The associated mechanisms may include vector display and printing hardware, vector data models and file formats, as well as the software based on these data models. Vector graphics are an alternative to raster or bitmap graphics, with each having advantages and disadvantages in specific situations.

Gecko is a browser engine developed by Mozilla. It is used in the Firefox browser, the Thunderbird email client, and many other projects.

<span class="mw-page-title-main">Graphics Device Interface</span> Microsoft Windows API

The Graphics Device Interface (GDI) is a legacy component of Microsoft Windows responsible for representing graphical objects and transmitting them to output devices such as monitors and printers. It was superseded by DirectDraw API and later Direct2D API. Windows apps use Windows API to interact with GDI, for such tasks as drawing lines and curves, rendering fonts, and handling palettes. The Windows USER subsystem uses GDI to render such UI elements as window frames and menus. Other systems have components that are similar to GDI; for example: Mac OS has QuickDraw, and Linux and Unix have X Window System core protocol.

<span class="mw-page-title-main">Pango</span> Library for text rendering

Pango is a text layout engine library which works with the HarfBuzz shaping engine for displaying multi-language text.

<span class="mw-page-title-main">Inkscape</span> Free open-source vector graphics editor

Inkscape is a free and open-source vector graphics editor for traditional Unix-compatible systems such as GNU/Linux, BSD derivatives and Illumos, as well as Windows and macOS. It offers a rich set of features and is widely used for both artistic and technical illustrations such as cartoons, clip art, logos, typography, diagramming and flowcharting. It uses vector graphics to allow for sharp printouts and renderings at unlimited resolution and is not bound to a fixed number of pixels like raster graphics. Inkscape uses the standardized Scalable Vector Graphics (SVG) file format as its main format, which is supported by many other applications including web browsers. It can import and export various other file formats, including SVG, AI, EPS, PDF, PS and PNG.

freedesktop.org (fd.o), formerly X Desktop Group (XDG), is a project to work on interoperability and shared base technology for free-software desktop environments for the X Window System (X11) and Wayland on Linux and other Unix-like operating systems. Although freedesktop.org produces specifications for interoperability, it is not a formal standards body.

WebKit is a browser engine primarily used in Apple's Safari web browser, as well as all web browsers on iOS and iPadOS. WebKit is also used by the PlayStation consoles starting with the PS3, the Tizen mobile operating systems, the Amazon Kindle e-book reader, Nintendo consoles starting with the 3DS Internet Browser, and the discontinued BlackBerry Browser.

OpenVG is an API designed for hardware-accelerated 2D vector graphics. Its primary platforms are mobile phones, gaming & media consoles and consumer electronic devices. It was designed to help manufacturers create more attractive user interfaces by offloading computationally intensive graphics processing from the CPU onto a GPU to save energy. The OpenGL ES library provides similar functionality for 3D graphics. OpenVG is managed by the non-profit technology consortium Khronos Group.

<span class="mw-page-title-main">GDK</span> Software library

GDK is a library that acts as a wrapper around the low-level functions provided by the underlying windowing and graphics systems. GDK lies between the display server and the GTK library, handling basic rendering such as drawing primitives, raster graphics (bitmaps), cursors, fonts, as well as window events and drag-and-drop functionality.

The canvas element is part of HTML5 and allows for dynamic, scriptable rendering of 2D shapes and bitmap images. It is a low level, procedural model that updates a bitmap. HTML5 Canvas also helps in making 2D games.

<span class="mw-page-title-main">Poppler (software)</span> PDF rendering engine

Poppler is a free and open-source software library for rendering Portable Document Format (PDF) documents. Its development is supported by freedesktop.org. Commonly used on Linux systems, it powers the PDF viewers of the GNOME and KDE desktop environments.

PGF/Ti<i>k</i>Z Graphics languages

PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description, with standard features including the drawing of points, lines, arrows, paths, circles, ellipses and polygons. PGF is a lower-level language, while TikZ is a set of higher-level macros that use PGF. The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost. Till Tantau is the designer of the PGF and TikZ languages. He is also the main developer of the only known interpreter for PGF and TikZ, which is written in TeX. PGF is an acronym for "Portable Graphics Format". TikZ was introduced in version 0.95 of PGF, and it is a recursive acronym for "TikZ ist kein Zeichenprogramm".

Direct2D is a 2D vector graphics application programming interface (API) designed by Microsoft and implemented in Windows 10, Windows 8, Windows 7 and Windows Server 2008 R2, and also Windows Vista and Windows Server 2008.

<span class="mw-page-title-main">Skia Graphics Engine</span> Open source graphics library written in C++

The Skia Graphics Engine or Skia is an open-source 2D graphics library written in C++. Skia abstracts away platform-specific graphics APIs. Skia Inc. originally developed the library; Google acquired it in 2005, and then released the software as open source licensed under the New BSD free software license in 2008.

Google Chrome Experiments Online showroom of web browser based experiments

Google Chrome Experiments is an online showroom of web browser-based experiments, interactive programs, and artistic projects. Launched on March 1, 2009, Google Chrome Experiments is an official Google website that was originally meant to test the limits of JavaScript and the Google Chrome browser's performance and abilities. As the project progressed, it took on the role of showcasing and experimenting with the latest open-source web-based technologies, such as JavaScript, HTML, WebGL, Canvas, SVG, and CSS. All the projects on Chrome Experiments are user-submitted and are made using open-source technologies. As of 2024, the website continues to host a growing number of experiments, featuring over 1,500 projects.

<span class="mw-page-title-main">GTK Scene Graph Kit</span>

GTK Scene Graph Kit (GSK) is the rendering and scene graph API for GTK introduced with version 3.90. GSK lies between the graphical control elements (widgets) and the rendering.

<span class="mw-page-title-main">OpenFL</span> Software framework for video games

OpenFL is a free and open-source software framework and platform for the creation of multi-platform applications and video games. OpenFL applications can be written in Haxe, JavaScript, or TypeScript, and may be published as standalone applications for several targets including iOS, Android, HTML5, Windows, macOS, Linux, WebAssembly, Flash, AIR, PlayStation 4, PlayStation 3, PlayStation Vita, Xbox One, Wii U, TiVo, Raspberry Pi, and Node.js.

raylib Game programming library

Raylib is a cross-platform open-source software development library. The library was made to create graphical applications and games.

References

  1. "Carl's boring web pages". cworth.org. 2013. Retrieved July 11, 2014.
  2. 1 2 "Xr: Cross-device Rendering for Vector Graphics" . Retrieved June 8, 2009.
  3. "cairo-1.18.2". www.cairographics.org. Retrieved September 8, 2024.
  4. "Cairo homepage" . Retrieved October 30, 2010.
  5. "Cairo Language Bindings" . Retrieved April 16, 2014.
  6. "SDL". Cairo. February 17, 2009. Retrieved November 3, 2014.
  7. Chris Wilson (July 22, 2009). "New OpenGL backend merged" . Retrieved February 12, 2010.
  8. Øyvind Kolås (January 24, 2008). "Announcing OpenVG backend" . Retrieved February 12, 2010.
  9. Vladimir Vukićević (May 6, 2008). "Well Isn't That Qt". Archived from the original on April 9, 2010. Retrieved February 12, 2010.
  10. Chris Wilson (August 31, 2009). "Cool Stuff" . Retrieved February 12, 2010.
  11. Bas Schouten (November 22, 2009). "Direct2D: Hardware Rendering a Browser" . Retrieved February 12, 2010.
  12. Larabel, Michael (February 27, 2022). "Cairo graphics library drops many old backends". Phoronix. Retrieved June 5, 2022.
  13. "GNOME/librsvg". GitHub.
  14. "GTK+ to Use Cairo Vector Engine". February 5, 2005. Retrieved December 27, 2009.
  15. "Details of package gtk-vector-screenshot in stretch". Debian. GitHub
  16. "Mono - Drawing" . Retrieved December 27, 2009.
  17. "Moonlight Notes" . Retrieved December 27, 2009.
  18. "Gecko 1.9 Roadmap" . Retrieved December 27, 2009.
  19. "ReleaseNotes046". Inkscape Wiki. Retrieved March 31, 2008.
  20. "ManimCE tutorial by TheoremOfBeethoven — ManimCE tutorial by TB documentation". zavden.github.io. Retrieved January 29, 2023.
  21. "Gnuplot version 4.4.0 announcement". Gnuplot homepage. Archived from the original on May 14, 2011. Retrieved February 22, 2011.
  22. "Mailing list thread about the Cairo name change". July 15, 2003. Retrieved June 8, 2009.
  23. "Mailing list thread about the Cairo name change". July 12, 2003. Retrieved December 2, 2006.
  24. "How do I use Pango instead of Cairo's "toy" text API?" . Retrieved March 20, 2024.