[librsvg: 2/7] drawing_ctx: add get_transform method



commit 1ef0938376e730f82277d8e9683ee083b7d0f072
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Jan 17 22:55:17 2020 +0100

    drawing_ctx: add get_transform method

 rsvg_internals/src/drawing_ctx.rs     | 18 +++++++++++-------
 rsvg_internals/src/filters/context.rs | 10 +++++-----
 rsvg_internals/src/pattern.rs         |  8 +++-----
 rsvg_internals/src/text.rs            | 15 ++++++---------
 4 files changed, 25 insertions(+), 26 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index e8213d4b..cf4ae9d5 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -185,8 +185,12 @@ impl DrawingCtx {
         self.cr.clone()
     }
 
+    pub fn get_transform(&self) -> cairo::Matrix {
+        self.cr.get_matrix()
+    }
+
     pub fn empty_bbox(&self) -> BoundingBox {
-        BoundingBox::new().with_affine(self.cr.get_matrix())
+        BoundingBox::new().with_affine(self.get_transform())
     }
 
     // FIXME: Usage of this function is more less a hack... The caller
@@ -471,7 +475,7 @@ impl DrawingCtx {
 
                 let UnitInterval(opacity) = values.opacity.0;
 
-                let affine_at_start = dc.cr.get_matrix();
+                let affine_at_start = dc.get_transform();
 
                 let (clip_in_user_space, clip_in_object_space) =
                     get_clip_in_user_and_object_space(&mut dc.acquired_nodes, clip_uri);
@@ -618,7 +622,7 @@ impl DrawingCtx {
         transform: Option<cairo::Matrix>,
         draw_fn: &mut dyn FnMut(&mut DrawingCtx) -> Result<BoundingBox, RenderingError>,
     ) -> Result<BoundingBox, RenderingError> {
-        let matrix = self.cr.get_matrix();
+        let orig_transform = self.get_transform();
 
         if let Some(t) = transform {
             self.cr.transform(t);
@@ -626,12 +630,12 @@ impl DrawingCtx {
 
         let res = draw_fn(self);
 
-        self.cr.set_matrix(matrix);
+        self.cr.set_matrix(orig_transform);
 
         if let Ok(bbox) = res {
-            let mut orig_matrix_bbox = BoundingBox::new().with_affine(matrix);
-            orig_matrix_bbox.insert(&bbox);
-            Ok(orig_matrix_bbox)
+            let mut res_bbox = BoundingBox::new().with_affine(orig_transform);
+            res_bbox.insert(&bbox);
+            Ok(res_bbox)
         } else {
             res
         }
diff --git a/rsvg_internals/src/filters/context.rs b/rsvg_internals/src/filters/context.rs
index 74c0dbec..359291ed 100644
--- a/rsvg_internals/src/filters/context.rs
+++ b/rsvg_internals/src/filters/context.rs
@@ -103,7 +103,7 @@ impl FilterContext {
         draw_ctx: &mut DrawingCtx,
         node_bbox: BoundingBox,
     ) -> Self {
-        let cr_affine = draw_ctx.get_cairo_context().get_matrix();
+        let draw_transform = draw_ctx.get_transform();
 
         // The rect can be empty (for example, if the filter is applied to an empty group).
         // However, with userSpaceOnUse it's still possible to create images with a filter.
@@ -113,7 +113,7 @@ impl FilterContext {
         let filter = node_data.get_impl::<Filter>();
 
         let affine = match filter.get_filter_units() {
-            CoordUnits::UserSpaceOnUse => cr_affine,
+            CoordUnits::UserSpaceOnUse => draw_transform,
             CoordUnits::ObjectBoundingBox => {
                 let affine = cairo::Matrix::new(
                     bbox_rect.width(),
@@ -123,12 +123,12 @@ impl FilterContext {
                     bbox_rect.x0,
                     bbox_rect.y0,
                 );
-                cairo::Matrix::multiply(&affine, &cr_affine)
+                cairo::Matrix::multiply(&affine, &draw_transform)
             }
         };
 
         let paffine = match filter.get_primitive_units() {
-            CoordUnits::UserSpaceOnUse => cr_affine,
+            CoordUnits::UserSpaceOnUse => draw_transform,
             CoordUnits::ObjectBoundingBox => {
                 let affine = cairo::Matrix::new(
                     bbox_rect.width(),
@@ -138,7 +138,7 @@ impl FilterContext {
                     bbox_rect.x0,
                     bbox_rect.y0,
                 );
-                cairo::Matrix::multiply(&affine, &cr_affine)
+                cairo::Matrix::multiply(&affine, &draw_transform)
             }
         };
 
diff --git a/rsvg_internals/src/pattern.rs b/rsvg_internals/src/pattern.rs
index b2673f81..6d49ab4b 100644
--- a/rsvg_internals/src/pattern.rs
+++ b/rsvg_internals/src/pattern.rs
@@ -257,12 +257,10 @@ impl AsPaintSource for ResolvedPattern {
             PatternUnits(CoordUnits::UserSpaceOnUse) => (1.0, 1.0),
         };
 
-        let cr = draw_ctx.get_cairo_context();
-        let affine = cr.get_matrix();
-        let taffine = cairo::Matrix::multiply(&pattern_affine, &affine);
+        let taffine = cairo::Matrix::multiply(&pattern_affine, &draw_ctx.get_transform());
 
-        let mut scwscale = (taffine.xx * taffine.xx + taffine.xy * taffine.xy).sqrt();
-        let mut schscale = (taffine.yx * taffine.yx + taffine.yy * taffine.yy).sqrt();
+        let mut scwscale = (taffine.xx.powi(2) + taffine.xy.powi(2)).sqrt();
+        let mut schscale = (taffine.yx.powi(2) + taffine.yy.powi(2)).sqrt();
 
         let scaled_width = pattern_rect.width() * bbwscale;
         let scaled_height = pattern_rect.height() * bbhscale;
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index 2ad63683..405d8bf9 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -271,12 +271,10 @@ impl PositionedSpan {
         clipping: bool,
     ) -> Result<BoundingBox, RenderingError> {
         draw_ctx.with_saved_cr(&mut |dc| {
-            let cr = dc.get_cairo_context();
-
-            let affine = cr.get_matrix();
+            let transform = dc.get_transform();
 
             let gravity = self.layout.get_context().unwrap().get_gravity();
-            let bbox = self.compute_text_bbox(affine, gravity);
+            let bbox = self.compute_text_bbox(transform, gravity);
             if bbox.is_none() {
                 return Ok(dc.empty_bbox());
             }
@@ -287,10 +285,9 @@ impl PositionedSpan {
                 bbox.unwrap()
             };
 
+            let cr = dc.get_cairo_context();
             cr.set_antialias(cairo::Antialias::from(self.values.text_rendering));
-
             dc.setup_cr_for_stroke(&cr, &self.values);
-
             cr.move_to(self.rendered_position.0, self.rendered_position.1);
 
             let rotation = unsafe { pango_sys::pango_gravity_to_rotation(gravity.to_glib()) };
@@ -345,7 +342,7 @@ impl PositionedSpan {
                     if !clipping {
                         let (x0, y0, x1, y1) = cr.stroke_extents();
                         let r = Rect::new(x0, y0, x1, y1);
-                        let ib = BoundingBox::new().with_affine(affine).with_ink_rect(r);
+                        let ib = BoundingBox::new().with_affine(transform).with_ink_rect(r);
                         cr.stroke();
                         bbox.insert(&ib);
                     }
@@ -358,7 +355,7 @@ impl PositionedSpan {
 
     fn compute_text_bbox(
         &self,
-        affine: cairo::Matrix,
+        transform: cairo::Matrix,
         gravity: pango::Gravity,
     ) -> Option<BoundingBox> {
         let (ink, _) = self.layout.get_extents();
@@ -391,7 +388,7 @@ impl PositionedSpan {
 
         let r = Rect::new(x, y, x + w, y + h);
         let bbox = BoundingBox::new()
-            .with_affine(affine)
+            .with_affine(transform)
             .with_rect(r)
             .with_ink_rect(r);
 


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