summaryrefslogtreecommitdiff
path: root/gi/pygi-foreign-cairo.c
blob: ada59bf7011ec0fdce7503bcb4b371bfd109a0f1 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
 * Copyright (c) 2010  Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <cairo.h>
#include <Python.h>

#if PY_VERSION_HEX < 0x03000000
#include <pycairo.h>
#else
#include <pycairo/py3cairo.h>
#endif

Pycairo_CAPI_t *Pycairo_CAPI;

#include "pygi-foreign.h"

#include <pyglib-python-compat.h>

PyObject *
cairo_context_to_arg (PyObject       *value,
                      GITypeInfo     *type_info,
                      GITransfer      transfer,
                      GIArgument      *arg)
{
    cairo_t *cr;

    g_assert (transfer == GI_TRANSFER_NOTHING);

    cr = PycairoContext_GET (value);
    if (!cr) {
        return NULL;
    }

    arg->v_pointer = cr;
    Py_RETURN_NONE;
}

PyObject *
cairo_context_from_arg (GITypeInfo *type_info, GIArgument  *arg)
{
    cairo_t *context = (cairo_t*) arg;

    cairo_reference (context);

    return PycairoContext_FromContext (context, &PycairoContext_Type, NULL);
}

PyObject *
cairo_context_release (GIBaseInfo *base_info,
                       gpointer    struct_)
{
    cairo_destroy ( (cairo_t*) struct_);
    Py_RETURN_NONE;
}


PyObject *
cairo_surface_to_arg (PyObject       *value,
                      GITypeInfo     *type_info,
                      GITransfer      transfer,
                      GIArgument      *arg)
{
    cairo_surface_t *surface;

    g_assert (transfer == GI_TRANSFER_NOTHING);

    surface = ( (PycairoSurface*) value)->surface;
    if (!surface) {
        PyErr_SetString (PyExc_ValueError, "Surface instance wrapping a NULL surface");
        return NULL;
    }

    arg->v_pointer = surface;
    Py_RETURN_NONE;
}

PyObject *
cairo_surface_from_arg (GITypeInfo *type_info, GIArgument  *arg)
{
    cairo_surface_t *surface = (cairo_surface_t*) arg;

    cairo_surface_reference (surface);

    return PycairoSurface_FromSurface (surface, NULL);
}

PyObject *
cairo_surface_release (GIBaseInfo *base_info,
                       gpointer    struct_)
{
    cairo_surface_destroy ( (cairo_surface_t*) struct_);
    Py_RETURN_NONE;
}

#define _PY_TUPLE_GET_INT(t, pos, i)    \
{                                       \
    PyObject *item;                     \
    long l;                             \
    item = PyTuple_GET_ITEM(t, pos);    \
    if (!item)                          \
        goto err;                       \
    l = PYGLIB_PyLong_AsLong(item);     \
    if (PyErr_Occurred()) {             \
        Py_DECREF(item);                \
        goto err;                       \
    }                                   \
    if (l > INT_MAX || l < INT_MIN) {   \
        Py_DECREF(item);                \
        PyErr_Format(PyExc_ValueError, "integer %l is out of range", l); \
        goto err;                       \
    }                                   \
    i = (int)l;                         \
    Py_DECREF(item);                    \
}

#define _PY_TUPLE_SET_INT(t, pos, i)    \
{                                       \
    PyObject *item;                     \
    item = PYGLIB_PyLong_FromLong(i);   \
    if (!item)                          \
        goto err;                       \
    PyTuple_SET_ITEM(t, pos, item);     \
}

PyObject *
cairo_rectangle_int_to_arg (PyObject       *value,
                            GITypeInfo     *type_info,
                            GITransfer      transfer,
                            GIArgument      *arg)
{
    cairo_rectangle_int_t *rect;
    Py_ssize_t seq_len;

    seq_len = PySequence_Size(value);
    if (!PySequence_Check(value) || (seq_len != 4)) {
        PyErr_Format(PyExc_TypeError, "expected a sequence of length 4");
        goto err;
    }

    rect = g_malloc(sizeof(cairo_rectangle_int_t));
    if (!rect)
        return PyErr_NoMemory();

    _PY_TUPLE_GET_INT(value, 0, rect->x);
    _PY_TUPLE_GET_INT(value, 1, rect->y);
    _PY_TUPLE_GET_INT(value, 2, rect->width);
    _PY_TUPLE_GET_INT(value, 3, rect->height);

    arg->v_pointer = rect;
    Py_RETURN_NONE;
err:
    return NULL;
}

PyObject *
cairo_rectangle_int_from_arg (GITypeInfo *type_info, GIArgument  *arg)
{
    PyObject *result;
    cairo_rectangle_int_t *rect = (cairo_rectangle_int_t*) arg;

    result = PyTuple_New(4);
    if (!result)
        return NULL;

    if (rect) {
        _PY_TUPLE_SET_INT(result, 0, rect->x);
        _PY_TUPLE_SET_INT(result, 1, rect->y);
        _PY_TUPLE_SET_INT(result, 2, rect->width);
        _PY_TUPLE_SET_INT(result, 3, rect->height);
    } else {
        _PY_TUPLE_SET_INT(result, 0, 0);
        _PY_TUPLE_SET_INT(result, 1, 0);
        _PY_TUPLE_SET_INT(result, 2, 0);
        _PY_TUPLE_SET_INT(result, 3, 0);
    }

    return result;
err:
    Py_DECREF(result);
    return NULL;
}

PyObject *
cairo_rectangle_int_release (GIBaseInfo *base_info,
                             gpointer    struct_)
{
    g_free (struct_);
    Py_RETURN_NONE;
}

static PyMethodDef _gi_cairo_functions[] = {};
PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
{
    Pycairo_IMPORT;
    if (Pycairo_CAPI == NULL)
        return 0;

    pygi_register_foreign_struct ("cairo",
                                  "Context",
                                  cairo_context_to_arg,
                                  cairo_context_from_arg,
                                  cairo_context_release);

    pygi_register_foreign_struct ("cairo",
                                  "Surface",
                                  cairo_surface_to_arg,
                                  cairo_surface_from_arg,
                                  cairo_surface_release);

#ifdef PycairoRectangleInt_FromRectangleInt
     pygi_register_foreign_struct ("cairo",
                                  "RectangleInt",
                                  cairo_rectangle_int_to_arg,
                                  cairo_rectangle_int_from_arg,
                                  cairo_rectangle_int_release);
#endif

}
PYGLIB_MODULE_END;