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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Entry point for the CSS filters infrastructure.

use cssparser::{BasicParseError, Parser};
use markup5ever::{expanded_name, local_name, namespace_url, ns};
use std::rc::Rc;
use std::time::Instant;

use crate::bbox::BoundingBox;
use crate::document::AcquiredNodes;
use crate::drawing_ctx::{DrawingCtx, Viewport};
use crate::element::{set_attribute, ElementTrait};
use crate::error::{InternalRenderingError, ParseError};
use crate::filter::UserSpaceFilter;
use crate::length::*;
use crate::node::Node;
use crate::parse_identifiers;
use crate::parsers::{CustomIdent, Parse, ParseValue};
use crate::properties::ColorInterpolationFilters;
use crate::rsvg_log;
use crate::session::Session;
use crate::surface_utils::{
    shared_surface::{SharedImageSurface, SurfaceType},
    EdgeMode,
};
use crate::xml::Attributes;

mod bounds;
use self::bounds::BoundsBuilder;

pub mod context;
use self::context::{FilterContext, FilterOutput, FilterResult};

mod error;
use self::error::FilterError;
pub use self::error::FilterResolveError;

/// A filter primitive interface.
pub trait FilterEffect: ElementTrait {
    fn resolve(
        &self,
        acquired_nodes: &mut AcquiredNodes<'_>,
        node: &Node,
    ) -> Result<Vec<ResolvedPrimitive>, FilterResolveError>;
}

pub mod blend;
pub mod color_matrix;
pub mod component_transfer;
pub mod composite;
pub mod convolve_matrix;
pub mod displacement_map;
pub mod drop_shadow;
pub mod flood;
pub mod gaussian_blur;
pub mod image;
pub mod lighting;
pub mod merge;
pub mod morphology;
pub mod offset;
pub mod tile;
pub mod turbulence;

/// Parameters to apply a list of SVG filter primitives onto a surface.
///
/// This is almost everything needed to take a surface and apply a list of SVG filter
/// primitives to it.
pub struct FilterSpec {
    /// Human-readable identifier for the filter, for logging/debugging purposes.
    pub name: String,

    /// Coordinates and bounds.
    pub user_space_filter: UserSpaceFilter,

    /// List of filter primitives to apply to the surface, in order.
    pub primitives: Vec<UserSpacePrimitive>,
}

/// Parameters using while rendering a whole `filter` property.
///
/// The `filter` property may contain a single primitive, like `filter="blur(2px)", or a
/// list of filter specs like `filter="blur(2px) url(#filter_id) drop_shadow(5 5)"`.  Each
/// of those specs may produce more than one primitive; for example, the `url(#filter_id)`
/// there may refer to a `<filter>` element that has several primitives inside it.  Also,
/// the `drop_shadow()` function will expand to the few primitives used to implement a
/// drop shadow.
///
/// Each filter spec will be rendered within a [`FilterContext`], so that the context can maintain
/// the list of named outputs within a `<filter>` element.
///
/// While rendering all those [`FilterContext`]s, there are some immutable parameters.
/// This `FilterPlan` struct contains those parameters.
pub struct FilterPlan {
    session: Session,

    /// Current viewport at the time the filter is invoked.
    pub viewport: Viewport,

    /// Surface corresponding to the background image snapshot, for `in="BackgroundImage"`.
    background_image: Option<SharedImageSurface>,

    /// Surface filled with the current stroke paint, for `in="StrokePaint"`.
    ///
    /// Filter Effects 1: <https://www.w3.org/TR/filter-effects/#attr-valuedef-in-strokepaint>
    stroke_paint_image: Option<SharedImageSurface>,

    /// Surface filled with the current fill paint, for `in="FillPaint"`.
    ///
    /// Filter Effects 1: <https://www.w3.org/TR/filter-effects/#attr-valuedef-in-fillpaint>
    fill_paint_image: Option<SharedImageSurface>,
}

impl FilterPlan {
    pub fn new(
        session: &Session,
        viewport: Viewport,
        requirements: InputRequirements,
        background_image: Option<SharedImageSurface>,
        stroke_paint_image: Option<SharedImageSurface>,
        fill_paint_image: Option<SharedImageSurface>,
    ) -> Result<FilterPlan, InternalRenderingError> {
        assert_eq!(
            requirements.needs_background_image || requirements.needs_background_alpha,
            background_image.is_some()
        );

        assert_eq!(
            requirements.needs_stroke_paint_image,
            stroke_paint_image.is_some()
        );

        assert_eq!(
            requirements.needs_fill_paint_image,
            fill_paint_image.is_some()
        );

        Ok(FilterPlan {
            session: session.clone(),
            viewport,
            background_image,
            stroke_paint_image,
            fill_paint_image,
        })
    }
}

