Return `0.0` if font not found in `glyph_width` instead of panic (#7559)
This commit is contained in:
parent
5ee88da61c
commit
47c5617740
|
|
@ -478,12 +478,11 @@ 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 {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Can we display this glyph?
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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…
Reference in New Issue