From 88e2ef3448397ab406e8278a53ce6297972a6fa0 Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Tue, 20 Mar 2018 10:24:28 +0100 Subject: [PATCH] cairo: support cairo.Matrix conversion. Fixes #16 --- gi/pygi-foreign-cairo.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/regressextra.c | 41 +++++++++++++++++++++++++++++++++++++++++ tests/regressextra.h | 3 +++ tests/test_cairo.py | 12 ++++++++++++ 4 files changed, 104 insertions(+) diff --git a/gi/pygi-foreign-cairo.c b/gi/pygi-foreign-cairo.c index 63a27433..b2ec9fc5 100644 --- a/gi/pygi-foreign-cairo.c +++ b/gi/pygi-foreign-cairo.c @@ -429,6 +429,48 @@ cairo_region_release (GIBaseInfo *base_info, Py_RETURN_NONE; } +static PyObject * +cairo_matrix_from_arg (GIInterfaceInfo *interface_info, + GITransfer transfer, + gpointer data) +{ + cairo_matrix_t *matrix = (cairo_matrix_t*) data; + + if (transfer != GI_TRANSFER_NOTHING) { + PyErr_SetString(PyExc_TypeError, "Unsupported annotation (transfer full) for cairo.Matrix"); + return NULL; + } + + if (matrix == NULL) { + /* NULL in case of caller-allocates */ + cairo_matrix_t temp = {0}; + return PycairoMatrix_FromMatrix (&temp); + } + + return PycairoMatrix_FromMatrix (matrix); +} + +static PyObject * +cairo_matrix_to_arg (PyObject *value, + GIInterfaceInfo *interface_info, + GITransfer transfer, + GIArgument *arg) +{ + cairo_matrix_t *matrix; + + matrix = &(( (PycairoMatrix*) value)->matrix); + + arg->v_pointer = matrix; + Py_RETURN_NONE; +} + +static PyObject * +cairo_matrix_release (GIBaseInfo *base_info, + gpointer struct_) +{ + Py_RETURN_NONE; +} + static PyMethodDef _gi_cairo_functions[] = { {0,} }; PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo") { @@ -444,6 +486,12 @@ PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo") pygobject_init (3, 13, 2); pygi_register_foreign_struct ("cairo", + "Matrix", + cairo_matrix_to_arg, + cairo_matrix_from_arg, + cairo_matrix_release); + + pygi_register_foreign_struct ("cairo", "Context", cairo_context_to_arg, cairo_context_from_arg, diff --git a/tests/regressextra.c b/tests/regressextra.c index 892de0be..a1426782 100644 --- a/tests/regressextra.c +++ b/tests/regressextra.c @@ -242,4 +242,45 @@ regress_test_cairo_font_options_none_in (cairo_font_options_t *options) { } + +/** + * regress_test_cairo_matrix_none_in: + * @matrix: (transfer none): + */ +void +regress_test_cairo_matrix_none_in (const cairo_matrix_t *matrix) +{ + cairo_matrix_t m = *matrix; + g_assert (m.x0 == 0); + g_assert (m.y0 == 0); + g_assert (m.xx == 1); + g_assert (m.xy == 0); + g_assert (m.yy == 1); + g_assert (m.yx == 0); +} + +/** + * regress_test_cairo_matrix_none_return: + * Returns: (transfer none): + */ +cairo_matrix_t * +regress_test_cairo_matrix_none_return (void) +{ + static cairo_matrix_t matrix; + cairo_matrix_init_identity (&matrix); + return &matrix; +} + +/** + * regress_test_cairo_matrix_out_caller_allocates: + * @matrix: (out): + */ +void +regress_test_cairo_matrix_out_caller_allocates (cairo_matrix_t *matrix) +{ + cairo_matrix_t m; + cairo_matrix_init_identity (&m); + *matrix = m; +} + #endif diff --git a/tests/regressextra.h b/tests/regressextra.h index 421dfae8..14c72ac4 100644 --- a/tests/regressextra.h +++ b/tests/regressextra.h @@ -34,6 +34,9 @@ void regress_test_cairo_font_options_full_in (cairo_font_options_t *options); void regress_test_cairo_font_options_none_in (cairo_font_options_t *options); void regress_test_cairo_region_full_in (cairo_region_t *region); void regress_test_cairo_surface_full_in (cairo_surface_t *surface); +void regress_test_cairo_matrix_none_in (const cairo_matrix_t *matrix); +cairo_matrix_t *regress_test_cairo_matrix_none_return (void); +void regress_test_cairo_matrix_out_caller_allocates (cairo_matrix_t *matrix); #endif diff --git a/tests/test_cairo.py b/tests/test_cairo.py index e8037d1a..ea2733b2 100644 --- a/tests/test_cairo.py +++ b/tests/test_cairo.py @@ -87,6 +87,18 @@ class Test(unittest.TestCase): region = cairo.Region() Regress.test_cairo_region_full_in(region) + def test_cairo_matrix_none_in(self): + matrix = cairo.Matrix() + Regress.test_cairo_matrix_none_in(matrix) + + def test_cairo_matrix_none_return(self): + matrix = Regress.test_cairo_matrix_none_return() + assert matrix == cairo.Matrix() + + def test_cairo_matrix_out_caller_allocates(self): + matrix = Regress.test_cairo_matrix_out_caller_allocates() + assert matrix == cairo.Matrix() + def test_cairo_surface(self): surface = Regress.test_cairo_surface_none_return() self.assertTrue(isinstance(surface, cairo.ImageSurface)) -- 2.11.4.GIT