Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

svg

cairo

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.

like image 671
Scott Avatar asked Jul 29 '11 00:07

Scott


1 Answers

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.

like image 158
Seth Avatar answered Oct 06 '22 13:10

Seth