Skip to content

Commit 06e639e

Browse files
committed
Replace a hack with a proper implementation
1 parent 110a9c3 commit 06e639e

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

crates/epaint/src/text/fonts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ impl GalleyCache {
730730
fn layout_multiline(&mut self, fonts: &mut FontsImpl, job: LayoutJob) -> Galley {
731731
let pixels_per_point = fonts.pixels_per_point;
732732
let round_to_pixel =
733-
move |point: emath::Pos2| (point * pixels_per_point).round() / pixels_per_point;
733+
move |point: f32| (point * pixels_per_point).round() / pixels_per_point;
734734

735735
let mut current_section = 0;
736736
let mut current = 0;
@@ -816,7 +816,8 @@ impl GalleyCache {
816816
}
817817

818818
merged_galley.rows.extend(rows.map(|placed_row| {
819-
let new_pos = round_to_pixel(placed_row.pos + current_offset);
819+
let mut new_pos = placed_row.pos + current_offset;
820+
new_pos.y = round_to_pixel(new_pos.y);
820821
merged_galley.mesh_bounds = merged_galley
821822
.mesh_bounds
822823
.union(placed_row.visuals.mesh_bounds.translate(new_pos.to_vec2()));

crates/epaint/src/text/text_layout.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::ops::RangeInclusive;
21
use std::sync::Arc;
32

43
use emath::{pos2, vec2, Align, NumExt, Pos2, Rect, Vec2};
@@ -190,11 +189,6 @@ fn layout_section(
190189
}
191190
}
192191

193-
/// We ignore y at this stage
194-
fn rect_from_x_range(x_range: RangeInclusive<f32>) -> Rect {
195-
Rect::from_x_y_ranges(x_range, 0.0..=0.0)
196-
}
197-
198192
// Ignores the Y coordinate.
199193
fn rows_from_paragraphs(
200194
paragraphs: Vec<Paragraph>,
@@ -222,23 +216,21 @@ fn rows_from_paragraphs(
222216
size: vec2(0.0, paragraph.empty_paragraph_height),
223217
ends_with_newline: !is_last_paragraph,
224218
}),
225-
pos: pos2(paragraph.cursor_x, 0.0),
219+
pos: pos2(0.0, 0.0),
226220
});
227221
} else {
228222
let paragraph_max_x = paragraph.glyphs.last().unwrap().max_x();
229223
if paragraph_max_x <= job.effective_wrap_width() {
230224
// Early-out optimization: the whole paragraph fits on one row.
231-
let paragraph_min_x = paragraph.glyphs[0].pos.x;
232-
let rect = rect_from_x_range(paragraph_min_x..=paragraph_max_x);
233225
rows.push(PlacedRow {
234226
row: Arc::new(Row {
235227
section_index_at_start: paragraph.section_index_at_start,
236228
glyphs: paragraph.glyphs,
237229
visuals: Default::default(),
238-
size: rect.size(),
230+
size: vec2(paragraph_max_x, 0.0),
239231
ends_with_newline: !is_last_paragraph,
240232
}),
241-
pos: rect.min,
233+
pos: pos2(0.0, f32::NAN),
242234
});
243235
} else {
244236
line_break(&paragraph, job, &mut rows, elided);
@@ -283,16 +275,15 @@ fn line_break(
283275
{
284276
// Allow the first row to be completely empty, because we know there will be more space on the next row:
285277
// 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.
286-
let rect = rect_from_x_range(first_row_indentation..=first_row_indentation);
287278
out_rows.push(PlacedRow {
288279
row: Arc::new(Row {
289280
section_index_at_start: paragraph.section_index_at_start,
290281
glyphs: vec![],
291282
visuals: Default::default(),
292-
size: rect.size(),
283+
size: vec2(0.0, 0.0),
293284
ends_with_newline: false,
294285
}),
295-
pos: rect.min,
286+
pos: pos2(0.0, f32::NAN),
296287
});
297288
row_start_x += first_row_indentation;
298289
first_row_indentation = 0.0;
@@ -308,19 +299,17 @@ fn line_break(
308299
.collect();
309300

310301
let section_index_at_start = glyphs[0].section_index;
311-
let paragraph_min_x = glyphs[0].pos.x;
312302
let paragraph_max_x = glyphs.last().unwrap().max_x();
313303

314-
let rect = rect_from_x_range(paragraph_min_x..=paragraph_max_x);
315304
out_rows.push(PlacedRow {
316305
row: Arc::new(Row {
317306
section_index_at_start,
318307
glyphs,
319308
visuals: Default::default(),
320-
size: rect.size(),
309+
size: vec2(paragraph_max_x, 0.0),
321310
ends_with_newline: false,
322311
}),
323-
pos: rect.min,
312+
pos: pos2(0.0, f32::NAN),
324313
});
325314

326315
// Start a new row:
@@ -354,16 +343,15 @@ fn line_break(
354343
let paragraph_min_x = glyphs[0].pos.x;
355344
let paragraph_max_x = glyphs.last().unwrap().max_x();
356345

357-
let rect = rect_from_x_range(paragraph_min_x..=paragraph_max_x);
358346
out_rows.push(PlacedRow {
359347
row: Arc::new(Row {
360348
section_index_at_start,
361349
glyphs,
362350
visuals: Default::default(),
363-
size: rect.size(),
351+
size: vec2(paragraph_max_x - paragraph_min_x, 0.0),
364352
ends_with_newline: false,
365353
}),
366-
pos: rect.min,
354+
pos: pos2(paragraph_min_x, 0.0),
367355
});
368356
}
369357
}
@@ -609,8 +597,7 @@ fn halign_and_justify_row(
609597
}
610598
}
611599

612-
// Note we ignore the leading/trailing whitespace here!
613-
pos.x = target_min_x;
600+
// Note we **don't** ignore the leading/trailing whitespace here!
614601
row.size.x = target_max_x - target_min_x;
615602
}
616603

@@ -647,10 +634,6 @@ fn galley_from_rows(
647634
// When mixing different `FontImpl` (e.g. latin and emojis),
648635
// we always center the difference:
649636
+ 0.5 * (glyph.font_height - glyph.font_impl_height);
650-
651-
// FIXME(afishhh): HACK! change the proper code above instead!!
652-
// this should probably not be merged like this!
653-
glyph.pos.x -= placed_row.pos.x;
654637
}
655638

656639
placed_row.pos.y = cursor_y;

0 commit comments

Comments
 (0)