Skip to content

Commit

Permalink
Merge pull request #163 from embedded-graphics/space
Browse files Browse the repository at this point in the history
Simplifications
  • Loading branch information
bugadani authored Oct 13, 2023
2 parents a413703 + 45fa319 commit c4ed22d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 55 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
0.6.5 (2023-10-13)
==================

## Changed:

- [#163] Internal improvements

[#163]: https://github.com/embedded-graphics/embedded-text/pull/163

0.6.4 (2023-10-10)
==================

Expand Down
21 changes: 8 additions & 13 deletions src/alignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,22 @@ impl HorizontalAlignment {
renderer: &impl TextRenderer,
measurement: LineMeasurement,
) -> (u32, SpaceConfig) {
let space_width = str_width(renderer, " ");
let space_config = SpaceConfig::new(space_width, None);
let remaining_space = measurement.max_line_width - measurement.width;
match self {
HorizontalAlignment::Left => (0, SpaceConfig::new_from_renderer(renderer)),
HorizontalAlignment::Center => (
(measurement.max_line_width - measurement.width + 1) / 2,
SpaceConfig::new_from_renderer(renderer),
),
HorizontalAlignment::Right => (
measurement.max_line_width - measurement.width,
SpaceConfig::new_from_renderer(renderer),
),
HorizontalAlignment::Left => (0, space_config),
HorizontalAlignment::Center => ((remaining_space + 1) / 2, space_config),
HorizontalAlignment::Right => (remaining_space, space_config),
HorizontalAlignment::Justified => {
let space_width = str_width(renderer, " ");
let space_count = measurement.space_count;
let space_info = if !measurement.last_line() && space_count != 0 {
let space =
measurement.max_line_width - measurement.width + space_count * space_width;
let space = remaining_space + space_count * space_width;
let space_width = space / space_count;
let extra_pixels = space % space_count;
SpaceConfig::new(space_width, Some(extra_pixels))
} else {
SpaceConfig::new(space_width, None)
space_config
};
(0, space_info)
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/ansi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ mod test {
let state = LineRenderState {
parser,
character_style,
style,
style: &style,
end_type: LineEndType::EndOfText,
plugin: &plugin,
};
Expand Down
6 changes: 3 additions & 3 deletions src/rendering/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Cursor to track rendering position.
use embedded_graphics::{geometry::Point, primitives::Rectangle, text::LineHeight};
use embedded_graphics::{geometry::Point, prelude::Size, primitives::Rectangle, text::LineHeight};

use az::{SaturatingAs, SaturatingCast};

Expand Down Expand Up @@ -105,7 +105,7 @@ impl Cursor {
y: bounds.top_left.y,
line_height: base_line_height.saturating_as(),
line_spacing: line_height.to_absolute(base_line_height).saturating_as(),
bounds,
bounds: Rectangle::new(bounds.top_left, bounds.size + Size::new(0, 1)),
tab_width,
}
}
Expand Down Expand Up @@ -159,6 +159,6 @@ impl Cursor {
#[inline]
#[must_use]
pub fn in_display_area(&self) -> bool {
self.bounds.top_left.y <= self.y && self.y <= self.bottom_right().y - self.line_height + 1
self.bounds.top_left.y <= self.y && self.y + self.line_height <= self.bottom_right().y
}
}
4 changes: 2 additions & 2 deletions src/rendering/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
{
pub parser: Parser<'a, S::Color>,
pub character_style: S,
pub style: TextBoxStyle,
pub style: &'b TextBoxStyle,
pub end_type: LineEndType,
pub plugin: &'b PluginWrapper<'a, M, S::Color>,
}
Expand Down Expand Up @@ -279,7 +279,7 @@ mod test {
let state = LineRenderState {
parser,
character_style,
style,
style: &style,
end_type: LineEndType::EndOfText,
plugin: &plugin,
};
Expand Down
35 changes: 17 additions & 18 deletions src/rendering/line_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,32 @@ where
}

fn next_word_width<E: ElementHandler>(&mut self, handler: &E) -> Option<u32> {
let mut width = None;

// This looks extremely inefficient.
let lookahead = self.plugin.clone();
let mut lookahead_parser = self.parser.clone();

// We don't want to count the current token.
lookahead.consume_peeked_token();
let mut width = 0;
let mut width_set = false;

'lookahead: loop {
loop {
lookahead.consume_peeked_token();
match lookahead.peek_token(&mut lookahead_parser) {
Some(Token::Word(w)) => {
*width.get_or_insert(0) += handler.measure(w);
}

Some(Token::Break(w, _original)) => {
*width.get_or_insert(0) += handler.measure(w);

break 'lookahead;
width += handler.measure(w);
width_set = true;
}

Some(Token::Break(w, _original)) => return Some(width + handler.measure(w)),
Some(Token::ChangeTextStyle(_)) | Some(Token::MoveCursor { .. }) => {}

_ => break 'lookahead,
_ => {
return match width_set {
true => Some(width),
false => None,
};
}
}
lookahead.consume_peeked_token();
}

width
}

fn move_cursor(&mut self, by: i32) -> Result<i32, i32> {
Expand Down Expand Up @@ -555,7 +552,8 @@ pub(crate) mod test {
{
let style = MonoTextStyle::new(&FONT_6X9, BinaryColor::On.into());

let config = SpaceConfig::new_from_renderer(&style);
let space_width = str_width(&style, " ");
let config = SpaceConfig::new(space_width, None);
let cursor = Cursor::new(
Rectangle::new(Point::zero(), size_for(&FONT_6X9, max_chars, 1)),
style.line_height(),
Expand All @@ -580,7 +578,8 @@ pub(crate) mod test {

let style = MonoTextStyle::new(&FONT_6X9, BinaryColor::On.into());

let config = SpaceConfig::new_from_renderer(&style);
let space_width = str_width(&style, " ");
let config = SpaceConfig::new(space_width, None);
let cursor = Cursor::new(
Rectangle::new(Point::zero(), size_for(&FONT_6X9, 1, 1) - Size::new(1, 0)),
style.line_height(),
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ where
self.plugin.on_start_render(&mut cursor, props);

let mut state = LineRenderState {
style: self.style,
style: &self.style,
character_style: self.character_style.clone(),
parser: Parser::parse(self.text),
end_type: LineEndType::EndOfText,
Expand Down
9 changes: 0 additions & 9 deletions src/rendering/space_config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//! Space rendering config

use embedded_graphics::text::renderer::TextRenderer;

use crate::utils::str_width;

#[derive(Copy, Clone, Debug)]
pub struct SpaceConfig {
/// The width of the whitespace characters.
Expand All @@ -21,11 +17,6 @@ impl SpaceConfig {
Self { width, count }
}

pub fn new_from_renderer(renderer: &impl TextRenderer) -> Self {
let width = str_width(renderer, " ");
Self::new(width, None)
}

/// Look at the size of next n spaces, without advancing.
pub fn peek_next_width(&self, n: u32) -> u32 {
match self.count {
Expand Down
5 changes: 1 addition & 4 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ impl LineMeasurement {
struct MeasureLineElementHandler<'a, S> {
style: &'a S,
trailing_spaces: bool,
max_line_width: u32,
cursor: u32,
pos: u32,
right: u32,
Expand Down Expand Up @@ -408,8 +407,7 @@ impl<'a, S: TextRenderer> ElementHandler for MeasureLineElementHandler<'a, S> {
}

fn move_cursor(&mut self, by: i32) -> Result<(), Self::Error> {
self.cursor = (self.cursor.saturating_as::<i32>() + by)
.clamp(0, self.max_line_width.saturating_as()) as u32;
self.cursor = (self.cursor.saturating_as::<i32>() + by) as u32;

Ok(())
}
Expand Down Expand Up @@ -449,7 +447,6 @@ impl TextBoxStyle {
let mut handler = MeasureLineElementHandler {
style: character_style,
trailing_spaces: self.trailing_spaces,
max_line_width,

cursor: 0,
pos: 0,
Expand Down
9 changes: 5 additions & 4 deletions src/style/vertical_overdraw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Vertical overdraw options.

use crate::rendering::cursor::Cursor;
use core::ops::Range;

Expand All @@ -16,24 +17,24 @@ pub enum VerticalOverdraw {
impl VerticalOverdraw {
/// Calculate the range of rows of the current line that can be drawn.
pub(crate) fn calculate_displayed_row_range(self, cursor: &Cursor) -> Range<i32> {
let line_height = cursor.line_height();
match self {
VerticalOverdraw::FullRowsOnly => {
if cursor.in_display_area() {
0..cursor.line_height()
0..line_height
} else {
0..0
}
}

VerticalOverdraw::Hidden => {
let offset_top = (cursor.top_left().y - cursor.y).max(0);
let offset_bottom =
(cursor.bottom_right().y - cursor.y + 1).min(cursor.line_height());
let offset_bottom = (cursor.bottom_right().y - cursor.y).min(line_height);

offset_top..offset_bottom
}

VerticalOverdraw::Visible => 0..cursor.line_height(),
VerticalOverdraw::Visible => 0..line_height,
}
}
}
Expand Down

0 comments on commit c4ed22d

Please sign in to comment.