[librsvg: 4/13] New test for the computed bounds of text elements at the API level




commit 0e371a1c0ba2a8bde65c6d3e2eea71fd0f93d7f0
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Feb 3 18:14:17 2022 -0600

    New test for the computed bounds of text elements at the API level
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/660>

 src/rect.rs                    | 10 +++++++++
 tests/fixtures/text/bounds.svg |  8 +++----
 tests/src/text.rs              | 49 +++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 62 insertions(+), 5 deletions(-)
---
diff --git a/src/rect.rs b/src/rect.rs
index a8325f178..7de0cd4cb 100644
--- a/src/rect.rs
+++ b/src/rect.rs
@@ -4,6 +4,7 @@
 mod rect {
     use crate::float_eq_cairo::ApproxEqCairo;
     use core::ops::{Add, Range, Sub};
+    use float_cmp::approx_eq;
     use num_traits::Zero;
 
     // Use our own min() and max() that are acceptable for floating point
@@ -154,6 +155,15 @@ mod rect {
                 y1: self.y1 * y,
             }
         }
+
+        pub fn approx_eq(&self, other: &Self) -> bool {
+            // FIXME: this is super fishy; shouldn't we be using 2x the epsilon against the width/height
+            // instead of the raw coordinates?
+            approx_eq!(f64, self.x0, other.x0)
+                && approx_eq!(f64, self.y0, other.y0)
+                && approx_eq!(f64, self.x1, other.x1)
+                && approx_eq!(f64, self.y1, other.y1)
+        }
     }
 }
 
diff --git a/tests/fixtures/text/bounds.svg b/tests/fixtures/text/bounds.svg
index d21b79605..1f78a5c27 100644
--- a/tests/fixtures/text/bounds.svg
+++ b/tests/fixtures/text/bounds.svg
@@ -33,15 +33,15 @@
        The second test is vertical, to test the vertical bounds.
   -->
 
-  <text x="50" y="100" fill="url(#horizontal)" stroke-width="2">XX</text>
-  <text x="0" y="-10" fill="url(#horizontal)" transform="translate(200, 60) rotate(90)">XX</text>
+  <text id="a" x="50" y="100" fill="url(#horizontal)" stroke-width="2">XX</text>
+  <text id="b" x="0" y="-10" fill="url(#horizontal)" transform="translate(200, 60) rotate(90)">XX</text>
 
-  <text fill="url(#vertical)">
+  <text id="c" fill="url(#vertical)">
     <tspan x="300" y="100">X</tspan>
     <tspan x="300" y="150">X</tspan>
   </text>
 
-  <text fill="url(#vertical)" transform="translate(490, 110) rotate(-90)">
+  <text id="d" fill="url(#vertical)" transform="translate(490, 110) rotate(-90)">
     <tspan x="0" y="-50">X</tspan>
     <tspan x="0" y="0">X</tspan>
   </text>
diff --git a/tests/src/text.rs b/tests/src/text.rs
index f73f23926..8e0ace8f8 100644
--- a/tests/src/text.rs
+++ b/tests/src/text.rs
@@ -1,7 +1,9 @@
 use cairo;
+use librsvg::{CairoRenderer, Loader};
+use test_generator::test_resources;
 
 use crate::reference_utils::{Compare, Evaluate, Reference};
-use crate::utils::{load_svg, render_document, SurfaceSize};
+use crate::utils::{load_svg, render_document, setup_font_map, SurfaceSize};
 use crate::{test_compare_render_output, test_svg_reference};
 
 // From https://www.w3.org/Style/CSS/Test/Fonts/Ahem/
@@ -75,3 +77,48 @@ test_svg_reference!(
     "tests/fixtures/text/bounds.svg",
     "tests/fixtures/text/bounds-ref.svg"
 );
+
+fn rect(x: f64, y: f64, width: f64, height: f64) -> cairo::Rectangle {
+    cairo::Rectangle {
+        x,
+        y,
+        width,
+        height,
+    }
+}
+
+fn rectangle_approx_eq(a: &cairo::Rectangle, b: &cairo::Rectangle) -> bool {
+    // FIXME: this is super fishy; shouldn't we be using 2x the epsilon against the width/height
+    // instead of the raw coordinates?
+    approx_eq!(f64, a.x, b.x)
+        && approx_eq!(f64, a.y, b.y)
+        && approx_eq!(f64, a.width, b.width)
+        && approx_eq!(f64, a.height, b.height)
+}
+
+// Test that the computed geometry of text layers is as expected.
+#[test_resources("tests/fixtures/text/bounds.svg")]
+fn text_layer_geometry(name: &str) {
+    setup_font_map();
+
+    let handle = Loader::new()
+        .read_path(name)
+        .unwrap_or_else(|e| panic!("could not load: {}", e));
+
+    let renderer = CairoRenderer::new(&handle).test_mode(true);
+
+    let viewport = rect(0.0, 0.0, 600.0, 600.0);
+
+    // tuples of (element_id, ink_rect)
+    let cases = vec![
+        ("#a", rect(50.0, 60.0, 100.0, 50.0)),
+        ("#b", rect(200.0, 60.0, 50.0, 100.0)),
+        ("#c", rect(300.0, 60.0, 50.0, 100.0)),
+        ("#d", rect(400.0, 60.0, 100.0, 50.0)),
+    ];
+
+    for (id, expected_ink_rect) in cases {
+        let (ink_rect, _) = renderer.geometry_for_layer(Some(id), &viewport).unwrap();
+        assert!(rectangle_approx_eq(&ink_rect, &expected_ink_rect));
+    }
+}


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