introduce a FontAttributes type for the config

This has a little more structure than the linux specfic
fontconfig_pattern and can be used in a future diff to
derive a fontconfig_pattern.

This is a breaking change for the config file format.
This commit is contained in:
Wez Furlong committed 2019-02-18 09:04:17 -08:00
1 parent 9c9261f332
commit 018418a0c2
3 files changed
+84 -13

No files matched your search

+57 -2
View File
@@ -80,14 +80,46 @@ impl Default for Config {
}
}
#[cfg(target_os = "macos")]
const FONT_FAMILY: &str = "Menlo";
#[cfg(not(target_os = "macos"))]
const FONT_FAMILY: &str = "monospace";
#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct FontAttributes {
/// The font family name
pub family: String,
/// Whether the font should be a bold variant
pub bold: Option<bool>,
/// Whether the font should be an italic variant
pub italic: Option<bool>,
}
impl Default for FontAttributes {
fn default() -> Self {
Self {
family: FONT_FAMILY.into(),
bold: None,
italic: None,
}
}
}
fn default_fontconfig_pattern() -> String {
FONT_FAMILY.into()
}
/// Represents textual styling.
#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct TextStyle {
/// A font config pattern to parse to locate the font.
/// Note that the dpi and current font_size for the terminal
/// will be set on the parsed result.
#[serde(default = "default_fontconfig_pattern")]
pub fontconfig_pattern: String,
pub font: Vec<FontAttributes>,
/// If set, when rendering text that is set to the default
/// foreground color, use this color instead. This is most
/// useful in a `[[font_rules]]` section to implement changing
@@ -98,8 +130,9 @@ pub struct TextStyle {
impl Default for TextStyle {
fn default() -> Self {
Self {
fontconfig_pattern: "monospace".into(),
fontconfig_pattern: FONT_FAMILY.into(),
foreground: None,
font: vec![FontAttributes::default()],
}
}
}
@@ -114,6 +147,15 @@ impl TextStyle {
Self {
fontconfig_pattern: format!("{}:weight=bold", self.fontconfig_pattern),
foreground: self.foreground,
font: self
.font
.iter()
.map(|attr| {
let mut attr = attr.clone();
attr.bold = Some(true);
attr
})
.collect(),
}
}
@@ -126,6 +168,15 @@ impl TextStyle {
Self {
fontconfig_pattern: format!("{}:style=Italic", self.fontconfig_pattern),
foreground: self.foreground,
font: self
.font
.iter()
.map(|attr| {
let mut attr = attr.clone();
attr.italic = Some(true);
attr
})
.collect(),
}
}
}
@@ -204,7 +255,11 @@ impl Config {
return Ok(cfg.compute_extra_defaults());
}
Ok(Self::default().compute_extra_defaults())
Ok(Self::default_config())
}
pub fn default_config() -> Self {
Self::default().compute_extra_defaults()
}
/// In some cases we need to compute expanded values based
+17 -10
View File
@@ -28,17 +28,24 @@ impl RustTypeFonts {
impl FontSystem for RustTypeFonts {
fn load_font(&self, config: &Config, style: &TextStyle) -> Result<Box<NamedFont>, Error> {
#[cfg(not(target_os = "macos"))]
let family = "monospace";
#[cfg(target_os = "macos")]
let family = "Menlo";
let font_props = system_fonts::FontPropertyBuilder::new()
//.family(&style.fontconfig_pattern)
.family(family)
.monospace()
.build();
let font = &style.font[0];
let mut font_props = system_fonts::FontPropertyBuilder::new()
.family(&font.family)
.monospace();
font_props = if *font.bold.as_ref().unwrap_or(&false) {
font_props.bold()
} else {
font_props
};
font_props = if *font.italic.as_ref().unwrap_or(&false) {
font_props.italic()
} else {
font_props
};
let font_props = font_props.build();
let (data, idx) = system_fonts::get(&font_props)
.ok_or_else(|| format_err!("no font matching {:?}", family))?;
.ok_or_else(|| format_err!("no font matching {:?} {:?}", font_props, font))?;
eprintln!("want idx {} in bytes of len {}", idx, data.len());
let collection = FontCollection::from_bytes(data)?;
let font = collection.font_at(idx as usize)?;
+10 -1
View File
@@ -122,13 +122,22 @@ fn main() -> Result<(), Error> {
"Wez's Terminal Emulator\n\
http://github.com/wez/wezterm",
)
.arg(
Arg::with_name("SKIP_CONFIG")
.short("n")
.help("Skip loading ~/.wezterm.toml"),
)
.arg(Arg::with_name("PROG").multiple(true).help(
"Instead of executing your shell, run PROG. \
For example: `wezterm -- bash -l` will spawn bash \
as if it were a login shell.",
))
.get_matches();
let config = Rc::new(config::Config::load()?);
let config = Rc::new(if args.is_present("SKIP_CONFIG") {
config::Config::default_config()
} else {
config::Config::load()?
});
println!("Using configuration: {:#?}", config);
let fontconfig = Rc::new(FontConfiguration::new(Rc::clone(&config)));