Skip to content

Commit

Permalink
Use the largest ascent as the ascent for a font
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 18, 2024
1 parent f4ed394 commit 9947d7c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions crates/epaint/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,18 @@ impl Font {
(Some(font_impl), glyph_info)
}

pub(crate) fn ascent(&self) -> f32 {
if self.fonts.is_empty() {
self.row_height
} else {
let mut max_ascent = 0.0;
for font in &self.fonts {
max_ascent = f32::max(max_ascent, font.ascent());
}
max_ascent
}
}

fn glyph_info_no_cache_or_fallback(&mut self, c: char) -> Option<(FontIndex, GlyphInfo)> {
for (font_index, font_impl) in self.fonts.iter().enumerate() {
if let Some(glyph_info) = font_impl.glyph_info(c) {
Expand Down
8 changes: 4 additions & 4 deletions crates/epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn layout_section(
chr,
pos: pos2(paragraph.cursor_x, f32::NAN),
size: vec2(glyph_info.advance_width, line_height),
ascent: font_impl.map_or(0.0, |font| font.ascent()), // Failure to find the font here would be weird
ascent: font.ascent(),
uv_rect: glyph_info.uv_rect,
section_index,
});
Expand Down Expand Up @@ -392,7 +392,7 @@ fn replace_last_glyph_with_overflow_character(
chr: overflow_character,
pos: pos2(x, f32::NAN),
size: vec2(replacement_glyph_info.advance_width, line_height),
ascent: font_impl.map_or(0.0, |font| font.ascent()), // Failure to find the font here would be weird
ascent: font.ascent(),
uv_rect: replacement_glyph_info.uv_rect,
section_index,
});
Expand All @@ -404,13 +404,13 @@ fn replace_last_glyph_with_overflow_character(

let x = 0.0; // TODO(emilk): heed paragraph leading_space 😬

let (font_impl, replacement_glyph_info) = font.font_impl_and_glyph_info(overflow_character);
let (_, replacement_glyph_info) = font.font_impl_and_glyph_info(overflow_character);

row.glyphs.push(Glyph {
chr: overflow_character,
pos: pos2(x, f32::NAN),
size: vec2(replacement_glyph_info.advance_width, line_height),
ascent: font_impl.map_or(0.0, |font| font.ascent()), // Failure to find the font here would be weird
ascent: font.ascent(),
uv_rect: replacement_glyph_info.uv_rect,
section_index,
});
Expand Down

0 comments on commit 9947d7c

Please sign in to comment.