[cairo-commit] pycairo/cairo pycairo-context.c,1.79,1.80

Steve Chaplin commit at pdx.freedesktop.org
Sat Jan 13 18:15:46 PST 2007


Committed by: stevech1097

Update of /cvs/cairo/pycairo/cairo
In directory kemper:/tmp/cvs-serv9154/cairo

Modified Files:
	pycairo-context.c 
Log Message:
'SC'

Index: pycairo-context.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-context.c,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -d -r1.79 -r1.80
--- pycairo-context.c	13 Jan 2007 10:50:37 -0000	1.79
+++ pycairo-context.c	14 Jan 2007 02:15:41 -0000	1.80
@@ -490,41 +490,39 @@
     return PyFloat_FromDouble (cairo_get_tolerance (o->ctx));
 }
 
-static PyObject *
-pycairo_glyph_extents (PycairoContext *o, PyObject *args)
+/* read a Python sequence of (i,x,y) sequences
+ * return cairo_glyph_t *
+ *        num_glyphs
+ *        must call PyMem_Free(glyphs) when finished using the glyphs
+ */
+static cairo_glyph_t *
+_PyGlyphs_AsGlyphs (PyObject *py_object, int *num_glyphs)
 {
-    /* almost identical code to pycairo_show_glyphs */
-    int num_glyphs = -1, length, i;
+    int length, i;
     cairo_glyph_t *glyphs = NULL, *glyph;
-    cairo_text_extents_t extents;
     PyObject *py_glyphs, *py_seq = NULL;
 
-    if (!PyArg_ParseTuple (args, "O|i:Context.glyph_path",
-			   &py_glyphs, &num_glyphs))
-	return NULL;
-
-    py_glyphs = PySequence_Fast (py_glyphs,
-				 "first argument must be a sequence");
+    py_glyphs = PySequence_Fast (py_object, "glyphs must be a sequence");
     if (py_glyphs == NULL)
 	return NULL;
 
     length = PySequence_Fast_GET_SIZE(py_glyphs);
-    if (num_glyphs < 0 || num_glyphs > length)
-	num_glyphs = length;
+    if (*num_glyphs < 0 || *num_glyphs > length)
+	*num_glyphs = length;
 
-    glyphs = PyMem_Malloc (num_glyphs * sizeof(cairo_glyph_t));
+    glyphs = PyMem_Malloc (*num_glyphs * sizeof(cairo_glyph_t));
     if (glyphs == NULL) {
 	PyErr_NoMemory();
 	goto error;
     }
-    for (i = 0, glyph = glyphs; i < num_glyphs; i++, glyph++) {
+    for (i = 0, glyph = glyphs; i < *num_glyphs; i++, glyph++) {
 	PyObject *py_item = PySequence_Fast_GET_ITEM(py_glyphs, i);
 	py_seq = PySequence_Fast (py_item, "glyph items must be a sequence");
 	if (py_seq == NULL)
 	    goto error;
 	if (PySequence_Fast_GET_SIZE(py_seq) != 3) {
 	    PyErr_SetString(PyExc_ValueError,
-			    "glyph items must be an (i,x,y) sequence");
+			    "each glyph item must be an (i,x,y) sequence");
 	    goto error;
 	}
 	glyph->index = PyInt_AsLong(PySequence_Fast_GET_ITEM(py_seq, 0));
@@ -534,74 +532,56 @@
 	    goto error;
 	Py_DECREF(py_seq);
     }
+    Py_DECREF(py_glyphs);
+    return glyphs;
+ error:
+    Py_DECREF(py_glyphs);
+    Py_XDECREF(py_seq);
+    PyMem_Free(glyphs);
+    return NULL;
+}
+
+static PyObject *
+pycairo_glyph_extents (PycairoContext *o, PyObject *args)
+{
+    int num_glyphs = -1;
+    cairo_glyph_t *glyphs;
+    cairo_text_extents_t extents;
+    PyObject *py_object;
+
+    if (!PyArg_ParseTuple (args, "O|i:Context.glyph_extents",
+			   &py_object, &num_glyphs))
+	return NULL;
+
+    glyphs = _PyGlyphs_AsGlyphs (py_object, &num_glyphs);
+    if (glyphs == NULL)
+	return NULL;
     cairo_glyph_extents (o->ctx, glyphs, num_glyphs, &extents);
     PyMem_Free (glyphs);
-    Py_DECREF(py_glyphs);
     RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
-
     return Py_BuildValue("(dddddd)", extents.x_bearing, extents.y_bearing,
 			 extents.width, extents.height, extents.x_advance,
 			 extents.y_advance);
- error:
-    PyMem_Free (glyphs);
-    Py_DECREF(py_glyphs);
-    Py_XDECREF(py_seq);
-    return NULL;
 }
 
 static PyObject *
 pycairo_glyph_path (PycairoContext *o, PyObject *args)
 {
-    /* almost identical code to pycairo_show_glyphs */
-    int num_glyphs = -1, length, i;
-    cairo_glyph_t *glyphs = NULL, *glyph;
-    PyObject *py_glyphs, *py_seq = NULL;
+    int num_glyphs = -1;
+    cairo_glyph_t *glyphs;
+    PyObject *py_object;
 
     if (!PyArg_ParseTuple (args, "O|i:Context.glyph_path",
-			   &py_glyphs, &num_glyphs))
+			   &py_object, &num_glyphs))
 	return NULL;
 
