Only commit a previous word range if we had an existing visual line.

Fixes #155, and also fixes Word::Wrap when two buffer overflowing words are next to each other.
This commit is contained in:
dtzxporter 2024-02-02 13:29:53 -05:00 committed by Jeremy Soller
parent ceb6dccb40
commit de587b3f50

View file

@ -1046,42 +1046,40 @@ impl ShapeLine {
} else { } else {
// Wrap::Word, Wrap::WordOrGlyph // Wrap::Word, Wrap::WordOrGlyph
// TODO: What if the previous span ended with whitespace and the next // If we had a previous range, commit that line before the next word.
// span wraps a new line? Is that possible? if word_range_width > 0. {
// // Current word causing a wrap is not whitespace, so we ignore the
// TODO: This comment it outdated, the current word can be a // previous word if it's a whitespace
// whitespace. let trailing_blank = span
// .words
// Current word causing a wrap is not whitespace, so we ignore the .get(i + 1)
// previous word if it's a whitespace .map_or(false, |previous_word| previous_word.blank);
let trailing_blank = span if trailing_blank {
.words number_of_blanks = number_of_blanks.saturating_sub(1);
.get(i + 1) add_to_visual_line(
.map_or(false, |previous_word| previous_word.blank); &mut current_visual_line,
if trailing_blank { span_index,
number_of_blanks = number_of_blanks.saturating_sub(1); (i + 2, 0),
add_to_visual_line( fitting_start,
&mut current_visual_line, width_before_last_blank,
span_index, number_of_blanks,
(i + 2, 0), );
fitting_start, } else {
width_before_last_blank, add_to_visual_line(
number_of_blanks, &mut current_visual_line,
); span_index,
} else { (i + 1, 0),
add_to_visual_line( fitting_start,
&mut current_visual_line, word_range_width,
span_index, number_of_blanks,
(i + 1, 0), );
fitting_start, }
word_range_width,
number_of_blanks, visual_lines.push(current_visual_line);
); current_visual_line = VisualLine::default();
} number_of_blanks = 0;
visual_lines.push(current_visual_line); }
current_visual_line = VisualLine::default();
number_of_blanks = 0;
if word.blank { if word.blank {
word_range_width = 0.; word_range_width = 0.;
fitting_start = (i, 0); fitting_start = (i, 0);
@ -1172,32 +1170,37 @@ impl ShapeLine {
} else { } else {
// Wrap::Word, Wrap::WordOrGlyph // Wrap::Word, Wrap::WordOrGlyph
// Current word causing a wrap is not whitespace, so we ignore the // If we had a previous range, commit that line before the next word.
// previous word if it's a whitespace if word_range_width > 0. {
let trailing_blank = i > 0 && span.words[i - 1].blank; // Current word causing a wrap is not whitespace, so we ignore the
if trailing_blank { // previous word if it's a whitespace.
number_of_blanks = number_of_blanks.saturating_sub(1); let trailing_blank = i > 0 && span.words[i - 1].blank;
add_to_visual_line(
&mut current_visual_line, if trailing_blank {
span_index, number_of_blanks = number_of_blanks.saturating_sub(1);
fitting_start, add_to_visual_line(
(i - 1, 0), &mut current_visual_line,
width_before_last_blank, span_index,
number_of_blanks, fitting_start,
); (i - 1, 0),
} else { width_before_last_blank,
add_to_visual_line( number_of_blanks,
&mut current_visual_line, );
span_index, } else {
fitting_start, add_to_visual_line(
(i, 0), &mut current_visual_line,
word_range_width, span_index,
number_of_blanks, fitting_start,
); (i, 0),
word_range_width,
number_of_blanks,
);
}
visual_lines.push(current_visual_line);
current_visual_line = VisualLine::default();
number_of_blanks = 0;
} }
visual_lines.push(current_visual_line);
current_visual_line = VisualLine::default();
number_of_blanks = 0;
if word.blank { if word.blank {
word_range_width = 0.; word_range_width = 0.;