Skip to content

Commit

Permalink
Replace a hack with a proper implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
afishhh committed Nov 30, 2024
1 parent 110a9c3 commit 06e639e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
5 changes: 3 additions & 2 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
37 changes: 10 additions & 27 deletions crates/epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::ops::RangeInclusive;
use std::sync::Arc;

use emath::{pos2, vec2, Align, NumExt, Pos2, Rect, Vec2};
Expand Down Expand Up @@ -190,11 +189,6 @@ fn layout_section(
}
}

/// We ignore y at this stage
fn rect_from_x_range(x_range: RangeInclusive<f32>) -> Rect {
Rect::from_x_y_ranges(x_range, 0.0..=0.0)
}

// Ignores the Y coordinate.
fn rows_from_paragraphs(
paragraphs: Vec<Paragraph>,
Expand Down Expand Up @@ -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(&paragraph, job, &mut rows, elided);
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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),
});
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 06e639e

Please sign in to comment.