Skip to content

Instantly share code, notes, and snippets.

@mikoim
Last active January 12, 2018 11:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikoim/498dfcdd39439316e3eb9b8897914302 to your computer and use it in GitHub Desktop.
Save mikoim/498dfcdd39439316e3eb9b8897914302 to your computer and use it in GitHub Desktop.
Alacritty calculates the cell width from maximum advance via freetype2. But its value is too wide with some font. So this patch makes it use the average of rasterized glyph width instead of maximum advance.
diff --git a/src/display.rs b/src/display.rs
index 14c5a66..a5a8980 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -239,7 +239,7 @@ impl Display {
// font metrics should be computed before creating the window in the first
// place so that a resize is not needed.
let metrics = glyph_cache.font_metrics();
- let cell_width = (metrics.average_advance + font.offset().x as f64) as u32;
+ let cell_width = (glyph_cache.true_average_width() + font.offset().x as f64) as u32;
let cell_height = (metrics.line_height + font.offset().y as f64) as u32;
Ok((glyph_cache, cell_width as f32, cell_height as f32))
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs
index 237c841..87cae66 100644
--- a/src/renderer/mod.rs
+++ b/src/renderer/mod.rs
@@ -273,6 +273,10 @@ impl GlyphCache {
.expect("metrics load since font is loaded at glyph cache creation")
}
+ pub fn true_average_width(&self) -> f64 {
+ (self.cache.values().fold(0., |sum, g| sum + g.width) / self.cache.len() as f32 + 2.) as f64
+ }
+
pub fn get<'a, L>(&'a mut self, glyph_key: &GlyphKey, loader: &mut L) -> &'a Glyph
where L: LoadGlyph
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment