Debian Codegraph @

doc/recommendations.md

raw symbols SCIP
1 ---
2 Title: Recommendations for Applications
3 ---
4
5 # Recommendations for Applications
6
7 Let's consider two common cases for rendering SVG documents:
8
9 * Your application uses fixed-size assets, for example, "all icons at 16×16
10 pixels".
11
12 * Your application needs to accept arbitrarily-sized SVG documents, to either
13 render them at a fixed size, or to render them at a "natural" size.
14
15 In either case, librsvg assumes that for rendering you have already
16 obtained a Cairo surface, and a Cairo context to draw on the surface.
17 For the case of fixed-size assets, this is easy; you create a surface
18 of the size you know you want, and tell librsvg to render to it at
19 that exact size. For the case of wanting to use a "natural" size, you
20 first have to ask librsvg about the document's size so you can create
21 an appropriately-sized surface. Let's see how to do both cases.
22
23 ## Rendering SVG assets at a fixed size
24
25 This is the case when you have a known `WIDTH` and `HEIGHT`, and you
26 want to tell librsvg to render an SVG at that size:
27
28 ```c
29 GError *error = NULL;
30 GFile *file = g_file_new_for_path ("hello.svg");
31 RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file, RSVG_HANDLE_FLAGS_NONE, NULL, &error); /* 1 */
32
33 if (!handle)
34 {
35 g_error ("could not load: %s", error->message);
36 }
37
38 cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT); /* 2 */
39 cairo_t *cr = cairo_create (surface);
40
41 /* Render the handle scaled proportionally into that whole surface */
42
43 RsvgRectangle viewport = { /* 3 */
44 .x = 0.0,
45 .y = 0.0,
46 .width = WIDTH,
47 .height = HEIGHT,
48 };
49
50 if (!rsvg_handle_render_document (handle, cr, &viewport, &error)) /* 4 */
51 {
52 g_error ("could not render: %s", error->message);
53 }
54
55 /* The surface is now rendered */
56 ```
57
58 1. Load the SVG document.
59
60 2. Create an image surface of the size you want.
61
62 3. Declare a viewport of that size. If you want a non-zero `(x, y)` offset you
63 can set it right there.
64
65 4. Render the document within that viewport. Done!
66
67 This will scale the SVG document proportionally to make it fit in
68 `WIDTH×HEIGHT` pixels.
69
70 ## Picking a "natural" size for SVG documents
71
72 In some cases, your application may not want to use a predefined size,
73 but instead query the SVG for a "natural" size at which to render. Some
74 SVG documents make this easy, and some don't.
75
76 ### SVG documents with intrinsic dimensions in absolute units
77
78 Consider an SVG document that starts like this:
79
80 ```xml
81 <svg xmlns="http://www.w3.org/2000/svg" width="200" height="100">
82 ```
83
84 This is completely unambiguous; the SVG says that its intrinsic size
85 is 200×100 pixels. It can be scaled arbitrarily, but of course most
86 of the time you'll want to scale it proportionally at that 2:1 ratio.
87
88 Here is a slightly more complicated case:
89
90 ```xml
91 <svg xmlns="http://www.w3.org/2000/svg" width="10cm" height="5cm">
92 ```
93
94 The SVG says that its intrinsic size is 10cm × 5cm. This is not hard
95 to convert to pixels if you know the Dots-Per-Inch (DPI) at which you
96 want to render things, but it is a small inconvenience.
97
98 And here is a more complicated case still:
99
100 ```xml
101 <svg xmlns="http://www.w3.org/2000/svg" width="10em" height="5em">
102 <style>
103 * { font-size: 2cm; }
104 </style>
105 ```
106
107 This means that the width is 10 times the font size of 2cm, and the
108 height is 5 times the font size.
109
110 In general an application cannot figure this out easily, since it
111 would need a CSS parser and cascading engine to even be able to know
112 what the font size is for the toplevel `<svg>`. Fortunately, librsvg
113 already does that!
114
115 In all those cases, the width and height are in physical units (px,
116 cm, mm, etc.), or font-based units (em, ex) that can be resolved to
117 pixels. You can use [[email protected]_intrinsic_size_in_pixels]
118 to do the conversion easily if all you want to do is to create a
119 surface with the "natural" number of pixels:
120
121 ```c
122 gboolean rsvg_handle_get_intrinsic_size_in_pixels (RsvgHandle *handle,
123 gdouble *out_width,
124 gdouble *out_height);
125 ```
126
127 However, the documentation for [that
128 function](method.Handle.get_intrinsic_size_in_pixels.html) indicates
129 that it may return `FALSE` in some cases. Let's see what those ugly
130 cases are.
131
132 ### SVG documents with just proportions
133
134 What if we have this:
135
136 ```xml
137 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100">
138 ```
139
140 This document has no intrinsic dimensions, but it clearly states that
141 its *proportions* are 200:100, or 2:1. It will look good when scaled
142 to a rectangle that is twice as wide as it is tall. Our old friend
143 [[email protected]_document] will still scale the SVG
144 proportionally to fit the viewport size you pass in. But what if you
145 must pick a size yourself, instead of having a predefined viewport?
146
147 In that case you bite the bullet, call
148 [[email protected]_intrinsic_dimensions] and make your own choice
149 about what to do with the proportions that come in the `viewBox`:
150
151 ```c
152 typedef enum {
153 RSVG_UNIT_PERCENT,
154 RSVG_UNIT_PX,
155 RSVG_UNIT_EM,
156 RSVG_UNIT_EX,
157 RSVG_UNIT_IN,
158 RSVG_UNIT_CM,
159 RSVG_UNIT_MM,
160 RSVG_UNIT_PT,
161 RSVG_UNIT_PC
162 } RsvgUnit;
163
164 typedef struct {
165 double length;
166 RsvgUnit unit;
167 } RsvgLength;
168
169 void rsvg_handle_get_intrinsic_dimensions (RsvgHandle *handle,
170 gboolean *out_has_width,
171 RsvgLength *out_width,
172 gboolean *out_has_height,
173 RsvgLength *out_height,
174 gboolean *out_has_viewbox,
175 RsvgRectangle *out_viewbox);
176 ```
177
178 You'll cleverly note that I have not answered your question. You have
179 an SVG with only a `viewBox`, and you want to pick a reasonable size
180 to render it.
181
182 And here is where I want to say, SVG documents are **scalable**. Pick
183 a size, any size for a viewport! Here are some suggestions:
184
185 * The size of your window's visible area.
186
187 * The size of your device's screen.
188
189 * The size of your sheet of paper, minus the margins.
190
191 Take that size, pass it as the viewport size to
192 [[email protected]_document], and be done with it.
193
194 ### SVG documents without any usable sizing information at all
195
196 This is pretty nasty, but *possibly* not useless for doing special
197 effects in web browsers:
198
199 ```xml
200 <svg xmlns="http://www.w3.org/2000/svg">
201 ```
202
203 That's right, no `width`, no `height`, no `viewBox`. There is no easy
204 way to figure out a suitable size for this. You have two options:
205
206 * Shrug your shoulders, and [[email protected]_document] with a
207 comfortable viewport size like in the last section.
208
209 * Do a best-effort job of actually computing the geometries of all the elements
210 in the document. You can use [[email protected]_geometry_for_element] by
211 passing `NULL` for the target element's `id`; this will measure all the
212 elements in the document. This is not expensive for typical SVGs, but it is
213 not "almost instantaneous" like just asking for intrinsic dimensions would be.
214
215 If this is starting to sound too complicated, please remember that
216 **SVG documents are scalable**. That's their whole reason for being!
217 Pick a size for a viewport, and ask librsvg to render the document
218 within that viewport with [[email protected]_document].
219
220 ## Recommendations for applications with SVG assets
221
222 Before librsvg 2.46, applications would normally load an SVG asset, then
223 they would query librsvg for the SVG's size, and then they would
224 compute the dimensions of their user interface based on the SVG's size.
225
226 With librsvg 2.46 and later, applications may have an easier time by
227 letting the UI choose whatever size it wants, or by hardcoding a size
228 for SVG assets, and then asking librsvg to render SVG assets at that
229 particular size. Applications can use [[email protected]_document],
230 which takes a destination viewport, to do this in a single step.
231
232 To extract individual elements from an SVG document and render them in
233 arbitrary locations — for example, to extract a single icon from a
234 document full of icons —, applications can use
235 [[email protected]_element].
236
237 ### Injecting a user stylesheet
238
239 It is sometimes convenient for applications to inject an extra
240 stylesheet while rendering an SVG document. You can do this with
241 [[email protected]_stylesheet]. During the CSS cascade, the specified
242 stylesheet will be used with a ["User"
243 origin](https://drafts.csswg.org/css-cascade-3/#cascading-origins).