[librsvg: 11/45] Move the functions which set the gradient on the DrawingCtx to other impls



commit af76b4df96c06cf3d955cd75ef0b614604d879d0
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Sep 26 18:28:28 2019 -0500

    Move the functions which set the gradient on the DrawingCtx to other impls
    
    Since they now operate on resolved structs, there is no unwrap()

 rsvg_internals/src/gradient.rs | 175 ++++++++++++++++++++++-------------------
 1 file changed, 92 insertions(+), 83 deletions(-)
---
diff --git a/rsvg_internals/src/gradient.rs b/rsvg_internals/src/gradient.rs
index 6d0cc1a0..810ea2da 100644
--- a/rsvg_internals/src/gradient.rs
+++ b/rsvg_internals/src/gradient.rs
@@ -95,6 +95,57 @@ pub struct CommonGradient {
     pub stops: Vec<ColorStop>,
 }
 
+impl CommonGradient {
+    fn set_on_cairo_pattern(
+        &self,
+        pattern: &cairo::Gradient,
+        bbox: &BoundingBox,
+        opacity: &UnitInterval,
+    ) {
+        let mut affine = self.affine;
+        let units = self.units;
+
+        if units == GradientUnits(CoordUnits::ObjectBoundingBox) {
+            let bbox_rect = bbox.rect.unwrap();
+            let bbox_matrix = cairo::Matrix::new(
+                bbox_rect.width,
+                0.0,
+                0.0,
+                bbox_rect.height,
+                bbox_rect.x,
+                bbox_rect.y,
+            );
+            affine = cairo::Matrix::multiply(&affine, &bbox_matrix);
+        }
+
+        affine.invert();
+        pattern.set_matrix(affine);
+        pattern.set_extend(cairo::Extend::from(self.spread));
+
+        self.add_color_stops_to_pattern(pattern, opacity);
+    }
+
+    fn add_color_stops_to_pattern(
+        &self,
+        pattern: &cairo::Gradient,
+        opacity: &UnitInterval,
+    ) {
+        for stop in &self.stops {
+            let UnitInterval(stop_offset) = stop.offset;
+            let &UnitInterval(o) = opacity;
+            let UnitInterval(stop_opacity) = stop.opacity;
+
+            pattern.add_color_stop_rgba(
+                stop_offset,
+                f64::from(stop.rgba.red_f32()),
+                f64::from(stop.rgba.green_f32()),
+                f64::from(stop.rgba.blue_f32()),
+                f64::from(stop.rgba.alpha_f32()) * stop_opacity * o,
+            );
+        }
+    }
+}
+
 impl Resolve for CommonGradientData {
     fn is_resolved(&self) -> bool {
         self.units.is_some()
@@ -217,57 +268,6 @@ impl CommonGradientData {
         }
     }
 
-    fn add_color_stops_to_pattern(
-        &self,
-        pattern: &cairo::Gradient,
-        opacity: &UnitInterval,
-    ) {
-        if let Some(stops) = self.stops.as_ref() {
-            for stop in stops {
-                let UnitInterval(stop_offset) = stop.offset;
-                let &UnitInterval(o) = opacity;
-                let UnitInterval(stop_opacity) = stop.opacity;
-
-                pattern.add_color_stop_rgba(
-                    stop_offset,
-                    f64::from(stop.rgba.red_f32()),
-                    f64::from(stop.rgba.green_f32()),
-                    f64::from(stop.rgba.blue_f32()),
-                    f64::from(stop.rgba.alpha_f32()) * stop_opacity * o,
-                );
-            }
-        }
-    }
-
-    fn set_on_cairo_pattern(
-        &self,
-        pattern: &cairo::Gradient,
-        bbox: &BoundingBox,
-        opacity: &UnitInterval,
-    ) {
-        let mut affine = self.affine.unwrap();
-        let units = self.units.unwrap();
-
-        if units == GradientUnits(CoordUnits::ObjectBoundingBox) {
-            let bbox_rect = bbox.rect.unwrap();
-            let bbox_matrix = cairo::Matrix::new(
-                bbox_rect.width,
-                0.0,
-                0.0,
-                bbox_rect.height,
-                bbox_rect.x,
-                bbox_rect.y,
-            );
-            affine = cairo::Matrix::multiply(&affine, &bbox_matrix);
-        }
-
-        affine.invert();
-        pattern.set_matrix(affine);
-        pattern.set_extend(cairo::Extend::from(self.spread.unwrap_or_default()));
-
-        self.add_color_stops_to_pattern(pattern, opacity);
-    }
-
     fn bounds_are_valid(&self, bbox: &BoundingBox) -> bool {
         if self.units == Some(GradientUnits(CoordUnits::UserSpaceOnUse)) {
             true
@@ -292,6 +292,21 @@ pub struct LinearGradient {
     y2: LengthVertical,
 }
 
+impl LinearGradient {
+    fn to_cairo_gradient(
+        &self,
+        values: &ComputedValues,
+        params: &ViewParams,
+    ) -> cairo::LinearGradient {
+        cairo::LinearGradient::new(
+            self.x1.normalize(values, params),
+            self.y1.normalize(values, params),
+            self.x2.normalize(values, params),
+            self.y2.normalize(values, params),
+        )
+    }
+}
+
 impl Resolve for LinearGradientData {
     fn is_resolved(&self) -> bool {
         self.x1.is_some() && self.y1.is_some() && self.x2.is_some() && self.y2.is_some()
@@ -340,19 +355,6 @@ impl LinearGradientData {
             y2: y2.unwrap(),
         }
     }
-
-    fn to_cairo_gradient(
-        &self,
-        values: &ComputedValues,
-        params: &ViewParams,
-    ) -> cairo::LinearGradient {
-        cairo::LinearGradient::new(
-            self.x1.as_ref().unwrap().normalize(values, params),
-            self.y1.as_ref().unwrap().normalize(values, params),
-            self.x2.as_ref().unwrap().normalize(values, params),
-            self.y2.as_ref().unwrap().normalize(values, params),
-        )
-    }
 }
 
 #[derive(Copy, Clone, Default)]
@@ -372,6 +374,23 @@ pub struct RadialGradient {
     fy: LengthVertical,
 }
 
+impl RadialGradient {
+    fn to_cairo_gradient(
+        &self,
+        values: &ComputedValues,
+        params: &ViewParams,
+    ) -> cairo::RadialGradient {
+        let n_cx = self.cx.normalize(values, params);
+        let n_cy = self.cy.normalize(values, params);
+        let n_r = self.r.normalize(values, params);
+        let n_fx = self.fx.normalize(values, params);
+        let n_fy = self.fy.normalize(values, params);
+        let (new_fx, new_fy) = fix_focus_point(n_fx, n_fy, n_cx, n_cy, n_r);
+
+        cairo::RadialGradient::new(new_fx, new_fy, 0.0, n_cx, n_cy, n_r)
+    }
+}
+
 impl Resolve for RadialGradientData {
     fn is_resolved(&self) -> bool {
         self.cx.is_some()
@@ -430,21 +449,6 @@ impl RadialGradientData {
             fy: fy.unwrap(),
         }
     }
-
-    fn to_cairo_gradient(
-        &self,
-        values: &ComputedValues,
-        params: &ViewParams,
-    ) -> cairo::RadialGradient {
-        let n_cx = self.cx.as_ref().unwrap().normalize(values, params);
-        let n_cy = self.cy.as_ref().unwrap().normalize(values, params);
-        let n_r = self.r.as_ref().unwrap().normalize(values, params);
-        let n_fx = self.fx.as_ref().unwrap().normalize(values, params);
-        let n_fy = self.fy.as_ref().unwrap().normalize(values, params);
-        let (new_fx, new_fy) = fix_focus_point(n_fx, n_fy, n_cx, n_cy, n_r);
-
-        cairo::RadialGradient::new(new_fx, new_fy, 0.0, n_cx, n_cy, n_r)
-    }
 }
 
 // SVG defines radial gradients as being inside a circle (cx, cy, radius).  The
@@ -574,7 +578,7 @@ macro_rules! impl_resolve {
 }
 
 macro_rules! impl_paint_source {
-    ($gradient:ty, $node_type:pat, $other_gradient:ty, $other_type:pat) => {
+    ($gradient:tt, $node_type:pat, $other_gradient:ty, $other_type:pat) => {
         impl PaintSource for $gradient {
             type Resolved = $gradient;
 
@@ -643,15 +647,20 @@ macro_rules! impl_paint_source {
             ) -> Result<bool, RenderingError> {
                 assert!(self.is_resolved());
 
-                let units = self.common.units.unwrap();
+                let $gradient { common, variant } = self;
+
+                let common = common.to_resolved();
+                let variant = variant.to_resolved();
+
+                let units = common.units;
                 let params = if units == GradientUnits(CoordUnits::ObjectBoundingBox) {
                     draw_ctx.push_view_box(1.0, 1.0)
                 } else {
                     draw_ctx.get_view_params()
                 };
 
-                let p = self.variant.to_cairo_gradient(values, &params);
-                self.common.set_on_cairo_pattern(&p, bbox, opacity);
+                let p = variant.to_cairo_gradient(values, &params);
+                common.set_on_cairo_pattern(&p, bbox, opacity);
                 let cr = draw_ctx.get_cairo_context();
                 cr.set_source(&p);
 


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