Re: Simple SVG Example in C
Uli Schlachter <[email protected]>
| Newsgroups | gmane.comp.lib.cairo |
|---|---|
| Message-ID | <[email protected]> |
On 14.09.2012 17:25, Timothy Bolton wrote: > Hi all, > > I'm looking for a very simple example of getting Cairo to save to an SVG > file. I'm working in ANSI-C. > > I've gone through the documentation as best I could, I've searched > Google, mulled over StackOverflow and even cried a little. So far, I > haven't been able to get it to work. Writing a surface to a PNG works > for me, but for whatever reason, I'm at a loss for the SVG. > > I've included a pastebin of the code: http://pastebin.com/bDtFZ9Ww > > The environment that I'm working in is Ubuntu server (11 I think), and > I'm using gcc for compiling. Attached is a new version of that code which actually works. Changes that I did: - Instead of using a char array, this now has a char pointer for the filename (causes less garbage at the end of the filename) - The broken call to cairo_image_surface_create_for_data() and the usage of text_surface was removed, it's not important for getting this to work. - The problem you have was fixed by adding the following at the end of main: cairo_destroy(cr); cairo_surface_finish(surface); cairo_surface_destroy(surface); Always make sure to destroy ressources when you are done with them. The call to cairo_surface_finish() isn't strictly needed for your example because a surface is automatically finished when the last reference to it gets destroyed. However, this is what makes cairo actually write the svg out. Cheers, Uli -- "In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move." -- cairo mailing list [email protected] http://lists.cairographics.org/mailman/listinfo/cairo
t.c
(text/x-csrc, 1.2 KB)
#include "cairo.h"
#include "cairo-svg.h"
int main(int argc, char* argv[])
{
double font_size = 25.0,
height = 200.0,
left = 15.0,
top = 15.0,
width = 200.0;
char *filename = "temp.svg",
*text = "This is a Test";
cairo_surface_t *surface = cairo_svg_surface_create(filename,
width,
height);
//cairo_svg_surface_restrict_to_version (surface, CAIRO_SVG_VERSION_1_1);
cairo_t* cr = cairo_create (surface);
int size = height * width;
int data[size];
cairo_move_to (cr, left, top);
cairo_text_extents_t te;
cairo_select_font_face (cr,
"Georgia",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, (double)font_size);
cairo_text_extents (cr, text, &te);
cairo_show_text (cr, text);
cairo_fill(cr);
cairo_destroy(cr);
cairo_surface_finish(surface);
cairo_surface_destroy(surface);
return 0;
}