[librsvg: 4/13] create_pango_context() - Take the font options as an argument




commit 871927575d807c17b72b632cf978b10e1321cc4e
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Nov 4 11:58:08 2021 -0600

    create_pango_context() - Take the font options as an argument
    
    Do not pick them up from the drawing_ctx.cr.
    
    Now we have an opaque FontOptions type, which wraps a cairo::FontOptions.
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/626>

 src/drawing_ctx.rs | 40 ++++++++++++++++++++++++++++++++++------
 src/text.rs        |  8 ++++++--
 2 files changed, 40 insertions(+), 8 deletions(-)
---
diff --git a/src/drawing_ctx.rs b/src/drawing_ctx.rs
index 95f4bac5e..8270cdced 100644
--- a/src/drawing_ctx.rs
+++ b/src/drawing_ctx.rs
@@ -2,7 +2,9 @@
 
 use cssparser::RGBA;
 use float_cmp::approx_eq;
+use glib::translate::*;
 use once_cell::sync::Lazy;
+use pango::ffi::PangoMatrix;
 use pango::prelude::FontMapExt;
 use regex::{Captures, Regex};
 use std::borrow::Cow;
@@ -95,6 +97,13 @@ impl Drop for ViewParams {
     }
 }
 
+/// Opaque font options for a DrawingCtx.
+///
+/// This is used for DrawingCtx::create_pango_context.
+pub struct FontOptions {
+    options: cairo::FontOptions,
+}
+
 #[derive(Debug, Copy, Clone, PartialEq)]
 pub enum ClipMode {
     ClipToViewport,
@@ -1750,10 +1759,10 @@ impl DrawingCtx {
         }
     }
 
-    /// Create a Pango context based on the cr and `testing` flag from the DrawingCtx.
-    pub fn create_pango_context(&self) -> pango::Context {
-        let cr = self.cr.clone();
-
+    /// Extracts the font options for the current state of the DrawingCtx.
+    ///
+    /// You can use the font options later with create_pango_context().
+    pub fn get_font_options(&self) -> FontOptions {
         let mut options = cairo::FontOptions::new().unwrap();
         if self.testing {
             options.set_antialias(cairo::Antialias::Gray);
@@ -1762,14 +1771,33 @@ impl DrawingCtx {
         options.set_hint_style(cairo::HintStyle::None);
         options.set_hint_metrics(cairo::HintMetrics::Off);
 
-        cr.set_font_options(&options);
+        FontOptions { options }
+    }
 
+    /// Create a Pango context based on the cr and `testing` flag from the DrawingCtx.
+    pub fn create_pango_context(&self, font_options: &FontOptions) -> pango::Context {
         let font_map = pangocairo::FontMap::default().unwrap();
         let context = font_map.create_context().unwrap();
 
         context.set_round_glyph_positions(false);
 
-        pangocairo::functions::update_context(&cr, &context);
+        let transform = self.get_transform();
+
+        let pango_matrix = PangoMatrix {
+            xx: transform.xx,
+            xy: transform.xy,
+            yx: transform.yx,
+            yy: transform.yy,
+            x0: transform.x0,
+            y0: transform.y0,
+        };
+
+        let pango_matrix_ptr: *const PangoMatrix = &pango_matrix;
+
+        let matrix = unsafe { pango::Matrix::from_glib_none(pango_matrix_ptr) };
+        context.set_matrix(Some(&matrix));
+
+        pangocairo::functions::context_set_font_options(&context, Some(&font_options.options));
 
         // Pango says this about pango_cairo_context_set_resolution():
         //
diff --git a/src/text.rs b/src/text.rs
index 75192de49..ffe1698e8 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -7,7 +7,7 @@ use std::rc::Rc;
 
 use crate::bbox::BoundingBox;
 use crate::document::{AcquiredNodes, NodeId};
-use crate::drawing_ctx::{DrawingCtx, ViewParams};
+use crate::drawing_ctx::{DrawingCtx, FontOptions, ViewParams};
 use crate::element::{Draw, Element, ElementResult, SetAttributes};
 use crate::error::*;
 use crate::layout::{self, FontProperties, StackingContext, Stroke, TextSpan};
@@ -31,6 +31,9 @@ struct LayoutContext {
 
     /// Current transform in the DrawingCtx.
     transform: Transform,
+
+    /// Font options from the DrawingCtx.
+    font_options: FontOptions,
 }
 
 /// An absolutely-positioned array of `Span`s
@@ -767,6 +770,7 @@ impl Draw for Text {
                 let layout_context = LayoutContext {
                     writing_mode: values.writing_mode(),
                     transform: dc.get_transform(),
+                    font_options: dc.get_font_options(),
                 };
 
                 let mut x = self.x.to_user(&params);
@@ -1186,7 +1190,7 @@ fn create_pango_layout(
     props: &FontProperties,
     text: &str,
 ) -> pango::Layout {
-    let pango_context = draw_ctx.create_pango_context();
+    let pango_context = draw_ctx.create_pango_context(&layout_context.font_options);
 
     if let XmlLang(Some(ref lang)) = props.xml_lang {
         pango_context.set_language(&pango::Language::from_string(lang.as_str()));


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