Skip to content

Commit 6b7f431

Browse files
authored
Fix text sometime line-breaking or truncating too early (emilk#5077)
1 parent b2dcb7d commit 6b7f431

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

crates/epaint/src/text/text_layout.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn rows_from_paragraphs(
220220
});
221221
} else {
222222
let paragraph_max_x = paragraph.glyphs.last().unwrap().max_x();
223-
if paragraph_max_x <= job.wrap.max_width {
223+
if paragraph_max_x <= job.effective_wrap_width() {
224224
// Early-out optimization: the whole paragraph fits on one row.
225225
let paragraph_min_x = paragraph.glyphs[0].pos.x;
226226
rows.push(Row {
@@ -241,11 +241,7 @@ fn rows_from_paragraphs(
241241
}
242242

243243
fn line_break(paragraph: &Paragraph, job: &LayoutJob, out_rows: &mut Vec<Row>, elided: &mut bool) {
244-
let wrap_width_margin = if job.round_output_size_to_nearest_ui_point {
245-
0.5
246-
} else {
247-
0.0
248-
};
244+
let wrap_width = job.effective_wrap_width();
249245

250246
// Keeps track of good places to insert row break if we exceed `wrap_width`.
251247
let mut row_break_candidates = RowBreakCandidates::default();
@@ -262,7 +258,7 @@ fn line_break(paragraph: &Paragraph, job: &LayoutJob, out_rows: &mut Vec<Row>, e
262258

263259
let potential_row_width = paragraph.glyphs[i].max_x() - row_start_x;
264260

265-
if job.wrap.max_width + wrap_width_margin < potential_row_width {
261+
if wrap_width < potential_row_width {
266262
// Row break:
267263

268264
if first_row_indentation > 0.0
@@ -420,7 +416,7 @@ fn replace_last_glyph_with_overflow_character(
420416
});
421417
}
422418

423-
if row_width(row) <= job.wrap.max_width || row.glyphs.len() == 1 {
419+
if row_width(row) <= job.effective_wrap_width() || row.glyphs.len() == 1 {
424420
return; // we are done
425421
}
426422

@@ -467,7 +463,7 @@ fn replace_last_glyph_with_overflow_character(
467463
}
468464

469465
// Check if we're within width budget:
470-
if row_width(row) <= job.wrap.max_width || row.glyphs.len() == 1 {
466+
if row_width(row) <= job.effective_wrap_width() || row.glyphs.len() == 1 {
471467
return; // We are done
472468
}
473469

@@ -653,8 +649,8 @@ fn galley_from_rows(
653649
// If the user picked a too aggressive wrap width (e.g. more narrow than any individual glyph),
654650
// we should let the user know.
655651
} else {
656-
// Make sure we don't over the max wrap width the user picked:
657-
rect.max.x = rect.max.x.at_most(rect.min.x + job.wrap.max_width);
652+
// Make sure we don't go over the max wrap width the user picked:
653+
rect.max.x = rect.max.x.at_most(rect.min.x + job.wrap.max_width).floor();
658654
}
659655
}
660656

crates/epaint/src/text/text_layout_types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ impl LayoutJob {
175175
}
176176
max_height
177177
}
178+
179+
/// The wrap with, with a small margin in some cases.
180+
pub fn effective_wrap_width(&self) -> f32 {
181+
if self.round_output_size_to_nearest_ui_point {
182+
// On a previous pass we may have rounded down by at most 0.5 and reported that as a width.
183+
// egui may then set that width as the max width for subsequent frames, and it is important
184+
// that we then don't wrap earlier.
185+
self.wrap.max_width + 0.5
186+
} else {
187+
self.wrap.max_width
188+
}
189+
}
178190
}
179191

180192
impl std::hash::Hash for LayoutJob {

0 commit comments

Comments
 (0)