[go: up one dir, main page]

File: cffi_api.rst

package info (click to toggle)
cairocffi 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 688 kB
  • sloc: python: 3,000; makefile: 19
file content (149 lines) | stat: -rw-r--r-- 4,335 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
CFFI API
========

.. currentmodule:: cairocffi

cairocffi’s :doc:`API <api>` is made of a number of
:ref:`wrapper <wrappers>` classes
that provide a more Pythonic interface for various cairo objects.
Functions that take a pointer as their first argument become methods,
error statuses become exceptions,
and :ref:`reference counting <refcounting>` is hidden away.

In order to use other C libraries that use integrate with cairo,
or if cairocffi’s API is not sufficient
(Consider making a `pull request`_!)
you can access cairo’s lower level C pointers and API through CFFI_.

.. _pull request: https://github.com/SimonSapin/cairocffi
.. _CFFI: https://cffi.readthedocs.org/


Module-level objects
--------------------

.. data:: ffi

    A :class:`cffi.FFI` instance with all of the cairo C API declared.

.. data:: cairo

    The libcairo library, pre-loaded with :meth:`ffi.dlopen`.
    All cairo functions are accessible as attributes of this object::

        import cairocffi
        from cairocffi import cairo as cairo_c, SURFACE_TYPE_XLIB

        if cairo_c.cairo_surface_get_type(surface._pointer) == SURFACE_TYPE_XLIB:
            ...

    See the `cairo manual`_ for details.

    .. _cairo manual: http://cairographics.org/manual/


.. _refcounting:

Reference counting in cairo
---------------------------

Most cairo objects are reference-counted,
and freed when the count reaches zero.
cairocffi’s Python wrapper will automatically decrease the reference count
when they are garbage-collected.
Therefore, care must be taken when creating a wrapper
as to the reference count should be increased (for existing cairo objects)
or not (for cairo objects that were just created with a refcount of 1.)


.. _wrappers:

Wrappers
--------

.. automethod:: Surface._from_pointer
.. automethod:: Pattern._from_pointer
.. automethod:: FontFace._from_pointer
.. automethod:: ScaledFont._from_pointer
.. automethod:: Context._from_pointer

.. attribute:: Surface._pointer

    The underlying :c:type:`cairo_surface_t *` cdata pointer.

.. attribute:: Pattern._pointer

    The underlying :c:type:`cairo_pattern_t *` cdata pointer.

.. attribute:: FontFace._pointer

    The underlying :c:type:`cairo_font_face_t *` cdata pointer.

.. attribute:: ScaledFont._pointer

    The underlying :c:type:`cairo_scaled_font_t *` cdata pointer.

.. attribute:: FontOptions._pointer

    The underlying :c:type:`cairo_scaled_font_t *` cdata pointer.

.. attribute:: Matrix._pointer

    The underlying :c:type:`cairo_matrix_t *` cdata pointer.

.. attribute:: Context._pointer

    The underlying :c:type:`cairo_t *` cdata pointer.


.. _converting_pycairo:

Converting pycairo wrappers to cairocffi
----------------------------------------

Some libraries such as PyGTK or PyGObject
provide a pycairo :class:`~cairo.Context` object for you to draw on.
It is possible to extract the underlying :c:type:`cairo_t *` pointer
and create a cairocffi wrapper for the same cairo context.

The follwing function does that with unsafe pointer manipulation.
It only works on CPython.

.. literalinclude:: ../utils/pycairo_to_cairocffi.py

Converting other types of objects like surfaces is very similar,
but left as an exercise to the reader.


Converting cairocffi wrappers to pycairo
----------------------------------------

The reverse conversion is also possible.
Here we use ctypes rather than CFFI
because Python’s C API is sensitive to the GIL.

.. literalinclude:: ../utils/cairocffi_to_pycairo.py


.. _using_pango:

Example: using Pango through CFFI with cairocffi
------------------------------------------------

The program below shows a fairly standard usage of CFFI
to access Pango’s C API.
The :attr:`Context._pointer` pointer can be used directly as an argument
to CFFI functions that expect ``cairo_t *``.
The C definitions are copied from `Pango’s`_ and `GLib’s`_ documentation.

.. _Pango’s: http://developer.gnome.org/pango/stable/
.. _GLib’s: http://developer.gnome.org/glib/stable/


Using CFFI for accessing Pango
(rather than the traditional bindings in PyGTK or PyGObject with introspection)
is not only easiest for using together with cairocffi,
but also means that all of Pango’s API is within reach,
whereas bindings often only expose the high level API.

.. literalinclude:: ../utils/pango_example.py