[librsvg: 5/38] draw_tree - Take a single DrawingMode enum instead of limit_to_stack/node/cascaded arguments




commit 3342df57f52ab7644ae0d4f3b5531a30c4f35049
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Jun 19 18:25:11 2020 -0500

    draw_tree - Take a single DrawingMode enum instead of limit_to_stack/node/cascaded arguments
    
    Those are in fact related.  The cascaded values can be derived from the node
    at which drawing starts, and the root is just a convenience for the function.

 rsvg_internals/src/drawing_ctx.rs | 35 ++++++++++++++++++++++++-----------
 rsvg_internals/src/handle.rs      | 33 +++++++++++++++------------------
 2 files changed, 39 insertions(+), 29 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index a28efd89..fcdeed97 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -145,29 +145,42 @@ pub struct DrawingCtx {
     testing: bool,
 }
 
+pub enum DrawingMode {
+    LimitToStack {
+        node: Node,
+        root: Node,
+    },
+
+    OnlyNode(Node),
+}
+
 /// The toplevel drawing routine.
 ///
 /// This creates a DrawingCtx internally and starts drawing at the specified `node`.
 pub fn draw_tree(
-    limit_to_stack: Option<&Node>,
+    mode: DrawingMode,
     cr: &cairo::Context,
     viewport: Rect,
     dpi: Dpi,
     measuring: bool,
     testing: bool,
-    node: &Node,
     acquired_nodes: &mut AcquiredNodes,
-    cascaded: &CascadedValues<'_>,
 ) -> Result<BoundingBox, RenderingError> {
-    let drawsub_stack = if let Some(limit_to_stack) = limit_to_stack {
-        limit_to_stack
-            .ancestors()
-            .map(|n| n.clone())
-            .collect()
-    } else {
-        Vec::new()
+    let (drawsub_stack, node) = match mode {
+        DrawingMode::LimitToStack { node, root } => {
+            (node
+             .ancestors()
+             .map(|n| n.clone())
+             .collect(),
+             root,
+            )
+        }
+
+        DrawingMode::OnlyNode(node) => (Vec::new(), node),
     };
 
+    let cascaded = CascadedValues::new_from_node(&node);
+
     let mut draw_ctx = DrawingCtx::new(
         cr,
         viewport,
@@ -177,7 +190,7 @@ pub fn draw_tree(
         drawsub_stack,
     );
 
-    draw_ctx.draw_node_from_stack(node, acquired_nodes, cascaded, false)
+    draw_ctx.draw_node_from_stack(&node, acquired_nodes, &cascaded, false)
 }
 
 impl DrawingCtx {
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index c5a6bce2..6a061634 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -7,7 +7,7 @@ use crate::bbox::BoundingBox;
 use crate::css::{Origin, Stylesheet};
 use crate::document::{AcquiredNodes, Document};
 use crate::dpi::Dpi;
-use crate::drawing_ctx::draw_tree;
+use crate::drawing_ctx::{DrawingMode, draw_tree};
 use crate::element::Element;
 use crate::error::{DefsLookupErrorKind, LoadingError, RenderingError};
 use crate::node::{CascadedValues, Node, NodeBorrow};
@@ -113,21 +113,20 @@ impl Handle {
         dpi: Dpi,
         is_testing: bool,
     ) -> Result<(Rect, Rect), RenderingError> {
+        let node = node.clone();
         let root = self.document.root();
 
         let target = cairo::ImageSurface::create(cairo::Format::Rgb24, 1, 1)?;
         let cr = cairo::Context::new(&target);
 
         let bbox = draw_tree(
-            Some(node),
+            DrawingMode::LimitToStack { node, root },
             &cr,
             viewport,
             dpi,
             true,
             is_testing,
-            &root,
             &mut AcquiredNodes::new(&self.document),
-            &CascadedValues::new_from_node(&root),
         )?;
 
         let ink_rect = bbox.ink_rect.unwrap_or_default();
@@ -243,26 +242,26 @@ impl Handle {
     ) -> Result<(), RenderingError> {
         check_cairo_context(cr)?;
 
-        let node = if let Some(id) = id {
-            Some(self.lookup_node(id).map_err(RenderingError::InvalidId)?)
+        let root = self.document.root();
+
+        let mode = if let Some(id) = id {
+            let node = self.lookup_node(id).map_err(RenderingError::InvalidId)?;
+
+            DrawingMode::LimitToStack { node, root }
         } else {
-            None
+            DrawingMode::OnlyNode(root)
         };
 
-        let root = self.document.root();
-
         cr.save();
 
         let res = draw_tree(
-            node.as_ref(),
+            mode,
             cr,
             Rect::from(*viewport),
             dpi,
             false,
             is_testing,
-            &root,
             &mut AcquiredNodes::new(&self.document),
-            &CascadedValues::new_from_node(&root),
         );
 
         cr.restore();
@@ -279,16 +278,16 @@ impl Handle {
         let target = cairo::ImageSurface::create(cairo::Format::Rgb24, 1, 1)?;
         let cr = cairo::Context::new(&target);
 
+        let node = node.clone();
+
         draw_tree(
-            None,
+            DrawingMode::OnlyNode(node),
             &cr,
             unit_rectangle(),
             dpi,
             true,
             is_testing,
-            node,
             &mut AcquiredNodes::new(&self.document),
-            &CascadedValues::new_from_node(node),
         )
     }
 
@@ -352,15 +351,13 @@ impl Handle {
         cr.translate(-ink_r.x0, -ink_r.y0);
 
         let res = draw_tree(
-            None,
+            DrawingMode::OnlyNode(node),
             &cr,
             unit_rectangle(),
             dpi,
             false,
             is_testing,
-            &node,
             &mut AcquiredNodes::new(&self.document),
-            &CascadedValues::new_from_node(&node),
         );
 
         cr.restore();


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