[librsvg: 5/10] image filter: use Fragment::parse to decide what to do




commit afd7bb97cb118d92dbde67db9d357acbd95f9258
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Dec 12 16:08:15 2020 +0100

    image filter: use Fragment::parse to decide what to do
    
    Store href as a string, then at render time we parse it as a fragment,
    and if it is one, we render a node, otherwise we render an image.

 src/filters/image.rs | 43 +++++++++++++++++++------------------------
 1 file changed, 19 insertions(+), 24 deletions(-)
---
diff --git a/src/filters/image.rs b/src/filters/image.rs
index 339168b5..e99362ba 100644
--- a/src/filters/image.rs
+++ b/src/filters/image.rs
@@ -5,13 +5,12 @@ use crate::attributes::Attributes;
 use crate::document::AcquiredNodes;
 use crate::drawing_ctx::DrawingCtx;
 use crate::element::{ElementResult, SetAttributes};
-use crate::error::*;
 use crate::href::{is_href, set_href};
 use crate::node::{CascadedValues, Node};
 use crate::parsers::ParseValue;
 use crate::rect::Rect;
 use crate::surface_utils::shared_surface::SharedImageSurface;
-use crate::url_resolver::{Fragment, Href};
+use crate::url_resolver::Fragment;
 use crate::viewbox::ViewBox;
 
 use super::context::{FilterContext, FilterOutput, FilterResult};
@@ -21,7 +20,7 @@ use super::{FilterEffect, FilterError, Primitive};
 pub struct FeImage {
     base: Primitive,
     aspect: AspectRatio,
-    href: Option<Href>,
+    href: Option<String>,
 }
 
 impl Default for FeImage {
@@ -109,11 +108,7 @@ impl SetAttributes for FeImage {
 
                 // "path" is used by some older Adobe Illustrator versions
                 ref a if is_href(a) || *a == expanded_name!("", "path") => {
-                    let href = Href::parse(value)
-                        .map_err(ValueErrorKind::from)
-                        .attribute(attr.clone())?;
-
-                    set_href(a, &mut self.href, href);
+                    set_href(a, &mut self.href, value.to_string());
                 }
 
                 _ => (),
@@ -135,22 +130,22 @@ impl FilterEffect for FeImage {
         let bounds_builder = self.base.get_bounds(ctx, node.parent().as_ref())?;
         let bounds = bounds_builder.into_rect(draw_ctx);
 
-        let surface = match self.href {
-            Some(Href::PlainUrl(ref url)) => {
-                let unclipped_bounds = bounds_builder.into_rect_without_clipping(draw_ctx);
-                self.render_external_image(
-                    ctx,
-                    acquired_nodes,
-                    draw_ctx,
-                    bounds,
-                    &unclipped_bounds,
-                    url,
-                )
-            }
-            Some(Href::WithFragment(ref frag)) => {
-                self.render_node(ctx, acquired_nodes, draw_ctx, bounds, frag)
-            }
-            _ => Err(FilterError::InvalidInput),
+        let href = self.href.as_ref().ok_or(FilterError::InvalidInput)?;
+
+        let surface = if let Ok(fragment) = Fragment::parse(href) {
+            // if there is a fragment, render as a node
+            self.render_node(ctx, acquired_nodes, draw_ctx, bounds, &fragment)
+        } else {
+            // if there is no fragment, render as an image
+            let unclipped_bounds = bounds_builder.into_rect_without_clipping(draw_ctx);
+            self.render_external_image(
+                ctx,
+                acquired_nodes,
+                draw_ctx,
+                bounds,
+                &unclipped_bounds,
+                href,
+            )
         }?;
 
         Ok(FilterResult {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]