Struct embedded_text::TextBox

source ·
pub struct TextBox<'a, S, M = NoPlugin<<S as TextRenderer>::Color>>where
    S: TextRenderer,{
    pub text: &'a str,
    pub bounds: Rectangle,
    pub character_style: S,
    pub style: TextBoxStyle,
    pub vertical_offset: i32,
    /* private fields */
}
Expand description

A text box object.

The TextBox object can be used to draw text on a draw target. It is meant to be a more feature-rich alternative to Text in embedded-graphics.

To construct a TextBox object at least a text string, a bounding box and character style are required. For advanced formatting options an additional TextBoxStyle object might be used. For more information about text box styling, see the documentation of the style module.

Text rendering in embedded-graphics is designed to be extendable by text renderers for different font formats. embedded-text follows this philosophy by using the same text renderer infrastructure. To use a text renderer in an embedded-text project each renderer provides a character style object. See the embedded-graphics documentation for more information on text renderers and character styling.

Plugins

The feature set of TextBox can be extended by plugins. Plugins can be used to implement optional features which are not essential to the core functionality of embedded-text.

Use the add_plugin method to add a plugin to the TextBox object. Multiple plugins can be used at the same time. Plugins are applied in the reverse order they are added. Note that some plugins may interfere with others if used together or not in the expected order.

If you need to extract data from plugins after the text box has been rendered, you can use the take_plugins method.

See the list of built-in plugins in the plugin module.

Note: Implementing custom plugins is experimental and require enabling the plugin feature.

Example: advanced text styling using the ANSI plugin

use embedded_text::{TextBox, plugin::ansi::Ansi};
TextBox::new(
    "Some \x1b[4munderlined\x1b[24m text",
    bounding_box,
    character_style,
)
.add_plugin(Ansi::new())
.draw(&mut display)?;

Vertical offsetting

You can use the set_vertical_offset method to move the text inside the text box. Vertical offset is applied after all vertical measurements and alignments. This can be useful to scroll text in a fixed text box. Setting a positive value moves the text down.

Residual text

If the text does not fit the given bounding box, the draw method returns the part which was not processed. The return value can be used to flow text into multiple text boxes.

Fields§

§text: &'a str

The text to be displayed in this TextBox

§bounds: Rectangle

The bounding box of this TextBox

§character_style: S

The character style of the TextBox.

§style: TextBoxStyle

The style of the TextBox.

§vertical_offset: i32

Vertical offset applied to the text just before rendering.

Implementations§

source§

impl<'a, S> TextBox<'a, S, NoPlugin<<S as TextRenderer>::Color>>where S: TextRenderer + CharacterStyle,

source