/// Which surfaces need to be provided as inputs for a [`FilterPlan`].
///
/// The various filters in a `filter` property may require different source images that
/// the calling [`DrawingCtx`] is able to compute.  For example, if a primitive inside a
/// `<filter>` element has `in="FillPaint"`, then the calling [`DrawingCtx`] must supply a
/// surface filled as per the `fill` property of the element being filtered.
///
/// This struct holds the requirements for which such surfaces are needed.  The caller is
/// expected to construct it from an array of [`FilterSpec`], and then to create the
/// corresponding [`Inputs`] to create a [`FilterPlan`].
#[derive(Debug, Default, PartialEq)]
pub struct InputRequirements {
    pub needs_source_alpha: bool,
    pub needs_background_image: bool,
    pub needs_background_alpha: bool,
    pub needs_stroke_paint_image: bool,
    pub needs_fill_paint_image: bool,
}

impl InputRequirements {
    pub fn new_from_filter_specs(specs: &[FilterSpec]) -> InputRequirements {
        specs
            .iter()
            .flat_map(|spec| spec.primitives.iter())
            .map(|primitive| primitive.params.get_input_requirements())
            .fold(InputRequirements::default(), |a, b| a.fold(b))
    }

    #[rustfmt::skip]
    fn fold(self, r: InputRequirements) -> InputRequirements {
        InputRequirements {
            needs_source_alpha:       self.needs_source_alpha       || r.needs_source_alpha,
            needs_background_image:   self.needs_background_image   || r.needs_background_image,
            needs_background_alpha:   self.needs_background_alpha   || r.needs_background_alpha,
            needs_stroke_paint_image: self.needs_stroke_paint_image || r.needs_stroke_paint_image,
            needs_fill_paint_image:   self.needs_fill_paint_image   || r.needs_fill_paint_image,
        }
    }
}

/// Resolved parameters for each filter primitive.
///
/// These gather all the data that a primitive may need during rendering:
/// the `feFoo` element's attributes, any computed values from its properties,
/// and parameters extracted from the element's children (for example,
/// `feMerge` gathers info from its `feMergNode` children).
pub enum PrimitiveParams {
    Blend(blend::Blend),
    ColorMatrix(color_matrix::ColorMatrix),
    ComponentTransfer(component_transfer::ComponentTransfer),
    Composite(composite::Composite),
    ConvolveMatrix(convolve_matrix::ConvolveMatrix),
    DiffuseLighting(lighting::DiffuseLighting),
    DisplacementMap(displacement_map::DisplacementMap),
    Flood(flood::Flood),
    GaussianBlur(gaussian_blur::GaussianBlur),
    Image(image::Image),
    Merge(merge::Merge),
    Morphology(morphology::Morphology),
    Offset(offset::Offset),
    SpecularLighting(lighting::SpecularLighting),
    Tile(tile::Tile),
    Turbulence(turbulence::Turbulence),
}

