[librsvg] bbox: add with_extents method



commit 01d789e6000a4dcde0d918607e34998d11367672
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Jun 18 23:21:05 2018 +0200

    bbox: add with_extents method
    
    Similar to with_rect and removes the only place (from a quick look)
    where we assign bbox rect/ink_rect from outside

 rsvg_internals/src/bbox.rs | 19 +++++++++++++++++++
 rsvg_internals/src/draw.rs | 30 ++++--------------------------
 2 files changed, 23 insertions(+), 26 deletions(-)
---
diff --git a/rsvg_internals/src/bbox.rs b/rsvg_internals/src/bbox.rs
index 83770407..d88254ee 100644
--- a/rsvg_internals/src/bbox.rs
+++ b/rsvg_internals/src/bbox.rs
@@ -26,6 +26,16 @@ impl BoundingBox {
         BoundingBox { rect, ..self }
     }
 
+    pub fn with_extents(self, extents: (f64, f64, f64, f64)) -> BoundingBox {
+        let rect = rect_from_extents(extents);
+        BoundingBox { rect, ..self }
+    }
+
+    pub fn with_ink_extents(self, extents: (f64, f64, f64, f64)) -> BoundingBox {
+        let ink_rect = rect_from_extents(extents);
+        BoundingBox { ink_rect, ..self }
+    }
+
     fn combine(&mut self, src: &BoundingBox, clip: bool) {
         if src.rect.is_none() && src.ink_rect.is_none() {
             return;
@@ -50,6 +60,15 @@ impl BoundingBox {
     }
 }
 
+fn rect_from_extents((x1, y1, x2, y2): (f64, f64, f64, f64)) -> Option<cairo::Rectangle> {
+    Some(cairo::Rectangle {
+        x: x1,
+        y: y1,
+        width: x2 - x1,
+        height: y2 - y1,
+    })
+}
+
 fn combine_rects(
     r1: Option<cairo::Rectangle>,
     r2: Option<cairo::Rectangle>,
diff --git a/rsvg_internals/src/draw.rs b/rsvg_internals/src/draw.rs
index 14b56a89..285e77fe 100644
--- a/rsvg_internals/src/draw.rs
+++ b/rsvg_internals/src/draw.rs
@@ -238,28 +238,6 @@ fn path_extents(cr: &cairo::Context) -> (f64, f64, f64, f64) {
     (x1, y1, x2, y2)
 }
 
-fn bbox_from_extents(
-    affine: &cairo::Matrix,
-    (x1, y1, x2, y2): (f64, f64, f64, f64),
-    ink: bool,
-) -> BoundingBox {
-    let mut bb = BoundingBox::new(affine);
-    let rect = cairo::Rectangle {
-        x: x1,
-        y: y1,
-        width: x2 - x1,
-        height: y2 - y1,
-    };
-
-    if ink {
-        bb.ink_rect = Some(rect);
-    } else {
-        bb.rect = Some(rect);
-    }
-
-    bb
-}
-
 fn compute_stroke_and_fill_box(cr: &cairo::Context, values: &ComputedValues) -> BoundingBox {
     let affine = cr.get_matrix();
 
@@ -279,19 +257,19 @@ fn compute_stroke_and_fill_box(cr: &cairo::Context, values: &ComputedValues) ->
     // paths for the icon's shape.  We need to be able to compute the bounding
     // rectangle's extents, even when it has no fill nor stroke.
 
-    let fb = bbox_from_extents(&affine, cr.fill_extents(), true);
+    let fb = BoundingBox::new(&affine).with_ink_extents(cr.fill_extents());
     bbox.insert(&fb);
 
     // Bounding box for stroke
 
     if values.stroke.0 != PaintServer::None {
-        let sb = bbox_from_extents(&affine, cr.stroke_extents(), true);
+        let sb = BoundingBox::new(&affine).with_ink_extents(cr.stroke_extents());
         bbox.insert(&sb);
     }
 
     // objectBoundingBox
 
-    let ob = bbox_from_extents(&affine, path_extents(cr), false);
+    let ob = BoundingBox::new(&affine).with_extents(path_extents(cr));
     bbox.insert(&ob);
 
     // restore tolerance
@@ -379,7 +357,7 @@ pub fn draw_pango_layout(
         pangocairo::functions::layout_path(&cr, layout);
 
         if !clipping {
-            let ib = bbox_from_extents(&affine, cr.stroke_extents(), true);
+            let ib = BoundingBox::new(&affine).with_ink_extents(cr.stroke_extents());
             cr.stroke();
             drawing_ctx::insert_bbox(draw_ctx, &ib);
         }


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