Skip to content

Commit

Permalink
Merge pull request #164 from embedded-graphics/renderer
Browse files Browse the repository at this point in the history
Even more simplifications
  • Loading branch information
bugadani committed Oct 15, 2023
2 parents 6c2d0f0 + 65b0c38 commit 6f55a03
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 189 deletions.
4 changes: 2 additions & 2 deletions examples/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ where

Some(Token::Word(word))
}
Some(Token::Break(_, _)) => {
Some(Token::Break(_)) => {
self.measured += 1;
token
}
Expand All @@ -115,7 +115,7 @@ where
Some(Token::Whitespace(to_render, s.first_n_chars(to_render)))
}
Token::Word(s) => Some(Token::Word(s.first_n_chars(to_render))),
Token::Break(repl, orig) => Some(Token::Break(repl.first_n_chars(to_render), orig)),
Token::Break(repl) => Some(Token::Break(repl.first_n_chars(to_render))),
_ => Some(token),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/alignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ impl HorizontalAlignment {
self,
renderer: &impl TextRenderer,
measurement: LineMeasurement,
) -> (u32, SpaceConfig) {
) -> (i32, 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, space_config),
HorizontalAlignment::Center => ((remaining_space + 1) / 2, space_config),
HorizontalAlignment::Right => (remaining_space, space_config),
HorizontalAlignment::Center => ((remaining_space as i32 + 1) / 2, space_config),
HorizontalAlignment::Right => (remaining_space as i32, space_config),
HorizontalAlignment::Justified => {
let space_count = measurement.space_count;
let space_info = if !measurement.last_line() && space_count != 0 {
Expand Down
17 changes: 4 additions & 13 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub enum Token<'a, C> {
/// A word (a sequence of non-whitespace characters).
Word(&'a str),

/// A possible wrapping point
Break(&'a str, &'a str),
/// A possible wrapping point. Contains the separator character(s).
Break(&'a str),

/// Change of text style.
ChangeTextStyle(ChangeTextStyle<C>),
Expand Down Expand Up @@ -170,11 +170,6 @@ where
})),
SPEC_CHAR_SHY => Some(Token::Break(
"-", // translate SHY to a printable character
unsafe {
// SAFETY: we only work with character boundaries and
// offset is <= length
string.get_unchecked(0..c.len_utf8())
},
)),

// count consecutive whitespace
Expand Down Expand Up @@ -232,7 +227,7 @@ mod test {
Token::Word("sit"),
Token::Whitespace(1, " "),
Token::Word("am"),
Token::Break("-", "\u{ad}"),
Token::Break("-"),
Token::Word("et,"),
Token::Tab,
Token::Word("conse😅ctetur"),
Expand Down Expand Up @@ -280,11 +275,7 @@ mod test {
fn parse_shy_issue_42() {
assert_tokens(
"foo\u{AD}bar",
vec![
Token::Word("foo"),
Token::Break("-", "\u{ad}"),
Token::Word("bar"),
],
vec![Token::Word("foo"), Token::Break("-"), Token::Word("bar")],
);
}
}
19 changes: 11 additions & 8 deletions src/plugin/ansi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ mod test {

let parser = Parser::parse("foo\x1b[2Dsample");

let character_style = MonoTextStyleBuilder::new()
let text_renderer = MonoTextStyleBuilder::new()
.font(&FONT_6X9)
.text_color(BinaryColor::On)
.background_color(BinaryColor::Off)
Expand All @@ -339,20 +339,23 @@ mod test {

let cursor = LineCursor::new(
size_for(&FONT_6X9, 7, 1).width,
TabSize::Spaces(4).into_pixels(&character_style),
TabSize::Spaces(4).into_pixels(&text_renderer),
);

let plugin = PluginWrapper::new(Ansi::new());
let state = LineRenderState {
let mut state = LineRenderState {
parser,
character_style,
style: &style,
text_renderer,
end_type: LineEndType::EndOfText,
plugin: &plugin,
};
StyledLineRenderer::new(cursor, state)
.draw(&mut display)
.unwrap();
StyledLineRenderer {
cursor,
state: &mut state,
style: &style,
}
.draw(&mut display)
.unwrap();

display.assert_pattern(&[
"..........................................",
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a, M: Clone, C: Clone> Clone for PluginWrapper<'a, M, C> {
Self {
inner: UnsafeCell::new(self.with(|this| PluginInner {
plugin: this.plugin.clone(),
state: this.state.clone(),
state: this.state,
peeked_token: unsafe { addr_of!(this.peeked_token).read() },
})),
}
Expand Down
62 changes: 43 additions & 19 deletions src/rendering/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Cursor to track rendering position.
use embedded_graphics::{geometry::Point, prelude::Size, primitives::Rectangle, text::LineHeight};
use embedded_graphics::{geometry::Point, primitives::Rectangle, text::LineHeight};

use az::{SaturatingAs, SaturatingCast};
use az::SaturatingAs;

/// Tracks position within a line.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -60,10 +60,10 @@ impl LineCursor {
self.position -= abs;
Ok(by)
} else {
Err(-self.position.saturating_as::<i32>())
Err(-(self.position as i32))
}
} else {
let space = self.space().saturating_cast();
let space = self.space() as i32;
if by <= space {
// Here we know by > 0, cast is safe
self.position += by as u32;
Expand All @@ -73,6 +73,18 @@ impl LineCursor {
}
}
}

/// Moves the cursor forward by a given amount.
pub fn move_cursor_forward(&mut self, by: u32) -> Result<u32, u32> {
let space = self.space();
if by <= space {
// Here we know by > 0, cast is safe
self.position += by;
Ok(by)
} else {
Err(space)
}
}
}

/// Internal structure that keeps track of position information while rendering a [`TextBox`].
Expand All @@ -83,10 +95,11 @@ pub struct Cursor {
/// Current cursor position
pub y: i32,

/// TextBox bounding rectangle
bounds: Rectangle,
top_left: Point,
bottom: i32,

line_height: i32,
line_width: u32,
line_height: u32,
line_spacing: i32,
tab_width: u32,
}
Expand All @@ -103,44 +116,55 @@ impl Cursor {
) -> Self {
Self {
y: bounds.top_left.y,
line_height: base_line_height.saturating_as(),

top_left: bounds.top_left,
bottom: bounds.top_left.y + bounds.size.height.saturating_as::<i32>()
- base_line_height.saturating_as::<i32>(),

line_width: bounds.size.width,
line_height: base_line_height,
line_spacing: line_height.to_absolute(base_line_height).saturating_as(),
bounds: Rectangle::new(bounds.top_left, bounds.size + Size::new(0, 1)),
tab_width,
}
}

#[must_use]
pub(crate) fn line(&self) -> LineCursor {
LineCursor {
start: Point::new(self.bounds.top_left.x, self.y),
width: self.bounds.size.width,
start: self.line_start(),
width: self.line_width,
position: 0,
tab_width: self.tab_width,
}
}

/// Returns the coordinates of the bottom right corner.
/// Returns the coordinates of the start of the current line.
#[inline]
pub(crate) fn line_start(&self) -> Point {
Point::new(self.top_left.x, self.y)
}

/// Returns the vertical offset of the bottom of the last visible line.
#[inline]
pub fn bottom_right(&self) -> Point {
self.bounds.bottom_right().unwrap_or(self.bounds.top_left)
pub fn bottom(&self) -> i32 {
self.bottom
}

/// Returns the coordinates of the bottom right corner.
/// Returns the coordinates of the top left corner.
#[inline]
pub fn top_left(&self) -> Point {
self.bounds.top_left
self.top_left
}

/// Returns the width of the text box.
#[inline]
pub fn line_width(&self) -> u32 {
self.bounds.size.width
self.line_width
}

/// Returns the height of a line.
#[inline]
pub fn line_height(&self) -> i32 {
pub fn line_height(&self) -> u32 {
self.line_height
}

Expand All @@ -159,6 +183,6 @@ impl Cursor {
#[inline]
#[must_use]
pub fn in_display_area(&self) -> bool {
self.bounds.top_left.y <= self.y && self.y + self.line_height <= self.bottom_right().y
self.top_left.y <= self.y && self.y <= self.bottom
}
}

0 comments on commit 6f55a03

Please sign in to comment.