[librsvg: 26/29] Test rsvg_handle_get_pixbuf_sub()



commit bfbdf26e79cfff46e3fcc112fce1fd1e560cb0f1
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Feb 6 14:01:46 2018 -0600

    Test rsvg_handle_get_pixbuf_sub()
    
    And document that function better

 rsvg-handle.c | 20 +++++++++++++-------
 tests/api.c   | 27 ++++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 8 deletions(-)
---
diff --git a/rsvg-handle.c b/rsvg-handle.c
index b407250..18efb4b 100644
--- a/rsvg-handle.c
+++ b/rsvg-handle.c
@@ -1018,13 +1018,19 @@ rsvg_handle_has_sub (RsvgHandle * handle,
  * @id: (nullable): An element's id within the SVG, starting with "##", for
  * example, "##layer1"; or %NULL to use the whole SVG.
  *
- * Returns the pixbuf loaded by @handle.  The pixbuf returned will be reffed, so
- * the caller of this function must assume that ref.  If insufficient data has
- * been read to create the pixbuf, or an error occurred in loading, then %NULL
- * will be returned.  Note that the pixbuf may not be complete until
- * @rsvg_handle_close has been called.
- *
- * Returns: (transfer full) (nullable): the pixbuf loaded by @handle, or %NULL.
+ * Creates a #GdkPixbuf the same size as the entire SVG loaded into @handle, but
+ * only renders the sub-element that has the specified @id (and all its
+ * sub-sub-elements recursively).  If @id is #NULL, this function renders the
+ * whole SVG.
+ *
+ * If you need to render an image which is only big enough to fit a particular
+ * sub-element of the SVG, consider using rsvg_handle_render_cairo_sub(), upon a
+ * surface that is just the size returned by rsvg_handle_get_dimensions_sub().
+ * You will need to offset the rendering by the amount returned in
+ * rsvg_handle_get_position_sub().
+ *
+ * Returns: (transfer full) (nullable): a pixbuf, or %NULL if an error occurs
+ * during rendering.
  *
  * Since: 2.14
  **/
diff --git a/tests/api.c b/tests/api.c
index 2fee059..749d55f 100644
--- a/tests/api.c
+++ b/tests/api.c
@@ -14,7 +14,6 @@
 rsvg_handle_get_dimensions
 rsvg_handle_get_dimensions_sub
 rsvg_handle_get_position_sub
-rsvg_handle_get_pixbuf_sub
 rsvg_handle_get_base_uri
 rsvg_handle_set_base_uri
 rsvg_handle_set_size_callback
@@ -394,6 +393,31 @@ handle_get_pixbuf (void)
     g_object_unref (handle);
 }
 
+static void
+handle_get_pixbuf_sub (void)
+{
+    char *filename = get_test_filename ("example.svg");
+    GError *error = NULL;
+
+    RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
+    g_free (filename);
+
+    g_assert (handle != NULL);
+    g_assert (error == NULL);
+
+    GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf_sub (handle, EXAMPLE_ONE_ID);
+    g_assert (pixbuf != NULL);
+
+    /* Note that rsvg_handle_get_pixbuf_sub() creates a surface the size of the
+     * whole SVG, not just the size of the sub-element.
+     */
+    g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, EXAMPLE_WIDTH);
+    g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, EXAMPLE_HEIGHT);
+
+    g_object_unref (pixbuf);
+    g_object_unref (handle);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -417,6 +441,7 @@ main (int argc, char **argv)
     g_test_add_func ("/api/handle_read_stream_sync", handle_read_stream_sync);
     g_test_add_func ("/api/handle_has_sub", handle_has_sub);
     g_test_add_func ("/api/handle_get_pixbuf", handle_get_pixbuf);
+    g_test_add_func ("/api/handle_get_pixbuf_sub", handle_get_pixbuf_sub);
 
     return g_test_run ();
 }


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