1
1
use rsvg::test_utils::reference_utils::{Compare, Evaluate, Reference};
2
use rsvg::test_utils::{load_svg, render_document, SurfaceSize};
3
use rsvg::{test_compare_render_output, test_svg_reference};
4

            
5
#[test]
6
2
fn invalid_filter_reference_cancels_filter_chain() {
7
    // The <rect> has a filter chain with two URLs listed, but the second one doesn't resolve.
8
    // The whole filter chain should be ignored.
9
1
    let svg = load_svg(
10
        br##"<?xml version="1.0" encoding="UTF-8"?>
11
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
12
  <defs>
13
    <filter id="filter">
14
      <feColorMatrix type="hueRotate" values="240"/>
15
    </filter>
16
  </defs>
17

            
18
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter) url(#nonexistent)"/>
19
</svg>
20
"##,
21
    ).unwrap();
22

            
23
1
    let output_surf = render_document(
24
        &svg,
25
1
        SurfaceSize(400, 400),
26
1
        |_| (),
27
1
        cairo::Rectangle::new(0.0, 0.0, 400.0, 400.0),
28
    )
29
    .unwrap();
30

            
31
1
    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 400, 400).unwrap();
32

            
33
    {
34
1
        let cr = cairo::Context::new(&reference_surf).expect("Failed to create a cairo context");
35

            
36
1
        cr.rectangle(100.0, 100.0, 200.0, 200.0);
37
1
        cr.set_source_rgb(0.0, 1.0, 0.0);
38
1
        cr.fill().unwrap();
39
1
    }
40

            
41
1
    Reference::from_surface(reference_surf)
42
        .compare(&output_surf)
43
        .evaluate(
44
            &output_surf,
45
            "invalid_filter_reference_cancels_filter_chain",
46
1
        );
47
2
}
48

            
49
#[test]
50
2
fn non_filter_reference_cancels_filter_chain() {
51
    // The <rect> has a filter chain, but one of the URLs does not point to a <filter>.
52
    // The whole filter chain should be ignored.
53
1
    let svg = load_svg(
54
        br##"<?xml version="1.0" encoding="UTF-8"?>
55
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
56
  <defs>
57
    <filter id="filter">
58
      <feColorMatrix type="hueRotate" values="240"/>
59
    </filter>
60
    <g id="not_a_filter"/>
61
  </defs>
62

            
63
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter) url(#not_a_filter)"/>
64
</svg>
65
"##,
66
    ).unwrap();
67

            
68
1
    let output_surf = render_document(
69
        &svg,
70
1
        SurfaceSize(400, 400),
71
1
        |_| (),
72
1
        cairo::Rectangle::new(0.0, 0.0, 400.0, 400.0),
73
    )
74
    .unwrap();
75

            
76
1
    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 400, 400).unwrap();
77

            
78
    {
79
1
        let cr = cairo::Context::new(&reference_surf).expect("Failed to create a cairo context");
80

            
81
1
        cr.rectangle(100.0, 100.0, 200.0, 200.0);
82
1
        cr.set_source_rgb(0.0, 1.0, 0.0);
83
1
        cr.fill().unwrap();
84
1
    }
85

            
86
1
    Reference::from_surface(reference_surf)
87
        .compare(&output_surf)
88
1
        .evaluate(&output_surf, "non_filter_reference_cancels_filter_chain");
