Debian Codegraph @

doc/migrating.md

raw symbols SCIP
1 ---
2 Title: Migrating from old APIs
3 ---
4
5 # Migrating from old APIs
6
7 ## Migrating from the deprecated API that does not use viewports
8
9 First, some context. Until librsvg version 2.44, the only way to
10 render an [[email protected]] into a Cairo context was with the
11 functions [`rsvg_handle_render_cairo(handle,
12 cairo_t)`](method.Handle.render_cairo.html) and
13 [`rsvg_handle_render_cairo_sub(handle, cairo_t,
14 id)`](method.Handle.render_cairo_sub.html) — respectively, to
15 render the whole document, and to render a single \"layer\" from it.
16 Both functions assumed that the SVG document was to be rendered at its
17 \"natural size\", or to the size overridden with
18 [[email protected]_size_callback]. Since the Cairo context can already
19 have an affine transform applied to it, that transform can further
20 change the size of the rendered image.
21
22 Librsvg 2.46 introduced the following functions, designed to replace the
23 `render_cairo` ones:
24
25 * [[email protected]_document] - renders the whole document
26 * [[email protected]_layer] - renders a single layer
27 * [[email protected]_element] - renders a single element
28 * Plus corresponding functions to get the geometries of the
29 document/layer/element.
30
31 All of those functions take a viewport argument. Let\'s see what this
32 means. But first, some history.
33
34 ### Historical note: before librsvg supported viewports
35
36 When librsvg was first written, its API basically consisted of only functions to
37 load an [[email protected]], plus [[email protected]_pixbuf] to render it
38 directly to a GdkPixbuf image. Internally the library used libart (a pre-Cairo
39 2D rendering library), but did not expose it in the public API.
40
41 The only way to specify a size at which to render an [[email protected]] was
42 with [[email protected]_size_callback], and the callback would run at an
43 unspecified time during *loading*: when just enough of the SVG document had been
44 loaded to read in the `width/height` attributes of the toplevel `<svg>` element,
45 the callback would let the application override these values with its own
46 desired size.
47
48 Some years later, Cairo was introduced, and it started to replace libart. Unlike
49 libart, which could only render to in-memory RGBA buffers, Cairo had a notion of
50 \"backends\": it could render to RGBA buffers, or it could translate its drawing
51 model commands into PDF or PostScript. In Cairo\'s terms, one creates a
52 [`cairo_surface_t`][cairo_surface_t] of a particular kind (in-memory image
53 surface, PDF surface, EPS surface, etc.), and then a [`cairo_t`][cairo_t]
54 context for the surface. The context is what makes the drawing commands
55 available.
56
57 Being able to render SVG documents directly to PDF or PostScript was clearly
58 attractive, so librsvg\'s API of [[email protected]_pixbuf] would clearly
59 not be enough. It would be better to pass a [`cairo_t`][cairo_t] for an
60 already-created surface, and have librsvg issue its drawing commands to it. Then
61 the application would be in control of the surface type, or in the case of GTK
62 widgets, they would already get a [`cairo_t`][cairo_t] passed to their drawing
63 functions. Librsvg got modified to export a
64 [`rsvg_handle_render_cairo(handle, cairo_t)`](method.Handle.render_cairo.html),
65 and then it reimplemented the old [[email protected]_pixbuf] in terms of
66 Cairo.
67
68 At this point, librsvg still kept the notion of rendering SVG documents
69 at their \"natural size\": the `<svg>` element\'s `width` and `height`
70 attributes converted to pixels (e.g. converting from `width="5cm"` by
71 using the dots-per-inch value from the [[email protected]]), or if those
72 attributes don\'t exist, by using the `viewBox` as a pixel size. The
73 assumption was that if you needed a different size, you could always
74 start by setting the transformation matrix on your [`cairo_t`][cairo_t] and then
75 rendering to that.
76
77 [cairo_surface_t]: https://www.cairographics.org/manual/cairo-cairo-surface-t.html
78 [cairo_t]: https://www.cairographics.org/manual/cairo-cairo-t.html
79
80 ### The problem with not having viewports
81
82 Most applications which use librsvg to render SVG assets for their user
83 interface generally work in the same way. For example, to take an SVG
84 icon and render it, they do something like this:
85
86 1. Create an [[email protected]] by loading it from the SVG icon data.
87
88 2. Ask the [[email protected]] for its dimensions.
89
90 3. Divide the dimensions by the GUI\'s preferred size for icons.
91
92 4. Translate a Cairo context so the icon will appear at the desired location.
93 Scale the Cairo context by the result of the previous step to obtain the
94 desired dimensions.
95
96 5. Render the [[email protected]] in that Cairo context.
97
98 This is\... too much work. The web world has moved on to using the CSS
99 box model practically everywhere. To embed an image you specify *where*
100 and at *what size* you want to place it, and it gets done automatically.
101 You actually have to do extra work if you want to do non-standard things
102 like scale an image non-proportionally.
103
104 ### The new rendering API that uses viewports
105
106 Starting with librsvg 2.46, the following functions are available:
107
108 ```c
109 typedef struct {
110 double x;
111 double y;
112 double width;
113 double height;
114 } RsvgRectangle;
115
116 gboolean rsvg_handle_render_document (RsvgHandle *handle,
117 cairo_t *cr,
118 const RsvgRectangle *viewport,
119 GError **error);
120
121 gboolean rsvg_handle_render_layer (RsvgHandle *handle,
122 cairo_t *cr,
123 const char *id,
124 const RsvgRectangle *viewport,
125 GError **error);
126
127 gboolean rsvg_handle_render_element (RsvgHandle *handle,
128 cairo_t *cr,
129 const char *id,
130 const RsvgRectangle *element_viewport,
131 GError **error);
132 ```
133
134 For brevity we will omit the `rsvg_handle` namespace prefix, and just talk about
135 the actual function names. You can see that
136 [`render_document`](method.Handle.render_document.html) is basically the same as
137 [`render_cairo`](method.Handle.render_cairo.html), but it has an extra
138 `viewport` argument. The same occurs in
139 [`render_layer`](method.Handle.render_layer.html) versus
140 [`render_cairo_sub`](method.Handle.render_cairo_sub.html).
141
142 In both of those cases — [`render_document`](method.Handle.render_document.html)
143 and [`render_layer`](method.Handle.render_layer.html) —, the `viewport` argument
144 specifies a rectangle into which the SVG will be positioned and scaled to fit.
145 Consider something like this:
146
147 ```c
148 RsvgRectangle viewport = {
149 .x = 10.0,
150 .y = 20.0,
151 .width = 640.0,
152 .height = 480.0,
153 };
154
155 rsvg_handle_render_document (handle, cr, &viewport, NULL);
156 ```
157
158 This is equivalent to first figuring out the scaling factor to make the SVG fit
159 proportionally in 640×480 pixels, then translating the `cr` by (10, 20) pixels,
160 and then calling [[email protected]_cairo]. If the SVG has different
161 proportions than the width and height of the rectangle, it will be rendered and
162 centered to fit the rectangle.
163
164 ---
165
166 **Note:** [[email protected]_element] is new in librsvg 2.46. It
167 extracts a single element from the SVG and renders it scaled to the viewport you
168 specify. It is different from `render_layer` (or the old-style
169 `render_cairo_sub`) in that those ones act as if they rendered the whole
170
171 ## document\'s area, but they only paint the layer you specify
172
173 ---
174
175 Even better: the old functions to get an SVG\'s natural dimensions, like
176 [[email protected]_dimensions], returned integers instead of floating-point
177 numbers, so you could not always get an exact fit. Please use the new
178 `get_geometry` functions that take viewports; they will give you easier and
179 better results:
180
181 * [[email protected]_geometry_for_layer]
182 * [[email protected]_geometry_for_element]
183
184 ### New API for obtaining an SVG's dimensions
185
186 Per the previous section, you should seldom need to obtain the \"natural
187 size\" of an SVG document now that you can render it directly into a
188 viewport. But if you still need to know what the SVG document specifies
189 for its own size, you can use the following functions, depending on the
190 level of detail you require:
191
192 ```c
193 gboolean rsvg_handle_get_intrinsic_size_in_pixels (RsvgHandle *handle,
194 gdouble *out_width,
195 gdouble *out_height);
196 ```
197
198 [[email protected]_intrinsic_size_in_pixels] returns an exact width and
199 height in floating-point pixels. **You should round up to the next integer** if
200 you need to allocate a pixel buffer big enough, to avoid clipping the last
201 column or row of pixels, which may be only partially covered. For example, if a
202 document's width is `41.3` CSS pixels, you should create a raster image `42`
203 pixels wide so it fits without clipping the last pixel. You can do this with the
204 `ceil()` function.
205
206 [[email protected]_intrinsic_size_in_pixels] works by resolving the
207 `width/height` attributes of the toplevel `<svg>` element against the
208 handle\'s current DPI and the `font-size` that is defined for the
209 `<svg>` element.
210
211 However, that is only possible if the `width/height` attributes actually
212 exist and are in physical units. The function will return FALSE if the
213 SVG has no resolvable units, for example if the `width/height`
214 attributes are specified in percentages (e.g. `width="50%"`), since the
215 function has no knowledge of the viewport where you will place the SVG,
216 or if those attributes are not specified.
217
218 The other way of obtaining an SVG\'s dimensions is to actually query its
219 \"intrinsic dimensions\", i.e. what is actually specified in the SVG
220 document:
221
222 ```c
223 typedef enum {
224 RSVG_UNIT_PERCENT,
225 RSVG_UNIT_PX,
226 RSVG_UNIT_EM,
227 RSVG_UNIT_EX,
228 RSVG_UNIT_IN,
229 RSVG_UNIT_CM,
230 RSVG_UNIT_MM,
231 RSVG_UNIT_PT,
232 RSVG_UNIT_PC
233 } RsvgUnit;
234
235 typedef struct {
236 double length;
237 RsvgUnit unit;
238 } RsvgLength;
239
240 void rsvg_handle_get_intrinsic_dimensions (RsvgHandle *handle,
241 gboolean *out_has_width,
242 RsvgLength *out_width,
243 gboolean *out_has_height,
244 RsvgLength *out_height,
245 gboolean *out_has_viewbox,
246 RsvgRectangle *out_viewbox);
247 ```
248
249 [[email protected]_intrinsic_dimensions] will tell you precisely if the
250 toplevel `<svg>` has `width/height` attributes and their values, and also
251 whether it has a `viewBox` and its value.
252
253 ---
254
255 **Note:** Remember that SVGs are *scalable*. They are not like raster images
256 which have an exact size in pixels, and which you must always take into account
257 to scale them to a convenient size. For SVGs, you can just render them to a
258 viewport, and avoid working directly with their size — which is kind of
259 arbitrary, and all that matters is the
260
261 ## document's aspect ratio
262
263 ---
264
265 ### SVGs with no intrinsic dimensions nor aspect ratio
266
267 SVG documents that have none of the `width`, `height`, or `viewBox` attributes
268 are thankfully not very common, but they are hard to deal with: the software
269 cannot immediately know their natural size or aspect ratio, so they cannot be
270 easily scaled to fit within a viewport. If you need to measure the extents of
271 all the objects in an SVG document, you can use
272 [[email protected]_geometry_for_element] by passing `NULL` for the target
273 element's `id`; this will measure all the elements in the document.
274
275 ## Migrating to the geometry APIs
276
277 Until librsvg 2.44, the available APIs to query the geometry of a layer
278 or element were these:
279
280 ```c
281 struct _RsvgPositionData {
282 int x;
283 int y;
284 };
285
286 gboolean rsvg_handle_get_position_sub (RsvgHandle *handle,
287 RsvgPositionData *position_data,
288 const char *id);
289
290 struct _RsvgDimensionData {
291 int width;
292 int height;
293 gdouble em;
294 gdouble ex;
295 };
296
297 gboolean rsvg_handle_get_dimensions_sub (RsvgHandle *handle,
298 RsvgDimensionData *dimension_data,
299 const char *id);
300 ```
301
302 These functions are inconvenient — separate calls to get the position
303 and dimensions —, and also inexact, since they only return integer
304 values, while SVG uses floating-point units.
305
306 Since librsvg 2.46, you can use these functions instead:
307
308 ```c
309 typedef struct {
310 double x;
311 double y;
312 double width;
313 double height;
314 } RsvgRectangle;
315
316 gboolean rsvg_handle_get_geometry_for_layer (RsvgHandle *handle,
317 const char *id,
318 const RsvgRectangle *viewport,
319 RsvgRectangle *out_ink_rect,
320 RsvgRectangle *out_logical_rect,
321 GError **error);
322
323 gboolean rsvg_handle_get_geometry_for_element (RsvgHandle *handle,
324 const char *id,
325 RsvgRectangle *out_ink_rect,
326 RsvgRectangle *out_logical_rect,
327 GError **error);
328 ```
329
330 These functions return exact floating-point values. They also give you
331 the ink rectangle, or area covered by paint, as well as the logical
332 rectangle, which is the extents of unstroked paths (i.e. just the
333 outlines).
334
335 * [[email protected]_geometry_for_layer]
336 * [[email protected]_geometry_for_element]