| 1 |
CFFI API |
| 2 |
======== |
| 3 |
|
| 4 |
.. currentmodule:: cairocffi |
| 5 |
|
| 6 |
cairocffi’s :doc:`API <api>` is made of a number of |
| 7 |
:ref:`wrapper <wrappers>` classes |
| 8 |
that provide a more Pythonic interface for various cairo objects. |
| 9 |
Functions that take a pointer as their first argument become methods, |
| 10 |
error statuses become exceptions, |
| 11 |
and :ref:`reference counting <refcounting>` is hidden away. |
| 12 |
|
| 13 |
In order to use other C libraries that use integrate with cairo, |
| 14 |
or if cairocffi’s API is not sufficient |
| 15 |
(Consider making a `pull request`_!) |
| 16 |
you can access cairo’s lower level C pointers and API through CFFI_. |
| 17 |
|
| 18 |
.. _pull request: https://github.com/SimonSapin/cairocffi |
| 19 |
.. _CFFI: https://cffi.readthedocs.org/ |
| 20 |
|
| 21 |
|
| 22 |
Module-level objects |
| 23 |
-------------------- |
| 24 |
|
| 25 |
.. data:: ffi |
| 26 |
|
| 27 |
A :external:cffi:doc:`FFI <ref>` instance with all of the cairo C API |
| 28 |
declared. |
| 29 |
|
| 30 |
.. data:: cairo |
| 31 |
|
| 32 |
The libcairo library, pre-loaded with :external:cffi:ref:`ffi.dlopen() <dlopen>`. |
| 33 |
All cairo functions are accessible as attributes of this object:: |
| 34 |
|
| 35 |
import cairocffi |
| 36 |
from cairocffi import cairo as cairo_c, SURFACE_TYPE_XLIB |
| 37 |
|
| 38 |
if cairo_c.cairo_surface_get_type(surface._pointer) == SURFACE_TYPE_XLIB: |
| 39 |
... |
| 40 |
|
| 41 |
See the `cairo manual`_ for details. |
| 42 |
|
| 43 |
.. _cairo manual: http://cairographics.org/manual/ |
| 44 |
|
| 45 |
|
| 46 |
.. _refcounting: |
| 47 |
|
| 48 |
Reference counting in cairo |
| 49 |
--------------------------- |
| 50 |
|
| 51 |
Most cairo objects are reference-counted, |
| 52 |
and freed when the count reaches zero. |
| 53 |
cairocffi’s Python wrapper will automatically decrease the reference count |
| 54 |
when they are garbage-collected. |
| 55 |
Therefore, care must be taken when creating a wrapper |
| 56 |
as to the reference count should be increased (for existing cairo objects) |
| 57 |
or not (for cairo objects that were just created with a refcount of 1.) |
| 58 |
|
| 59 |
|
| 60 |
Wrappers |
| 61 |
-------- |
| 62 |
|
| 63 |
.. automethod:: Surface._from_pointer |
| 64 |
.. automethod:: Pattern._from_pointer |
| 65 |
.. automethod:: FontFace._from_pointer |
| 66 |
.. automethod:: ScaledFont._from_pointer |
| 67 |
.. automethod:: Context._from_pointer |
| 68 |
|
| 69 |
.. attribute:: Surface._pointer |
| 70 |
|
| 71 |
The underlying ``cairo_surface_t *`` cdata pointer. |
| 72 |
|
| 73 |
.. attribute:: Pattern._pointer |
| 74 |
|
| 75 |
The underlying ``cairo_pattern_t *`` cdata pointer. |
| 76 |
|
| 77 |
.. attribute:: FontFace._pointer |
| 78 |
|
| 79 |
The underlying ``cairo_font_face_t *`` cdata pointer. |
| 80 |
|
| 81 |
.. attribute:: ScaledFont._pointer |
| 82 |
|
| 83 |
The underlying ``cairo_scaled_font_t *`` cdata pointer. |
| 84 |
|
| 85 |
.. attribute:: FontOptions._pointer |
| 86 |
|
| 87 |
The underlying ``cairo_scaled_font_t *`` cdata pointer. |
| 88 |
|
| 89 |
.. attribute:: Matrix._pointer |
| 90 |
|
| 91 |
The underlying ``cairo_matrix_t *`` cdata pointer. |
| 92 |
|
| 93 |
.. attribute:: Context._pointer |
| 94 |
|
| 95 |
The underlying ``cairo_t *`` cdata pointer. |
| 96 |
|
| 97 |
|
| 98 |
.. _converting_pycairo: |
| 99 |
|
| 100 |
Converting pycairo wrappers to cairocffi |
| 101 |
---------------------------------------- |
| 102 |
|
| 103 |
Some libraries such as PyGTK or PyGObject |
| 104 |
provide a pycairo :class:`~cairo.Context` object for you to draw on. |
| 105 |
It is possible to extract the underlying ``cairo_t *`` pointer |
| 106 |
and create a cairocffi wrapper for the same cairo context. |
| 107 |
|
| 108 |
The follwing function does that with unsafe pointer manipulation. |
| 109 |
It only works on CPython. |
| 110 |
|
| 111 |
.. literalinclude:: ../utils/pycairo_to_cairocffi.py |
| 112 |
|
| 113 |
Converting other types of objects like surfaces is very similar, |
| 114 |
but left as an exercise to the reader. |
| 115 |
|
| 116 |
|
| 117 |
Converting cairocffi wrappers to pycairo |
| 118 |
---------------------------------------- |
| 119 |
|
| 120 |
The reverse conversion is also possible. |
| 121 |
Here we use ctypes rather than CFFI |
| 122 |
because Python’s C API is sensitive to the GIL. |
| 123 |
|
| 124 |
.. literalinclude:: ../utils/cairocffi_to_pycairo.py |
| 125 |
|
| 126 |
|
| 127 |
.. _using_pango: |
| 128 |
|
| 129 |
Example: using Pango through CFFI with cairocffi |
| 130 |
------------------------------------------------ |
| 131 |
|
| 132 |
The program below shows a fairly standard usage of CFFI |
| 133 |
to access Pango’s C API. |
| 134 |
The :attr:`Context._pointer` pointer can be used directly as an argument |
| 135 |
to CFFI functions that expect ``cairo_t *``. |
| 136 |
The C definitions are copied from `Pango’s`_ and `GLib’s`_ documentation. |
| 137 |
|
| 138 |
.. _Pango’s: http://developer.gnome.org/pango/stable/ |
| 139 |
.. _GLib’s: http://developer.gnome.org/glib/stable/ |
| 140 |
|
| 141 |
|
| 142 |
Using CFFI for accessing Pango |
| 143 |
(rather than the traditional bindings in PyGTK or PyGObject with introspection) |
| 144 |
is not only easiest for using together with cairocffi, |
| 145 |
but also means that all of Pango’s API is within reach, |
| 146 |
whereas bindings often only expose the high level API. |
| 147 |
|
| 148 |
.. literalinclude:: ../utils/pango_example.py |