[librsvg: 1/3] (#618) Add context-fill and stroke for use element




commit 4a571cfc7be4a4de5ae97468cb13335c8b0c8ecf
Author: Madds H <madds hollandart io>
Date:   Wed Jun 30 21:38:32 2021 -0500

    (#618) Add context-fill and stroke for use element
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/-/issues/618
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/572>

 src/drawing_ctx.rs     | 16 ++++++++++++++--
 src/filters/image.rs   |  3 ++-
 src/node.rs            | 18 ++++++++++++++----
 src/shapes.rs          |  1 -
 src/structure.rs       | 27 ++++++++++++++++++++++++++-
 tests/src/reference.rs | 29 +++++++++++++++++++++++++++++
 6 files changed, 85 insertions(+), 9 deletions(-)
---
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 6e429857..af647c94 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -1564,6 +1564,8 @@ impl DrawingCtx {
         use_rect: Rect,
         link: &NodeId,
         clipping: bool,
+        fill_paint: PaintSource,
+        stroke_paint: PaintSource,
     ) -> Result<BoundingBox, RenderingError> {
         // <use> is an element that is used directly, unlike
         // <pattern>, which is used through a fill="url(#...)"
@@ -1659,7 +1661,12 @@ impl DrawingCtx {
 
                     child.draw_children(
                         an,
-                        &CascadedValues::new_from_values(&child, values),
+                        &CascadedValues::new_from_values(
+                            &child,
+                            values,
+                            Some(fill_paint.clone()),
+                            Some(stroke_paint.clone()),
+                        ),
                         dc,
                         clipping,
                     )
@@ -1684,7 +1691,12 @@ impl DrawingCtx {
                 &mut |an, dc| {
                     child.draw(
                         an,
-                        &CascadedValues::new_from_values(&child, values),
+                        &CascadedValues::new_from_values(
+                            &child,
+                            values,
+                            Some(fill_paint.clone()),
+                            Some(stroke_paint.clone()),
+                        ),
                         dc,
                         clipping,
                     )
diff --git a/src/filters/image.rs b/src/filters/image.rs
index c9ffac3c..9722006c 100644
--- a/src/filters/image.rs
+++ b/src/filters/image.rs
@@ -67,7 +67,8 @@ impl Image {
         // referenced resource is rendered according to the behavior of the use element."
         // I think this means that we use the same cascading mode as <use>, i.e. the
         // referenced object inherits its properties from the feImage element.
-        let cascaded = CascadedValues::new_from_values(&referenced_node, &self.feimage_values);
+        let cascaded =
+            CascadedValues::new_from_values(&referenced_node, &self.feimage_values, None, None);
 
         let image = draw_ctx.draw_node_to_surface(
             &referenced_node,
diff --git a/src/node.rs b/src/node.rs
index c93fd714..10b126c1 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -124,7 +124,12 @@ impl<'a> CascadedValues<'a> {
                 context_stroke: self.context_stroke.clone(),
             },
 
-            CascadedInner::FromValues(ref v) => CascadedValues::new_from_values(node, &*v),
+            CascadedInner::FromValues(ref v) => CascadedValues::new_from_values(
+                node,
+                &*v,
+                self.context_fill.clone(),
+                self.context_stroke.clone(),
+            ),
         }
     }
 
@@ -146,7 +151,12 @@ impl<'a> CascadedValues<'a> {
     ///
     /// This is for the `<use>` element, which draws the element which it references with the
     /// `<use>`'s own cascade, not with the element's original cascade.
-    pub fn new_from_values(node: &'a Node, values: &ComputedValues) -> CascadedValues<'a> {
+    pub fn new_from_values(
+        node: &'a Node,
+        values: &ComputedValues,
+        fill: Option<PaintSource>,
+        stroke: Option<PaintSource>,
+    ) -> CascadedValues<'a> {
         let mut v = Box::new(values.clone());
         node.borrow_element()
             .get_specified_values()
@@ -154,8 +164,8 @@ impl<'a> CascadedValues<'a> {
 
         CascadedValues {
             inner: CascadedInner::FromValues(v),
-            context_fill: None,
-            context_stroke: None,
+            context_fill: fill.clone(),
+            context_stroke: stroke.clone(),
         }
     }
 
diff --git a/src/shapes.rs b/src/shapes.rs
index 731c433c..15b0a423 100644
--- a/src/shapes.rs
+++ b/src/shapes.rs
@@ -50,7 +50,6 @@ macro_rules! impl_draw {
                 cascaded: &CascadedValues<'_>,
                 draw_ctx: &mut DrawingCtx,
                 clipping: bool,
-                //context: &CascadedValues<'_>.inner::FromNode,
             ) -> Result<BoundingBox, RenderingError> {
                 let values = cascaded.get();
                 let view_params = draw_ctx.get_view_params();
diff --git a/src/structure.rs b/src/structure.rs
index 4189f2ac..e6ab541b 100644
--- a/src/structure.rs
+++ b/src/structure.rs
@@ -347,7 +347,32 @@ impl Draw for Use {
             let params = NormalizeParams::new(values, &view_params);
             let rect = self.get_rect(&params);
 
-            draw_ctx.draw_from_use_node(node, acquired_nodes, values, rect, link, clipping)
+            let stroke_paint = values.stroke().0.resolve(
+                acquired_nodes,
+                values.stroke_opacity().0,
+                values.color().0,
+                cascaded.context_fill.clone(),
+                cascaded.context_stroke.clone(),
+            );
+
+            let fill_paint = values.fill().0.resolve(
+                acquired_nodes,
+                values.fill_opacity().0,
+                values.color().0,
+                cascaded.context_fill.clone(),
+                cascaded.context_stroke.clone(),
+            );
+
+            draw_ctx.draw_from_use_node(
+                node,
+                acquired_nodes,
+                values,
+                rect,
+                link,
+                clipping,
+                fill_paint,
+                stroke_paint,
+            )
         } else {
             Ok(draw_ctx.empty_bbox())
         }
diff --git a/tests/src/reference.rs b/tests/src/reference.rs
index 669b239d..abc385a6 100644
--- a/tests/src/reference.rs
+++ b/tests/src/reference.rs
@@ -364,3 +364,32 @@ test_compare_render_output!(
       <rect x="10" y="10" width="100%" height="100%" fill="lime"/>
     </svg>"##,
 );
+
+test_compare_render_output!(
+    use_context_stroke,
+    400,
+    400,
+    br##"<?xml version="1.0" encoding="UTF-8"?>
+    <svg width="100" height="20" viewBox="0 0 40 10" xmlns="http://www.w3.org/2000/svg";>
+  <g id="group">
+    <circle cx="5" cy="5" r="4" stroke="context-stroke" fill="black"/>
+    <circle cx="14" cy="5" r="4" stroke="context-fill"/>
+  </g>
+  <use href="#group" x="20" stroke="blue" fill="yellow"/>
+  <!--
+  Modified from: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use
+  -->
+</svg>
+    "##,
+    br##"<?xml version="1.0" encoding="UTF-8"?>
+    <svg width="100" height="20" viewBox="0 0 40 10" xmlns="http://www.w3.org/2000/svg";>
+    <circle cx="5" cy="5" r="4" fill="black"/>
+    <circle cx="14" cy="5" r="4" fill="black"/>
+    <circle cx="25" cy="5" r="4" stroke="blue" fill="black"/>
+    <circle cx="34" cy="5" r="4" stroke="yellow" fill="yellow"/>
+    <!--
+    Modified from: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use
+    -->
+  </svg>
+    "##,
+);


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