pub fn new(text: &'a str, bounds: Rectangle, character_style: S) -> Self

Creates a new TextBox instance with a given bounding Rectangle.

source

pub fn with_textbox_style( text: &'a str, bounds: Rectangle, character_style: S, textbox_style: TextBoxStyle ) -> Self

Creates a new TextBox instance with a given bounding Rectangle and a given TextBoxStyle.

source

pub fn with_alignment( text: &'a str, bounds: Rectangle, character_style: S, alignment: HorizontalAlignment ) -> Self

Creates a new TextBox instance with a given bounding Rectangle and a default TextBoxStyle with the given horizontal alignment.

source

pub fn with_vertical_alignment( text: &'a str, bounds: Rectangle, character_style: S, vertical_alignment: VerticalAlignment ) -> Self

Creates a new TextBox instance with a given bounding Rectangle and a default TextBoxStyle and the given vertical alignment.

source

pub fn with_height_mode( text: &'a str, bounds: Rectangle, character_style: S, mode: HeightMode ) -> Self

Creates a new TextBox instance with a given bounding Rectangle and a default TextBoxStyle and the given height mode.

source

pub fn with_line_height( text: &'a str, bounds: Rectangle, character_style: S, line_height: LineHeight ) -> Self

Creates a new TextBox instance with a given bounding Rectangle and a default TextBoxStyle and the given line height.

source

pub fn with_paragraph_spacing( text: &'a str, bounds: Rectangle, character_style: S, spacing: u32 ) -> Self

Creates a new TextBox instance with a given bounding Rectangle and a default TextBoxStyle and the given paragraph spacing.

source

pub fn with_tab_size( text: &'a str, bounds: Rectangle, character_style: S, tab_size: TabSize ) -> Self

Creates a new TextBox instance with a given bounding Rectangle and a default TextBoxStyle and the given tab size.

source

pub fn set_vertical_offset(&mut self, offset: i32) -> &mut Self

Sets the vertical text offset.

Vertical offset changes the vertical position of the displayed text within the bounding box. Setting a positive value moves the text down.

source

pub fn add_plugin<M>(self, plugin: M) -> TextBox<'a, S, Chain<M>>where M: Plugin<'a, <S as TextRenderer>::Color>,

Adds a new plugin to the TextBox.

source§

impl<'a, S, P> TextBox<'a, S, P>where S: TextRenderer + CharacterStyle, P: Plugin<'a, <S as TextRenderer>::Color> + ChainElement,

source

pub fn add_plugin<M>(self, plugin: M) -> TextBox<'a, S, Link<M, P>>where M: Plugin<'a, <S as TextRenderer>::Color>,

Adds a new plugin to the TextBox.

source

pub fn take_plugins(self) -> P

Deconstruct the text box and return the plugins.

Trait Implementations§

source§

impl<'a, S, M: Clone> Clone for TextBox<'a, S, M>where S: TextRenderer + Clone, S::Color: Clone,

source§

fn clone(&self) -> TextBox<'a, S, M>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, S, M: Debug> Debug for TextBox<'a, S, M>where S: TextRenderer + Debug, S::Color: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, S, M> Dimensions for TextBox<'a, S, M>where S: TextRenderer, M: Plugin<'a, S::Color>,

source§

fn bounding_box(&self) -> Rectangle

Returns the bounding box.
source§

impl<'a, F, M> Drawable for TextBox<'a, F, M>where F: TextRenderer<Color = <F as CharacterStyle>::Color> + CharacterStyle, M: Plugin<'a, <F as TextRenderer>::Color> + Plugin<'a, <F as CharacterStyle>::Color>, <F as CharacterStyle>::Color: Default,

§

type Color = <F as CharacterStyle>::Color

The pixel color type.
§

type Output = &'a str

The return type of the draw method. Read more
source§

fn draw<D: DrawTarget<Color = Self::Color>>( &self, display: &mut D ) -> Result<&'a str, D::Error>

Draw the graphics object using the supplied DrawTarget.
source§

impl<'a, S, M: Hash> Hash for TextBox<'a, S, M>where S: TextRenderer + Hash, S::Color: Hash,

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, S, M> Transform for TextBox<'a, S, M>where S: TextRenderer + Clone, M: Plugin<'a, S::Color>,

source§

fn translate(&self, by: Point) -> Self

Move the origin of an object by a given number of (x, y) pixels, returning a new object
source§

fn translate_mut(&mut self, by: Point) -> &mut Self

Move the origin of an object by a given number of (x, y) pixels, mutating the object in place

Auto Trait Implementations§

§

impl<'a, S, M = NoPlugin<<S as TextRenderer>::Color>> !RefUnwindSafe for TextBox<'a, S, M>

§

impl<'a, S, M> Send for TextBox<'a, S, M>where M: Send, S: Send, <S as TextRenderer>::Color: Send,

§

impl<'a, S, M = NoPlugin<<S as TextRenderer>::Color>> !Sync for TextBox<'a, S, M>

§

impl<'a, S, M> Unpin for TextBox<'a, S, M>where M: Unpin, S: Unpin, <S as TextRenderer>::Color: Unpin,

§

impl<'a, S, M> UnwindSafe for TextBox<'a, S, M>where M: UnwindSafe, S: UnwindSafe, <S as TextRenderer>::Color: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dstwhere T: Cast<Dst>,

Casts the value.
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dstwhere Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dstwhere T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dstwhere T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.