|
| 1 | +//! # Example: whitespace control. |
| 2 | +//! |
| 3 | +//! This example demonstrates the different leading/trailing whitespace options and their effect. |
| 4 | +
|
| 5 | +use embedded_graphics::{ |
| 6 | + geometry::AnchorPoint, |
| 7 | + mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder}, |
| 8 | + pixelcolor::Rgb888, |
| 9 | + prelude::*, |
| 10 | + primitives::Rectangle, |
| 11 | +}; |
| 12 | +use embedded_graphics_simulator::{OutputSettingsBuilder, SimulatorDisplay, Window}; |
| 13 | +use embedded_text::{style::TextBoxStyleBuilder, TextBox}; |
| 14 | +use std::convert::Infallible; |
| 15 | + |
| 16 | +fn main() -> Result<(), Infallible> { |
| 17 | + // Set up the window. |
| 18 | + let output_settings = OutputSettingsBuilder::new().scale(3).build(); |
| 19 | + let mut window = Window::new("Interactive TextBox demonstration", &output_settings); |
| 20 | + |
| 21 | + let text = " Hello, World!\n \ |
| 22 | + Lorem Ipsum is simply dummy text of the printing and typesetting industry. \ |
| 23 | + Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when \ |
| 24 | + an unknown printer took a galley of type and scrambled it to make a type specimen book."; |
| 25 | + |
| 26 | + let character_style = MonoTextStyleBuilder::new() |
| 27 | + .font(&FONT_6X10) |
| 28 | + .text_color(Rgb888::WHITE) |
| 29 | + .background_color(Rgb888::CSS_STEEL_BLUE) |
| 30 | + .build(); |
| 31 | + |
| 32 | + // Create a simulated display. |
| 33 | + let mut display = SimulatorDisplay::new(Size::new(255, 140)); |
| 34 | + |
| 35 | + // Create bounding boxes |
| 36 | + let bounds = Rectangle::new(Point::zero(), Size::new(255, 140)); |
| 37 | + |
| 38 | + // Create and draw the text boxes. |
| 39 | + TextBox::with_textbox_style( |
| 40 | + text, |
| 41 | + bounds.resized(Size::new(128, 255), AnchorPoint::TopLeft), |
| 42 | + character_style, |
| 43 | + TextBoxStyleBuilder::default().build(), |
| 44 | + ) |
| 45 | + .draw(&mut display)?; |
| 46 | + |
| 47 | + TextBox::with_textbox_style( |
| 48 | + text, |
| 49 | + bounds.resized(Size::new(128, 255), AnchorPoint::TopRight), |
| 50 | + character_style, |
| 51 | + TextBoxStyleBuilder::default() |
| 52 | + .leading_spaces(false) |
| 53 | + .trailing_spaces(true) |
| 54 | + .build(), |
| 55 | + ) |
| 56 | + .draw(&mut display)?; |
| 57 | + |
| 58 | + // Update the window. |
| 59 | + window.show_static(&display); |
| 60 | + |
| 61 | + Ok(()) |
| 62 | +} |
0 commit comments