[librsvg/rustification] path_parser.rs: Start a parser for path data in Rust



commit be7cfed03a6dbef723daf80bbcb62735e6cdbde1
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Nov 3 17:59:16 2016 -0600

    path_parser.rs: Start a parser for path data in Rust
    
    We can port the old C version, but it doesn't handle the full BNF for
    path data as specified in the SVG spec.  So, we'll write a new parser.

 rust/src/lib.rs         |    1 +
 rust/src/path_parser.rs |   62 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index ab7c4aa..8b359fe 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -14,4 +14,5 @@ pub use marker::{
 };
 
 mod path_builder;
+mod path_parser;
 mod marker;
diff --git a/rust/src/path_parser.rs b/rust/src/path_parser.rs
new file mode 100644
index 0000000..20e00f7
--- /dev/null
+++ b/rust/src/path_parser.rs
@@ -0,0 +1,62 @@
+use path_builder::*;
+
+extern crate cairo;
+
+
+fn parse_path (path_str: &str) -> RsvgPathBuilder {
+    let builder = RsvgPathBuilder::new ();
+
+    builder
+}
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use path_builder::*;
+    extern crate cairo;
+
+    fn path_segment_vectors_are_equal (a: &Vec<cairo::PathSegment>,
+                                       b: &Vec<cairo::PathSegment>) -> bool {
+        if a.len () == 0 && b.len () == 0 {
+            return true;
+        }
+
+        let mut iter = a.iter().zip (b);
+
+        loop {
+            if let Some ((seg1, seg2)) = iter.next () {
+                match *seg1 {
+                    cairo::PathSegment::MoveTo ((x, y)) => {
+                        if let cairo::PathSegment::MoveTo ((ox, oy)) = *seg2 { return (x, y) == (ox, oy); }
+                    },
+
+                    cairo::PathSegment::LineTo ((x, y)) => {
+                        if let cairo::PathSegment::LineTo ((ox, oy)) = *seg2 { return (x, y) == (ox, oy); }
+                    },
+
+                    cairo::PathSegment::CurveTo ((x2, y2), (x3, y3), (x4, y4)) => {
+                        if let cairo::PathSegment::CurveTo ((ox2, oy2), (ox3, oy3), (ox4, oy4)) = *seg2 {
+                            return (ox2, oy2, ox3, oy3, ox4, oy4) == (x2, y2, x3, y3, x4, y4);
+                        }
+                    },
+
+                    cairo::PathSegment::ClosePath => {
+                        if let cairo::PathSegment::ClosePath = *seg2 { return true; }
+                    }
+                }
+            } else {
+                return false;
+            }
+        }
+    }
+
+    #[test]
+    fn path_parser_handles_empty_data () {
+        let builder = super::parse_path ("");
+        let segments = builder.get_path_segments ();
+        let expected_segments = Vec::<cairo::PathSegment>::new ();
+
+        assert! (path_segment_vectors_are_equal (&expected_segments, segments));
+    }
+}


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