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

fonts: FontDataSource::Memory -> BuiltIn

This makes it harder to accidentally copy the associated data.
This commit is contained in:
Wez Furlong 2021-04-09 22:44:58 -07:00
parent f9bdd2502a
commit 3ce44823a0
3 changed files with 19 additions and 14 deletions

View File

@ -6,7 +6,6 @@ use anyhow::{anyhow, Context};
use config::{configuration, FreeTypeLoadTarget}; use config::{configuration, FreeTypeLoadTarget};
pub use freetype::*; pub use freetype::*;
use rangeset::RangeSet; use rangeset::RangeSet;
use std::borrow::Cow;
use std::convert::TryInto; use std::convert::TryInto;
use std::ffi::CStr; use std::ffi::CStr;
use std::ptr; use std::ptr;
@ -496,8 +495,11 @@ impl Library {
pub fn query_num_faces(&self, source: &FontDataSource) -> anyhow::Result<u32> { pub fn query_num_faces(&self, source: &FontDataSource) -> anyhow::Result<u32> {
let face = match source { let face = match source {
FontDataSource::OnDisk(path) => self.new_face(path, -1).context("query_num_faces")?, FontDataSource::OnDisk(path) => self.new_face(path, -1).context("query_num_faces")?,
FontDataSource::BuiltIn { data, .. } => self
.new_face_from_slice(data, -1)
.context("query_num_faces")?,
FontDataSource::Memory { data, .. } => self FontDataSource::Memory { data, .. } => self
.new_face_from_slice(&data, -1) .new_face_from_slice(&*data, -1)
.context("query_num_faces")?, .context("query_num_faces")?,
}; };
let num_faces = unsafe { (*face).num_faces }.try_into(); let num_faces = unsafe { (*face).num_faces }.try_into();
@ -519,6 +521,7 @@ impl Library {
let face = match &source.source { let face = match &source.source {
FontDataSource::OnDisk(path) => self.new_face(path.to_str().unwrap(), index as _), FontDataSource::OnDisk(path) => self.new_face(path.to_str().unwrap(), index as _),
FontDataSource::BuiltIn { data, .. } => self.new_face_from_slice(&data, index as _),
FontDataSource::Memory { data, .. } => self.new_face_from_slice(&data, index as _), FontDataSource::Memory { data, .. } => self.new_face_from_slice(&data, index as _),
} }
.with_context(|| format!("face_from_locator({:?})", handle))?; .with_context(|| format!("face_from_locator({:?})", handle))?;
@ -563,11 +566,7 @@ impl Library {
); );
} }
fn new_face_from_slice( fn new_face_from_slice(&self, data: &[u8], face_index: FT_Long) -> anyhow::Result<FT_Face> {
&self,
data: &Cow<'static, [u8]>,
face_index: FT_Long,
) -> anyhow::Result<FT_Face> {
let mut face = ptr::null_mut(); let mut face = ptr::null_mut();
let res = unsafe { let res = unsafe {

View File

@ -14,9 +14,13 @@ pub mod gdi;
#[derive(Clone)] #[derive(Clone)]
pub enum FontDataSource { pub enum FontDataSource {
OnDisk(PathBuf), OnDisk(PathBuf),
BuiltIn {
name: &'static str,
data: &'static [u8],
},
Memory { Memory {
name: String, name: String,
data: Cow<'static, [u8]>, data: Arc<[u8]>,
}, },
} }
@ -24,6 +28,7 @@ impl FontDataSource {
pub fn name_or_path_str(&self) -> Cow<str> { pub fn name_or_path_str(&self) -> Cow<str> {
match self { match self {
Self::OnDisk(path) => path.to_string_lossy(), Self::OnDisk(path) => path.to_string_lossy(),
Self::BuiltIn { name, .. } => Cow::Borrowed(name),
Self::Memory { name, .. } => Cow::Borrowed(name), Self::Memory { name, .. } => Cow::Borrowed(name),
} }
} }
@ -34,7 +39,8 @@ impl FontDataSource {
let data = std::fs::read(path)?; let data = std::fs::read(path)?;
Ok(Cow::Owned(data)) Ok(Cow::Owned(data))
} }
Self::Memory { data, .. } => Ok(data.clone()), Self::BuiltIn { data, .. } => Ok(Cow::Borrowed(data)),
Self::Memory { data, .. } => Ok(Cow::Borrowed(&*data)),
} }
} }
} }
@ -48,6 +54,9 @@ impl PartialEq for FontDataSource {
(Self::Memory { name: name_a, .. }, Self::Memory { name: name_b, .. }) => { (Self::Memory { name: name_a, .. }, Self::Memory { name: name_b, .. }) => {
name_a == name_b name_a == name_b
} }
(Self::BuiltIn { name: name_a, .. }, Self::BuiltIn { name: name_b, .. }) => {
name_a == name_b
}
_ => false, _ => false,
} }
} }
@ -71,6 +80,7 @@ impl std::fmt::Debug for FontDataSource {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self { match self {
Self::OnDisk(path) => fmt.debug_struct("OnDisk").field("path", &path).finish(), Self::OnDisk(path) => fmt.debug_struct("OnDisk").field("path", &path).finish(),
Self::BuiltIn { name, .. } => fmt.debug_struct("BuiltIn").field("name", &name).finish(),
Self::Memory { data, name } => fmt Self::Memory { data, name } => fmt
.debug_struct("Memory") .debug_struct("Memory")
.field("name", &name) .field("name", &name)

View File

@ -2,7 +2,6 @@ use crate::locator::{FontDataHandle, FontDataSource};
use crate::shaper::GlyphInfo; use crate::shaper::GlyphInfo;
use config::FontAttributes; use config::FontAttributes;
pub use config::{FontStretch, FontWeight}; pub use config::{FontStretch, FontWeight};
use std::borrow::Cow;
#[derive(Debug)] #[derive(Debug)]
pub enum MaybeShaped { pub enum MaybeShaped {
@ -278,10 +277,7 @@ pub(crate) fn load_built_in_fonts(font_info: &mut Vec<ParsedFont>) -> anyhow::Re
font!("../../assets/fonts/LastResortHE-Regular.ttf"), font!("../../assets/fonts/LastResortHE-Regular.ttf"),
] { ] {
let locator = FontDataHandle { let locator = FontDataHandle {
source: FontDataSource::Memory { source: FontDataSource::BuiltIn { data, name },
data: Cow::Borrowed(data),
name: name.to_string(),
},
index: 0, index: 0,
variation: 0, variation: 0,
}; };