[librsvg] Start using the servo/rust-cssparser crate to parse colors



commit a8f2ef109c71cdbec14b05dc07d1734866a93f8e
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri May 19 18:56:16 2017 -0500

    Start using the servo/rust-cssparser crate to parse colors
    
    This is not wired to the real librsvg code yet; it's just a test of
    using that crate.

 rust/Cargo.lock   |  124 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 rust/Cargo.toml   |    5 ++
 rust/src/color.rs |   69 ++++++++---------------------
 rust/src/lib.rs   |    2 +
 4 files changed, 150 insertions(+), 50 deletions(-)
---
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index d8f2c30..eab69af 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -5,6 +5,7 @@ dependencies = [
  "bitflags 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "cairo-rs 0.1.1 (git+https://github.com/federicomenaquintero/cairo.git)",
  "cairo-sys-rs 0.3.2 (git+https://github.com/federicomenaquintero/cairo.git)",
+ "cssparser 0.13.5 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
  "downcast-rs 1.0.0 (git+https://github.com/marcianx/downcast-rs)",
  "glib 0.1.1 (git+https://github.com/gtk-rs/glib)",
  "glib-sys 0.3.2 (git+https://github.com/gtk-rs/sys)",
@@ -97,6 +98,30 @@ dependencies = [
 ]
 
 [[package]]
+name = "cssparser"
+version = "0.13.5"
+source = "git+https://github.com/servo/rust-cssparser?tag=v0.13.5#fc0bdcd1845e3aa616c3bdd15127aed259b87cb8";
+dependencies = [
+ "cssparser-macros 0.3.0 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
+ "matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
+ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "cssparser-macros"
+version = "0.3.0"
+source = "git+https://github.com/servo/rust-cssparser?tag=v0.13.5#fc0bdcd1845e3aa616c3bdd15127aed259b87cb8";
+dependencies = [
+ "phf_codegen 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
+ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "diff"
 version = "0.1.10"
 source = "registry+https://github.com/rust-lang/crates.io-index";
@@ -232,6 +257,11 @@ version = "0.2.21"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
 [[package]]
+name = "matches"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
 name = "memchr"
 version = "0.1.11"
 source = "registry+https://github.com/rust-lang/crates.io-index";
@@ -261,11 +291,63 @@ dependencies = [
 ]
 
 [[package]]
+name = "phf"
+version = "0.7.21"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "phf_codegen"
+version = "0.7.21"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "phf_generator 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "phf_generator"
+version = "0.7.21"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "phf_shared"
+version = "0.7.21"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "pkg-config"
 version = "0.3.9"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
 [[package]]
+name = "procedural-masquerade"
+version = "0.1.2"
+source = "git+https://github.com/servo/rust-cssparser?tag=v0.13.5#fc0bdcd1845e3aa616c3bdd15127aed259b87cb8";
+
+[[package]]
+name = "quote"
+version = "0.3.15"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
+name = "rand"
+version = "0.3.15"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "regex"
 version = "0.1.80"
 source = "registry+https://github.com/rust-lang/crates.io-index";
@@ -310,11 +392,34 @@ version = "0.3.23"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
 [[package]]
+name = "siphasher"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
 name = "strsim"
 version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
 [[package]]
+name = "syn"
+version = "0.11.11"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
+ "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "synom"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "term"
 version = "0.4.5"
 source = "registry+https://github.com/rust-lang/crates.io-index";
@@ -364,6 +469,11 @@ version = "0.0.2"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
 [[package]]
+name = "unicode-xid"
+version = "0.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
 name = "unreachable"
 version = "0.1.1"
 source = "registry+https://github.com/rust-lang/crates.io-index";
@@ -408,6 +518,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum c_vec 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"0059f5a658f62a4bd3937a7addc52ccfda144b75cce7a92b187e528629cdc507"
 "checksum cairo-rs 0.1.1 (git+https://github.com/federicomenaquintero/cairo.git)" = "<none>"
 "checksum cairo-sys-rs 0.3.2 (git+https://github.com/federicomenaquintero/cairo.git)" = "<none>"
+"checksum cssparser 0.13.5 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)" = "<none>"
+"checksum cssparser-macros 0.3.0 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)" = "<none>"
 "checksum diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = 
"0a515461b6c8c08419850ced27bc29e86166dcdcde8fbe76f8b1f0589bb49472"
 "checksum docopt 0.6.86 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4a7ef30445607f6fc8720f0a0a2c7442284b629cf0d049286860fae23e71c4d9"
 "checksum downcast-rs 1.0.0 (git+https://github.com/marcianx/downcast-rs)" = "<none>"
@@ -423,24 +535,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum lalrpop-util 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"36497edf44be49f4663ebd9cfb154a81c84491986a62773c62624911efd3d84d"
 "checksum lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7291b1dd97d331f752620b02dfdbc231df7fc01bf282a00769e1cdb963c460dc"
 "checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135"
+"checksum matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"efd7622e3022e1a6eaa602c4cea8912254e5582c9c692e9167714182244801b1"
 "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
 "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"
 "checksum nom 2.1.0 (git+https://github.com/federicomenaquintero/nom.git)" = "<none>"
 "checksum petgraph 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = 
"bfd1de18b0a5f1777162e5b61aaf498032467d5409ab4ca6dbd03049f5708de1"
+"checksum phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"cb325642290f28ee14d8c6201159949a872f220c62af6e110a56ea914fbe42fc"
+"checksum phf_codegen 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d62594c0bb54c464f633175d502038177e90309daf2e0158be42ed5f023ce88f"
+"checksum phf_generator 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"6b07ffcc532ccc85e3afc45865469bf5d9e4ef5bfcf9622e3cfe80c2d275ec03"
+"checksum phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"07e24b0ca9643bdecd0632f2b3da6b1b89bbb0030e0b992afc1113b23a7bc2f2"
 "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
+"checksum procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)" = "<none>"
+"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
+"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = 
"022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
 "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
 "checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01"
 "checksum regex-syntax 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a21935ce5a4dfa48e3ded1aefbbe353fb9ab258b0d3fa0bd168bef00797b3dc7"
 "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
 "checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457"
 "checksum rustc-serialize 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = 
"684ce48436d6465300c9ea783b6b14c4361d6b8dcbb1375b486a69cc19e2dfb0"
+"checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537"
 "checksum strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"67f84c44fbb2f91db7fef94554e6b2ac05909c9c0b0bc23bb98d3a1aebfe7f7c"
+"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
+"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
 "checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989"
 "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
 "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"
 "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = 
"8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
 "checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"
 "checksum unicode-xid 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f69506a2561962651710609304bbb961fa3da598c812f877975a82e48ee144f9"
+"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
 "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"
 "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
 "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 8a548f3..17c2976 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -8,6 +8,11 @@ build = "build.rs"
 libc = "0.2"
 bitflags = ""
 
+[dependencies.cssparser]
+git = "https://github.com/servo/rust-cssparser";
+version = "0.13.5"
+tag = "v0.13.5"
+
 [dependencies.cairo-sys-rs]
 #git = "https://github.com/gtk-rs/cairo.git";
 git = "https://github.com/federicomenaquintero/cairo.git";
diff --git a/rust/src/color.rs b/rust/src/color.rs
index 6ac2ee1..efa0fae 100644
--- a/rust/src/color.rs
+++ b/rust/src/color.rs
@@ -3,6 +3,8 @@ use std::str::FromStr;
 use parsers::ParseError;
 use error::*;
 
+use ::cssparser;
+
 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
 pub struct RawColor {
     pub argb: u32
@@ -32,57 +34,10 @@ impl FromStr for RawColor {
     type Err = AttributeError;
 
     fn from_str (s: &str) -> Result<RawColor, AttributeError> {
-        if s.starts_with ('#') {
-            return parse_hex (s[1..].as_bytes ()).or (Err (
-                AttributeError::Parse (ParseError::new ("expected one of #rrggbbaa, #rrggbb, #rgba, #rgb"))))
+        match cssparser::Color::parse (&mut cssparser::Parser::new (s)) {
+            Ok (cssparser::Color::RGBA (rgba)) => Ok (RawColor::new_argb (rgba.alpha, rgba.red, rgba.green, 
rgba.blue)),
+            _ => Err (AttributeError::Parse (ParseError::new ("invalid color specification")))
         }
-
-        Err (AttributeError::Parse (ParseError::new ("invalid color specification")))
-    }
-}
-
-fn hex_digit (c: u8) -> Result<u8, ()> {
-    match c {
-        b'0'...b'9' => Ok (c - b'0'),
-        b'A'...b'F' => Ok (c - b'A' + 10),
-        b'a'...b'f' => Ok (c - b'a' + 10),
-        _ => Err (())
-    }
-}
-
-fn parse_hex (s: &[u8]) -> Result<RawColor, ()> {
-    match s.len () {
-        8 => {
-            // #rrggbbaa -> 0xaarrggbb
-            Ok (RawColor::new_argb (hex_digit (s[6])? * 16 + hex_digit (s[7])?,
-                                    hex_digit (s[0])? * 16 + hex_digit (s[1])?,
-                                    hex_digit (s[2])? * 16 + hex_digit (s[3])?,
-                                    hex_digit (s[4])? * 16 + hex_digit (s[5])?))
-        },
-
-        6 => {
-            // #rrggbb -> 0xffrrggbb
-            Ok (RawColor::new_rgb (hex_digit (s[0])? * 16 + hex_digit (s[1])?,
-                                   hex_digit (s[2])? * 16 + hex_digit (s[3])?,
-                                   hex_digit (s[4])? * 16 + hex_digit (s[5])?))
-        },
-
-        4 => {
-            // #rgba -> 0xaarrggbb
-            Ok (RawColor::new_argb (hex_digit (s[3])? * 0x11,
-                                    hex_digit (s[0])? * 0x11,
-                                    hex_digit (s[1])? * 0x11,
-                                    hex_digit (s[2])? * 0x11))
-        },
-
-        3 => {
-            // #rgb -> 0xffrrggbb
-            Ok (RawColor::new_rgb (hex_digit (s[0])? * 0x11,
-                                   hex_digit (s[1])? * 0x11,
-                                   hex_digit (s[2])? * 0x11))
-        }
-
-        _ => Err (())
     }
 }
 
@@ -100,6 +55,20 @@ mod tests {
     }
 
     #[test]
+    fn parses_color_keywords () {
+        assert_eq! (RawColor::from_str ("red").unwrap (),  RawColor::new_rgb (0xff, 0x00, 0x00));
+        assert_eq! (RawColor::from_str ("lime").unwrap (), RawColor::new_rgb (0x00, 0xff, 0x00));
+        assert_eq! (RawColor::from_str ("blue").unwrap (), RawColor::new_rgb (0x00, 0x00, 0xff));
+    }
+
+    #[test]
+    fn parses_color_functions () {
+        assert_eq! (RawColor::from_str ("rgb(255, 0, 0)").unwrap (), RawColor::new_rgb (0xff, 0x00, 0x00));
+        assert_eq! (RawColor::from_str ("rgb(0, 255, 0)").unwrap (), RawColor::new_rgb (0x00, 0xff, 0x00));
+        assert_eq! (RawColor::from_str ("rgb(0, 0, 255)").unwrap (), RawColor::new_rgb (0x00, 0x00, 0xff));
+    }
+
+    #[test]
     fn invalid_hash_hex_colors_yield_error () {
         assert! (RawColor::from_str ("#").is_err ());
         assert! (RawColor::from_str ("#xyz").is_err ());
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index b85c9e3..3358586 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -7,6 +7,8 @@ extern crate nom;
 #[macro_use]
 extern crate downcast_rs;
 
+extern crate cssparser;
+
 pub use aspect_ratio::{
     rsvg_aspect_ratio_parse,
     rsvg_aspect_ratio_compute


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