[pygobject] Add a foreign type for cairo_region_t.



commit 7a3e4c005a6677ed7bf3ef807cd1a8487b0465de
Author: Shyouzou Sugitani <shy users sourceforge jp>
Date:   Mon Jun 6 12:26:44 2016 +0200

    Add a foreign type for cairo_region_t.
    
    Based on the patch at
        https://bugzilla.gnome.org/show_bug.cgi?id=667959#c7
    
    * Rebased on master
    * Fixes the cairo_region_from_arg signature
    * Add check for pycairo region support (only in pycairo master)
    * Add some tests
    
    Co-Authored-By: Martin Pitt <martin pitt ubuntu com>
    Co-Authored-By: Christoph Reiter <creiter src gnome org>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=667959

 gi/pygi-foreign-cairo.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_cairo.py     |   28 ++++++++++++++++++++++++-
 2 files changed, 77 insertions(+), 2 deletions(-)
---
diff --git a/gi/pygi-foreign-cairo.c b/gi/pygi-foreign-cairo.c
index 8e76529..3ad3a99 100644
--- a/gi/pygi-foreign-cairo.c
+++ b/gi/pygi-foreign-cairo.c
@@ -367,6 +367,49 @@ cairo_pattern_from_gvalue (const GValue *value)
     return PycairoPattern_FromPattern (pattern, NULL);
 }
 
+#if PY_VERSION_HEX >= 0x03000000 && defined(PycairoRegion_Type)
+
+static PyObject *
+cairo_region_to_arg (PyObject        *value,
+                     GIInterfaceInfo *interface_info,
+                     GITransfer       transfer,
+                     GIArgument      *arg)
+{
+    cairo_region_t *region;
+
+    g_assert (transfer == GI_TRANSFER_NOTHING);
+
+    region = ( (PycairoRegion*) value)->region;
+    if (!region) {
+        PyErr_SetString (PyExc_ValueError, "Region instance wrapping a NULL region");
+        return NULL;
+    }
+
+    arg->v_pointer = region;
+    Py_RETURN_NONE;
+}
+
+static PyObject *
+cairo_region_from_arg (GIInterfaceInfo *interface_info,
+                       GITransfer transfer,
+                       gpointer data)
+{
+    cairo_region_t *region = (cairo_region_t*) data;
+
+    if (transfer == GI_TRANSFER_NOTHING)
+        cairo_region_reference (region);
+
+    return PycairoRegion_FromRegion (region);
+}
+
+static PyObject *
+cairo_region_release (GIBaseInfo *base_info,
+                      gpointer    struct_)
+{
+    cairo_region_destroy ( (cairo_region_t*) struct_);
+    Py_RETURN_NONE;
+}
+#endif
 
 static PyMethodDef _gi_cairo_functions[] = { {0,} };
 PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
@@ -406,6 +449,14 @@ PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
                                   cairo_font_options_from_arg,
                                   cairo_font_options_release);
 
+#if PY_VERSION_HEX >= 0x03000000 && defined(PycairoRegion_Type)
+    pygi_register_foreign_struct ("cairo",
+                                  "Region",
+                                  cairo_region_to_arg,
+                                  cairo_region_from_arg,
+                                  cairo_region_release);
+#endif
+
     pyg_register_gtype_custom (CAIRO_GOBJECT_TYPE_CONTEXT,
                                cairo_context_from_gvalue,
                                cairo_context_to_gvalue);
diff --git a/tests/test_cairo.py b/tests/test_cairo.py
index fdf86a2..02049c0 100644
--- a/tests/test_cairo.py
+++ b/tests/test_cairo.py
@@ -14,11 +14,14 @@ try:
 except ImportError:
     has_cairo = False
 
+has_region = has_cairo and hasattr(cairo, "Region")
+
 try:
-    from gi.repository import Gtk
-    Gtk  # pyflakes
+    from gi.repository import Gtk, Gdk
+    Gtk, Gdk  # pyflakes
 except:
     Gtk = None
+    Gdk = None
 
 from gi.repository import GObject
 
@@ -67,6 +70,27 @@ class Test(unittest.TestCase):
 
 
 @unittest.skipUnless(has_cairo, 'built without cairo support')
+@unittest.skipUnless(has_region, 'built without cairo.Region support')
+@unittest.skipUnless(Gdk, 'Gdk not available')
+class TestRegion(unittest.TestCase):
+
+    def test_region_to_py(self):
+        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
+        context = cairo.Context(surface)
+        context.paint()
+        region = Gdk.cairo_region_create_from_surface(surface)
+        r = region.get_extents()
+        self.assertEqual((r.height, r.width), (10, 10))
+
+    def test_region_from_py(self):
+        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 10, 10)
+        context = cairo.Context(surface)
+        region = cairo.Region(cairo.RectangleInt(0, 0, 42, 42))
+        Gdk.cairo_region(context, region)
+        self.assertTrue("42" in repr(list(context.copy_path())))
+
+
+@unittest.skipUnless(has_cairo, 'built without cairo support')
 @unittest.skipUnless(Gtk, 'Gtk not available')
 class TestPango(unittest.TestCase):
     def test_cairo_font_options(self):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]