[librsvg: 10/12] layout::FontProperties - extract the font-related properties from ComputedValues here




commit 36dcbd0e500ec56328ef85729c1e746217b0228b
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Jun 3 17:44:54 2021 -0500

    layout::FontProperties - extract the font-related properties from ComputedValues here

 src/layout.rs | 41 +++++++++++++++++++++++++++++++++++++++--
 src/text.rs   | 53 ++++++++++++++++++++++++-----------------------------
 2 files changed, 63 insertions(+), 31 deletions(-)
---
diff --git a/src/layout.rs b/src/layout.rs
index 23835d1e..2586cf1e 100644
--- a/src/layout.rs
+++ b/src/layout.rs
@@ -9,14 +9,16 @@ use crate::coord_units::CoordUnits;
 use crate::dasharray::Dasharray;
 use crate::document::AcquiredNodes;
 use crate::element::Element;
+use crate::font_props::{FontFamily, FontWeight};
 use crate::length::*;
 use crate::node::*;
 use crate::paint_server::PaintSource;
 use crate::path_builder::Path;
 use crate::properties::ComputedValues;
 use crate::property_defs::{
-    ClipRule, FillRule, Filter, MixBlendMode, Opacity, Overflow, PaintOrder, ShapeRendering,
-    StrokeDasharray, StrokeLinecap, StrokeLinejoin, StrokeMiterlimit, TextRendering,
+    ClipRule, Direction, FillRule, Filter, FontStretch, FontStyle, FontVariant, MixBlendMode,
+    Opacity, Overflow, PaintOrder, ShapeRendering, StrokeDasharray, StrokeLinecap, StrokeLinejoin,
+    StrokeMiterlimit, TextDecoration, TextRendering, UnicodeBidi, WritingMode, XmlLang,
 };
 use crate::rect::Rect;
 use crate::surface_utils::shared_surface::SharedImageSurface;
@@ -98,6 +100,22 @@ pub struct TextSpan {
     pub text_rendering: TextRendering,
 }
 
+/// Font-related properties extracted from `ComputedValues`.
+pub struct FontProperties {
+    pub xml_lang: XmlLang,
+    pub writing_mode: WritingMode,
+    pub unicode_bidi: UnicodeBidi,
+    pub direction: Direction,
+    pub font_family: FontFamily,
+    pub font_style: FontStyle,
+    pub font_variant: FontVariant,
+    pub font_weight: FontWeight,
+    pub font_stretch: FontStretch,
+    pub font_size: f64,
+    pub letter_spacing: f64,
+    pub text_decoration: TextDecoration,
+}
+
 impl StackingContext {
     pub fn new(
         acquired_nodes: &mut AcquiredNodes<'_>,
@@ -213,3 +231,22 @@ impl Stroke {
         }
     }
 }
+
+impl FontProperties {
+    pub fn new(values: &ComputedValues, params: &NormalizeParams) -> FontProperties {
+        FontProperties {
+            xml_lang: values.xml_lang(),
+            writing_mode: values.writing_mode(),
+            unicode_bidi: values.unicode_bidi(),
+            direction: values.direction(),
+            font_family: values.font_family(),
+            font_style: values.font_style(),
+            font_variant: values.font_variant(),
+            font_weight: values.font_weight(),
+            font_stretch: values.font_stretch(),
+            font_size: values.font_size().to_user(params),
+            letter_spacing: values.letter_spacing().to_user(params),
+            text_decoration: values.text_decoration(),
+        }
+    }
+}
diff --git a/src/text.rs b/src/text.rs
index f5ec19db..1c77b238 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -10,13 +10,14 @@ use crate::drawing_ctx::DrawingCtx;
 use crate::element::{Draw, Element, ElementResult, SetAttributes};
 use crate::error::*;
 use crate::font_props::FontWeight;
-use crate::layout::{self, StackingContext, Stroke};
+use crate::layout::{self, FontProperties, StackingContext, Stroke};
 use crate::length::*;
 use crate::node::{CascadedValues, Node, NodeBorrow};
 use crate::parsers::ParseValue;
 use crate::properties::ComputedValues;
 use crate::property_defs::{
-    Direction, FontStretch, FontStyle, FontVariant, TextAnchor, UnicodeBidi, WritingMode, XmlSpace,
+    Direction, FontStretch, FontStyle, FontVariant, TextAnchor, UnicodeBidi, WritingMode, XmlLang,
+    XmlSpace,
 };
 use crate::space::{xml_space_normalize, NormalizeDefault, XmlSpaceNormalize};
 use crate::xml::Attributes;
