|
LIBRSVG 2.62.3
C library for rendering SVG
|
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().
| 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).
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
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
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
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
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
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
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
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
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.