Debian Codegraph @

cairo/pattern.c

raw symbols SCIP
1 /* -*- mode: C; c-basic-offset: 2 -*-
2 *
3 * Pycairo - Python bindings for cairo
4 *
5 * Copyright © 2004-2006 Steve Chaplin
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
14 *
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
20 *
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
25 *
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
29 */
30
31 #define PY_SSIZE_T_CLEAN
32 #include <Python.h>
33
34 #include "private.h"
35
36
37 /* Class Pattern ---------------------------------------------------------- */
38
39 PyObject *
40 PycairoPattern_FromPattern (cairo_pattern_t *pattern, PyObject *base) {
41 PyTypeObject *type = NULL;
42 PyObject *o;
43
44 assert (pattern != NULL);
45
46 if (Pycairo_Check_Status (cairo_pattern_status (pattern))) {
47 cairo_pattern_destroy (pattern);
48 return NULL;
49 }
50
51 switch (cairo_pattern_get_type (pattern)) {
52 case CAIRO_PATTERN_TYPE_SOLID:
53 type = &PycairoSolidPattern_Type;
54 break;
55 case CAIRO_PATTERN_TYPE_SURFACE:
56 type = &PycairoSurfacePattern_Type;
57 break;
58 case CAIRO_PATTERN_TYPE_LINEAR:
59 type = &PycairoLinearGradient_Type;
60 break;
61 case CAIRO_PATTERN_TYPE_RADIAL:
62 type = &PycairoRadialGradient_Type;
63 break;
64 case CAIRO_PATTERN_TYPE_MESH:
65 type = &PycairoMeshPattern_Type;
66 break;
67 case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
68 type = &PycairoRasterSourcePattern_Type;
69 break;
70 default:
71 type = &PycairoPattern_Type;
72 break;
73 }
74
75 o = type->tp_alloc(type, 0);
76 if (o == NULL) {
77 cairo_pattern_destroy (pattern);
78 } else {
79 ((PycairoPattern *)o)->pattern = pattern;
80 Py_XINCREF(base);
81 ((PycairoPattern *)o)->base = base;
82 }
83 return o;
84 }
85
86 static void
87 pattern_dealloc (PycairoPattern *o) {
88 if (o->pattern) {
89 cairo_pattern_destroy (o->pattern);
90 o->pattern = NULL;
91 }
92 Py_CLEAR(o->base);
93
94 Py_TYPE(o)->tp_free(o);
95 }
96
97 static PyObject *
98 pattern_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
99 PyErr_SetString(PyExc_TypeError,
100 "The Pattern type cannot be instantiated");
101 return NULL;
102 }
103
104 static PyObject *
105 pattern_get_extend (PycairoPattern *o, PyObject *ignored) {
106 RETURN_INT_ENUM (Extend, cairo_pattern_get_extend (o->pattern));
107 }
108
109 static PyObject *
110 pattern_get_matrix (PycairoPattern *o, PyObject *ignored) {
111 cairo_matrix_t matrix;
112 cairo_pattern_get_matrix (o->pattern, &matrix);
113 return PycairoMatrix_FromMatrix (&matrix);
114 }
115
116 static PyObject *
117 pattern_set_extend (PycairoPattern *o, PyObject *args) {
118 cairo_extend_t extend;
119 int extend_arg;
120
121 if (!PyArg_ParseTuple (args, "i:Pattern.set_extend", &extend_arg))
122 return NULL;
123
124 extend = (cairo_extend_t)extend_arg;
125
126 cairo_pattern_set_extend (o->pattern, extend);
127 Py_RETURN_NONE;
128 }
129
130 static PyObject *
131 pattern_set_matrix (PycairoPattern *o, PyObject *args) {
132 PycairoMatrix *m;
133
134 if (!PyArg_ParseTuple (args, "O!:Pattern.set_matrix",
135 &PycairoMatrix_Type, &m))
136 return NULL;
137
138 cairo_pattern_set_matrix (o->pattern, &m->matrix);
139 Py_RETURN_NONE;
140 }
141
142 static PyObject *
143 pattern_get_filter (PycairoPattern *o, PyObject *ignored) {
144 cairo_filter_t filter;
145
146 Py_BEGIN_ALLOW_THREADS;
147 filter = cairo_pattern_get_filter (o->pattern);
148 Py_END_ALLOW_THREADS;
149
150 RETURN_INT_ENUM (Filter, filter);
151 }
152
153 static PyObject *
154 pattern_set_filter (PycairoPattern *o, PyObject *args) {
155 cairo_filter_t filter;
156 int filter_arg;
157
158 if (!PyArg_ParseTuple (args, "i:Pattern.set_filter", &filter_arg))
159 return NULL;
160
161 filter = (cairo_filter_t)filter_arg;
162
163 Py_BEGIN_ALLOW_THREADS;
164 cairo_pattern_set_filter (o->pattern, filter);
165 Py_END_ALLOW_THREADS;
166
167 Py_RETURN_NONE;
168 }
169
170 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 18, 0)
171 static PyObject *
172 pattern_get_dither (PycairoPattern *o, PyObject *ignored) {
173 cairo_dither_t dither;
174
175 Py_BEGIN_ALLOW_THREADS;
176 dither = cairo_pattern_get_dither (o->pattern);
177 Py_END_ALLOW_THREADS;
178
179 RETURN_INT_ENUM (Dither, dither);
180 }
181
182 static PyObject *
183 pattern_set_dither (PycairoPattern *o, PyObject *args) {
184 cairo_dither_t dither;
185 int dither_arg;
186
187 if (!PyArg_ParseTuple (args, "i:Pattern.set_dither", &dither_arg))
188 return NULL;
189
190 dither = (cairo_dither_t)dither_arg;
191
192 Py_BEGIN_ALLOW_THREADS;
193 cairo_pattern_set_dither (o->pattern, dither);
194 Py_END_ALLOW_THREADS;
195
196 Py_RETURN_NONE;
197 }
198 #endif
199
200 static PyMethodDef pattern_methods[] = {
201 /* methods never exposed in a language binding:
202 * cairo_pattern_destroy()
203 * cairo_pattern_get_type()
204 * cairo_pattern_reference()
205 *
206 * cairo_pattern_status()
207 * - not needed since Pycairo handles status checking
208 */
209 {"get_extend", (PyCFunction)pattern_get_extend, METH_NOARGS },
210 {"get_matrix", (PyCFunction)pattern_get_matrix, METH_NOARGS },
211 {"set_extend", (PyCFunction)pattern_set_extend, METH_VARARGS },
212 {"set_matrix", (PyCFunction)pattern_set_matrix, METH_VARARGS },
213 {"get_filter", (PyCFunction)pattern_get_filter, METH_NOARGS },
214 {"set_filter", (PyCFunction)pattern_set_filter, METH_VARARGS },
215 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 18, 0)
216 {"get_dither", (PyCFunction)pattern_get_dither, METH_NOARGS },
217 {"set_dither", (PyCFunction)pattern_set_dither, METH_VARARGS },
218 #endif
219 {NULL, NULL, 0, NULL},
220 };
221
222 static PyObject*
223 pattern_richcompare (PyObject *self, PyObject *other, int op)
224 {
225 if (Py_TYPE(self) == Py_TYPE(other))
226 return Pycairo_richcompare (
227 ((PycairoPattern *)self)->pattern,
228 ((PycairoPattern *)other)->pattern,
229 op);
230 else {
231 Py_INCREF(Py_NotImplemented);
232 return Py_NotImplemented;
233 }
234 }
235
236 static Py_hash_t
237 pattern_hash (PyObject *self)
238 {
239 return PYCAIRO_Py_hash_t_FromVoidPtr (((PycairoPattern *)self)->pattern);
240 }
241
242 PyTypeObject PycairoPattern_Type = {
243 PyVarObject_HEAD_INIT(NULL, 0)
244 "cairo.Pattern", /* tp_name */
245 sizeof(PycairoPattern), /* tp_basicsize */
246 0, /* tp_itemsize */
247 (destructor)pattern_dealloc, /* tp_dealloc */
248 0, /* tp_print */
249 0, /* tp_getattr */
250 0, /* tp_setattr */
251 0, /* tp_compare */
252 0, /* tp_repr */
253 0, /* tp_as_number */
254 0, /* tp_as_sequence */
255 0, /* tp_as_mapping */
256 pattern_hash, /* tp_hash */
257 0, /* tp_call */
258 0, /* tp_str */
259 0, /* tp_getattro */
260 0, /* tp_setattro */
261 0, /* tp_as_buffer */
262 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
263 0, /* tp_doc */
264 0, /* tp_traverse */
265 0, /* tp_clear */
266 pattern_richcompare, /* tp_richcompare */
267 0, /* tp_weaklistoffset */
268 0, /* tp_iter */
269 0, /* tp_iternext */
270 pattern_methods, /* tp_methods */
271 0, /* tp_members */
272 0, /* tp_getset */
273 0, /* tp_base */
274 0, /* tp_dict */
275 0, /* tp_descr_get */
276 0, /* tp_descr_set */
277 0, /* tp_dictoffset */
278 0, /* tp_init */
279 0, /* tp_alloc */
280 (newfunc)pattern_new, /* tp_new */
281 0, /* tp_free */
282 0, /* tp_is_gc */
283 0, /* tp_bases */
284 };
285
286
287 /* Class SolidPattern ----------------------------------------------------- */
288
289 static PyObject *
290 solid_pattern_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
291 double r, g, b, a = 1.0;
292 if (!PyArg_ParseTuple (args, "ddd|d:SolidPattern.__new__", &r, &g, &b, &a))
293 return NULL;
294 return PycairoPattern_FromPattern (cairo_pattern_create_rgba (r, g, b, a),
295 NULL);
296 }
297
298 static PyObject *
299 solid_pattern_get_rgba (PycairoSolidPattern *o, PyObject *ignored) {
300 double red, green, blue, alpha;
301 cairo_pattern_get_rgba (o->pattern, &red, &green, &blue, &alpha);
302 return Py_BuildValue("(dddd)", red, green, blue, alpha);
303 }
304
305 static PyMethodDef solid_pattern_methods[] = {
306 {"get_rgba", (PyCFunction)solid_pattern_get_rgba, METH_NOARGS },
307 {NULL, NULL, 0, NULL},
308 };
309
310 PyTypeObject PycairoSolidPattern_Type = {
311 PyVarObject_HEAD_INIT(NULL, 0)
312 "cairo.SolidPattern", /* tp_name */
313 sizeof(PycairoSolidPattern), /* tp_basicsize */
314 0, /* tp_itemsize */
315 0, /* tp_dealloc */
316 0, /* tp_print */
317 0, /* tp_getattr */
318 0, /* tp_setattr */
319 0, /* tp_compare */
320 0, /* tp_repr */
321 0, /* tp_as_number */
322 0, /* tp_as_sequence */
323 0, /* tp_as_mapping */
324 0, /* tp_hash */
325 0, /* tp_call */
326 0, /* tp_str */
327 0, /* tp_getattro */
328 0, /* tp_setattro */
329 0, /* tp_as_buffer */
330 Py_TPFLAGS_DEFAULT, /* tp_flags */
331 0, /* tp_doc */
332 0, /* tp_traverse */
333 0, /* tp_clear */
334 0, /* tp_richcompare */
335 0, /* tp_weaklistoffset */
336 0, /* tp_iter */
337 0, /* tp_iternext */
338 solid_pattern_methods, /* tp_methods */
339 0, /* tp_members */
340 0, /* tp_getset */
341 &PycairoPattern_Type, /* tp_base */
342 0, /* tp_dict */
343 0, /* tp_descr_get */
344 0, /* tp_descr_set */
345 0, /* tp_dictoffset */
346 0, /* tp_init */
347 0, /* tp_alloc */
348 (newfunc)solid_pattern_new, /* tp_new */
349 0, /* tp_free */
350 0, /* tp_is_gc */
351 0, /* tp_bases */
352 };
353
354 /* Class SurfacePattern --------------------------------------------------- */
355
356 static PyObject *
357 surface_pattern_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
358 PycairoSurface *s;
359 if (!PyArg_ParseTuple (args, "O!:SurfacePattern.__new__",
360 &PycairoSurface_Type, &s))
361 return NULL;
362 return PycairoPattern_FromPattern (
363 cairo_pattern_create_for_surface (s->surface), NULL);
364 }
365
366 static PyObject *
367 surface_pattern_get_surface (PycairoSurfacePattern *o, PyObject *ignored) {
368 cairo_surface_t *surface;
369 RETURN_NULL_IF_CAIRO_ERROR(cairo_pattern_get_surface (o->pattern, &surface));
370 return PycairoSurface_FromSurface(cairo_surface_reference (surface), NULL);
371 }
372
373 static PyMethodDef surface_pattern_methods[] = {
374 {"get_surface", (PyCFunction)surface_pattern_get_surface, METH_NOARGS },
375 {NULL, NULL, 0, NULL},
376 };
377
378 PyTypeObject PycairoSurfacePattern_Type = {
379 PyVarObject_HEAD_INIT(NULL, 0)
380 "cairo.SurfacePattern", /* tp_name */
381 sizeof(PycairoSurfacePattern), /* tp_basicsize */
382 0, /* tp_itemsize */
383 0, /* tp_dealloc */
384 0, /* tp_print */
385 0, /* tp_getattr */
386 0, /* tp_setattr */
387 0, /* tp_compare */
388 0, /* tp_repr */
389 0, /* tp_as_number */
390 0, /* tp_as_sequence */
391 0, /* tp_as_mapping */
392 0, /* tp_hash */
393 0, /* tp_call */
394 0, /* tp_str */
395 0, /* tp_getattro */
396 0, /* tp_setattro */
397 0, /* tp_as_buffer */
398 Py_TPFLAGS_DEFAULT, /* tp_flags */
399 0, /* tp_doc */
400 0, /* tp_traverse */
401 0, /* tp_clear */
402 0, /* tp_richcompare */
403 0, /* tp_weaklistoffset */
404 0, /* tp_iter */
405 0, /* tp_iternext */
406 surface_pattern_methods, /* tp_methods */
407 0, /* tp_members */
408 0, /* tp_getset */
409 &PycairoPattern_Type, /* tp_base */
410 0, /* tp_dict */
411 0, /* tp_descr_get */
412 0, /* tp_descr_set */
413 0, /* tp_dictoffset */
414 0, /* tp_init */
415 0, /* tp_alloc */
416 (newfunc)surface_pattern_new, /* tp_new */
417 0, /* tp_free */
418 0, /* tp_is_gc */
419 0, /* tp_bases */
420 };
421
422 /* Class Gradient --------------------------------------------------------- */
423
424 static PyObject *
425 gradient_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
426 PyErr_SetString(PyExc_TypeError,
427 "The Gradient type cannot be instantiated");
428 return NULL;
429 }
430
431 static PyObject *
432 gradient_add_color_stop_rgb (PycairoGradient *o, PyObject *args) {
433 double offset, red, green, blue;
434 if (!PyArg_ParseTuple(args, "dddd:Gradient.add_color_stop_rgb",
435 &offset, &red, &green, &blue))
436 return NULL;
437 cairo_pattern_add_color_stop_rgb (o->pattern, offset, red, green, blue);
438 RETURN_NULL_IF_CAIRO_PATTERN_ERROR(o->pattern);
439 Py_RETURN_NONE;
440 }
441
442 static PyObject *
443 gradient_add_color_stop_rgba (PycairoGradient *o, PyObject *args) {
444 double offset, red, green, blue, alpha;
445 if (!PyArg_ParseTuple(args, "ddddd:Gradient.add_color_stop_rgba",
446 &offset, &red, &green, &blue, &alpha))
447 return NULL;
448 cairo_pattern_add_color_stop_rgba (o->pattern, offset, red,
449 green, blue, alpha);
450 RETURN_NULL_IF_CAIRO_PATTERN_ERROR(o->pattern);
451 Py_RETURN_NONE;
452 }
453
454 static PyObject *
455 gradient_get_color_stops_rgba (PycairoGradient *obj, PyObject *ignored) {
456 cairo_status_t status;
457 double offset, red, green, blue, alpha;
458 int count;
459 PyObject *list, *tuple;
460 int result, i;
461
462 status = cairo_pattern_get_color_stop_count (obj->pattern, &count);
463 RETURN_NULL_IF_CAIRO_ERROR (status);
464
465 list = PyList_New (0);
466 if (list == NULL)
467 return NULL;
468
469 for (i = 0; i < count; i++) {
470 status = cairo_pattern_get_color_stop_rgba (
471 obj->pattern, i, &offset, &red, &green, &blue, &alpha);
472 if (status != CAIRO_STATUS_SUCCESS)
473 goto error;
474
475 tuple = Py_BuildValue("(ddddd)", offset, red, green, blue, alpha);
476 if (tuple == NULL)
477 goto error;
478
479 result = PyList_Append (list, tuple);
480 Py_DECREF (tuple);
481 if (result == -1)
482 goto error;
483 }
484
485 return list;
486 error:
487 Py_DECREF (list);
488 if (status != CAIRO_STATUS_SUCCESS)
489 Pycairo_Check_Status (status);
490 return NULL;
491 }
492
493 static PyMethodDef gradient_methods[] = {
494 {"add_color_stop_rgb",(PyCFunction)gradient_add_color_stop_rgb,
495 METH_VARARGS },
496 {"add_color_stop_rgba",(PyCFunction)gradient_add_color_stop_rgba,
497 METH_VARARGS },
498 {"get_color_stops_rgba",(PyCFunction)gradient_get_color_stops_rgba,
499 METH_NOARGS },
500 {NULL, NULL, 0, NULL},
501 };
502
503 PyTypeObject PycairoGradient_Type = {
504 PyVarObject_HEAD_INIT(NULL, 0)
505 "cairo.Gradient", /* tp_name */
506 sizeof(PycairoGradient), /* tp_basicsize */
507 0, /* tp_itemsize */
508 0, /* tp_dealloc */
509 0, /* tp_print */
510 0, /* tp_getattr */
511 0, /* tp_setattr */
512 0, /* tp_compare */
513 0, /* tp_repr */
514 0, /* tp_as_number */
515 0, /* tp_as_sequence */
516 0, /* tp_as_mapping */
517 0, /* tp_hash */
518 0, /* tp_call */
519 0, /* tp_str */
520 0, /* tp_getattro */
521 0, /* tp_setattro */
522 0, /* tp_as_buffer */
523 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
524 0, /* tp_doc */
525 0, /* tp_traverse */
526 0, /* tp_clear */
527 0, /* tp_richcompare */
528 0, /* tp_weaklistoffset */
529 0, /* tp_iter */
530 0, /* tp_iternext */
531 gradient_methods, /* tp_methods */
532 0, /* tp_members */
533 0, /* tp_getset */
534 &PycairoPattern_Type, /* tp_base */
535 0, /* tp_dict */
536 0, /* tp_descr_get */
537 0, /* tp_descr_set */
538 0, /* tp_dictoffset */
539 0, /* tp_init */
540 0, /* tp_alloc */
541 (newfunc)gradient_new, /* tp_new */
542 0, /* tp_free */
543 0, /* tp_is_gc */
544 0, /* tp_bases */
545 };
546
547 /* Class LinearGradient --------------------------------------------------- */
548
549 static PyObject *
550 linear_gradient_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
551 double x0, y0, x1, y1;
552 if (!PyArg_ParseTuple(args, "dddd:LinearGradient.__new__",
553 &x0, &y0, &x1, &y1))
554 return NULL;
555 return PycairoPattern_FromPattern (
556 cairo_pattern_create_linear (x0, y0, x1, y1), NULL);
557 }
558
559 static PyObject *
560 linear_gradient_get_linear_points (PycairoLinearGradient *o, PyObject *ignored) {
561 double x0, y0, x1, y1;
562 cairo_pattern_get_linear_points (o->pattern, &x0, &y0, &x1, &y1);
563 return Py_BuildValue("(dddd)", x0, y0, x1, y1);
564 }
565
566 static PyMethodDef linear_gradient_methods[] = {
567 {"get_linear_points", (PyCFunction)linear_gradient_get_linear_points,
568 METH_NOARGS },
569 {NULL, NULL, 0, NULL},
570 };
571
572 PyTypeObject PycairoLinearGradient_Type = {
573 PyVarObject_HEAD_INIT(NULL, 0)
574 "cairo.LinearGradient", /* tp_name */
575 sizeof(PycairoLinearGradient), /* tp_basicsize */
576 0, /* tp_itemsize */
577 0, /* tp_dealloc */
578 0, /* tp_print */
579 0, /* tp_getattr */
580 0, /* tp_setattr */
581 0, /* tp_compare */
582 0, /* tp_repr */
583 0, /* tp_as_number */
584 0, /* tp_as_sequence */
585 0, /* tp_as_mapping */
586 0, /* tp_hash */
587 0, /* tp_call */
588 0, /* tp_str */
589 0, /* tp_getattro */
590 0, /* tp_setattro */
591 0, /* tp_as_buffer */
592 Py_TPFLAGS_DEFAULT, /* tp_flags */
593 0, /* tp_doc */
594 0, /* tp_traverse */
595 0, /* tp_clear */
596 0, /* tp_richcompare */
597 0, /* tp_weaklistoffset */
598 0, /* tp_iter */
599 0, /* tp_iternext */
600 linear_gradient_methods, /* tp_methods */
601 0, /* tp_members */
602 0, /* tp_getset */
603 &PycairoGradient_Type, /* tp_base */
604 0, /* tp_dict */
605 0, /* tp_descr_get */
606 0, /* tp_descr_set */
607 0, /* tp_dictoffset */
608 0, /* tp_init */
609 0, /* tp_alloc */
610 (newfunc)linear_gradient_new, /* tp_new */
611 0, /* tp_free */
612 0, /* tp_is_gc */
613 0, /* tp_bases */
614 };
615
616 /* Class RadialGradient --------------------------------------------------- */
617
618 static PyObject *
619 radial_gradient_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
620 double cx0, cy0, radius0, cx1, cy1, radius1;
621 if (!PyArg_ParseTuple(args, "dddddd:RadialGradient.__new__",
622 &cx0, &cy0, &radius0, &cx1, &cy1, &radius1))
623 return NULL;
624 return PycairoPattern_FromPattern (
625 cairo_pattern_create_radial (cx0, cy0, radius0, cx1, cy1, radius1),
626 NULL);
627 }
628
629 static PyObject *
630 radial_gradient_get_radial_circles (PycairoRadialGradient *o, PyObject *ignored) {
631 double x0, y0, r0, x1, y1, r1;
632 cairo_pattern_get_radial_circles (o->pattern, &x0, &y0, &r0,
633 &x1, &y1, &r1);
634 return Py_BuildValue("(dddddd)", x0, y0, r0, x1, y1, r1);
635 }
636
637 static PyMethodDef radial_gradient_methods[] = {
638 {"get_radial_circles", (PyCFunction)radial_gradient_get_radial_circles,
639 METH_NOARGS },
640 {NULL, NULL, 0, NULL},
641 };
642
643 PyTypeObject PycairoRadialGradient_Type = {
644 PyVarObject_HEAD_INIT(NULL, 0)
645 "cairo.RadialGradient", /* tp_name */
646 sizeof(PycairoRadialGradient), /* tp_basicsize */
647 0, /* tp_itemsize */
648 0, /* tp_dealloc */
649 0, /* tp_print */
650 0, /* tp_getattr */
651 0, /* tp_setattr */
652 0, /* tp_compare */
653 0, /* tp_repr */
654 0, /* tp_as_number */
655 0, /* tp_as_sequence */
656 0, /* tp_as_mapping */
657 0, /* tp_hash */
658 0, /* tp_call */
659 0, /* tp_str */
660 0, /* tp_getattro */
661 0, /* tp_setattro */
662 0, /* tp_as_buffer */
663 Py_TPFLAGS_DEFAULT, /* tp_flags */
664 0, /* tp_doc */
665 0, /* tp_traverse */
666 0, /* tp_clear */
667 0, /* tp_richcompare */
668 0, /* tp_weaklistoffset */
669 0, /* tp_iter */
670 0, /* tp_iternext */
671 radial_gradient_methods, /* tp_methods */
672 0, /* tp_members */
673 0, /* tp_getset */
674 &PycairoGradient_Type, /* tp_base */
675 0, /* tp_dict */
676 0, /* tp_descr_get */
677 0, /* tp_descr_set */
678 0, /* tp_dictoffset */
679 0, /* tp_init */
680 0, /* tp_alloc */
681 (newfunc)radial_gradient_new, /* tp_new */
682 0, /* tp_free */
683 0, /* tp_is_gc */
684 0, /* tp_bases */
685 };
686
687 static PyObject *
688 mesh_pattern_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
689 if (!PyArg_ParseTuple (args, ":Mesh.__new__"))
690 return NULL;
691
692 return PycairoPattern_FromPattern (cairo_pattern_create_mesh (), NULL);
693 }
694
695 static PyObject *
696 mesh_pattern_begin_patch (PycairoMeshPattern *obj, PyObject *ignored) {
697 Py_BEGIN_ALLOW_THREADS;
698 cairo_mesh_pattern_begin_patch (obj->pattern);
699 Py_END_ALLOW_THREADS;
700
701 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
702
703 Py_RETURN_NONE;
704 }
705
706 static PyObject *
707 mesh_pattern_end_patch (PycairoMeshPattern *obj, PyObject *ignored) {
708 Py_BEGIN_ALLOW_THREADS;
709 cairo_mesh_pattern_end_patch (obj->pattern);
710 Py_END_ALLOW_THREADS;
711
712 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
713
714 Py_RETURN_NONE;
715 }
716
717 static PyObject *
718 mesh_pattern_curve_to (PycairoMeshPattern *obj, PyObject *args) {
719 double x1, y1, x2, y2, x3, y3;
720
721 if (!PyArg_ParseTuple(args, "dddddd:MeshPattern.curve_to",
722 &x1, &y1, &x2, &y2, &x3, &y3))
723 return NULL;
724
725 Py_BEGIN_ALLOW_THREADS;
726 cairo_mesh_pattern_curve_to (obj->pattern, x1, y1, x2, y2, x3, y3);
727 Py_END_ALLOW_THREADS;
728
729 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
730
731 Py_RETURN_NONE;
732 }
733
734 static PyObject *
735 mesh_pattern_get_control_point (PycairoMeshPattern *obj, PyObject *args) {
736 double x, y;
737 unsigned int patch_num, point_num;
738 cairo_status_t status;
739
740 if (!PyArg_ParseTuple(args, "II:MeshPattern.get_control_point",
741 &patch_num, &point_num))
742 return NULL;
743
744 Py_BEGIN_ALLOW_THREADS;
745 status = cairo_mesh_pattern_get_control_point (
746 obj->pattern, patch_num, point_num, &x, &y);
747 Py_END_ALLOW_THREADS;
748
749 RETURN_NULL_IF_CAIRO_ERROR (status);
750
751 return Py_BuildValue("(dd)", x, y);
752 }
753
754 static PyObject *
755 mesh_pattern_get_corner_color_rgba (PycairoMeshPattern *obj, PyObject *args) {
756 double red, green, blue, alpha;
757 unsigned int patch_num, corner_num;
758 cairo_status_t status;
759
760 if (!PyArg_ParseTuple(args, "II:MeshPattern.get_corner_color_rgba",
761 &patch_num, &corner_num))
762 return NULL;
763
764 Py_BEGIN_ALLOW_THREADS;
765 status = cairo_mesh_pattern_get_corner_color_rgba (
766 obj->pattern, patch_num, corner_num, &red, &green, &blue, &alpha);
767 Py_END_ALLOW_THREADS;
768
769 RETURN_NULL_IF_CAIRO_ERROR (status);
770
771 return Py_BuildValue("(dddd)", red, green, blue, alpha);
772 }
773
774 static PyObject *
775 mesh_pattern_get_patch_count (PycairoMeshPattern *obj, PyObject *ignored) {
776 unsigned int count;
777 cairo_status_t status;
778
779 Py_BEGIN_ALLOW_THREADS;
780 status = cairo_mesh_pattern_get_patch_count (obj->pattern, &count);
781 Py_END_ALLOW_THREADS;
782
783 RETURN_NULL_IF_CAIRO_ERROR (status);
784
785 return PyLong_FromLong ((long)count);
786 }
787
788 static PyObject *
789 mesh_pattern_get_path (PycairoMeshPattern *obj, PyObject *args) {
790 unsigned int patch_num;
791 cairo_path_t *path;
792
793 if (!PyArg_ParseTuple(args, "I:MeshPattern.get_path", &patch_num))
794 return NULL;
795
796 Py_BEGIN_ALLOW_THREADS;
797 path = cairo_mesh_pattern_get_path (obj->pattern, patch_num);
798 Py_END_ALLOW_THREADS;
799
800 return PycairoPath_FromPath (path);
801 }
802
803 static PyObject *
804 mesh_pattern_line_to (PycairoMeshPattern *obj, PyObject *args) {
805 double x, y;
806
807 if (!PyArg_ParseTuple(args, "dd:MeshPattern.line_to", &x, &y))
808 return NULL;
809
810 Py_BEGIN_ALLOW_THREADS;
811 cairo_mesh_pattern_line_to (obj->pattern, x, y);
812 Py_END_ALLOW_THREADS;
813
814 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
815
816 Py_RETURN_NONE;
817 }
818
819 static PyObject *
820 mesh_pattern_move_to (PycairoMeshPattern *obj, PyObject *args) {
821 double x, y;
822
823 if (!PyArg_ParseTuple(args, "dd:MeshPattern.move_to", &x, &y))
824 return NULL;
825
826 Py_BEGIN_ALLOW_THREADS;
827 cairo_mesh_pattern_move_to (obj->pattern, x, y);
828 Py_END_ALLOW_THREADS;
829
830 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
831
832 Py_RETURN_NONE;
833 }
834
835 static PyObject *
836 mesh_pattern_set_control_point (PycairoMeshPattern *obj, PyObject *args) {
837 double x, y;
838 unsigned int point_num;
839
840 if (!PyArg_ParseTuple(args, "Idd:MeshPattern.set_control_point",
841 &point_num, &x, &y))
842 return NULL;
843
844 Py_BEGIN_ALLOW_THREADS;
845 cairo_mesh_pattern_set_control_point (obj->pattern, point_num, x, y);
846 Py_END_ALLOW_THREADS;
847
848 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
849
850 Py_RETURN_NONE;
851 }
852
853 static PyObject *
854 mesh_pattern_set_corner_color_rgb (PycairoMeshPattern *obj, PyObject *args) {
855 double red, green, blue;
856 unsigned int corner_num;
857
858 if (!PyArg_ParseTuple(args, "Iddd:MeshPattern.set_corner_color_rgb",
859 &corner_num, &red, &green, &blue))
860 return NULL;
861
862 Py_BEGIN_ALLOW_THREADS;
863 cairo_mesh_pattern_set_corner_color_rgb (
864 obj->pattern, corner_num, red, green, blue);
865 Py_END_ALLOW_THREADS;
866
867 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
868
869 Py_RETURN_NONE;
870 }
871
872 static PyObject *
873 mesh_pattern_set_corner_color_rgba (PycairoMeshPattern *obj, PyObject *args) {
874 double red, green, blue, alpha;
875 unsigned int corner_num;
876
877 if (!PyArg_ParseTuple(args, "Idddd:MeshPattern.set_corner_color_rgba",
878 &corner_num, &red, &green, &blue, &alpha))
879 return NULL;
880
881 Py_BEGIN_ALLOW_THREADS;
882 cairo_mesh_pattern_set_corner_color_rgba (
883 obj->pattern, corner_num, red, green, blue, alpha);
884 Py_END_ALLOW_THREADS;
885
886 RETURN_NULL_IF_CAIRO_PATTERN_ERROR (obj->pattern);
887
888 Py_RETURN_NONE;
889 }
890
891 static PyMethodDef mesh_pattern_methods[] = {
892 {"begin_patch", (PyCFunction)mesh_pattern_begin_patch, METH_NOARGS},
893 {"end_patch", (PyCFunction)mesh_pattern_end_patch, METH_NOARGS},
894 {"curve_to", (PyCFunction)mesh_pattern_curve_to, METH_VARARGS},
895 {"get_control_point",
896 (PyCFunction)mesh_pattern_get_control_point, METH_VARARGS},
897 {"get_corner_color_rgba",
898 (PyCFunction)mesh_pattern_get_corner_color_rgba, METH_VARARGS},
899 {"get_patch_count",
900 (PyCFunction)mesh_pattern_get_patch_count, METH_NOARGS},
901 {"get_path", (PyCFunction)mesh_pattern_get_path, METH_VARARGS},
902 {"line_to", (PyCFunction)mesh_pattern_line_to, METH_VARARGS},
903 {"move_to", (PyCFunction)mesh_pattern_move_to, METH_VARARGS},
904 {"set_control_point",
905 (PyCFunction)mesh_pattern_set_control_point, METH_VARARGS},
906 {"set_corner_color_rgb",
907 (PyCFunction)mesh_pattern_set_corner_color_rgb, METH_VARARGS},
908 {"set_corner_color_rgba",
909 (PyCFunction)mesh_pattern_set_corner_color_rgba, METH_VARARGS},
910 {NULL, NULL, 0, NULL},
911 };
912
913 PyTypeObject PycairoMeshPattern_Type = {
914 PyVarObject_HEAD_INIT(NULL, 0)
915 "cairo.MeshPattern", /* tp_name */
916 sizeof(PycairoMeshPattern), /* tp_basicsize */
917 0, /* tp_itemsize */
918 0, /* tp_dealloc */
919 0, /* tp_print */
920 0, /* tp_getattr */
921 0, /* tp_setattr */
922 0, /* tp_compare */
923 0, /* tp_repr */
924 0, /* tp_as_number */
925 0, /* tp_as_sequence */
926 0, /* tp_as_mapping */
927 0, /* tp_hash */
928 0, /* tp_call */
929 0, /* tp_str */
930 0, /* tp_getattro */
931 0, /* tp_setattro */
932 0, /* tp_as_buffer */
933 Py_TPFLAGS_DEFAULT, /* tp_flags */
934 0, /* tp_doc */
935 0, /* tp_traverse */
936 0, /* tp_clear */
937 0, /* tp_richcompare */
938 0, /* tp_weaklistoffset */
939 0, /* tp_iter */
940 0, /* tp_iternext */
941 mesh_pattern_methods, /* tp_methods */
942 0, /* tp_members */
943 0, /* tp_getset */
944 &PycairoPattern_Type, /* tp_base */
945 0, /* tp_dict */
946 0, /* tp_descr_get */
947 0, /* tp_descr_set */
948 0, /* tp_dictoffset */
949 0, /* tp_init */
950 0, /* tp_alloc */
951 (newfunc)mesh_pattern_new, /* tp_new */
952 0, /* tp_free */
953 0, /* tp_is_gc */
954 0, /* tp_bases */
955 };
956
957
958 static PyObject *
959 raster_source_pattern_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
960 cairo_content_t content;
961 int width, height, content_arg;
962
963 if (!PyArg_ParseTuple (args, "iii:RasterSourcePattern.__new__",
964 &content_arg, &width, &height))
965 return NULL;
966
967 content = (cairo_content_t)content_arg;
968
969 return PycairoPattern_FromPattern (
970 cairo_pattern_create_raster_source (NULL, content, width, height), NULL);
971 }
972
973 static const cairo_user_data_key_t raster_source_acquire_key;
974 static const cairo_user_data_key_t raster_source_release_key;
975
976 static void
977 _decref_destroy_func (void *user_data) {
978 PyGILState_STATE gstate = PyGILState_Ensure ();
979 Py_DECREF (user_data);
980 PyGILState_Release (gstate);
981 }
982
983 static void
984 _raster_source_release_func (cairo_pattern_t *pattern,
985 void *callback_data,
986 cairo_surface_t *surface) {
987 void *user_data;
988 PyObject *pysurface, *result;
989 PyGILState_STATE gstate;
990
991 pattern = (cairo_pattern_t *)callback_data;
992
993 user_data = cairo_pattern_get_user_data (
994 pattern, &raster_source_release_key);
995 /* in case there is a acquire callback but no release one, this gets called
996 * anyway so we can free the surface
997 */
998 if (user_data == NULL) {
999 cairo_surface_destroy (surface);
1000 return;
1001 }
1002
1003 gstate = PyGILState_Ensure ();
1004
1005 pysurface = PycairoSurface_FromSurface (
1006 cairo_surface_reference (surface), NULL);
1007 if (pysurface == NULL)
1008 goto error;
1009
1010 result = PyObject_CallFunction ((PyObject *)user_data, "(O)", pysurface);
1011 if (result == NULL)
1012 goto error;
1013
1014 if (result != Py_None) {
1015 Py_DECREF (result);
1016 PyErr_SetString (PyExc_TypeError,
1017 "Return value of release callback needs to be None");
1018 result = NULL;
1019 goto error;
1020 }
1021
1022 Py_DECREF (pysurface);
1023 PyGILState_Release (gstate);
1024 cairo_surface_destroy (surface);
1025 return;
1026 error:
1027 if (PyErr_Occurred ()) {
1028 PyErr_Print ();
1029 PyErr_Clear ();
1030 }
1031 Py_XDECREF (pysurface);
1032 PyGILState_Release (gstate);
1033 cairo_surface_destroy (surface);
1034 return;
1035 }
1036
1037 static cairo_surface_t*
1038 _raster_source_acquire_func (cairo_pattern_t *pattern, void *callback_data,
1039 cairo_surface_t *target,
1040 const cairo_rectangle_int_t *extents) {
1041 void *user_data;
1042 PyGILState_STATE gstate;
1043 PyObject *result;
1044 PyObject *pysurface = NULL, *pyrect = NULL;
1045 cairo_surface_t *result_surface;
1046
1047 /* https://bugs.freedesktop.org/show_bug.cgi?id=101866
1048 * If something changes the callback data, we are screwed, but not much
1049 * we can do to detect that..
1050 */
1051 pattern = (cairo_pattern_t *)callback_data;
1052
1053 gstate = PyGILState_Ensure ();
1054
1055 user_data = cairo_pattern_get_user_data (
1056 pattern, &raster_source_acquire_key);
1057 if (user_data == NULL)
1058 goto error;
1059
1060 pysurface = PycairoSurface_FromSurface (
1061 cairo_surface_reference (target), NULL);
1062 if (pysurface == NULL)
1063 goto error;
1064
1065 pyrect = PycairoRectangleInt_FromRectangleInt (extents);
1066 if (pyrect == NULL)
1067 goto error;
1068
1069 result = PyObject_CallFunction (
1070 (PyObject *)user_data, "(OO)", pysurface, pyrect);
1071
1072 if (result != NULL) {
1073 if (!PyObject_TypeCheck (result, &PycairoSurface_Type)) {
1074 Py_DECREF (result);
1075 PyErr_SetString (PyExc_TypeError,
1076 "Return value of acquire callback needs to be of type Surface");
1077 result = NULL;
1078 }
1079 }
1080
1081 if (result == NULL)
1082 goto error;
1083
1084 Py_DECREF (pysurface);
1085 Py_DECREF (pyrect);
1086 result_surface = ((PycairoSurface *)result)->surface;
1087 cairo_surface_reference (result_surface);
1088 Py_DECREF (result);
1089 PyGILState_Release (gstate);
1090 return result_surface;
1091
1092 error:
1093 if (PyErr_Occurred ()) {
1094 PyErr_Print ();
1095 PyErr_Clear ();
1096 }
1097 Py_XDECREF (pysurface);
1098 Py_XDECREF (pyrect);
1099 PyGILState_Release (gstate);
1100 return NULL;
1101 }
1102
1103 static PyObject *
1104 raster_source_pattern_set_acquire (PycairoRasterSourcePattern *obj,
1105 PyObject *args) {
1106 PyObject *acquire_callable, *release_callable;
1107 cairo_status_t status;
1108 cairo_pattern_t *pattern;
1109 void *callback_data;
1110 cairo_raster_source_acquire_func_t acquire_func;
1111 cairo_raster_source_release_func_t release_func;
1112 void *acquire_user_data, *release_user_data;
1113
1114 if (!PyArg_ParseTuple (args, "OO:RasterSourcePattern.set_acquire",
1115 &acquire_callable, &release_callable))
1116 return NULL;
1117
1118 pattern = obj->pattern;
1119
1120 callback_data = cairo_raster_source_pattern_get_callback_data (pattern);
1121 if (callback_data != NULL && callback_data != pattern) {
1122 PyErr_SetString (PyExc_RuntimeError,
1123 "Callback is set, but not through Pycairo. Replacing not supported.");
1124 return NULL;
1125 }
1126
1127 if (!PyCallable_Check (acquire_callable) && acquire_callable != Py_None) {
1128 PyErr_SetString (
1129 PyExc_TypeError, "argument needs to be a callable or None");
1130 return NULL;
1131 }
1132
1133 if (!PyCallable_Check (release_callable) && release_callable != Py_None) {
1134 PyErr_SetString (
1135 PyExc_TypeError, "argument needs to be a callable or None");
1136 return NULL;
1137 }
1138
1139 if (acquire_callable == Py_None) {
1140 acquire_func = NULL;
1141 acquire_user_data = NULL;
1142 } else {
1143 acquire_func = _raster_source_acquire_func;
1144 acquire_user_data = acquire_callable;
1145 }
1146
1147 if (release_callable == Py_None) {
1148 release_func = NULL;
1149 release_user_data = NULL;
1150 } else {
1151 release_func = _raster_source_release_func;
1152 release_user_data = release_callable;
1153 }
1154
1155 /* in case acquire is set we have to clean up anyway */
1156 if (acquire_func != NULL && release_func == NULL) {
1157 release_func = _raster_source_release_func;
1158 }
1159
1160 status = cairo_pattern_set_user_data (
1161 pattern, &raster_source_acquire_key, acquire_user_data,
1162 (acquire_user_data) ? _decref_destroy_func : NULL);
1163 RETURN_NULL_IF_CAIRO_ERROR (status);
1164 if (acquire_user_data != NULL)
1165 Py_INCREF (acquire_user_data);
1166
1167 status = cairo_pattern_set_user_data (
1168 pattern, &raster_source_release_key, release_user_data,
1169 (release_user_data) ? _decref_destroy_func : NULL);
1170 if (status != CAIRO_STATUS_SUCCESS) {
1171 cairo_pattern_set_user_data (
1172 pattern, &raster_source_acquire_key, NULL, NULL);
1173 RETURN_NULL_IF_CAIRO_ERROR (status);
1174 }
1175 if (release_user_data != NULL)
1176 Py_INCREF (release_user_data);
1177
1178 cairo_raster_source_pattern_set_callback_data (pattern, pattern);
1179
1180 Py_BEGIN_ALLOW_THREADS;
1181 cairo_raster_source_pattern_set_acquire (
1182 pattern, acquire_func, release_func);
1183 Py_END_ALLOW_THREADS;
1184
1185 Py_RETURN_NONE;
1186 }
1187
1188 static PyObject *
1189 raster_source_pattern_get_acquire (PycairoRasterSourcePattern *obj, PyObject *ignored) {
1190 cairo_pattern_t *pattern;
1191 void *user_data;
1192 PyObject *acquire_callable, *release_callable;
1193
1194 pattern = obj->pattern;
1195
1196 user_data = cairo_pattern_get_user_data (
1197 pattern, &raster_source_acquire_key);
1198 if (user_data == NULL) {
1199 acquire_callable = Py_None;
1200 } else {
1201 acquire_callable = user_data;
1202 }
1203
1204 user_data = cairo_pattern_get_user_data (
1205 pattern, &raster_source_release_key);
1206 if (user_data == NULL) {
1207 release_callable = Py_None;
1208 } else {
1209 release_callable = user_data;
1210 }
1211
1212 return Py_BuildValue ("(OO)", acquire_callable, release_callable);
1213 }
1214
1215 static PyMethodDef raster_source_pattern_methods[] = {
1216 {"set_acquire",
1217 (PyCFunction)raster_source_pattern_set_acquire, METH_VARARGS},
1218 {"get_acquire",
1219 (PyCFunction)raster_source_pattern_get_acquire, METH_NOARGS},
1220 {NULL, NULL, 0, NULL},
1221 };
1222
1223 PyTypeObject PycairoRasterSourcePattern_Type = {
1224 PyVarObject_HEAD_INIT(NULL, 0)
1225 "cairo.RasterSourcePattern", /* tp_name */
1226 sizeof(PycairoRasterSourcePattern), /* tp_basicsize */
1227 0, /* tp_itemsize */
1228 0, /* tp_dealloc */
1229 0, /* tp_print */
1230 0, /* tp_getattr */
1231 0, /* tp_setattr */
1232 0, /* tp_compare */
1233 0, /* tp_repr */
1234 0, /* tp_as_number */
1235 0, /* tp_as_sequence */
1236 0, /* tp_as_mapping */
1237 0, /* tp_hash */
1238 0, /* tp_call */
1239 0, /* tp_str */
1240 0, /* tp_getattro */
1241 0, /* tp_setattro */
1242 0, /* tp_as_buffer */
1243 Py_TPFLAGS_DEFAULT, /* tp_flags */
1244 0, /* tp_doc */
1245 0, /* tp_traverse */
1246 0, /* tp_clear */
1247 0, /* tp_richcompare */
1248 0, /* tp_weaklistoffset */
1249 0, /* tp_iter */
1250 0, /* tp_iternext */
1251 raster_source_pattern_methods, /* tp_methods */
1252 0, /* tp_members */
1253 0, /* tp_getset */
1254 &PycairoPattern_Type, /* tp_base */
1255 0, /* tp_dict */
1256 0, /* tp_descr_get */
1257 0, /* tp_descr_set */
1258 0, /* tp_dictoffset */
1259 0, /* tp_init */
1260 0, /* tp_alloc */
1261 (newfunc)raster_source_pattern_new, /* tp_new */
1262 0, /* tp_free */
1263 0, /* tp_is_gc */
1264 0, /* tp_bases */
1265 };