diff options
author | Joe Wilm <joe@jwilm.com> | 2016-08-12 13:38:41 -0500 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2016-08-12 19:30:14 -0500 |
commit | b70394170c0fa9cd4e4be8150121a698a426a069 (patch) | |
tree | 41d84240a4e58df201fbd7f4f984c3814c7cb055 /src/config.rs | |
parent | 874719580ce3e12f09d01a9a53c5e64205ef5b5d (diff) | |
download | alacritty-b70394170c0fa9cd4e4be8150121a698a426a069.tar.gz alacritty-b70394170c0fa9cd4e4be8150121a698a426a069.zip |
Support bold/italic font rendering on macOS
This patch adds support for rendering italic fonts and bold fonts.
The `font` crate has a couple of new paradigms to support this: font
keys and glyph keys. `FontKey` is a lightweight (4 byte) identifier for
a font loaded out of the rasterizer. This replaces `FontDesc` for
rasterizing glyphs from a loaded font. `FontDesc` had the problem that
it contained two strings, and the glyph cache needs to store a copy of
the font key for every loaded glyph. `GlyphKey` is now passed to the
glyph rasterization method instead of a simple `char`. `GlyphKey`
contains information including font, size, and the character.
The rasterizer APIs do not define what happens when loading the same
font from a `FontDesc` more than once. It is assumed that the
application will track the resulting `FontKey` instead of asking the
font to be loaded multiple times.
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 80 |
1 files changed, 75 insertions, 5 deletions
diff --git a/src/config.rs b/src/config.rs index 426a3381..30bbf1b5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,9 @@ use std::fs; use std::io::{self, Read}; use std::path::{Path, PathBuf}; +use font::Size; use serde_yaml; +use serde; /// Top-level config type #[derive(Debug, Deserialize, Default)] @@ -220,6 +222,47 @@ impl FontOffset { } } +trait DeserializeFromF32 : Sized { + fn deserialize_from_f32<D>(&mut D) -> ::std::result::Result<Self, D::Error> + where D: serde::de::Deserializer; +} + +impl DeserializeFromF32 for Size { + fn deserialize_from_f32<D>(deserializer: &mut D) -> ::std::result::Result<Self, D::Error> + where D: serde::de::Deserializer + { + use std::marker::PhantomData; + + struct FloatVisitor<__D> { + _marker: PhantomData<__D>, + } + + impl<__D> ::serde::de::Visitor for FloatVisitor<__D> + where __D: ::serde::de::Deserializer + { + type Value = f32; + + fn visit_f32<E>(&mut self, value: f32) -> ::std::result::Result<Self::Value, E> + where E: ::serde::de::Error + { + Ok(value) + } + + fn visit_str<E>(&mut self, value: &str) -> ::std::result::Result<Self::Value, E> + where E: ::serde::de::Error + { + // FIXME serde-yaml visits a str for real numbers. + // https://github.com/dtolnay/serde-yaml/issues/24 + Ok(value.parse::<f32>().expect("size must be float")) + } + } + + deserializer + .deserialize_f32(FloatVisitor::<D>{ _marker: PhantomData }) + .map(|v| Size::new(v)) + } +} + /// Font config /// /// Defaults are provided at the level of this struct per platform, but not per @@ -234,8 +277,15 @@ pub struct Font { /// Font style style: String, - /// Font size in points - size: f32, + /// Bold font style + bold_style: Option<String>, + + /// Italic font style + italic_style: Option<String>, + + // Font size in points + #[serde(deserialize_with="DeserializeFromF32::deserialize_from_f32")] + size: Size, /// Extra spacing per character offset: FontOffset, @@ -254,9 +304,25 @@ impl Font { &self.style[..] } + /// Get italic font style; assumes same family + #[inline] + pub fn italic_style(&self) -> Option<&str> { + self.italic_style + .as_ref() + .map(|s| s.as_str()) + } + + /// Get bold font style; assumes same family + #[inline] + pub fn bold_style(&self) -> Option<&str> { + self.bold_style + .as_ref() + .map(|s| s.as_str()) + } + /// Get the font size in points #[inline] - pub fn size(&self) -> f32 { + pub fn size(&self) -> Size { self.size } @@ -273,7 +339,9 @@ impl Default for Font { Font { family: String::from("Menlo"), style: String::from("Regular"), - size: 11.0, + size: Size::new(11.0), + bold_style: Some(String::from("Bold")), + italic_style: Some(String::from("Italic")), offset: FontOffset { x: 0.0, y: 0.0 @@ -288,7 +356,9 @@ impl Default for Font { Font { family: String::from("DejaVu Sans Mono"), style: String::from("Book"), - size: 11.0, + size: Size::new(11.0), + bold_style: Some(String::from("Bold")), + italic_style: Some(String::from("Italic")), offset: FontOffset { // TODO should improve freetype metrics... shouldn't need such // drastic offsets for the default! |