[librsvg/rustification] Add a testing mode so reftests can use predictable text rendering



commit 25457b72770018045aa88d47688fbf1a45bff9af
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Dec 19 11:49:17 2016 -0600

    Add a testing mode so reftests can use predictable text rendering
    
    This adds an rsvg_handle_internal_set_testing() API, which should only
    be called by the test suite.  Internally, it sets cairo_font_options_t
    that should yield reproducible text rendering.
    
    For example, I get different rendering results from text objects when I
    run "make check" as a user, than when I run it as root.

 rsvg-base.c         |   16 ++++++++++++++++
 rsvg-cairo-draw.c   |   20 ++++++++++++++++++++
 rsvg-cairo-render.c |    1 +
 rsvg-gobject.c      |    2 ++
 rsvg-private.h      |    3 +++
 rsvg.h              |    2 ++
 tests/rsvg-test.c   |    2 ++
 7 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/rsvg-base.c b/rsvg-base.c
index 61e0474..b735134 100644
--- a/rsvg-base.c
+++ b/rsvg-base.c
@@ -2143,6 +2143,22 @@ rsvg_handle_new_from_stream_sync (GInputStream   *input_stream,
 }
 
 /**
+ * _rsvg_handle_internal_set_testing:
+ * @handle: a #RsvgHandle
+ * @testing: Whether to enable testing mode
+ *
+ * Do not call this function.  This is intended for librsvg's internal
+ * test suite only.
+ **/
+void
+rsvg_handle_internal_set_testing (RsvgHandle *handle, gboolean testing)
+{
+    g_return_if_fail (RSVG_IS_HANDLE (handle));
+
+    handle->priv->is_testing = testing ? TRUE : FALSE;
+}
+
+/**
  * rsvg_init:
  *
  * Initializes librsvg
diff --git a/rsvg-cairo-draw.c b/rsvg-cairo-draw.c
index 7933aa7..1575188 100644
--- a/rsvg-cairo-draw.c
+++ b/rsvg-cairo-draw.c
@@ -358,6 +358,19 @@ _set_rsvg_affine (RsvgCairoRender * render, cairo_matrix_t *affine)
     cairo_set_matrix (cr, &matrix);
 }
 
+static cairo_font_options_t *
+get_font_options_for_testing (void)
+{
+    cairo_font_options_t *options;
+
+    options = cairo_font_options_create ();
+    cairo_font_options_set_antialias (options, CAIRO_ANTIALIAS_GRAY);
+    cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_FULL);
+    cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
+
+    return options;
+}
+
 PangoContext *
 rsvg_cairo_create_pango_context (RsvgDrawingCtx * ctx)
 {
@@ -373,6 +386,13 @@ rsvg_cairo_create_pango_context (RsvgDrawingCtx * ctx)
     rsvg_drawing_ctx_get_dpi (ctx, NULL, &dpi_y);
     pango_cairo_context_set_resolution (context, dpi_y);
 
+    if (ctx->is_testing) {
+        cairo_font_options_t *font_options;
+        font_options = get_font_options_for_testing ();
+        pango_cairo_context_set_font_options (context, font_options);
+        cairo_font_options_destroy (font_options);
+    }
+
     return context;
 }
 
diff --git a/rsvg-cairo-render.c b/rsvg-cairo-render.c
index 6f6522e..167bf64 100644
--- a/rsvg-cairo-render.c
+++ b/rsvg-cairo-render.c
@@ -160,6 +160,7 @@ rsvg_cairo_new_drawing_ctx (cairo_t * cr, RsvgHandle * handle)
     draw->pango_context = NULL;
     draw->drawsub_stack = NULL;
     draw->acquired_nodes = NULL;
+    draw->is_testing = handle->priv->is_testing;
 
     rsvg_state_push (draw);
     state = rsvg_current_state (draw);
diff --git a/rsvg-gobject.c b/rsvg-gobject.c
index d0a965d..611b73e 100644
--- a/rsvg-gobject.c
+++ b/rsvg-gobject.c
@@ -95,6 +95,8 @@ rsvg_handle_init (RsvgHandle * self)
 
     self->priv->is_disposed = FALSE;
     self->priv->in_loop = FALSE;
+
+    self->priv->is_testing = FALSE;
 }
 
 static void
diff --git a/rsvg-private.h b/rsvg-private.h
index 3dffc3b..2a92791 100644
--- a/rsvg-private.h
+++ b/rsvg-private.h
@@ -176,6 +176,8 @@ struct RsvgHandlePrivate {
 
     gboolean first_write;
     GInputStream *data_input_stream; /* for rsvg_handle_write of svgz data */
+
+    gboolean is_testing; /* Are we being run from the test suite? */
 };
 
 typedef struct {
@@ -196,6 +198,7 @@ struct RsvgDrawingCtx {
     GSList *vb_stack;
     GSList *drawsub_stack;
     GSList *acquired_nodes;
+    gboolean is_testing;
 };
 
 /*Abstract base class for context for our backends (one as yet)*/
diff --git a/rsvg.h b/rsvg.h
index 25b775a..e4350da 100644
--- a/rsvg.h
+++ b/rsvg.h
@@ -194,6 +194,8 @@ RsvgHandle *rsvg_handle_new_from_stream_sync (GInputStream   *input_stream,
 RsvgHandle *rsvg_handle_new_from_data (const guint8 * data, gsize data_len, GError ** error);
 RsvgHandle *rsvg_handle_new_from_file (const gchar * file_name, GError ** error);
 
+void rsvg_handle_internal_set_testing (RsvgHandle *handle, gboolean testing);
+
 /* BEGIN deprecated APIs. Do not use! */
 
 #ifndef __GI_SCANNER__
diff --git a/tests/rsvg-test.c b/tests/rsvg-test.c
index 7160113..0a86b13 100644
--- a/tests/rsvg-test.c
+++ b/tests/rsvg-test.c
@@ -259,6 +259,8 @@ rsvg_cairo_check (gconstpointer data)
     g_assert_no_error (error);
     g_assert (rsvg != NULL);
 
+    rsvg_handle_internal_set_testing (rsvg, TRUE);
+
     rsvg_handle_get_dimensions (rsvg, &dimensions);
     g_assert (dimensions.width > 0);
     g_assert (dimensions.height > 0);


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