Skip to main content

ferroui_base/media/
text_options.rs

1use crate::media::{BaselinePixelAlignment, TextHintingMode, TextRenderingMode};
2use crate::Visual;
3
4/// Provides options for controlling text rendering and hinting.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
6pub struct TextOptions {
7    /// Gets the text rendering mode.
8    pub text_rendering_mode: TextRenderingMode,
9    /// Gets the text hinting mode.
10    pub text_hinting_mode: TextHintingMode,
11    /// Gets a value indicating whether the text baseline should be aligned to
12    /// the pixel grid.
13    pub baseline_pixel_alignment: BaselinePixelAlignment,
14}
15
16impl TextOptions {
17    /// Merges this instance with `other`: every unspecified value of `self`
18    /// is taken from `other`.
19    pub fn merge_with(&self, other: TextOptions) -> TextOptions {
20        let mut text_rendering_mode = self.text_rendering_mode;
21
22        if text_rendering_mode == TextRenderingMode::Unspecified {
23            text_rendering_mode = other.text_rendering_mode;
24        }
25
26        let mut text_hinting_mode = self.text_hinting_mode;
27
28        if text_hinting_mode == TextHintingMode::Unspecified {
29            text_hinting_mode = other.text_hinting_mode;
30        }
31
32        let mut baseline_pixel_alignment = self.baseline_pixel_alignment;
33
34        if baseline_pixel_alignment == BaselinePixelAlignment::Unspecified {
35            baseline_pixel_alignment = other.baseline_pixel_alignment;
36        }
37
38        TextOptions { text_rendering_mode, text_hinting_mode, baseline_pixel_alignment }
39    }
40
41    /// Gets the text options of a visual.
42    pub fn get_text_options(visual: &Visual) -> TextOptions {
43        visual.text_options()
44    }
45
46    /// Sets the text options of a visual.
47    pub fn set_text_options(visual: &Visual, value: TextOptions) {
48        visual.set_text_options(value);
49    }
50
51    /// Gets the value of the `TextRenderingMode` text option for a visual.
52    pub fn get_text_rendering_mode(visual: &Visual) -> TextRenderingMode {
53        visual.text_options().text_rendering_mode
54    }
55
56    /// Sets the value of the `TextRenderingMode` text option for a visual.
57    pub fn set_text_rendering_mode(visual: &Visual, value: TextRenderingMode) {
58        visual.set_text_options(TextOptions { text_rendering_mode: value, ..visual.text_options() });
59    }
60
61    /// Gets the value of the `TextHintingMode` text option for a visual.
62    pub fn get_text_hinting_mode(visual: &Visual) -> TextHintingMode {
63        visual.text_options().text_hinting_mode
64    }
65
66    /// Sets the value of the `TextHintingMode` text option for a visual.
67    pub fn set_text_hinting_mode(visual: &Visual, value: TextHintingMode) {
68        visual.set_text_options(TextOptions { text_hinting_mode: value, ..visual.text_options() });
69    }
70
71    /// Gets the value of the `BaselinePixelAlignment` text option for a
72    /// visual.
73    pub fn get_baseline_pixel_alignment(visual: &Visual) -> BaselinePixelAlignment {
74        visual.text_options().baseline_pixel_alignment
75    }
76
77    /// Sets the value of the `BaselinePixelAlignment` text option for a
78    /// visual.
79    pub fn set_baseline_pixel_alignment(visual: &Visual, value: BaselinePixelAlignment) {
80        visual.set_text_options(TextOptions { baseline_pixel_alignment: value, ..visual.text_options() });
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn merge_with_takes_unspecified_values_from_other() {
90        let a = TextOptions { text_hinting_mode: TextHintingMode::Light, ..Default::default() };
91        let b = TextOptions {
92            text_rendering_mode: TextRenderingMode::Antialias,
93            text_hinting_mode: TextHintingMode::Strong,
94            baseline_pixel_alignment: BaselinePixelAlignment::Aligned,
95        };
96
97        let merged = a.merge_with(b);
98
99        assert_eq!(merged.text_rendering_mode, TextRenderingMode::Antialias);
100        assert_eq!(merged.text_hinting_mode, TextHintingMode::Light);
101        assert_eq!(merged.baseline_pixel_alignment, BaselinePixelAlignment::Aligned);
102    }
103}