use crate::{
alignment::TextAlignment,
parser::Token,
rendering::{
cursor::Cursor,
line::{StyledLineIterator, UniformSpaceConfig},
StateFactory, StyledTextBoxIterator,
},
style::StyledTextBox,
utils::font_ext::FontExt,
};
use embedded_graphics::{drawable::Pixel, fonts::Font, pixelcolor::PixelColor};
#[derive(Copy, Clone, Debug)]
pub struct LeftAligned;
impl TextAlignment for LeftAligned {}
#[derive(Debug)]
pub enum LeftAlignedState<'a, C, F>
where
C: PixelColor,
F: Font + Copy,
{
NextLine(Option<Token<'a>>, Cursor<F>),
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig>),
}
impl<'a, C, F> StateFactory for StyledTextBox<'a, C, F, LeftAligned>
where
C: PixelColor,
F: Font + Copy,
{
type PixelIteratorState = LeftAlignedState<'a, C, F>;
#[inline]
#[must_use]
fn create_state(&self) -> Self::PixelIteratorState {
LeftAlignedState::NextLine(None, Cursor::new(self.text_box.bounds))
}
}
impl<C, F> Iterator for StyledTextBoxIterator<'_, C, F, LeftAligned>
where
C: PixelColor,
F: Font + Copy,
{
type Item = Pixel<C>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.state {
LeftAlignedState::NextLine(ref carried_token, ref cursor) => {
if !cursor.in_display_area() {
break None;
}
if carried_token.is_none() && self.parser.is_empty() {
break None;
}
self.state = LeftAlignedState::DrawLine(StyledLineIterator::new(
self.parser.clone(),
*cursor,
UniformSpaceConfig {
starting_spaces: true,
ending_spaces: true,
space_width: F::total_char_width(' '),
},
self.style.text_style,
carried_token.clone(),
));
}
LeftAlignedState::DrawLine(ref mut line_iterator) => {
if let pixel @ Some(_) = line_iterator.next() {
break pixel;
}
let mut cursor = line_iterator.cursor;
cursor.new_line();
cursor.carriage_return();
self.parser = line_iterator.parser.clone();
self.state =
LeftAlignedState::NextLine(line_iterator.remaining_token(), cursor);
}
};
}
}
}
#[cfg(test)]
mod test {
use embedded_graphics::{
fonts::Font6x8, mock_display::MockDisplay, pixelcolor::BinaryColor, prelude::*,
primitives::Rectangle,
};
use crate::{alignment::LeftAligned, style::TextBoxStyleBuilder, TextBox};
#[test]
fn simple_render() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(LeftAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new("word", Rectangle::new(Point::zero(), Point::new(54, 54)))
.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(LeftAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"word wrapping",
Rectangle::new(Point::zero(), Point::new(54, 54)),
)
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
"......................#....... ",
"......................#....... ",
"#...#..###..#.##...##.#....... ",
"#...#.#...#.##..#.#..##....... ",
"#.#.#.#...#.#.....#...#....... ",
"#.#.#.#...#.#.....#...#....... ",
".#.#...###..#......####....... ",
".............................. ",
"................................#...............",
"................................................",
"#...#.#.##...###..####..####...##...#.##...####.",
"#...#.##..#.....#.#...#.#...#...#...##..#.#...#.",
"#.#.#.#......####.#...#.#...#...#...#...#.#...#.",
"#.#.#.#.....#...#.####..####....#...#...#..####.",
".#.#..#......####.#.....#......###..#...#.....#.",
"..................#.....#..................###.."
])
);
}
#[test]
fn simple_word_wrapping_by_space() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(LeftAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"wrapping word",
Rectangle::new(Point::zero(), Point::new(47, 54)),
)
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
"................................#...............",
"................................................",
"#...#.#.##...###..####..####...##...#.##...####.",
"#...#.##..#.....#.#...#.#...#...#...##..#.#...#.",
"#.#.#.#......####.#...#.#...#...#...#...#.#...#.",
"#.#.#.#.....#...#.####..####....#...#...#..####.",
".#.#..#......####.#.....#......###..#...#.....#.",
"..................#.....#..................###..",
"......................#. ",
"......................#. ",
"#...#..###..#.##...##.#. ",
"#...#.#...#.##..#.#..##. ",
"#.#.#.#...#.#.....#...#. ",
"#.#.#.#...#.#.....#...#. ",
".#.#...###..#......####. ",
"........................ ",
])
);
}
#[test]
fn whitespace_word_wrapping() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(LeftAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"word wrap",
Rectangle::new(Point::zero(), Point::new(30, 54)),
)
.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(LeftAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"word somereallylongword",
Rectangle::new(Point::zero(), Point::new(54, 54)),
)
.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(LeftAligned)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
.build();
TextBox::new(
"somereallylongword",
Rectangle::new(Point::zero(), Point::new(54, 54)),
)
.into_styled(style)
.draw(&mut display)
.unwrap();
assert_eq!(
display,
MockDisplay::from_pattern(&[
"...........................................##....##...",
"............................................#.....#...",
".####..###..##.#...###..#.##...###...###....#.....#...",
"#.....#...#.#.#.#.#...#.##..#.#...#.....#...#.....#...",
".###..#...#.#...#.#####.#.....#####..####...#.....#...",
"....#.#...#.#...#.#.....#.....#.....#...#...#.....#...",
"####...###..#...#..###..#......###...####..###...###..",
"......................................................",
".......##...........................................#.",
"........#...........................................#.",
"#...#...#....###..#.##...####.#...#..###..#.##...##.#.",
"#...#...#...#...#.##..#.#...#.#...#.#...#.##..#.#..##.",
"#...#...#...#...#.#...#.#...#.#.#.#.#...#.#.....#...#.",
".####...#...#...#.#...#..####.#.#.#.#...#.#.....#...#.",
"....#..###...###..#...#.....#..#.#...###..#......####.",
".###.....................###..........................",
])
);
}
}