[librsvg: 9/12] state: port flood color to rust



commit 762c6d5539a382e856bf22bef1bdfcc70218464b
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Apr 23 22:58:45 2018 +0200

    state: port flood color to rust

 librsvg/rsvg-styles.c       | 40 ++--------------------------------------
 librsvg/rsvg-styles.h       |  3 ---
 rsvg_internals/src/color.rs | 19 +++++++++++++++++--
 rsvg_internals/src/lib.rs   |  1 +
 rsvg_internals/src/state.rs | 30 +++++++++++++++++++++++++++++-
 5 files changed, 49 insertions(+), 44 deletions(-)
---
diff --git a/librsvg/rsvg-styles.c b/librsvg/rsvg-styles.c
index ff017a83..0f4e3514 100644
--- a/librsvg/rsvg-styles.c
+++ b/librsvg/rsvg-styles.c
@@ -45,6 +45,7 @@ extern State *rsvg_state_rust_clone(State *state);
 extern cairo_matrix_t rsvg_state_rust_get_affine(const State *state);
 extern void rsvg_state_rust_set_affine(State *state, cairo_matrix_t affine);
 extern cairo_operator_t rsvg_state_rust_get_comp_op(const State *state);
+extern guint32 rsvg_state_rust_get_flood_color(const State *state);
 extern guint8 rsvg_state_rust_get_flood_opacity(const State *state);
 extern RsvgEnableBackgroundType rsvg_state_rust_get_enable_background(const State *state);
 extern char *rsvg_state_rust_get_clip_path(const State *state);
@@ -112,10 +113,7 @@ rsvg_state_init (RsvgState * state)
     state->stop_color.kind = RSVG_CSS_COLOR_SPEC_INHERIT;
     state->stop_opacity.kind = RSVG_OPACITY_INHERIT;
 
-    state->flood_color = 0;
-
     state->has_current_color = FALSE;
-    state->has_flood_color = FALSE;
     state->has_fill_server = FALSE;
     state->has_stroke_server = FALSE;
     state->has_stop_color = FALSE;
