[librsvg: 1/2] Port the remaining C convenience functions to Rust




commit 41c23b4347362282afa301fcc296a6022ec6ab27
Author: Sven Neumann <sven svenfoo org>
Date:   Thu Sep 17 21:33:38 2020 +0200

    Port the remaining C convenience functions to Rust
    
    Closes #626

 librsvg/c_api.rs      | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
 librsvg/rsvg-handle.c | 21 ++++++++-------
 2 files changed, 85 insertions(+), 9 deletions(-)
---
diff --git a/librsvg/c_api.rs b/librsvg/c_api.rs
index 50d2a039..5985f3c6 100644
--- a/librsvg/c_api.rs
+++ b/librsvg/c_api.rs
@@ -1163,6 +1163,19 @@ pub unsafe extern "C" fn rsvg_rust_handle_get_base_url(
     rhandle.get_base_url_as_ptr()
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_rust_handle_set_dpi(handle: *const RsvgHandle, dpi: f64) {
+    rsvg_return_if_fail! {
+        rsvg_handle_set_dpi;
+
+        is_rsvg_handle(handle),
+    }
+
+    let rhandle = get_rust_handle(handle);
+    rhandle.set_dpi_x(dpi);
+    rhandle.set_dpi_y(dpi);
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_rust_handle_set_dpi_x_y(
     handle: *const RsvgHandle,
@@ -1311,6 +1324,31 @@ pub unsafe extern "C" fn rsvg_rust_handle_has_sub(
     rhandle.has_sub(&id).unwrap_or(false).to_glib()
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_rust_handle_render_cairo(
+    handle: *const RsvgHandle,
+    cr: *mut cairo_sys::cairo_t,
+) -> glib_sys::gboolean {
+    rsvg_return_val_if_fail! {
+        rsvg_handle_render_cairo => false.to_glib();
+
+        is_rsvg_handle(handle),
+        !cr.is_null(),
+    }
+
+    let rhandle = get_rust_handle(handle);
+    let cr = from_glib_none(cr);
+
+    match rhandle.render_cairo_sub(&cr, None) {
+        Ok(()) => true.to_glib(),
+
+        Err(e) => {
+            rsvg_log!("could not render: {}", e);
+            false.to_glib()
+        }
+    }
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_rust_handle_render_cairo_sub(
     handle: *const RsvgHandle,
@@ -1338,6 +1376,27 @@ pub unsafe extern "C" fn rsvg_rust_handle_render_cairo_sub(
     }
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_rust_handle_get_pixbuf(
+    handle: *const RsvgHandle,
+) -> *mut gdk_pixbuf_sys::GdkPixbuf {
+    rsvg_return_val_if_fail! {
+        rsvg_handle_get_pixbuf => ptr::null_mut();
+
+        is_rsvg_handle(handle),
+    }
+
+    let rhandle = get_rust_handle(handle);
+
+    match rhandle.get_pixbuf_sub(None) {
+        Ok(pixbuf) => pixbuf.to_glib_full(),
+        Err(e) => {
+            rsvg_log!("could not render: {}", e);
+            ptr::null_mut()
+        }
+    }
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_rust_handle_get_pixbuf_sub(
     handle: *const RsvgHandle,
@@ -1435,6 +1494,15 @@ pub unsafe extern "C" fn rsvg_rust_handle_get_position_sub(
     }
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_rust_handle_new() -> *const RsvgHandle {
+    let obj: *mut gobject_sys::GObject = glib::Object::new(CHandle::get_type(), &[])
+        .unwrap()
+        .to_glib_full();
+
+    obj as *mut _
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_rust_handle_new_with_flags(flags: u32) -> *const RsvgHandle {
     let obj: *mut gobject_sys::GObject =
@@ -1612,6 +1680,11 @@ unsafe fn set_out_param<T: Copy>(
     }
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_rust_handle_free(handle: *mut RsvgHandle) {
+    gobject_sys::g_object_unref(handle as *mut _);
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_rust_handle_set_stylesheet(
     handle: *const RsvgHandle,
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 8902460d..bc06d4a2 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -323,6 +323,7 @@
 #include "rsvg.h"
 
 /* Implemented in rsvg_internals/src/handle.rs */
+extern void rsvg_rust_handle_set_dpi (RsvgHandle *raw_handle, double dpi);
 extern void rsvg_rust_handle_set_dpi_x_y (RsvgHandle *raw_handle, double dpi_x, double dpi_y);
 extern void rsvg_rust_handle_set_base_url (RsvgHandle *raw_handle, const char *uri);
 extern void rsvg_rust_handle_set_base_gfile (RsvgHandle *raw_handle, GFile *file);
@@ -335,9 +336,11 @@ extern gboolean rsvg_rust_handle_read_stream_sync (RsvgHandle *handle,
 extern gboolean rsvg_rust_handle_write (RsvgHandle *handle, const guchar *buf, gsize count, GError **error);
 extern gboolean rsvg_rust_handle_close (RsvgHandle *handle, GError **error);
 extern gboolean rsvg_rust_handle_has_sub (RsvgHandle *handle, const char *id);
+extern gboolean rsvg_rust_handle_render_cairo (RsvgHandle *handle, cairo_t *cr);
 extern gboolean rsvg_rust_handle_render_cairo_sub (RsvgHandle *handle,
                                                    cairo_t *cr,
                                                    const char *id);
+extern GdkPixbuf *rsvg_rust_handle_get_pixbuf (RsvgHandle *handle);
 extern GdkPixbuf *rsvg_rust_handle_get_pixbuf_sub (RsvgHandle *handle, const char *id);
 extern void rsvg_rust_handle_get_dimensions (RsvgHandle *handle,
                                              RsvgDimensionData *dimension_data);
@@ -351,6 +354,7 @@ extern void rsvg_rust_handle_set_size_callback (RsvgHandle *raw_handle,
                                                 RsvgSizeFunc size_func,
                                                 gpointer user_data,
                                                 GDestroyNotify destroy_notify);
+extern RsvgHandle *rsvg_rust_handle_new (void);
 extern RsvgHandle *rsvg_rust_handle_new_with_flags (RsvgHandleFlags flags);
 extern RsvgHandle *rsvg_rust_handle_new_from_file (const char *filename,
                                                    GError **error);
@@ -366,6 +370,7 @@ extern RsvgHandle *rsvg_rust_handle_new_from_stream_sync (GInputStream *input_st
 extern RsvgHandle *rsvg_rust_handle_new_from_data (const guint8 *data,
                                                    gsize data_len,
                                                    GError **error);
+extern void rsvg_rust_handle_free (RsvgHandle *handle);
 extern gboolean rsvg_rust_handle_set_stylesheet (RsvgHandle  *handle,
                                                  const char  *css,
                                                  gsize        css_len,
@@ -406,12 +411,10 @@ extern gboolean rsvg_rust_handle_render_element (RsvgHandle           *handle,
 
 
 
-/* Implemented in rsvg_internals/src/c_api.rs */
+/* Implemented in librsvg/c_api.rs */
 extern GType rsvg_rust_error_get_type (void);
-extern GType rsvg_rust_handle_flags_get_type (void);
-
-/* Implemented in rsvg_internals/src/c_api.rs */
 extern GType rsvg_rust_handle_get_type (void);
+extern GType rsvg_rust_handle_flags_get_type (void);
 
 GType
 rsvg_handle_get_type (void)
@@ -429,7 +432,7 @@ rsvg_handle_get_type (void)
 void
 rsvg_handle_free (RsvgHandle *handle)
 {
-    g_object_unref (handle);
+    rsvg_rust_handle_free (handle);
 }
 
 /**
@@ -461,7 +464,7 @@ rsvg_handle_free (RsvgHandle *handle)
 RsvgHandle *
 rsvg_handle_new (void)
 {
-    return RSVG_HANDLE (g_object_new (RSVG_TYPE_HANDLE, NULL));
+    return rsvg_rust_handle_new();
 }
 
 /**
@@ -875,7 +878,7 @@ rsvg_handle_render_cairo_sub (RsvgHandle *handle, cairo_t *cr, const char *id)
 gboolean
 rsvg_handle_render_cairo (RsvgHandle *handle, cairo_t *cr)
 {
-    return rsvg_handle_render_cairo_sub (handle, cr, NULL);
+    return rsvg_rust_handle_render_cairo (handle, cr);
 }
 
 /**
@@ -1034,7 +1037,7 @@ rsvg_handle_get_pixbuf_sub (RsvgHandle *handle, const char *id)
 GdkPixbuf *
 rsvg_handle_get_pixbuf (RsvgHandle *handle)
 {
-    return rsvg_handle_get_pixbuf_sub (handle, NULL);
+    return rsvg_rust_handle_get_pixbuf (handle);
 }
 
 /**
@@ -1054,7 +1057,7 @@ rsvg_handle_get_pixbuf (RsvgHandle *handle)
 void
 rsvg_handle_set_dpi (RsvgHandle *handle, double dpi)
 {
-    rsvg_handle_set_dpi_x_y (handle, dpi, dpi);
+    rsvg_rust_handle_set_dpi (handle, dpi);
 }
 
 /**


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