[cairo] Add get_width()/get_height() to pycairo svg surfaces

Keith Packard keithp at keithp.com
Mon Jul 28 22:31:46 PDT 2008


This change (ab)uses cairo_clip_extents to recover the size of an svg
surface -- if you look at the svg clip code, you'll see that this will
reliably return the size passed in to the surface creation function
(although it will have passed through a double->short->double
conversion).

I found this helpful in using pycha to create some SVG pie charts.

(btw, CVS, ick)

Index: cairo/pycairo-surface.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-surface.c,v
retrieving revision 1.89
diff -u -p -r1.89 pycairo-surface.c
--- cairo/pycairo-surface.c	12 May 2008 12:06:37 -0000	1.89
+++ cairo/pycairo-surface.c	29 Jul 2008 05:27:09 -0000
@@ -972,12 +972,34 @@ svg_surface_new (PyTypeObject *type, PyO
 	       file);
 }
 
+static PyObject *
+svg_surface_get_height (PycairoXlibSurface *o)
+{
+    cairo_t *cr = cairo_create (o->surface);
+    double x1, y1, x2, y2;
+    cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
+    cairo_destroy (cr);
+    return PyInt_FromLong (y2 - y1);
+}
+
+static PyObject *
+svg_surface_get_width (PycairoXlibSurface *o)
+{
+    cairo_t *cr = cairo_create (o->surface);
+    double x1, y1, x2, y2;
+    cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
+    cairo_destroy (cr);
+    return PyInt_FromLong (x2 - x1);
+}
+
 static PyMethodDef svg_surface_methods[] = {
     /* TODO
      * cairo_svg_surface_restrict_to_version
      * cairo_svg_get_versions
      * cairo_svg_version_to_string
      */
+    {"get_height",(PyCFunction)svg_surface_get_height,   METH_NOARGS },
+    {"get_width", (PyCFunction)svg_surface_get_width,    METH_NOARGS },
     {NULL, NULL, 0, NULL},
 };
 

-- 
keith.packard at intel.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.cairographics.org/archives/cairo/attachments/20080728/1910603a/attachment.pgp 


More information about the cairo mailing list