use crate::{
alignment::TextAlignment,
parser::Token,
rendering::{
cursor::Cursor,
line::{StyledLineIterator, UniformSpaceConfig},
StateFactory, StyledTextBoxIterator,
},
style::StyledTextBox,
utils::font_ext::{FontExt, LineMeasurement},
};
use embedded_graphics::{drawable::Pixel, fonts::Font, pixelcolor::PixelColor};
#[derive(Copy, Clone, Debug)]
pub struct RightAligned;
impl TextAlignment for RightAligned {}
#[derive(Debug)]
pub enum RightAlignedState<'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, RightAligned>
where
C: PixelColor,
F: Font + Copy,
{
type PixelIteratorState = RightAlignedState<'a, C, F>;
#[inline]
#[must_use]
fn create_state(&self) -> Self::PixelIteratorState {
RightAlignedState::NextLine(None, Cursor::new(self.text_box.bounds))
}
}
impl<C, F> Iterator for StyledTextBoxIterator<'_, C, F, RightAligned>
where
C: PixelColor,
F: Font + Copy,
{
type Item = Pixel<C>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.state {
RightAlignedState::NextLine(ref carried_token, ref mut cursor) => {
if !cursor.in_display_area() {
break None;
}
if carried_token.is_none() && self.parser.is_empty() {
break None;
}
let max_line_width = cursor.line_width();
let measurement = if let Some(Token::Word(ref w)) = carried_token {
F::measure_line(w.chars(), max_line_width)
} else {
LineMeasurement::empty()
};
let mut space = max_line_width - measurement.width;
if measurement.fits_line {
let mut last_whitespace_width = 0;
for token in self.parser.clone() {
match token {
Token::NewLine => {
break;
}
Token::Whitespace(_) if space == max_line_width => {
}
Token::Whitespace(n) => {
last_whitespace_width =
(n as u32 * F::total_char_width(' ')).min(space);
}
Token::Word(w) => {
let space_with_last_ws = space - last_whitespace_width;
let word_measurement =
F::measure_line(w.chars(), space_with_last_ws);
if word_measurement.fits_line {
space = space_with_last_ws - word_measurement.width;
} else {
if space == max_line_width {
space = max_line_width
- F::measure_line(w.chars(), max_line_width).width;
}
break;
}
}
}
}
}
cursor.carriage_return();
cursor.advance(space);
self.state = RightAlignedState::DrawLine(StyledLineIterator::new(
self.parser.clone(),
*cursor,
UniformSpaceConfig {
starting_spaces: false,
ending_spaces: false,
space_width: F::total_char_width(' '),
},
self.style.text_style,
carried_token.clone(),
));
}
RightAlignedState::DrawLine(ref mut line_iterator) => {
if let pixel @ Some(_) = line_iterator.next() {
break pixel;
}
let mut cursor = line_iterator.cursor;
cursor.new_line();
self.parser = line_iterator.parser.clone();
self.state =
RightAlignedState::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::RightAligned, style::TextBoxStyleBuilder, TextBox};
#[test]
fn simple_render() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(RightAligned)
.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(RightAligned)
.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 word_longer_than_line_wraps_word() {
let mut display = MockDisplay::new();
let style = TextBoxStyleBuilder::new(Font6x8)
.alignment(RightAligned)
.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(RightAligned)
.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(&[
" ...........................................##....##...",
" ............................................#.....#...",
" .####..###..##.#...###..#.##...###...###....#.....#...",
" #.....#...#.#.#.#.#...#.##..#.#...#.....#...#.....#...",
" .###..#...#.#...#.#####.#.....#####..####...#.....#...",
" ....#.#...#.#...#.#.....#.....#.....#...#...#.....#...",
" ####...###..#...#..###..#......###...####..###...###..",
" ......................................................",
" .......##...........................................#.",
" ........#...........................................#.",
" #...#...#....###..#.##...####.#...#..###..#.##...##.#.",
" #...#...#...#...#.##..#.#...#.#...#.#...#.##..#.#..##.",
" #...#...#...#...#.#...#.#...#.#.#.#.#...#.#.....#...#.",
" .####...#...#...#.#...#..####.#.#.#.#...#.#.....#...#.",
" ....#..###...###..#...#.....#..#.#...###..#......####.",
" .###.....................###..........................",
])
);
}
}