[librsvg/wip/subclass] Use cairo::Surface::set_mime_data() instead of the FFI function



commit 79e4acf2686d9d68f68ab6a42a4e5deb01960626
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Feb 11 09:04:57 2019 -0600

    Use cairo::Surface::set_mime_data() instead of the FFI function
    
    Thanks to Julian Sparber for the initial cut at this patch.

 rsvg_internals/src/surface_utils/shared_surface.rs | 19 ++++++++++++-
 rsvg_internals/src/svg.rs                          | 33 +++++-----------------
 2 files changed, 25 insertions(+), 27 deletions(-)
---
diff --git a/rsvg_internals/src/surface_utils/shared_surface.rs 
b/rsvg_internals/src/surface_utils/shared_surface.rs
index e91f1ceb..ae6496ca 100644
--- a/rsvg_internals/src/surface_utils/shared_surface.rs
+++ b/rsvg_internals/src/surface_utils/shared_surface.rs
@@ -11,6 +11,7 @@ use glib::translate::{Stash, ToGlibPtr};
 use nalgebra::{storage::Storage, Dim, Matrix};
 use rayon;
 
+use io::BinaryData;
 use rect::IRect;
 use srgb;
 use util::clamp;
@@ -167,7 +168,12 @@ impl SharedImageSurface {
         }
     }
 
-    pub fn from_pixbuf(pixbuf: &Pixbuf) -> Result<SharedImageSurface, cairo::Status> {
+    /// Creates a surface from the pixbuf, and optionally attaches the
+    /// original MIME data to the Cairo surface.
+    pub fn from_pixbuf(
+        pixbuf: &Pixbuf,
+        data: Option<BinaryData>,
+    ) -> Result<SharedImageSurface, cairo::Status> {
         assert!(pixbuf.get_colorspace() == Colorspace::Rgb);
 
         let n_channels = pixbuf.get_n_channels();
@@ -231,6 +237,17 @@ impl SharedImageSurface {
             }
         }
 
+        if let Some(data) = data {
+            let BinaryData {
+                data: bytes,
+                content_type,
+            } = data;
+
+            if let Some(mime_type) = content_type {
+                surf.set_mime_data(&mime_type, bytes)?;
+            }
+        }
+
         Self::new(surf, SurfaceType::SRgb)
     }
 
diff --git a/rsvg_internals/src/svg.rs b/rsvg_internals/src/svg.rs
index 50da17a6..bc0e3ae0 100644
--- a/rsvg_internals/src/svg.rs
+++ b/rsvg_internals/src/svg.rs
@@ -1,8 +1,5 @@
-use cairo::Status;
-use cairo_sys::cairo_surface_set_mime_data;
 use gdk_pixbuf::{PixbufLoader, PixbufLoaderExt};
 use gio;
-use glib::translate::*;
 use std::cell::RefCell;
 use std::collections::hash_map::Entry;
 use std::collections::HashMap;
@@ -191,29 +188,13 @@ fn load_image(
 
     let pixbuf = loader.get_pixbuf().ok_or(LoadingError::Unknown)?;
 
-    let surface = SharedImageSurface::from_pixbuf(&pixbuf)?;
-
-    if load_options.flags.keep_image_data {
-        if let Some(mime_type) = data.content_type {
-            let data_ptr = ToGlibContainerFromSlice::to_glib_full_from_slice(&data.data);
-
-            unsafe {
-                let status = cairo_surface_set_mime_data(
-                    surface.to_glib_none().0,
-                    mime_type.to_glib_none().0,
-                    data_ptr as *mut _,
-                    data.data.len() as libc::c_ulong,
-                    Some(glib_sys::g_free),
-                    data_ptr as *mut _,
-                )
-                .into();
-
-                if status != Status::Success {
-                    return Err(LoadingError::Cairo(status));
-                }
-            }
-        }
-    }
+    let data = if load_options.flags.keep_image_data {
+        Some(data)
+    } else {
+        None
+    };
+
+    let surface = SharedImageSurface::from_pixbuf(&pixbuf, data)?;
 
     Ok(surface)
 }


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