Debian Codegraph @

cairo/region.c

raw symbols SCIP
1 /* -*- mode: C; c-basic-offset: 2 -*-
2 *
3 * Pycairo - Python bindings for cairo
4 *
5 * Copyright © 2005,2010 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 #include "structmember.h"
34
35 #include "private.h"
36
37 PyObject *
38 PycairoRectangleInt_FromRectangleInt (const cairo_rectangle_int_t *rectangle_int) {
39 PyObject *o;
40
41 assert (rectangle_int != NULL);
42
43 o = PycairoRectangleInt_Type.tp_alloc (&PycairoRectangleInt_Type, 0);
44 if (o)
45 ((PycairoRectangleInt *)o)->rectangle_int = *rectangle_int;
46 return o;
47 }
48
49 static void
50 rectangle_int_dealloc(PycairoRectangleInt *o) {
51 Py_TYPE(o)->tp_free(o);
52 }
53
54 static PyObject *
55 rectangle_int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
56 static char *kwlist[] = { "x", "y", "width", "height", NULL };
57 int x, y, w, h;
58 cairo_rectangle_int_t rect;
59 x = y = w = h = 0;
60
61 if (!PyArg_ParseTupleAndKeywords(args, kwds,
62 "|iiii:RectangleInt.__new__", kwlist,
63 &x, &y, &w, &h))
64 return NULL;
65
66 rect.x = x;
67 rect.y = y;
68 rect.width = w;
69 rect.height = h;
70
71 return PycairoRectangleInt_FromRectangleInt(&rect);
72 }
73
74 static PyObject *
75 rectangle_int_repr(PycairoRectangleInt *rect_o) {
76 PyObject *s;
77 cairo_rectangle_int_t *rect = &(rect_o->rectangle_int);
78 char buf[80];
79 PyOS_snprintf(buf, sizeof(buf),
80 "cairo.RectangleInt(x=%d, y=%d, width=%d, height=%d)",
81 rect->x, rect->y, rect->width, rect->height);
82 s = PyUnicode_FromString (buf);
83 return s;
84 }
85
86 static PyObject *
87 rectangle_int_richcompare(PycairoRectangleInt *self,
88 PycairoRectangleInt *other, int op) {
89 int res = 0;
90 PyObject *b;
91
92 if (op != Py_EQ && op != Py_NE) {
93 PyErr_SetString(PyExc_TypeError, "Only support testing for == or !=");
94 return NULL;
95 }
96
97 if (!PyObject_TypeCheck((PyObject*)other, &PycairoRectangleInt_Type)) {
98 Py_INCREF(Py_NotImplemented);
99 return Py_NotImplemented;
100 } else if (
101 self->rectangle_int.x == other->rectangle_int.x &&
102 self->rectangle_int.y == other->rectangle_int.y &&
103 self->rectangle_int.width == other->rectangle_int.width &&
104 self->rectangle_int.height == other->rectangle_int.height
105 )
106 res = 1;
107 res = op == Py_NE ? !res : res;
108 b = res ? Py_True : Py_False;
109 Py_INCREF(b);
110
111 return b;
112 }
113
114 static PyMemberDef RectangleInt_members[] = {
115 {"x", T_INT, sizeof(PyObject), 0,
116 "X coordinate of the left side of the rectangle"},
117 {"y", T_INT, sizeof(PyObject)+sizeof(int), 0,
118 "Y coordinate of the the top side of the rectangle"},
119 {"width", T_INT, sizeof(PyObject)+sizeof(int)*2, 0,
120 "width of the rectangle"},
121 {"height", T_INT, sizeof(PyObject)+sizeof(int)*3, 0,
122 "height of the rectangle"},
123 {NULL}
124 };
125
126 PyTypeObject PycairoRectangleInt_Type = {
127 PyVarObject_HEAD_INIT(NULL, 0)
128 "cairo.RectangleInt", /* tp_name */
129 sizeof(PycairoRectangleInt), /* tp_basicsize */
130 0, /* tp_itemsize */
131 (destructor)rectangle_int_dealloc, /* tp_dealloc */
132 0, /* tp_print */
133 0, /* tp_getattr */
134 0, /* tp_setattr */
135 0, /* tp_compare */
136 (reprfunc)rectangle_int_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 (hashfunc)PyObject_HashNotImplemented,/* 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, /* tp_flags */
147 0, /* tp_doc */
148 0, /* tp_traverse */
149 0, /* tp_clear */
150 (richcmpfunc)rectangle_int_richcompare, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
152 0, /* tp_iter */
153 0, /* tp_iternext */
154 0, /* tp_methods */
155 RectangleInt_members, /* tp_members */
156 0, /* tp_getset */
157 0, /* tp_base */
158 0, /* tp_dict */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 0, /* tp_init */
163 0, /* tp_alloc */
164 (newfunc)rectangle_int_new, /* tp_new */
165 };
166
167 PyObject *
168 PycairoRegion_FromRegion (cairo_region_t *region) {
169 PyObject *o;
170
171 assert (region != NULL);
172
173 if (Pycairo_Check_Status (cairo_region_status(region))) {
174 cairo_region_destroy (region);
175 return NULL;
176 }
177
178 o = PycairoRegion_Type.tp_alloc (&PycairoRegion_Type, 0);
179 if (o)
180 ((PycairoRegion *)o)->region = region;
181 else
182 cairo_region_destroy (region);
183 return o;
184 }
185
186 static void
187 region_dealloc(PycairoRegion *o) {
188 if (o->region) {
189 cairo_region_destroy(o->region);
190 o->region = NULL;
191 }
192 Py_TYPE(o)->tp_free(o);
193 }
194
195 static PyObject *
196 region_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
197 PyObject *s = NULL;
198 PycairoRectangleInt *rect_obj = NULL;
199 cairo_region_t *region = NULL;
200 cairo_rectangle_int_t *rect = NULL;
201
202 if (PyArg_ParseTuple(args, "|O!:Region.__new__",
203 &PycairoRectangleInt_Type, &rect_obj)) {
204 if (rect_obj != NULL) {
205 region = cairo_region_create_rectangle(&(rect_obj->rectangle_int));
206 }
207 } else if (!PyArg_ParseTuple(args, "|O:Region.__new__", &s)) {
208 PyErr_SetString(PyExc_TypeError,
209 "argument must be a RectangleInt or a sequence of RectangleInt.");
210 return NULL;
211 }
212 PyErr_Clear(); /* Clear possible err in the 1st arg parser. */
213
214 /* list of rectangle_int or no args */
215 if (s != NULL) {
216 Py_ssize_t i, rect_size;
217 PyObject *seq = NULL;
218 seq = PySequence_Fast (s,
219 "argument must be a RectangleInt or a sequence of RectangleInt.");
220 if (seq == NULL) {
221 return NULL;
222 }
223 rect_size = PySequence_Fast_GET_SIZE(seq);
224 if (rect_size > INT_MAX) {
225 Py_DECREF (seq);
226 PyErr_SetString (PyExc_ValueError, "sequence too large");
227 return NULL;
228 }
229 rect = PyMem_Malloc ((unsigned int)rect_size * sizeof(cairo_rectangle_int_t));
230 if (rect == NULL) {
231 Py_DECREF(seq);
232 return PyErr_NoMemory();
233 }
234
235 for(i=0; i<rect_size; i++) {
236 PyObject *obj_tmp = PySequence_Fast_GET_ITEM(seq, i);
237 if (!PyObject_TypeCheck(obj_tmp, &PycairoRectangleInt_Type)) {
238 PyErr_SetString(PyExc_TypeError, "Must be RectangleInt");
239 Py_DECREF(seq);
240 PyMem_Free(rect);
241 return NULL;
242 }
243 rect_obj = (PycairoRectangleInt*) obj_tmp;
244 rect[i] = rect_obj->rectangle_int;
245 }
246
247 region = cairo_region_create_rectangles(rect, (int)rect_size);
248
249 Py_DECREF(seq);
250 PyMem_Free(rect);
251 }
252
253 if (region == NULL) {
254 region = cairo_region_create();
255 }
256
257 RETURN_NULL_IF_CAIRO_REGION_ERROR(region);
258 return PycairoRegion_FromRegion(region);
259 }
260
261 static PyObject *
262 region_copy (PycairoRegion *o, PyObject *ignored) {
263 cairo_region_t *res;
264 Py_BEGIN_ALLOW_THREADS;
265 res = cairo_region_copy (o->region);
266 Py_END_ALLOW_THREADS;
267 RETURN_NULL_IF_CAIRO_REGION_ERROR(res);
268 return PycairoRegion_FromRegion(res);
269 }
270
271
272 static PyObject *
273 region_get_extents (PycairoRegion *o, PyObject *ignored) {
274 cairo_rectangle_int_t rect;
275 Py_BEGIN_ALLOW_THREADS;
276 cairo_region_get_extents(o->region, &rect);
277 Py_END_ALLOW_THREADS;
278
279 return PycairoRectangleInt_FromRectangleInt(&rect);
280 }
281
282
283 static PyObject *
284 region_num_rectangles (PycairoRegion *o, PyObject *ignored) {
285 int res;
286 Py_BEGIN_ALLOW_THREADS;
287 res = cairo_region_num_rectangles(o->region);
288 Py_END_ALLOW_THREADS;
289 return PyLong_FromLong(res);
290 }
291
292
293 static PyObject *
294 region_get_rectangle (PycairoRegion *o, PyObject *args) {
295 cairo_rectangle_int_t rect;
296 int i;
297 int total;
298 if (!PyArg_ParseTuple (args, "i:Region.get_rectangle", &i))
299 return NULL;
300 total = cairo_region_num_rectangles(o->region);
301 if (i >= total || i < 0) {
302 if ( i < 0)
303 PyErr_SetString(PyExc_ValueError, "index must be a positive number");
304 else
305 PyErr_SetString(PyExc_ValueError, "index is to big for the region");
306 return NULL;
307 }
308 Py_BEGIN_ALLOW_THREADS;
309 cairo_region_get_rectangle(o->region, i, &rect);
310 Py_END_ALLOW_THREADS;
311 return PycairoRectangleInt_FromRectangleInt(&rect);
312 }
313
314
315 static PyObject *
316 region_is_empty (PycairoRegion *o, PyObject *ignored) {
317 cairo_bool_t res;
318 PyObject *b;
319 Py_BEGIN_ALLOW_THREADS;
320 res = cairo_region_is_empty(o->region);
321 Py_END_ALLOW_THREADS;
322 b = res ? Py_True : Py_False;
323 Py_INCREF(b);
324 return b;
325 }
326
327
328 static PyObject *
329 region_contains_point (PycairoRegion *o, PyObject *args) {
330 int x, y;
331 cairo_bool_t res;
332 PyObject *b;
333 if (!PyArg_ParseTuple (args, "ii:Region.contains_point", &x, &y))
334 return NULL;
335 Py_BEGIN_ALLOW_THREADS;
336 res = cairo_region_contains_point(o->region, x, y);
337 Py_END_ALLOW_THREADS;
338 b = res ? Py_True : Py_False;
339 Py_INCREF(b);
340 return b;
341 }
342
343
344 static PyObject *
345 region_contains_rectangle (PycairoRegion *o, PyObject *args) {
346 cairo_region_overlap_t res;
347 PycairoRectangleInt *rect_int;
348 if (!PyArg_ParseTuple (args, "O!:Region.contains_rectangle",
349 &PycairoRectangleInt_Type, &rect_int))
350 return NULL;
351 Py_BEGIN_ALLOW_THREADS;
352 res = cairo_region_contains_rectangle(o->region, &(rect_int->rectangle_int));
353 Py_END_ALLOW_THREADS;
354
355 RETURN_INT_ENUM(RegionOverlap, res);
356 }
357
358
359 static PyObject *
360 region_equal (PycairoRegion *o, PyObject *args) {
361 cairo_bool_t res;
362 PyObject *b;
363 PycairoRegion *region_obj;
364 if (!PyArg_ParseTuple (args, "O!:Region.equal",
365 &PycairoRegion_Type, &region_obj))
366 return NULL;
367 Py_BEGIN_ALLOW_THREADS;
368 res = cairo_region_equal (o->region, region_obj->region);
369 Py_END_ALLOW_THREADS;
370 b = res ? Py_True : Py_False;
371 Py_INCREF(b);
372 return b;
373 }
374
375 static PyObject *
376 region_richcompare(PycairoRegion *self, PycairoRegion *other, int op) {
377 int res = 0;
378 PyObject *b;
379
380 if (op != Py_EQ && op != Py_NE) {
381 PyErr_SetString(PyExc_TypeError, "Only support testing for == or !=");
382 return NULL;
383 }
384
385 if (!PyObject_TypeCheck((PyObject*)other, &PycairoRegion_Type)) {
386 Py_INCREF(Py_NotImplemented);
387 return Py_NotImplemented;
388 } else {
389 res = cairo_region_equal (self->region, other->region);
390 }
391
392 res = op == Py_NE ? !res : res;
393 b = res ? Py_True : Py_False;
394 Py_INCREF(b);
395
396 return b;
397 }
398
399 static PyObject *
400 region_translate (PycairoRegion *o, PyObject *args) {
401 int x, y;
402 if (!PyArg_ParseTuple (args, "ii:Region.translate", &x, &y))
403 return NULL;
404 Py_BEGIN_ALLOW_THREADS;
405 cairo_region_translate (o->region, x, y);
406 Py_END_ALLOW_THREADS;
407 Py_RETURN_NONE;
408 }
409
410
411 static PyObject *
412 region_intersect (PycairoRegion *o, PyObject *args) {
413 cairo_status_t res;
414 PyObject *other;
415 if (!PyArg_ParseTuple (args, "O:Region.intersect", &other))
416 return NULL;
417
418 if (PyObject_TypeCheck(other, &PycairoRegion_Type)) {
419 Py_BEGIN_ALLOW_THREADS;
420 res = cairo_region_intersect(o->region,
421 ((PycairoRegion *)other)->region);
422 Py_END_ALLOW_THREADS;
423 } else if (PyObject_TypeCheck(other, &PycairoRectangleInt_Type)) {
424 Py_BEGIN_ALLOW_THREADS;
425 res = cairo_region_intersect_rectangle(o->region,
426 &(((PycairoRectangleInt *)other)->rectangle_int));
427 Py_END_ALLOW_THREADS;
428 } else {
429 PyErr_SetString(PyExc_TypeError,
430 "argument must be a Region or a RectangleInt.");
431 return NULL;
432 }
433
434 RETURN_NULL_IF_CAIRO_ERROR(res);
435 Py_RETURN_NONE;
436 }
437
438 static PyObject *
439 region_subtract (PycairoRegion *o, PyObject *args) {
440 cairo_status_t res;
441 PyObject *other;
442 if (!PyArg_ParseTuple (args, "O:Region.subtract", &other))
443 return NULL;
444
445 if (PyObject_TypeCheck(other, &PycairoRegion_Type)) {
446 Py_BEGIN_ALLOW_THREADS;
447 res = cairo_region_subtract(o->region,
448 ((PycairoRegion *)other)->region);
449 Py_END_ALLOW_THREADS;
450 } else if (PyObject_TypeCheck(other, &PycairoRectangleInt_Type)) {
451 Py_BEGIN_ALLOW_THREADS;
452 res = cairo_region_subtract_rectangle(o->region,
453 &(((PycairoRectangleInt *)other)->rectangle_int));
454 Py_END_ALLOW_THREADS;
455 } else {
456 PyErr_SetString(PyExc_TypeError,
457 "argument must be a Region or a RectangleInt.");
458 return NULL;
459 }
460 RETURN_NULL_IF_CAIRO_ERROR(res);
461 Py_RETURN_NONE;
462 }
463
464 static PyObject *
465 region_union (PycairoRegion *o, PyObject *args) {
466 cairo_status_t res;
467 PyObject *other;
468 if (!PyArg_ParseTuple (args, "O:Region.union", &other))
469 return NULL;
470
471 if (PyObject_TypeCheck(other, &PycairoRegion_Type)) {
472 Py_BEGIN_ALLOW_THREADS;
473 res = cairo_region_union(o->region,
474 ((PycairoRegion *)other)->region);
475 Py_END_ALLOW_THREADS;
476 } else if (PyObject_TypeCheck(other, &PycairoRectangleInt_Type)) {
477 Py_BEGIN_ALLOW_THREADS;
478 res = cairo_region_union_rectangle(o->region,
479 &(((PycairoRectangleInt *)other)->rectangle_int));
480 Py_END_ALLOW_THREADS;
481 } else {
482 PyErr_SetString(PyExc_TypeError,
483 "argument must be a Region or a RectangleInt.");
484 return NULL;
485 }
486 RETURN_NULL_IF_CAIRO_ERROR(res);
487 Py_RETURN_NONE;
488 }
489
490 static PyObject *
491 region_xor (PycairoRegion *o, PyObject *args) {
492 cairo_status_t res;
493 PyObject *other;
494 if (!PyArg_ParseTuple (args, "O:Region.xorg", &other))
495 return NULL;
496
497 if (PyObject_TypeCheck(other, &PycairoRegion_Type)) {
498 Py_BEGIN_ALLOW_THREADS;
499 res = cairo_region_xor(o->region,
500 ((PycairoRegion *)other)->region);
501 Py_END_ALLOW_THREADS;
502 } else if (PyObject_TypeCheck(other, &PycairoRectangleInt_Type)) {
503 Py_BEGIN_ALLOW_THREADS;
504 res = cairo_region_xor_rectangle(o->region,
505 &(((PycairoRectangleInt *)other)->rectangle_int));
506 Py_END_ALLOW_THREADS;
507 } else {
508 PyErr_SetString(PyExc_TypeError,
509 "argument must be a Region or a RectangleInt.");
510 return NULL;
511 }
512 RETURN_NULL_IF_CAIRO_ERROR(res);
513 Py_RETURN_NONE;
514 }
515
516 static PyMethodDef region_methods[] = {
517 /* methods never exposed in a language binding:
518 * cairo_region_destroy()
519 * cairo_region_get_type()
520 * cairo_region_reference()
521 *
522 * cairo_region_status()
523 * - not needed since Pycairo handles status checking
524 *
525 * _(intersect/subtract/union/xor)_rectangle are merged with the region
526 * ones.
527 */
528 {"copy", (PyCFunction)region_copy, METH_NOARGS },
529 {"get_extents", (PyCFunction)region_get_extents, METH_NOARGS },
530 {"num_rectangles", (PyCFunction)region_num_rectangles, METH_NOARGS },
531 {"get_rectangle", (PyCFunction)region_get_rectangle, METH_VARARGS },
532 {"is_empty", (PyCFunction)region_is_empty, METH_NOARGS },
533 {"contains_point", (PyCFunction)region_contains_point, METH_VARARGS },
534 {"contains_rectangle", (PyCFunction)region_contains_rectangle,
535 METH_VARARGS },
536 {"equal", (PyCFunction)region_equal, METH_VARARGS },
537 {"translate", (PyCFunction)region_translate, METH_VARARGS },
538 {"intersect", (PyCFunction)region_intersect, METH_VARARGS },
539 {"subtract", (PyCFunction)region_subtract, METH_VARARGS },
540 {"union", (PyCFunction)region_union, METH_VARARGS },
541 {"xor", (PyCFunction)region_xor, METH_VARARGS },
542 {NULL, NULL, 0, NULL},
543 };
544
545 PyTypeObject PycairoRegion_Type = {
546 PyVarObject_HEAD_INIT(NULL, 0)
547 "cairo.Region", /* tp_name */
548 sizeof(PycairoRegion), /* tp_basicsize */
549 0, /* tp_itemsize */
550 (destructor)region_dealloc, /* tp_dealloc */
551 0, /* tp_print */
552 0, /* tp_getattr */
553 0, /* tp_setattr */
554 0, /* tp_compare */
555 0, /* tp_repr */
556 0, /* tp_as_number */
557 0, /* tp_as_sequence */
558 0, /* tp_as_mapping */
559 (hashfunc)PyObject_HashNotImplemented,/* tp_hash */
560 0, /* tp_call */
561 0, /* tp_str */
562 0, /* tp_getattro */
563 0, /* tp_setattro */
564 0, /* tp_as_buffer */
565 Py_TPFLAGS_DEFAULT, /* tp_flags */
566 0, /* tp_doc */
567 0, /* tp_traverse */
568 0, /* tp_clear */
569 (richcmpfunc)region_richcompare, /* tp_richcompare */
570 0, /* tp_weaklistoffset */
571 0, /* tp_iter */
572 0, /* tp_iternext */
573 region_methods, /* tp_methods */
574 0, /* tp_members */
575 0, /* tp_getset */
576 0, /* tp_base */
577 0, /* tp_dict */
578 0, /* tp_descr_get */
579 0, /* tp_descr_set */
580 0, /* tp_dictoffset */
581 0, /* tp_init */
582 0, /* tp_alloc */
583 (newfunc)region_new, /* tp_new */
584 };
585