LIBRSVG 2.62.3
C library for rendering SVG
Loading...
Searching...
No Matches
Examples

Curated samples for librsvg 2.62.3.

Full programs live in examples/ in the source tree and in the librsvg-c-2.62.3 tarball. Build from that directory (needs pkg-config librsvg-2.0; Cairo samples also need cairo / cairo-png):

cc -o 01-render-document 01-render-document.c \
    $(pkg-config --cflags --libs librsvg-2.0 cairo cairo-png)

Run from examples/ so icon.svg is found. Prefer the modern calls shown here: rsvg_handle_render_document(), rsvg_handle_set_stylesheet(), rsvg_handle_get_pixbuf_and_error().

Catalogue

Sample Purpose
examples/01-render-document.c Load a file, render the whole SVG to Cairo, write PNG
examples/02-render-element.c Render one element (#dot)
examples/03-stylesheet.c Inject CSS (currentColor)
examples/04-geometry.c Intrinsic size and layer geometry
examples/05-from-memory.c Load SVG bytes, not a file
examples/06-pixbuf.c Render to a GdkPixbuf
examples/deprecated/render-cairo.c Deprecated Cairo call (kept for old code)
rsvg-convert Command-line converter

Shared fixture: examples/icon.svg (64×64; #bg uses currentColor, #dot is the inner circle).

Render a document

Load icon.svg, fit it into a 256×256 viewport, draw with Cairo, write a PNG. This is the usual application path.

APIs: rsvg_handle_new_from_file(), rsvg_handle_render_document().

Source: examples/01-render-document.c

handle = rsvg_handle_new_from_file ("icon.svg", &error);
if (handle == NULL)
return 1;
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 256, 256);
cr = cairo_create (surface);
viewport.x = 0.0;
viewport.y = 0.0;
viewport.width = 256.0;
viewport.height = 256.0;
if (!rsvg_handle_render_document (handle, cr, &viewport, &error))
return 1;
cairo_surface_write_to_png (surface, "01-out.png");
RsvgHandle * rsvg_handle_new_from_file(const gchar *filename, GError **error)
rsvg_handle_new_from_file: @filename: a file name @error: return location for a #GError
gboolean rsvg_handle_render_document(RsvgHandle *handle, cairo_t *cr, const RsvgRectangle *viewport, GError **error)
Draw the whole SVG into @viewport on @cr.

Render one element

Draw only the element whose id is dot. The id string must start with #. The viewport is the element’s box, not the whole document.

APIs: rsvg_handle_new_from_file(), rsvg_handle_render_element().

Source: examples/02-render-element.c

if (!rsvg_handle_render_element (handle, cr, "#dot", &viewport, &error))
return 1;
gboolean rsvg_handle_render_element(RsvgHandle *handle, cairo_t *cr, const char *id, const RsvgRectangle *element_viewport, GError **error)
rsvg_handle_render_element: @handle: an #RsvgHandle @cr: a Cairo context @id: (nullable): element id ...

Inject a stylesheet

Apply a user stylesheet before rendering so currentColor on #bg becomes red. The CSS length is required (not implied by strlen at the API; pass it explicitly).

APIs: rsvg_handle_set_stylesheet(), rsvg_handle_render_document().

Source: examples/03-stylesheet.c

const char *css = "* { color: #c0392b; }";
if (!rsvg_handle_set_stylesheet (handle, (const guint8 *) css,
strlen (css), &error))
return 1;
gboolean rsvg_handle_set_stylesheet(RsvgHandle *handle, const guint8 *css, gsize css_len, GError **error)
Set a CSS stylesheet for the SVG document.

Intrinsic size and layer geometry

Print the document’s intrinsic width/height/viewBox and pixel size, then the ink and logical boxes of #dot at a viewport. Optionally writes a PNG of the whole document.

APIs: rsvg_handle_get_intrinsic_dimensions(), rsvg_handle_get_intrinsic_size_in_pixels(), rsvg_handle_get_geometry_for_layer().

Source: examples/04-geometry.c

rsvg_handle_get_intrinsic_dimensions (handle, &has_w, &w, &has_h, &h,
&has_vb, &vb);
if (!rsvg_handle_get_geometry_for_layer (handle, "#dot", &viewport,
&ink, &logical, &error))
return 1;
void rsvg_handle_get_intrinsic_dimensions(RsvgHandle *handle, gboolean *out_has_width, RsvgLength *out_width, gboolean *out_has_height, RsvgLength *out_height, gboolean *out_has_viewbox, RsvgRectangle *out_viewbox)
rsvg_handle_get_intrinsic_dimensions: @handle: an #RsvgHandle @out_has_width: (out)(optional): whethe...
gboolean rsvg_handle_get_geometry_for_layer(RsvgHandle *handle, const char *id, const RsvgRectangle *viewport, RsvgRectangle *out_ink_rect, RsvgRectangle *out_logical_rect, GError **error)
Geometry of an element (or the whole SVG) as if rendered to @viewport.
gboolean rsvg_handle_get_intrinsic_size_in_pixels(RsvgHandle *handle, gdouble *out_width, gdouble *out_height)
rsvg_handle_get_intrinsic_size_in_pixels: @handle: an #RsvgHandle @out_width: (out)(optional): width ...

Load from memory

Build an SVG string in process memory and load it with rsvg_handle_new_from_data(). No icon.svg is required.

APIs: rsvg_handle_new_from_data(), rsvg_handle_render_document().

Source: examples/05-from-memory.c

static const char svg[] =
"<svg xmlns='http://www.w3.org/2000/svg' width='64' height='64'>"
"<rect width='64' height='64' fill='#27ae60'/>"
"<circle cx='32' cy='32' r='16' fill='#ecf0f1'/>"
"</svg>";
handle = rsvg_handle_new_from_data ((const guint8 *) svg, strlen (svg),
&error);
RsvgHandle * rsvg_handle_new_from_data(const guint8 *data, gsize data_len, GError **error)
rsvg_handle_new_from_data: @data: SVG bytes @data_len: length of @data @error: return location for a ...

GdkPixbuf

Render to a #GdkPixbuf without creating a Cairo surface. The pixbuf size uses the handle DPI (default 96) and the document’s intrinsic dimensions. Prefer this over the deprecated rsvg_handle_get_pixbuf().

APIs: rsvg_handle_new_from_file(), rsvg_handle_get_pixbuf_and_error().

Source: examples/06-pixbuf.c

pixbuf = rsvg_handle_get_pixbuf_and_error (handle, &error);
if (pixbuf == NULL)
return 1;
gdk_pixbuf_save (pixbuf, "06-out.png", "png", &error, NULL);
g_object_unref (pixbuf);
GdkPixbuf * rsvg_handle_get_pixbuf_and_error(RsvgHandle *handle, GError **error)
Render the SVG to a #GdkPixbuf.

rsvg-convert

Command-line tool shipped with the library. Default DPI is 96. rsvg-convert -d 90 -p 90 matches the older CLI default.

Full source: rsvg-convert.c. Manual: rsvg-convert(1).

rsvg-convert icon.svg -o icon.png
rsvg-convert -w 256 -h 256 icon.svg -o icon.png
rsvg-convert -d 90 -p 90 in.svg -o out.png
rsvg-convert -f pdf in.svg -o out.pdf

Deprecated Cairo render

rsvg_handle_render_cairo() has no viewport and no #GError. New code should use rsvg_handle_render_document() (see Render a document). This file is only a translation aid.

APIs: rsvg_handle_render_cairo() (deprecated since 2.52).

Source: examples/deprecated/render-cairo.c

#define RSVG_DISABLE_DEPRECATION_WARNINGS
#include <librsvg/rsvg.h>
if (!rsvg_handle_render_cairo (handle, cr))
return 1;
gboolean rsvg_handle_render_cairo(RsvgHandle *handle, cairo_t *cr)
rsvg_handle_render_cairo: @handle: an #RsvgHandle @cr: a Cairo context

Other in-tree programs

These are not copy-paste public API samples:

  • test-display.c — GTK widget that views an SVG. It includes private headers (rsvg-private.h) and is not an application template.
  • tools/rsvg-dimensions.c — measuring helper used in development.
  • gdk-pixbuf-loader/io-svg.c — the gdk-pixbuf module (libpixbufloader-svg).

Public C API groups: Loading, Rendering, Geometry, Stylesheet, Pixbuf.