[librsvg: 1/14] handle: move is_testing to rust



commit 1aabfbb9c5445b362ac9ecd00bc72ad1ad307b4f
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Jan 5 10:47:28 2019 +0100

    handle: move is_testing to rust
    
    Also move pango context creation to text.rs so that DrawingCtx
    does not use pango anymore.

 librsvg/rsvg-handle.c             | 51 ++++++++++++++++-----------------------
 rsvg_internals/src/drawing_ctx.rs | 47 +++---------------------------------
 rsvg_internals/src/handle.rs      | 45 +++++++++++++++-------------------
 rsvg_internals/src/lib.rs         |  1 +
 rsvg_internals/src/text.rs        | 42 ++++++++++++++++++++++++++++++--
 5 files changed, 85 insertions(+), 101 deletions(-)
---
diff --git a/librsvg/rsvg-handle.c b/librsvg/rsvg-handle.c
index 86bb6cd5..707cd052 100644
--- a/librsvg/rsvg-handle.c
+++ b/librsvg/rsvg-handle.c
@@ -153,6 +153,7 @@ extern void rsvg_handle_rust_set_base_url (RsvgHandleRust *raw_handle, const cha
 extern GFile *rsvg_handle_rust_get_base_gfile (RsvgHandleRust *raw_handle);
 extern guint rsvg_handle_rust_get_flags (RsvgHandleRust *raw_handle);
 extern void rsvg_handle_rust_set_flags (RsvgHandleRust *raw_handle, guint flags);
+extern guint rsvg_handle_rust_set_testing (RsvgHandleRust *raw_handle, gboolean testing);
 extern gboolean rsvg_handle_rust_is_at_start_for_setting_base_file (RsvgHandle *handle);
 extern gboolean rsvg_handle_rust_is_loaded (RsvgHandle *handle);
 extern gboolean rsvg_handle_rust_read_stream_sync (RsvgHandle *handle,
@@ -180,8 +181,6 @@ struct RsvgHandlePrivate {
 
     gboolean in_loop;          /* see get_dimension() */
 
-    gboolean is_testing; /* Are we being run from the test suite? */
-
 #ifdef HAVE_PANGOFT2
     FcConfig *font_config_for_testing;
     PangoFontMap *font_map_for_testing;
@@ -216,8 +215,6 @@ rsvg_handle_init (RsvgHandle * self)
 
     self->priv->in_loop = FALSE;
 
-    self->priv->is_testing = FALSE;
-
 #ifdef HAVE_PANGOFT2
     self->priv->font_config_for_testing = NULL;
     self->priv->font_map_for_testing = NULL;
@@ -1320,8 +1317,8 @@ rsvg_handle_set_size_callback (RsvgHandle * handle,
 
 #ifdef HAVE_PANGOFT2
 
-static void
-create_font_config_for_testing (RsvgHandle *handle)
+static FcConfig *
+create_font_config_for_testing (void)
 {
     const char *font_paths[] = {
         "resources/Roboto-Regular.ttf",
@@ -1330,46 +1327,50 @@ create_font_config_for_testing (RsvgHandle *handle)
         "resources/Roboto-BoldItalic.ttf",
     };
 
+    FcConfig *config = FcConfigCreate ();
     int i;
 
-    if (handle->priv->font_config_for_testing != NULL)
-        return;
-
-    handle->priv->font_config_for_testing = FcConfigCreate ();
-
     for (i = 0; i < G_N_ELEMENTS(font_paths); i++) {
         char *font_path = g_test_build_filename (G_TEST_DIST, font_paths[i], NULL);
 
-        if (!FcConfigAppFontAddFile (handle->priv->font_config_for_testing, (const FcChar8 *) font_path)) {
+        if (!FcConfigAppFontAddFile (config, (const FcChar8 *) font_path)) {
             g_error ("Could not load font file \"%s\" for tests; aborting", font_path);
         }
 
         g_free (font_path);
     }
+
+    return config;
 }
 
 #endif
 
 static void
-rsvg_handle_update_font_map_for_testing (RsvgHandle *handle)
+rsvg_handle_update_font_map_for_testing (RsvgHandle *handle, gboolean testing)
 {
 #ifdef HAVE_PANGOFT2
-    if (handle->priv->is_testing) {
-        create_font_config_for_testing (handle);
+    PangoCairoFontMap *font_map = NULL;
+
+    if (testing) {
+        if (handle->priv->font_config_for_testing == NULL) {
+            handle->priv->font_config_for_testing = create_font_config_for_testing ();
+        }
 
         if (handle->priv->font_map_for_testing == NULL) {
             handle->priv->font_map_for_testing = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT);
             pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (handle->priv->font_map_for_testing),
                                           handle->priv->font_config_for_testing);
-
-            pango_cairo_font_map_set_default (PANGO_CAIRO_FONT_MAP (handle->priv->font_map_for_testing));
         }
+
+        font_map = PANGO_CAIRO_FONT_MAP (handle->priv->font_map_for_testing);
     }
+
+    pango_cairo_font_map_set_default (font_map);
 #endif
 }
 
 /**
- * _rsvg_handle_internal_set_testing:
+ * rsvg_handle_internal_set_testing:
  * @handle: a #RsvgHandle
  * @testing: Whether to enable testing mode
  *
@@ -1381,18 +1382,8 @@ 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_handle_update_font_map_for_testing (handle);
-}
-
-G_GNUC_INTERNAL
-gboolean rsvg_handle_get_is_testing (RsvgHandle *handle);
-
-gboolean
-rsvg_handle_get_is_testing (RsvgHandle *handle)
-{
-    return handle->priv->is_testing;
+    rsvg_handle_rust_set_testing (handle->priv->rust_handle, testing);
+    rsvg_handle_update_font_map_for_testing (handle, testing);
 }
 
 /* This one is defined in the C code, because the prototype has varargs
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 515df453..f2398c22 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -2,8 +2,6 @@ use cairo;
 use cairo::MatrixTrait;
 use cairo_sys;
 use glib::translate::*;
-use pango::{self, FontMapExt};
-use pangocairo;
 use std::cell::RefCell;
 use std::rc::{Rc, Weak};
 
@@ -126,8 +124,6 @@ pub struct DrawingCtx {
     drawsub_stack: Vec<RsvgNode>,
 
     acquired_nodes: Rc<RefCell<Vec<RsvgNode>>>,
-
-    is_testing: bool,
 }
 
 impl DrawingCtx {
@@ -139,7 +135,6 @@ impl DrawingCtx {
         vb_width: f64,
         vb_height: f64,
         dpi: Dpi,
-        is_testing: bool,
     ) -> DrawingCtx {
         let mut affine = cr.get_matrix();
         let rect = cairo::Rectangle {
@@ -179,10 +174,13 @@ impl DrawingCtx {
             bbox_stack: Vec::new(),
             drawsub_stack: Vec::new(),
             acquired_nodes: Rc::new(RefCell::new(Vec::new())),
-            is_testing,
         }
     }
 
+    pub fn is_testing(&self) -> bool {
+        handle::is_testing(self.handle)
+    }
+
     pub fn get_cairo_context(&self) -> cairo::Context {
         self.cr.clone()
     }
@@ -595,43 +593,6 @@ impl DrawingCtx {
         Ok(had_paint_server)
     }
 
-    pub fn get_pango_context(&self) -> pango::Context {
-        let font_map = pangocairo::FontMap::get_default().unwrap();
-        let context = font_map.create_context().unwrap();
-        let cr = self.get_cairo_context();
-        pangocairo::functions::update_context(&cr, &context);
-
-        // Pango says this about pango_cairo_context_set_resolution():
-        //
-        //     Sets the resolution for the context. This is a scale factor between
-        //     points specified in a #PangoFontDescription and Cairo units. The
-        //     default value is 96, meaning that a 10 point font will be 13
-        //     units high. (10 * 96. / 72. = 13.3).
-        //
-        // I.e. Pango font sizes in a PangoFontDescription are in *points*, not pixels.
-        // However, we are normalizing everything to userspace units, which amount to
-        // pixels.  So, we will use 72.0 here to make Pango not apply any further scaling
-        // to the size values we give it.
-        //
-        // An alternative would be to divide our font sizes by (dpi_y / 72) to effectively
-        // cancel out Pango's scaling, but it's probably better to deal with Pango-isms
-        // right here, instead of spreading them out through our Length normalization
-        // code.
-        pangocairo::functions::context_set_resolution(&context, 72.0);
-
-        if self.is_testing {
-            let mut options = cairo::FontOptions::new();
-
-            options.set_antialias(cairo::Antialias::Gray);
-            options.set_hint_style(cairo::enums::HintStyle::Full);
-            options.set_hint_metrics(cairo::enums::HintMetrics::On);
-
-            pangocairo::functions::context_set_font_options(&context, &options);
-        }
-
-        context
-    }
-
     pub fn setup_cr_for_stroke(&self, cr: &cairo::Context, values: &ComputedValues) {
         let params = self.get_view_params();
 
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 4095c1b2..f247dc2c 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -79,6 +79,7 @@ pub struct Handle {
     load_options: Cell<LoadOptions>,
     load_state: Cell<LoadState>,
     load: RefCell<Option<LoadContext>>,
+    is_testing: Cell<bool>,
 }
 
 impl Handle {
@@ -90,6 +91,7 @@ impl Handle {
             load_options: Cell::new(LoadOptions::default()),
             load_state: Cell::new(LoadState::Start),
             load: RefCell::new(None),
+            is_testing: Cell::new(false),
         }
     }
 
@@ -211,7 +213,6 @@ impl Handle {
         cr: &cairo::Context,
         dimensions: &RsvgDimensionData,
         node: Option<&RsvgNode>,
-        is_testing: bool,
     ) -> DrawingCtx {
         let mut draw_ctx = DrawingCtx::new(
             handle,
@@ -221,7 +222,6 @@ impl Handle {
             dimensions.em,
             dimensions.ex,
             get_dpi(handle).clone(),
-            is_testing,
         );
 
         if let Some(node) = node {
@@ -256,22 +256,11 @@ impl Handle {
         node: &RsvgNode,
     ) -> Result<(RsvgRectangle, RsvgRectangle), RenderingError> {
         let dimensions = self.get_dimensions(handle)?;
-
         let target = ImageSurface::create(cairo::Format::Rgb24, 1, 1)?;
-
         let cr = cairo::Context::new(&target);
-
-        let mut draw_ctx = self.create_drawing_ctx_for_node(
-            handle,
-            &cr,
-            &dimensions,
-            Some(node),
-            is_testing(handle),
-        );
-
+        let mut draw_ctx = self.create_drawing_ctx_for_node(handle, &cr, &dimensions, Some(node));
         let svg_ref = self.svg.borrow();
         let svg = svg_ref.as_ref().unwrap();
-
         let root = svg.tree.root();
 
         draw_ctx.draw_node_from_stack(&root.get_cascaded_values(), &root, false)?;
@@ -413,17 +402,10 @@ impl Handle {
 
         cr.save();
 
-        let mut draw_ctx = self.create_drawing_ctx_for_node(
-            handle,
-            cr,
-            &dimensions,
-            node.as_ref(),
-            is_testing(handle),
-        );
+        let mut draw_ctx = self.create_drawing_ctx_for_node(handle, cr, &dimensions, node.as_ref());
 
         let svg_ref = self.svg.borrow();
         let svg = svg_ref.as_ref().unwrap();
-
         let root = svg.tree.root();
 
         let res = draw_ctx.draw_node_from_stack(&root.get_cascaded_values(), &root, false);
@@ -523,13 +505,14 @@ extern "C" {
 
     fn rsvg_handle_get_rust(handle: *const RsvgHandle) -> *mut Handle;
 
-    fn rsvg_handle_get_is_testing(handle: *const RsvgHandle) -> glib_sys::gboolean;
-
     fn rsvg_handle_get_dimensions(handle: *mut RsvgHandle, dimensions: *mut RsvgDimensionData);
 }
 
-fn is_testing(handle: *const RsvgHandle) -> bool {
-    unsafe { from_glib(rsvg_handle_get_is_testing(handle)) }
+/// Whether we are being run from the test suite
+pub fn is_testing(handle: *const RsvgHandle) -> bool {
+    let rhandle = get_rust_handle(handle);
+
+    rhandle.is_testing.get()
 }
 
 pub fn lookup_node(handle: *const RsvgHandle, fragment: &Fragment) -> Option<Rc<Node>> {
@@ -813,6 +796,16 @@ pub unsafe extern "C" fn rsvg_handle_rust_set_flags(raw_handle: *const Handle, f
     rhandle.load_options.set(LoadOptions::from_flags(flags));
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn rsvg_handle_rust_set_testing(
+    raw_handle: *const Handle,
+    testing: glib_sys::gboolean,
+) {
+    let rhandle = &*raw_handle;
+
+    rhandle.is_testing.set(from_glib(testing));
+}
+
 #[no_mangle]
 pub unsafe extern "C" fn rsvg_handle_rust_is_at_start_for_setting_base_file(
     handle: *const RsvgHandle,
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index af092b02..60303056 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -55,6 +55,7 @@ pub use handle::{
     rsvg_handle_rust_set_dpi_x,
     rsvg_handle_rust_set_dpi_y,
     rsvg_handle_rust_set_flags,
+    rsvg_handle_rust_set_testing,
     rsvg_handle_rust_write,
 };
 
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index 1c929e3c..ef7187a0 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -1,6 +1,7 @@
 use glib::translate::*;
-use pango::{self, ContextExt, LayoutExt};
+use pango::{self, ContextExt, FontMapExt, LayoutExt};
 use pango_sys;
+use pangocairo;
 use std::cell::{Cell, RefCell};
 
 use attributes::Attribute;
@@ -894,12 +895,49 @@ impl From<WritingMode> for pango::Gravity {
     }
 }
 
+fn get_pango_context(cr: &cairo::Context, is_testing: bool) -> pango::Context {
+    let font_map = pangocairo::FontMap::get_default().unwrap();
+    let context = font_map.create_context().unwrap();
+    pangocairo::functions::update_context(&cr, &context);
+
+    // Pango says this about pango_cairo_context_set_resolution():
+    //
+    //     Sets the resolution for the context. This is a scale factor between
+    //     points specified in a #PangoFontDescription and Cairo units. The
+    //     default value is 96, meaning that a 10 point font will be 13
+    //     units high. (10 * 96. / 72. = 13.3).
+    //
+    // I.e. Pango font sizes in a PangoFontDescription are in *points*, not pixels.
+    // However, we are normalizing everything to userspace units, which amount to
+    // pixels.  So, we will use 72.0 here to make Pango not apply any further scaling
+    // to the size values we give it.
+    //
+    // An alternative would be to divide our font sizes by (dpi_y / 72) to effectively
+    // cancel out Pango's scaling, but it's probably better to deal with Pango-isms
+    // right here, instead of spreading them out through our Length normalization
+    // code.
+    pangocairo::functions::context_set_resolution(&context, 72.0);
+
+    if is_testing {
+        let mut options = cairo::FontOptions::new();
+
+        options.set_antialias(cairo::Antialias::Gray);
+        options.set_hint_style(cairo::enums::HintStyle::Full);
+        options.set_hint_metrics(cairo::enums::HintMetrics::On);
+
+        pangocairo::functions::context_set_font_options(&context, &options);
+    }
+
+    context
+}
+
 fn create_pango_layout(
     draw_ctx: &DrawingCtx,
     values: &ComputedValues,
     text: &str,
 ) -> pango::Layout {
-    let pango_context = draw_ctx.get_pango_context();
+    let cr = draw_ctx.get_cairo_context();
+    let pango_context = get_pango_context(&cr, draw_ctx.is_testing());
 
     // See the construction of the XmlLang property
     // We use "" there as the default value; this means that the language is not set.


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