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
//! Text box style builder.
use embedded_graphics::text::LineHeight;

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

/// [`TextBoxStyle`] builder object.
///
/// [`TextBoxStyle`]: struct.TextBoxStyle.html
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[must_use]
pub struct TextBoxStyleBuilder {
    style: TextBoxStyle,
}

impl Default for TextBoxStyleBuilder {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl TextBoxStyleBuilder {
    /// 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),
            },
        }
    }

    /// 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
    }

    /// Builds the [`TextBoxStyle`].
    ///
    /// [`TextBoxStyle`]: struct.TextBoxStyle.html
    #[inline]
    pub const fn build(self) -> TextBoxStyle {
        self.style
    }
}

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