[librsvg/wip/aruiz/rust-pixbuf-loader] bootstrap pixbuf context



commit c9f8d54b4f9400c77efcf34109625ad296fa7e12
Author: Alberto Ruiz <aruiz redhat com>
Date:   Sat Jul 23 03:28:56 2022 +0100

    bootstrap pixbuf context

 gdk-pixbuf-loader/Cargo.toml |  4 ++-
 gdk-pixbuf-loader/src/lib.rs | 67 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 61 insertions(+), 10 deletions(-)
---
diff --git a/gdk-pixbuf-loader/Cargo.toml b/gdk-pixbuf-loader/Cargo.toml
index 9d7fd8137..180157635 100644
--- a/gdk-pixbuf-loader/Cargo.toml
+++ b/gdk-pixbuf-loader/Cargo.toml
@@ -8,4 +8,6 @@ edition = "2021"
 librsvg = { path = ".." }
 gdk-pixbuf-sys = "0.15"
 libc = "0.2"
-glib-sys = { version="0.15" }
\ No newline at end of file
+glib-sys = "0.15"
+glib = "0.15"
+gio = "0.15"
\ No newline at end of file
diff --git a/gdk-pixbuf-loader/src/lib.rs b/gdk-pixbuf-loader/src/lib.rs
index 12b0b625f..e9103359f 100644
--- a/gdk-pixbuf-loader/src/lib.rs
+++ b/gdk-pixbuf-loader/src/lib.rs
@@ -1,3 +1,5 @@
+use std::ptr::null_mut;
+
 use gdk_pixbuf_sys::{
     GdkPixbufFormat, GdkPixbufModule, GdkPixbufModulePattern, GdkPixbufModulePreparedFunc,
     GdkPixbufModuleSizeFunc, GdkPixbufModuleUpdatedFunc, GDK_PIXBUF_FORMAT_SCALABLE,
@@ -7,19 +9,65 @@ use gdk_pixbuf_sys::{
 use glib_sys::GError;
 use libc::{c_int, c_uint, c_void};
 
+use gio::MemoryInputStream;
+use librsvg::{Loader, SvgHandle};
+
+#[allow(non_camel_case_types)]
+type c_bool = c_int;
+
+struct SvgContext {
+    size_func: GdkPixbufModuleSizeFunc,
+    prep_func: GdkPixbufModulePreparedFunc,
+    update_func: GdkPixbufModuleUpdatedFunc,
+    user_data: *mut c_void,
+    first_write: c_bool,
+    stream: *mut MemoryInputStream,
+    handle: Option<SvgHandle>,
+}
+
+impl Drop for SvgContext {
+    fn drop(&mut self) {
+        if self.stream != null_mut() {
+            unsafe { Box::from_raw(self.stream) };
+        }
+    }
+}
+
 #[no_mangle]
 unsafe extern "C" fn begin_load(
     size_func: GdkPixbufModuleSizeFunc,
     prep_func: GdkPixbufModulePreparedFunc,
     update_func: GdkPixbufModuleUpdatedFunc,
     user_data: *mut c_void,
-    error: *mut *mut GError,
+    _error: *mut *mut GError,
 ) -> *mut c_void {
-    std::ptr::null_mut()
+    let mut ctx = SvgContext {
+        size_func,
+        prep_func,
+        update_func,
+        user_data,
+        first_write: 0,
+        stream: Box::into_raw(Box::new(MemoryInputStream::new())),
+        handle: None,
+    };
+
+    let loader = Loader::new();
+    match loader.read_stream::<_, gio::File, gio::Cancellable>(&*ctx.stream, None, None) {
+        Ok(f) => {
+            ctx.handle = Some(f);
+            Box::into_raw(Box::new(ctx)) as *mut c_void
+        }
+        Err(e) => {
+            //TODO> Set error
+            null_mut()
+        }
+    }
 }
 
 #[no_mangle]
 unsafe extern "C" fn stop_load(user_data: *mut c_void, error: *mut *mut GError) -> c_int {
+    let ctx = Box::from_raw(user_data as *mut SvgContext);
+    let _ = Box::into_raw(ctx);
     1
 }
 
@@ -29,7 +77,8 @@ unsafe extern "C" fn load_increment(
     buffer: *const u8,
     size: c_uint,
     error: *mut *mut GError,
-) -> c_int {
+) -> c_bool {
+    let ctx = Box::from_raw(user_data as *mut SvgContext);
     1
 }
 
@@ -44,13 +93,13 @@ extern "C" fn fill_vtable(module: &mut GdkPixbufModule) {
 extern "C" fn fill_info(info: &mut GdkPixbufFormat) {
     let signature = vec![
         GdkPixbufModulePattern {
-            mask: " <svg".as_ptr() as *mut i8,
-            prefix: "*    ".as_ptr() as *mut i8,
+            mask: " <svg\0".as_ptr() as *mut i8,
+            prefix: "*    \0".as_ptr() as *mut i8,
             relevance: 100,
         },
         GdkPixbufModulePattern {
-            mask: " <!DOCTYPE svg".as_ptr() as *mut i8,
-            prefix: "*             ".as_ptr() as *mut i8,
+            mask: " <!DOCTYPE svg\0".as_ptr() as *mut i8,
+            prefix: "*             \0".as_ptr() as *mut i8,
             relevance: 100,
         },
         GdkPixbufModulePattern {
@@ -79,9 +128,9 @@ extern "C" fn fill_info(info: &mut GdkPixbufFormat) {
 
     info.name = "svg\0".as_ptr() as *mut i8;
     info.signature = signature.as_ptr() as *mut GdkPixbufModulePattern;
-    info.description = "Scalable Vector Graphics".as_ptr() as *mut i8; //TODO: Gettext this
+    info.description = "Scalable Vector Graphics\0".as_ptr() as *mut i8; //TODO: Gettext this
     info.mime_types = mime_types.as_ptr() as *mut *mut i8;
     info.extensions = extensions.as_ptr() as *mut *mut i8;
     info.flags = GDK_PIXBUF_FORMAT_SCALABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
-    info.license = "LGPLv2".as_ptr() as *mut i8;
+    info.license = "LGPL\0".as_ptr() as *mut i8;
 }


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