89
2
}
90

            
91
test_compare_render_output!(
92
    blur_filter_func,
93
    400,
94
    400,
95
    br##"<?xml version="1.0" encoding="UTF-8"?>
96
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
97
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="blur(5)"/>
98
</svg>
99
"##,
100
    br##"<?xml version="1.0" encoding="UTF-8"?>
101
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
102
  <defs>
103
    <filter id="filter">
104
      <feGaussianBlur stdDeviation="5 5" edgeMode="none"/>
105
    </filter>
106
  </defs>
107

            
108
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter)"/>
109
</svg>
110
"##,
111
);
112

            
113
test_compare_render_output!(
114
    brightness_filter_func,
115
    400,
116
    400,
117
    br##"<?xml version="1.0" encoding="UTF-8"?>
118
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
119
  <rect x="100" y="100" width="200" height="200" fill="green" filter="brightness(125%)"/>
120
</svg>
121
"##,
122
br##"<?xml version="1.0" encoding="UTF-8"?>
123
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
124
  <defs>
125
    <filter id="filter">
126
      <feComponentTransfer>
127
        <feFuncR type="linear" slope="1.25" />
128
        <feFuncG type="linear" slope="1.25" />
129
        <feFuncB type="linear" slope="1.25" />
130
      </feComponentTransfer>
131
    </filter>
132
  </defs>
133

            
134
  <rect x="100" y="100" width="200" height="200" fill="green" filter="url(#filter)"/>
135
</svg>
136
"##,
137
);
138

            
139
test_compare_render_output!(
140
    contrast_filter_func,
141
    400,
142
    400,
143
    br##"<?xml version="1.0" encoding="UTF-8"?>
144
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
145
  <rect x="100" y="100" width="200" height="200" fill="green" filter="contrast(125%)"/>
146
</svg>
147
"##,
148
br##"<?xml version="1.0" encoding="UTF-8"?>
149
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
150
  <defs>
151
    <filter id="filter">
152
      <feComponentTransfer>
153
        <feFuncR type="linear" slope="1.25" intercept="-0.125" />
154
        <feFuncG type="linear" slope="1.25" intercept="-0.125" />
155
        <feFuncB type="linear" slope="1.25" intercept="-0.125" />
156
      </feComponentTransfer>
157
    </filter>
158
  </defs>
159

            
160
  <rect x="100" y="100" width="200" height="200" fill="green" filter="url(#filter)"/>
161
</svg>
162
"##,
163
);
164

            
165
test_compare_render_output!(
166
    dropshadow_filter_func,
167
    400,
168
    400,
169
    br##"<?xml version="1.0" encoding="UTF-8"?>
170
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
171
  <rect x="100" y="100" width="200" height="200" fill="green" filter="drop-shadow(#ff0000 1px 4px 6px)"/>
172
</svg>
173
"##,
174
br##"<?xml version="1.0" encoding="UTF-8"?>
175
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
176
  <defs>
177
    <filter id="filter">
178
      <feGaussianBlur in="SourceAlpha" stdDeviation="6" />
179
      <feOffset dx="1" dy="4" result="offsetblur" />
180
      <feFlood flood-color="#ff0000" />
181
      <feComposite in2="offsetblur" operator="in" />
182
      <feMerge>
183
        <feMergeNode />
184
        <feMergeNode in="SourceGraphic" />
185
      </feMerge>
186
    </filter>
187
  </defs>
188

            
189
  <rect x="100" y="100" width="200" height="200" fill="green" filter="url(#filter)"/>
190
</svg>
191
"##,
192
);
193

            
194
test_compare_render_output!(
195
    grayscale_filter_func,
196
    400,
197
    400,
198
    br##"<?xml version="1.0" encoding="UTF-8"?>
199
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
200
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="grayscale(0.75)"/>
201
</svg>
202
"##,
203
    br##"<?xml version="1.0" encoding="UTF-8"?>
204
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
205
  <defs>
206
    <filter id="filter">
207
      <feColorMatrix type="saturate" values="0.25" />
208
    </filter>
209
  </defs>
210

            
211
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter)"/>
212
</svg>
213
"##,
214
);
215

            
216
test_compare_render_output!(
217
    huerotate_filter_func,
218
    400,
219
    400,
220
    br##"<?xml version="1.0" encoding="UTF-8"?>
221
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
222
  <rect x="100" y="100" width="200" height="200" fill="green" filter="hue-rotate(128deg)"/>
223
</svg>
224
"##,
225
    br##"<?xml version="1.0" encoding="UTF-8"?>
226
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
227
  <defs>
228
    <filter id="filter">
229
      <feColorMatrix type="hueRotate" values="128" />
230
    </filter>
231
  </defs>
232

            
233
  <rect x="100" y="100" width="200" height="200" fill="green" filter="url(#filter)"/>
234
</svg>
235
"##,
236
);
237

            
238
test_compare_render_output!(
239
    invert_filter_func,
240
    400,
241
    400,
242
    br##"<?xml version="1.0" encoding="UTF-8"?>
243
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
244
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="invert(0.75)"/>
245
</svg>
246
"##,
247
    br##"<?xml version="1.0" encoding="UTF-8"?>
248
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
249
  <defs>
250
    <filter id="filter">
251
      <feComponentTransfer>
252
        <feFuncR type="table" tableValues="0.75 0.25" />
253
        <feFuncG type="table" tableValues="0.75 0.25" />
254
        <feFuncB type="table" tableValues="0.75 0.25" />
255
      </feComponentTransfer>
256
    </filter>
257
  </defs>
258

            
259
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter)"/>
260
</svg>
261
"##,
262
);
263

            
264
test_compare_render_output!(
265
    opacity_filter_func,
266
    400,
267
    400,
268
    br##"<?xml version="1.0" encoding="UTF-8"?>
269
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
270
  <rect x="100" y="100" width="200" height="200" fill="red"/>
271
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="opacity(0.75)"/>
272
</svg>
273
"##,
274
    br##"<?xml version="1.0" encoding="UTF-8"?>
275
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
276
  <defs>
277
    <filter id="filter">
278
      <feComponentTransfer>
279
        <feFuncA type="table" tableValues="0 0.75" />
280
      </feComponentTransfer>
281
    </filter>
282
  </defs>
283

            
284
  <rect x="100" y="100" width="200" height="200" fill="red"/>
285
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter)"/>
286
</svg>
287
"##
288
);
289

            
290
test_compare_render_output!(
291
    saturate_filter_func,
292
    400,
293
    400,
294
    br##"<?xml version="1.0" encoding="UTF-8"?>
295
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
296
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="saturate(0.75)"/>
297
</svg>
298
"##,
299
    br##"<?xml version="1.0" encoding="UTF-8"?>
300
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
301
  <defs>
302
    <filter id="filter">
303
      <feColorMatrix type="saturate" values="0.75" />
304
    </filter>
305
  </defs>
306

            
307
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter)"/>
308
</svg>
309
"##,
310
);
311

            
312
test_compare_render_output!(
313
    sepia_filter_func,
314
    400,
315
    400,
316
    br##"<?xml version="1.0" encoding="UTF-8"?>
317
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
318
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="sepia(0.75)"/>
319
</svg>
320
"##,
321
    br##"<?xml version="1.0" encoding="UTF-8"?>
322
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="400">
323
  <defs>
324
    <filter id="filter">
325
      <feColorMatrix type="matrix"
326
         values="0.5447500000000001 0.57675 0.14175 0 0
327
                 0.26175 0.7645000000000001 0.126 0 0
328
                 0.20400000000000001 0.4005 0.34825 0 0
329
                 0 0 0 1 0"/>
330
    </filter>
331
  </defs>
332

            
333
  <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter)"/>
