2019-05-10 11:36:16 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-12-21 02:44:38 +00:00
|
|
|
use crossfont::Size as FontSize;
|
|
|
|
use serde::de::{self, Visitor};
|
2019-05-10 11:36:16 +00:00
|
|
|
use serde::{Deserialize, Deserializer};
|
|
|
|
|
2022-08-31 22:48:38 +00:00
|
|
|
use alacritty_config_derive::{ConfigDeserialize, SerdeReplace};
|
2020-07-11 17:03:09 +00:00
|
|
|
|
|
|
|
use crate::config::ui_config::Delta;
|
2019-05-10 11:36:16 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Font config.
|
2019-05-10 11:36:16 +00:00
|
|
|
///
|
|
|
|
/// Defaults are provided at the level of this struct per platform, but not per
|
|
|
|
/// field in this struct. It might be nice in the future to have defaults for
|
|
|
|
/// each value independently. Alternatively, maybe erroring when the user
|
|
|
|
/// doesn't provide complete config is Ok.
|
2022-01-29 19:50:44 +00:00
|
|
|
#[derive(ConfigDeserialize, Debug, Clone, PartialEq, Eq)]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub struct Font {
|
2020-12-21 02:44:38 +00:00
|
|
|
/// Extra spacing per character.
|
|
|
|
pub offset: Delta<i8>,
|
|
|
|
|
|
|
|
/// Glyph offset within character cell.
|
|
|
|
pub glyph_offset: Delta<i8>,
|
|
|
|
|
2022-07-15 21:56:26 +00:00
|
|
|
#[config(removed = "set the AppleFontSmoothing user default instead")]
|
2020-12-21 02:44:38 +00:00
|
|
|
pub use_thin_strokes: bool,
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Normal font face.
|
2019-05-10 11:36:16 +00:00
|
|
|
normal: FontDescription,
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Bold font face.
|
2019-08-25 10:46:52 +00:00
|
|
|
bold: SecondaryFontDescription,
|
2019-05-10 11:36:16 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Italic font face.
|
2019-08-25 10:46:52 +00:00
|
|
|
italic: SecondaryFontDescription,
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Bold italic font face.
|
2019-08-25 10:46:52 +00:00
|
|
|
bold_italic: SecondaryFontDescription,
|
2019-05-10 11:36:16 +00:00
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Font size in points.
|
2020-12-21 02:44:38 +00:00
|
|
|
size: Size,
|
2022-01-29 19:50:44 +00:00
|
|
|
|
|
|
|
/// Whether to use the built-in font for box drawing characters.
|
|
|
|
pub builtin_box_drawing: bool,
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Font {
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Get a font clone with a size modification.
|
2020-12-21 02:44:38 +00:00
|
|
|
pub fn with_size(self, size: FontSize) -> Font {
|
|
|
|
Font { size: Size(size), ..self }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn size(&self) -> FontSize {
|
|
|
|
self.size.0
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Get normal font description.
|
2019-05-10 11:36:16 +00:00
|
|
|
pub fn normal(&self) -> &FontDescription {
|
|
|
|
&self.normal
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Get bold font description.
|
2019-08-25 10:46:52 +00:00
|
|
|
pub fn bold(&self) -> FontDescription {
|
|
|
|
self.bold.desc(&self.normal)
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Get italic font description.
|
2019-05-10 11:36:16 +00:00
|
|
|
pub fn italic(&self) -> FontDescription {
|
|
|
|
self.italic.desc(&self.normal)
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Get bold italic font description.
|
2019-08-25 10:46:52 +00:00
|
|
|
pub fn bold_italic(&self) -> FontDescription {
|
|
|
|
self.bold_italic.desc(&self.normal)
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:50:44 +00:00
|
|
|
impl Default for Font {
|
|
|
|
fn default() -> Font {
|
|
|
|
Self {
|
|
|
|
builtin_box_drawing: true,
|
|
|
|
glyph_offset: Default::default(),
|
2022-07-15 21:56:26 +00:00
|
|
|
use_thin_strokes: Default::default(),
|
2022-01-29 19:50:44 +00:00
|
|
|
bold_italic: Default::default(),
|
|
|
|
italic: Default::default(),
|
|
|
|
offset: Default::default(),
|
|
|
|
normal: Default::default(),
|
|
|
|
bold: Default::default(),
|
|
|
|
size: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Description of the normal font.
|
2020-12-21 02:44:38 +00:00
|
|
|
#[derive(ConfigDeserialize, Debug, Clone, PartialEq, Eq)]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub struct FontDescription {
|
|
|
|
pub family: String,
|
|
|
|
pub style: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FontDescription {
|
|
|
|
fn default() -> FontDescription {
|
|
|
|
FontDescription {
|
|
|
|
#[cfg(not(any(target_os = "macos", windows)))]
|
|
|
|
family: "monospace".into(),
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
family: "Menlo".into(),
|
|
|
|
#[cfg(windows)]
|
|
|
|
family: "Consolas".into(),
|
|
|
|
style: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:50:23 +00:00
|
|
|
/// Description of the italic and bold font.
|
2020-12-21 02:44:38 +00:00
|
|
|
#[derive(ConfigDeserialize, Debug, Default, Clone, PartialEq, Eq)]
|
2019-05-10 11:36:16 +00:00
|
|
|
pub struct SecondaryFontDescription {
|
|
|
|
family: Option<String>,
|
|
|
|
style: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SecondaryFontDescription {
|
|
|
|
pub fn desc(&self, fallback: &FontDescription) -> FontDescription {
|
|
|
|
FontDescription {
|
|
|
|
family: self.family.clone().unwrap_or_else(|| fallback.family.clone()),
|
|
|
|
style: self.style.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:48:38 +00:00
|
|
|
#[derive(SerdeReplace, Debug, Clone, PartialEq, Eq)]
|
2020-12-21 02:44:38 +00:00
|
|
|
struct Size(FontSize);
|
|
|
|
|
|
|
|
impl Default for Size {
|
|
|
|
fn default() -> Self {
|
2023-12-14 03:15:40 +00:00
|
|
|
Self(FontSize::new(11.25))
|
2020-12-21 02:44:38 +00:00
|
|
|
}
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 02:44:38 +00:00
|
|
|
impl<'de> Deserialize<'de> for Size {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2019-05-10 11:36:16 +00:00
|
|
|
where
|
2020-12-21 02:44:38 +00:00
|
|
|
D: Deserializer<'de>,
|
2019-05-10 11:36:16 +00:00
|
|
|
{
|
2020-12-21 02:44:38 +00:00
|
|
|
struct NumVisitor;
|
|
|
|
impl<'v> Visitor<'v> for NumVisitor {
|
|
|
|
type Value = Size;
|
2019-05-10 11:36:16 +00:00
|
|
|
|
|
|
|
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-06-17 19:51:06 +00:00
|
|
|
f.write_str("f64 or i64")
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 02:44:38 +00:00
|
|
|
fn visit_f64<E: de::Error>(self, value: f64) -> Result<Self::Value, E> {
|
|
|
|
Ok(Size(FontSize::new(value as f32)))
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 19:51:06 +00:00
|
|
|
fn visit_i64<E: de::Error>(self, value: i64) -> Result<Self::Value, E> {
|
2020-12-21 02:44:38 +00:00
|
|
|
Ok(Size(FontSize::new(value as f32)))
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 02:44:38 +00:00
|
|
|
deserializer.deserialize_any(NumVisitor)
|
2019-05-10 11:36:16 +00:00
|
|
|
}
|
|
|
|
}
|