[librsvg] rsvg_handle_get_geometry_for_element(): New public API



commit 614b7c9ba0faa8033b190eae4fa7189758e85866
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Mar 26 19:12:55 2019 -0600

    rsvg_handle_get_geometry_for_element(): New public API
    
    This matches the corresponding CairoRenderer.geometry_for_element()
    from librsvg_crate.

 doc/rsvg-sections.txt       |  1 +
 librsvg/rsvg-handle.c       | 62 +++++++++++++++++++++++++++++++++++++++++++++
 librsvg/rsvg.h              |  7 +++++
 librsvg_crate/src/lib.rs    |  2 +-
 rsvg_internals/src/c_api.rs | 37 +++++++++++++++++++++++++++
 rsvg_internals/src/lib.rs   |  1 +
 6 files changed, 109 insertions(+), 1 deletion(-)
---
diff --git a/doc/rsvg-sections.txt b/doc/rsvg-sections.txt
index f121eefc..debad27a 100644
--- a/doc/rsvg-sections.txt
+++ b/doc/rsvg-sections.txt
@@ -24,6 +24,7 @@ rsvg_handle_get_dimensions
 rsvg_handle_get_dimensions_sub
 rsvg_handle_get_position_sub
 rsvg_handle_get_geometry_sub
+rsvg_handle_get_geometry_for_element
 rsvg_handle_has_sub
 rsvg_handle_get_title
 rsvg_handle_get_desc
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 65964670..d7607f4d 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -338,6 +338,13 @@ extern void rsvg_rust_handle_get_intrinsic_dimensions (RsvgHandle *handle,
                                                        RsvgLength *out_height,
                                                        gboolean   *out_has_viewbox,
                                                        RsvgRectangle *out_viewbox);
+extern gboolean rsvg_rust_handle_get_geometry_for_element (RsvgHandle     *handle,
+                                                           const char     *id,
+                                                           RsvgRectangle   viewport,
+                                                           RsvgRectangle  *out_ink_rect,
+                                                           RsvgRectangle  *out_logical_rect,
+                                                           GError        **error);
+
 
 /* Implemented in rsvg_internals/src/c_api.rs */
 extern GType rsvg_rust_error_get_type (void);
@@ -1060,6 +1067,61 @@ rsvg_handle_get_intrinsic_dimensions (RsvgHandle *handle,
                                                out_viewbox);
 }
 
