[librsvg] state: remove the State struct



commit 0b1652214bc66c73d45db262570740cc6e41aad5
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Jan 18 16:47:58 2019 +0100

    state: remove the State struct
    
    Just use SpecifiedValues directly

 rsvg_internals/src/css.rs    |   6 +--
 rsvg_internals/src/marker.rs |   6 +--
 rsvg_internals/src/node.rs   |  89 +++++++++++++++++++-----------
 rsvg_internals/src/state.rs  | 125 ++++++++++++++++++-------------------------
 4 files changed, 116 insertions(+), 110 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index e4a0e03c..48b21ab3 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -14,7 +14,7 @@ use attributes::Attribute;
 use croco::*;
 use error::LoadingError;
 use io::{self, BinaryData};
-use state::State;
+use state::SpecifiedValues;
 use util::utf8_cstr;
 
 struct Declaration {
@@ -130,14 +130,14 @@ impl CssStyles {
     pub fn lookup_apply(
         &self,
         selector: &str,
-        state: &mut State,
+        values: &mut SpecifiedValues,
         important_styles: &mut HashSet<Attribute>,
     ) -> bool {
         if let Some(decl_list) = self.selectors_to_declarations.get(selector) {
             for (prop_name, declaration) in decl_list.iter() {
                 if let Ok(attr) = Attribute::from_str(prop_name) {
                     // FIXME: this is ignoring errors
-                    let _ = state.parse_style_pair(
+                    let _ = values.parse_style_pair(
                         attr,
                         &declaration.prop_value,
                         declaration.important,
diff --git a/rsvg_internals/src/marker.rs b/rsvg_internals/src/marker.rs
index 4a291da2..209ecbac 100644
--- a/rsvg_internals/src/marker.rs
+++ b/rsvg_internals/src/marker.rs
@@ -18,7 +18,7 @@ use node::*;
 use parsers::{Parse, ParseError, ParseValue};
 use path_builder::*;
 use property_bag::PropertyBag;
-use state::{ComputedValues, SpecifiedValue, State};
+use state::{ComputedValues, SpecifiedValue, SpecifiedValues};
 use viewbox::*;
 
 // markerUnits attribute: https://www.w3.org/TR/SVG/painting.html#MarkerElement
@@ -244,9 +244,9 @@ impl NodeTrait for NodeMarker {
         Ok(())
     }
 
-    fn set_overridden_properties(&self, state: &mut State) {
+    fn set_overridden_properties(&self, values: &mut SpecifiedValues) {
         // markers are always displayed, even if <marker> or its ancestors are display:none
-        state.values.display = SpecifiedValue::Specified(Default::default());
+        values.display = SpecifiedValue::Specified(Default::default());
     }
 }
 
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index aa85350d..084b6716 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -11,7 +11,7 @@ use drawing_ctx::DrawingCtx;
 use error::*;
 use parsers::Parse;
 use property_bag::PropertyBag;
-use state::{ComputedValues, Overflow, SpecifiedValue, State};
+use state::{ComputedValues, Overflow, SpecifiedValue, SpecifiedValues};
 use tree_utils;
 
 // A *const RsvgNode is just a pointer for the C code's benefit: it
@@ -71,9 +71,11 @@ impl<'a> CascadedValues<'a> {
     /// This is for the `<use>` element, which draws the element which it references with the
     /// `<use>`'s own cascade, not wih the element's original cascade.
     pub fn new_from_values(node: &'a Node, values: &ComputedValues) -> CascadedValues<'a> {
-        let state = node.data.state.borrow();
         let mut v = values.clone();
-        state.get_specified_values().to_computed_values(&mut v);
+        node.data
+            .specified_values
+            .borrow()
+            .to_computed_values(&mut v);
 
         CascadedValues {
             inner: CascadedInner::FromValues(v),
@@ -100,8 +102,8 @@ pub trait NodeTrait: Downcast {
     fn set_atts(&self, node: &RsvgNode, pbag: &PropertyBag<'_>) -> NodeResult;
 
     /// Sets any special-cased properties that the node may have, that are different
-    /// from defaults in the node's `State`.
-    fn set_overridden_properties(&self, _state: &mut State) {}
+    /// from defaults in the node's `SpecifiedValues`.
+    fn set_overridden_properties(&self, _values: &mut SpecifiedValues) {}
 
     fn draw(
         &self,
@@ -143,7 +145,7 @@ pub struct NodeData {
     node_type: NodeType,
     id: Option<String>,    // id attribute from XML element
     class: Option<String>, // class attribute from XML element
-    state: RefCell<State>,
+    specified_values: RefCell<SpecifiedValues>,
     important_styles: RefCell<HashSet<Attribute>>,
     result: RefCell<NodeResult>,
     transform: Cell<Matrix>,
@@ -283,7 +285,7 @@ impl Node {
             node_type,
             id: id.map(str::to_string),
             class: class.map(str::to_string),
-            state: RefCell::new(State::new()),
+            specified_values: RefCell::new(Default::default()),
             important_styles: Default::default(),
             transform: Cell::new(Matrix::identity()),
             result: RefCell::new(Ok(())),
@@ -331,9 +333,11 @@ impl Node {
     }
 
     pub fn cascade(&self, values: &ComputedValues) {
-        let state = self.data.state.borrow();
         let mut values = values.clone();
-        state.get_specified_values().to_computed_values(&mut values);
+        self.data
+            .specified_values
+            .borrow()
+            .to_computed_values(&mut values);
         *self.data.values.borrow_mut() = values.clone();
 
         for child in self.children() {
@@ -421,8 +425,12 @@ impl Node {
 
     /// Hands the pbag to the node's state, to apply the presentation attributes
     fn set_presentation_attributes(&self, pbag: &PropertyBag<'_>) {
-        let mut state = self.data.state.borrow_mut();
-        match state.parse_presentation_attributes(pbag) {
+        match self
+            .data
+            .specified_values
+            .borrow_mut()
+            .parse_presentation_attributes(pbag)
+        {
             Ok(_) => (),
             Err(e) => {
                 // FIXME: we'll ignore errors here for now.
@@ -454,14 +462,14 @@ impl Node {
         // This is basically a semi-compliant CSS2 selection engine
 
         let element_name = self.get_type().element_name();
-        let mut state = self.data.state.borrow_mut();
+        let mut specified_values = self.data.specified_values.borrow_mut();
         let mut important_styles = self.data.important_styles.borrow_mut();
 
         // *
-        css_styles.lookup_apply("*", &mut state, &mut important_styles);
+        css_styles.lookup_apply("*", &mut specified_values, &mut important_styles);
 
         // tag
-        css_styles.lookup_apply(element_name, &mut state, &mut important_styles);
+        css_styles.lookup_apply(element_name, &mut specified_values, &mut important_styles);
 
         if let Some(klazz) = self.get_class() {
             for cls in klazz.split_whitespace() {
@@ -472,25 +480,41 @@ impl Node {
                     if let Some(id) = self.get_id() {
                         let target = format!("{}.{}#{}", element_name, cls, id);
                         found = found
-                            || css_styles.lookup_apply(&target, &mut state, &mut important_styles);
+                            || css_styles.lookup_apply(
+                                &target,
+                                &mut specified_values,
+                                &mut important_styles,
+                            );
                     }
 
                     // .class#id
                     if let Some(id) = self.get_id() {
                         let target = format!(".{}#{}", cls, id);
                         found = found
-                            || css_styles.lookup_apply(&target, &mut state, &mut important_styles);
+                            || css_styles.lookup_apply(
+                                &target,
+                                &mut specified_values,
+                                &mut important_styles,
+                            );
                     }
 
                     // tag.class
                     let target = format!("{}.{}", element_name, cls);
                     found = found
-                        || css_styles.lookup_apply(&target, &mut state, &mut important_styles);
+                        || css_styles.lookup_apply(
+                            &target,
+                            &mut specified_values,
+                            &mut important_styles,
+                        );
 
                     if !found {
                         // didn't find anything more specific, just apply the class style
                         let target = format!(".{}", cls);
-                        css_styles.lookup_apply(&target, &mut state, &mut important_styles);
+                        css_styles.lookup_apply(
+                            &target,
+                            &mut specified_values,
+                            &mut important_styles,
+                        );
                     }
                 }
             }
@@ -499,11 +523,11 @@ impl Node {
         if let Some(id) = self.get_id() {
             // id
             let target = format!("#{}", id);
-            css_styles.lookup_apply(&target, &mut state, &mut important_styles);
+            css_styles.lookup_apply(&target, &mut specified_values, &mut important_styles);
 
             // tag#id
             let target = format!("{}#{}", element_name, id);
-            css_styles.lookup_apply(&target, &mut state, &mut important_styles);
+            css_styles.lookup_apply(&target, &mut specified_values, &mut important_styles);
         }
     }
 
@@ -514,8 +538,12 @@ impl Node {
         for (attr, value) in pbag.iter() {
             match attr {
                 Attribute::Style => {
-                    let mut state = self.data.state.borrow_mut();
-                    if let Err(e) = state.parse_style_declarations(value, &mut important_styles) {
+                    if let Err(e) = self
+                        .data
+                        .specified_values
+                        .borrow_mut()
+                        .parse_style_declarations(value, &mut important_styles)
+                    {
                         self.set_error(e);
                         break;
                     }
@@ -526,8 +554,8 @@ impl Node {
         }
     }
 
-    // Sets the node's state from the style-related attributes in the pbag.  Also applies
-    // CSS rules in our limited way based on the node's tag/class/id.
+    // Sets the node's specified values from the style-related attributes in the pbag.
+    // Also applies CSS rules in our limited way based on the node's tag/class/id.
     pub fn set_style(&self, css_styles: &CssStyles, pbag: &PropertyBag<'_>) {
         self.set_presentation_attributes(pbag);
         self.set_css_styles(css_styles);
@@ -535,8 +563,10 @@ impl Node {
     }
 
     pub fn set_overridden_properties(&self) {
-        let mut state = self.data.state.borrow_mut();
-        self.data.node_impl.set_overridden_properties(&mut state);
+        let mut specified_values = self.data.specified_values.borrow_mut();
+        self.data
+            .node_impl
+            .set_overridden_properties(&mut specified_values);
     }
 
     pub fn draw(
@@ -615,13 +645,12 @@ impl Node {
     }
 
     pub fn is_overflow(&self) -> bool {
-        let state = self.data.state.borrow();
-        state.get_specified_values().is_overflow()
+        self.data.specified_values.borrow().is_overflow()
     }
 
     pub fn set_overflow_hidden(&self) {
-        let mut state = self.data.state.borrow_mut();
-        state.values.overflow = SpecifiedValue::Specified(Overflow::Hidden);
+        let mut specified_values = self.data.specified_values.borrow_mut();
+        specified_values.overflow = SpecifiedValue::Specified(Overflow::Hidden);
     }
 
     // find the last Chars node so that we can coalesce
diff --git a/rsvg_internals/src/state.rs b/rsvg_internals/src/state.rs
index c9ee9abd..d00e49d9 100644
--- a/rsvg_internals/src/state.rs
+++ b/rsvg_internals/src/state.rs
@@ -65,7 +65,7 @@ where
     }
 }
 
-/// Holds the state of CSS properties
+/// Holds the specified CSS properties
 ///
 /// This is used for various purposes:
 ///
@@ -74,15 +74,6 @@ where
 ///
 /// Each property should have its own data type, and implement
 /// `Default` and `parsers::Parse`.
-///
-/// If a property is `None`, is means it was not specified and must be
-/// inherited from the parent state, or in the end the caller can
-/// `.unwrap_or_default()` to get the default value for the property.
-
-pub struct State {
-    pub values: SpecifiedValues,
-}
-
 #[derive(Default, Clone)]
 pub struct SpecifiedValues {
     pub baseline_shift: SpecifiedValue<BaselineShift>,
@@ -266,14 +257,6 @@ impl SpecifiedValues {
             _ => false,
         }
     }
-}
-
-impl State {
-    pub fn new() -> State {
-        State {
-            values: Default::default(),
-        }
-    }
 
     fn parse_attribute_pair(
         &mut self,
@@ -286,205 +269,203 @@ impl State {
             // please keep these sorted
             match attr {
                 Attribute::BaselineShift => {
-                    self.values.baseline_shift = parse_property(value, ())?;
+                    self.baseline_shift = parse_property(value, ())?;
                 }
 
                 Attribute::ClipPath => {
-                    self.values.clip_path = parse_property(value, ())?;
+                    self.clip_path = parse_property(value, ())?;
                 }
 
                 Attribute::ClipRule => {
-                    self.values.clip_rule = parse_property(value, ())?;
+                    self.clip_rule = parse_property(value, ())?;
                 }
 
                 Attribute::Color => {
-                    self.values.color = parse_property(value, ())?;
+                    self.color = parse_property(value, ())?;
                 }
 
                 Attribute::ColorInterpolationFilters => {
-                    self.values.color_interpolation_filters = parse_property(value, ())?;
+                    self.color_interpolation_filters = parse_property(value, ())?;
                 }
 
                 Attribute::Direction => {
-                    self.values.direction = parse_property(value, ())?;
+                    self.direction = parse_property(value, ())?;
                 }
 
                 Attribute::Display => {
-                    self.values.display = parse_property(value, ())?;
+                    self.display = parse_property(value, ())?;
                 }
 
                 Attribute::EnableBackground => {
-                    self.values.enable_background = parse_property(value, ())?;
+                    self.enable_background = parse_property(value, ())?;
                 }
 
                 Attribute::Fill => {
-                    self.values.fill = parse_property(value, ())?;
+                    self.fill = parse_property(value, ())?;
                 }
 
                 Attribute::FillOpacity => {
-                    self.values.fill_opacity = parse_property(value, ())?;
+                    self.fill_opacity = parse_property(value, ())?;
                 }
 
                 Attribute::FillRule => {
-                    self.values.fill_rule = parse_property(value, ())?;
+                    self.fill_rule = parse_property(value, ())?;
                 }
 
                 Attribute::Filter => {
-                    self.values.filter = parse_property(value, ())?;
+                    self.filter = parse_property(value, ())?;
                 }
 
                 Attribute::FloodColor => {
-                    self.values.flood_color = parse_property(value, ())?;
+                    self.flood_color = parse_property(value, ())?;
                 }
 
                 Attribute::FloodOpacity => {
-                    self.values.flood_opacity = parse_property(value, ())?;
+                    self.flood_opacity = parse_property(value, ())?;
                 }
 
                 Attribute::FontFamily => {
-                    self.values.font_family = parse_property(value, ())?;
+                    self.font_family = parse_property(value, ())?;
                 }
 
                 Attribute::FontSize => {
-                    self.values.font_size = parse_property(value, ())?;
+                    self.font_size = parse_property(value, ())?;
                 }
 
                 Attribute::FontStretch => {
-                    self.values.font_stretch = parse_property(value, ())?;
+                    self.font_stretch = parse_property(value, ())?;
                 }
 
                 Attribute::FontStyle => {
-                    self.values.font_style = parse_property(value, ())?;
+                    self.font_style = parse_property(value, ())?;
                 }
 
                 Attribute::FontVariant => {
-                    self.values.font_variant = parse_property(value, ())?;
+                    self.font_variant = parse_property(value, ())?;
                 }
 
                 Attribute::FontWeight => {
-                    self.values.font_weight = parse_property(value, ())?;
+                    self.font_weight = parse_property(value, ())?;
                 }
 
                 Attribute::LetterSpacing => {
-                    self.values.letter_spacing = parse_property(value, ())?;
+                    self.letter_spacing = parse_property(value, ())?;
                 }
 
                 Attribute::LightingColor => {
-                    self.values.lighting_color = parse_property(value, ())?;
+                    self.lighting_color = parse_property(value, ())?;
                 }
 
                 Attribute::MarkerEnd => {
-                    self.values.marker_end = parse_property(value, ())?;
+                    self.marker_end = parse_property(value, ())?;
                 }
 
                 Attribute::MarkerMid => {
-                    self.values.marker_mid = parse_property(value, ())?;
+                    self.marker_mid = parse_property(value, ())?;
                 }
 
                 Attribute::MarkerStart => {
-                    self.values.marker_start = parse_property(value, ())?;
+                    self.marker_start = parse_property(value, ())?;
                 }
 
                 Attribute::Marker if accept_shorthands => {
-                    self.values.marker_end = parse_property(value, ())?;
-                    self.values.marker_mid = parse_property(value, ())?;
-                    self.values.marker_start = parse_property(value, ())?;
+                    self.marker_end = parse_property(value, ())?;
+                    self.marker_mid = parse_property(value, ())?;
+                    self.marker_start = parse_property(value, ())?;
                 }
 
                 Attribute::Mask => {
-                    self.values.mask = parse_property(value, ())?;
+                    self.mask = parse_property(value, ())?;
                 }
 
                 Attribute::Opacity => {
-                    self.values.opacity = parse_property(value, ())?;
+                    self.opacity = parse_property(value, ())?;
                 }
 
                 Attribute::Overflow => {
-                    self.values.overflow = parse_property(value, ())?;
+                    self.overflow = parse_property(value, ())?;
                 }
 
                 Attribute::ShapeRendering => {
-                    self.values.shape_rendering = parse_property(value, ())?;
+                    self.shape_rendering = parse_property(value, ())?;
                 }
 
                 Attribute::StopColor => {
-                    self.values.stop_color = parse_property(value, ())?;
+                    self.stop_color = parse_property(value, ())?;
                 }
 
                 Attribute::StopOpacity => {
-                    self.values.stop_opacity = parse_property(value, ())?;
+                    self.stop_opacity = parse_property(value, ())?;
                 }
 
                 Attribute::Stroke => {
-                    self.values.stroke = parse_property(value, ())?;
+                    self.stroke = parse_property(value, ())?;
                 }
 
                 Attribute::StrokeDasharray => {
-                    self.values.stroke_dasharray = parse_property(value, ())?;
+                    self.stroke_dasharray = parse_property(value, ())?;
                 }
 
                 Attribute::StrokeDashoffset => {
-                    self.values.stroke_dashoffset = parse_property(value, LengthDir::Both)?;
+                    self.stroke_dashoffset = parse_property(value, LengthDir::Both)?;
                 }
 
                 Attribute::StrokeLinecap => {
-                    self.values.stroke_line_cap = parse_property(value, ())?;
+                    self.stroke_line_cap = parse_property(value, ())?;
                 }
 
                 Attribute::StrokeLinejoin => {
-                    self.values.stroke_line_join = parse_property(value, ())?;
+                    self.stroke_line_join = parse_property(value, ())?;
                 }
 
                 Attribute::StrokeOpacity => {
-                    self.values.stroke_opacity = parse_property(value, ())?;
+                    self.stroke_opacity = parse_property(value, ())?;
                 }
 
                 Attribute::StrokeMiterlimit => {
-                    self.values.stroke_miterlimit = parse_property(value, ())?;
+                    self.stroke_miterlimit = parse_property(value, ())?;
                 }
 
                 Attribute::StrokeWidth => {
-                    self.values.stroke_width = parse_property(value, LengthDir::Both)?;
+                    self.stroke_width = parse_property(value, LengthDir::Both)?;
                 }
 
                 Attribute::TextAnchor => {
-                    self.values.text_anchor = parse_property(value, ())?;
+                    self.text_anchor = parse_property(value, ())?;
                 }
 
                 Attribute::TextDecoration => {
-                    self.values.text_decoration = parse_property(value, ())?;
+                    self.text_decoration = parse_property(value, ())?;
                 }
 
                 Attribute::TextRendering => {
-                    self.values.text_rendering = parse_property(value, ())?;
+                    self.text_rendering = parse_property(value, ())?;
                 }
 
                 Attribute::UnicodeBidi => {
-                    self.values.unicode_bidi = parse_property(value, ())?;
+                    self.unicode_bidi = parse_property(value, ())?;
                 }
 
                 Attribute::Visibility => {
-                    self.values.visibility = parse_property(value, ())?;
+                    self.visibility = parse_property(value, ())?;
                 }
 
                 Attribute::WritingMode => {
-                    self.values.writing_mode = parse_property(value, ())?;
+                    self.writing_mode = parse_property(value, ())?;
                 }
 
                 Attribute::XmlLang => {
                     // xml:lang is not a property; it is a non-presentation attribute and as such
                     // cannot have the "inherit" value.  So, we don't call parse_property() for it,
                     // but rather call its parser directly.
-                    self.values.xml_lang =
-                        SpecifiedValue::Specified(XmlLang::parse_str(value, ())?);
+                    self.xml_lang = SpecifiedValue::Specified(XmlLang::parse_str(value, ())?);
                 }
 
                 Attribute::XmlSpace => {
                     // xml:space is not a property; it is a non-presentation attribute and as such
                     // cannot have the "inherit" value.  So, we don't call parse_property() for it,
                     // but rather call its parser directly.
-                    self.values.xml_space =
-                        SpecifiedValue::Specified(XmlSpace::parse_str(value, ())?);
+                    self.xml_space = SpecifiedValue::Specified(XmlSpace::parse_str(value, ())?);
                 }
 
                 _ => {
@@ -588,10 +569,6 @@ impl State {
 
         Ok(())
     }
-
-    pub fn get_specified_values(&self) -> &SpecifiedValues {
-        &self.values
-    }
 }
 
 // Parses the `value` for the type `T` of the property, including `inherit` values.


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