[cairo] pycairo missing functions

Steve Chaplin stevech1097 at yahoo.com.au
Sun Oct 31 05:39:21 PST 2004


I've started using pycairo and needed to use 
cairo_set_target_png()
cairo_set_target_ps()
cairo_show_page()
which were not available.

Here's the additions I made to pycairo-context.c to get them to work.

Regards,
Steve


static PyObject *
pycairo_set_target_png(PyCairoContext *self, PyObject *args)
{
    FILE *file;
    char *filename;
    cairo_format_t format;
    int width, height;

    if (!PyArg_ParseTuple(args, "siii:Context.set_target_png", 
			  &filename, &format, &width, &height))
	return NULL;

    if ((file = fopen (filename, "w")) == NULL) {
        PyErr_SetString(PyExc_IOError, "file open failed");
	return NULL;
	}

    cairo_set_target_png(self->ctx, file, format, width, height);
    if (pycairo_check_status(cairo_status(self->ctx)))
	return NULL;

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pycairo_set_target_ps(PyCairoContext *self, PyObject *args)
{
    FILE *file;
    char *filename;
    double width_inches, height_inches;
    double x_pixels_per_inch, y_pixels_per_inch;

    if (!PyArg_ParseTuple(args, "sdddd:Context.set_target_ps",
			  &filename, &width_inches, &height_inches, 
			  &x_pixels_per_inch, &y_pixels_per_inch))
	return NULL;

    if ((file = fopen (filename, "w")) == NULL) {
        PyErr_SetString(PyExc_IOError, "file open failed");
	return NULL;
	}

    cairo_set_target_ps(self->ctx, file, width_inches, height_inches, 
			x_pixels_per_inch, y_pixels_per_inch);
    if (pycairo_check_status(cairo_status(self->ctx)))
	return NULL;

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pycairo_show_page(PyCairoContext *self)
{
    cairo_show_page(self->ctx);
    if (pycairo_check_status(cairo_status(self->ctx)))
	return NULL;
    Py_INCREF(Py_None);
    return Py_None;
}


static PyMethodDef pycairo_methods[] = {
...
    { "set_target_png", (PyCFunction)pycairo_set_target_png,
METH_VARARGS },
    { "set_target_ps", (PyCFunction)pycairo_set_target_ps, METH_VARARGS
},
    { "show_page", (PyCFunction)pycairo_show_page, METH_NOARGS },
}





More information about the cairo mailing list