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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Text box style builder.
use embedded_graphics::text::LineHeight;

use crate::{
    alignment::{HorizontalAlignment, VerticalAlignment},
    style::{HeightMode, TabSize, TextBoxStyle, VerticalOverdraw},
};

/// [`TextBoxStyle`] builder object.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[must_use]
pub struct TextBoxStyleBuilder {
    style: TextBoxStyle,
    leading_spaces: Option<bool>,
    trailing_spaces: Option<bool>,
}

impl TextBoxStyleBuilder {
    /// Create a new builder object.
    #[inline]
    pub const fn default() -> Self {
        Self::new()
    }

    /// Creates a new text box style builder object.
    #[inline]
    pub const fn new() -> Self {
        Self {
            style: TextBoxStyle {
                alignment: HorizontalAlignment::Left,
                vertical_alignment: VerticalAlignment::Top,
                height_mode: HeightMode::Exact(VerticalOverdraw::FullRowsOnly),
                line_height: LineHeight::Percent(100),
                paragraph_spacing: 0,
                tab_size: TabSize::Spaces(4),
                // we will update these at build time
                leading_spaces: false,
                trailing_spaces: false,
            },
            leading_spaces: None,
            trailing_spaces: None,
        }
    }

    /// Sets the line height.
    ///
    /// The line height is defined as the vertical distance between the baseline of two adjacent lines
    /// of text.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use embedded_text::style::TextBoxStyleBuilder;
    /// # use embedded_graphics::text::LineHeight;
    /// #
    /// let style = TextBoxStyleBuilder::new()
    ///     .line_height(LineHeight::Pixels(12))
    ///     .build();
    /// ```
    #[inline]
    pub const fn line_height(mut self, line_height: LineHeight) -> Self {
        self.style.line_height = line_height;

        self
    }

    /// Sets the paragraph spacing.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use embedded_text::style::TextBoxStyleBuilder;
    /// # use embedded_graphics::text::LineHeight;
    /// #
    /// let style = TextBoxStyleBuilder::new()
    ///     .paragraph_spacing(0)
    ///     .build();
    /// ```
    #[inline]
    pub const fn paragraph_spacing(mut self, paragraph_spacing: u32) -> Self {
        self.style.paragraph_spacing = paragraph_spacing;

        self
    }

    /// Sets the horizontal text alignment.
    #[inline]
    pub const fn alignment(mut self, alignment: HorizontalAlignment) -> TextBoxStyleBuilder {
        self.style.alignment = alignment;

        self
    }

    /// Sets the vertical text alignment.
    #[inline]
    pub const fn vertical_alignment(
        mut self,
        vertical_alignment: VerticalAlignment,
    ) -> TextBoxStyleBuilder {
        self.style.vertical_alignment = vertical_alignment;

        self
    }

    /// Sets the height mode.
    #[inline]
    pub const fn height_mode(mut self, height_mode: HeightMode) -> TextBoxStyleBuilder {
        self.style.height_mode = height_mode;

        self
    }

    /// Sets the tab size.
    #[inline]
    pub const fn tab_size(mut self, tab_size: TabSize) -> Self {
        self.style.tab_size = tab_size;

        self
    }

    /// Render leading spaces.
    #[inline]
    pub const fn leading_spaces(mut self, render: bool) -> Self {
        self.leading_spaces = Some(render);

        self
    }

    /// Render trailing spaces.
    #[inline]
    pub const fn trailing_spaces(mut self, render: bool) -> Self {
        self.trailing_spaces = Some(render);

        self
    }

    /// Builds the [`TextBoxStyle`].
    #[inline]
    pub const fn build(mut self) -> TextBoxStyle {
        self.style.leading_spaces = match self.leading_spaces {
            Some(leading_spaces) => leading_spaces,
            None => self.style.alignment.leading_spaces(),
        };

        self.style.trailing_spaces = match self.trailing_spaces {
            Some(trailing_spaces) => trailing_spaces,
            None => self.style.alignment.trailing_spaces(),
        };

        self.style
    }
}

impl From<&TextBoxStyle> for TextBoxStyleBuilder {
    #[inline]
    fn from(style: &TextBoxStyle) -> Self {
        Self {
            style: *style,
            leading_spaces: None,
            trailing_spaces: None,
        }
    }
}