-    py_glyphs = PySequence_Fast (py_glyphs,
-				 "first argument must be a sequence");
-    if (py_glyphs == NULL)
+    glyphs = _PyGlyphs_AsGlyphs (py_object, &num_glyphs);
+    if (glyphs == NULL)
 	return NULL;
-
-    length = PySequence_Fast_GET_SIZE(py_glyphs);
-    if (num_glyphs < 0 || num_glyphs > length)
-	num_glyphs = length;
-
-    glyphs = PyMem_Malloc (num_glyphs * sizeof(cairo_glyph_t));
-    if (glyphs == NULL) {
-	PyErr_NoMemory();
-	goto error;
-    }
-    for (i = 0, glyph = glyphs; i < num_glyphs; i++, glyph++) {
-	PyObject *py_item = PySequence_Fast_GET_ITEM(py_glyphs, i);
-	py_seq = PySequence_Fast (py_item, "glyph items must be a sequence");
-	if (py_seq == NULL)
-	    goto error;
-	if (PySequence_Fast_GET_SIZE(py_seq) != 3) {
-	    PyErr_SetString(PyExc_ValueError,
-			    "glyph items must be an (i,x,y) sequence");
-	    goto error;
-	}
-	glyph->index = PyInt_AsLong(PySequence_Fast_GET_ITEM(py_seq, 0));
-	glyph->x = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_seq, 1));
-	glyph->y = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_seq, 2));
-	if (PyErr_Occurred())
-	    goto error;
-	Py_DECREF(py_seq);
-    }
     cairo_glyph_path (o->ctx, glyphs, num_glyphs);
     PyMem_Free (glyphs);
-    Py_DECREF(py_glyphs);
     RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
     Py_RETURN_NONE;
- error:
-    PyMem_Free (glyphs);
-    Py_DECREF(py_glyphs);
-    Py_XDECREF(py_seq);
-    return NULL;
 }
 
 static PyObject *
@@ -1164,55 +1144,21 @@
 static PyObject *
 pycairo_show_glyphs (PycairoContext *o, PyObject *args)
 {
-    int num_glyphs = -1, length, i;
-    cairo_glyph_t *glyphs = NULL, *glyph;
-    PyObject *py_glyphs, *py_seq = NULL;
+    int num_glyphs = -1;
+    cairo_glyph_t *glyphs;
+    PyObject *py_object;
 
     if (!PyArg_ParseTuple (args, "O|i:Context.show_glyphs",
-			   &py_glyphs, &num_glyphs))
+			   &py_object, &num_glyphs))
 	return NULL;
 
-    py_glyphs = PySequence_Fast (py_glyphs,
-				 "first argument must be a sequence");
-    if (py_glyphs == NULL)
+    glyphs = _PyGlyphs_AsGlyphs (py_object, &num_glyphs);
+    if (glyphs == NULL)
 	return NULL;
-
-    length = PySequence_Fast_GET_SIZE(py_glyphs);
-    if (num_glyphs < 0 || num_glyphs > length)
-	num_glyphs = length;
-
-    glyphs = PyMem_Malloc (num_glyphs * sizeof(cairo_glyph_t));
-    if (glyphs == NULL) {
-	PyErr_NoMemory();
-	goto error;
-    }
-    for (i = 0, glyph = glyphs; i < num_glyphs; i++, glyph++) {
-	PyObject *py_item = PySequence_Fast_GET_ITEM(py_glyphs, i);
-	py_seq = PySequence_Fast (py_item, "glyph items must be a sequence");
-	if (py_seq == NULL)
-	    goto error;
-	if (PySequence_Fast_GET_SIZE(py_seq) != 3) {
-	    PyErr_SetString(PyExc_ValueError,
-			    "glyph items must be an (i,x,y) sequence");
-	    goto error;
-	}
-	glyph->index = PyInt_AsLong(PySequence_Fast_GET_ITEM(py_seq, 0));
-	glyph->x = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_seq, 1));
-	glyph->y = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_seq, 2));
-	if (PyErr_Occurred())
-	    goto error;
-	Py_DECREF(py_seq);
-    }
     cairo_show_glyphs (o->ctx, glyphs, num_glyphs);
     PyMem_Free (glyphs);
-    Py_DECREF(py_glyphs);
     RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
     Py_RETURN_NONE;
- error:
-    PyMem_Free (glyphs);
-    Py_DECREF(py_glyphs);
-    Py_XDECREF(py_seq);
-    return NULL;
 }
 
 static PyObject *



More information about the cairo-commit mailing list