| 1 |
Overview |
| 2 |
======== |
| 3 |
|
| 4 |
.. currentmodule:: cairocffi |
| 5 |
|
| 6 |
Installing CFFI |
| 7 |
--------------- |
| 8 |
|
| 9 |
cairocffi requires CFFI, |
| 10 |
which can be installed with pip_ but has its own dependencies |
| 11 |
that can be tricky to install. |
| 12 |
|
| 13 |
* On Linux, install ``python-dev`` and ``libffi-dev`` from your system’s package manager. |
| 14 |
* On OS X, install ``pkg-config`` and ``libffi``, for example with `Homebrew`_. |
| 15 |
You may need to `set the PKG_CONFIG_PATH environment variable`_. |
| 16 |
* On Windows, consider using `Christoph Gohlke’s unofficial binary builds`_. |
| 17 |
|
| 18 |
See `CFFI’s own documentation`_ for details. |
| 19 |
|
| 20 |
.. _Homebrew: http://brew.sh/ |
| 21 |
.. _set the PKG_CONFIG_PATH environment variable: http://cffi.readthedocs.org/#macos-x |
| 22 |
.. _Christoph Gohlke’s unofficial binary builds: http://www.lfd.uci.edu/~gohlke/pythonlibs/#cffi |
| 23 |
.. _CFFI’s own documentation: http://cffi.readthedocs.org/ |
| 24 |
|
| 25 |
|
| 26 |
Installing cairocffi |
| 27 |
-------------------- |
| 28 |
|
| 29 |
Install with pip_:: |
| 30 |
|
| 31 |
pip install cairocffi |
| 32 |
|
| 33 |
This will automatically install CFFI. |
| 34 |
|
| 35 |
cairocffi can also be setup to utilize XCB support via xcffib_, that has to be |
| 36 |
installed before cairocffi:: |
| 37 |
|
| 38 |
pip install xcffib |
| 39 |
pip install cairocffi |
| 40 |
|
| 41 |
In addition to other dependencies, this will install xcffib. |
| 42 |
|
| 43 |
.. _pip: http://pip-installer.org/ |
| 44 |
.. _xcffib: https://github.com/tych0/xcffib/ |
| 45 |
|
| 46 |
|
| 47 |
Importing |
| 48 |
--------- |
| 49 |
|
| 50 |
The module to import is named ``cairocffi`` |
| 51 |
in order to co-exist peacefully with Pycairo_ which uses ``cairo``, |
| 52 |
but ``cairo`` is shorter and nicer to use:: |
| 53 |
|
| 54 |
import cairocffi as cairo |
| 55 |
|
| 56 |
cairocffi will dynamically load cairo as a shared library at this point. |
| 57 |
If it fails to find it, you will see an exception like this:: |
| 58 |
|
| 59 |
OSError: library not found: 'cairo' |
| 60 |
|
| 61 |
Make sure cairo is correctly installed and available through your system’s |
| 62 |
usual mechanisms. |
| 63 |
|
| 64 |
On Linux, the ``LD_LIBRARY_PATH`` environment variable can be used to indicate |
| 65 |
where to find shared libraries. |
| 66 |
|
| 67 |
On Windows, you can put the folder where Cairo and other DLLs are installed in |
| 68 |
the ``CAIROCFFI_DLL_DIRECTORIES`` environment variable. |
| 69 |
|
| 70 |
.. _Pycairo: http://cairographics.org/pycairo/ |
| 71 |
|
| 72 |
|
| 73 |
Installing cairo on Windows |
| 74 |
........................... |
| 75 |
|
| 76 |
cairocffi needs a ``libcairo-2.dll`` file |
| 77 |
in a directory that is listed in the ``PATH`` environment variable. |
| 78 |
|
| 79 |
`Alexander Shaduri’s GTK+ installer |
| 80 |
<http://gtk-win.sourceforge.net/home/index.php/Main/Downloads>`_ works. |
| 81 |
(Make sure to leave the *Set up PATH environment variable* checkbox checked.) |
| 82 |
Pycairo on Windows is sometimes compiled statically against cairo |
| 83 |
and may not provide a ``.dll`` file that cairocffi can use. |
| 84 |
|
| 85 |
|
| 86 |
cairo versions |
| 87 |
-------------- |
| 88 |
|
| 89 |
Cairo, pycairo, and cairocffi each have version numbers. The same cairocffi |
| 90 |
version can be used with a variety of cairo versions. For example, the |
| 91 |
:meth:`Surface.set_mime_data` method is based on the |
| 92 |
``cairo_surface_set_mime_data`` C function, which is only available since |
| 93 |
cairo 1.10. You will get a runtime exception if you try to use it with an older |
| 94 |
cairo. You can however still use the rest of the API. There is no need for |
| 95 |
cairocffi’s versions to be tied to cairo’s versions. |
| 96 |
|
| 97 |
Use :func:`cairo_version` to test the version number for cairo:: |
| 98 |
|
| 99 |
if cairocffi.cairo_version() > 11000: |
| 100 |
surface.set_mime_data('image/jpeg', jpeg_bytes) |
| 101 |
|
| 102 |
Here are all the version numbers:: |
| 103 |
|
| 104 |
>>> print("The cairo version is %s, meaning %s." |
| 105 |
... % (cairocffi.cairo_version(), cairocffi.cairo_version_string()) |
| 106 |
The cairo version is 11402, meaning 1.14.02. |
| 107 |
>>> print("The latest pycairo version this cairocffi version is compatible with is %s." |
| 108 |
... % cairo.version) |
| 109 |
The latest pycairo version this cairocffi version is compatible with is 1.10.0. |
| 110 |
>>> print("The cairocffi version is %s." % cairo.VERSION) |
| 111 |
The cairocffi version is 0.7.2 |
| 112 |
|
| 113 |
cairocffi is automatically tested with both cairo 1.13.0 (Ubuntu Trusty's |
| 114 |
default version) and manually tested with the latest (1.15.12 as of this |
| 115 |
writing.) |
| 116 |
|
| 117 |
|
| 118 |
Compatibility with Pycairo |
| 119 |
-------------------------- |
| 120 |
|
| 121 |
cairocffi’s Python API is compatible with Pycairo. |
| 122 |
Please `file a bug <https://github.com/SimonSapin/cairocffi/issues>`_ |
| 123 |
if you find incompatibilities. |
| 124 |
|
| 125 |
In your own code that uses Pycairo, you should be able to just change |
| 126 |
the imports from ``import cairo`` to ``import cairocffi as cairo`` as above. |
| 127 |
If it’s not your own code that imports Pycairo, |
| 128 |
the :func:`install_as_pycairo` function can help:: |
| 129 |
|
| 130 |
import cairocffi |
| 131 |
cairocffi.install_as_pycairo() |
| 132 |
import cairo |
| 133 |
assert cairo is cairocffi |
| 134 |
|
| 135 |
Alternatively, add a ``cairo.py`` file somewhere in your ``sys.path``, |
| 136 |
so that it’s imported instead of pycairo:: |
| 137 |
|
| 138 |
from cairocffi import * |
| 139 |
|
| 140 |
It is also possible to :ref:`convert pycairo contexts to cairocffi |
| 141 |
<converting_pycairo>`. |
| 142 |
|
| 143 |
|
| 144 |
Basic usage |
| 145 |
----------- |
| 146 |
|
| 147 |
For doing something useful with cairo, |
| 148 |
you need at least a surface and a context:: |
| 149 |
|
| 150 |
import cairocffi as cairo |
| 151 |
|
| 152 |
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 300, 200) |
| 153 |
context = cairo.Context(surface) |
| 154 |
with context: |
| 155 |
context.set_source_rgb(1, 1, 1) # White |
| 156 |
context.paint() |
| 157 |
# Restore the default source which is black. |
| 158 |
context.move_to(90, 140) |
| 159 |
context.rotate(-0.5) |
| 160 |
context.set_font_size(20) |
| 161 |
context.show_text('Hi from cairo!') |
| 162 |
surface.write_to_png('example.png') |
| 163 |
|
| 164 |
The :class:`Surface` represents the target. |
| 165 |
There are various types of surface for various output backends. |
| 166 |
The :class:`Context` holds internal state and is use for drawing. |
| 167 |
We’re only using solid colors here, |
| 168 |
but more complex :class:`Pattern` types are also available. |
| 169 |
|
| 170 |
All the details are in :doc:`api`. |