embedded-text 0.7.3

TextBox for embedded-graphics
Documentation
use embedded_graphics::{
    geometry::Point,
    mock_display::MockDisplay,
    mono_font::{ascii::FONT_6X9, MonoTextStyle},
    pixelcolor::BinaryColor,
    primitives::Rectangle,
    Drawable,
};

use crate::{
    alignment::HorizontalAlignment,
    rendering::test::{assert_rendered, assert_styled_rendered},
    style::{TextBoxStyle, TextBoxStyleBuilder},
    utils::test::size_for,
    TextBox,
};

#[test]
fn simple_render() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "word",
        size_for(&FONT_6X9, 6, 1),
        &[
            "........................",
            "......................#.",
            "......................#.",
            "#...#...##...#.#....###.",
            "#.#.#..#..#..##.#..#..#.",
            "#.#.#..#..#..#.....#..#.",
            ".#.#....##...#......###.",
            "........................",
            "........................",
        ],
    );
}

#[test]
fn simple_render_cr() {
    let mut display = MockDisplay::new();
    display.set_allow_overdraw(true);

    let character_style = MonoTextStyle::new(&FONT_6X9, BinaryColor::On);
    let style = TextBoxStyle::with_alignment(HorizontalAlignment::Justified);

    TextBox::with_textbox_style(
        "O\rX",
        Rectangle::new(Point::zero(), size_for(&FONT_6X9, 1, 1)),
        character_style,
        style,
    )
    .draw(&mut display)
    .unwrap();

    display.assert_pattern(&[
        "         ",
        "#####    ",
        "## ##    ",
        "# # #    ",
        "# # #    ",
        "## ##    ",
        "#####    ",
    ]);
}

#[test]
fn wrapping_when_space_is_less_than_space_character() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "A word",
        size_for(&FONT_6X9, 5, 1),
        &[
            "......            ",
            "..#...            ",
            ".#.#..            ",
            "#...#.            ",
            "#####.            ",
            "#...#.            ",
            "#...#.            ",
            "......            ",
            "......            ",
        ],
    );
}

#[test]
fn simple_word_wrapping() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "word wrapping",
        size_for(&FONT_6X9, 9, 2),
        &[
            "........................                        ",
            "......................#.                        ",
            "......................#.                        ",
            "#...#...##...#.#....###.                        ",
            "#.#.#..#..#..##.#..#..#.                        ",
            "#.#.#..#..#..#.....#..#.                        ",
            ".#.#....##...#......###.                        ",
            "........................                        ",
            "........................                        ",
            "................................................",
            "................................#...............",
            "................................................",
            "#...#..#.#....###..###...###...##....###....##..",
            "#.#.#..##.#..#..#..#..#..#..#...#....#..#..#..#.",
            "#.#.#..#.....#..#..#..#..#..#...#....#..#..#..#.",
            ".#.#...#......###..###...###...###...#..#...###.",
            "...................#.....#....................#.",
            "...................#.....#..................##..",
        ],
    );
}

#[test]
fn simple_word_wrapping_with_spaces() {
    assert_styled_rendered(
        TextBoxStyleBuilder::new()
            .alignment(HorizontalAlignment::Justified)
            .leading_spaces(true)
            .trailing_spaces(true)
            .build(),
        " word wrapping",
        size_for(&FONT_6X9, 9, 2),
        &[
            "......................................................",
            ".....................................#................",
            ".....................................#................",
            "...............#...#...##...#.#....###................",
            "...............#.#.#..#..#..##.#..#..#................",
            "...............#.#.#..#..#..#.....#..#................",
            "................#.#....##...#......###................",
            "......................................................",
            "......................................................",
            "................................................      ",
            "................................#...............      ",
            "................................................      ",
            "#...#..#.#....###..###...###...##....###....##..      ",
            "#.#.#..##.#..#..#..#..#..#..#...#....#..#..#..#.      ",
            "#.#.#..#.....#..#..#..#..#..#...#....#..#..#..#.      ",
            ".#.#...#......###..###...###...###...#..#...###.      ",
            "...................#.....#....................#.      ",
            "...................#.....#..................##..      ",
        ],
    );
}

#[test]
fn justified_alignment() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "word and other word last line",
        size_for(&FONT_6X9, 10, 3),
        &[
            "............................................................",
            "......................#...................................#.",
            "......................#...................................#.",
            "#...#...##...#.#....###.....................###..###....###.",
            "#.#.#..#..#..##.#..#..#....................#..#..#..#..#..#.",
            "#.#.#..#..#..#.....#..#....................#..#..#..#..#..#.",
            ".#.#....##...#......###.....................###..#..#...###.",
            "............................................................",
            "............................................................",
            "............................................................",
            "........#....#............................................#.",
            "........#....#............................................#.",
            "..##...###...###....##...#.#........#...#...##...#.#....###.",
            ".#..#...#....#..#..#.##..##.#.......#.#.#..#..#..##.#..#..#.",
            ".#..#...#.#..#..#..##....#..........#.#.#..#..#..#.....#..#.",
            "..##.....#...#..#...###..#...........#.#....##...#......###.",
            "............................................................",
            "............................................................",
            "......................................................      ",
            ".##.................#..........##.....#...............      ",
            "..#.................#...........#.....................      ",
            "..#.....###...###..###..........#....##....###....##..      ",
            "..#....#..#..##.....#...........#.....#....#..#..#.##.      ",
            "..#....#..#....##...#.#.........#.....#....#..#..##...      ",
            ".###....###..###.....#.........###...###...#..#...###.      ",
            "......................................................      ",
            "......................................................      ",
        ],
    );
}