impl PrimitiveParams {
    /// Returns a human-readable name for a primitive.
    #[rustfmt::skip]
    fn name(&self) -> &'static str {
        use PrimitiveParams::*;
        match self {
            Blend(..)             => "feBlend",
            ColorMatrix(..)       => "feColorMatrix",
            ComponentTransfer(..) => "feComponentTransfer",
            Composite(..)         => "feComposite",
            ConvolveMatrix(..)    => "feConvolveMatrix",
            DiffuseLighting(..)   => "feDiffuseLighting",
            DisplacementMap(..)   => "feDisplacementMap",
            Flood(..)             => "feFlood",
            GaussianBlur(..)      => "feGaussianBlur",
            Image(..)             => "feImage",
            Merge(..)             => "feMerge",
            Morphology(..)        => "feMorphology",
            Offset(..)            => "feOffset",
            SpecularLighting(..)  => "feSpecularLighting",
            Tile(..)              => "feTile",
            Turbulence(..)        => "feTurbulence",
        }
    }

    #[rustfmt::skip]
    fn get_input_requirements(&self) -> InputRequirements {
        use PrimitiveParams::*;
        match self {
            Blend(p)             => p.get_input_requirements(),
            ColorMatrix(p)       => p.get_input_requirements(),
            ComponentTransfer(p) => p.get_input_requirements(),
            Composite(p)         => p.get_input_requirements(),
            ConvolveMatrix(p)    => p.get_input_requirements(),
            DiffuseLighting(p)   => p.get_input_requirements(),
            DisplacementMap(p)   => p.get_input_requirements(),
            Flood(p)             => p.get_input_requirements(),
            GaussianBlur(p)      => p.get_input_requirements(),
            Image(p)             => p.get_input_requirements(),
            Merge(p)             => p.get_input_requirements(),
            Morphology(p)        => p.get_input_requirements(),
            Offset(p)            => p.get_input_requirements(),
            SpecularLighting(p)  => p.get_input_requirements(),
            Tile(p)              => p.get_input_requirements(),
            Turbulence(p)        => p.get_input_requirements(),
        }
    }
}

/// The base filter primitive node containing common properties.
#[derive(Default, Clone)]
pub struct Primitive {
    pub x: Option<Length<Horizontal>>,
    pub y: Option<Length<Vertical>>,
    pub width: Option<ULength<Horizontal>>,
    pub height: Option<ULength<Vertical>>,
    pub result: Option<CustomIdent>,
}

pub struct ResolvedPrimitive {
    pub primitive: Primitive,
    pub params: PrimitiveParams,
}

/// A fully resolved filter primitive in user-space coordinates.
pub struct UserSpacePrimitive {
    x: Option<f64>,
    y: Option<f64>,
    width: Option<f64>,
    height: Option<f64>,
    result: Option<CustomIdent>,

    params: PrimitiveParams,
}

/// An enumeration of possible inputs for a filter primitive.
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub enum Input {
    #[default]
    Unspecified,
    SourceGraphic,
    SourceAlpha,
    BackgroundImage,
    BackgroundAlpha,
    FillPaint,
    StrokePaint,
    FilterOutput(CustomIdent),
}

impl Input {
    pub fn get_requirements(&self) -> InputRequirements {
        use Input::*;

        let mut reqs = InputRequirements::default();

        match self {
            SourceAlpha => reqs.needs_source_alpha = true,
            BackgroundImage => reqs.needs_background_image = true,
            BackgroundAlpha => reqs.needs_background_alpha = true,
            FillPaint => reqs.needs_fill_paint_image = true,
            StrokePaint => reqs.needs_stroke_paint_image = true,
            _ => (),
        }

        reqs
    }
}

impl Parse for Input {
    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
        parser
            .try_parse(|p| {
                parse_identifiers!(
                    p,
                    "SourceGraphic" => Input::SourceGraphic,
                    "SourceAlpha" => Input::SourceAlpha,
                    "BackgroundImage" => Input::BackgroundImage,
                    "BackgroundAlpha" => Input::BackgroundAlpha,
                    "FillPaint" => Input::FillPaint,
                    "StrokePaint" => Input::StrokePaint,
                )
            })
            .or_else(|_: BasicParseError<'_>| {
                let ident = CustomIdent::parse(parser)?;
                Ok(Input::FilterOutput(ident))
            })
    }
}

impl ResolvedPrimitive {
    pub fn into_user_space(self, params: &NormalizeParams) -> UserSpacePrimitive {
        let x = self.primitive.x.map(|l| l.to_user(params));
        let y = self.primitive.y.map(|l| l.to_user(params));
        let width = self.primitive.width.map(|l| l.to_user(params));
        let height = self.primitive.height.map(|l| l.to_user(params));

        UserSpacePrimitive {
            x,
            y,
            width,
            height,
            result: self.primitive.result,
            params: self.params,
        }
    }
}

impl UserSpacePrimitive {
    /// Validates attributes and returns the `BoundsBuilder` for bounds computation.
    #[inline]
    fn get_bounds(&self, ctx: &FilterContext) -> BoundsBuilder {
        BoundsBuilder::new(self.x, self.y, self.width, self.height, ctx.paffine())
    }
}

