From 89443af3d84317177f400da1e814fbe845e000e5 Mon Sep 17 00:00:00 2001 From: John (J5) Palmieri Date: Mon, 25 Oct 2010 17:49:57 -0400 Subject: [PATCH] wrap cairo_rectangle_int_t https://bugs.freedesktop.org/show_bug.cgi?id=31111 --- src/cairomodule.c | 9 +++ src/private.h | 3 + src/py3cairo.h | 11 +++ src/rectangle.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/wscript | 1 + test/api_test.py | 11 +++ 6 files changed, 238 insertions(+), 0 deletions(-) create mode 100644 src/rectangle.c diff --git a/src/cairomodule.c b/src/cairomodule.c index 58e665c..39cd1cb 100644 --- a/src/cairomodule.c +++ b/src/cairomodule.c @@ -97,6 +97,9 @@ static Pycairo_CAPI_t CAPI = { &PycairoRadialGradient_Type, PycairoPattern_FromPattern, + &PycairoRectangleInt_Type, + PycairoRectangleInt_FromRectangleInt, + &PycairoScaledFont_Type, PycairoScaledFont_FromScaledFont, @@ -266,6 +269,8 @@ PyInit__cairo(void) return NULL; #endif + if (PyType_Ready(&PycairoRectangleInt_Type) < 0) + return NULL; PyObject *m = PyModule_Create(&cairomodule); //PyObject *m; @@ -335,6 +340,10 @@ PyInit__cairo(void) Py_INCREF(&PycairoSurface_Type); PyModule_AddObject(m, "Surface", (PyObject *)&PycairoSurface_Type); + Py_INCREF(&PycairoRectangleInt_Type); + PyModule_AddObject(m, "RectangleInt", + (PyObject *)&PycairoRectangleInt_Type); + #ifdef CAIRO_HAS_IMAGE_SURFACE Py_INCREF(&PycairoImageSurface_Type); PyModule_AddObject(m, "ImageSurface", diff --git a/src/private.h b/src/private.h index ba59daf..3fba518 100644 --- a/src/private.h +++ b/src/private.h @@ -61,6 +61,9 @@ extern PyTypeObject PycairoRadialGradient_Type; PyObject *PycairoPattern_FromPattern (cairo_pattern_t *pattern, PyObject *base); +extern PyTypeObject PycairoRectangleInt_Type; +PyObject *PycairoRectangleInt_FromRectangleInt (cairo_rectangle_int_t *rectangle_int); + extern PyTypeObject PycairoScaledFont_Type; PyObject *PycairoScaledFont_FromScaledFont (cairo_scaled_font_t *scaled_font); diff --git a/src/py3cairo.h b/src/py3cairo.h index 35b4240..7d86b84 100644 --- a/src/py3cairo.h +++ b/src/py3cairo.h @@ -67,6 +67,11 @@ typedef struct { typedef struct { PyObject_HEAD + cairo_rectangle_int_t *rectangle_int; +} PycairoRectangleInt; + +typedef struct { + PyObject_HEAD cairo_scaled_font_t *scaled_font; } PycairoScaledFont; @@ -113,6 +118,9 @@ typedef struct { PyTypeObject *RadialGradient_Type; PyObject *(*Pattern_FromPattern)(cairo_pattern_t *pattern, PyObject *base); + PyTypeObject *RectangleInt_Type; + PyObject *(*RectangleInt_FromRectangleInt)(cairo_rectangle_int_t *rectangle_int); + PyTypeObject *ScaledFont_Type; PyObject *(*ScaledFont_FromScaledFont)(cairo_scaled_font_t *scaled_font); @@ -156,6 +164,9 @@ typedef struct { #define PycairoRadialGradient_Type *(Pycairo_CAPI->RadialGradient_Type) #define PycairoPattern_FromPattern (Pycairo_CAPI->Pattern_FromPattern) +#define PycairoRectangleInt_Type *(Pycairo_CAPI->RectangleInt_Type) +#define PycairoRectangleInt_FromRectangleInt (Pycairo_CAPI->RectangleInt_FromRectangleInt) + #define PycairoScaledFont_Type *(Pycairo_CAPI->ScaledFont_Type) #define PycairoScaledFont_FromScaledFont \ (Pycairo_CAPI->ScaledFont_FromScaledFont) diff --git a/src/rectangle.c b/src/rectangle.c new file mode 100644 index 0000000..903e903 --- /dev/null +++ b/src/rectangle.c @@ -0,0 +1,203 @@ +/* -*- mode: C; c-basic-offset: 2 -*- + * + * Copyright © 2005,2010 Steve Chaplin, John (J5) Palmieri + * + * This file is part of pycairo. + * + * Pycairo is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License version 3 as published + * by the Free Software Foundation. + * + * Pycairo is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with pycairo. If not, see . + */ + +#define PY_SSIZE_T_CLEAN +#include +#include + +#include "config.h" +#include "private.h" + +/* PycairoRectangleInt object + */ + +static PyObject * +rectangle_int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + PycairoRectangleInt *ri_obj; + PyObject *obj = type->tp_alloc(type, 0); + if (!obj) + return NULL; + + ri_obj = (PycairoRectangleInt *)obj; + ri_obj->rectangle_int = malloc(sizeof(cairo_rectangle_int_t)); + if (!ri_obj) { + Py_DECREF(obj); + PyErr_NoMemory(); + return NULL; + } + + return obj; + +} + +/* PycairoRectangleInt_FromRectangleInt + * Create a new PycairoRectangleInt from a cairo_rectangle_int_t + * rectangle_int - a cairo_rectangle_int_t to 'wrap' into a Python object. + * rectangle_int is unreferenced if the PycairoRectangleInt creation + * fails, or if rectangle_int is in an error status. + * Return value: New reference or NULL on failure + */ +PyObject * +PycairoRectangleInt_FromRectangleInt (cairo_rectangle_int_t *rectangle_int) { + PyObject *o; + + assert (rectangle_int != NULL); + + o = rectangle_int_new(&PycairoRectangleInt_Type, NULL, NULL); + if (o) { + memcpy(((PycairoRectangleInt *)o)->rectangle_int, + rectangle_int, + sizeof(cairo_rectangle_int_t)); + } + + return o; +} + +static void +rectangle_int_dealloc(PycairoRectangleInt *o) { +#ifdef DEBUG + printf("rectangle_int_dealloc start\n"); +#endif + if (o->rectangle_int) { + free(o->rectangle_int); + o->rectangle_int = NULL; + } + Py_TYPE(o)->tp_free(o); +#ifdef DEBUG + printf("rectangle_int_dealloc end\n"); +#endif +} + +static PyObject * +rectangle_int_get_x(PycairoRectangleInt *self, void *closure) { + return PyLong_FromLong(self->rectangle_int->x); +} + +static int +rectangle_int_set_x(PycairoRectangleInt *self, PyObject *value, void *closure) { + if (!PyLong_Check(value)) { + PyErr_SetString(PyExc_TypeError, "type mismatch: the 'x' member must be set to an integer"); + return -1; + } + + self->rectangle_int->x = PyLong_AsLong(value); + + return 0; +} + +static PyObject * +rectangle_int_get_y(PycairoRectangleInt *self, void *closure) { + return PyLong_FromLong(self->rectangle_int->y); +} + +static int +rectangle_int_set_y(PycairoRectangleInt *self, PyObject *value, void *closure) { + if (!PyLong_Check(value)) { + PyErr_SetString(PyExc_TypeError, "type mismatch: the 'y' member must be set to an integer"); + return -1; + } + + self->rectangle_int->y = PyLong_AsLong(value); + + return 0; +} + +static PyObject * +rectangle_int_get_height(PycairoRectangleInt *self, void *closure) { + return PyLong_FromLong(self->rectangle_int->height); +} + +static int +rectangle_int_set_height(PycairoRectangleInt *self, PyObject *value, void *closure) { + if (!PyLong_Check(value)) { + PyErr_SetString(PyExc_TypeError, "type mismatch: the 'height' member must be set to an integer"); + return -1; + } + + self->rectangle_int->height = PyLong_AsLong(value); + + return 0; +} + + +static PyObject * +rectangle_int_get_width(PycairoRectangleInt *self, void *closure) { + return PyLong_FromLong(self->rectangle_int->width); +} + +static int +rectangle_int_set_width(PycairoRectangleInt *self, PyObject *value, void *closure) { + if (!PyLong_Check(value)) { + PyErr_SetString(PyExc_TypeError, "type mismatch: the 'width' member must be set to an integer"); + return -1; + } + + self->rectangle_int->width = PyLong_AsLong(value); + + return 0; +} + +static PyGetSetDef rectangle_int_getset_funcs[] = { + { "x", (getter)rectangle_int_get_x, (setter)rectangle_int_set_x }, + { "y", (getter)rectangle_int_get_y, (setter)rectangle_int_set_y }, + { "height", (getter)rectangle_int_get_height, (setter)rectangle_int_set_height }, + { "width", (getter)rectangle_int_get_width, (setter)rectangle_int_set_width }, + { NULL, 0, 0 } +}; + +PyTypeObject PycairoRectangleInt_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "cairo.RectangleInt", /* tp_name */ + sizeof(PycairoRectangleInt), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)rectangle_int_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + 0, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + rectangle_int_getset_funcs, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + (newfunc)rectangle_int_new, /* tp_new */ +}; diff --git a/src/wscript b/src/wscript index d30c6ad..01d79ff 100644 --- a/src/wscript +++ b/src/wscript @@ -27,6 +27,7 @@ def build(ctx): 'pattern.c', 'matrix.c', 'surface.c', + 'rectangle.c', ], target = '_cairo', includes = '.', diff --git a/test/api_test.py b/test/api_test.py index c83bf99..343531d 100644 --- a/test/api_test.py +++ b/test/api_test.py @@ -81,6 +81,17 @@ def test_surface(): f, w, h = tfi.TemporaryFile(mode='w+b'), 100, 100 s = cairo.SVGSurface(f, w, h) +def test_rectangle_int(): + ri = cairo.RectangleInt() + ri.x = 5 + ri.y = 14 + ri.height = 764 + ri.width = 1080 + + assert ri.x == 5 + assert ri.y == 14 + assert ri.height = 764 + assert ri.width = 1080 def test_text(): pass -- 1.7.2.3