[cairo] Loading a SVG file and displaying it on SDL

Emmanuel Pacaud emmanuel.pacaud at lapp.in2p3.fr
Fri Mar 9 06:50:08 PST 2007


Le vendredi 09 mars 2007 à 11:15 -0300, André Wagner a écrit :
> Hello!
> 
> I'm trying to load a SVG file and show it in a SDL window using cairo. This is my code:
> 
> 	RsvgHandle* h;
> 	int width = 500;
> 	int height = 500;
> 	int stride = width * 4;
> 
> 	unsigned char* cairo_data = 
> 		(unsigned char *) calloc(stride * height, 1);
> 	cairo_surface_t *cairo_surface =
> 		cairo_image_surface_create_for_data(cairo_data,
> 				CAIRO_FORMAT_ARGB32, width,
> 				height, stride);
> 
> 	cairo_t *cr = cairo_create(cairo_surface);
> 	cairo_save(cr);
> 	cairo_scale(cr, width, height);
> 
> 	cairo_set_source_rgba(cr, 1, 1, 1, 1);
> 	cairo_paint(cr);
> 
> 	GError* e = NULL;
> 	h = rsvg_handle_new_from_file("closedbox.svg", &e);
> 	if(e != NULL)
> 		exit(1);
> 
> 	rsvg_handle_render_cairo(h, cr);
> 
> 	SDL_Surface* sf;
> 	sf = SDL_CreateRGBSurfaceFrom((void *) cairo_data, width, 
> 			height, 32, stride, 0x00ff0000, 0x0000ff00, 
>                         0x000000ff, 0xff000000);
> 
> 	SDL_BlitSurface(sf, NULL, screen, NULL);
> 	SDL_UpdateRect(screen, 0, 0, 0, 0);
> 
> 	cairo_destroy(cr);
> 
> What am I doing wrong? It displays a white window, so I know (suppose) the transference from cairo to SDL is working. It seems the SVG image I loaded using librsvga is not showing on my cairo surface.
> 
> I'm not very skilled with cairo or librsvga, so I might just have a stupid mistake.

I don't know where is the issue, but it may help to check if the SVG
file is actually renderered in the cairo surface by saving it in a png
file, using:

surface = cairo_get_target (cr);
cairo_surface_write_to_png (surface, "test.png");

You may also want to have a look to the code of rsvg-convert, the
conversion utility included in librsvg:

http://svn.gnome.org/viewcvs/librsvg/trunk/rsvg-convert.c?revision=1094&view=markup

	Emmanuel.

-- 
Emmanuel PACAUD
Groupe Virgo
LAPP - Laboratoire d'Annecy-le-Vieux de physique des particules
9, chemin de Bellevue - BP 110
74941 Annecy-le-Vieux CEDEX
France
Tél: (+33)4 50 09 17 89 - Fax: (+33)4 50 27 94 95



More information about the cairo mailing list