[librsvg] drawing_ctx: use From trait to obtain the pango context



commit 529ff0e95474d92b871310bde29da6fbb0993c1f
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Aug 28 17:02:20 2020 +0200

    drawing_ctx: use From trait to obtain the pango context

 rsvg_internals/src/drawing_ctx.rs | 44 +++++++++++++++++++++++++++++++++++----
 rsvg_internals/src/text.rs        | 40 +----------------------------------
 2 files changed, 41 insertions(+), 43 deletions(-)
---
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 9c0f16e4..43fbb442 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -2,6 +2,7 @@
 
 use float_cmp::approx_eq;
 use once_cell::sync::Lazy;
+use pango::FontMapExt;
 use regex::{Captures, Regex};
 use std::borrow::Cow;
 use std::cell::RefCell;
@@ -167,10 +168,6 @@ impl DrawingCtx {
         self.measuring
     }
 
-    pub fn is_testing(&self) -> bool {
-        self.testing
-    }
-
     pub fn get_cairo_context(&self) -> cairo::Context {
         self.cr.clone()
     }
@@ -1661,3 +1658,42 @@ impl From<TextRendering> for cairo::Antialias {
         }
     }
 }
+
+impl From<&DrawingCtx> for pango::Context {
+    fn from(draw_ctx: &DrawingCtx) -> pango::Context {
+        let cr = draw_ctx.get_cairo_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 draw_ctx.testing {
+            let mut options = cairo::FontOptions::new();
+
+            options.set_antialias(cairo::Antialias::Gray);
+            options.set_hint_style(cairo::HintStyle::Full);
+            options.set_hint_metrics(cairo::HintMetrics::On);
+
+            pangocairo::functions::context_set_font_options(&context, Some(&options));
+        }
+
+        context
+    }
+}
diff --git a/rsvg_internals/src/text.rs b/rsvg_internals/src/text.rs
index 016e7503..d0fda5fd 100644
--- a/rsvg_internals/src/text.rs
+++ b/rsvg_internals/src/text.rs
@@ -1,7 +1,6 @@
 //! Text elements: `text`, `tspan`, `tref`.
 
 use markup5ever::{expanded_name, local_name, namespace_url, ns};
-use pango::FontMapExt;
 use std::cell::RefCell;
 
 use crate::allowed_url::Fragment;
@@ -743,49 +742,12 @@ 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::HintStyle::Full);
-        options.set_hint_metrics(cairo::HintMetrics::On);
-
-        pangocairo::functions::context_set_font_options(&context, Some(&options));
-    }
-
-    context
-}
-
 fn create_pango_layout(
     draw_ctx: &DrawingCtx,
     values: &ComputedValues,
     text: &str,
 ) -> pango::Layout {
-    let cr = draw_ctx.get_cairo_context();
-    let pango_context = get_pango_context(&cr, draw_ctx.is_testing());
+    let pango_context = pango::Context::from(draw_ctx);
 
     // 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]