[librsvg/wip/dimensions-api: 11/11] rsvg_handle_get_intrinsic_dimensions(): New API



commit 6456715b846f3ed0f0102c5f7c6f09437281b462
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Feb 7 19:36:57 2019 -0600

    rsvg_handle_get_intrinsic_dimensions(): New API

 librsvg/rsvg-handle.c             | 27 ++++++++++++++++++++
 librsvg/rsvg.h                    |  8 ++++++
 rsvg_internals/src/drawing_ctx.rs | 11 ++++++++
 rsvg_internals/src/handle.rs      | 53 ++++++++++++++++++++++++++++++++++++++-
 rsvg_internals/src/length.rs      |  8 +++++-
 rsvg_internals/src/lib.rs         |  1 +
 rsvg_internals/src/structure.rs   |  6 ++---
 7 files changed, 109 insertions(+), 5 deletions(-)
---
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index fa03f6b6..45aee588 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -337,6 +337,13 @@ extern RsvgHandle *rsvg_handle_rust_new_from_stream_sync (GInputStream *input_st
 extern RsvgHandle *rsvg_handle_rust_new_from_data (const guint8 *data,
                                                    gsize data_len,
                                                    GError **error);
+extern void rsvg_handle_rust_get_intrinsic_dimensions (RsvgHandle *handle,
+                                                       gboolean   *out_has_width,
+                                                       RsvgLength *out_width,
+                                                       gboolean   *out_has_height,
+                                                       RsvgLength *out_height,
+                                                       gboolean   *out_has_viewbox,
+                                                       RsvgRectangle *out_viewbox);
 
 typedef struct {
     RsvgHandleRust *rust_handle;
@@ -1265,6 +1272,26 @@ rsvg_handle_set_size_callback (RsvgHandle *handle,
                                         user_data_destroy);
 }
 
+void
+rsvg_handle_get_intrinsic_dimensions (RsvgHandle *handle,
+                                      gboolean   *out_has_width,
+                                      RsvgLength *out_width,
+                                      gboolean   *out_has_height,
+                                      RsvgLength *out_height,
+                                      gboolean   *out_has_viewbox,
+                                      RsvgRectangle *out_viewbox)
+{
+    g_return_if_fail (RSVG_IS_HANDLE (handle));
+
+    rsvg_handle_rust_get_intrinsic_dimensions (handle,
+                                               out_has_width,
+                                               out_width,
+                                               out_has_height,
+                                               out_height,
+                                               out_has_viewbox,
+                                               out_viewbox);
+}
+
 /**
  * rsvg_handle_internal_set_testing:
  * @handle: a #RsvgHandle
diff --git a/librsvg/rsvg.h b/librsvg/rsvg.h
index 4232569d..2b6afc2e 100644
--- a/librsvg/rsvg.h
+++ b/librsvg/rsvg.h
@@ -221,6 +221,14 @@ typedef struct {
     RsvgUnit unit;
 } RsvgLength;
 
+void rsvg_handle_get_intrinsic_dimensions (RsvgHandle *handle,
+                                           gboolean   *out_has_width,
+                                           RsvgLength *out_width,
+                                           gboolean   *out_has_height,
+                                           RsvgLength *out_height,
+                                           gboolean   *out_has_viewbox,
+                                           RsvgRectangle *out_viewbox);
+
 /* GIO APIs */
 
 /**
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 4b784386..4a4662fb 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -914,6 +914,17 @@ impl From<cairo::Rectangle> for RsvgRectangle {
     }
 }
 
+impl From<ViewBox> for RsvgRectangle {
+    fn from(vb: ViewBox) -> RsvgRectangle {
+        RsvgRectangle {
+            x: vb.x,
+            y: vb.y,
+            width: vb.width,
+            height: vb.height,
+        }
+    }
+}
+
 pub struct AcquiredNode(Rc<RefCell<Vec<RsvgNode>>>, RsvgNode);
 
 impl Drop for AcquiredNode {
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index b179cf56..4e8d85ee 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -24,9 +24,10 @@ use allowed_url::{AllowedUrl, Href};
 use dpi::Dpi;
 use drawing_ctx::{DrawingCtx, RsvgRectangle};
 use error::{set_gerror, DefsLookupErrorKind, LoadingError, RenderingError};
+use length::RsvgLength;
 use node::RsvgNode;
 use pixbuf_utils::pixbuf_from_surface;
-use structure::NodeSvg;
+use structure::{IntrinsicDimensions, NodeSvg};
 use surface_utils::{shared_surface::SharedImageSurface, shared_surface::SurfaceType};
 use svg::Svg;
 use util::rsvg_g_warning;
@@ -535,6 +536,13 @@ impl Handle {
 
         self.read_stream_sync(stream, cancellable)
     }
+
+    fn get_intrinsic_dimensions(&self) -> IntrinsicDimensions {
+        let svg_ref = self.svg.borrow();
+        let svg = svg_ref.as_ref().unwrap();
+
+        svg.get_intrinsic_dimensions()
+    }
 }
 
 // Keep these in sync with rsvg.h:RsvgHandleFlags
@@ -1124,3 +1132,46 @@ pub unsafe extern "C" fn rsvg_handle_rust_new_from_data(
     gobject_sys::g_object_unref(raw_stream as *mut _);
     ret
 }
+
+unsafe fn set_out_param<T: Copy>(out_has_param: *mut glib_sys::gboolean, out_param: *mut T, value: 
&Option<T>) {
+    let has_value = if let Some(ref v) = *value {
+        if !out_param.is_null() {
+            *out_param = *v;
+        }
+
+        true
+    } else {
+        false
+    };
+
+    if !out_has_param.is_null() {
+        *out_has_param = has_value.to_glib();
+    }
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_handle_rust_get_intrinsic_dimensions(
+    handle: *mut RsvgHandle,
+    out_has_width: *mut glib_sys::gboolean,
+    out_width: *mut RsvgLength,
+    out_has_height: *mut glib_sys::gboolean,
+    out_height: *mut RsvgLength,
+    out_has_viewbox: *mut glib_sys::gboolean,
+    out_viewbox: *mut RsvgRectangle,
+) {
+    let rhandle = get_rust_handle(handle);
+
+    if !is_loaded(rhandle) {
+        return;
+    }
+
+    let d = rhandle.get_intrinsic_dimensions();
+
+    let w = d.width.map(|l| l.to_length());
+    let h = d.width.map(|l| l.to_length());
+    let r = d.vbox.map(RsvgRectangle::from);
+
+    set_out_param (out_has_width, out_width, &w);
+    set_out_param (out_has_height, out_height, &h);
+    set_out_param (out_has_viewbox, out_viewbox, &r);
+}
diff --git a/rsvg_internals/src/length.rs b/rsvg_internals/src/length.rs
index e41f4189..0f99b7c5 100644
--- a/rsvg_internals/src/length.rs
+++ b/rsvg_internals/src/length.rs
@@ -7,6 +7,8 @@ use parsers::Parse;
 use parsers::ParseError;
 use properties::ComputedValues;
 
+pub type RsvgLength = Length;
+
 // Keep this in sync with rsvg.h:RsvgUnit
 
 /// Units for length values
@@ -80,6 +82,10 @@ macro_rules! define_length_type {
                 self.0.get_unitless()
             }
 
+            pub fn to_length(&self) -> Length {
+                self.0
+            }
+
             pub fn check_nonnegative(self) -> Result<Self, ValueErrorKind> {
                 if self.length() >= 0.0 {
                     Ok(self)
@@ -179,7 +185,7 @@ define_length_type!(LengthBoth, LengthDir::Both);
 // Keep this in sync with rsvg.h:RsvgLength
 #[repr(C)]
 #[derive(Debug, PartialEq, Copy, Clone)]
-struct Length {
+pub struct Length {
     pub length: f64,
     pub unit: LengthUnit,
 }
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index e979c7ed..b0334ad1 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -50,6 +50,7 @@ pub use handle::{
     rsvg_handle_rust_get_dpi_y,
     rsvg_handle_rust_get_flags,
     rsvg_handle_rust_get_geometry_sub,
+    rsvg_handle_rust_get_intrinsic_dimensions,
     rsvg_handle_rust_get_pixbuf_sub,
     rsvg_handle_rust_get_position_sub,
     rsvg_handle_rust_has_sub,
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index ba2205d8..3040285d 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -98,9 +98,9 @@ impl NodeTrait for NodeSwitch {
 /// Intrinsic dimensions of an SVG document fragment
 #[derive(Copy, Clone)]
 pub struct IntrinsicDimensions {
-    width: Option<LengthHorizontal>,
-    height: Option<LengthVertical>,
-    vbox: Option<ViewBox>,
+    pub width: Option<LengthHorizontal>,
+    pub height: Option<LengthVertical>,
+    pub vbox: Option<ViewBox>,
 }
 
 pub struct NodeSvg {


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