impl Primitive {
    fn parse_standard_attributes(
        &mut self,
        attrs: &Attributes,
        session: &Session,
    ) -> (Input, Input) {
        let mut input_1 = Input::Unspecified;
        let mut input_2 = Input::Unspecified;

        for (attr, value) in attrs.iter() {
            match attr.expanded() {
                expanded_name!("", "x") => set_attribute(&mut self.x, attr.parse(value), session),
                expanded_name!("", "y") => set_attribute(&mut self.y, attr.parse(value), session),
                expanded_name!("", "width") => {
                    set_attribute(&mut self.width, attr.parse(value), session)
                }
                expanded_name!("", "height") => {
                    set_attribute(&mut self.height, attr.parse(value), session)
                }
                expanded_name!("", "result") => {
                    set_attribute(&mut self.result, attr.parse(value), session)
                }
                expanded_name!("", "in") => set_attribute(&mut input_1, attr.parse(value), session),
                expanded_name!("", "in2") => {
                    set_attribute(&mut input_2, attr.parse(value), session)
                }
                _ => (),
            }
        }

        (input_1, input_2)
    }

    pub fn parse_no_inputs(&mut self, attrs: &Attributes, session: &Session) {
        let (_, _) = self.parse_standard_attributes(attrs, session);
    }

    pub fn parse_one_input(&mut self, attrs: &Attributes, session: &Session) -> Input {
        let (input_1, _) = self.parse_standard_attributes(attrs, session);
        input_1
    }

    pub fn parse_two_inputs(&mut self, attrs: &Attributes, session: &Session) -> (Input, Input) {
        self.parse_standard_attributes(attrs, session)
    }
}

/// Applies a filter and returns the resulting surface.
pub fn render(
    plan: Rc<FilterPlan>,
    filter: &FilterSpec,
    source_surface: SharedImageSurface,
    acquired_nodes: &mut AcquiredNodes<'_>,
    draw_ctx: &mut DrawingCtx,
    node_bbox: &BoundingBox,
) -> Result<SharedImageSurface, InternalRenderingError> {
    let session = draw_ctx.session().clone();

    let surface_width = source_surface.width();
    let surface_height = source_surface.height();

    FilterContext::new(&filter.user_space_filter, plan, source_surface, *node_bbox)
        .and_then(|mut filter_ctx| {
            // the message has an unclosed parenthesis; we'll close it below.
            rsvg_log!(
                session,
                "(filter \"{}\" with effects_region={:?}",
                filter.name,
                filter_ctx.effects_region()
            );
            for user_space_primitive in &filter.primitives {
                let start = Instant::now();

                match render_primitive(user_space_primitive, &filter_ctx, acquired_nodes, draw_ctx)
                {
                    Ok(output) => {
                        let elapsed = start.elapsed();
                        rsvg_log!(
                            session,
                            "(rendered filter primitive {} in {} seconds)",
                            user_space_primitive.params.name(),
                            elapsed.as_secs() as f64 + f64::from(elapsed.subsec_nanos()) / 1e9
                        );

                        filter_ctx.store_result(FilterResult {
                            name: user_space_primitive.result.clone(),
                            output,
                        });
                    }

                    Err(err) => {
                        rsvg_log!(
                            session,
                            "(filter primitive {} returned an error: {})",
                            user_space_primitive.params.name(),
                            err
                        );

                        // close the opening parenthesis from the message at the start of this function
                        rsvg_log!(session, ")");

                        // Exit early on Cairo errors. Continue rendering otherwise.
                        if let FilterError::CairoError(status) = err {
                            return Err(FilterError::CairoError(status));
                        }
                    }
                }
            }

            // close the opening parenthesis from the message at the start of this function
            rsvg_log!(session, ")");

            Ok(filter_ctx.into_output()?)
        })
        .or_else(|err| match err {
            FilterError::CairoError(status) => {
                // Exit early on Cairo errors
                Err(InternalRenderingError::from(status))
            }

            _ => {
                // ignore other filter errors and just return an empty surface
                Ok(SharedImageSurface::empty(
                    surface_width,
                    surface_height,
                    SurfaceType::AlphaOnly,
                )?)
            }
        })
}

