| 1 |
/* gcc -Wall -g -O2 -o load-and-render load-and-render.c `pkg-config --cflags --libs rsvg-2.0` */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <librsvg/rsvg.h> |
| 5 |
|
| 6 |
#define WIDTH 640 |
| 7 |
#define HEIGHT 480 |
| 8 |
|
| 9 |
int |
| 10 |
main (void) |
| 11 |
{ |
| 12 |
/* First, load an SVG document into an RsvgHandle */ |
| 13 |
|
| 14 |
GError *error = NULL; |
| 15 |
GFile *file = g_file_new_for_path ("hello.svg"); |
| 16 |
RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file, RSVG_HANDLE_FLAGS_NONE, NULL, &error); |
| 17 |
|
| 18 |
if (!handle) |
| 19 |
{ |
| 20 |
g_printerr ("could not load: %s", error->message); |
| 21 |
exit (1); |
| 22 |
} |
| 23 |
|
| 24 |
/* Create a Cairo image surface and a rendering context for it */ |
| 25 |
|
| 26 |
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT); |
| 27 |
cairo_t *cr = cairo_create (surface); |
| 28 |
|
| 29 |
/* Render the handle scaled proportionally into that whole surface */ |
| 30 |
|
| 31 |
RsvgRectangle viewport = { |
| 32 |
.x = 0.0, |
| 33 |
.y = 0.0, |
| 34 |
.width = WIDTH, |
| 35 |
.height = HEIGHT, |
| 36 |
}; |
| 37 |
|
| 38 |
if (!rsvg_handle_render_document (handle, cr, &viewport, &error)) |
| 39 |
{ |
| 40 |
g_printerr ("could not render: %s", error->message); |
| 41 |
exit (1); |
| 42 |
} |
| 43 |
|
| 44 |
/* Write a PNG file */ |
| 45 |
|
| 46 |
if (cairo_surface_write_to_png (surface, "hello.png") != CAIRO_STATUS_SUCCESS) |
| 47 |
{ |
| 48 |
g_printerr ("could not write output file"); |
| 49 |
exit (1); |
| 50 |
} |
| 51 |
|
| 52 |
/* Free our memory and we are done! */ |
| 53 |
|
| 54 |
cairo_destroy (cr); |
| 55 |
cairo_surface_destroy (surface); |
| 56 |
g_object_unref (handle); |
| 57 |
g_object_unref (file); |
| 58 |
return 0; |
| 59 |
} |