1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Merge branch 'master' into cache_galley_lines

This commit is contained in:
Emil Ernerfeldt
2025-03-30 16:22:59 +02:00
6 changed files with 125 additions and 83 deletions

View File

@@ -743,7 +743,7 @@ fn tessellate_row(
mesh.reserve_vertices(row.glyphs.len() * 4);
if format_summary.any_background {
add_row_backgrounds(job, row, &mut mesh);
add_row_backgrounds(point_scale, job, row, &mut mesh);
}
let glyph_index_start = mesh.indices.len();
@@ -781,7 +781,7 @@ fn tessellate_row(
/// Create background for glyphs that have them.
/// Creates as few rectangular regions as possible.
fn add_row_backgrounds(job: &LayoutJob, row: &Row, mesh: &mut Mesh) {
fn add_row_backgrounds(point_scale: PointScale, job: &LayoutJob, row: &Row, mesh: &mut Mesh) {
if row.glyphs.is_empty() {
return;
}
@@ -790,6 +790,7 @@ fn add_row_backgrounds(job: &LayoutJob, row: &Row, mesh: &mut Mesh) {
if let Some((color, start_rect, expand)) = start {
let rect = Rect::from_min_max(start_rect.left_top(), pos2(stop_x, start_rect.bottom()));
let rect = rect.expand(expand);
let rect = rect.round_to_pixels(point_scale.pixels_per_point());
mesh.add_colored_rect(rect, color);
}
};
@@ -884,9 +885,15 @@ fn add_row_hline(
mesh: &mut Mesh,
stroke_and_y: impl Fn(&Glyph) -> (Stroke, f32),
) {
let mut path = crate::tessellator::Path::default(); // reusing path to avoid re-allocations.
let mut end_line = |start: Option<(Stroke, Pos2)>, stop_x: f32| {
if let Some((stroke, start)) = start {
add_hline(point_scale, [start, pos2(stop_x, start.y)], stroke, mesh);
let stop = pos2(stop_x, start.y);
path.clear();
path.add_line_segment([start, stop]);
let feathering = 1.0 / point_scale.pixels_per_point();
path.stroke_open(feathering, &PathStroke::from(stroke), mesh);
}
};
@@ -916,34 +923,6 @@ fn add_row_hline(
end_line(line_start.take(), last_right_x);
}
fn add_hline(point_scale: PointScale, [start, stop]: [Pos2; 2], stroke: Stroke, mesh: &mut Mesh) {
let antialiased = true;
if antialiased {
let mut path = crate::tessellator::Path::default(); // TODO(emilk): reuse this to avoid re-allocations.
path.add_line_segment([start, stop]);
let feathering = 1.0 / point_scale.pixels_per_point();
path.stroke_open(feathering, &PathStroke::from(stroke), mesh);
} else {
// Thin lines often lost, so this is a bad idea
assert_eq!(
start.y, stop.y,
"Horizontal line must be horizontal, but got: {start:?} -> {stop:?}"
);
let min_y = point_scale.round_to_pixel(start.y - 0.5 * stroke.width);
let max_y = point_scale.round_to_pixel(min_y + stroke.width);
let rect = Rect::from_min_max(
pos2(point_scale.round_to_pixel(start.x), min_y),
pos2(point_scale.round_to_pixel(stop.x), max_y),
);
mesh.add_colored_rect(rect, stroke.color);
}
}
// ----------------------------------------------------------------------------
/// Keeps track of good places to break a long row of text.

View File

@@ -823,13 +823,16 @@ impl Galley {
/// same as a cursor at the end.
/// This allows implementing text-selection by dragging above/below the galley.
pub fn cursor_from_pos(&self, pos: Vec2) -> CCursor {
// Vertical margin around galley improves text selection UX
const VMARGIN: f32 = 5.0;
if let Some(first_row) = self.rows.first() {
if pos.y < first_row.min_y() {
if pos.y < first_row.min_y() - VMARGIN {
return self.begin();
}
}
if let Some(last_row) = self.rows.last() {
if last_row.max_y() < pos.y {
if last_row.max_y() + VMARGIN < pos.y {
return self.end();
}
}