1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 12:50:04 -04:00

Add Galley::intrinsic_size and use it in AtomLayout (#7146)

- part of https://github.com/emilk/egui/issues/5762
- also allows me to simplify sizing logic in egui_flex
This commit is contained in:
Lucas Meurer
2025-07-09 08:19:04 +02:00
committed by GitHub
parent f46926aaf1
commit 508c60b2e2
9 changed files with 166 additions and 19 deletions

View File

@@ -82,6 +82,7 @@ pub fn layout(fonts: &mut FontsImpl, job: Arc<LayoutJob>) -> Galley {
num_indices: 0,
pixels_per_point: fonts.pixels_per_point(),
elided: true,
intrinsic_size: Vec2::ZERO,
};
}
@@ -94,6 +95,8 @@ pub fn layout(fonts: &mut FontsImpl, job: Arc<LayoutJob>) -> Galley {
let point_scale = PointScale::new(fonts.pixels_per_point());
let intrinsic_size = calculate_intrinsic_size(point_scale, &job, &paragraphs);
let mut elided = false;
let mut rows = rows_from_paragraphs(paragraphs, &job, &mut elided);
if elided {
@@ -124,7 +127,7 @@ pub fn layout(fonts: &mut FontsImpl, job: Arc<LayoutJob>) -> Galley {
}
// Calculate the Y positions and tessellate the text:
galley_from_rows(point_scale, job, rows, elided)
galley_from_rows(point_scale, job, rows, elided, intrinsic_size)
}
// Ignores the Y coordinate.
@@ -190,6 +193,46 @@ fn layout_section(
}
}
/// Calculate the intrinsic size of the text.
///
/// The result is eventually passed to `Response::intrinsic_size`.
/// This works by calculating the size of each `Paragraph` (instead of each `Row`).
fn calculate_intrinsic_size(
point_scale: PointScale,
job: &LayoutJob,
paragraphs: &[Paragraph],
) -> Vec2 {
let mut intrinsic_size = Vec2::ZERO;
for (idx, paragraph) in paragraphs.iter().enumerate() {
if paragraph.glyphs.is_empty() {
if idx == 0 {
intrinsic_size.y += point_scale.round_to_pixel(paragraph.empty_paragraph_height);
}
continue;
}
intrinsic_size.x = f32::max(
paragraph
.glyphs
.last()
.map(|l| l.max_x())
.unwrap_or_default(),
intrinsic_size.x,
);
let mut height = paragraph
.glyphs
.iter()
.map(|g| g.line_height)
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(paragraph.empty_paragraph_height);
if idx == 0 {
height = f32::max(height, job.first_row_min_height);
}
intrinsic_size.y += point_scale.round_to_pixel(height);
}
intrinsic_size
}
// Ignores the Y coordinate.
fn rows_from_paragraphs(
paragraphs: Vec<Paragraph>,
@@ -610,6 +653,7 @@ fn galley_from_rows(
job: Arc<LayoutJob>,
mut rows: Vec<PlacedRow>,
elided: bool,
intrinsic_size: Vec2,
) -> Galley {
let mut first_row_min_height = job.first_row_min_height;
let mut cursor_y = 0.0;
@@ -680,6 +724,7 @@ fn galley_from_rows(
num_vertices,
num_indices,
pixels_per_point: point_scale.pixels_per_point,
intrinsic_size,
};
if galley.job.round_output_to_gui {