1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 23:21:08 +03:00

macos: fixup fallback to JetBrains Mono

Split the font loading into two phases; first phase for non-fallback
fonts, second phase for fallback fonts.

This allows all potential sources to be searched for the preferred
fonts before populating the handles list with fallback fonts.
This commit is contained in:
Wez Furlong 2020-10-12 17:05:53 -07:00
parent da2bba866d
commit ad6197e4bc
7 changed files with 62 additions and 22 deletions

View File

@ -219,7 +219,9 @@ impl Library {
FontDataHandle::OnDisk { path, index } => {
self.new_face(path.to_str().unwrap(), *index as _)
}
FontDataHandle::Memory { data, index } => self.new_face_from_slice(&data, *index as _),
FontDataHandle::Memory { data, index, .. } => {
self.new_face_from_slice(&data, *index as _)
}
}
}

View File

@ -36,6 +36,7 @@ where
Ok(Handle::Memory { bytes, font_index }) => handles.push(FontDataHandle::Memory {
data: bytes.to_vec(),
index: font_index,
name: font.family.clone(),
}),
Err(_) => continue,
}

View File

@ -43,6 +43,7 @@ impl FontLocator for FontLoaderFontLocator {
let handle = FontDataHandle::Memory {
data,
index: index as u32,
name: font_attr.family.clone(),
};
fonts.push(handle);
loaded.insert(font_attr.clone());

View File

@ -23,6 +23,7 @@ pub enum FontDataHandle {
},
#[allow(dead_code)]
Memory {
name: String,
data: Vec<u8>,
index: u32,
},
@ -36,8 +37,9 @@ impl std::fmt::Debug for FontDataHandle {
.field("path", &path)
.field("index", &index)
.finish(),
Self::Memory { data, index } => fmt
Self::Memory { data, index, name } => fmt
.debug_struct("Memory")
.field("name", &name)
.field("data_len", &data.len())
.field("index", &index)
.finish(),

View File

@ -110,11 +110,37 @@ impl FontConfiguration {
}
let attributes = style.font_with_fallback();
let preferred_attributes = attributes
.iter()
.filter(|a| !a.is_fallback)
.map(|a| a.clone())
.collect::<Vec<_>>();
let fallback_attributes = attributes
.iter()
.filter(|a| a.is_fallback)
.map(|a| a.clone())
.collect::<Vec<_>>();
let mut loaded = HashSet::new();
let mut handles = parser::ParsedFont::load_fonts(&config, &attributes, &mut loaded)?;
handles.append(&mut self.locator.load_fonts(&attributes, &mut loaded)?);
let mut handles =
parser::ParsedFont::load_fonts(&config, &preferred_attributes, &mut loaded)?;
handles.append(
&mut self
.locator
.load_fonts(&preferred_attributes, &mut loaded)?,
);
handles.append(&mut parser::ParsedFont::load_built_in_fonts(
&attributes,
&preferred_attributes,
&mut loaded,
)?);
handles.append(&mut parser::ParsedFont::load_fonts(
&config,
&fallback_attributes,
&mut loaded,
)?);
handles.append(&mut self.locator.load_fonts(&fallback_attributes, &mut loaded)?);
handles.append(&mut parser::ParsedFont::load_built_in_fonts(
&fallback_attributes,
&mut loaded,
)?);

View File

@ -137,7 +137,7 @@ impl ParsedFont {
pub fn from_locator(handle: &FontDataHandle) -> anyhow::Result<Self> {
let (data, index) = match handle {
FontDataHandle::Memory { data, index } => (data.to_vec(), *index),
FontDataHandle::Memory { data, index, .. } => (data.to_vec(), *index),
FontDataHandle::OnDisk { path, index } => {
let data = std::fs::read(path)?;
(data, *index)
@ -493,22 +493,27 @@ fn font_info_matches(attr: &FontAttributes, names: &Names) -> bool {
fn load_built_in_fonts(
font_info: &mut Vec<(Names, PathBuf, FontDataHandle)>,
) -> anyhow::Result<()> {
for data in &[
include_bytes!("../../../assets/fonts/JetBrainsMono-Bold-Italic.ttf") as &'static [u8],
include_bytes!("../../../assets/fonts/JetBrainsMono-Bold.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-ExtraBold-Italic.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-ExtraBold.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-ExtraLight-Italic.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-ExtraLight.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-Italic.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-Light-Italic.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-Light.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-Medium-Italic.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-Medium.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-Regular.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-SemiLight-Italic.ttf"),
include_bytes!("../../../assets/fonts/JetBrainsMono-SemiLight.ttf"),
include_bytes!("../../../assets/fonts/NotoColorEmoji.ttf"),
macro_rules! font {
($font:literal) => {
(include_bytes!($font) as &'static [u8], $font)
};
}
for (data, name) in &[
font!("../../../assets/fonts/JetBrainsMono-Bold-Italic.ttf"),
font!("../../../assets/fonts/JetBrainsMono-Bold.ttf"),
font!("../../../assets/fonts/JetBrainsMono-ExtraBold-Italic.ttf"),
font!("../../../assets/fonts/JetBrainsMono-ExtraBold.ttf"),
font!("../../../assets/fonts/JetBrainsMono-ExtraLight-Italic.ttf"),
font!("../../../assets/fonts/JetBrainsMono-ExtraLight.ttf"),
font!("../../../assets/fonts/JetBrainsMono-Italic.ttf"),
font!("../../../assets/fonts/JetBrainsMono-Light-Italic.ttf"),
font!("../../../assets/fonts/JetBrainsMono-Light.ttf"),
font!("../../../assets/fonts/JetBrainsMono-Medium-Italic.ttf"),
font!("../../../assets/fonts/JetBrainsMono-Medium.ttf"),
font!("../../../assets/fonts/JetBrainsMono-Regular.ttf"),
font!("../../../assets/fonts/JetBrainsMono-SemiLight-Italic.ttf"),
font!("../../../assets/fonts/JetBrainsMono-SemiLight.ttf"),
font!("../../../assets/fonts/NotoColorEmoji.ttf"),
] {
let scope = allsorts::binary::read::ReadScope::new(&data);
let file = scope.read::<OpenTypeFile>()?;
@ -527,6 +532,7 @@ fn load_built_in_fonts(
FontDataHandle::Memory {
data: data.to_vec(),
index: 0,
name: name.to_string(),
},
));
}
@ -546,6 +552,7 @@ fn load_built_in_fonts(
FontDataHandle::Memory {
data: data.to_vec(),
index: index.try_into()?,
name: name.to_string(),
},
));
}

View File

@ -279,6 +279,7 @@ impl FontShaper for HarfbuzzShaper {
// by too much we'll skip to the next slot.
let theoretical_height = size * dpi as f64 / 72.0;
let mut metrics_idx = 0;
log::trace!("{:?}", self.handles);
while let Ok(Some(mut pair)) = self.load_fallback(metrics_idx) {
let (_, cell_height) = pair.face.set_font_size(size, dpi)?;
let diff = (theoretical_height - cell_height).abs();