Debian Codegraph @

cairo/device.c

raw symbols SCIP
1 /* Pycairo - Python bindings for cairo
2 *
3 * Copyright © 2017 Christoph Reiter
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
12 *
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
18 *
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
23 *
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
27 */
28
29 #define PY_SSIZE_T_CLEAN
30 #include <Python.h>
31
32 #include "private.h"
33
34 static void
35 device_dealloc(PycairoDevice *obj) {
36 if (obj->device) {
37 cairo_device_destroy(obj->device);
38 obj->device = NULL;
39 }
40 Py_TYPE(obj)->tp_free(obj);
41 }
42
43 static PyObject *
44 device_finish (PycairoDevice *obj, PyObject *ignored) {
45 cairo_device_finish (obj->device);
46 RETURN_NULL_IF_CAIRO_DEVICE_ERROR(obj->device);
47 Py_RETURN_NONE;
48 }
49
50 static PyObject *
51 device_flush (PycairoDevice *obj, PyObject *ignored) {
52 cairo_device_flush (obj->device);
53 RETURN_NULL_IF_CAIRO_DEVICE_ERROR(obj->device);
54 Py_RETURN_NONE;
55 }
56
57 static PyObject *
58 device_acquire (PycairoDevice *obj, PyObject *ignored) {
59 cairo_status_t status;
60
61 Py_BEGIN_ALLOW_THREADS;
62 status = cairo_device_acquire (obj->device);
63 Py_END_ALLOW_THREADS;
64
65 RETURN_NULL_IF_CAIRO_ERROR (status);
66 Py_RETURN_NONE;
67 }
68
69 static PyObject *
70 device_release (PycairoDevice *obj, PyObject *ignored) {
71 cairo_device_release (obj->device);
72 Py_RETURN_NONE;
73 }
74
75 static PyObject *
76 device_ctx_enter (PyObject *obj, PyObject *ignored) {
77 Py_INCREF (obj);
78 return obj;
79 }
80
81 static PyObject *
82 device_ctx_exit (PycairoDevice *obj, PyObject *args) {
83 Py_BEGIN_ALLOW_THREADS;
84 cairo_device_finish (obj->device);
85 Py_END_ALLOW_THREADS;
86 Py_RETURN_NONE;
87 }
88
89 static PyMethodDef device_methods[] = {
90 {"__enter__", (PyCFunction)device_ctx_enter, METH_NOARGS},
91 {"__exit__", (PyCFunction)device_ctx_exit, METH_VARARGS},
92 {"finish", (PyCFunction)device_finish, METH_NOARGS},
93 {"flush", (PyCFunction)device_flush, METH_NOARGS},
94 {"acquire", (PyCFunction)device_acquire, METH_NOARGS},
95 {"release", (PyCFunction)device_release, METH_NOARGS},
96 {NULL, NULL, 0, NULL},
97 };
98
99 static PyObject *
100 device_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
101 PyErr_SetString(PyExc_TypeError,
102 "The Device type cannot be instantiated");
103 return NULL;
104 }
105
106 static PyObject*
107 device_richcompare (PyObject *self, PyObject *other, int op)
108 {
109 if (Py_TYPE(self) == Py_TYPE(other))
110 return Pycairo_richcompare (
111 ((PycairoDevice *)self)->device,
112 ((PycairoDevice *)other)->device,
113 op);
114 else {
115 Py_INCREF(Py_NotImplemented);
116 return Py_NotImplemented;
117 }
118 }
119
120 static Py_hash_t
121 device_hash (PyObject *self)
122 {
123 return PYCAIRO_Py_hash_t_FromVoidPtr (((PycairoDevice *)self)->device);
124 }
125
126 PyTypeObject PycairoDevice_Type = {
127 PyVarObject_HEAD_INIT(NULL, 0)
128 "cairo.Device", /* tp_name */
129 sizeof(PycairoDevice), /* tp_basicsize */
130 0, /* tp_itemsize */
131 (destructor)device_dealloc, /* tp_dealloc */
132 0, /* tp_print */
133 0, /* tp_getattr */
134 0, /* tp_setattr */
135 0, /* tp_compare */
136 0, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 device_hash, /* tp_hash */
141 0, /* tp_call */
142 0, /* tp_str */
143 0, /* tp_getattro */
144 0, /* tp_setattro */
145 0, /* tp_as_buffer */
146 Py_TPFLAGS_DEFAULT |
147 Py_TPFLAGS_BASETYPE, /* tp_flags */
148 0, /* tp_doc */
149 0, /* tp_traverse */
150 0, /* tp_clear */
151 device_richcompare, /* tp_richcompare */
152 0, /* tp_weaklistoffset */
153 0, /* tp_iter */
154 0, /* tp_iternext */
155 device_methods, /* tp_methods */
156 0, /* tp_members */
157 0, /* tp_getset */
158 0, /* tp_base */
159 0, /* tp_dict */
160 0, /* tp_descr_get */
161 0, /* tp_descr_set */
162 0, /* tp_dictoffset */
163 0, /* tp_init */
164 0, /* tp_alloc */
165 (newfunc)device_new, /* tp_new */
166 };
167
168 #ifdef CAIRO_HAS_SCRIPT_SURFACE
169 #include <cairo-script.h>
170
171 static const cairo_user_data_key_t device_base_object_key;
172
173 static void
174 _decref_destroy_func(void *user_data) {
175 PyGILState_STATE gstate = PyGILState_Ensure();
176 Py_DECREF(user_data);
177 PyGILState_Release(gstate);
178 }
179
180 static PyObject *
181 _device_create_with_object(cairo_device_t *device, PyObject *base) {
182 PyObject *pydevice;
183 cairo_status_t status;
184
185 pydevice = PycairoDevice_FromDevice(device);
186 if (pydevice == NULL)
187 return NULL;
188
189 if (base != NULL) {
190 status = cairo_device_set_user_data(
191 device, &device_base_object_key, base, _decref_destroy_func);
192 if (status != CAIRO_STATUS_SUCCESS)
193 Py_DECREF(pydevice);
194 RETURN_NULL_IF_CAIRO_ERROR(status);
195 Py_INCREF(base);
196 }
197
198 return pydevice;
199 }
200
201 static cairo_status_t
202 _write_func (void *closure, const unsigned char *data, unsigned int length) {
203 PyGILState_STATE gstate = PyGILState_Ensure();
204 PyObject *res = PyObject_CallMethod (
205 (PyObject *)closure, "write", "(y#)",
206 data, (Py_ssize_t)length);
207
208 if (res == NULL) {
209 PyErr_Clear();
210 /* an exception has occurred, it will be picked up later by
211 * Pycairo_Check_Status()
212 */
213 PyGILState_Release(gstate);
214 return CAIRO_STATUS_WRITE_ERROR;
215 }
216
217 Py_DECREF(res);
218 PyGILState_Release(gstate);
219 return CAIRO_STATUS_SUCCESS;
220 }
221
222 static PyObject *
223 script_device_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
224 char *name = NULL;
225 PyObject *file;
226 cairo_device_t *device;
227
228 if (!PyArg_ParseTuple (args, "O:ScriptDevice.__new__", &file))
229 return NULL;
230
231 if (Pycairo_is_fspath (file)) {
232 if (!PyArg_ParseTuple (args, "O&:ScriptDevice.__new__",
233 Pycairo_fspath_converter, &name))
234 return NULL;
235
236 Py_BEGIN_ALLOW_THREADS;
237 device = cairo_script_create (name);
238 Py_END_ALLOW_THREADS;
239 PyMem_Free (name);
240 return PycairoDevice_FromDevice (device);
241 } else {
242 if (PyArg_ParseTuple (args, "O&:ScriptDevice.__new__",
243 Pycairo_writer_converter, &file)) {
244 Py_BEGIN_ALLOW_THREADS;
245 device = cairo_script_create_for_stream (_write_func, file);
246 Py_END_ALLOW_THREADS;
247 return _device_create_with_object (device, file);
248 } else {
249 PyErr_Clear ();
250 PyErr_SetString (PyExc_TypeError,
251 "ScriptDevice takes one argument which must be "
252 "a filename, file object, or a file-like object "
253 "which has a \"write\" method (like BytesIO) taking bytes.");
254 return NULL;
255 }
256 }
257 }
258
259 static PyObject *
260 script_device_get_mode (PycairoDevice *obj, PyObject *ignored) {
261 RETURN_INT_ENUM (ScriptMode, cairo_script_get_mode (obj->device));
262 }
263
264 static PyObject *
265 script_device_set_mode (PycairoDevice *obj, PyObject *args) {
266 cairo_script_mode_t mode;
267 int mode_arg;
268
269 if (!PyArg_ParseTuple (args, "i:ScriptDevice.set_mode", &mode_arg))
270 return NULL;
271
272 mode = (cairo_script_mode_t)mode_arg;
273
274 Py_BEGIN_ALLOW_THREADS;
275 cairo_script_set_mode (obj->device, mode);
276 Py_END_ALLOW_THREADS;
277
278 RETURN_NULL_IF_CAIRO_DEVICE_ERROR (obj->device);
279 Py_RETURN_NONE;
280 }
281
282 static PyObject *
283 script_device_write_comment (PycairoDevice *obj, PyObject *args) {
284 const char *comment;
285
286 if (!PyArg_ParseTuple(args, "s:ScriptDevice.write_comment", &comment))
287 return NULL;
288
289 Py_BEGIN_ALLOW_THREADS;
290 cairo_script_write_comment (obj->device, comment, -1);
291 Py_END_ALLOW_THREADS;
292
293 RETURN_NULL_IF_CAIRO_DEVICE_ERROR(obj->device);
294 Py_RETURN_NONE;
295 }
296
297 #ifdef CAIRO_HAS_RECORDING_SURFACE
298 static PyObject *
299 script_device_from_recording_surface (PycairoDevice *obj, PyObject *args) {
300 PyObject *pysurface;
301 cairo_status_t status;
302
303 if (!PyArg_ParseTuple(args, "O!:ScriptDevice.from_recording_surface",
304 &PycairoRecordingSurface_Type, &pysurface))
305 return NULL;
306
307 Py_BEGIN_ALLOW_THREADS;
308 status = cairo_script_from_recording_surface (obj->device, ((PycairoRecordingSurface*)pysurface)->surface);
309 Py_END_ALLOW_THREADS;
310
311 RETURN_NULL_IF_CAIRO_ERROR(status);
312 Py_RETURN_NONE;
313 }
314 #endif
315
316 static PyMethodDef script_device_methods[] = {
317 {"get_mode", (PyCFunction)script_device_get_mode, METH_NOARGS},
318 {"set_mode", (PyCFunction)script_device_set_mode, METH_VARARGS},
319 {"write_comment", (PyCFunction)script_device_write_comment, METH_VARARGS},
320 #ifdef CAIRO_HAS_RECORDING_SURFACE
321 {"from_recording_surface",
322 (PyCFunction)script_device_from_recording_surface, METH_VARARGS},
323 #endif
324 {NULL, NULL, 0, NULL},
325 };
326
327 PyTypeObject PycairoScriptDevice_Type = {
328 PyVarObject_HEAD_INIT(NULL, 0)
329 "cairo.ScriptDevice", /* tp_name */
330 sizeof(PycairoScriptDevice), /* tp_basicsize */
331 0, /* tp_itemsize */
332 0, /* tp_dealloc */
333 0, /* tp_print */
334 0, /* tp_getattr */
335 0, /* tp_setattr */
336 0, /* tp_compare */
337 0, /* tp_repr */
338 0, /* tp_as_number */
339 0, /* tp_as_sequence */
340 0, /* tp_as_mapping */
341 0, /* tp_hash */
342 0, /* tp_call */
343 0, /* tp_str */
344 0, /* tp_getattro */
345 0, /* tp_setattro */
346 0, /* tp_as_buffer */
347 Py_TPFLAGS_DEFAULT, /* tp_flags */
348 0, /* tp_doc */
349 0, /* tp_traverse */
350 0, /* tp_clear */
351 0, /* tp_richcompare */
352 0, /* tp_weaklistoffset */
353 0, /* tp_iter */
354 0, /* tp_iternext */
355 script_device_methods, /* tp_methods */
356 0, /* tp_members */
357 0, /* tp_getset */
358 &PycairoDevice_Type, /* tp_base */
359 0, /* tp_dict */
360 0, /* tp_descr_get */
361 0, /* tp_descr_set */
362 0, /* tp_dictoffset */
363 0, /* tp_init */
364 0, /* tp_alloc */
365 (newfunc)script_device_new, /* tp_new */
366 0, /* tp_free */
367 0, /* tp_is_gc */
368 0, /* tp_bases */
369 };
370
371 #endif
372
373 PyObject *
374 PycairoDevice_FromDevice (cairo_device_t *device) {
375 PyObject *obj;
376 PyTypeObject *type;
377
378 assert (device != NULL);
379
380 if (Pycairo_Check_Status (cairo_device_status (device))) {
381 cairo_device_destroy (device);
382 return NULL;
383 }
384
385 switch (cairo_device_get_type (device)) {
386 #ifdef CAIRO_HAS_SCRIPT_SURFACE
387 case CAIRO_DEVICE_TYPE_SCRIPT:
388 type = &PycairoScriptDevice_Type;
389 break;
390 #endif
391 default:
392 type = &PycairoDevice_Type;
393 break;
394 }
395
396 obj = type->tp_alloc (type, 0);
397 if (obj) {
398 ((PycairoDevice *)obj)->device = device;
399 } else {
400 cairo_device_destroy (device);
401 }
402 return obj;
403 }