[librsvg: 24/95] NodeSvg: parse attributes with the PHF



commit 47889ece0ef9e64fd92eeaee46a5514ef0d3200a
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Feb 16 10:26:11 2018 -0600

    NodeSvg: parse attributes with the PHF

 rsvg-attributes.h     |  2 ++
 rust/build.rs         |  2 ++
 rust/src/structure.rs | 47 +++++++++++++++++++++++++++++------------------
 3 files changed, 33 insertions(+), 18 deletions(-)
---
diff --git a/rsvg-attributes.h b/rsvg-attributes.h
index 87c92cf3..b3fe2335 100644
--- a/rsvg-attributes.h
+++ b/rsvg-attributes.h
@@ -79,6 +79,7 @@ typedef enum {
        RSVG_ATTRIBUTE_POINTS_AT_Y,
        RSVG_ATTRIBUTE_POINTS_AT_Z,
        RSVG_ATTRIBUTE_PRESERVE_ALPHA,
+    RSVG_ATTRIBUTE_PRESERVE_ASPECT_RATIO,
        RSVG_ATTRIBUTE_PRIMITIVE_UNITS,
     RSVG_ATTRIBUTE_R,
        RSVG_ATTRIBUTE_RADIUS,
@@ -119,6 +120,7 @@ typedef enum {
        RSVG_ATTRIBUTE_UNICODE_BIDI,
        RSVG_ATTRIBUTE_VALUES,
        RSVG_ATTRIBUTE_VERTS,
+    RSVG_ATTRIBUTE_VIEW_BOX,
        RSVG_ATTRIBUTE_VISIBILITY,
        RSVG_ATTRIBUTE_WIDTH,
        RSVG_ATTRIBUTE_WRITING_MODE,
diff --git a/rust/build.rs b/rust/build.rs
index 899cacdc..186c109b 100644
--- a/rust/build.rs
+++ b/rust/build.rs
@@ -86,6 +86,7 @@ fn generate_phf_of_svg_attributes() {
         ( "pointsAtY",          "PointsAtY" ),
         ( "pointsAtZ",          "PointsAtZ" ),
         ( "preserveAlpha",      "PreserveAlpha" ),
+        ( "preserveAspectRatio", "PreserveAspectRatio" ),
         ( "primitiveUnits",     "PrimitiveUnits" ),
         ( "r",                  "R" ),
         ( "radius",             "Radius" ),
@@ -126,6 +127,7 @@ fn generate_phf_of_svg_attributes() {
         ( "unicode-bidi",       "UnicodeBidi" ),
         ( "values",             "Values" ),
         ( "verts",              "Verts" ),
+        ( "viewBox",            "ViewBox" ),
         ( "visibility",         "Visibility" ),
         ( "width",              "Width" ),
         ( "writing-mode",       "WritingMode" ),
diff --git a/rust/src/structure.rs b/rust/src/structure.rs
index bceb8833..85969d79 100644
--- a/rust/src/structure.rs
+++ b/rust/src/structure.rs
@@ -3,16 +3,18 @@ use libc;
 
 use std::cell::RefCell;
 use std::cell::Cell;
+use std::str::FromStr;
 
 use cairo::MatrixTrait;
 
 use aspect_ratio::*;
+use attributes::Attribute;
 use drawing_ctx::RsvgDrawingCtx;
 use drawing_ctx;
 use handle::RsvgHandle;
 use length::*;
 use node::*;
-use parsers::Parse;
+use parsers::{Parse, parse};
 use property_bag::{self, OwnedPropertyBag, PropertyBag};
 use util::*;
 use viewbox::*;
@@ -136,28 +138,37 @@ impl NodeSvg {
 
 impl NodeTrait for NodeSvg {
     fn set_atts (&self, node: &RsvgNode, _: *const RsvgHandle, pbag: &PropertyBag) -> NodeResult {
-        self.preserve_aspect_ratio.set (property_bag::parse_or_default (pbag, "preserveAspectRatio", (), 
None)?);
-
         // x & y attributes have no effect on outermost svg
         // http://www.w3.org/TR/SVG/struct.html#SVGElement
-        if node.get_parent ().is_some () {
-            self.x.set (property_bag::parse_or_default (pbag, "x", LengthDir::Horizontal, None)?);
-            self.y.set (property_bag::parse_or_default (pbag, "y", LengthDir::Vertical, None)?);
-        }
+        let is_inner_svg = node.get_parent().is_some();
 
-        self.w.set (property_bag::parse_or_value (pbag,
-                                                  "width",
-                                                  LengthDir::Horizontal,
-                                                  RsvgLength::parse ("100%", LengthDir::Horizontal).unwrap 
(),
-                                                  Some(RsvgLength::check_nonnegative))?);
+        for (key, value) in pbag.iter() {
+            if let Ok(attr) = Attribute::from_str(key) {
+                match attr {
+                    Attribute::PreserveAspectRatio =>
+                        self.preserve_aspect_ratio.set(parse("preserveAspectRatio", value, (), None)?),
 
-        self.h.set (property_bag::parse_or_value (pbag,
-                                                  "height",
-                                                  LengthDir::Vertical,
-                                                  RsvgLength::parse ("100%", LengthDir::Vertical).unwrap (),
-                                                  Some(RsvgLength::check_nonnegative))?);
+                    Attribute::X => if is_inner_svg {
+                        self.x.set(parse("x", value, LengthDir::Horizontal, None)?);
+                    },
 
-        self.vbox.set (property_bag::parse_or_none (pbag, "viewBox", (), None)?);
+                    Attribute::Y => if is_inner_svg {
+                        self.y.set(parse("y", value, LengthDir::Vertical, None)?);
+                    },
+
+                    Attribute::Width => self.w.set(parse("width", value, LengthDir::Horizontal,
+                                                         Some(RsvgLength::check_nonnegative))?),
+
+                    Attribute::Height => self.h.set(parse("height", value, LengthDir::Vertical,
+                                                          Some(RsvgLength::check_nonnegative))?),
+
+                    Attribute::ViewBox => self.vbox.set(parse("viewBox", value, (), None)
+                                                        .map(Some)?),
+
+                    _ => (),
+                }
+            }
+        }
 
         // The "style" sub-element is not loaded yet here, so we need
         // to store other attributes to be applied later.


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