Skip to content

Commit

Permalink
Merge pull request #160 from embedded-graphics/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
bugadani committed Sep 4, 2023
2 parents 5f21ca0 + aacd6c4 commit 59109a7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 41 deletions.
1 change: 0 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ where
/// Create a new parser object to process the given piece of text.
#[inline]
#[must_use]

pub fn parse(text: &'a str) -> Self {
Self {
inner: text.chars(),
Expand Down
20 changes: 11 additions & 9 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,17 @@ where
chars.as_str()
};

let token = match this.peeked_token.take().unwrap() {
Token::Whitespace(count, seq) => {
Token::Whitespace(count - len as u32, skip_chars(seq, len))
}
Token::Word(w) => Token::Word(skip_chars(w, len)),
_ => unreachable!(),
};

this.peeked_token.replace(token);
if let Some(token) = this.peeked_token.take() {
let token = match token {
Token::Whitespace(count, seq) => {
Token::Whitespace(count - len as u32, skip_chars(seq, len))
}
Token::Word(w) => Token::Word(skip_chars(w, len)),
_ => return,
};

this.peeked_token.replace(token);
}
})
}

Expand Down
6 changes: 5 additions & 1 deletion src/rendering/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ impl LineCursor {

/// Returns the distance to the next tab position.
pub const fn next_tab_width(&self) -> u32 {
let next_tab_pos = (self.position / self.tab_width + 1) * self.tab_width;
let next_tab_pos = if self.tab_width == 0 {
self.position
} else {
(self.position / self.tab_width + 1) * self.tab_width
};
next_tab_pos - self.position
}

Expand Down
7 changes: 5 additions & 2 deletions src/rendering/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ where
Ok(())
}

fn printed_characters(&mut self, st: &str, width: u32) -> Result<(), Self::Error> {
fn printed_characters(&mut self, st: &str, width: Option<u32>) -> Result<(), Self::Error> {
let top_left = self.pos;
self.style
let render_width = self
.style
.draw_string(st, self.pos, Baseline::Top, self.display)?;

let width = width.unwrap_or((render_width - top_left).x as u32);

self.pos += Point::new(width.saturating_as(), 0);

let size = Size::new(width, self.style.line_height().saturating_as());
Expand Down
56 changes: 30 additions & 26 deletions src/rendering/line_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Turns a token stream into a number of events. A single `LineElementParser` object operates on
//! a single line and is responsible for handling word wrapping, eating leading/trailing whitespace,
//! handling tab characters, soft wrapping characters, non-breaking spaces, etc.

use crate::{
parser::{ChangeTextStyle, Parser, Token, SPEC_CHAR_NBSP},
plugin::{PluginMarker as Plugin, PluginWrapper},
Expand Down Expand Up @@ -51,7 +52,7 @@ pub trait ElementHandler {
}

/// A string of printable characters.
fn printed_characters(&mut self, _st: &str, _width: u32) -> Result<(), Self::Error> {
fn printed_characters(&mut self, _st: &str, _width: Option<u32>) -> Result<(), Self::Error> {
Ok(())
}

Expand Down Expand Up @@ -133,27 +134,26 @@ where
&mut self,
handler: &E,
w: &'a str,
) -> (&'a str, Option<&'a str>) {
) -> (&'a str, &'a str) {
let mut width = 0;
for (idx, c) in w.char_indices() {
let char_width = handler.measure(unsafe {
// SAFETY: we are working on character boundaries
w.get_unchecked(idx..idx + c.len_utf8())
});
if !self.cursor.fits_in_line(width + char_width) {
debug_assert!(w.is_char_boundary(idx));
return (
unsafe {
// SAFETY: we are working on character boundaries
w.get_unchecked(0..idx)
},
w.get(idx..),
);
unsafe {
if w.is_char_boundary(idx) {
return w.split_at(idx);
} else {
core::hint::unreachable_unchecked();
}
}
}
width += char_width;
}

(w, None)
(w, "")
}

fn next_word_fits<E: ElementHandler>(&self, space_width: i32, handler: &E) -> bool {
Expand Down Expand Up @@ -237,11 +237,15 @@ where
let single = space_width / space_count;
let consumed = moved as u32 / single;
if consumed > 0 {
let (pos, _) = string.char_indices().nth(consumed as usize).unwrap();
let consumed_str = unsafe {
// SAFETY: Pos is a valid index, we just got it
string.get_unchecked(0..pos)
};
let consumed_str = string
.char_indices()
.nth(consumed as usize)
.map(|(pos, _)| unsafe {
// SAFETY: Pos is a valid index, we just got it
string.get_unchecked(0..pos)
})
.unwrap_or(string);

let consumed_width = consumed * single;

let _ = self.move_cursor(consumed_width.saturating_as());
Expand Down Expand Up @@ -319,7 +323,7 @@ where
let width = handler.measure(c);
if self.move_cursor(width.saturating_as()).is_ok() {
if let Some(Token::Break(c, _)) = self.plugin.render_token(token) {
handler.printed_characters(c, width)?;
handler.printed_characters(c, Some(width))?;
}
self.consume_token();
}
Expand All @@ -338,7 +342,7 @@ where
let (word, remainder) = if self.move_cursor(width.saturating_as()).is_ok() {
// We can move the cursor here since `process_word()`
// doesn't depend on it.
(w, None)
(w, "")
} else if self.empty {
// This word does not fit into an empty line. Find longest part
// that fits and push the rest to the next line.
Expand All @@ -362,7 +366,7 @@ where
self.process_word(handler, word)?;
}

if remainder.is_some() {
if !remainder.is_empty() {
// Consume what was printed.
self.plugin.consume_partial(word.len());
return Ok(LineEndType::LineBreak);
Expand Down Expand Up @@ -439,7 +443,7 @@ where
// Safety: space_pos must be a character boundary
w.get_unchecked(0..space_pos)
};
handler.printed_characters(word, handler.measure(word))?;
handler.printed_characters(word, None)?;
}

handler.whitespace("\u{a0}", 1, self.spaces.consume(1))?;
Expand All @@ -450,9 +454,7 @@ where
}
}

None => {
handler.printed_characters(w, handler.measure(w))?;
}
None => handler.printed_characters(w, None)?,
}

Ok(())
Expand Down Expand Up @@ -526,9 +528,11 @@ pub(crate) mod test {
Ok(())
}

fn printed_characters(&mut self, st: &str, width: u32) -> Result<(), Self::Error> {
self.elements
.push(RenderElement::String(st.to_owned(), width));
fn printed_characters(&mut self, str: &str, width: Option<u32>) -> Result<(), Self::Error> {
self.elements.push(RenderElement::String(
str.to_owned(),
width.unwrap_or_else(|| self.measure(str)),
));
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ impl<'a, S: TextRenderer> ElementHandler for MeasureLineElementHandler<'a, S> {
Ok(())
}

fn printed_characters(&mut self, _: &str, width: u32) -> Result<(), Self::Error> {
self.cursor += width;
fn printed_characters(&mut self, str: &str, width: Option<u32>) -> Result<(), Self::Error> {
self.cursor += width.unwrap_or_else(|| self.measure(str));
self.pos = self.pos.max(self.cursor);
self.right = self.pos;
self.space_count = self.partial_space_count;
Expand Down

0 comments on commit 59109a7

Please sign in to comment.