use crate::{
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
parser::{Parser, Token},
rendering::{
cursor::Cursor,
line::{StyledLineIterator, UniformSpaceConfig},
StateFactory, StyledTextBoxIterator,
},
style::StyledTextBox,
utils::font_ext::FontExt,
};
use embedded_graphics::prelude::*;
#[derive(Copy, Clone, Debug)]
pub struct CenterAligned;
impl HorizontalTextAlignment for CenterAligned {
const STARTING_SPACES: bool = false;
const ENDING_SPACES: bool = false;
}
#[derive(Debug)]
pub enum State<'a, C, F>
where
C: PixelColor,
F: Font + Copy,
{
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig, CenterAligned>),
}
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, CenterAligned, V>
where
C: PixelColor,
F: Font + Copy,
V: VerticalTextAlignment,
{
type PixelIteratorState = State<'a, C, F>;
#[inline]
#[must_use]
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
State::NextLine(None, cursor, parser)
}
}
impl<C, F, V> Iterator for StyledTextBoxIterator<'_, C, F, CenterAligned, V>
where
C: PixelColor,
F: Font + Copy,
V: VerticalTextAlignment,
{
type Item = Pixel<C>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.state {
State::NextLine(ref carried_token, mut cursor, ref mut parser) => {
if !cursor.in_display_area() {
break None;
}
if carried_token.is_none() && parser.is_empty() {
break None;
}
let parser_clone = parser.clone();
let max_line_width = cursor.line_width();
let (width, _, _) =
self.style
.measure_line(parser, carried_token.clone(), max_line_width);
cursor.advance_unchecked((max_line_width - width + 1) / 2);
self.state = State::DrawLine(StyledLineIterator::new(
parser_clone,
cursor,
UniformSpaceConfig {
space_width: F::total_char_width(' '),
},
self.style.text_style,
carried_token.clone(),
));
}
State::DrawLine(ref mut line_iterator) => {
if let pixel @ Some(_) = line_iterator.next() {
break pixel;
}
let carried_token = line_iterator.remaining_token();
self.state = State::NextLine(
carried_token,
line_iterator.cursor,
line_iterator.parser.clone(),
);
}
}
}
}
}
impl VerticalTextAlignment for CenterAligned {
#[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) / 2;
cursor.position.y += offset as i32;
}
}
#[cfg(test)]
mod test_horizontal {
use embedded_graphics::{
fonts::Font6x8, mock_display::MockDisplay, pixelcolor::BinaryColor, prelude::*,
primitives::Rectangle,
};
use crate::{alignment::CenterAligned, style::TextBoxStyleBuilder, TextBox};
#[test]
fn simple_render() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(CenterAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new("word", Rectangle::new(Point::zero(), Point::new(54, 7)))
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
" ......................#. ",
" ......................#. ",
" #...#..###..#.##...##.#. ",
" #...#.#...#.##..#.#..##. ",
" #.#.#.#...#.#.....#...#. ",
" #.#.#.#...#.#.....#...#. ",
" .#.#...###..#......####. ",
" ........................ ",
])
);
}
#[test]
fn simple_render_cr() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(CenterAligned)
.text_color(BinaryColor::On)
.build();
TextBox::new("O\rX", Rectangle::new(Point::zero(), Point::new(54, 7)))
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
" ##### ",
" # # ",
" ## ## ",
" # # # ",
" ## ## ",
" # # ",
" ##### ",
])
);
}
#[test]
fn simple_word_wrapping() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(CenterAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"word wrapping",
Rectangle::new(Point::zero(), Point::new(54, 15)),
)
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
" ......................#. ",
" ......................#. ",
" #...#..###..#.##...##.#. ",
" #...#.#...#.##..#.#..##. ",
" #.#.#.#...#.#.....#...#. ",
" #.#.#.#...#.#.....#...#. ",
" .#.#...###..#......####. ",
" ........................ ",
" ................................#............... ",
" ................................................ ",
" #...#.#.##...###..####..####...##...#.##...####. ",
" #...#.##..#.....#.#...#.#...#...#...##..#.#...#. ",
" #.#.#.#......####.#...#.#...#...#...#...#.#...#. ",
" #.#.#.#.....#...#.####..####....#...#...#..####. ",
" .#.#..#......####.#.....#......###..#...#.....#. ",
" ..................#.....#..................###.. "
])
);
}
#[test]
fn word_longer_than_line_wraps_word() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(CenterAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"word somereallylongword",
Rectangle::new(Point::zero(), Point::new(54, 23)),
)
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
" ......................#. ",
" ......................#. ",
" #...#..###..#.##...##.#. ",
" #...#.#...#.##..#.#..##. ",
" #.#.#.#...#.#.....#...#. ",
" #.#.#.#...#.#.....#...#. ",
" .#.#...###..#......####. ",
" ........................ ",
" ...........................................##....##...",
" ............................................#.....#...",
" .####..###..##.#...###..#.##...###...###....#.....#...",
" #.....#...#.#.#.#.#...#.##..#.#...#.....#...#.....#...",
" .###..#...#.#...#.#####.#.....#####..####...#.....#...",
" ....#.#...#.#...#.#.....#.....#.....#...#...#.....#...",
" ####...###..#...#..###..#......###...####..###...###..",
" ......................................................",
" .......##...........................................#.",
" ........#...........................................#.",
" #...#...#....###..#.##...####.#...#..###..#.##...##.#.",
" #...#...#...#...#.##..#.#...#.#...#.#...#.##..#.#..##.",
" #...#...#...#...#.#...#.#...#.#.#.#.#...#.#.....#...#.",
" .####...#...#...#.#...#..####.#.#.#.#...#.#.....#...#.",
" ....#..###...###..#...#.....#..#.#...###..#......####.",
" .###.....................###..........................",
])
);
}
#[test]
fn first_word_longer_than_line_wraps_word() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(CenterAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"somereallylongword",
Rectangle::new(Point::zero(), Point::new(54, 15)),
)
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
" ...........................................##....##...",
" ............................................#.....#...",
" .####..###..##.#...###..#.##...###...###....#.....#...",
" #.....#...#.#.#.#.#...#.##..#.#...#.....#...#.....#...",
" .###..#...#.#...#.#####.#.....#####..####...#.....#...",
" ....#.#...#.#...#.#.....#.....#.....#...#...#.....#...",
" ####...###..#...#..###..#......###...####..###...###..",
" ......................................................",
" .......##...........................................#.",
" ........#...........................................#.",
" #...#...#....###..#.##...####.#...#..###..#.##...##.#.",
" #...#...#...#...#.##..#.#...#.#...#.#...#.##..#.#..##.",
" #...#...#...#...#.#...#.#...#.#.#.#.#...#.#.....#...#.",
" .####...#...#...#.#...#..####.#.#.#.#...#.#.....#...#.",
" ....#..###...###..#...#.....#..#.#...###..#......####.",
" .###.....................###..........................",
])
);
}
}
#[cfg(test)]
mod test_vertical {
use embedded_graphics::{
fonts::Font6x8, mock_display::MockDisplay, pixelcolor::BinaryColor, prelude::*,
primitives::Rectangle,
};
use crate::{alignment::CenterAligned, style::TextBoxStyleBuilder, TextBox};
#[test]
fn test_center_alignment() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.vertical_alignment(CenterAligned)
.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(&[
" ",
" ",
" ",
" ",
"......................#.",
"......................#.",
"#...#..###..#.##...##.#.",
"#...#.#...#.##..#.#..##.",
"#.#.#.#...#.#.....#...#.",
"#.#.#.#...#.#.....#...#.",
".#.#...###..#......####.",
"........................",
" ",
" ",
" ",
" ",
])
);
}
}