Skip to content

Commit

Permalink
Treat left offset as a unaccounted whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
w0rm committed Apr 28, 2024
1 parent 2d4b2d5 commit f7a23ad
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 38 deletions.
11 changes: 4 additions & 7 deletions src/alignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (0 + (remaining_space as i32 + 1) / 2, space_config),
HorizontalAlignment::Right => (0 + 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 All @@ -52,7 +49,7 @@ impl HorizontalAlignment {
} else {
space_config
};
(left_offset, space_info)
(0, space_info)
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/rendering/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions src/rendering/line_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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();
};
}

Expand Down Expand Up @@ -487,7 +484,6 @@ pub(crate) mod test {

#[derive(PartialEq, Eq, Debug)]
pub enum RenderElement<C: PixelColor> {
LeftOffset(u32),
Space(u32, bool),
String(String, u32),
MoveCursor(i32),
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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),
Expand Down
16 changes: 0 additions & 16 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -463,15 +449,13 @@ impl TextBoxStyle {

cursor: 0,
pos: 0,
left_offset: 0,
right: 0,
partial_space_count: 0,
space_count: 0,
};
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(),
Expand Down

0 comments on commit f7a23ad

Please sign in to comment.