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
use crate::{
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
rendering::cursor::Cursor,
style::StyledTextBox,
};
use embedded_graphics::prelude::*;
#[derive(Copy, Clone)]
pub struct BottomAligned;
impl VerticalTextAlignment for BottomAligned {
#[inline]
fn apply_vertical_alignment<'a, C, F, A>(
cursor: &mut Cursor<F>,
styled_text_box: &'a StyledTextBox<'a, C, F, A, Self>,
) where
C: PixelColor,
F: Font + Copy,
A: HorizontalTextAlignment,
{
let text_height = styled_text_box
.style
.measure_text_height(styled_text_box.text_box.text, cursor.line_width());
let box_height = styled_text_box.size().height;
let offset = box_height - text_height;
cursor.position.y += offset as i32
}
}
#[cfg(test)]
mod test {
use embedded_graphics::{
fonts::Font6x8, mock_display::MockDisplay, pixelcolor::BinaryColor, prelude::*,
primitives::Rectangle,
};
use crate::{alignment::BottomAligned, style::TextBoxStyleBuilder, TextBox};
#[test]
fn test_bottom_alignment() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.vertical_alignment(BottomAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new("word", Rectangle::new(Point::zero(), Point::new(54, 15)))
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"......................#.",
"......................#.",
"#...#..###..#.##...##.#.",
"#...#.#...#.##..#.#..##.",
"#.#.#.#...#.#.....#...#.",
"#.#.#.#...#.#.....#...#.",
".#.#...###..#......####.",
"........................",
])
);
}
}