[cairo-commit] pycairo/cairo pycairo-surface.c,1.55,1.56
Steve Chaplin
commit at pdx.freedesktop.org
Thu Aug 25 04:48:11 PDT 2005
Committed by: stevech1097
Update of /cvs/cairo/pycairo/cairo
In directory gabe:/tmp/cvs-serv1415/cairo
Modified Files:
pycairo-surface.c
Log Message:
'SC'
Index: pycairo-surface.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-surface.c,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -d -r1.55 -r1.56
--- pycairo-surface.c 22 Aug 2005 06:37:37 -0000 1.55
+++ pycairo-surface.c 25 Aug 2005 11:48:09 -0000 1.56
@@ -191,16 +191,41 @@
}
#ifdef CAIRO_HAS_PNG_FUNCTIONS
+static cairo_status_t
+pycairo_write_func (void *closure, unsigned char *data, unsigned int length)
+{
+ if (fwrite (data, 1, (size_t) length, (FILE *)closure) != length)
+ return CAIRO_STATUS_WRITE_ERROR;
+ return CAIRO_STATUS_SUCCESS;
+}
+
+/* METH_O */
static PyObject *
-surface_write_to_png (PycairoSurface *o, PyObject *args)
+surface_write_to_png (PycairoSurface *o, PyObject *file)
{
- const char *filename;
+ FILE *fp;
cairo_status_t status;
- if (!PyArg_ParseTuple (args, "s:Surface.write_to_png", &filename))
+ if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
+ fp = fopen (PyString_AsString(file), "wb");
+ if (fp == NULL) {
+ PyErr_SetString(PyExc_IOError, "unable to open file for writing");
+ return NULL;
+ }
+ } else if (PyObject_TypeCheck (file, &PyFile_Type)) {
+ fp = PyFile_AsFile(file);
+
+ } else {
+ PyErr_SetString(PyExc_TypeError,
+ "Surface.write_to_png takes one argument "
+ "which must be a filename (str) or file object");
return NULL;
+ }
+ status = cairo_surface_write_to_png_stream (o->surface, pycairo_write_func,
+ fp);
+ if (PyObject_TypeCheck (file, &PyBaseString_Type))
+ fclose (fp);
- status = cairo_surface_write_to_png (o->surface, filename);
if (Pycairo_Check_Status (status))
return NULL;
Py_RETURN_NONE;
@@ -223,8 +248,7 @@
{"set_device_offset",(PyCFunction)surface_set_device_offset,
METH_VARARGS },
#ifdef CAIRO_HAS_PNG_FUNCTIONS
- {"write_to_png", (PyCFunction)surface_write_to_png, METH_VARARGS },
- /* write_to_png_stream */
+ {"write_to_png", (PyCFunction)surface_write_to_png, METH_O },
#endif
{NULL, NULL, 0, NULL},
};
More information about the cairo-commit
mailing list