From f69962cdece2f2de3ed5d31876b5e5e2862f63db Mon Sep 17 00:00:00 2001 From: Andrey Kuzmin Date: Sun, 28 Apr 2024 17:24:37 +0200 Subject: [PATCH] Treat left offset as a unaccounted whitespace --- src/alignment/mod.rs | 11 ++++------- src/rendering/line.rs | 5 ----- src/rendering/line_iter.rs | 16 ++++------------ src/style/mod.rs | 16 ---------------- 4 files changed, 8 insertions(+), 40 deletions(-) diff --git a/src/alignment/mod.rs b/src/alignment/mod.rs index 86f87141..5f1441fc 100644 --- a/src/alignment/mod.rs +++ b/src/alignment/mod.rs @@ -35,13 +35,10 @@ impl HorizontalAlignment { let space_width = str_width(renderer, " "); let space_config = SpaceConfig::new(space_width, None); let remaining_space = measurement.max_line_width - measurement.width; - let left_offset = measurement.left_offset as i32; match self { - HorizontalAlignment::Left => (left_offset, space_config), - HorizontalAlignment::Center => { - (left_offset + (remaining_space as i32 + 1) / 2, space_config) - } - HorizontalAlignment::Right => (left_offset + remaining_space as i32, space_config), + HorizontalAlignment::Left => (0, 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 { @@ -52,7 +49,7 @@ impl HorizontalAlignment { } else { space_config }; - (left_offset, space_info) + (0, space_info) } } } diff --git a/src/rendering/line.rs b/src/rendering/line.rs index 18f0022e..2d9e0c52 100644 --- a/src/rendering/line.rs +++ b/src/rendering/line.rs @@ -109,11 +109,6 @@ where str_left_offset(self.text_renderer, st) } - fn left_offset(&mut self, _offset: u32) { - // Not used in rendering, because this is set when measuring. - // Rendering starts from the measured position. - } - fn whitespace(&mut self, st: &str, _space_count: u32, width: u32) -> Result<(), Self::Error> { if width > 0 { self.text_renderer diff --git a/src/rendering/line_iter.rs b/src/rendering/line_iter.rs index c2f7fcdc..a50c9059 100644 --- a/src/rendering/line_iter.rs +++ b/src/rendering/line_iter.rs @@ -49,9 +49,6 @@ pub trait ElementHandler { /// Returns the left offset in pixels. fn measure_left_offset(&self, _st: &str) -> u32; - /// Start a new line at the given horizontal offset in pixels. - fn left_offset(&mut self, _offset: u32); - /// A whitespace block with the given width. fn whitespace(&mut self, _st: &str, _space_count: u32, _width: u32) -> Result<(), Self::Error> { Ok(()) @@ -332,7 +329,7 @@ where // the word's left negative boundary to make sure it is not clipped. let offset = handler.measure_left_offset(w); if offset > 0 && self.move_cursor_forward(offset).is_ok() { - handler.left_offset(offset); + handler.whitespace("", 0, offset).ok(); }; } @@ -487,7 +484,6 @@ pub(crate) mod test { #[derive(PartialEq, Eq, Debug)] pub enum RenderElement { - LeftOffset(u32), Space(u32, bool), String(String, u32), MoveCursor(i32), @@ -532,10 +528,6 @@ pub(crate) mod test { str_left_offset(&self.style, st) } - fn left_offset(&mut self, offset: u32) { - self.elements.push(RenderElement::LeftOffset(offset)); - } - fn whitespace(&mut self, _string: &str, count: u32, width: u32) -> Result<(), Self::Error> { self.elements .push(RenderElement::Space(count, (width > 0) as bool)); @@ -808,7 +800,7 @@ pub(crate) mod test { } /// A font where each glyph is 4x10 pixels, where the - /// glyph 'j' has a left side bearing of 2 pixels (renders with negative offset) + /// glyph 'j' has a negative left side bearing of 2 pixels struct TestTextStyle {} impl TextRenderer for TestTextStyle { @@ -867,7 +859,7 @@ pub(crate) mod test { let plugin = PluginWrapper::new(NoPlugin::::new()); let style = TestTextStyle {}; // the glyph 'j' occupies 2 pixels because of the negative left side bearing - // however, the first 'j' is rendered in full because of the left line offset + // however, the first 'j' on the line is prepended with an extra 2 pixel whitespace let size = Size::new(4 * text.len() as u32 - 2, 10); let config = SpaceConfig::new(str_width(&style, " "), None); let cursor = Cursor::new( @@ -889,7 +881,7 @@ pub(crate) mod test { assert_eq!( handler.elements, &[ - RenderElement::LeftOffset(2), + RenderElement::Space(0, true), RenderElement::string("just", 14), RenderElement::Space(1, true), RenderElement::string("a", 4), diff --git a/src/style/mod.rs b/src/style/mod.rs index 2afa47cb..54ac125a 100644 --- a/src/style/mod.rs +++ b/src/style/mod.rs @@ -323,9 +323,6 @@ impl TextBoxStyle { #[derive(Debug, Copy, Clone)] #[must_use] pub(crate) struct LineMeasurement { - /// Left offset in pixels. - pub left_offset: u32, - /// Maximum line width in pixels. pub max_line_width: u32, @@ -357,7 +354,6 @@ struct MeasureLineElementHandler<'a, S> { trailing_spaces: bool, cursor: u32, pos: u32, - left_offset: u32, right: u32, partial_space_count: u32, space_count: u32, @@ -372,10 +368,6 @@ impl<'a, S> MeasureLineElementHandler<'a, S> { } } - fn left_offset(&self) -> u32 { - self.left_offset - } - fn right(&self) -> u32 { if self.trailing_spaces { self.pos @@ -397,12 +389,6 @@ impl<'a, S: TextRenderer> ElementHandler for MeasureLineElementHandler<'a, S> { str_left_offset(self.style, st) } - fn left_offset(&mut self, offset: u32) { - self.left_offset = offset; - self.cursor += offset; - self.pos = self.pos.max(self.cursor); - } - fn whitespace(&mut self, _st: &str, count: u32, width: u32) -> Result<(), Self::Error> { self.cursor += width; self.pos = self.pos.max(self.cursor); @@ -463,7 +449,6 @@ impl TextBoxStyle { cursor: 0, pos: 0, - left_offset: 0, right: 0, partial_space_count: 0, space_count: 0, @@ -471,7 +456,6 @@ impl TextBoxStyle { let last_token = iter.process(&mut handler).unwrap(); LineMeasurement { - left_offset: handler.left_offset(), max_line_width, width: handler.right(), space_count: handler.space_count(),