From 06e639edc64d4294fc7d34a1c844ac3f43e0aae6 Mon Sep 17 00:00:00 2001 From: Fishhh Date: Sat, 30 Nov 2024 19:23:32 +0100 Subject: [PATCH] Replace a hack with a proper implementation --- crates/epaint/src/text/fonts.rs | 5 ++-- crates/epaint/src/text/text_layout.rs | 37 ++++++++------------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/crates/epaint/src/text/fonts.rs b/crates/epaint/src/text/fonts.rs index 2e0343d6d4d..36f2aa5d195 100644 --- a/crates/epaint/src/text/fonts.rs +++ b/crates/epaint/src/text/fonts.rs @@ -730,7 +730,7 @@ impl GalleyCache { fn layout_multiline(&mut self, fonts: &mut FontsImpl, job: LayoutJob) -> Galley { let pixels_per_point = fonts.pixels_per_point; let round_to_pixel = - move |point: emath::Pos2| (point * pixels_per_point).round() / pixels_per_point; + move |point: f32| (point * pixels_per_point).round() / pixels_per_point; let mut current_section = 0; let mut current = 0; @@ -816,7 +816,8 @@ impl GalleyCache { } merged_galley.rows.extend(rows.map(|placed_row| { - let new_pos = round_to_pixel(placed_row.pos + current_offset); + let mut new_pos = placed_row.pos + current_offset; + new_pos.y = round_to_pixel(new_pos.y); merged_galley.mesh_bounds = merged_galley .mesh_bounds .union(placed_row.visuals.mesh_bounds.translate(new_pos.to_vec2())); diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs index 5f52235c566..877cff5ba36 100644 --- a/crates/epaint/src/text/text_layout.rs +++ b/crates/epaint/src/text/text_layout.rs @@ -1,4 +1,3 @@ -use std::ops::RangeInclusive; use std::sync::Arc; use emath::{pos2, vec2, Align, NumExt, Pos2, Rect, Vec2}; @@ -190,11 +189,6 @@ fn layout_section( } } -/// We ignore y at this stage -fn rect_from_x_range(x_range: RangeInclusive) -> Rect { - Rect::from_x_y_ranges(x_range, 0.0..=0.0) -} - // Ignores the Y coordinate. fn rows_from_paragraphs( paragraphs: Vec, @@ -222,23 +216,21 @@ fn rows_from_paragraphs( size: vec2(0.0, paragraph.empty_paragraph_height), ends_with_newline: !is_last_paragraph, }), - pos: pos2(paragraph.cursor_x, 0.0), + pos: pos2(0.0, 0.0), }); } else { let paragraph_max_x = paragraph.glyphs.last().unwrap().max_x(); if paragraph_max_x <= job.effective_wrap_width() { // Early-out optimization: the whole paragraph fits on one row. - let paragraph_min_x = paragraph.glyphs[0].pos.x; - let rect = rect_from_x_range(paragraph_min_x..=paragraph_max_x); rows.push(PlacedRow { row: Arc::new(Row { section_index_at_start: paragraph.section_index_at_start, glyphs: paragraph.glyphs, visuals: Default::default(), - size: rect.size(), + size: vec2(paragraph_max_x, 0.0), ends_with_newline: !is_last_paragraph, }), - pos: rect.min, + pos: pos2(0.0, f32::NAN), }); } else { line_break(¶graph, job, &mut rows, elided); @@ -283,16 +275,15 @@ fn line_break( { // Allow the first row to be completely empty, because we know there will be more space on the next row: // TODO(emilk): this records the height of this first row as zero, though that is probably fine since first_row_indentation usually comes with a first_row_min_height. - let rect = rect_from_x_range(first_row_indentation..=first_row_indentation); out_rows.push(PlacedRow { row: Arc::new(Row { section_index_at_start: paragraph.section_index_at_start, glyphs: vec![], visuals: Default::default(), - size: rect.size(), + size: vec2(0.0, 0.0), ends_with_newline: false, }), - pos: rect.min, + pos: pos2(0.0, f32::NAN), }); row_start_x += first_row_indentation; first_row_indentation = 0.0; @@ -308,19 +299,17 @@ fn line_break( .collect(); let section_index_at_start = glyphs[0].section_index; - let paragraph_min_x = glyphs[0].pos.x; let paragraph_max_x = glyphs.last().unwrap().max_x(); - let rect = rect_from_x_range(paragraph_min_x..=paragraph_max_x); out_rows.push(PlacedRow { row: Arc::new(Row { section_index_at_start, glyphs, visuals: Default::default(), - size: rect.size(), + size: vec2(paragraph_max_x, 0.0), ends_with_newline: false, }), - pos: rect.min, + pos: pos2(0.0, f32::NAN), }); // Start a new row: @@ -354,16 +343,15 @@ fn line_break( let paragraph_min_x = glyphs[0].pos.x; let paragraph_max_x = glyphs.last().unwrap().max_x(); - let rect = rect_from_x_range(paragraph_min_x..=paragraph_max_x); out_rows.push(PlacedRow { row: Arc::new(Row { section_index_at_start, glyphs, visuals: Default::default(), - size: rect.size(), + size: vec2(paragraph_max_x - paragraph_min_x, 0.0), ends_with_newline: false, }), - pos: rect.min, + pos: pos2(paragraph_min_x, 0.0), }); } } @@ -609,8 +597,7 @@ fn halign_and_justify_row( } } - // Note we ignore the leading/trailing whitespace here! - pos.x = target_min_x; + // Note we **don't** ignore the leading/trailing whitespace here! row.size.x = target_max_x - target_min_x; } @@ -647,10 +634,6 @@ fn galley_from_rows( // When mixing different `FontImpl` (e.g. latin and emojis), // we always center the difference: + 0.5 * (glyph.font_height - glyph.font_impl_height); - - // FIXME(afishhh): HACK! change the proper code above instead!! - // this should probably not be merged like this! - glyph.pos.x -= placed_row.pos.x; } placed_row.pos.y = cursor_y;