[librsvg: 1/31] Stroke - new struct for a stroke parameter's in user-space coordinates




commit 651b98c31f6ee43984ba2533412ed25001ff90da
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jun 2 11:17:33 2021 -0500

    Stroke - new struct for a stroke parameter's in user-space coordinates

 src/drawing_ctx.rs | 28 ++++++++++++----------------
 src/layout.rs      | 43 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 17 deletions(-)
---
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 83133490..4e6ea53a 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -14,7 +14,6 @@ use crate::accept_language::UserLanguage;
 use crate::aspect_ratio::AspectRatio;
 use crate::bbox::BoundingBox;
 use crate::coord_units::CoordUnits;
-use crate::dasharray::Dasharray;
 use crate::document::{AcquiredNodes, NodeId};
 use crate::dpi::Dpi;
 use crate::element::Element;
@@ -22,7 +21,7 @@ use crate::error::{AcquireError, ImplementationLimit, RenderingError};
 use crate::filters::{self, FilterSpec};
 use crate::float_eq_cairo::ApproxEqCairo;
 use crate::gradient::{GradientVariant, SpreadMethod, UserSpaceGradient};
-use crate::layout::StackingContext;
+use crate::layout::{StackingContext, Stroke};
 use crate::length::*;
 use crate::marker;
 use crate::node::{CascadedValues, Node, NodeBorrow, NodeDraw};
@@ -32,7 +31,7 @@ use crate::pattern::UserSpacePattern;
 use crate::properties::ComputedValues;
 use crate::property_defs::{
     ClipRule, FillRule, Filter, MixBlendMode, Opacity, Overflow, PaintTarget, ShapeRendering,
-    StrokeDasharray, StrokeLinecap, StrokeLinejoin, TextRendering,
+    StrokeLinecap, StrokeLinejoin, TextRendering,
 };
 use crate::rect::Rect;
 use crate::shapes::{Markers, Shape};
@@ -1112,22 +1111,19 @@ impl DrawingCtx {
         let view_params = self.get_view_params();
         let params = NormalizeParams::new(values, &view_params);
 
-        cr.set_line_width(values.stroke_width().0.to_user(&params));
-        cr.set_miter_limit(values.stroke_miterlimit().0);
-        cr.set_line_cap(cairo::LineCap::from(values.stroke_line_cap()));
-        cr.set_line_join(cairo::LineJoin::from(values.stroke_line_join()));
+        let stroke = Stroke::new(values, &params);
 
-        if let StrokeDasharray(Dasharray::Array(ref dashes)) = values.stroke_dasharray() {
-            let normalized_dashes: Vec<f64> = dashes.iter().map(|l| l.to_user(&params)).collect();
+        cr.set_line_width(stroke.width);
+        cr.set_miter_limit(stroke.miter_limit.0);
+        cr.set_line_cap(cairo::LineCap::from(stroke.line_cap));
+        cr.set_line_join(cairo::LineJoin::from(stroke.line_join));
 
-            let total_length = normalized_dashes.iter().fold(0.0, |acc, &len| acc + len);
+        let total_length: f64 = stroke.dashes.iter().sum();
 
-            if total_length > 0.0 {
-                let offset = values.stroke_dashoffset().0.to_user(&params);
-                cr.set_dash(&normalized_dashes, offset);
-            } else {
-                cr.set_dash(&[], 0.0);
-            }
+        if total_length > 0.0 {
+            cr.set_dash(&stroke.dashes, stroke.dash_offset);
+        } else {
+            cr.set_dash(&[], 0.0);
         }
     }
 
diff --git a/src/layout.rs b/src/layout.rs
index 43009b9f..fd05f558 100644
--- a/src/layout.rs
+++ b/src/layout.rs
@@ -2,11 +2,15 @@
 //!
 //! The idea is to take the DOM tree and produce a layout tree with SVG concepts.
 
+use crate::dasharray::Dasharray;
 use crate::document::AcquiredNodes;
 use crate::element::Element;
+use crate::length::*;
 use crate::node::*;
 use crate::properties::ComputedValues;
-use crate::property_defs::{Filter, Opacity};
+use crate::property_defs::{
+    Filter, Opacity, StrokeDasharray, StrokeLinecap, StrokeLinejoin, StrokeMiterlimit,
+};
 use crate::transform::Transform;
 use crate::unit_interval::UnitInterval;
 
@@ -31,6 +35,16 @@ pub struct StackingContext {
     pub mask: Option<Node>,
 }
 
+/// Stroke parameters in user-space coordinates.
+pub struct Stroke {
+    pub width: f64,
+    pub miter_limit: StrokeMiterlimit,
+    pub line_cap: StrokeLinecap,
+    pub line_join: StrokeLinejoin,
+    pub dash_offset: f64,
+    pub dashes: Box<[f64]>,
+}
+
 impl StackingContext {
     pub fn new(
         acquired_nodes: &mut AcquiredNodes<'_>,
@@ -93,3 +107,30 @@ impl StackingContext {
         }
     }
 }
+
+impl Stroke {
+    pub fn new(values: &ComputedValues, params: &NormalizeParams) -> Stroke {
+        let width = values.stroke_width().0.to_user(params);
+        let miter_limit = values.stroke_miterlimit();
+        let line_cap = values.stroke_line_cap();
+        let line_join = values.stroke_line_join();
+        let dash_offset = values.stroke_dashoffset().0.to_user(&params);
+
+        let dashes = match values.stroke_dasharray() {
+            StrokeDasharray(Dasharray::None) => Box::new([]),
+            StrokeDasharray(Dasharray::Array(dashes)) => dashes
+                .iter()
+                .map(|l| l.to_user(&params))
+                .collect::<Box<[f64]>>(),
+        };
+
+        Stroke {
+            width,
+            miter_limit,
+            line_cap,
+            line_join,
+            dash_offset,
+            dashes,
+        }
+    }
+}


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