From 0bb78c6bfc6bb58e06478d7d35f15544c7a456a1 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 13 Sep 2021 18:21:04 -0500 Subject: Include an example of usage from C Holy crap, including C code into DocBook actually works! Part-of: --- doc/load-and-render.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ doc/overview.xml | 66 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 doc/load-and-render.c (limited to 'doc') diff --git a/doc/load-and-render.c b/doc/load-and-render.c new file mode 100644 index 00000000..cac59eee --- /dev/null +++ b/doc/load-and-render.c @@ -0,0 +1,59 @@ +/* gcc -Wall -g -O2 -o load-and-render load-and-render.c `pkg-config --cflags --libs rsvg-2.0` */ + +#include +#include + +#define WIDTH 640 +#define HEIGHT 480 + +int +main (void) +{ + /* First, load an SVG document into an RsvgHandle */ + + GError *error = NULL; + GFile *file = g_file_new_for_path ("hello.svg"); + RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file, RSVG_HANDLE_FLAGS_NONE, NULL, &error); + + if (!handle) + { + g_printerr ("could not load: %s", error->message); + exit (1); + } + + /* Create a Cairo image surface and a rendering context for it */ + + cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT); + cairo_t *cr = cairo_create (surface); + + /* Render the handle scaled proportionally into that whole surface */ + + RsvgRectangle viewport = { + .x = 0.0, + .y = 0.0, + .width = WIDTH, + .height = HEIGHT, + }; + + if (!rsvg_handle_render_document (handle, cr, &viewport, &error)) + { + g_printerr ("could not render: %s", error->message); + exit (1); + } + + /* Write a PNG file */ + + if (cairo_surface_write_to_png (surface, "hello.png") != CAIRO_STATUS_SUCCESS) + { + g_printerr ("could not write output file"); + exit (1); + } + + /* Free our memory and we are done! */ + + cairo_destroy (cr); + cairo_surface_destroy (surface); + g_object_unref (handle); + g_object_unref (file); + return 0; +} diff --git a/doc/overview.xml b/doc/overview.xml index c930d887..d0d3eea2 100644 --- a/doc/overview.xml +++ b/doc/overview.xml @@ -11,7 +11,7 @@ it to render itself to a Cairo context. - +
Loading @@ -23,9 +23,19 @@ loading completes successfully, the RsvgHandle will be ready for rendering. - - + + Generally you should use + rsvg_handle_new_from_gfile_sync() or + rsvg_handle_new_from_stream_sync() to load + an SVG document into an RsvgHandle. There are other convenience + functions to load an SVG document, but these two functions let + one set the "base file" and the RsvgHandleFlags in a single + call. + +
+ +
Rendering @@ -36,5 +46,53 @@ render only that element; this is so that sub-elements can be extracted conveniently out of a larger SVG. - + + + Generally you should use + rsvg_handle_render_document() to render the + whole SVG document at any size you choose into a Cairo context. + +
+ +
+ Example: simple loading and rendering + + + The following program loads hello.svg, + renders it scaled to fit within 640x480 pixels, and writes a + hello.png file. + + + + Note the following: + + + + + + rsvg_handle_render_document() will + scale the document proportionally to fit the viewport you + specify, and it will center the image within that viewport. + + + + + + Librsvg does not paint a background color by default, so in + the following example all unfilled areas of the SVG will + appear as fully transparent. If you wish to have a specific + background, fill the viewport area yourself before rendering + the SVG. + + + + + + Load and render an SVG document as a PNG + + + + + +
-- cgit v1.2.1