[librsvg: 1/2] drawing_ctx: add with_alpha() method



commit 5db9c7d5809103010dffaff48ed86bb71b1dda3f
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Feb 2 11:45:23 2020 +0100

    drawing_ctx: add with_alpha() method
    
    Continue the pattern of having with_foo methods on the drawing ctx.

 rsvg_internals/src/drawing_ctx.rs | 20 ++++++++++++++++++++
 rsvg_internals/src/pattern.rs     | 26 +++++---------------------
 2 files changed, 25 insertions(+), 21 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 6514b461..afa33959 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -678,6 +678,26 @@ impl DrawingCtx {
         res
     }
 
+    /// Run the drawing function with the specified opacity
+    pub fn with_alpha(
+        &mut self,
+        opacity: UnitInterval,
+        draw_fn: &mut dyn FnMut(&mut DrawingCtx) -> Result<BoundingBox, RenderingError>,
+    ) -> Result<BoundingBox, RenderingError> {
+        let res;
+        let UnitInterval(o) = opacity;
+        if o < 1.0 {
+            self.cr.push_group();
+            res = draw_fn(self);
+            self.cr.pop_group_to_source();
+            self.cr.paint_with_alpha(o);
+        } else {
+            res = draw_fn(self);
+        }
+
+        res
+    }
+
     /// Saves the current Cairo context, runs the draw_fn, and restores the context
     pub fn with_saved_cr(
         &mut self,
diff --git a/rsvg_internals/src/pattern.rs b/rsvg_internals/src/pattern.rs
index bdedf78b..61a1c0e9 100644
--- a/rsvg_internals/src/pattern.rs
+++ b/rsvg_internals/src/pattern.rs
@@ -231,7 +231,7 @@ impl AsPaintSource for ResolvedPattern {
         opacity: UnitInterval,
         bbox: &BoundingBox,
     ) -> Result<bool, RenderingError> {
-        let node_with_children = if let Some(n) = self.children.node_with_children() {
+        let node = if let Some(n) = self.children.node_with_children() {
             n
         } else {
             // This means we didn't find any children among the fallbacks,
@@ -348,30 +348,14 @@ impl AsPaintSource for ResolvedPattern {
         // Set up transformations to be determined by the contents units
         cr_pattern.set_matrix(caffine.into());
 
-        // Declare a drawing function
-        fn draw_children(
-            acquired_nodes: &mut AcquiredNodes,
-            ctx: &mut DrawingCtx,
-            node: &RsvgNode,
-        ) -> Result<BoundingBox, RenderingError> {
+        // Draw everything
+        let res = draw_ctx.with_alpha(opacity, &mut |dc| {
             let pattern_cascaded = CascadedValues::new_from_node(&node);
             let pattern_values = pattern_cascaded.get();
-            ctx.with_discrete_layer(&node, acquired_nodes, pattern_values, false, &mut |an, dc| {
+            dc.with_discrete_layer(&node, acquired_nodes, pattern_values, false, &mut |an, dc| {
                 node.draw_children(an, &pattern_cascaded, dc, false)
             })
-        }
-
-        // Draw everything
-        let res;
-        let UnitInterval(o) = opacity;
-        if o < 1.0 {
-            cr_pattern.push_group();
-            res = draw_children(acquired_nodes, draw_ctx, &node_with_children);
-            cr_pattern.pop_group_to_source();
-            cr_pattern.paint_with_alpha(o);
-        } else {
-            res = draw_children(acquired_nodes, draw_ctx, &node_with_children);
-        }
+        });
 
         // Return to the original coordinate system and rendering context
         draw_ctx.set_cairo_context(&cr_save);


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