diff --git a/src/config/font.rs b/src/config/font.rs index 045080269..4b3c91ae7 100644 --- a/src/config/font.rs +++ b/src/config/font.rs @@ -207,3 +207,158 @@ pub struct StyleRule { pub font: TextStyle, } impl_lua_conversion!(StyleRule); + +#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)] +pub enum FontLocatorSelection { + /// Use fontconfig APIs to resolve fonts (!macos, posix systems) + FontConfig, + /// Use the fontloader crate to use a system specific method of + /// resolving fonts + FontLoader, + /// Use the fontkit crate to use a different system specific + /// method of resolving fonts + FontKit, + /// Use only the font_dirs configuration to locate fonts + ConfigDirsOnly, +} + +lazy_static::lazy_static! { + static ref DEFAULT_LOCATOR: Mutex = Mutex::new(Default::default()); +} + +impl Default for FontLocatorSelection { + fn default() -> Self { + if cfg!(all(unix, not(target_os = "macos"))) { + FontLocatorSelection::FontConfig + } else { + FontLocatorSelection::FontLoader + } + } +} + +impl FontLocatorSelection { + pub fn set_default(self) { + let mut def = DEFAULT_LOCATOR.lock().unwrap(); + *def = self; + } + + pub fn get_default() -> Self { + let def = DEFAULT_LOCATOR.lock().unwrap(); + *def + } + + pub fn variants() -> Vec<&'static str> { + vec!["FontConfig", "FontLoader", "FontKit", "ConfigDirsOnly"] + } +} + +impl std::str::FromStr for FontLocatorSelection { + type Err = Error; + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_ref() { + "fontconfig" => Ok(Self::FontConfig), + "fontloader" => Ok(Self::FontLoader), + "fontkit" => Ok(Self::FontKit), + "configdirsonly" => Ok(Self::ConfigDirsOnly), + _ => Err(anyhow!( + "{} is not a valid FontLocatorSelection variant, possible values are {:?}", + s, + Self::variants() + )), + } + } +} + +#[derive(Debug, Deserialize, Clone, Copy)] +pub enum FontRasterizerSelection { + FreeType, + FontKit, +} + +lazy_static::lazy_static! { + static ref DEFAULT_RASTER: Mutex = Mutex::new(Default::default()); +} + +impl Default for FontRasterizerSelection { + fn default() -> Self { + FontRasterizerSelection::FreeType + } +} + +impl FontRasterizerSelection { + pub fn set_default(self) { + let mut def = DEFAULT_RASTER.lock().unwrap(); + *def = self; + } + + pub fn get_default() -> Self { + let def = DEFAULT_RASTER.lock().unwrap(); + *def + } + + pub fn variants() -> Vec<&'static str> { + vec!["FreeType", "FontKit"] + } +} + +impl std::str::FromStr for FontRasterizerSelection { + type Err = Error; + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_ref() { + "freetype" => Ok(Self::FreeType), + "fontkit" => Ok(Self::FontKit), + _ => Err(anyhow!( + "{} is not a valid FontRasterizerSelection variant, possible values are {:?}", + s, + Self::variants() + )), + } + } +} + +#[derive(Debug, Deserialize, Clone, Copy)] +pub enum FontShaperSelection { + Allsorts, + Harfbuzz, +} + +lazy_static::lazy_static! { + static ref DEFAULT_SHAPER: Mutex = Mutex::new(Default::default()); +} + +impl Default for FontShaperSelection { + fn default() -> Self { + FontShaperSelection::Harfbuzz + } +} + +impl FontShaperSelection { + pub fn set_default(self) { + let mut def = DEFAULT_SHAPER.lock().unwrap(); + *def = self; + } + + pub fn get_default() -> Self { + let def = DEFAULT_SHAPER.lock().unwrap(); + *def + } + + pub fn variants() -> Vec<&'static str> { + vec!["Harfbuzz", "AllSorts"] + } +} + +impl std::str::FromStr for FontShaperSelection { + type Err = Error; + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_ref() { + "harfbuzz" => Ok(Self::Harfbuzz), + "allsorts" => Ok(Self::Allsorts), + _ => Err(anyhow!( + "{} is not a valid FontShaperSelection variant, possible values are {:?}", + s, + Self::variants() + )), + } + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index 33ced9bb9..997414597 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -2,9 +2,6 @@ use crate::config::keyassignment::{KeyAssignment, MouseEventTrigger, SpawnCommand}; use crate::create_user_owned_dirs; -use crate::font::locator::FontLocatorSelection; -use crate::font::rasterizer::FontRasterizerSelection; -use crate::font::shaper::FontShaperSelection; use anyhow::{anyhow, bail, Context, Error}; use lazy_static::lazy_static; use luahelper::impl_lua_conversion; diff --git a/src/font/locator/mod.rs b/src/font/locator/mod.rs index 4ccf51c1b..c2d6c190f 100644 --- a/src/font/locator/mod.rs +++ b/src/font/locator/mod.rs @@ -1,8 +1,5 @@ use crate::config::FontAttributes; -use anyhow::{anyhow, Error}; -use serde::Deserialize; use std::path::PathBuf; -use std::sync::Mutex; #[cfg(all(unix, not(target_os = "macos")))] pub mod font_config; @@ -54,93 +51,34 @@ pub trait FontLocator { -> anyhow::Result>; } -#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)] -pub enum FontLocatorSelection { - /// Use fontconfig APIs to resolve fonts (!macos, posix systems) - FontConfig, - /// Use the fontloader crate to use a system specific method of - /// resolving fonts - FontLoader, - /// Use the fontkit crate to use a different system specific - /// method of resolving fonts - FontKit, - /// Use only the font_dirs configuration to locate fonts - ConfigDirsOnly, -} - -lazy_static::lazy_static! { - static ref DEFAULT_LOCATOR: Mutex = Mutex::new(Default::default()); -} - -impl Default for FontLocatorSelection { - fn default() -> Self { - if cfg!(all(unix, not(target_os = "macos"))) { - FontLocatorSelection::FontConfig - } else { - FontLocatorSelection::FontLoader +pub fn new_locator(locator: FontLocatorSelection) -> Box { + match locator { + FontLocatorSelection::FontConfig => { + #[cfg(all(unix, not(target_os = "macos")))] + return Box::new(font_config::FontConfigFontLocator {}); + #[cfg(not(all(unix, not(target_os = "macos"))))] + panic!("fontconfig not compiled in"); } - } -} - -impl FontLocatorSelection { - pub fn set_default(self) { - let mut def = DEFAULT_LOCATOR.lock().unwrap(); - *def = self; - } - - pub fn get_default() -> Self { - let def = DEFAULT_LOCATOR.lock().unwrap(); - *def - } - - pub fn variants() -> Vec<&'static str> { - vec!["FontConfig", "FontLoader", "FontKit", "ConfigDirsOnly"] - } - - pub fn new_locator(self) -> Box { - match self { - Self::FontConfig => { - #[cfg(all(unix, not(target_os = "macos")))] - return Box::new(font_config::FontConfigFontLocator {}); - #[cfg(not(all(unix, not(target_os = "macos"))))] - panic!("fontconfig not compiled in"); - } - Self::FontLoader => { - #[cfg(any(target_os = "macos", windows))] - return Box::new(font_loader::FontLoaderFontLocator {}); - #[cfg(not(any(target_os = "macos", windows)))] - panic!("fontloader not compiled in"); - } - Self::FontKit => { - #[cfg(any(target_os = "macos", windows))] - return Box::new(::font_kit::source::SystemSource::new()); - #[cfg(not(any(target_os = "macos", windows)))] - panic!("fontkit not compiled in"); - } - Self::ConfigDirsOnly => Box::new(NopSystemSource {}), + FontLocatorSelection::FontLoader => { + #[cfg(any(target_os = "macos", windows))] + return Box::new(font_loader::FontLoaderFontLocator {}); + #[cfg(not(any(target_os = "macos", windows)))] + panic!("fontloader not compiled in"); } - } -} - -impl std::str::FromStr for FontLocatorSelection { - type Err = Error; - fn from_str(s: &str) -> Result { - match s.to_lowercase().as_ref() { - "fontconfig" => Ok(Self::FontConfig), - "fontloader" => Ok(Self::FontLoader), - "fontkit" => Ok(Self::FontKit), - "configdirsonly" => Ok(Self::ConfigDirsOnly), - _ => Err(anyhow!( - "{} is not a valid FontLocatorSelection variant, possible values are {:?}", - s, - Self::variants() - )), + FontLocatorSelection::FontKit => { + #[cfg(any(target_os = "macos", windows))] + return Box::new(::font_kit::source::SystemSource::new()); + #[cfg(not(any(target_os = "macos", windows)))] + panic!("fontkit not compiled in"); } + FontLocatorSelection::ConfigDirsOnly => Box::new(NopSystemSource {}), } } struct NopSystemSource {} +pub use crate::config::FontLocatorSelection; + impl FontLocator for NopSystemSource { fn load_fonts( &self, diff --git a/src/font/mod.rs b/src/font/mod.rs index 09c1a600a..986f65f1b 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -15,11 +15,11 @@ pub mod units; #[cfg(all(unix, not(target_os = "macos")))] pub mod fcwrap; -use crate::font::locator::{FontDataHandle, FontLocator, FontLocatorSelection}; +use crate::font::locator::{new_locator, FontDataHandle, FontLocator, FontLocatorSelection}; pub use crate::font::rasterizer::RasterizedGlyph; -use crate::font::rasterizer::{FontRasterizer, FontRasterizerSelection}; +use crate::font::rasterizer::{new_rasterizer, FontRasterizer, FontRasterizerSelection}; +use crate::font::shaper::{new_shaper, FontShaper, FontShaperSelection}; pub use crate::font::shaper::{FallbackIdx, FontMetrics, GlyphInfo}; -use crate::font::shaper::{FontShaper, FontShaperSelection}; use super::config::{configuration, ConfigHandle, TextStyle}; use wezterm_term::CellAttributes; @@ -53,8 +53,10 @@ impl LoadedFont { .ok_or_else(|| anyhow!("no such fallback index: {}", fallback))?; let mut opt_raster = cell.borrow_mut(); if opt_raster.is_none() { - let raster = - FontRasterizerSelection::get_default().new_rasterizer(&self.handles[fallback])?; + let raster = new_rasterizer( + FontRasterizerSelection::get_default(), + &self.handles[fallback], + )?; opt_raster.replace(raster); } @@ -78,7 +80,7 @@ pub struct FontConfiguration { impl FontConfiguration { /// Create a new empty configuration pub fn new() -> Self { - let locator = FontLocatorSelection::get_default().new_locator(); + let locator = new_locator(FontLocatorSelection::get_default()); Self { fonts: RefCell::new(HashMap::new()), locator, @@ -116,7 +118,7 @@ impl FontConfiguration { for _ in &handles { rasterizers.push(RefCell::new(None)); } - let shaper = FontShaperSelection::get_default().new_shaper(&handles)?; + let shaper = new_shaper(FontShaperSelection::get_default(), &handles)?; let config = configuration(); let font_size = config.font_size * *self.font_scale.borrow(); diff --git a/src/font/rasterizer/mod.rs b/src/font/rasterizer/mod.rs index 0851217ad..22375b30b 100644 --- a/src/font/rasterizer/mod.rs +++ b/src/font/rasterizer/mod.rs @@ -1,8 +1,6 @@ use crate::font::locator::FontDataHandle; use crate::font::units::*; -use anyhow::{anyhow, bail, Error}; -use serde::Deserialize; -use std::sync::Mutex; +use anyhow::bail; pub mod freetype; @@ -28,61 +26,16 @@ pub trait FontRasterizer { ) -> anyhow::Result; } -#[derive(Debug, Deserialize, Clone, Copy)] -pub enum FontRasterizerSelection { - FreeType, - FontKit, -} +pub use crate::config::FontRasterizerSelection; -lazy_static::lazy_static! { - static ref DEFAULT_RASTER: Mutex = Mutex::new(Default::default()); -} - -impl Default for FontRasterizerSelection { - fn default() -> Self { - FontRasterizerSelection::FreeType - } -} - -impl FontRasterizerSelection { - pub fn set_default(self) { - let mut def = DEFAULT_RASTER.lock().unwrap(); - *def = self; - } - - pub fn get_default() -> Self { - let def = DEFAULT_RASTER.lock().unwrap(); - *def - } - - pub fn variants() -> Vec<&'static str> { - vec!["FreeType", "FontKit"] - } - - pub fn new_rasterizer( - self, - handle: &FontDataHandle, - ) -> anyhow::Result> { - match self { - Self::FreeType => Ok(Box::new(freetype::FreeTypeRasterizer::from_locator( - handle, - )?)), - Self::FontKit => bail!("FontKit rasterizer not implemented yet"), - } - } -} - -impl std::str::FromStr for FontRasterizerSelection { - type Err = Error; - fn from_str(s: &str) -> Result { - match s.to_lowercase().as_ref() { - "freetype" => Ok(Self::FreeType), - "fontkit" => Ok(Self::FontKit), - _ => Err(anyhow!( - "{} is not a valid FontRasterizerSelection variant, possible values are {:?}", - s, - Self::variants() - )), - } +pub fn new_rasterizer( + rasterizer: FontRasterizerSelection, + handle: &FontDataHandle, +) -> anyhow::Result> { + match rasterizer { + FontRasterizerSelection::FreeType => Ok(Box::new( + freetype::FreeTypeRasterizer::from_locator(handle)?, + )), + FontRasterizerSelection::FontKit => bail!("FontKit rasterizer not implemented yet"), } } diff --git a/src/font/shaper/mod.rs b/src/font/shaper/mod.rs index 5a92ba124..6532e84b3 100644 --- a/src/font/shaper/mod.rs +++ b/src/font/shaper/mod.rs @@ -1,8 +1,5 @@ use crate::font::locator::FontDataHandle; use crate::font::units::PixelLength; -use anyhow::{anyhow, Error}; -use serde::Deserialize; -use std::sync::Mutex; pub mod allsorts; pub mod harfbuzz; @@ -64,56 +61,14 @@ pub trait FontShaper { fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result; } -#[derive(Debug, Deserialize, Clone, Copy)] -pub enum FontShaperSelection { - Allsorts, - Harfbuzz, -} +pub use crate::config::FontShaperSelection; -lazy_static::lazy_static! { - static ref DEFAULT_SHAPER: Mutex = Mutex::new(Default::default()); -} - -impl Default for FontShaperSelection { - fn default() -> Self { - FontShaperSelection::Harfbuzz - } -} - -impl FontShaperSelection { - pub fn set_default(self) { - let mut def = DEFAULT_SHAPER.lock().unwrap(); - *def = self; - } - - pub fn get_default() -> Self { - let def = DEFAULT_SHAPER.lock().unwrap(); - *def - } - - pub fn variants() -> Vec<&'static str> { - vec!["Harfbuzz", "AllSorts"] - } - - pub fn new_shaper(self, handles: &[FontDataHandle]) -> anyhow::Result> { - match self { - Self::Harfbuzz => Ok(Box::new(harfbuzz::HarfbuzzShaper::new(handles)?)), - Self::Allsorts => Ok(Box::new(allsorts::AllsortsShaper::new(handles)?)), - } - } -} - -impl std::str::FromStr for FontShaperSelection { - type Err = Error; - fn from_str(s: &str) -> Result { - match s.to_lowercase().as_ref() { - "harfbuzz" => Ok(Self::Harfbuzz), - "allsorts" => Ok(Self::Allsorts), - _ => Err(anyhow!( - "{} is not a valid FontShaperSelection variant, possible values are {:?}", - s, - Self::variants() - )), - } +pub fn new_shaper( + shaper: FontShaperSelection, + handles: &[FontDataHandle], +) -> anyhow::Result> { + match shaper { + FontShaperSelection::Harfbuzz => Ok(Box::new(harfbuzz::HarfbuzzShaper::new(handles)?)), + FontShaperSelection::Allsorts => Ok(Box::new(allsorts::AllsortsShaper::new(handles)?)), } }