mirror of
https://github.com/wez/wezterm.git
synced 2024-11-27 12:23:46 +03:00
move font configuration enums to config module
This commit is contained in:
parent
35f6a3dd9d
commit
01445bf5b8
@ -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<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()
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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<Vec<FontDataHandle>>;
|
||||
}
|
||||
|
||||
#[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"]
|
||||
}
|
||||
|
||||
pub fn new_locator(self) -> Box<dyn FontLocator> {
|
||||
match self {
|
||||
Self::FontConfig => {
|
||||
pub fn new_locator(locator: FontLocatorSelection) -> Box<dyn FontLocator> {
|
||||
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");
|
||||
}
|
||||
Self::FontLoader => {
|
||||
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");
|
||||
}
|
||||
Self::FontKit => {
|
||||
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");
|
||||
}
|
||||
Self::ConfigDirsOnly => Box::new(NopSystemSource {}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
)),
|
||||
}
|
||||
FontLocatorSelection::ConfigDirsOnly => Box::new(NopSystemSource {}),
|
||||
}
|
||||
}
|
||||
|
||||
struct NopSystemSource {}
|
||||
|
||||
pub use crate::config::FontLocatorSelection;
|
||||
|
||||
impl FontLocator for NopSystemSource {
|
||||
fn load_fonts(
|
||||
&self,
|
||||
|
@ -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();
|
||||
|
@ -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<RasterizedGlyph>;
|
||||
}
|
||||
|
||||
#[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"]
|
||||
}
|
||||
pub use crate::config::FontRasterizerSelection;
|
||||
|
||||
pub fn new_rasterizer(
|
||||
self,
|
||||
rasterizer: FontRasterizerSelection,
|
||||
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()
|
||||
match rasterizer {
|
||||
FontRasterizerSelection::FreeType => Ok(Box::new(
|
||||
freetype::FreeTypeRasterizer::from_locator(handle)?,
|
||||
)),
|
||||
}
|
||||
FontRasterizerSelection::FontKit => bail!("FontKit rasterizer not implemented yet"),
|
||||
}
|
||||
}
|
||||
|
@ -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<FontMetrics>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Copy)]
|
||||
pub enum FontShaperSelection {
|
||||
Allsorts,
|
||||
Harfbuzz,
|
||||
}
|
||||
pub use crate::config::FontShaperSelection;
|
||||
|
||||
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"]
|
||||
}
|
||||
|
||||
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()
|
||||
)),
|
||||
}
|
||||
pub fn new_shaper(
|
||||
shaper: FontShaperSelection,
|
||||
handles: &[FontDataHandle],
|
||||
) -> anyhow::Result<Box<dyn FontShaper>> {
|
||||
match shaper {
|
||||
FontShaperSelection::Harfbuzz => Ok(Box::new(harfbuzz::HarfbuzzShaper::new(handles)?)),
|
||||
FontShaperSelection::Allsorts => Ok(Box::new(allsorts::AllsortsShaper::new(handles)?)),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user