Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the largest ascent as the ascent for a font #5126

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 3 additions & 6 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Default for FontTweak {
scale: 1.0,
y_offset_factor: 0.0,
y_offset: 0.0,
baseline_offset_factor: -0.0333, // makes the default fonts look more centered in buttons and such
baseline_offset_factor: 0.0,
}
}
}
Expand Down Expand Up @@ -289,11 +289,8 @@ impl Default for FontDefinitions {
font_data.insert(
"emoji-icon-font".to_owned(),
FontData::from_static(EMOJI_ICON).tweak(FontTweak {
scale: 0.88, // make it smaller

// probably not correct, but this does make texts look better (#2724 for details)
y_offset_factor: 0.11, // move glyphs down to better align with common fonts
baseline_offset_factor: -0.11, // ...now the entire row is a bit down so shift it back
scale: 0.88, // Make it smaller
y_offset_factor: 0.04, // Move down slightly
..Default::default()
}),
);
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
Loading