[librsvg: 65/90] Offset the initial_affine by the original viewport



commit 21062b3187496d69dd3329ba028cdc64426d6379
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Mar 20 18:42:45 2019 -0600

    Offset the initial_affine by the original viewport
    
    Add a test simple_opacity_with_offset_viewport() for this.

 librsvg_crate/tests/primitives.rs | 42 ++++++++++++++++++++++++++++++++++++++-
 rsvg_internals/src/drawing_ctx.rs | 12 ++++++++---
 2 files changed, 50 insertions(+), 4 deletions(-)
---
diff --git a/librsvg_crate/tests/primitives.rs b/librsvg_crate/tests/primitives.rs
index 5d1cdd1f..789d18a6 100644
--- a/librsvg_crate/tests/primitives.rs
+++ b/librsvg_crate/tests/primitives.rs
@@ -48,6 +48,46 @@ fn simple_opacity_with_transform() {
 
     let reference_surf = SharedImageSurface::new(reference_surf, SurfaceType::SRgb).unwrap();
 
-    compare_to_surface(&output_surf, &reference_surf, "simple_opacity");
     compare_to_surface(&output_surf, &reference_surf, "simple_opacity_with_transform");
 }
+
+#[test]
+fn simple_opacity_with_offset_viewport() {
+    let svg = load_svg(
+        br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"; width="50" height="50">
+  <g opacity="0.5">
+    <rect x="10" y="10" width="30" height="30" fill="blue"/>
+  </g>
+</svg>
+"#,
+    );
+
+    let output_surf = render_to_viewport(
+        &svg,
+        SurfaceSize(150, 150),
+        |_cr| (),
+        cairo::Rectangle {
+            x: 50.0,
+            y: 50.0,
+            width: 50.0,
+            height: 50.0,
+        },
+    )
+    .unwrap();
+
+    let reference_surf = cairo::ImageSurface::create(cairo::Format::ARgb32, 150, 150).unwrap();
+
+    {
+        let cr = cairo::Context::new(&reference_surf);
+
+        cr.translate(50.0, 50.0);
+        cr.rectangle(10.0, 10.0, 30.0, 30.0);
+        cr.set_source_rgba(0.0, 0.0, 1.0, 0.5);
+        cr.fill();
+    }
+
+    let reference_surf = SharedImageSurface::new(reference_surf, SurfaceType::SRgb).unwrap();
+
+    compare_to_surface(&output_surf, &reference_surf, "simple_opacity_with_offset_viewport");
+}
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index c725a782..6a36ec2c 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -474,7 +474,7 @@ impl DrawingCtx {
                     let stack_was_empty = dc.cr_stack.len() == 0;
 
                     let temporary_affine = if stack_was_empty {
-                        let initial_inverse = dc.initial_affine.try_invert().unwrap();
+                        let initial_inverse = dc.initial_affine_with_offset().try_invert().unwrap();
                         let untransformed = cairo::Matrix::multiply(&affine, &initial_inverse);
                         untransformed
                     } else {
@@ -529,7 +529,7 @@ impl DrawingCtx {
                             );
                         }
                     } else {
-                        dc.cr.set_matrix(dc.initial_affine);
+                        dc.cr.set_matrix(dc.initial_affine_with_offset());
 
                         if opacity < 1.0 {
                             dc.cr.paint_with_alpha(opacity);
@@ -552,8 +552,14 @@ impl DrawingCtx {
         }
     }
 
+    fn initial_affine_with_offset(&self) -> cairo::Matrix {
+        let mut initial_with_offset = self.initial_affine;
+        initial_with_offset.translate(self.rect.x, self.rect.y);
+        initial_with_offset
+    }
+
     pub fn set_initial_affine(&self, cr: &cairo::Context) {
-        cr.set_matrix(self.initial_affine);
+        cr.set_matrix(self.initial_affine_with_offset());
     }
 
     /// Saves the current Cairo matrix, runs the draw_fn, and restores the matrix


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