1
1
use librsvg_c::sizing::LegacySize;
2
use rsvg::{CairoRenderer, Loader, LoadingError, SvgHandle};
3

            
4
8
fn load_svg(input: &'static [u8]) -> Result<SvgHandle, LoadingError> {
5
8
    let bytes = glib::Bytes::from_static(input);
6
8
    let stream = gio::MemoryInputStream::from_bytes(&bytes);
7

            
8
8
    Loader::new().read_stream(&stream, None::<&gio::File>, None::<&gio::Cancellable>)
9
8
}
10

            
11
#[test]
12
2
fn just_viewbox_uses_viewbox_size() {
13
1
    let svg = load_svg(
14
        br#"<?xml version="1.0" encoding="UTF-8"?>
15
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 200"/>
16
"#,
17
    )
18
    .unwrap();
19

            
20
2
    assert_eq!(
21
1
        CairoRenderer::new(&svg)
22
1
            .legacy_layer_geometry(None)
23
            .unwrap(),
24
1
        (
25
1
            cairo::Rectangle::new(0.0, 0.0, 100.0, 200.0),
26
1
            cairo::Rectangle::new(0.0, 0.0, 100.0, 200.0),
27
        )
28
    );
29
2
}
30

            
31
#[test]
32
2
fn no_intrinsic_size_uses_element_geometries() {
33
1
    let svg = load_svg(
34
        br#"<?xml version="1.0" encoding="UTF-8"?>
35
<svg xmlns="http://www.w3.org/2000/svg">
36
  <rect x="10" y="20" width="30" height="40" fill="black"/>
37
</svg>
38
"#,
39
    )
40
    .unwrap();
41

            
42
2
    assert_eq!(
43
1
        CairoRenderer::new(&svg)
44
1
            .legacy_layer_geometry(None)
45
            .unwrap(),
46
1
        (
47
1
            cairo::Rectangle::new(10.0, 20.0, 30.0, 40.0),
48
1
            cairo::Rectangle::new(10.0, 20.0, 30.0, 40.0),
49
        )
50
    );
51
2
}
52

            
53
#[test]
54
2
fn hundred_percent_width_height_uses_viewbox() {
55
1
    let svg = load_svg(
56
        br#"<?xml version="1.0" encoding="UTF-8"?>
57
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 100 200"/>
58
"#,
59
    )
60
    .unwrap();
61

            
62
2
    assert_eq!(
63
1
        CairoRenderer::new(&svg)
64
1
            .legacy_layer_geometry(None)
65
            .unwrap(),
66
1
        (
67
1
            cairo::Rectangle::new(0.0, 0.0, 100.0, 200.0),
68
1
            cairo::Rectangle::new(0.0, 0.0, 100.0, 200.0),
69
        )
70
    );
71
2
}
72

            
73
#[test]
74
2
fn hundred_percent_width_height_no_viewbox_uses_element_geometries() {
75
1
    let svg = load_svg(
76
        br#"<?xml version="1.0" encoding="UTF-8"?>
77
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
78
  <rect x="10" y="20" width="30" height="40" fill="black"/>
79
</svg>
80
"#,
81
    )
82
    .unwrap();
83

            
84
2
    assert_eq!(
85
1
        CairoRenderer::new(&svg)
86
1
            .legacy_layer_geometry(None)
87
            .unwrap(),
88
1
        (
89
1
            cairo::Rectangle::new(10.0, 20.0, 30.0, 40.0),
90
1
            cairo::Rectangle::new(10.0, 20.0, 30.0, 40.0),
91
        )
92
    );
93
2
}
94

            
95
#[test]
96
2
fn width_and_viewbox_preserves_aspect_ratio() {
97
1
    let svg = load_svg(
98
        br#"<?xml version="1.0" encoding="UTF-8"?>
99
<svg xmlns="http://www.w3.org/2000/svg" width="60" viewBox="0 0 30 40">
100
  <rect x="10" y="20" width="30" height="40" fill="black"/>
101
</svg>
102
"#,
103
    )
104
    .unwrap();
105

            
106
    // Per the spec, the height property should default to 100%, so the above would end up
107
    // like <svg width="60" height="100%" viewBox="0 0 30 40">
108
    //
109
    // If that were being *rendered* to a viewport, no problem, just use units horizontally
110
    // and 100% of the viewport vertically.
111
    //
112
    // But we are being asked to compute the SVG's natural geometry, so the best we can do
113
    // is to take the aspect ratio defined by the viewBox and apply it to the width.
114

            
115
2
    assert_eq!(
116
1
        CairoRenderer::new(&svg)
117
1
            .legacy_layer_geometry(None)
118
            .unwrap(),
119
1
        (
120
1
            cairo::Rectangle::new(0.0, 0.0, 60.0, 80.0),
121
1
            cairo::Rectangle::new(0.0, 0.0, 60.0, 80.0),
122
        )
123
    );
124
2
}
125

            
126
#[test]
127
2
fn height_and_viewbox_preserves_aspect_ratio() {
128
1
    let svg = load_svg(
129
        br#"<?xml version="1.0" encoding="UTF-8"?>
130
<svg xmlns="http://www.w3.org/2000/svg" height="80" viewBox="0 0 30 40">
131
  <rect x="10" y="20" width="30" height="40" fill="black"/>
132
</svg>
133
"#,
134
    )
135
    .unwrap();
136

            
137
    // See the comment above in width_and_viewbox_preserves_aspect_ratio(); this
138
    // is equivalent but for the height.
139

            
140
2
    assert_eq!(
141
1
        CairoRenderer::new(&svg)
142
1
            .legacy_layer_geometry(None)
143
            .unwrap(),
144
1
        (
145
1
            cairo::Rectangle::new(0.0, 0.0, 60.0, 80.0),
146
1
            cairo::Rectangle::new(0.0, 0.0, 60.0, 80.0),
147
        )
148
    );
149
2
}
150

            
151
#[test]
152
2
fn zero_width_vbox() {
153
1
    let svg = load_svg(
154
        br#"<?xml version="1.0" encoding="UTF-8"?>
155
<svg xmlns="http://www.w3.org/2000/svg" width="80" viewBox="0 0 0 40">
156
  <rect x="10" y="20" width="30" height="40" fill="black"/>
157
</svg>
158
"#,
159
    )
160
    .unwrap();
161

            
162
2
    assert_eq!(
163
1
        CairoRenderer::new(&svg)
164
1
            .legacy_layer_geometry(None)
165
            .unwrap(),
166
1
        (
167
1
            cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0),
168
1
            cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0)
169
        )
170
    );
171
2
}
172

            
173
#[test]
174
2
fn zero_height_vbox() {
175
1
    let svg = load_svg(
176
        br#"<?xml version="1.0" encoding="UTF-8"?>
177
<svg xmlns="http://www.w3.org/2000/svg" width="80" viewBox="0 0 30 0">
178
  <rect x="10" y="20" width="30" height="40" fill="black"/>
179
</svg>
180
"#,
181
    )
182
    .unwrap();
183

            
184
2
    assert_eq!(
185
1
        CairoRenderer::new(&svg)
186
1
            .legacy_layer_geometry(None)
187
            .unwrap(),
188
1
        (
189
1
            cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0),
190
1
            cairo::Rectangle::new(0.0, 0.0, 0.0, 0.0)
191
        )
192
    );
193
2
}