From 7fc50f669056dc9a3a601b3f0248926384abb570 Mon Sep 17 00:00:00 2001 From: Aaron Williamson Date: Sat, 28 Jan 2017 19:30:47 -0700 Subject: [PATCH] Improve freetype metric usage The font metrics function was using freetype metrics in an ineffective way, improve the use of those metrics and remove the now unnecessary separate default values for font offset in linux. --- alacritty.yml | 4 ++-- font/src/ft/mod.rs | 22 +++++++++++++--------- src/config.rs | 18 ++++++++---------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/alacritty.yml b/alacritty.yml index b6ad6a3..c064dbc 100644 --- a/alacritty.yml +++ b/alacritty.yml @@ -57,8 +57,8 @@ font: # Offset is the extra space around each character. offset.y can be thought of # as modifying the linespacing, and offset.x as modifying the letter spacing. offset: - x: 2.0 - y: -7.0 + x: 0.0 + y: 0.0 # OS X only: use thin stroke font rendering. Thin strokes are suitable # for retina displays, but for non-retina you probably want this set to diff --git a/font/src/ft/mod.rs b/font/src/ft/mod.rs index 82f0677..24ef5c7 100644 --- a/font/src/ft/mod.rs +++ b/font/src/ft/mod.rs @@ -59,18 +59,15 @@ impl ::Rasterize for FreeTypeRasterizer { .get(&key) .ok_or(Error::FontNotLoaded)?; - let scale_size = self.dpr as f64 * size.as_f32_pts() as f64; + let size_metrics = face.size_metrics() + .ok_or(Error::MissingSizeMetrics)?; - let em_size = face.em_size() as f64; - let w = face.max_advance_width() as f64; - let h = (face.ascender() - face.descender() + face.height()) as f64; - - let w_scale = w * scale_size / em_size; - let h_scale = h * scale_size / em_size; + let width = (size_metrics.max_advance / 64) as f64; + let height = (size_metrics.height / 64) as f64; Ok(Metrics { - average_advance: w_scale, - line_height: h_scale, + average_advance: width, + line_height: height, }) } @@ -278,6 +275,9 @@ pub enum Error { /// Couldn't find font matching description MissingFont(FontDesc), + /// Tried to get size metrics from a Face that didn't have a size + MissingSizeMetrics, + /// Requested an operation with a FontKey that isn't known to the rasterizer FontNotLoaded, } @@ -295,6 +295,7 @@ impl ::std::error::Error for Error { Error::FreeType(ref err) => err.description(), Error::MissingFont(ref _desc) => "couldn't find the requested font", Error::FontNotLoaded => "tried to operate on font that hasn't been loaded", + Error::MissingSizeMetrics => "tried to get size metrics from a face without a size", } } } @@ -311,6 +312,9 @@ impl ::std::fmt::Display for Error { }, Error::FontNotLoaded => { f.write_str("Tried to use a font that hasn't been loaded") + }, + Error::MissingSizeMetrics => { + f.write_str("Tried to get size metrics from a face without a size") } } } diff --git a/src/config.rs b/src/config.rs index ba1ae5f..61bdb25 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1189,6 +1189,12 @@ impl FontOffset { } } +impl Default for FontOffset { + fn default() -> FontOffset { + FontOffset { x: 0.0, y: 0.0 } + } +} + trait DeserializeFromF32 : Sized { fn deserialize_from_f32(D) -> ::std::result::Result where D: serde::de::Deserializer; @@ -1301,10 +1307,7 @@ impl Default for Font { italic: FontDescription::new_with_family("Menlo"), size: Size::new(11.0), use_thin_strokes: true, - offset: FontOffset { - x: 0.0, - y: 0.0 - } + offset: Default::default() } } } @@ -1318,12 +1321,7 @@ impl Default for Font { italic: FontDescription::new_with_family("monospace"), size: Size::new(11.0), use_thin_strokes: false, - offset: FontOffset { - // TODO should improve freetype metrics... shouldn't need such - // drastic offsets for the default! - x: 2.0, - y: -7.0 - } + offset: Default::default() } } }