embedded_text/style/
builder.rs

1//! Text box style builder.
2use embedded_graphics::text::LineHeight;
3
4use crate::{
5    alignment::{HorizontalAlignment, VerticalAlignment},
6    style::{HeightMode, TabSize, TextBoxStyle, VerticalOverdraw},
7};
8
9/// [`TextBoxStyle`] builder object.
10#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
11#[must_use]
12pub struct TextBoxStyleBuilder {
13    style: TextBoxStyle,
14    leading_spaces: Option<bool>,
15    trailing_spaces: Option<bool>,
16}
17
18impl TextBoxStyleBuilder {
19    /// Create a new builder object.
20    #[inline]
21    pub const fn default() -> Self {
22        Self::new()
23    }
24
25    /// Creates a new text box style builder object.
26    #[inline]
27    pub const fn new() -> Self {
28        Self {
29            style: TextBoxStyle {
30                alignment: HorizontalAlignment::Left,
31                vertical_alignment: VerticalAlignment::Top,
32                height_mode: HeightMode::Exact(VerticalOverdraw::FullRowsOnly),
33                line_height: LineHeight::Percent(100),
34                paragraph_spacing: 0,
35                tab_size: TabSize::Spaces(4),
36                // we will update these at build time
37                leading_spaces: false,
38                trailing_spaces: false,
39            },
40            leading_spaces: None,
41            trailing_spaces: None,
42        }
43    }
44
45    /// Sets the line height.
46    ///
47    /// The line height is defined as the vertical distance between the baseline of two adjacent lines
48    /// of text.
49    ///
50    /// # Example
51    ///
52    /// ```rust
53    /// # use embedded_text::style::TextBoxStyleBuilder;
54    /// # use embedded_graphics::text::LineHeight;
55    /// #
56    /// let style = TextBoxStyleBuilder::new()
57    ///     .line_height(LineHeight::Pixels(12))
58    ///     .build();
59    /// ```
60    #[inline]
61    pub const fn line_height(mut self, line_height: LineHeight) -> Self {
62        self.style.line_height = line_height;
63
64        self
65    }
66
67    /// Sets the paragraph spacing.
68    ///
69    /// # Example
70    ///
71    /// ```rust
72    /// # use embedded_text::style::TextBoxStyleBuilder;
73    /// # use embedded_graphics::text::LineHeight;
74    /// #
75    /// let style = TextBoxStyleBuilder::new()
76    ///     .paragraph_spacing(0)
77    ///     .build();
78    /// ```
79    #[inline]
80    pub const fn paragraph_spacing(mut self, paragraph_spacing: u32) -> Self {
81        self.style.paragraph_spacing = paragraph_spacing;
82
83        self
84    }
85
86    /// Sets the horizontal text alignment.
87    #[inline]
88    pub const fn alignment(mut self, alignment: HorizontalAlignment) -> TextBoxStyleBuilder {
89        self.style.alignment = alignment;
90
91        self
92    }
93
94    /// Sets the vertical text alignment.
95    #[inline]
96    pub const fn vertical_alignment(
97        mut self,
98        vertical_alignment: VerticalAlignment,
99    ) -> TextBoxStyleBuilder {
100        self.style.vertical_alignment = vertical_alignment;
101
102        self
103    }
104
105    /// Sets the height mode.
106    #[inline]
107    pub const fn height_mode(mut self, height_mode: HeightMode) -> TextBoxStyleBuilder {
108        self.style.height_mode = height_mode;
109
110        self
111    }
112
113    /// Sets the tab size.
114    #[inline]
115    pub const fn tab_size(mut self, tab_size: TabSize) -> Self {
116        self.style.tab_size = tab_size;
117
118        self
119    }
120
121    /// Render leading spaces.
122    #[inline]
123    pub const fn leading_spaces(mut self, render: bool) -> Self {
124        self.leading_spaces = Some(render);
125
126        self
127    }
128
129    /// Render trailing spaces.
130    #[inline]
131    pub const fn trailing_spaces(mut self, render: bool) -> Self {
132        self.trailing_spaces = Some(render);
133
134        self
135    }
136
137    /// Builds the [`TextBoxStyle`].
138    #[inline]
139    pub const fn build(mut self) -> TextBoxStyle {
140        self.style.leading_spaces = match self.leading_spaces {
141            Some(leading_spaces) => leading_spaces,
142            None => self.style.alignment.leading_spaces(),
143        };
144
145        self.style.trailing_spaces = match self.trailing_spaces {
146            Some(trailing_spaces) => trailing_spaces,
147            None => self.style.alignment.trailing_spaces(),
148        };
149
150        self.style
151    }
152}
153
154impl From<&TextBoxStyle> for TextBoxStyleBuilder {
155    #[inline]
156    fn from(style: &TextBoxStyle) -> Self {
157        Self {
158            style: *style,
159            leading_spaces: None,
160            trailing_spaces: None,
161        }
162    }
163}