librsvg 2.63.2

A library to render SVG images to Cairo surfaces. GNOME uses this to render SVG icons. Outside of GNOME, other desktop environments use it for similar purposes. Wikimedia uses it for Wikipedia's SVG diagrams.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use matches::matches;
use rsvg::{CairoRenderer, Loader, LoadingError, SvgHandle};

use rsvg::test_utils::reference_utils::{Compare, Evaluate, Reference};
use rsvg::test_utils::{SurfaceSize, load_svg, render_document, setup_font_map, setup_language};

// https://gitlab.gnome.org/GNOME/librsvg/issues/335
#[test]
fn non_svg_root() {
    assert!(matches!(load_svg(b"<x></x>"), Err(LoadingError::NoSvgRoot)));
}

// https://gitlab.gnome.org/GNOME/librsvg/issues/496
#[test]
fn inf_width() {
    let svg = load_svg(
        br#"<?xml version="1.0" encoding="UTF-8"?>
<svg s="Pg" width="1001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" heiNht=" 00">
 [l<g mask="url(sHaf:ax-fwiw0\inside\ax-ide\ax-flow#o0" styli="fility:!.5;">>
  </g>
</svg>"#,
    ).unwrap();

    let _output_surf = render_document(
        &svg,
        SurfaceSize(150, 150),
        |cr| cr.translate(50.0, 50.0),
        cairo::Rectangle::new(0.0, 0.0, 50.0, 50.0),
    )
    .unwrap();
}

// https://gitlab.gnome.org/GNOME/librsvg/issues/547
#[test]
fn nonexistent_image_shouldnt_cancel_rendering() {
    let svg = load_svg(
        br#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
     width="50" height="50">
  <image xlink:href="nonexistent.png" width="10" height="10"/>
  <rect x="10" y="10" width="30" height="30" fill="blue"/>
</svg>
"#,
    )
    .unwrap();

    let output_surf = render_document(
        &svg,
        SurfaceSize(50, 50),
        |_| (),
        cairo::Rectangle::new(0.0, 0.0, 50.0, 50.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 50, 50).unwrap();

    {
        let cr = cairo::Context::new(&reference_surf).unwrap();

        cr.rectangle(10.0, 10.0, 30.0, 30.0);
        cr.set_source_rgba(0.0, 0.0, 1.0, 1.0);
        cr.fill().unwrap();
    }

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(&output_surf, "nonexistent_image_shouldnt_cancel_rendering");
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/568
#[test]
fn href_attribute_overrides_xlink_href() {
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     width="500" height="500">
  <defs>
    <rect id="one" x="100" y="100" width="100" height="100" fill="red"/>
    <rect id="two" x="100" y="100" width="100" height="100" fill="lime"/>
  </defs>

  <!-- Per https://svgwg.org/svg2-draft/linking.html#XLinkRefAttrs a plain
       href attribute overrides an xlink:href one in SVG2 -->
  <use xlink:href="#one" href="#two"/>
</svg>
"##,
    )
    .unwrap();

    let output_surf = render_document(
        &svg,
        SurfaceSize(500, 500),
        |_| (),
        cairo::Rectangle::new(0.0, 0.0, 500.0, 500.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 500, 500).unwrap();

    {
        let cr = cairo::Context::new(&reference_surf).unwrap();

        cr.rectangle(100.0, 100.0, 100.0, 100.0);
        cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
        cr.fill().unwrap();
    }

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(&output_surf, "href_attribute_overrides_xlink_href");
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/560
#[test]
fn nonexistent_filter_leaves_object_unfiltered() {
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
     width="500" height="500">
  <rect x="100" y="100" width="100" height="100" fill="lime" filter="url(#nonexistent)"/>
</svg>
"##,
    )
    .unwrap();

    let output_surf = render_document(
        &svg,
        SurfaceSize(500, 500),
        |_| (),
        cairo::Rectangle::new(0.0, 0.0, 500.0, 500.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 500, 500).unwrap();

    {
        let cr = cairo::Context::new(&reference_surf).unwrap();

        cr.rectangle(100.0, 100.0, 100.0, 100.0);
        cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
        cr.fill().unwrap();
    }

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(&output_surf, "nonexistent_filter_leaves_object_unfiltered");
}

// https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint says this:
//
// A <paint> allows a paint server reference, to be optionally
// followed by a <color> or the keyword none. When this optional value
// is given, the <color> value or the value none is a fallback value
// to use if the paint server reference in the layer is invalid (due
// to pointing to an element that does not exist or which is not a
// valid paint server).
//
// I'm interpreting this to mean that if we have
// fill="url(#recursive_paint_server) fallback_color", then the
// recursive paint server is not valid, and should fall back to to the
// specified color.
#[test]
fn recursive_paint_servers_fallback_to_color() {
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
     width="200" height="200">
  <defs>
    <pattern id="p" width="10" height="10" xlink:href="#p"/>
    <linearGradient id="l" xlink:href="#r"/>
    <radialGradient id="r" xlink:href="#l"/>
  </defs>

  <!-- These two should not render as there is no fallback color -->
  <rect fill="url(#p)" x="0" y="0" width="100" height="100" />
  <rect fill="url(#l)" x="100" y="0" width="100" height="100" />

  <!-- These two should render with the fallback color -->
  <rect fill="url(#p) lime" x="0" y="100" width="100" height="100" />
  <rect fill="url(#l) lime" x="100" y="100" width="100" height="100" />
</svg>
"##,
    )
    .unwrap();

    let output_surf = render_document(
        &svg,
        SurfaceSize(200, 200),
        |_| (),
        cairo::Rectangle::new(0.0, 0.0, 200.0, 200.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 200, 200).unwrap();

    {
        let cr = cairo::Context::new(&reference_surf).unwrap();

        cr.rectangle(0.0, 100.0, 200.0, 100.0);
        cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
        cr.fill().unwrap();
    }

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(&output_surf, "recursive_paint_servers_fallback_to_color");
}

fn test_renders_as_empty(svg: &SvgHandle, test_name: &str) {
    let output_surf = render_document(
        svg,
        SurfaceSize(100, 100),
        |_| (),
        cairo::Rectangle::new(0.0, 0.0, 100.0, 100.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 100, 100).unwrap();

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(&output_surf, test_name);
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/308
#[test]
fn recursive_use() {
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <g id="one">
      <use xlink:href="#one"/>
    </g>
  </defs>

  <use xlink:href="#one"/>
</svg>
"##,
    )
    .unwrap();

    test_renders_as_empty(&svg, "308-recursive-use");
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/308
#[test]
fn use_self_ref() {
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <use id="one" xlink:href="#one"/>
  </defs>

  <use xlink:href="#one"/>
</svg>
"##,
    )
    .unwrap();

    test_renders_as_empty(&svg, "308-use-self-ref");
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/308
#[test]
fn doubly_recursive_use() {
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <g id="one">
      <use xlink:href="#two"/>
    </g>

    <g id="two">
      <use xlink:href="#one"/>
    </g>
  </defs>

  <use xlink:href="#one"/>
</svg>
"##,
    )
    .unwrap();

    test_renders_as_empty(&svg, "308-doubly-recursive-use");
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/347
#[test]
fn test_text_bounds() {
    setup_font_map();

    let handle = Loader::new()
        .read_path("tests/fixtures/dimensions/bug347-wrapper.svg")
        .unwrap_or_else(|e| panic!("could not load: {}", e));

    let renderer = CairoRenderer::new(&handle).test_mode(true);

    let (ink_r, _) = renderer
        .geometry_for_layer(
            Some("#LabelA"),
            &cairo::Rectangle::new(0.0, 0.0, 248.0, 176.0),
        )
        .unwrap();

    assert!(ink_r.x() >= 80.0 && ink_r.x() < 80.1);

    // This is kind of suspicious, but we don't know the actual height of the
    // text set at y=49 in the test SVG.  However, this test is more "text
    // elements compute sensible bounds"; the bug #347 was that their ink_rect
    // was not being computed correctly at all.
    assert!(ink_r.y() > 48.0 && ink_r.y() < 49.0);
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/703
#[test]
fn switch_element_should_ignore_elements_in_error() {
    setup_language();

    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
  <switch>
    <rect x="10" y="10" width="10" height="10" systemLanguage="es_MX" id="es" fill="red"/>
    <rect x="10" y="10" width="10" height="10" id="no_lang" fill="blue"/>
  </switch>
</svg>
"##,
    )
    .unwrap();

    let output_surf = render_document(
        &svg,
        SurfaceSize(100, 100),
        |_| (),
        cairo::Rectangle::new(0.0, 0.0, 100.0, 100.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 100, 100).unwrap();

    {
        let cr = cairo::Context::new(&reference_surf).unwrap();

        cr.rectangle(10.0, 10.0, 10.0, 10.0);
        cr.set_source_rgba(0.0, 0.0, 1.0, 1.0);
        cr.fill().unwrap();
    }

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(
            &output_surf,
            "switch_element_should_ignore_elements_in_error",
        );
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/566
#[test]
fn accepted_children_inside_clip_path() {
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
  <defs>
    <clipPath id="one">
      <g>
        <rect x="10" y="10" width="50" height="50"/>
      </g>
    </clipPath>

    <clipPath id="two">
      <use xlink:href="#three"/>
    </clipPath>

    <use id="three" xlink:href="#four"/>

    <rect id="four" x="10" y="10" width="50" height="50"/>
  </defs>

  <rect x="10" y="10" width="100" height="100" fill="lime"/>

  <rect x="20" y="20" width="10" height="10" fill="red" clip-path="url(#one)"/>

  <rect x="40" y="40" width="10" height="10" fill="red" clip-path="url(#two)"/>
</svg>
"##,
    )
    .unwrap();

    let output_surf = render_document(
        &svg,
        SurfaceSize(200, 200),
        |_| (),
        cairo::Rectangle::new(0.0, 0.0, 200.0, 200.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 200, 200).unwrap();

    {
        let cr = cairo::Context::new(&reference_surf).unwrap();

        cr.rectangle(10.0, 10.0, 100.0, 100.0);
        cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
        cr.fill().unwrap();
    }

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(&output_surf, "accepted_children_inside_clip_path");
}

#[test]
fn can_draw_to_non_image_surface() {
    // This tries to exercise the various tricky code paths in DrawingCtx::with_discrete_layer()
    // that depend on whether there are filter/masks/opacity - they are easy to break when
    // the application is using something other than a cairo::ImageSurface.
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="100">
  <!-- code path with opacity, no mask -->
  <rect x="0" y="0" width="100" height="100" fill="lime" opacity="0.5"/>

  <!-- code path with mask -->
  <mask id="mask" maskUnits="objectBoundingBox">
    <rect x="10%" y="10%" width="80%" height="80%" fill="white"/>
  </mask>
  <rect x="100" y="0" width="100" height="100" fill="lime" mask="url(#mask)"/>

  <!-- code path with filter -->
  <rect x="200" y="0" width="100" height="100" fill="lime" filter="blur(5)"/>

  <!-- code path with filter and mask-->
  <rect x="300" y="0" width="100" height="100" fill="lime" filter="blur(5)" mask="url(#mask)"/>
</svg>
"##,
    )
    .unwrap();

    let renderer = CairoRenderer::new(&svg);

    let viewport = cairo::Rectangle::new(0.0, 0.0, 200.0, 200.0);

    let output =
        cairo::RecordingSurface::create(cairo::Content::ColorAlpha, Some(viewport)).unwrap();

    let cr = cairo::Context::new(&output).expect("Failed to create a cairo context");
    renderer
        .render_document(&cr, &viewport)
        .expect("Failed to render to non-image surface");
}

// https://gitlab.gnome.org/GNOME/librsvg/-/issues/1142
#[test]
fn embedded_svg_image_renders_at_device_resolution() {
    // The href is a base64-encoded SVG equivalent to:
    //   <svg width="10" height="10" xmlns="http://www.w3.org/2000/svg">
    //     <rect x="0" y="0" width="5" height="5" fill="#000000"/>
    //     <rect x="5" y="5" width="5" height="5" fill="#00ff00"/>
    //   </svg>
    let svg = load_svg(
        br##"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10">
  <image width="10" height="10"
    href="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAiIGhlaWdodD0iMTAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjUiIGhlaWdodD0iNSIgZmlsbD0iIzAwMDAwMCIvPjxyZWN0IHg9IjUiIHk9IjUiIHdpZHRoPSI1IiBoZWlnaHQ9IjUiIGZpbGw9IiMwMGZmMDAiLz48L3N2Zz4="/>
</svg>
"##,
    )
    .unwrap();

    let output_surf = render_document(
        &svg,
        SurfaceSize(20, 20),
        |cr| cr.scale(2.0, 2.0),
        cairo::Rectangle::new(0.0, 0.0, 10.0, 10.0),
    )
    .unwrap();

    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 20, 20).unwrap();
    {
        let cr = cairo::Context::new(&reference_surf).unwrap();

        cr.rectangle(0.0, 0.0, 10.0, 10.0);
        cr.set_source_rgba(0.0, 0.0, 0.0, 1.0);
        cr.fill().unwrap();

        cr.rectangle(10.0, 10.0, 10.0, 10.0);
        cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
        cr.fill().unwrap();
    }

    Reference::from_surface(reference_surf)
        .compare(&output_surf)
        .evaluate(
            &output_surf,
            "embedded_svg_image_renders_at_device_resolution",
        );
}