| 1 | --- |
| 2 | Title: Overview of Librsvg |
| 3 | --- |
| 4 | |
| 5 | # Overview of Librsvg |
| 6 | |
| 7 | Librsvg is a library for rendering Scalable Vector Graphics files (SVG). |
| 8 | Specifically, it can take non-animated, non-scripted SVG documents and |
| 9 | render them into a [Cairo](https://www.cairographics.org/) surface. |
| 10 | Normally this means an in-memory raster surface, but it could also be |
| 11 | any of the other surface types that Cairo supports. |
| 12 | |
| 13 | Librsvg supports many of the graphic features in the [SVG |
| 14 | 1.1](https://www.w3.org/TR/SVG/) and [SVG2](https://www.w3.org/TR/SVG2/) |
| 15 | specifications. The main features of SVG that librsvg does not support |
| 16 | are the following: |
| 17 | |
| 18 | * Scripting or animation: Librsvg reads SVG data and renders it to a static |
| 19 | image. There is no provision to execute scripts that may control animation |
| 20 | parameters. |
| 21 | |
| 22 | * Access to the DOM: Librsvg creates an internal representation of the SVG data, |
| 23 | but it does not provide outside access to the resulting Document Object Model |
| 24 | (DOM). |
| 25 | |
| 26 | * SVG fonts: Instead, librsvg relies on the system's fonts, particularly those |
| 27 | that are available through Cairo/Pango. |
| 28 | |
| 29 | Librsvg's API is divided into two main parts: one for loading SVG data |
| 30 | and one for rendering it. In the *loading stage*, you create an |
| 31 | [[email protected]] object from SVG data, which can come from a file or from a |
| 32 | stream of bytes. In the *rendering stage*, you take an [[email protected]] and |
| 33 | ask it to render itself to a Cairo context. |
| 34 | |
| 35 | ## Loading |
| 36 | |
| 37 | [[email protected]] is an object that represents SVG data in memory. Your |
| 38 | program creates an [[email protected]] from an SVG file, or from a memory buffer |
| 39 | that contains SVG data, or in the most general form, from a |
| 40 | [[email protected]] that will provide SVG data. At this stage you can get |
| 41 | either I/O errors or parsing errors. If loading completes successfully, the |
| 42 | [[email protected]] will be ready for rendering. |
| 43 | |
| 44 | Generally you should use [[email protected]_from_gfile_sync] or |
| 45 | [[email protected]_from_stream_sync] to load an SVG document into an |
| 46 | [[email protected]]. There are other convenience functions to load an SVG |
| 47 | document, but these two functions let one set the " |
| 48 | [base file](class.Handle.html#the-base-file-and-resolving-references-to-external-files) |
| 49 | " and the [[email protected]] in a single call. |
| 50 | |
| 51 | ## Rendering |
| 52 | |
| 53 | Once you have an SVG image loaded into an [[email protected]], you can render it |
| 54 | to a Cairo context any number of times, or to different Cairo contexts, |
| 55 | as needed. As a convenience, you can pick a single element in the SVG by |
| 56 | its `id` attribute and render only that element; this is so that |
| 57 | sub-elements can be extracted conveniently out of a larger SVG. |
| 58 | |
| 59 | Generally you should use [[email protected]_document] to render the |
| 60 | whole SVG document at any size you choose into a Cairo context. |
| 61 | |
| 62 | ## Example: simple loading and rendering |
| 63 | |
| 64 | The following program loads `hello.svg`, renders it scaled to fit within |
| 65 | 640×480 pixels, and writes a `hello.png` file. |
| 66 | |
| 67 | Note the following: |
| 68 | |
| 69 | * [[email protected]_document] will scale the document proportionally |
| 70 | to fit the viewport you specify, and it will center the image within that |
| 71 | viewport. |
| 72 | |
| 73 | * Librsvg does not paint a background color by default, so in the following |
| 74 | example all unfilled areas of the SVG will appear as fully transparent. If you |
| 75 | wish to have a specific background, fill the viewport area yourself before |
| 76 | rendering the SVG. |
| 77 | |
| 78 | ```c |
| 79 | /* gcc -Wall -g -O2 -o load-and-render load-and-render.c `pkg-config --cflags --libs rsvg-2.0` */ |
| 80 | |
| 81 | #include <stdlib.h> |
| 82 | #include <librsvg/rsvg.h> |
| 83 | |
| 84 | #define WIDTH 640 |
| 85 | #define HEIGHT 480 |
| 86 | |
| 87 | int |
| 88 | main (void) |
| 89 | { |
| 90 | /* First, load an SVG document into an RsvgHandle */ |
| 91 | |
| 92 | GError *error = NULL; |
| 93 | GFile *file = g_file_new_for_path ("hello.svg"); |
| 94 | RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file, RSVG_HANDLE_FLAGS_NONE, NULL, &error); |
| 95 | |
| 96 | if (!handle) |
| 97 | { |
| 98 | g_printerr ("could not load: %s", error->message); |
| 99 | exit (1); |
| 100 | } |
| 101 | |
| 102 | /* Create a Cairo image surface and a rendering context for it */ |
| 103 | |
| 104 | cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT); |
| 105 | cairo_t *cr = cairo_create (surface); |
| 106 | |
| 107 | /* Set the dots-per-inch */ |
| 108 | |
| 109 | rsvg_handle_set_dpi (handle, 96.0); |
| 110 | |
| 111 | /* Render the handle scaled proportionally into that whole surface */ |
| 112 | |
| 113 | RsvgRectangle viewport = { |
| 114 | .x = 0.0, |
| 115 | .y = 0.0, |
| 116 | .width = WIDTH, |
| 117 | .height = HEIGHT, |
| 118 | }; |
| 119 | |
| 120 | if (!rsvg_handle_render_document (handle, cr, &viewport, &error)) |
| 121 | { |
| 122 | g_printerr ("could not render: %s", error->message); |
| 123 | exit (1); |
| 124 | } |
| 125 | |
| 126 | /* Write a PNG file */ |
| 127 | |
| 128 | if (cairo_surface_write_to_png (surface, "hello.png") != CAIRO_STATUS_SUCCESS) |
| 129 | { |
| 130 | g_printerr ("could not write output file"); |
| 131 | exit (1); |
| 132 | } |
| 133 | |
| 134 | /* Free our memory and we are done! */ |
| 135 | |
| 136 | cairo_destroy (cr); |
| 137 | cairo_surface_destroy (surface); |
| 138 | g_object_unref (handle); |
| 139 | g_object_unref (file); |
| 140 | return 0; |
| 141 | } |
| 142 | ``` |