1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::text::{Alignment, Baseline, LineHeight};

/// Text style.
///
/// A text style is used to set how text lines are laid out in a text drawable.
///
/// Use [`TextStyleBuilder`] to build a text style object.
///
/// See the [module-level documentation] for more information about text styles and examples.
///
/// [`TextStyleBuilder`]: struct.TextStyleBuilder.html
/// [module-level documentation]: index.html
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub struct TextStyle {
    /// Horizontal text alignment.
    pub alignment: Alignment,

    /// Text baseline.
    pub baseline: Baseline,

    /// Line height.
    pub line_height: LineHeight,
}

impl TextStyle {
    /// Creates a new text style with the given baseline.
    pub const fn with_baseline(baseline: Baseline) -> Self {
        TextStyleBuilder::new().baseline(baseline).build()
    }

    /// Creates a new text style with the given alignment.
    pub const fn with_alignment(alignment: Alignment) -> Self {
        TextStyleBuilder::new().alignment(alignment).build()
    }
}

impl Default for TextStyle {
    fn default() -> Self {
        TextStyleBuilder::new().build()
    }
}

/// Builder for text styles.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct TextStyleBuilder {
    style: TextStyle,
}

impl TextStyleBuilder {
    /// Creates a new text style builder.
    pub const fn new() -> Self {
        Self {
            style: TextStyle {
                alignment: Alignment::Left,
                baseline: Baseline::Alphabetic,
                line_height: LineHeight::Percent(100),
            },
        }
    }
}

impl TextStyleBuilder {
    /// Sets the horizontal text alignment.
    pub const fn alignment(mut self, alignment: Alignment) -> Self {
        self.style.alignment = alignment;

        self
    }

    /// Sets the text baseline.
    pub const fn baseline(mut self, baseline: Baseline) -> Self {
        self.style.baseline = baseline;

        self
    }

    /// Sets the line height.
    pub const fn line_height(mut self, line_height: LineHeight) -> Self {
        self.style.line_height = line_height;

        self
    }

    /// Builds the text style.
    pub const fn build(self) -> TextStyle {
        self.style
    }
}

impl From<&TextStyle> for TextStyleBuilder {
    fn from(style: &TextStyle) -> Self {
        Self { style: *style }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn builder() {
        let text_style = TextStyleBuilder::new()
            .alignment(Alignment::Right)
            .baseline(Baseline::Top)
            .line_height(LineHeight::Pixels(123))
            .build();

        assert_eq!(text_style.alignment, Alignment::Right);
        assert_eq!(text_style.baseline, Baseline::Top);
        assert_eq!(text_style.line_height, LineHeight::Pixels(123));
    }

    #[test]
    fn builder_default() {
        let text_style = TextStyleBuilder::new().build();

        assert_eq!(text_style.alignment, Alignment::Left);
        assert_eq!(text_style.baseline, Baseline::Alphabetic);
        assert_eq!(text_style.line_height, LineHeight::Percent(100));
    }
}