Skip to content

Commit

Permalink
Fix font spacing error
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduerr authored Feb 11, 2019
1 parent 8681e2e commit bfb67ed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Accept fonts which are smaller in width or height than a single pixel

### Fixed

- Incorrect font spacing after moving Alacritty between displays

## Version 0.2.8

### Added
Expand Down
58 changes: 19 additions & 39 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ pub enum Error {

/// Error in renderer
Render(renderer::Error),

/// Computed cell size is invalid
InvalidCellSize,
}

impl ::std::error::Error for Error {
Expand All @@ -54,7 +51,6 @@ impl ::std::error::Error for Error {
Error::Window(ref err) => Some(err),
Error::Font(ref err) => Some(err),
Error::Render(ref err) => Some(err),
Error::InvalidCellSize => None,
}
}

Expand All @@ -63,19 +59,16 @@ impl ::std::error::Error for Error {
Error::Window(ref err) => err.description(),
Error::Font(ref err) => err.description(),
Error::Render(ref err) => err.description(),
Error::InvalidCellSize => "cell size is invalid",
}
}
}

impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
use std::error::Error as StdError;
match *self {
Error::Window(ref err) => err.fmt(f),
Error::Font(ref err) => err.fmt(f),
Error::Render(ref err) => err.fmt(f),
Error::InvalidCellSize => write!(f, "{}", self.description()),
}
}
}
Expand Down Expand Up @@ -262,45 +255,32 @@ impl Display {
// Need font metrics to resize the window properly. This suggests to me the
// font metrics should be computed before creating the window in the first
// place so that a resize is not needed.
let (cell_width, cell_height) =
Self::compute_cell_size(&config, &glyph_cache.font_metrics())?;
let (cw, ch) = Self::compute_cell_size(config, &glyph_cache.font_metrics());

Ok((glyph_cache, cell_width, cell_height))
Ok((glyph_cache, cw, ch))
}

fn compute_cell_size(config: &Config, metrics: &font::Metrics) -> Result<(f32, f32), Error> {
let cell_width =
((metrics.average_advance + f64::from(config.font().offset().x)) as f32).floor();
let cell_height =
((metrics.line_height + f64::from(config.font().offset().y)) as f32).floor();

if cell_width < 1. || cell_height < 1. {
return Err(Error::InvalidCellSize)
}

Ok((cell_width, cell_height))
}

pub fn update_glyph_cache(
&mut self,
config: &Config,
size: font::Size
) -> Result<(), Error> {
let dpr = self.size_info.dpr;
pub fn update_glyph_cache(&mut self, config: &Config) {
let cache = &mut self.glyph_cache;

let (cell_width, cell_height) = Self::compute_cell_size(config, &cache.font_metrics())?;

self.font_size = size;
let dpr = self.size_info.dpr;
let size = self.font_size;

self.renderer.with_loader(|mut api| {
let _ = cache.update_font_size(config.font(), size, dpr, &mut api);
});

self.size_info.cell_width = cell_width;
self.size_info.cell_height = cell_height;
let (cw, ch) = Self::compute_cell_size(config, &cache.font_metrics());
self.size_info.cell_width = cw;
self.size_info.cell_height = ch;
}

Ok(())
fn compute_cell_size(config: &Config, metrics: &font::Metrics) -> (f32, f32) {
let offset_x = f64::from(config.font().offset().x);
let offset_y = f64::from(config.font().offset().y);
(
f32::max(1., ((metrics.average_advance + offset_x) as f32).floor()),
f32::max(1., ((metrics.line_height + offset_y) as f32).floor()),
)
}

#[inline]
Expand Down Expand Up @@ -346,13 +326,13 @@ impl Display {
));
}

self.font_size = terminal.font_size;
self.last_message = terminal.message_buffer_mut().message();
self.size_info.dpr = dpr;
}

if font_changed && self.update_glyph_cache(config, terminal.font_size).is_err() {
// Revert the terminal font size back to last known good state
terminal.font_size = self.font_size;
if font_changed {
self.update_glyph_cache(config);
}

if let Some(psize) = new_size.take() {
Expand Down

0 comments on commit bfb67ed

Please sign in to comment.