334
</svg>
335
"##,
336
);
337

            
338
test_compare_render_output!(
339
    mask_type,
340
    200,
341
    100,
342
    br##"<?xml version="1.0" encoding="UTF-8"?>
343
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100">
344
  <mask id="luminance" mask-type="luminance" maskContentUnits="objectBoundingBox">
345
    <rect x="0.1" y="0.1" width="0.8" height="0.8" fill="white"/>
346
  </mask>
347
  <mask id="alpha" mask-type="alpha" maskContentUnits="objectBoundingBox">
348
    <rect x="0.1" y="0.1" width="0.8" height="0.8" fill="black"/>
349
  </mask>
350

            
351
  <rect x="0" y="0" width="100" height="100" fill="green" mask="url(#luminance)"/>
352

            
353
  <rect x="100" y="0" width="100" height="100" fill="green" mask="url(#alpha)"/>
354
</svg>
355
"##,
356
    br##"<?xml version="1.0" encoding="UTF-8"?>
357
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100">
358
  <rect x="10" y="10" width="80" height="80" fill="green"/>
359

            
360
  <rect x="110" y="10" width="80" height="80" fill="green"/>
361
</svg>
362
"##,
363
);
364

            
365
test_svg_reference!(
366
    bug_743_fe_drop_shadow,
367
    "tests/fixtures/reftests/svg2/bug743-fe-drop-shadow.svg",
368
    "tests/fixtures/reftests/svg2/bug743-fe-drop-shadow-ref.svg"
369
);