#[test]
fn word_longer_than_line_wraps_word() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "word somereallylongword",
        size_for(&FONT_6X9, 9, 3),
        &[
            "........................                              ",
            "......................#.                              ",
            "......................#.                              ",
            "#...#...##...#.#....###.                              ",
            "#.#.#..#..#..##.#..#..#.                              ",
            "#.#.#..#..#..#.....#..#.                              ",
            ".#.#....##...#......###.                              ",
            "........................                              ",
            "........................                              ",
            "......................................................",
            "...........................................##....##...",
            "............................................#.....#...",
            "..###...##..##.#....##...#.#....##....###...#.....#...",
            ".##....#..#.#.#.#..#.##..##.#..#.##..#..#...#.....#...",
            "...##..#..#.#.#.#..##....#.....##....#..#...#.....#...",
            ".###....##..#...#...###..#......###...###..###...###..",
            "......................................................",
            "......................................................",
            "......................................................",
            ".......##...........................................#.",
            "........#...........................................#.",
            ".#..#...#.....##...###....##..#...#...##...#.#....###.",
            ".#..#...#....#..#..#..#..#..#.#.#.#..#..#..##.#..#..#.",
            ".#..#...#....#..#..#..#..#..#.#.#.#..#..#..#.....#..#.",
            "..###..###....##...#..#...###..#.#....##...#......###.",
            ".#..#.......................#.........................",
            "..##......................##..........................",
        ],
    );
}

#[test]
fn first_word_longer_than_line_wraps_word() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "somereallylongword",
        size_for(&FONT_6X9, 9, 2),
        &[
            "......................................................",
            "...........................................##....##...",
            "............................................#.....#...",
            "..###...##..##.#....##...#.#....##....###...#.....#...",
            ".##....#..#.#.#.#..#.##..##.#..#.##..#..#...#.....#...",
            "...##..#..#.#.#.#..##....#.....##....#..#...#.....#...",
            ".###....##..#...#...###..#......###...###..###...###..",
            "......................................................",
            "......................................................",
            "......................................................",
            ".......##...........................................#.",
            "........#...........................................#.",
            ".#..#...#.....##...###....##..#...#...##...#.#....###.",
            ".#..#...#....#..#..#..#..#..#.#.#.#..#..#..##.#..#..#.",
            ".#..#...#....#..#..#..#..#..#.#.#.#..#..#..#.....#..#.",
            "..###..###....##...#..#...###..#.#....##...#......###.",
            ".#..#.......................#.........................",
            "..##......................##..........................",
        ],
    );
}

#[test]
fn soft_hyphen_rendering() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "soft\u{AD}hyphen",
        size_for(&FONT_6X9, 6, 2),
        &[
            "..............................      ",
            "...............#....#.........      ",
            "..............#.#...#.........      ",
            "..###...##....#....###........      ",
            ".##....#..#..###....#...#####.      ",
            "...##..#..#...#.....#.#.......      ",
            ".###....##....#......#........      ",
            "..............................      ",
            "..............................      ",
            "....................................",
            ".#.................#................",
            ".#.................#................",
            ".###...#..#..###...###....##...###..",
            ".#..#..#..#..#..#..#..#..#.##..#..#.",
            ".#..#..#..#..#..#..#..#..##....#..#.",
            ".#..#...###..###...#..#...###..#..#.",
            ".......#..#..#......................",
            "........##...#......................",
        ],
    );
}

#[test]
fn wrapped_soft_hyphen_rendering() {
    assert_rendered(
        HorizontalAlignment::Justified,
        "soft\u{AD}hyp",
        size_for(&FONT_6X9, 4, 2),
        &[
            "........................",
            "...............#....#...",
            "..............#.#...#...",
            "..###...##....#....###..",
            ".##....#..#..###....#...",
            "...##..#..#...#.....#.#.",
            ".###....##....#......#..",
            "........................",
            "........................",
            "........................",
            ".......#................",
            ".......#................",
            ".......###...#..#..###..",
            "#####..#..#..#..#..#..#.",
            ".......#..#..#..#..#..#.",
            ".......#..#...###..###..",
            ".............#..#..#....",
            "..............##...#....",
        ],
    );
}

#[test]
fn tab_rendering() {
    // Expect \t to render as 3 space characters, ignored by the justified alignment.
    assert_rendered(
        HorizontalAlignment::Justified,
        "a\ttab + two te xt words",
        size_for(&FONT_6X9, 10, 3),
        &[
            "............................................................",
            "..........................#..........#......................",
            "..........................#..........#..................#...",
            "..###....................###....###..###................#...",
            ".#..#.....................#....#..#..#..#.............#####.",
            ".#..#.....................#.#..#..#..#..#...............#...",
            "..###......................#....###..###................#...",
            "............................................................",
            "............................................................",
            "............................................................",
            "..#..........................#..........................#...",
            "..#..........................#..........................#...",
            ".###..#...#...##............###....##............#..#..###..",
            "..#...#.#.#..#..#............#....#.##............##....#...",
            "..#.#.#.#.#..#..#............#.#..##..............##....#.#.",
            "...#...#.#....##..............#....###...........#..#....#..",
            "............................................................",
            "............................................................",
            "..............................                              ",
            "......................#.......                              ",
            "......................#.......                              ",
            "#...#...##...#.#....###...###.                              ",
            "#.#.#..#..#..##.#..#..#..##...                              ",
            "#.#.#..#..#..#.....#..#....##.                              ",
            ".#.#....##...#......###..###..                              ",
            "..............................                              ",
            "..............................                              ",
        ],
    );
}