fonts: avoid making copies of Memory font handles

refs: https://github.com/wez/wezterm/issues/559
This commit is contained in:
Wez Furlong committed 2021-03-20 22:49:27 -07:00
1 parent 5eb6a15312
commit 72f2ff850f
6 files changed
+18 -12

No files matched your search

+5 -5
View File
@@ -5,6 +5,7 @@ use crate::FontDataHandle;
use anyhow::{anyhow, Context};
use config::{Config, FontAttributes};
use rangeset::RangeSet;
use std::borrow::Cow;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
@@ -22,17 +23,16 @@ impl Entry {
/// the set of codepoints for which the font has coverage.
fn compute_coverage(&self) -> anyhow::Result<RangeSet<u32>> {
use ttf_parser::Face;
let on_disk_data;
let (data, index) = match &self.handle {
FontDataHandle::Memory { data, index, .. } => (data, *index),
FontDataHandle::Memory { data, index, .. } => (data.clone(), *index),
FontDataHandle::OnDisk { path, index } => {
on_disk_data = std::fs::read(path)
let data = std::fs::read(path)
.with_context(|| anyhow!("reading font data from {}", path.display()))?;
(&on_disk_data, *index)
(Cow::Owned(data), *index)
}
};
let face = Face::from_slice(data, index)?;
let face = Face::from_slice(&data, index)?;
let mut coverage = RangeSet::new();
for table in face.character_mapping_subtables() {
+5 -1
View File
@@ -329,7 +329,11 @@ impl Library {
// face because freetype doesn't use O_CLOEXEC and keeps the fd
// floating around for a long time!
let data = std::fs::read(path)?;
log::trace!("Loading {} for freetype!", path.display());
log::trace!(
"Loading {} ({} bytes) for freetype!",
path.display(),
data.len()
);
let res = unsafe {
FT_New_Memory_Face(
+2 -1
View File
@@ -9,6 +9,7 @@ use core_foundation::number::CFNumber;
use core_foundation::string::CFString;
use core_text::font::*;
use core_text::font_descriptor::*;
use std::borrow::Cow;
use std::collections::HashSet;
use ttf_parser::fonts_in_collection;
@@ -58,7 +59,7 @@ fn handle_from_descriptor(descriptor: &CTFontDescriptor) -> Option<FontDataHandl
let size = fonts_in_collection(&data).unwrap_or(1);
let mut handle = FontDataHandle::Memory {
data,
data: Cow::Owned(data),
name,
index: 0,
};
+3 -2
View File
@@ -4,6 +4,7 @@ use crate::locator::{FontDataHandle, FontLocator};
use config::FontAttributes;
use dwrote::{FontStretch, FontStyle, FontWeight};
use std::borrow::Cow;
use std::borrow::Cow;
use std::collections::HashSet;
use winapi::shared::windef::HFONT;
use winapi::um::dwrite::*;
@@ -43,7 +44,7 @@ fn extract_font_data(font: HFONT, attr: &FontAttributes) -> anyhow::Result<FontD
let index =
crate::parser::resolve_font_from_ttc_data(&attr, &data)?.unwrap_or(0) as u32;
Ok(FontDataHandle::Memory {
data,
data: Cow::Owned(data),
index,
name: attr.family.clone(),
})
@@ -56,7 +57,7 @@ fn extract_font_data(font: HFONT, attr: &FontAttributes) -> anyhow::Result<FontD
let mut data = vec![0u8; size as usize];
GetFontData(hdc, 0, 0, data.as_mut_ptr() as *mut _, size);
Ok(FontDataHandle::Memory {
data,
data: Cow::Owned(data),
index: 0,
name: attr.family.clone(),
})
+1 -2
View File
@@ -19,10 +19,9 @@ pub enum FontDataHandle {
path: PathBuf,
index: u32,
},
#[allow(dead_code)]
Memory {
name: String,
data: Vec<u8>,
data: std::borrow::Cow<'static, [u8]>,
index: u32,
},
}
+2 -1
View File
@@ -2,6 +2,7 @@ use crate::locator::FontDataHandle;
use crate::shaper::GlyphInfo;
use anyhow::anyhow;
use config::FontAttributes;
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use ttf_parser::{fonts_in_collection, Face, Name, PlatformId};
@@ -338,7 +339,7 @@ pub(crate) fn load_built_in_fonts(
names,
PathBuf::from(name),
FontDataHandle::Memory {
data: data.to_vec(),
data: Cow::Borrowed(data),
index: 0,
name: name.to_string(),
},