[librsvg: 7/20] feColorMatrix: shorten code by implementing Default and Clone for its parameters




commit 3e3524d813395b4348d6ca718a875e56052c2d30
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Mar 16 12:58:12 2021 -0600

    feColorMatrix: shorten code by implementing Default and Clone for its parameters
    
    We can't just derive(Default) for ColorMatrix, because the
    Matrix5<f64> field defaults to all-zeros, not the identity matrix :(

 src/filters/color_matrix.rs | 35 ++++++++++++++++-------------------
 1 file changed, 16 insertions(+), 19 deletions(-)
---
diff --git a/src/filters/color_matrix.rs b/src/filters/color_matrix.rs
index b8eda8f7..5f6fbac9 100644
--- a/src/filters/color_matrix.rs
+++ b/src/filters/color_matrix.rs
@@ -30,26 +30,27 @@ enum OperationType {
 enum_default!(OperationType, OperationType::Matrix);
 
 /// The `feColorMatrix` filter primitive.
+#[derive(Default)]
 pub struct FeColorMatrix {
     base: Primitive,
-    in1: Input,
-    matrix: Matrix5<f64>,
+    params: ColorMatrix,
 }
 
 /// Resolved `feColorMatrix` primitive for rendering.
+#[derive(Clone)]
 pub struct ColorMatrix {
     in1: Input,
     matrix: Matrix5<f64>,
     color_interpolation_filters: ColorInterpolationFilters,
 }
 
-impl Default for FeColorMatrix {
-    /// Constructs a new `ColorMatrix` with empty properties.
-    #[inline]
-    fn default() -> FeColorMatrix {
-        FeColorMatrix {
-            base: Default::default(),
+impl Default for ColorMatrix {
+    fn default() -> ColorMatrix {
+        ColorMatrix {
             in1: Default::default(),
+            color_interpolation_filters: Default::default(),
+
+            // nalgebra's Default for Matrix5 is all zeroes, so we actually need this :(
             matrix: Matrix5::identity(),
         }
     }
@@ -58,7 +59,7 @@ impl Default for FeColorMatrix {
 #[rustfmt::skip]
 impl SetAttributes for FeColorMatrix {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
-        self.in1 = self.base.parse_one_input(attrs)?;
+        self.params.in1 = self.base.parse_one_input(attrs)?;
 
         // First, determine the operation type.
         let mut operation_type = Default::default();
@@ -72,7 +73,7 @@ impl SetAttributes for FeColorMatrix {
         // Now read the matrix correspondingly.
         // LuminanceToAlpha doesn't accept any matrix.
         if operation_type == OperationType::LuminanceToAlpha {
-            self.matrix = {
+            self.params.matrix = {
                 Matrix5::new(
                     0.0,    0.0,    0.0,    0.0, 0.0,
                     0.0,    0.0,    0.0,    0.0, 0.0,
@@ -139,7 +140,7 @@ impl SetAttributes for FeColorMatrix {
                     }
                 };
 
-                self.matrix = new_matrix;
+                self.params.matrix = new_matrix;
             }
         }
 
@@ -220,14 +221,10 @@ impl FilterEffect for FeColorMatrix {
         let cascaded = CascadedValues::new_from_node(node);
         let values = cascaded.get();
 
-        Ok((
-            self.base.clone(),
-            PrimitiveParams::ColorMatrix(ColorMatrix {
-                in1: self.in1.clone(),
-                matrix: self.matrix,
-                color_interpolation_filters: values.color_interpolation_filters(),
-            }),
-        ))
+        let mut params = self.params.clone();
+        params.color_interpolation_filters = values.color_interpolation_filters();
+
+        Ok((self.base.clone(), PrimitiveParams::ColorMatrix(params)))
     }
 }
 


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