@@ -209,8 +207,6 @@ rsvg_state_inherit_run (RsvgState * dst, const RsvgState * src,
 {
     if (function (dst->has_current_color, src->has_current_color))
         dst->current_color = src->current_color;
-    if (function (dst->has_flood_color, src->has_flood_color))
-        dst->flood_color = src->flood_color;
     if (function (dst->has_fill_server, src->has_fill_server)) {
         rsvg_paint_server_ref (src->fill);
         if (dst->fill)
@@ -393,38 +389,6 @@ rsvg_parse_style_pair (RsvgState *state,
     }
     break;
 
-    case RSVG_ATTRIBUTE_FLOOD_COLOR:
-    {
-        RsvgCssColorSpec spec;
-
-        spec = rsvg_css_parse_color (value, ALLOW_INHERIT_YES, ALLOW_CURRENT_COLOR_YES);
-        switch (spec.kind) {
-        case RSVG_CSS_COLOR_SPEC_INHERIT:
-            /* FIXME: we should inherit; see how stop-color is handled in rsvg-styles.c */
-            state->has_current_color = FALSE;
-            break;
-
-        case RSVG_CSS_COLOR_SPEC_CURRENT_COLOR:
-            /* FIXME: in the caller, fix up the current color */
-            state->has_flood_color = FALSE;
-            break;
-
-        case RSVG_CSS_COLOR_SPEC_ARGB:
-            state->flood_color = spec.argb;
-            state->has_flood_color = TRUE;
-            break;
-
-        case RSVG_CSS_COLOR_PARSE_ERROR:
-            /* FIXME: no error handling */
-            state->has_current_color = FALSE;
-            break;
-
-        default:
-            g_assert_not_reached ();
-        }
-    }
-    break;
-
     case RSVG_ATTRIBUTE_FILL:
     {
         RsvgPaintServer *fill = state->fill;
@@ -1078,7 +1042,7 @@ rsvg_state_get_fill (RsvgState *state)
 guint32
 rsvg_state_get_flood_color (RsvgState *state)
 {
-    return state->flood_color;
+    return rsvg_state_rust_get_flood_color (state->state_rust);
 }
 
 guint8
diff --git a/librsvg/rsvg-styles.h b/librsvg/rsvg-styles.h
index 81e89eb2..b70f9ed1 100644
--- a/librsvg/rsvg-styles.h
+++ b/librsvg/rsvg-styles.h
@@ -65,9 +65,6 @@ struct _RsvgState {
     guint32 current_color;
     gboolean has_current_color;
 
-    guint32 flood_color;
-    gboolean has_flood_color;
-
     GHashTable *styles;
 
     State *state_rust;
diff --git a/rsvg_internals/src/color.rs b/rsvg_internals/src/color.rs
index bbed55fe..a272114a 100644
--- a/rsvg_internals/src/color.rs
+++ b/rsvg_internals/src/color.rs
@@ -6,6 +6,17 @@ use parsers::Parse;
 use parsers::ParseError;
 use util::utf8_cstr;
 
+impl Parse for cssparser::Color {
+    type Data = ();
+    type Err = AttributeError;
+
+    fn parse(s: &str, _: Self::Data) -> Result<cssparser::Color, AttributeError> {
+        let mut input = cssparser::ParserInput::new(s);
+        cssparser::Color::parse(&mut cssparser::Parser::new(&mut input))
+            .map_err(|_| AttributeError::Parse(ParseError::new("invalid syntax for color")))
+    }
+}
+
 // There are two quirks here:
 //
 // First, we need to expose the Color algebraic type *and* a parse
@@ -135,6 +146,11 @@ fn rgba_from_argb(argb: u32) -> cssparser::RGBA {
     )
 }
 
+pub fn rgba_to_argb(rgba: cssparser::RGBA) -> u32 {
+    u32::from(rgba.alpha) << 24 | u32::from(rgba.red) << 16 | u32::from(rgba.green) << 8
+        | u32::from(rgba.blue)
+}
+
 impl From<cssparser::Color> for Color {
     fn from(c: cssparser::Color) -> Color {
         match c {
@@ -165,8 +181,7 @@ impl From<Result<Color, AttributeError>> for ColorSpec {
 
             Ok(Color::RGBA(rgba)) => ColorSpec {
                 kind: ColorKind::ARGB,
-                argb: (u32::from(rgba.alpha) << 24 | u32::from(rgba.red) << 16
-                    | u32::from(rgba.green) << 8 | u32::from(rgba.blue)),
+                argb: rgba_to_argb(rgba),
             },
 
             _ => ColorSpec {
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index d3feab2f..60820e70 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -121,6 +121,7 @@ pub use state::{
     rsvg_state_rust_get_affine,
     rsvg_state_rust_get_clip_path,
     rsvg_state_rust_get_comp_op,
+    rsvg_state_rust_get_flood_color,
     rsvg_state_rust_get_flood_opacity,
     rsvg_state_rust_get_enable_background,
     rsvg_state_rust_get_filter,
diff --git a/rsvg_internals/src/state.rs b/rsvg_internals/src/state.rs
index 542f2be5..707e3cfe 100644
--- a/rsvg_internals/src/state.rs
+++ b/rsvg_internals/src/state.rs
@@ -1,4 +1,5 @@
 use cairo::{self, MatrixTrait};
+use cssparser;
 use glib;
 use glib::translate::*;
 use glib_sys;
@@ -8,7 +9,7 @@ use std::collections::HashSet;
 use std::ptr;
 
 use attributes::Attribute;
-use color::{Color, ColorSpec};
+use color::{rgba_to_argb, Color, ColorSpec};
 use cond::{RequiredExtensions, RequiredFeatures, SystemLanguage};
 use error::*;
 use iri::IRI;
@@ -50,6 +51,7 @@ pub struct State {
     pub fill_opacity: Option<FillOpacity>,
     pub fill_rule: Option<FillRule>,
     pub filter: Option<Filter>,
+    pub flood_color: Option<FloodColor>,
     pub flood_opacity: Option<FloodOpacity>,
     pub font_family: Option<FontFamily>,
     pub font_size: Option<FontSize>,
@@ -100,6 +102,7 @@ impl State {
             fill_opacity: Default::default(),
             fill_rule: Default::default(),
             filter: Default::default(),
+            flood_color: Default::default(),
             flood_opacity: Default::default(),
             font_family: Default::default(),
             font_size: Default::default(),
@@ -183,6 +186,10 @@ impl State {
                 self.filter = parse_property(value, ())?;
             }
 
+            Attribute::FloodColor => {
+                self.flood_color = parse_property(value, ())?;
+            }
+
             Attribute::FloodOpacity => {
                 self.flood_opacity = parse_property(value, ())?;
             }
@@ -717,6 +724,14 @@ make_property!(
     parse_data_type: ()
 );
 
+make_property!(
+    FloodColor,
+    default: cssparser::Color::RGBA(cssparser::RGBA::new(0, 0, 0, 0)),
+    inherits_automatically: true,
+    newtype_parse: cssparser::Color,
+    parse_data_type: ()
+);
+
 make_property!(
     FloodOpacity,
     default: Opacity::Specified(1.0),
@@ -1152,6 +1167,7 @@ pub extern "C" fn rsvg_state_rust_inherit_run(
     inherit(inherit_fn, &mut dst.display, &src.display);
     inherit(inherit_fn, &mut dst.fill_opacity, &src.fill_opacity);
     inherit(inherit_fn, &mut dst.fill_rule, &src.fill_rule);
+    inherit(inherit_fn, &mut dst.flood_color, &src.flood_color);
     inherit(inherit_fn, &mut dst.flood_opacity, &src.flood_opacity);
     inherit(inherit_fn, &mut dst.font_family, &src.font_family);
     inherit(inherit_fn, &mut dst.font_size, &src.font_size);
@@ -1223,6 +1239,18 @@ pub extern "C" fn rsvg_state_rust_get_comp_op(state: *const State) -> cairo::Ope
     }
 }
 
+#[no_mangle]
+pub extern "C" fn rsvg_state_rust_get_flood_color(state: *const State) -> u32 {
+    unsafe {
+        let state = &*state;
+        match state.flood_color {
+            Some(FloodColor(cssparser::Color::RGBA(rgba))) => rgba_to_argb(rgba),
+            // FIXME: fallback to current color if Color::inherit and current color is set
+            _ => 0xff000000,
+        }
+    }
+}
+
 #[no_mangle]
 pub extern "C" fn rsvg_state_rust_get_flood_opacity(state: *const State) -> u8 {
     unsafe {


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