Fix crash when configured font is missing

Fixes #3942.
This commit is contained in:
Christian Duerr 2020-07-10 23:41:45 +00:00 committed by GitHub
parent 9ece44e762
commit 521a58d691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -69,6 +69,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Cursor color escape ignored when its color is set to inverted in the config
- Fontconfig's `autohint` and `hinting` options being ignored
- Ingoring of default FreeType properties
- Alacritty crashing at startup when the configured font does not exist
## 0.4.3

View File

@ -10,7 +10,7 @@ use std::time::Duration;
use fnv::FnvHasher;
use font::{
self, BitmapBuffer, FontDesc, FontKey, GlyphKey, Rasterize, RasterizedGlyph, Rasterizer,
self, BitmapBuffer, FontDesc, FontKey, GlyphKey, Rasterize, RasterizedGlyph, Rasterizer, Size,
};
use log::{error, info};
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
@ -224,7 +224,7 @@ impl GlyphCache {
let regular_desc =
Self::make_desc(&font.normal(), font::Slant::Normal, font::Weight::Normal);
let regular = rasterizer.load_font(&regular_desc, size)?;
let regular = Self::load_regular_font(rasterizer, &regular_desc, size)?;
// Helper to load a description if it is not the `regular_desc`.
let mut load_or_regular = |desc: FontDesc| {
@ -255,6 +255,26 @@ impl GlyphCache {
Ok((regular, bold, italic, bold_italic))
}
fn load_regular_font(
rasterizer: &mut Rasterizer,
description: &FontDesc,
size: Size,
) -> Result<FontKey, font::Error> {
match rasterizer.load_font(description, size) {
Ok(font) => Ok(font),
Err(err) => {
error!("{}", err);
let fallback_desc = Self::make_desc(
&Font::default().normal(),
font::Slant::Normal,
font::Weight::Normal,
);
rasterizer.load_font(&fallback_desc, size)
},
}
}
fn make_desc(
desc: &config::FontDescription,
slant: font::Slant,
@ -343,7 +363,7 @@ impl GlyphCache {
let mut rasterizer = font::Rasterizer::new(dpr as f32, font.use_thin_strokes())?;
let regular_desc =
GlyphCache::make_desc(&font.normal(), font::Slant::Normal, font::Weight::Normal);
let regular = rasterizer.load_font(&regular_desc, font.size)?;
let regular = Self::load_regular_font(&mut rasterizer, &regular_desc, font.size)?;
rasterizer.get_glyph(GlyphKey { font_key: regular, c: 'm', size: font.size })?;
rasterizer.metrics(regular, font.size)