[−][src]Module embedded_graphics::fonts
Monospaced bitmap fonts.
This module contains support for drawing monospaced bitmap fonts and provides several built-in fonts.
Additional custom fonts can be added by the application or other crates. This
is demonstrated in the text-custom-font
example in the simulator crate.
Examples
The examples below use the Font6x8
font, however any of the built-in fonts
in this module or custom fonts can be substituted.
Print styled "Hello Rust!"
Text can be drawn to a display by creating a Text
object and attaching a
text style to it by using a Styled
object. This example prints
"Hello Rust" with a yellow text on a blue background.
use embedded_graphics::{ fonts::{Font6x8, Text}, pixelcolor::Rgb565, prelude::*, style::{MonoTextStyle, MonoTextStyleBuilder}, }; // Create a new text style let style = MonoTextStyleBuilder::new(Font6x8) .text_color(Rgb565::YELLOW) .background_color(Rgb565::BLUE) .build(); // Create a text at position (20, 30) and draw it using the previously defined style Text::new("Hello Rust!", Point::new(20, 30)) .into_styled(style) .draw(&mut display)?;
Translate text by (20px, 30px)
use embedded_graphics::{ fonts::{Font6x8, Text}, pixelcolor::BinaryColor, prelude::*, style::MonoTextStyle, }; Text::new("Hello Rust!", Point::zero()) .into_styled(MonoTextStyle::new(Font6x8, BinaryColor::On)) .translate(Point::new(20, 30)) .draw(&mut display)?; // this is equivalent to: Text::new("Hello Rust!", Point::new(20, 30)) .into_styled(MonoTextStyle::new(Font6x8, BinaryColor::On)) .draw(&mut display)?;
Use write!()
and arrayvec to render a formatted string
This example uses arrayvec's ArrayString
to render a floating point value using the
write!()
macro. These strings have a fixed maximum length, but allow the use of
Rust's builtin string formatting.
use arrayvec::ArrayString; use core::fmt::Write; use embedded_graphics::{ fonts::{Font6x8, Text}, pixelcolor::Rgb565, prelude::*, style::MonoTextStyleBuilder, }; let value = 12.34567; // Create a fixed buffer of length 12 let mut buf = ArrayString::<[_; 12]>::new(); // Output `Value: 12.35` write!(&mut buf, "Value: {:.2}", value).expect("Failed to write to buffer"); Text::new(&buf, Point::zero()) .into_styled( MonoTextStyleBuilder::new(Font6x8) .text_color(Rgb565::YELLOW) .background_color(Rgb565::BLUE) .build(), ) .draw(&mut display)?;
Built-in fonts
Type | Screenshot |
---|---|
Font6x8 | |
Font6x12 | |
Font8x16 | |
Font12x16 | |
Font24x32 | The 24x32 font is a pixel doubled version of the 12x16 font. |
Structs
Font6x8 | 6x8 pixel monospace font. |
Font6x12 | 6x12 pixel monospace font. |
Font8x16 | 8x16 pixel monospace font. |
Font12x16 | 12x16 pixel monospace font. |
Font24x32 | 24x32 pixel monospace font. |
MonoPixels | Pixel iterator for styled text with a monospaced font. |
Text | A text object. |
Traits
MonoFont | Monospaced bitmap font. |