[librsvg: 36/48] wip: xinclude, in acquire_text()



commit af417dd194b9f75a2dcdcfb242f72503cd07e541
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Oct 8 16:22:37 2018 -0500

    wip: xinclude, in acquire_text()

 Cargo.lock                |  65 ++++++++++++++++++++++
 rsvg_internals/Cargo.toml |   1 +
 rsvg_internals/src/lib.rs |   4 +-
 rsvg_internals/src/xml.rs | 134 +++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 194 insertions(+), 10 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 5fef23ca..aa40a180 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -304,6 +304,63 @@ name = "either"
 version = "1.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "encoding"
+version = "0.2.33"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "encoding-index-simpchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "encoding-index-singlebyte 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "encoding-index-japanese"
+version = "1.20141219.5"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "encoding-index-korean"
+version = "1.20141219.5"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "encoding-index-simpchinese"
+version = "1.20141219.5"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "encoding-index-singlebyte"
+version = "1.20141219.5"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "encoding-index-tradchinese"
+version = "1.20141219.5"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "encoding_index_tests"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "failure"
 version = "0.1.1"
@@ -766,6 +823,7 @@ dependencies = [
  "criterion 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "cssparser 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "downcast-rs 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
  "float-cmp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "glib 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "glib-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1043,6 +1101,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
 "checksum dtoa-short 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"59020b8513b76630c49d918c33db9f4c91638e7d3404a28084083b87e33f76f2"
 "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
+"checksum encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = 
"6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec"
+"checksum encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91"
+"checksum encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81"
+"checksum encoding-index-simpchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7"
+"checksum encoding-index-singlebyte 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a"
+"checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = 
"fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
+"checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
 "checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82"
 "checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b"
 "checksum float-cmp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"134a8fa843d80a51a5b77d36d42bc2def9edcb0262c914861d08129fd1926600"
diff --git a/rsvg_internals/Cargo.toml b/rsvg_internals/Cargo.toml
index c859cd29..74bf3dda 100644
--- a/rsvg_internals/Cargo.toml
+++ b/rsvg_internals/Cargo.toml
@@ -22,6 +22,7 @@ phf_codegen = "0.7.21"
 [dependencies]
 libc = "0.2"
 downcast-rs = "^1.0.0"
+encoding = "0.2.33"
 regex = "1"
 itertools = "0.7.4"
 pango = "0.4"
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 227167d3..2783da78 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -5,6 +5,8 @@
 extern crate cairo;
 extern crate cairo_sys;
 extern crate cssparser;
+extern crate downcast_rs;
+extern crate encoding;
 extern crate float_cmp;
 extern crate glib;
 extern crate glib_sys;
@@ -23,8 +25,6 @@ extern crate regex;
 #[macro_use]
 extern crate lazy_static;
 
-extern crate downcast_rs;
-
 pub use color::{rsvg_css_parse_color, ColorKind, ColorSpec};
 
 pub use css::{
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index e0d5814e..362af7dc 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -1,3 +1,5 @@
+use encoding::label::encoding_from_whatwg_label;
+use encoding::DecoderTrap;
 use libc;
 use std;
 use std::cell::RefCell;
@@ -59,13 +61,22 @@ impl XmlHandler for NodeCreationContext {
         name: &str,
         pbag: &PropertyBag,
     ) -> Box<XmlHandler> {
-        if name == "style" {
-            let ctx = StyleContext::empty();
-            ctx.start_element(Some(self), parent, handle, name, pbag)
-        } else {
-            let node = self.create_node(parent, handle, name, pbag);
+        match name {
+            "include" => {
+                let ctx = XIncludeContext::empty();
+                ctx.start_element(Some(self), parent, handle, name, pbag)
+            }
+
+            "style" => {
+                let ctx = StyleContext::empty();
+                ctx.start_element(Some(self), parent, handle, name, pbag)
+            }
 
-            Box::new(NodeCreationContext { node: Some(node) })
+            _ => {
+                let node = self.create_node(parent, handle, name, pbag);
+
+                Box::new(NodeCreationContext { node: Some(node) })
+            }
         }
     }
 
@@ -215,6 +226,108 @@ impl StyleContext {
     }
 }
 
+struct XIncludeContext {
+    needs_fallback: bool,
+}
+
+impl XmlHandler for XIncludeContext {
+    fn start_element(
+        &self,
+        _previous_handler: Option<&XmlHandler>,
+        _parent: Option<&Rc<Node>>,
+        handle: *mut RsvgHandle,
+        _name: &str,
+        pbag: &PropertyBag,
+    ) -> Box<XmlHandler> {
+        let mut href = None;
+        let mut parse = None;
+        let mut encoding = None;
+
+        for (_key, attr, value) in pbag.iter() {
+            match attr {
+                Attribute::Href => href = Some(value),
+                Attribute::Parse => parse = Some(value),
+                Attribute::Encoding => encoding = Some(value),
+                _ => (),
+            }
+        }
+
+        self.acquire(handle, href, parse, encoding);
+
+        unimplemented!("finish start_xinclude() here");
+
+        Box::new(XIncludeContext::empty())
+    }
+
+    fn end_element(&self, handle: *mut RsvgHandle, _name: &str) -> Option<Rc<Node>> {
+        unimplemented!();
+    }
+
+    fn characters(&self, text: &str) {
+        unimplemented!();
+    }
+}
+
+impl XIncludeContext {
+    fn empty() -> XIncludeContext {
+        XIncludeContext {
+            needs_fallback: true,
+        }
+    }
+
+    fn acquire(
+        &self,
+        handle: *mut RsvgHandle,
+        href: Option<&str>,
+        parse: Option<&str>,
+        encoding: Option<&str>,
+    ) {
+        if let Some(href) = href {
+            if parse == Some("text") {
+                self.acquire_text(handle, href, encoding);
+            } else {
+                unimplemented!("finish the xml case here");
+            }
+        }
+    }
+
+    fn acquire_text(&self, handle: *mut RsvgHandle, href: &str, encoding: Option<&str>) {
+        let binary = match handle::acquire_data(handle, href) {
+            Ok(b) => b,
+            Err(e) => {
+                rsvg_log!("could not acquire \"{}\": {}", href, e);
+                return;
+            }
+        };
+
+        let encoding = encoding.unwrap_or("utf-8");
+
+        let encoder = match encoding_from_whatwg_label(encoding) {
+            Some(enc) => enc,
+            None => {
+                rsvg_log!("unknown encoding \"{}\" for \"{}\"", encoding, href);
+                return;
+            }
+        };
+
+        let utf8_data = match encoder.decode(&binary.data, DecoderTrap::Strict) {
+            Ok(data) => data,
+
+            Err(e) => {
+                rsvg_log!(
+                    "could not convert contents of \"{}\" from character encoding \"{}\": {}",
+                    href,
+                    encoding,
+                    e
+                );
+                return;
+            }
+        };
+
+        unimplemented!("rsvg_xml_state_characters(utf8_data)");
+    }
+}
+
 /// A concrete parsing context for a surrounding `element_name` and its XML event handlers
 struct Context {
     element_name: String,
@@ -264,8 +377,13 @@ impl XmlState {
 
     pub fn start_element(&mut self, handle: *mut RsvgHandle, name: &str, pbag: &PropertyBag) {
         let next_context = if let Some(top) = self.context_stack.last() {
-            top.handler
-                .start_element(Some(&*top.handler), top.handler.get_node().as_ref(), handle, name, pbag)
+            top.handler.start_element(
+                Some(&*top.handler),
+                top.handler.get_node().as_ref(),
+                handle,
+                name,
+                pbag,
+            )
         } else {
             let default_context = NodeCreationContext::empty();
 


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