Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 9 additions & 6 deletions crates/epaint/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,15 @@ impl Font<'_> {
/// Width of this character in points.
pub fn glyph_width(&mut self, c: char, font_size: f32) -> f32 {
let (key, glyph_info) = self.glyph_info(c);
let font = &self
.fonts_by_id
.get(&key)
.expect("Nonexistent font ID")
.ab_glyph_font;
glyph_info.advance_width_unscaled.0 * font.px_scale_factor(font_size)
if let Some(font) = &self.fonts_by_id.get(&key) {
glyph_info.advance_width_unscaled.0 * font.ab_glyph_font.px_scale_factor(font_size)
} else {
debug_assert_eq!(
glyph_info.advance_width_unscaled.0, 0.0,
"We had no font, so this should be the zero-width fallback glyph"
);
Comment thread
lucasmerlin marked this conversation as resolved.
Outdated
0.0
}
}

/// Can we display this glyph?
Expand Down
11 changes: 11 additions & 0 deletions crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ impl FontsView<'_> {
}

/// Width of this character in points.
///
/// If the font doesn't exist, this will return `0.0`.
pub fn glyph_width(&mut self, font_id: &FontId, c: char) -> f32 {
self.fonts
.font(&font_id.family)
Expand Down Expand Up @@ -1302,4 +1304,13 @@ mod tests {
}
}
}

#[test]
fn test_fallback_glyph_width() {
let mut fonts = Fonts::new(1024, AlphaFromCoverage::default(), FontDefinitions::empty());
let mut view = fonts.with_pixels_per_point(1.0);

let width = view.glyph_width(&FontId::new(12.0, FontFamily::Proportional), ' ');
assert_eq!(width, 0.0);
}
}
Loading