From a0f6f4ddc7090433a640f581c34b76d518de06ac Mon Sep 17 00:00:00 2001 From: Torsten Landschoff Date: Wed, 4 May 2011 22:57:15 +0200 Subject: [PATCH] Add support for recording surfaces. --- src/cairomodule.c | 9 +++++ src/private.h | 4 ++ src/pycairo.h | 1 + src/surface.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 0 deletions(-) diff --git a/src/cairomodule.c b/src/cairomodule.c index f0d39f8..c5c914d 100644 --- a/src/cairomodule.c +++ b/src/cairomodule.c @@ -241,6 +241,10 @@ init_cairo(void) if (PyType_Ready(&PycairoXlibSurface_Type) < 0) return; #endif +#ifdef CAIRO_HAS_RECORDING_SURFACE + if (PyType_Ready(&PycairoRecordingSurface_Type) < 0) + return; +#endif m = Py_InitModule("cairo._cairo", cairo_functions); @@ -331,6 +335,11 @@ init_cairo(void) (PyObject *)&PycairoXlibSurface_Type); #endif +#ifdef CAIRO_HAS_RECORDING_SURFACE + Py_INCREF(&PycairoRecordingSurface_Type); + PyModule_AddObject(m, "RecordingSurface", (PyObject *)&PycairoRecordingSurface_Type); +#endif + PyModule_AddObject(m, "CAPI", PyCObject_FromVoidPtr(&CAPI, NULL)); /* Add 'cairo.Error' to the module */ diff --git a/src/private.h b/src/private.h index 4e09922..fdcf9df 100644 --- a/src/private.h +++ b/src/private.h @@ -108,6 +108,10 @@ extern PyObject *xpybVISUALTYPE_type; extern PyTypeObject PycairoXlibSurface_Type; #endif +#if CAIRO_HAS_RECORDING_SURFACE +extern PyTypeObject PycairoRecordingSurface_Type; +#endif + PyObject *PycairoSurface_FromSurface (cairo_surface_t *surface, PyObject *base); diff --git a/src/pycairo.h b/src/pycairo.h index c886de8..37ad8b1 100644 --- a/src/pycairo.h +++ b/src/pycairo.h @@ -95,6 +95,7 @@ typedef struct { #define PycairoWin32PrintingSurface PycairoSurface #define PycairoXCBSurface PycairoSurface #define PycairoXlibSurface PycairoSurface +#define PycairoRecordingSurface PycairoSurface /* get C object out of the Python wrapper */ #define PycairoContext_GET(obj) (((PycairoContext *)(obj))->ctx) diff --git a/src/surface.c b/src/surface.c index 2888e9c..794b8e3 100644 --- a/src/surface.c +++ b/src/surface.c @@ -106,6 +106,11 @@ PycairoSurface_FromSurface (cairo_surface_t *surface, PyObject *base) { type = &PycairoXlibSurface_Type; break; #endif +#if CAIRO_HAS_RECORDING_SURFACE + case CAIRO_SURFACE_TYPE_RECORDING: + type = &PycairoRecordingSurface_Type; + break; +#endif default: type = &PycairoSurface_Type; break; @@ -1480,3 +1485,91 @@ PyTypeObject PycairoXlibSurface_Type = { 0, /* tp_bases */ }; #endif /* CAIRO_HAS_XLIB_SURFACE */ + + +/* Class RecordingSurface(Surface) ---------------------------------------- */ +#ifdef CAIRO_HAS_RECORDING_SURFACE + +static PyObject * +recording_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { + int content; + PyObject *extents_tuple = Py_None; + cairo_rectangle_t extents, *extents_ptr = NULL; + cairo_surface_t *sfc; + + if (!PyArg_ParseTuple(args, "i|O:RecordingSurface.__new__", + &content, &extents_tuple)) + return NULL; + + if (extents_tuple != Py_None) { + PyArg_ParseTuple(extents_tuple, "dddd", + &extents.x, &extents.y, &extents.width, &extents.height); + extents_ptr = &extents; + } + + Py_BEGIN_ALLOW_THREADS; + sfc = cairo_recording_surface_create (content, extents_ptr); + Py_END_ALLOW_THREADS; + return PycairoSurface_FromSurface (sfc, NULL); +} + +static PyObject * +recording_surface_ink_extents (PycairoRecordingSurface *o, PyObject *args) { + double x0, y0, width, height; + + if (!PyArg_ParseTuple(args, ":RecordingSurface.ink_extents")) + return NULL; + cairo_recording_surface_ink_extents(o->surface, &x0, &y0, &width, &height); + return Py_BuildValue("(dddd)", x0, y0, width, height); +} + +static PyMethodDef recording_surface_methods[] = { + {"ink_extents", (PyCFunction)recording_surface_ink_extents, METH_VARARGS }, + {NULL, NULL, 0, NULL}, +}; + +PyTypeObject PycairoRecordingSurface_Type = { + PyObject_HEAD_INIT(NULL) + 0, /* ob_size */ + "cairo.RecordingSurface", /* tp_name */ + sizeof(PycairoRecordingSurface), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* 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 */ + recording_surface_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + &PycairoSurface_Type, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + (newfunc)recording_surface_new, /* tp_new */ + 0, /* tp_free */ + 0, /* tp_is_gc */ + 0, /* tp_bases */ +}; +#endif /* CAIRO_HAS_RECORDING_SURFACE */ -- 1.7.1.rc2.dirty