@@ -189,7 +190,11 @@ impl MeasuredSpan {
     fn from_span(span: &Span, draw_ctx: &DrawingCtx) -> MeasuredSpan {
         let values = span.values.clone();
 
-        let layout = create_pango_layout(draw_ctx, &*values, &span.text);
+        let view_params = draw_ctx.get_view_params();
+        let params = NormalizeParams::new(&values, &view_params);
+
+        let properties = FontProperties::new(&values, &params);
+        let layout = create_pango_layout(draw_ctx, &properties, &span.text);
         let (w, h) = layout.get_size();
 
         let w = f64::from(w) / f64::from(pango::SCALE);
@@ -807,22 +812,18 @@ impl From<WritingMode> for pango::Gravity {
     }
 }
 
-fn create_pango_layout(
-    draw_ctx: &DrawingCtx,
-    values: &ComputedValues,
-    text: &str,
-) -> pango::Layout {
+fn create_pango_layout(draw_ctx: &DrawingCtx, props: &FontProperties, text: &str) -> pango::Layout {
     let pango_context = pango::Context::from(draw_ctx);
 
-    if let Some(ref lang) = values.xml_lang().0 {
+    if let XmlLang(Some(ref lang)) = props.xml_lang {
         pango_context.set_language(&pango::Language::from_string(lang));
     }
 
-    pango_context.set_base_gravity(pango::Gravity::from(values.writing_mode()));
+    pango_context.set_base_gravity(pango::Gravity::from(props.writing_mode));
 
-    match (values.unicode_bidi(), values.direction()) {
+    match (props.unicode_bidi, props.direction) {
         (UnicodeBidi::Override, _) | (UnicodeBidi::Embed, _) => {
-            pango_context.set_base_dir(pango::Direction::from(values.direction()));
+            pango_context.set_base_dir(pango::Direction::from(props.direction));
         }
 
         (_, direction) if direction != Direction::Ltr => {
@@ -830,21 +831,18 @@ fn create_pango_layout(
         }
 
         (_, _) => {
-            pango_context.set_base_dir(pango::Direction::from(values.writing_mode()));
+            pango_context.set_base_dir(pango::Direction::from(props.writing_mode));
         }
     }
 
     let mut font_desc = pango_context.get_font_description().unwrap();
-    font_desc.set_family(values.font_family().as_str());
-    font_desc.set_style(pango::Style::from(values.font_style()));
-    font_desc.set_variant(pango::Variant::from(values.font_variant()));
-    font_desc.set_weight(pango::Weight::from(values.font_weight()));
-    font_desc.set_stretch(pango::Stretch::from(values.font_stretch()));
-
-    let view_params = draw_ctx.get_view_params();
-    let params = NormalizeParams::new(values, &view_params);
+    font_desc.set_family(props.font_family.as_str());
+    font_desc.set_style(pango::Style::from(props.font_style));
+    font_desc.set_variant(pango::Variant::from(props.font_variant));
+    font_desc.set_weight(pango::Weight::from(props.font_weight));
+    font_desc.set_stretch(pango::Stretch::from(props.font_stretch));
 
-    font_desc.set_size(to_pango_units(values.font_size().to_user(&params)));
+    font_desc.set_size(to_pango_units(props.font_size));
 
     let layout = pango::Layout::new(&pango_context);
     layout.set_auto_dir(false);
@@ -865,22 +863,19 @@ fn create_pango_layout(
     let attr_list = pango::AttrList::new();
 
     attr_list.insert(
-        pango::Attribute::new_letter_spacing(to_pango_units(
-            values.letter_spacing().to_user(&params),
-        ))
-        .unwrap(),
+        pango::Attribute::new_letter_spacing(to_pango_units(props.letter_spacing)).unwrap(),
     );
 
-    if values.text_decoration().underline {
+    if props.text_decoration.underline {
         attr_list.insert(pango::Attribute::new_underline(pango::Underline::Single).unwrap());
     }
 
-    if values.text_decoration().strike {
+    if props.text_decoration.strike {
         attr_list.insert(pango::Attribute::new_strikethrough(true).unwrap());
     }
 
     layout.set_attributes(Some(&attr_list));
-    layout.set_alignment(pango::Alignment::from(values.direction()));
+    layout.set_alignment(pango::Alignment::from(props.direction));
     layout.set_text(text);
 
     layout


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