Skip to content

Commit 7d5a49a

Browse files
authored
Merge pull request #157 from embedded-graphics/spaces
Fix space handling when wrapping lines
2 parents 0485555 + 53fe66d commit 7d5a49a

6 files changed

Lines changed: 86 additions & 6 deletions

File tree

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ jobs:
116116
compare -metric AE ./assets/styles-plugin.png ./target/screenshots/styles-plugin.png result.png
117117
EG_SIMULATOR_DUMP="./target/screenshots/styles-static.png" cargo run --example styles-static
118118
compare -metric AE ./assets/styles-static.png ./target/screenshots/styles-static.png result.png
119+
EG_SIMULATOR_DUMP="./target/screenshots/whitespace_control.png" cargo run --example whitespace_control
120+
compare -metric AE ./assets/whitespace_control.png ./target/screenshots/whitespace_control.png result.png
119121
120122
docs:
121123
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
0.5.0 (??)
22
==========
33

4+
## Fixed:
5+
6+
* [#157] Fixed carrying whitespace to the next line
7+
8+
## Removed:
9+
410
* [#155] Removed `TextBox::fit_height` and `TextBox::fit_height_limited`
511

612
[#155]: https://github.com/embedded-graphics/embedded-text/pull/155
13+
[#157]: https://github.com/embedded-graphics/embedded-text/pull/157
714

815
0.5.0-beta.4 (2021-08-16)
916
=========================

assets/whitespace_control.png

8.92 KB
Loading

examples/whitespace_control.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

src/rendering/line_iter.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,11 @@ where
250250
consumed,
251251
consumed_width * self.render_trailing_spaces() as u32,
252252
)?;
253-
254-
self.plugin.consume_partial(consumed as usize);
255-
return Ok(true);
256253
}
254+
255+
self.plugin
256+
.consume_partial((consumed + 1).min(space_count) as usize);
257+
return Ok(true);
257258
}
258259
}
259260
Ok(false)
@@ -680,7 +681,15 @@ pub(crate) mod test {
680681
],
681682
&mw,
682683
);
683-
assert_line_elements(&mut parser, 5, &[RenderElement::string("f", 6)], &mw);
684+
assert_line_elements(
685+
&mut parser,
686+
5,
687+
&[
688+
RenderElement::Space(0, false), // FIXME: why 🤷‍♂️ Should have eaten in prev line
689+
RenderElement::string("f", 6),
690+
],
691+
&mw,
692+
);
684693
}
685694

686695
#[test]
@@ -750,7 +759,7 @@ pub(crate) mod test {
750759

751760
#[test]
752761
fn space_wrapping_issue() {
753-
let mut parser = Parser::parse("Hello, s");
762+
let mut parser = Parser::parse("Hello, s");
754763
let mw = PluginWrapper::new(NoPlugin::<Rgb888>::new());
755764

756765
assert_line_elements(

src/rendering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ pub mod test {
380380
.build();
381381

382382
TextBox::with_textbox_style(
383-
"Hello, s",
383+
"Hello, s",
384384
Rectangle::new(Point::zero(), size_for(&FONT_6X10, 10, 2)),
385385
character_style,
386386
TextBoxStyleBuilder::new()

0 commit comments

Comments
 (0)