+/**
+ * rsvg_handle_get_geometry_for_element:
+ * @handle: An #RsvgHandle
+ * @id: (nullable): An element's id within the SVG, or %NULL to compute
+ *   the geometry for the whole SVG. For example, if you have a layer called "layer1"
+ *   that you wish to render, pass "##layer1" as the id.
+ * @viewport: Viewport size at which the whole SVG would be fitted.
+ * @out_ink_rect: (out)(optional): Place to store the ink rectangle of the element.
+ * @out_logical_rect: (out)(optional): Place to store the logical rectangle of the element.
+ * @error: (allow-none): a location to store a #GError, or %NULL
+ *
+ * Computes the ink rectangle and logical rectangle of an SVG element, or the
+ * whole SVG, as if the whole SVG were rendered to a specific viewport.
+ *
+ * Element IDs should look like an URL fragment identifier; for
+ * example, pass "##foo" to get the geometry of the
+ * element that has an <literal>id="foo"</literal> attribute.
+ *
+ * The "ink rectangle" is the bounding box that would be painted
+ * for fully- stroked and filled elements.
+ *
+ * The "logical rectangle" just takes into account the unstroked
+ * paths and text outlines.
+ *
+ * Note that these bounds are not minimum bounds; for example,
+ * clipping paths are not taken into account.
+ *
+ * You can pass #NULL for the @id if you want to measure all
+ * the elements in the SVG, i.e. to measure everything from the
+ * root element.
+ *
+ * This operation is not constant-time, as it involves going through all
+ * the child elements.
+ *
+ * Since: 2.46
+ */
+gboolean
+rsvg_handle_get_geometry_for_element (RsvgHandle     *handle,
+                                      const char     *id,
+                                      RsvgRectangle   viewport,
+                                      RsvgRectangle  *out_ink_rect,
+                                      RsvgRectangle  *out_logical_rect,
+                                      GError        **error)
+{
+    g_return_val_if_fail (RSVG_IS_HANDLE (handle), FALSE);
+    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+    return rsvg_rust_handle_get_geometry_for_element (handle,
+                                                      id,
+                                                      viewport,
+                                                      out_ink_rect,
+                                                      out_logical_rect,
+                                                      error);
+}
+
 /**
  * rsvg_handle_internal_set_testing:
  * @handle: a #RsvgHandle
diff --git a/librsvg/rsvg.h b/librsvg/rsvg.h
index a29b889d..f717390d 100644
--- a/librsvg/rsvg.h
+++ b/librsvg/rsvg.h
@@ -232,6 +232,13 @@ void rsvg_handle_get_intrinsic_dimensions (RsvgHandle *handle,
                                            gboolean   *out_has_viewbox,
                                            RsvgRectangle *out_viewbox);
 
+gboolean rsvg_handle_get_geometry_for_element (RsvgHandle     *handle,
+                                               const char     *id,
+                                               RsvgRectangle   viewport,
+                                               RsvgRectangle  *out_ink_rect,
+                                               RsvgRectangle  *out_logical_rect,
+                                               GError        **error);
+
 /* GIO APIs */
 
 /**
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index 3f773d04..4565d12c 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -370,7 +370,7 @@ impl<'a> CairoRenderer<'a> {
     }
 
     /// Computes the (ink_rect, logical_rect) of an SVG element, as if
-    /// it were rendered to a specific viewport.
+    /// the SVG were rendered to a specific viewport.
     ///
     /// Element IDs should look like an URL fragment identifier; for
     /// example, pass `Some("#foo")` to get the geometry of the
diff --git a/rsvg_internals/src/c_api.rs b/rsvg_internals/src/c_api.rs
index 1d45e1db..f937faf8 100644
--- a/rsvg_internals/src/c_api.rs
+++ b/rsvg_internals/src/c_api.rs
@@ -929,6 +929,43 @@ pub unsafe extern "C" fn rsvg_rust_handle_get_intrinsic_dimensions(
     set_out_param(out_has_viewbox, out_viewbox, &r);
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_rust_handle_get_geometry_for_element(
+    handle: *mut RsvgHandle,
+    id: *const libc::c_char,
+    viewport: RsvgRectangle,
+    out_ink_rect: *mut RsvgRectangle,
+    out_logical_rect: *mut RsvgRectangle,
+    error: *mut *mut glib_sys::GError,
+) -> glib_sys::gboolean {
+    let rhandle = get_rust_handle(handle);
+
+    if rhandle.check_is_loaded().is_err() {
+        return false.to_glib();
+    }
+
+    let id: Option<String> = from_glib_none(id);
+
+    match rhandle.get_geometry_for_element(id.as_ref().map(String::as_str), &viewport.into()) {
+        Ok((ink_rect, logical_rect)) => {
+            if !out_ink_rect.is_null() {
+                *out_ink_rect = ink_rect;
+            }
+
+            if !out_logical_rect.is_null() {
+                *out_logical_rect = logical_rect;
+            }
+
+            true.to_glib()
+        },
+
+        Err(e) => {
+            set_gerror(error, 0, &format!("{}", e));
+            false.to_glib()
+        }
+    }
+}
+
 /// Detects whether a `*const libc::c_char` is a path or a URI
 ///
 /// `rsvg_handle_new_from_file()` takes a `filename` argument, and advertises
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 21897b24..d092e084 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -24,6 +24,7 @@ pub use crate::c_api::{
     rsvg_rust_handle_get_dpi_x,
     rsvg_rust_handle_get_dpi_y,
     rsvg_rust_handle_get_flags,
+    rsvg_rust_handle_get_geometry_for_element,
     rsvg_rust_handle_get_geometry_sub,
     rsvg_rust_handle_get_intrinsic_dimensions,
     rsvg_rust_handle_get_pixbuf_sub,


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