| 1 |
==================================== |
| 2 |
An Introduction to Cairo with Python |
| 3 |
==================================== |
| 4 |
|
| 5 |
Cairo is a library for drawing vector graphics. Vector graphics are |
| 6 |
interesting because they don't lose clarity when resized or transformed. |
| 7 |
|
| 8 |
Pycairo is a set of bindings for cairo. It provides the cairo module which can |
| 9 |
be used to call cairo commands from Python. |
| 10 |
|
| 11 |
|
| 12 |
Understanding How to use Cairo |
| 13 |
------------------------------ |
| 14 |
|
| 15 |
The best way to understand how to use cairo is to imagine that you are an |
| 16 |
artist using a paintbrush to draw out a shape on canvas. |
| 17 |
|
| 18 |
To begin, you can choose a few characteristics of your brush. You can choose |
| 19 |
the thickness of your brush and the colour you want to paint with. You can |
| 20 |
also choose the shape of your brush tip - You can choose either a circle or a |
| 21 |
square. |
| 22 |
|
| 23 |
Once you have chosen your brush, you are ready to start painting. You have to |
| 24 |
be quite precise when describing what you want to appear. |
| 25 |
|
| 26 |
Firstly, decide where you want to place your brush on the canvas. You do this |
| 27 |
by supplying an x & y coordinate. Next you define how you want your brush |
| 28 |
stroke to look - an arc, a straight line etc. Finally you define the point |
| 29 |
where you want your stoke to end, again by supplying an x & y coordinate. |
| 30 |
Triangles and squares are very easy to do! |
| 31 |
|
| 32 |
More complex graphics are generated using variations of the above theme with a |
| 33 |
few additions such as Fills (colouring in), transformations (zooming in, |
| 34 |
moving) etc. Using the Python interface to cairo |
| 35 |
|
| 36 |
Nearly all the work revolves around using the :class:`cairo.Context` (or |
| 37 |
``cairo_t`` in the cairo C API). This is the object that you send your drawing |
| 38 |
commands to. There are a few options available to initialize this object in |
| 39 |
different ways. |
| 40 |
|
| 41 |
Initializing the cairo.Context Object |
| 42 |
------------------------------------- |
| 43 |
|
| 44 |
* One Very Important thing to realize is there is a difference between the |
| 45 |
coordinates you are describing your graphics on and the coordinates you will |
| 46 |
be displaying your graphic on. |
| 47 |
|
| 48 |
(Ex - When giving a presentation you draw on your transparent acetate before |
| 49 |
hand, and then display it on your overhead projector - cairo calls the |
| 50 |
transparent acetate the user space coordinates and the projected image the |
| 51 |
device space coordinates) |
| 52 |
|
| 53 |
On initializing the cairo context object, we tell it how to transform our |
| 54 |
description to how it should be displayed. To do this we supply a |
| 55 |
transformation matrix. Modifying the transformation matrix can lead to some |
| 56 |
very interesting results. |
| 57 |
|
| 58 |
* One of cairo's most powerful features is that it can output graphics in many |
| 59 |
different formats (it can use multiple back ends). For printing, we can have |
| 60 |
cairo translate our graphics into Postscript to be sent off to the printer. |
| 61 |
For on screen display, we can have cairo translate our graphics into |
| 62 |
something glitz can understand for hardware accelerated rendering! It has |
| 63 |
many more important and useful target back ends. On initializing the |
| 64 |
:class:`cairo.Context`, we set its target back end, supplying a few details |
| 65 |
(such as colour depth and size), as in the example below. |
| 66 |
|
| 67 |
Example |
| 68 |
------- |
| 69 |
|
| 70 |
.. code:: python |
| 71 |
|
| 72 |
#!/usr/bin/env python |
| 73 |
|
| 74 |
import math |
| 75 |
import cairo |
| 76 |
|
| 77 |
WIDTH, HEIGHT = 256, 256 |
| 78 |
|
| 79 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) |
| 80 |
ctx = cairo.Context(surface) |
| 81 |
|
| 82 |
ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas |
| 83 |
|
| 84 |
pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0) |
| 85 |
pat.add_color_stop_rgba(1, 0.7, 0, 0, 0.5) # First stop, 50% opacity |
| 86 |
pat.add_color_stop_rgba(0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity |
| 87 |
|
| 88 |
ctx.rectangle(0, 0, 1, 1) # Rectangle(x0, y0, x1, y1) |
| 89 |
ctx.set_source(pat) |
| 90 |
ctx.fill() |
| 91 |
|
| 92 |
ctx.translate(0.1, 0.1) # Changing the current transformation matrix |
| 93 |
|
| 94 |
ctx.move_to(0, 0) |
| 95 |
# Arc(cx, cy, radius, start_angle, stop_angle) |
| 96 |
ctx.arc(0.2, 0.1, 0.1, -math.pi / 2, 0) |
| 97 |
ctx.line_to(0.5, 0.1) # Line to (x,y) |
| 98 |
# Curve(x1, y1, x2, y2, x3, y3) |
| 99 |
ctx.curve_to(0.5, 0.2, 0.5, 0.4, 0.2, 0.8) |
| 100 |
ctx.close_path() |
| 101 |
|
| 102 |
ctx.set_source_rgb(0.3, 0.2, 0.5) # Solid color |
| 103 |
ctx.set_line_width(0.02) |
| 104 |
ctx.stroke() |
| 105 |
|
| 106 |
surface.write_to_png("example.png") # Output to PNG |