[librsvg: 1/3] load: remove legacy load context



commit 8f059e3df4c038b109a068d0d32612137ec8390b
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Jan 15 21:37:43 2019 +0100

    load: remove legacy load context
    
    Having it factored out helped porting, but now we can simplify
    and shorten the code.

 Makefile.am                  |  1 -
 rsvg_internals/src/handle.rs | 66 +++++++++++++++++-----------------
 rsvg_internals/src/lib.rs    |  1 -
 rsvg_internals/src/load.rs   | 85 --------------------------------------------
 4 files changed, 34 insertions(+), 119 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index be1926a4..601d34d5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -83,7 +83,6 @@ RUST_SRC =                                                    \
        rsvg_internals/src/iri.rs                               \
        rsvg_internals/src/length.rs                            \
        rsvg_internals/src/lib.rs                               \
-       rsvg_internals/src/load.rs                              \
        rsvg_internals/src/log.rs                               \
        rsvg_internals/src/link.rs                              \
        rsvg_internals/src/marker.rs                            \
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 9561469c..4b4f34ac 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -13,7 +13,7 @@ use gdk_pixbuf_sys;
 use gio::{self, FileExt};
 use gio_sys;
 use glib::translate::*;
-use glib::Cast;
+use glib::{Bytes, Cast};
 use glib_sys;
 use gobject_sys;
 use libc;
@@ -23,13 +23,14 @@ use allowed_url::{AllowedUrl, Href};
 use dpi::Dpi;
 use drawing_ctx::{DrawingCtx, RsvgRectangle};
 use error::{set_gerror, DefsLookupErrorKind, LoadingError, RenderingError};
-use load::LoadContext;
 use node::RsvgNode;
 use pixbuf_utils::pixbuf_from_surface;
 use structure::NodeSvg;
 use surface_utils::{shared_surface::SharedImageSurface, shared_surface::SurfaceType};
 use svg::Svg;
 use util::rsvg_g_warning;
+use xml::XmlState;
+use xml2_load::xml_state_load_from_possibly_compressed_stream;
 
 // A *const RsvgHandle is just an opaque pointer we get from C
 #[repr(C)]
@@ -148,7 +149,7 @@ pub struct Handle {
     svg: RefCell<Option<Rc<Svg>>>,
     load_flags: Cell<LoadFlags>,
     load_state: Cell<LoadState>,
-    load: RefCell<Option<LoadContext>>,
+    buffer: Vec<u8>, // used by the legacy write() api
     size_callback: RefCell<SizeCallback>,
     in_loop: Cell<bool>,
     is_testing: Cell<bool>,
@@ -163,7 +164,7 @@ impl Handle {
             svg: RefCell::new(None),
             load_flags: Cell::new(LoadFlags::default()),
             load_state: Cell::new(LoadState::Start),
-            load: RefCell::new(None),
+            buffer: Vec::new(),
             size_callback: RefCell::new(SizeCallback::new()),
             in_loop: Cell::new(false),
             is_testing: Cell::new(false),
@@ -228,46 +229,47 @@ impl Handle {
     }
 
     pub fn write(&mut self, buf: &[u8]) {
-        assert!(
-            self.load_state.get() == LoadState::Start
-                || self.load_state.get() == LoadState::Loading
-        );
-
-        if self.load_state.get() == LoadState::Start {
-            self.load_state.set(LoadState::Loading);
-
-            self.load = RefCell::new(Some(LoadContext::new(&self.load_options())));
-        }
-
-        assert!(self.load_state.get() == LoadState::Loading);
+        match self.load_state.get() {
+            LoadState::Start => self.load_state.set(LoadState::Loading),
+            LoadState::Loading => (),
+            _ => unreachable!(),
+        };
 
-        self.load.borrow_mut().as_mut().unwrap().write(buf);
+        self.buffer.extend_from_slice(buf);
     }
 
     pub fn close(&mut self) -> Result<(), LoadingError> {
-        let load_state = self.load_state.get();
-
-        let res = match load_state {
+        let res = match self.load_state.get() {
             LoadState::Start => {
                 self.load_state.set(LoadState::ClosedError);
                 Err(LoadingError::NoDataPassedToParser)
             }
 
-            LoadState::Loading => self
-                .load
-                .borrow_mut()
-                .as_mut()
-                .unwrap()
-                .close()
-                .and_then(|mut xml| {
-                    self.load_state.set(LoadState::ClosedOk);
-                    *self.svg.borrow_mut() = Some(Rc::new(xml.steal_result()?));
-                    Ok(())
-                })
+            LoadState::Loading => {
+                let bytes = Bytes::from(&self.buffer);
+                let stream = gio::MemoryInputStream::new_from_bytes(&bytes);
+                let mut xml = XmlState::new(&self.load_options());
+
+                xml_state_load_from_possibly_compressed_stream(
+                    &mut xml,
+                    self.load_flags.get(),
+                    &stream.upcast(),
+                    None,
+                )
                 .map_err(|e| {
                     self.load_state.set(LoadState::ClosedError);
                     e
-                }),
+                })?;
+
+                let svg = xml.steal_result().map_err(|e| {
+                    self.load_state.set(LoadState::ClosedError);
+                    e
+                })?;
+
+                self.load_state.set(LoadState::ClosedOk);
+                *self.svg.borrow_mut() = Some(Rc::new(svg));
+                Ok(())
+            }
 
             LoadState::ClosedOk | LoadState::ClosedError => {
                 // closing is idempotent
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index a80ce2c2..5ad608b6 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -113,7 +113,6 @@ mod io;
 mod iri;
 mod length;
 mod link;
-mod load;
 mod marker;
 mod mask;
 mod node;


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