Alex Rivera | Logout

How do I write an image into an SVG file using cairo?

Asked 2011-07-29T00:08:08.730
10

I have some code that looks like this:

cairo_surface_t * surface = cairo_svg_surface_create("0.svg", 512, 512);
cairo_t * context = cairo_create(surface);

int * data = new int[512*512];

// fill the data...

cairo_surface_t * image_surface = 
    cairo_image_surface_for_data(data, 512, 512, 512*4);
cairo_set_source_surface(context, image_surface, 0, 0);
cairo_paint(context);

// do some other drawing ...

cairo_surface_flush(surface);
cairo_surface_finish(surface);
cairo_surface_destroy(surface);
cairo_destroy(context);

However, the svg always appears corrupted. The image is not properly written, and all drawing commands following do not work. Changing the surface type to PS, ie:

cairo_surface_t * surface = cairo_ps_surface_create("0.ps", 512, 512);

produces a perfectly correct PS document. Any help fixing the SVG would be appreciated.

EDIT: Forgot to provide version info. Cairo 1.10.2 as given by cairo_version_string(). g++ 4.52 Running on Ubuntu 11.04

EDIT(2): Ok, I have traced this down to PNG problems with cairo and discovered that cairo_surface_write_to_png does not behave as expected either. Both this function and attempting to embed an image in an SVG cause "out of memory errors", and I still don't know why.

Edit
Report

1 Answer

2

Looks like you may have forgotten to specify the SVG version as:

cairo_svg_surface_restrict_to_version (surface, CAIRO_SVG_VERSION_1_2);

You can do this immediately after creating the surface.

answered 2011-08-04T11:41:19.377

Your Answer