Skip to content

Commit

Permalink
Avoid potentially double-measuring strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Sep 4, 2023
1 parent 6912e65 commit fd7295a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
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
18 changes: 9 additions & 9 deletions src/rendering/line_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 @@ -325,7 +325,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 Down Expand Up @@ -445,7 +445,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 @@ -456,9 +456,7 @@ where
}
}

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

Ok(())
Expand Down Expand Up @@ -532,9 +530,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 fd7295a

Please sign in to comment.