[librsvg: 35/95] Pattern: parse attributes with the PHF



commit 126abb3f0cd4ca1b47cd7155e17b5f560cde126a
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Feb 16 19:09:55 2018 -0600

    Pattern: parse attributes with the PHF

 rsvg-attributes.h   |  3 +++
 rust/build.rs       |  3 +++
 rust/src/pattern.rs | 50 +++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 45 insertions(+), 11 deletions(-)
---
diff --git a/rsvg-attributes.h b/rsvg-attributes.h
index f06b6a7f..85b8bfc7 100644
--- a/rsvg-attributes.h
+++ b/rsvg-attributes.h
@@ -78,6 +78,9 @@ typedef enum {
     RSVG_ATTRIBUTE_ORDER,
     RSVG_ATTRIBUTE_OVERFLOW,
     RSVG_ATTRIBUTE_PARSE,
+    RSVG_ATTRIBUTE_PATTERN_CONTENT_UNITS,
+    RSVG_ATTRIBUTE_PATTERN_TRANSFORM,
+    RSVG_ATTRIBUTE_PATTERN_UNITS,
     RSVG_ATTRIBUTE_POINTS,
     RSVG_ATTRIBUTE_POINTS_AT_X,
     RSVG_ATTRIBUTE_POINTS_AT_Y,
diff --git a/rust/build.rs b/rust/build.rs
index 4fac335d..22594f46 100644
--- a/rust/build.rs
+++ b/rust/build.rs
@@ -85,6 +85,9 @@ fn generate_phf_of_svg_attributes() {
         ( "order",              "Order" ),
         ( "overflow",           "Overflow" ),
         ( "parse",              "Parse" ),
+        ( "patternContentUnits", "PatternContentUnits" ),
+        ( "patternTransform",   "PatternTransform" ),
+        ( "patternUnits",       "PatternUnits" ),
         ( "points",             "Points" ),
         ( "pointsAtX",          "PointsAtX" ),
         ( "pointsAtY",          "PointsAtY" ),
diff --git a/rust/src/pattern.rs b/rust/src/pattern.rs
index 61c9c0ec..c0223e36 100644
--- a/rust/src/pattern.rs
+++ b/rust/src/pattern.rs
@@ -5,11 +5,13 @@ use libc;
 
 use std::cell::RefCell;
 use std::rc::*;
+use std::str::FromStr;
 
 use cairo::MatrixTrait;
 use cairo::Pattern as CairoPattern;
 
 use aspect_ratio::*;
+use attributes::Attribute;
 use bbox::*;
 use coord_units::CoordUnits;
 use drawing_ctx;
@@ -17,7 +19,8 @@ use drawing_ctx::RsvgDrawingCtx;
 use handle::RsvgHandle;
 use length::*;
 use node::*;
-use property_bag::{self, PropertyBag};
+use parsers::parse;
+use property_bag::PropertyBag;
 use util::*;
 use viewbox::*;
 
@@ -174,20 +177,45 @@ impl NodeTrait for NodePattern {
 
         p.node = Some (Rc::downgrade (node));
 
-        p.units         = property_bag::parse_or_none (pbag, "patternUnits", (), None)?;
-        p.content_units = property_bag::parse_or_none (pbag, "patternContentUnits", (), None)?;
-        p.vbox          = property_bag::parse_or_none (pbag, "viewBox", (), None)?.map (Some).or (None);
+        for (key, value) in pbag.iter() {
+            if let Ok(attr) = Attribute::from_str(key) {
+                match attr {
+                    Attribute::PatternUnits =>
+                        p.units = Some(parse("patternUnits", value, (), None)?),
 
-        p.preserve_aspect_ratio = property_bag::parse_or_none (pbag, "preserveAspectRatio", (), None)?;
+                    Attribute::PatternContentUnits =>
+                        p.content_units = Some(parse("patternContentUnits", value, (), None)?),
 
-        p.affine = property_bag::parse_or_none (pbag, "patternTransform", (), None)?;
+                    Attribute::ViewBox =>
+                        p.vbox = Some(Some(parse("viewBox", value, (), None)?)),
 
-        p.fallback = pbag.lookup("xlink:href").map(|s| s.to_owned());
+                    Attribute::PreserveAspectRatio =>
+                        p.preserve_aspect_ratio = Some(parse("preserveAspectRatio", value, (), None)?),
 
-        p.x      = property_bag::parse_or_none (pbag, "x", LengthDir::Horizontal, None)?;
-        p.y      = property_bag::parse_or_none (pbag, "y", LengthDir::Vertical, None)?;
-        p.width  = property_bag::parse_or_none (pbag, "width", LengthDir::Horizontal, None)?;
-        p.height = property_bag::parse_or_none (pbag, "height", LengthDir::Vertical, None)?;
+                    Attribute::PatternTransform =>
+                        p.affine = Some(parse("patternTransform", value, (), None)?),
+
+                    Attribute::XlinkHref =>
+                        p.fallback = Some(value.to_owned()),
+
+                    Attribute::X =>
+                        p.x = Some(parse("x", value, LengthDir::Horizontal, None)?),
+
+                    Attribute::Y =>
+                        p.y = Some(parse("y", value, LengthDir::Vertical, None)?),
+
+                    Attribute::Width =>
+                        p.width = Some(parse("width", value, LengthDir::Horizontal,
+                                             Some(RsvgLength::check_nonnegative))?),
+
+                    Attribute::Height =>
+                        p.height = Some(parse("height", value, LengthDir::Vertical,
+                                              Some(RsvgLength::check_nonnegative))?),
+
+                    _ => (),
+                }
+            }
+        }
 
         Ok (())
     }


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