Skip to content

Instantly share code, notes, and snippets.

@avivace
Created January 12, 2018 12:32
Show Gist options
  • Save avivace/9d3fddcb43e853d53cadbca4ed11d27e to your computer and use it in GitHub Desktop.
Save avivace/9d3fddcb43e853d53cadbca4ed11d27e to your computer and use it in GitHub Desktop.
From 88a5973aeac2d53ba855cf2d3a9ce75ca65b4dba Mon Sep 17 00:00:00 2001
From: Antonio Vivace <[email protected]>
Date: Fri, 12 Jan 2018 13:29:13 +0100
Subject: [PATCH] Fix wrong font width
---
src/display.rs | 2 +-
src/renderer/mod.rs | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/display.rs b/src/display.rs
index 9227037..f29cfbf 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -238,7 +238,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 + f64::from(font.offset().x)) as u32;
+ let cell_width = (glyph_cache.true_average_width() + font.offset().x as f64) as u32;
let cell_height = (metrics.line_height + f64::from(font.offset().y)) 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 c226a88..f71e34a 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
{
--
2.15.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment