1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-30 14:49:26 +03:00

move font configuration enums to config module

This commit is contained in:
Wez Furlong 2020-10-02 10:50:01 -07:00
parent 35f6a3dd9d
commit 01445bf5b8
6 changed files with 203 additions and 203 deletions

View File

@ -207,3 +207,158 @@ pub struct StyleRule {
pub font: TextStyle, pub font: TextStyle,
} }
impl_lua_conversion!(StyleRule); 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<FontLocatorSelection> = 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<Self, Self::Err> {
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<FontRasterizerSelection> = 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<Self, Self::Err> {
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<FontShaperSelection> = 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<Self, Self::Err> {
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()
)),
}
}
}

View File

@ -2,9 +2,6 @@
use crate::config::keyassignment::{KeyAssignment, MouseEventTrigger, SpawnCommand}; use crate::config::keyassignment::{KeyAssignment, MouseEventTrigger, SpawnCommand};
use crate::create_user_owned_dirs; 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 anyhow::{anyhow, bail, Context, Error};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use luahelper::impl_lua_conversion; use luahelper::impl_lua_conversion;

View File

@ -1,8 +1,5 @@
use crate::config::FontAttributes; use crate::config::FontAttributes;
use anyhow::{anyhow, Error};
use serde::Deserialize;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Mutex;
#[cfg(all(unix, not(target_os = "macos")))] #[cfg(all(unix, not(target_os = "macos")))]
pub mod font_config; pub mod font_config;
@ -54,93 +51,34 @@ pub trait FontLocator {
-> anyhow::Result<Vec<FontDataHandle>>; -> anyhow::Result<Vec<FontDataHandle>>;
} }
#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)] pub fn new_locator(locator: FontLocatorSelection) -> Box<dyn FontLocator> {
pub enum FontLocatorSelection { match locator {
/// Use fontconfig APIs to resolve fonts (!macos, posix systems) FontLocatorSelection::FontConfig => {
FontConfig, #[cfg(all(unix, not(target_os = "macos")))]
/// Use the fontloader crate to use a system specific method of return Box::new(font_config::FontConfigFontLocator {});
/// resolving fonts #[cfg(not(all(unix, not(target_os = "macos"))))]
FontLoader, panic!("fontconfig not compiled in");
/// 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<FontLocatorSelection> = Mutex::new(Default::default());
}
impl Default for FontLocatorSelection {
fn default() -> Self {
if cfg!(all(unix, not(target_os = "macos"))) {
FontLocatorSelection::FontConfig
} else {
FontLocatorSelection::FontLoader
} }
} FontLocatorSelection::FontLoader => {
} #[cfg(any(target_os = "macos", windows))]
return Box::new(font_loader::FontLoaderFontLocator {});
impl FontLocatorSelection { #[cfg(not(any(target_os = "macos", windows)))]
pub fn set_default(self) { panic!("fontloader not compiled in");
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<dyn FontLocator> {
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::FontKit => {
} #[cfg(any(target_os = "macos", windows))]
return Box::new(::font_kit::source::SystemSource::new());
impl std::str::FromStr for FontLocatorSelection { #[cfg(not(any(target_os = "macos", windows)))]
type Err = Error; panic!("fontkit not compiled in");
fn from_str(s: &str) -> Result<Self, Self::Err> {
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::ConfigDirsOnly => Box::new(NopSystemSource {}),
} }
} }
struct NopSystemSource {} struct NopSystemSource {}
pub use crate::config::FontLocatorSelection;
impl FontLocator for NopSystemSource { impl FontLocator for NopSystemSource {
fn load_fonts( fn load_fonts(
&self, &self,

View File

@ -15,11 +15,11 @@ pub mod units;
#[cfg(all(unix, not(target_os = "macos")))] #[cfg(all(unix, not(target_os = "macos")))]
pub mod fcwrap; 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; 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}; pub use crate::font::shaper::{FallbackIdx, FontMetrics, GlyphInfo};
use crate::font::shaper::{FontShaper, FontShaperSelection};
use super::config::{configuration, ConfigHandle, TextStyle}; use super::config::{configuration, ConfigHandle, TextStyle};
use wezterm_term::CellAttributes; use wezterm_term::CellAttributes;
@ -53,8 +53,10 @@ impl LoadedFont {
.ok_or_else(|| anyhow!("no such fallback index: {}", fallback))?; .ok_or_else(|| anyhow!("no such fallback index: {}", fallback))?;
let mut opt_raster = cell.borrow_mut(); let mut opt_raster = cell.borrow_mut();
if opt_raster.is_none() { if opt_raster.is_none() {
let raster = let raster = new_rasterizer(
FontRasterizerSelection::get_default().new_rasterizer(&self.handles[fallback])?; FontRasterizerSelection::get_default(),
&self.handles[fallback],
)?;
opt_raster.replace(raster); opt_raster.replace(raster);
} }
@ -78,7 +80,7 @@ pub struct FontConfiguration {
impl FontConfiguration { impl FontConfiguration {
/// Create a new empty configuration /// Create a new empty configuration
pub fn new() -> Self { pub fn new() -> Self {
let locator = FontLocatorSelection::get_default().new_locator(); let locator = new_locator(FontLocatorSelection::get_default());
Self { Self {
fonts: RefCell::new(HashMap::new()), fonts: RefCell::new(HashMap::new()),
locator, locator,
@ -116,7 +118,7 @@ impl FontConfiguration {
for _ in &handles { for _ in &handles {
rasterizers.push(RefCell::new(None)); 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 config = configuration();
let font_size = config.font_size * *self.font_scale.borrow(); let font_size = config.font_size * *self.font_scale.borrow();

View File

@ -1,8 +1,6 @@
use crate::font::locator::FontDataHandle; use crate::font::locator::FontDataHandle;
use crate::font::units::*; use crate::font::units::*;
use anyhow::{anyhow, bail, Error}; use anyhow::bail;
use serde::Deserialize;
use std::sync::Mutex;
pub mod freetype; pub mod freetype;
@ -28,61 +26,16 @@ pub trait FontRasterizer {
) -> anyhow::Result<RasterizedGlyph>; ) -> anyhow::Result<RasterizedGlyph>;
} }
#[derive(Debug, Deserialize, Clone, Copy)] pub use crate::config::FontRasterizerSelection;
pub enum FontRasterizerSelection {
FreeType,
FontKit,
}
lazy_static::lazy_static! { pub fn new_rasterizer(
static ref DEFAULT_RASTER: Mutex<FontRasterizerSelection> = Mutex::new(Default::default()); rasterizer: FontRasterizerSelection,
} handle: &FontDataHandle,
) -> anyhow::Result<Box<dyn FontRasterizer>> {
impl Default for FontRasterizerSelection { match rasterizer {
fn default() -> Self { FontRasterizerSelection::FreeType => Ok(Box::new(
FontRasterizerSelection::FreeType freetype::FreeTypeRasterizer::from_locator(handle)?,
} )),
} FontRasterizerSelection::FontKit => bail!("FontKit rasterizer not implemented yet"),
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<Box<dyn FontRasterizer>> {
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<Self, Self::Err> {
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()
)),
}
} }
} }

View File

@ -1,8 +1,5 @@
use crate::font::locator::FontDataHandle; use crate::font::locator::FontDataHandle;
use crate::font::units::PixelLength; use crate::font::units::PixelLength;
use anyhow::{anyhow, Error};
use serde::Deserialize;
use std::sync::Mutex;
pub mod allsorts; pub mod allsorts;
pub mod harfbuzz; pub mod harfbuzz;
@ -64,56 +61,14 @@ pub trait FontShaper {
fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result<FontMetrics>; fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result<FontMetrics>;
} }
#[derive(Debug, Deserialize, Clone, Copy)] pub use crate::config::FontShaperSelection;
pub enum FontShaperSelection {
Allsorts,
Harfbuzz,
}
lazy_static::lazy_static! { pub fn new_shaper(
static ref DEFAULT_SHAPER: Mutex<FontShaperSelection> = Mutex::new(Default::default()); shaper: FontShaperSelection,
} handles: &[FontDataHandle],
) -> anyhow::Result<Box<dyn FontShaper>> {
impl Default for FontShaperSelection { match shaper {
fn default() -> Self { FontShaperSelection::Harfbuzz => Ok(Box::new(harfbuzz::HarfbuzzShaper::new(handles)?)),
FontShaperSelection::Harfbuzz FontShaperSelection::Allsorts => Ok(Box::new(allsorts::AllsortsShaper::new(handles)?)),
}
}
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<Box<dyn FontShaper>> {
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<Self, Self::Err> {
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()
)),
}
} }
} }