[cairo] Simple SVG Example in C

Uli Schlachter psychon at znc.in
Fri Sep 14 08:53:30 PDT 2012


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."
-------------- next part --------------
A non-text attachment was scrubbed...
Name: t.c
Type: text/x-csrc
Size: 1182 bytes
Desc: not available
URL: <http://lists.cairographics.org/archives/cairo/attachments/20120914/6ac47ff8/attachment-0001.c>


More information about the cairo mailing list