embedded-text 0.1.0

TextBox for embedded-graphics
Documentation

embedded-text crates.io docs.rs Rust

TextBox for embedded-graphics

This crate provides a configurable TextBox to render multiline text inside a bounding Rectangle using embedded-graphics.

TextBox supports the common text alignments:

  • LeftAligned
  • RightAligned
  • CenterAligned
  • Justified

Example

The examples are based on the embedded-graphics simulator. The simulator is built on top of SDL2. See the simulator README for more information.

embedded-text example with center aligned text

use embedded_graphics_simulator::{
    BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, Window,
};

use embedded_graphics::{fonts::Font6x8, pixelcolor::BinaryColor, prelude::*};

use embedded_text::{alignment::CenterAligned, prelude::*};

fn main() -> Result<(), core::convert::Infallible> {
    let text = "Hello, World!\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";

    let textbox_style = TextBoxStyleBuilder::new(Font6x8)
        .alignment(CenterAligned)
        .text_color(BinaryColor::On)
        .build();

    let height = textbox_style.measure_text_height(text, 129);

    // Create a window just tall enough to fit the text.
    let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(129, height));

    TextBox::new(
        text,
        Rectangle::new(Point::zero(), Point::new(128, height as i32 - 1)),
    )
    .into_styled(textbox_style)
    .draw(&mut display)
    .unwrap();

    let output_settings = OutputSettingsBuilder::new()
        .theme(BinaryColorTheme::OledBlue)
        .build();
    Window::new("Hello center aligned TextBox", &output_settings).show_static(&display);
    Ok(())
}

Development setup

Minimum supported Rust version

The minimum supported Rust version for embedded-text is 1.40.0 or greater. Ensure you have the latest stable version of Rust installed, preferably through https://rustup.rs.

Installation

For setup in general, follow the installation instructions for embedded-graphics.

To install SDL2 on Windows, see https://github.com/Rust-SDL2/rust-sdl2#windows-msvc

Attribution

The example text is copied from https://www.lipsum.com