diff --git a/src/rendering/line.rs b/src/rendering/line.rs index a4f158aa..382802f2 100644 --- a/src/rendering/line.rs +++ b/src/rendering/line.rs @@ -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) -> 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()); diff --git a/src/rendering/line_iter.rs b/src/rendering/line_iter.rs index cf5c6f0b..399d860a 100644 --- a/src/rendering/line_iter.rs +++ b/src/rendering/line_iter.rs @@ -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) -> Result<(), Self::Error> { Ok(()) } @@ -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(); } @@ -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))?; @@ -456,9 +456,7 @@ where } } - None => { - handler.printed_characters(w, handler.measure(w))?; - } + None => handler.printed_characters(w, None)?, } Ok(()) @@ -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) -> Result<(), Self::Error> { + self.elements.push(RenderElement::String( + str.to_owned(), + width.unwrap_or_else(|| self.measure(str)), + )); Ok(()) } diff --git a/src/style/mod.rs b/src/style/mod.rs index 9d3d11d2..4f67af69 100644 --- a/src/style/mod.rs +++ b/src/style/mod.rs @@ -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) -> 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;