/* METH_O | METH_CLASS */ static PyObject * image_surface_create_from_png (PyTypeObject *type, PyObject *file) { PyObject* reader; if (PyObject_TypeCheck (file, &PyBaseString_Type)) { return PycairoSurface_FromSurface ( cairo_image_surface_create_from_png (PyString_AsString(file)), NULL); } /* file or file-like object argument */ reader = PyObject_GetAttrString (file, "read"); if (reader == NULL || !PyCallable_Check (reader)) { Py_XDECREF(reader); PyErr_SetString(PyExc_TypeError, "ImageSurface.create_from_png argument must be a filename (str), file object, " "or an object that has a \"read\" method (like StringIO)"); return NULL; } Py_DECREF(reader); return PycairoSurface_FromSurface ( cairo_image_surface_create_from_png_stream (_read_func, file), NULL); }
static PyObject * svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { double width_in_points, height_in_points; PyObject *file, *writer; if (!PyArg_ParseTuple(args, "Odd:SVGSurface.__new__", &file, &width_in_points, &height_in_points)) return NULL; if (PyObject_TypeCheck (file, &PyBaseString_Type)) { /* string (filename) argument */ return PycairoSurface_FromSurface ( cairo_svg_surface_create (PyString_AsString(file), width_in_points, height_in_points), NULL); } /* else: file or file-like object argument */ writer = PyObject_GetAttrString (file, "write"); if (writer == NULL || !PyCallable_Check (writer)) { Py_XDECREF(writer); PyErr_SetString(PyExc_TypeError, "SVGSurface argument 1 must be a filename (str), file object, or an object " "that has a \"write\" method (like StringIO)"); return NULL; } Py_DECREF(writer); return PycairoSurface_FromSurface ( cairo_svg_surface_create_for_stream (_write_func, file, width_in_points, height_in_points), file); }
static PyObject *get_debug_surface(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "")) return NULL; if ((!sdaps_create_debug_surface) || (sdaps_debug_surface == NULL)) { Py_INCREF(Py_None); return Py_None; } else { PyObject *result; PyObject *pysurface; cairo_surface_reference(sdaps_debug_surface); pysurface = (PyObject*) PycairoSurface_FromSurface(sdaps_debug_surface, NULL); if (pysurface == NULL) return NULL; result = Py_BuildValue("Nii", pysurface, sdaps_debug_surface_ox, sdaps_debug_surface_oy); if (result == NULL) Py_DECREF(pysurface); return result; } }
/* METH_O | METH_CLASS */ static PyObject * image_surface_create_from_png (PyTypeObject *type, PyObject *o) { FILE *fp; cairo_surface_t *surface; if (PyObject_TypeCheck (o, &PyBaseString_Type)) { fp = fopen (PyString_AsString(o), "rb"); if (fp == NULL) { PyErr_SetString(PyExc_IOError, "unable to open file for reading"); return NULL; } } else if (PyObject_TypeCheck (o, &PyFile_Type)) { fp = PyFile_AsFile(o); } else { PyErr_SetString(PyExc_TypeError, "ImageSurface.create_from_png takes one argument " "which must be a filename (str) or file object"); return NULL; } surface = cairo_image_surface_create_from_png_stream (_read_func, fp); if (PyObject_TypeCheck (o, &PyBaseString_Type)) fclose (fp); return PycairoSurface_FromSurface (surface, &PycairoImageSurface_Type, NULL); }
static PyObject * surface_pattern_get_surface (PycairoSurfacePattern *o) { cairo_surface_t *surface; cairo_pattern_get_surface (o->pattern, &surface); return PycairoSurface_FromSurface ( cairo_surface_reference (surface), NULL); }
static PyObject * pycairo_get_group_target (PycairoContext *o) { cairo_surface_t *surface = cairo_get_group_target (o->ctx); if (surface != NULL) return PycairoSurface_FromSurface (cairo_surface_reference (surface), NULL); Py_RETURN_NONE; }
static PyObject * image_surface_create_for_array (PyTypeObject *type, PyObject *args) { PyArrayObject *array; cairo_format_t format; cairo_surface_t *surface; int nd; if (!load_numpy()) return NULL; if (!PyArg_ParseTuple(args, "O!:surface_create_for_array", &PyArray_Type, &array)) return NULL; if (array->descr->type_num != PyArray_UBYTE) { PyErr_SetString(PyExc_TypeError, "array data must be unsigned bytes"); return NULL; } nd = array->nd; if (nd < 2) { PyErr_SetString(PyExc_TypeError, "array must have at least two dimensions"); return NULL; } if (nd == 2 || (nd == 3 && array->dimensions[2] == 1)) { if (array->strides[1] != 1) { PyErr_SetString(PyExc_TypeError, "second axis must be contiguous"); return NULL; } format = CAIRO_FORMAT_A8; } else if (nd == 3 && array->dimensions[2] == 3) { if (array->strides[1] != 3) { PyErr_SetString(PyExc_TypeError, "second axis must be contiguous"); return NULL; } format = CAIRO_FORMAT_RGB24; } else if (nd == 3 && array->dimensions[2] == 4) { if (array->strides[1] != 4) { PyErr_SetString(PyExc_TypeError, "second axis must be contiguous"); return NULL; } format = CAIRO_FORMAT_ARGB32; } else { PyErr_SetString(PyExc_TypeError, "array must be MxN or MxNxP where P is 1, 3 or 4"); return NULL; } surface = cairo_image_surface_create_for_data( (unsigned char *) array->data, format, array->dimensions[1], array->dimensions[0], array->strides[0]); return PycairoSurface_FromSurface(surface, &PycairoImageSurface_Type, (PyObject *)array); }
static PyObject * win32_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { int hdc; if (!PyArg_ParseTuple(args, "i:Win32Surface.__new__", &hdc)) return NULL; return PycairoSurface_FromSurface ( cairo_win32_surface_create ((HDC)hdc), NULL); }
static PyObject * pycairo_get_target (PycairoContext *o) { cairo_surface_t *surface = cairo_get_target (o->ctx); cairo_surface_reference (surface); /* bug #2765 - "How do we identify surface types?" * should pass surface type as arg2 */ return PycairoSurface_FromSurface (surface, NULL, NULL); }
static PyObject * image_surface_create_for_data (PyTypeObject *type, PyObject *args) { cairo_surface_t *surface; cairo_format_t format; unsigned char *buffer; int width, height, stride = -1, res; Py_ssize_t buffer_len; PyObject *obj; if (!PyArg_ParseTuple(args, "Oiii|i:Surface.create_for_data", &obj, &format, &width, &height, &stride)) return NULL; res = PyObject_AsWriteBuffer (obj, (void **)&buffer, &buffer_len); if (res == -1) return NULL; if (width <= 0) { PyErr_SetString(PyExc_ValueError, "width must be positive"); return NULL; } if (height <= 0) { PyErr_SetString(PyExc_ValueError, "height must be positive"); return NULL; } /* if stride is missing, calculate it from width */ if (stride < 0) { switch (format) { case CAIRO_FORMAT_ARGB32: case CAIRO_FORMAT_RGB24: stride = width * 4; break; case CAIRO_FORMAT_RGB16_565: stride = width * 2; break; case CAIRO_FORMAT_A8: stride = width; break; case CAIRO_FORMAT_A1: /* should be stride = (width + 31) / 32 * 4 ? */ stride = (width + 1) / 8; break; default: PyErr_SetString(CairoError, "Unknown format"); return NULL; } } if (height * stride > buffer_len) { PyErr_SetString(PyExc_TypeError, "buffer is not long enough"); return NULL; } surface = cairo_image_surface_create_for_data (buffer, format, width, height, stride); return PycairoSurface_FromSurface(surface, obj); }
static PyObject * surface_pattern_get_surface (PycairoSurfacePattern *o) { if (o->base != NULL) { // surface_pattern was created using surface_pattern_new() return Py_BuildValue("O", o->base); } else { cairo_surface_t *surface; cairo_pattern_get_surface (o->pattern, &surface); return PycairoSurface_FromSurface(cairo_surface_reference (surface), NULL); } }
static PyObject * image_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { cairo_format_t format; int width, height; if (!PyArg_ParseTuple (args, "iii:ImageSurface.__new__", &format, &width, &height)) return NULL; return PycairoSurface_FromSurface ( cairo_image_surface_create (format, width, height), NULL); }
static PyObject * surface_create_similar (PycairoSurface *o, PyObject *args) { cairo_content_t content; int width, height; if (!PyArg_ParseTuple (args, "iii:Surface.create_similar", &content, &width, &height)) return NULL; return PycairoSurface_FromSurface ( cairo_surface_create_similar (o->surface, content, width, height), NULL); }
static PyObject * surface_create_similar (PycairoSurface *o, PyObject *args) { cairo_surface_t *surface; cairo_content_t content; int width, height; if (!PyArg_ParseTuple (args, "iii:Surface.create_similar", &content, &width, &height)) return NULL; surface = cairo_surface_create_similar (o->surface, content, width, height); /* bug #2765 - "How do we identify surface types?" * should pass surface type as arg2 */ return PycairoSurface_FromSurface (surface, NULL, NULL); }
static PyObject * wrap_get_a1_from_tiff(PyObject *self, PyObject *args) { cairo_surface_t *surface; const char *filename = NULL; gboolean rotated; gint page; if (!PyArg_ParseTuple(args, "sii", &filename, &page, &rotated)) return NULL; surface = get_a1_from_tiff(filename, page, rotated); if (surface) { return PycairoSurface_FromSurface(surface, NULL); } else { PyErr_SetString(PyExc_AssertionError, "The image surface could not be created! Broken or non 1bit tiff file?"); return NULL; } }
static PyObject * pycairo_get_target (PycairoContext *o) { return PycairoSurface_FromSurface ( cairo_surface_reference (cairo_get_target (o->ctx)), NULL); }
/* * This function must be called with a range of samples, and a desired * width and height. * It will average samples if needed. */ static PyObject * py_fill_surface (PyObject * self, PyObject * args) { PyObject *samples; PyObject *sampleObj; int length, i; double sample; cairo_surface_t *surface; cairo_t *ctx; int width, height; float pixelsPerSample; float currentPixel; int samplesInAccum; float x = 0.; float lastX = 0.; double accum; double lastAccum = 0.; if (!PyArg_ParseTuple (args, "O!ii", &PyList_Type, &samples, &width, &height)) return NULL; length = PyList_Size (samples); surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); ctx = cairo_create (surface); cairo_set_source_rgb (ctx, 0.2, 0.6, 0.0); cairo_set_line_width (ctx, 0.5); cairo_move_to (ctx, 0, height); pixelsPerSample = width / (float) length; currentPixel = 0.; samplesInAccum = 0; accum = 0.; for (i = 0; i < length; i++) { /* Guaranteed to return something */ sampleObj = PyList_GetItem (samples, i); sample = PyFloat_AsDouble (sampleObj); /* If the object was not a float or convertible to float */ if (PyErr_Occurred ()) { cairo_surface_finish (surface); Py_DECREF (samples); return NULL; } currentPixel += pixelsPerSample; samplesInAccum += 1; accum += sample; if (currentPixel > 1.0) { accum /= samplesInAccum; cairo_line_to (ctx, x, height - accum); lastAccum = accum; accum = 0; currentPixel -= 1.0; samplesInAccum = 0; lastX = x; } x += pixelsPerSample; } Py_DECREF (samples); cairo_line_to (ctx, width, height); cairo_close_path (ctx); cairo_fill_preserve (ctx); return PycairoSurface_FromSurface (surface, NULL); }
static PyObject * _cairo_surface_from_gvalue(const GValue *value) { return PycairoSurface_FromSurface((cairo_surface_t *) g_value_get_boxed(value), NULL); }