[librsvg: 6/7] drawing_ctx: make with_saved_matrix take an optional transform



commit 86209d07270036408418f4befcfd24caffa1d061
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Jan 2 20:08:39 2020 +0100

    drawing_ctx: make with_saved_matrix take an optional transform
    
    This way node.rs does not have to work on the cairo context itself.

 rsvg_internals/src/drawing_ctx.rs | 40 ++++++++++++++++++++++++---------------
 rsvg_internals/src/node.rs        |  6 ++----
 2 files changed, 27 insertions(+), 19 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 470784b7..506fff09 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -372,21 +372,23 @@ impl DrawingCtx {
 
             let cascaded = CascadedValues::new_from_node(node);
 
-            self.with_saved_matrix(&mut |dc| {
-                let cr = dc.get_cairo_context();
+            let matrix = if units == CoordUnits::ObjectBoundingBox {
+                let bbox_rect = bbox.rect.as_ref().unwrap();
+
+                Some(cairo::Matrix::new(
+                    bbox_rect.width(),
+                    0.0,
+                    0.0,
+                    bbox_rect.height(),
+                    bbox_rect.x0,
+                    bbox_rect.y0,
+                ))
+            } else {
+                None
+            };
 
-                if units == CoordUnits::ObjectBoundingBox {
-                    let bbox_rect = bbox.rect.as_ref().unwrap();
-
-                    cr.transform(cairo::Matrix::new(
-                        bbox_rect.width(),
-                        0.0,
-                        0.0,
-                        bbox_rect.height(),
-                        bbox_rect.x0,
-                        bbox_rect.y0,
-                    ))
-                }
+            self.with_saved_matrix(matrix, &mut |dc| {
+                let cr = dc.get_cairo_context();
 
                 // here we don't push a layer because we are clipping
                 let res = node.draw_children(&cascaded, dc, true);
@@ -633,7 +635,8 @@ impl DrawingCtx {
         initial_with_offset
     }
 
-    /// Saves the current Cairo matrix, runs the draw_fn, and restores the matrix
+    /// Saves the current Cairo matrix, applies a transform if specified,
+    /// runs the draw_fn, and restores the original matrix
     ///
     /// This is slightly cheaper than a `cr.save()` / `cr.restore()`
     /// pair, but more importantly, it does not reset the whole
@@ -641,10 +644,17 @@ impl DrawingCtx {
     /// was set by the `draw_fn`.
     pub fn with_saved_matrix(
         &mut self,
+        transform: Option<cairo::Matrix>,
         draw_fn: &mut dyn FnMut(&mut DrawingCtx) -> Result<BoundingBox, RenderingError>,
     ) -> Result<BoundingBox, RenderingError> {
         let matrix = self.cr.get_matrix();
+
+        if let Some(t) = transform {
+            self.cr.transform(t);
+        }
+
         let res = draw_fn(self);
+
         self.cr.set_matrix(matrix);
 
         if let Ok(bbox) = res {
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index b3dd0130..702fa15a 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -502,10 +502,8 @@ impl NodeDraw for RsvgNode {
         clipping: bool,
     ) -> Result<BoundingBox, RenderingError> {
         if !self.borrow().is_in_error() {
-            draw_ctx.with_saved_matrix(&mut |dc| {
-                let cr = dc.get_cairo_context();
-                cr.transform(self.borrow().get_transform());
-
+            let transform = self.borrow().get_transform();
+            draw_ctx.with_saved_matrix(Some(transform), &mut |dc| {
                 self.borrow()
                     .get_node_trait()
                     .draw(self, cascaded, dc, clipping)


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