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::Center,
        "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::Center);

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

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

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

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

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

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

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