Skip to main content

ferroui_base/media/text_formatting/
generic_text_run_properties.rs

1use std::cell::OnceCell;
2use std::rc::Rc;
3
4use crate::media::text_formatting::TextRunProperties;
5use crate::media::{
6    BaselineAlignment, FontFeatureCollection, GlyphTypeface, IBrush, TextDecorationCollection, Typeface,
7};
8use crate::utilities::CultureInfo;
9
10/// Generic implementation of `TextRunProperties`.
11pub struct GenericTextRunProperties {
12    typeface: Typeface,
13    font_rendering_em_size: f64,
14    text_decorations: Option<TextDecorationCollection>,
15    foreground_brush: Option<Rc<dyn IBrush>>,
16    background_brush: Option<Rc<dyn IBrush>>,
17    font_features: Option<FontFeatureCollection>,
18    baseline_alignment: BaselineAlignment,
19    culture_info: Option<CultureInfo>,
20    cached_glyph_typeface: OnceCell<Rc<GlyphTypeface>>,
21}
22
23impl GenericTextRunProperties {
24    // Internal upstream; public so the Skia unit tests reach it.
25    pub const DEFAULT_FONT_RENDERING_EM_SIZE: f64 = 12.0;
26
27    /// Creates properties for a typeface with the default em size (12) and
28    /// nothing else set.
29    pub fn new(typeface: Typeface) -> Self {
30        Self::with_font_size(typeface, Self::DEFAULT_FONT_RENDERING_EM_SIZE)
31    }
32
33    /// Creates properties for a typeface and em size.
34    pub fn with_font_size(typeface: Typeface, font_rendering_em_size: f64) -> Self {
35        Self::with_all(typeface, font_rendering_em_size, None, None, None, BaselineAlignment::Baseline, None, None)
36    }
37
38    /// Creates properties with every value specified.
39    #[allow(clippy::too_many_arguments)]
40    pub fn with_all(
41        typeface: Typeface,
42        font_rendering_em_size: f64,
43        text_decorations: Option<TextDecorationCollection>,
44        foreground_brush: Option<Rc<dyn IBrush>>,
45        background_brush: Option<Rc<dyn IBrush>>,
46        baseline_alignment: BaselineAlignment,
47        culture_info: Option<CultureInfo>,
48        font_features: Option<FontFeatureCollection>,
49    ) -> Self {
50        Self {
51            typeface,
52            font_rendering_em_size,
53            text_decorations,
54            foreground_brush,
55            background_brush,
56            font_features,
57            baseline_alignment,
58            culture_info,
59            cached_glyph_typeface: OnceCell::new(),
60        }
61    }
62}
63
64impl TextRunProperties for GenericTextRunProperties {
65    fn typeface(&self) -> &Typeface {
66        &self.typeface
67    }
68
69    fn font_rendering_em_size(&self) -> f64 {
70        self.font_rendering_em_size
71    }
72
73    fn text_decorations(&self) -> Option<&TextDecorationCollection> {
74        self.text_decorations.as_ref()
75    }
76
77    fn foreground_brush(&self) -> Option<&Rc<dyn IBrush>> {
78        self.foreground_brush.as_ref()
79    }
80
81    fn background_brush(&self) -> Option<&Rc<dyn IBrush>> {
82        self.background_brush.as_ref()
83    }
84
85    fn font_features(&self) -> Option<&FontFeatureCollection> {
86        self.font_features.as_ref()
87    }
88
89    fn baseline_alignment(&self) -> BaselineAlignment {
90        self.baseline_alignment
91    }
92
93    fn culture_info(&self) -> Option<&CultureInfo> {
94        self.culture_info.as_ref()
95    }
96
97    fn cached_glyph_typeface(&self) -> Rc<GlyphTypeface> {
98        self.cached_glyph_typeface.get_or_init(|| self.typeface.glyph_typeface()).clone()
99    }
100}