[librsvg: 9/11] Normalize paint sources before setting them on the cr




commit 7965b71b9343dfc9104223859e3b6b3cd9ff19fb
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Dec 1 11:26:07 2020 -0600

    Normalize paint sources before setting them on the cr
    
    This introduces UserSpacePaintSource, analogous to gradients/patterns.

 src/drawing_ctx.rs  | 34 ++++++++++------------------------
 src/paint_server.rs | 40 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 48 insertions(+), 26 deletions(-)
---
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 81a9f9e1..f9fdfa50 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -23,7 +23,7 @@ use crate::float_eq_cairo::ApproxEqCairo;
 use crate::gradient::{GradientVariant, SpreadMethod, UserSpaceGradient};
 use crate::marker;
 use crate::node::{CascadedValues, Node, NodeBorrow, NodeDraw};
-use crate::paint_server::{PaintServer, PaintSource};
+use crate::paint_server::{PaintServer, UserSpacePaintSource};
 use crate::path_builder::*;
 use crate::pattern::{PatternContentUnits, PatternUnits, UserSpacePattern};
 use crate::properties::ComputedValues;
@@ -1143,19 +1143,13 @@ impl DrawingCtx {
         current_color: cssparser::RGBA,
         values: &ComputedValues,
     ) -> Result<bool, RenderingError> {
-        let paint_source = paint_server.resolve(acquired_nodes)?;
+        let paint_source = paint_server
+            .resolve(acquired_nodes)?
+            .to_user_space(bbox, self, values);
 
         match paint_source {
-            PaintSource::Gradient(g, c) => {
-                let had_gradient;
-
-                if let Some(gradient) = g.to_user_space(bbox, self, values) {
-                    had_gradient = self.set_gradient(&gradient, opacity)?;
-                } else {
-                    had_gradient = false;
-                }
-
-                if had_gradient {
+            UserSpacePaintSource::Gradient(gradient, c) => {
+                if self.set_gradient(&gradient, opacity)? {
                     Ok(true)
                 } else if let Some(c) = c {
                     self.set_color(c, opacity, current_color)
@@ -1163,16 +1157,8 @@ impl DrawingCtx {
                     Ok(false)
                 }
             }
-            PaintSource::Pattern(p, c) => {
-                let had_pattern;
-
-                if let Some(pattern) = p.to_user_space(bbox, self, values) {
-                    had_pattern = self.set_pattern(&pattern, acquired_nodes, opacity)?;
-                } else {
-                    had_pattern = false;
-                }
-
-                if had_pattern {
+            UserSpacePaintSource::Pattern(pattern, c) => {
+                if self.set_pattern(&pattern, acquired_nodes, opacity)? {
                     Ok(true)
                 } else if let Some(c) = c {
                     self.set_color(c, opacity, current_color)
@@ -1180,8 +1166,8 @@ impl DrawingCtx {
                     Ok(false)
                 }
             }
-            PaintSource::SolidColor(c) => self.set_color(c, opacity, current_color),
-            PaintSource::None => Ok(false),
+            UserSpacePaintSource::SolidColor(c) => self.set_color(c, opacity, current_color),
+            UserSpacePaintSource::None => Ok(false),
         }
     }
 
diff --git a/src/paint_server.rs b/src/paint_server.rs
index 06fdfd15..974477f7 100644
--- a/src/paint_server.rs
+++ b/src/paint_server.rs
@@ -2,13 +2,16 @@
 
 use cssparser::Parser;
 
+use crate::bbox::BoundingBox;
 use crate::document::AcquiredNodes;
+use crate::drawing_ctx::DrawingCtx;
 use crate::element::Element;
 use crate::error::*;
-use crate::gradient::ResolvedGradient;
+use crate::gradient::{ResolvedGradient, UserSpaceGradient};
 use crate::node::NodeBorrow;
 use crate::parsers::Parse;
-use crate::pattern::ResolvedPattern;
+use crate::pattern::{ResolvedPattern, UserSpacePattern};
+use crate::properties::ComputedValues;
 use crate::url_resolver::Fragment;
 
 #[derive(Debug, Clone, PartialEq)]
@@ -28,6 +31,13 @@ pub enum PaintSource {
     SolidColor(cssparser::Color),
 }
 
+pub enum UserSpacePaintSource {
+    None,
+    Gradient(UserSpaceGradient, Option<cssparser::Color>),
+    Pattern(UserSpacePattern, Option<cssparser::Color>),
+    SolidColor(cssparser::Color),
+}
+
 impl Parse for PaintServer {
     fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<PaintServer, ParseError<'i>> {
         if parser
@@ -128,6 +138,32 @@ impl PaintServer {
     }
 }
 
+impl PaintSource {
+    pub fn to_user_space(
+        &self,
+        bbox: &BoundingBox,
+        draw_ctx: &DrawingCtx,
+        values: &ComputedValues,
+    ) -> UserSpacePaintSource {
+        match *self {
+            PaintSource::None => UserSpacePaintSource::None,
+            PaintSource::SolidColor(c) => UserSpacePaintSource::SolidColor(c),
+
+            PaintSource::Gradient(ref g, c) => match (g.to_user_space(bbox, draw_ctx, values), c) {
+                (Some(gradient), c) => UserSpacePaintSource::Gradient(gradient, c),
+                (None, Some(c)) => UserSpacePaintSource::SolidColor(c),
+                (None, None) => UserSpacePaintSource::None,
+            },
+
+            PaintSource::Pattern(ref p, c) => match (p.to_user_space(bbox, draw_ctx, values), c) {
+                (Some(pattern), c) => UserSpacePaintSource::Pattern(pattern, c),
+                (None, Some(c)) => UserSpacePaintSource::SolidColor(c),
+                (None, None) => UserSpacePaintSource::None,
+            },
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;


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