#[rustfmt::skip]
fn render_primitive(
    primitive: &UserSpacePrimitive,
    ctx: &FilterContext,
    acquired_nodes: &mut AcquiredNodes<'_>,
    draw_ctx: &mut DrawingCtx,
) -> Result<FilterOutput, FilterError> {
    use PrimitiveParams::*;

    let bounds_builder = primitive.get_bounds(ctx);

    // Note that feDropShadow is not handled here.  When its FilterElement::resolve() is called,
    // it returns a series of lower-level primitives (flood, blur, offset, etc.) that make up
    // the drop-shadow effect.

    match primitive.params {
        Blend(ref p)             => p.render(bounds_builder, ctx),
        ColorMatrix(ref p)       => p.render(bounds_builder, ctx),
        ComponentTransfer(ref p) => p.render(bounds_builder, ctx),
        Composite(ref p)         => p.render(bounds_builder, ctx),
        ConvolveMatrix(ref p)    => p.render(bounds_builder, ctx),
        DiffuseLighting(ref p)   => p.render(bounds_builder, ctx),
        DisplacementMap(ref p)   => p.render(bounds_builder, ctx),
        Flood(ref p)             => p.render(bounds_builder, ctx),
        GaussianBlur(ref p)      => p.render(bounds_builder, ctx),
        Image(ref p)             => p.render(bounds_builder, ctx, acquired_nodes, draw_ctx),
        Merge(ref p)             => p.render(bounds_builder, ctx),
        Morphology(ref p)        => p.render(bounds_builder, ctx),
        Offset(ref p)            => p.render(bounds_builder, ctx),
        SpecularLighting(ref p)  => p.render(bounds_builder, ctx),
        Tile(ref p)              => p.render(bounds_builder, ctx),
        Turbulence(ref p)        => p.render(bounds_builder, ctx),
    }
}

impl From<ColorInterpolationFilters> for SurfaceType {
    fn from(c: ColorInterpolationFilters) -> Self {
        match c {
            ColorInterpolationFilters::LinearRgb => SurfaceType::LinearRgb,
            _ => SurfaceType::SRgb,
        }
    }
}

impl Parse for EdgeMode {
    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
        Ok(parse_identifiers!(
            parser,
            "duplicate" => EdgeMode::Duplicate,
            "wrap" => EdgeMode::Wrap,
            "none" => EdgeMode::None,
        )?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::document::Document;
    use crate::dpi::Dpi;
    use crate::node::NodeBorrow;
    use crate::properties::Filter;

    fn get_input_requirements_for_node(document: &Document, node_id: &str) -> InputRequirements {
        let node = document.lookup_internal_node(node_id).unwrap();
        let elt = node.borrow_element();
        let values = elt.get_computed_values();

        let session = Session::default();
        let mut acquired_nodes = AcquiredNodes::new(&document, None::<gio::Cancellable>);

        let viewport = Viewport::new(Dpi::new(96.0, 96.0), 100.0, 100.0);

        let filter = values.filter();

        let filter_list = match filter {
            Filter::None => {
                panic!("the referenced node should have a filter property that is not 'none'")
            }
            Filter::List(filter_list) => filter_list,
        };

        let params = NormalizeParams::new(&values, &viewport);

        let filter_specs = filter_list
            .iter()
            .map(|filter_value| {
                filter_value.to_filter_spec(
                    &mut acquired_nodes,
                    &params,
                    cssparser::Color::parse_str("black").unwrap(),
                    &viewport,
                    &session,
                    "rect",
                )
            })
            .collect::<Result<Vec<FilterSpec>, _>>()
            .unwrap();

        InputRequirements::new_from_filter_specs(&filter_specs)
    }

    fn input_requirements_with_only_source_alpha() -> InputRequirements {
        InputRequirements {
            needs_source_alpha: true,
            needs_background_image: false,
            needs_background_alpha: false,
            needs_stroke_paint_image: false,
            needs_fill_paint_image: false,
        }
    }

    #[test]
    fn detects_source_alpha() {
        let document = Document::load_from_bytes(include_bytes!("test_input_requirements.svg"));

        assert_eq!(
            get_input_requirements_for_node(&document, "rect_1"),
            input_requirements_with_only_source_alpha(),
        );

        assert_eq!(
            get_input_requirements_for_node(&document, "rect_2"),
            input_requirements_with_only_source_alpha(),
        );

        assert_eq!(
            get_input_requirements_for_node(&document, "rect_3"),
            InputRequirements {
                needs_source_alpha: false,
                needs_background_image: true,
                needs_background_alpha: true,
                needs_stroke_paint_image: true,
                needs_fill_paint_image: true,
            }
        );
    }
}