Auto merge of #27184 - jdm:font-crash, r=asajeffrey

Bail out from font loading if no default font can be loaded.

The font loading setup in font-kit doesn't quite work in UWP yet. This change avoids panics in web content that calls fillText while that is the case.

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #27166
- [x] These changes do not require tests because they only occur on platforms that we can't test.
This commit is contained in:
bors-servo 2020-07-06 22:31:32 -04:00 committed by GitHub
commit 4a0cd44ee5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -528,13 +528,21 @@ impl<'a> CanvasData<'a> {
// are not already in the memory.
if let Some(bytes) = font.handle.template().bytes_if_in_memory() {
Font::from_bytes(Arc::new(bytes), 0)
.unwrap_or_else(|_| load_system_font_from_style(Some(style)))
.ok()
.or_else(|| load_system_font_from_style(Some(style)))
} else {
load_system_font_from_style(Some(style))
}
})
},
);
let font = match font {
Some(f) => f,
None => {
error!("Couldn't load desired font or system fallback.");
return;
},
};
let font_width = font_width(&text, point_size, &font);
// Step 6.
@ -1390,7 +1398,7 @@ fn to_font_kit_family(font_family: &font::SingleFontFamily) -> FamilyName {
}
}
fn load_system_font_from_style(font_style: Option<&FontStyleStruct>) -> Font {
fn load_system_font_from_style(font_style: Option<&FontStyleStruct>) -> Option<Font> {
let mut properties = Properties::new();
let style = match font_style {
Some(style) => style,
@ -1420,18 +1428,21 @@ fn load_system_font_from_style(font_style: Option<&FontStyleStruct>) -> Font {
return load_default_system_fallback_font(&properties);
},
};
font_handle.load().unwrap_or_else(|e| {
error!("error loading font for style {:?}: {}", style, e);
load_default_system_fallback_font(&properties)
})
match font_handle.load() {
Ok(f) => Some(f),
Err(e) => {
error!("error loading font for style {:?}: {}", style, e);
load_default_system_fallback_font(&properties)
},
}
}
fn load_default_system_fallback_font(properties: &Properties) -> Font {
fn load_default_system_fallback_font(properties: &Properties) -> Option<Font> {
SystemSource::new()
.select_best_match(&[FamilyName::SansSerif], properties)
.expect("error getting font handle for default system font")
.ok()?
.load()
.expect("error loading default system font")
.ok()
}
fn replace_ascii_whitespace(text: String) -> String {