1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-25 14:05:41 -05:00

Prevent font_size_modifier from sinking too low (#994)

This replaces the `font_size_modifier` stored on the `Term` struct with a `font_size` field.

With this change it is not necessary anymore to calculate the new font size from a delta but the current font size is always stored directly on the `Term` struct.

As a result of this it is now possible to increase the font size by more than 127 steps at runtime. It also limits the minimum font size to 1, so issues with the `font_size_modifier` dropping far below font size 1 are resolved with this change.

This fixes #955.
This commit is contained in:
Christian Duerr 2018-01-05 03:22:58 +00:00 committed by GitHub
parent 7b4ba80bb1
commit 228400a6c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 32 deletions

View file

@ -166,7 +166,7 @@ impl Hash for GlyphKey {
} }
/// Font size stored as integer /// Font size stored as integer
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Size(i16); pub struct Size(i16);
impl Size { impl Size {

View file

@ -1467,13 +1467,9 @@ impl Font {
} }
/// Get a font clone with a size modification /// Get a font clone with a size modification
pub fn with_size_delta(self, delta: f32) -> Font { pub fn with_size(self, size: Size) -> Font {
let mut new_size = self.size.as_f32_pts() + delta;
if new_size < 1.0 {
new_size = 1.0;
}
Font { Font {
size : Size::new(new_size), size,
.. self .. self
} }
} }

View file

@ -96,7 +96,7 @@ pub struct Display {
rx: mpsc::Receiver<(u32, u32)>, rx: mpsc::Receiver<(u32, u32)>,
tx: mpsc::Sender<(u32, u32)>, tx: mpsc::Sender<(u32, u32)>,
meter: Meter, meter: Meter,
font_size_modifier: i8, font_size: font::Size,
size_info: SizeInfo, size_info: SizeInfo,
last_background_color: Rgb, last_background_color: Rgb,
} }
@ -150,7 +150,7 @@ impl Display {
let mut renderer = QuadRenderer::new(config, viewport_size)?; let mut renderer = QuadRenderer::new(config, viewport_size)?;
let (glyph_cache, cell_width, cell_height) = let (glyph_cache, cell_width, cell_height) =
Self::new_glyph_cache(&window, &mut renderer, config, 0)?; Self::new_glyph_cache(&window, &mut renderer, config)?;
let dimensions = options.dimensions() let dimensions = options.dimensions()
@ -205,17 +205,16 @@ impl Display {
tx: tx, tx: tx,
rx: rx, rx: rx,
meter: Meter::new(), meter: Meter::new(),
font_size_modifier: 0, font_size: font::Size::new(0.),
size_info: size_info, size_info: size_info,
last_background_color: background_color, last_background_color: background_color,
}) })
} }
fn new_glyph_cache(window : &Window, renderer : &mut QuadRenderer, fn new_glyph_cache(window : &Window, renderer : &mut QuadRenderer, config: &Config)
config: &Config, font_size_delta: i8)
-> Result<(GlyphCache, f32, f32), Error> -> Result<(GlyphCache, f32, f32), Error>
{ {
let font = config.font().clone().with_size_delta(font_size_delta as f32); let font = config.font().clone();
let dpr = window.hidpi_factor(); let dpr = window.hidpi_factor();
let rasterizer = font::Rasterizer::new(dpr, config.use_thin_strokes())?; let rasterizer = font::Rasterizer::new(dpr, config.use_thin_strokes())?;
@ -245,10 +244,11 @@ impl Display {
Ok((glyph_cache, cell_width as f32, cell_height as f32)) Ok((glyph_cache, cell_width as f32, cell_height as f32))
} }
pub fn update_glyph_cache(&mut self, config: &Config, font_size_delta: i8) { pub fn update_glyph_cache(&mut self, config: &Config) {
let cache = &mut self.glyph_cache; let cache = &mut self.glyph_cache;
let size = self.font_size;
self.renderer.with_loader(|mut api| { self.renderer.with_loader(|mut api| {
let _ = cache.update_font_size(config.font(), font_size_delta, &mut api); let _ = cache.update_font_size(config.font(), size, &mut api);
}); });
let metrics = cache.font_metrics(); let metrics = cache.font_metrics();
@ -282,11 +282,10 @@ impl Display {
new_size = Some(sz); new_size = Some(sz);
} }
if terminal.font_size_modifier != self.font_size_modifier { // Font size modification detected
// Font size modification detected if terminal.font_size != self.font_size {
self.font_size = terminal.font_size;
self.font_size_modifier = terminal.font_size_modifier; self.update_glyph_cache(config);
self.update_glyph_cache(config, terminal.font_size_modifier);
if new_size == None { if new_size == None {
// Force a resize to refresh things // Force a resize to refresh things

View file

@ -295,7 +295,7 @@ impl GlyphCache {
pub fn update_font_size<L: LoadGlyph>( pub fn update_font_size<L: LoadGlyph>(
&mut self, &mut self,
font: &config::Font, font: &config::Font,
delta: i8, size: font::Size,
loader: &mut L loader: &mut L
) -> Result<(), font::Error> { ) -> Result<(), font::Error> {
// Clear currently cached data in both GL and the registry // Clear currently cached data in both GL and the registry
@ -303,8 +303,8 @@ impl GlyphCache {
self.cache = HashMap::default(); self.cache = HashMap::default();
// Recompute font keys // Recompute font keys
let font = font.to_owned().with_size_delta(delta as _); let font = font.to_owned().with_size(size);
println!("{:?}", font.size); info!("Font size changed: {:?}", font.size);
let (regular, bold, italic) = Self::compute_font_keys(&font, &mut self.rasterizer)?; let (regular, bold, italic) = Self::compute_font_keys(&font, &mut self.rasterizer)?;
self.rasterizer.get_glyph(&GlyphKey { font_key: regular, c: 'm', size: font.size() })?; self.rasterizer.get_glyph(&GlyphKey { font_key: regular, c: 'm', size: font.size() })?;
let metrics = self.rasterizer.metrics(regular)?; let metrics = self.rasterizer.metrics(regular)?;

View file

@ -22,7 +22,7 @@ use std::time::{Duration, Instant};
use arraydeque::ArrayDeque; use arraydeque::ArrayDeque;
use unicode_width::UnicodeWidthChar; use unicode_width::UnicodeWidthChar;
use font; use font::{self, Size};
use ansi::{self, Color, NamedColor, Attr, Handler, CharsetIndex, StandardCharset, CursorStyle}; use ansi::{self, Color, NamedColor, Attr, Handler, CharsetIndex, StandardCharset, CursorStyle};
use grid::{BidirectionalIterator, Grid, ClearRegion, ToRange, Indexed}; use grid::{BidirectionalIterator, Grid, ClearRegion, ToRange, Indexed};
use index::{self, Point, Column, Line, Linear, IndexRange, Contains, RangeInclusive}; use index::{self, Point, Column, Line, Linear, IndexRange, Contains, RangeInclusive};
@ -685,8 +685,9 @@ pub struct Term {
/// Scroll region /// Scroll region
scroll_region: Range<Line>, scroll_region: Range<Line>,
/// Font size modifier /// Font size
pub font_size_modifier: i8, pub font_size: Size,
original_font_size: Size,
/// Size /// Size
size_info: SizeInfo, size_info: SizeInfo,
@ -814,7 +815,8 @@ impl Term {
grid: grid, grid: grid,
alt_grid: alt, alt_grid: alt,
alt: false, alt: false,
font_size_modifier: 0, font_size: config.font().size(),
original_font_size: config.font().size(),
active_charset: Default::default(), active_charset: Default::default(),
cursor: Default::default(), cursor: Default::default(),
cursor_save: Default::default(), cursor_save: Default::default(),
@ -834,14 +836,14 @@ impl Term {
} }
pub fn change_font_size(&mut self, delta: i8) { pub fn change_font_size(&mut self, delta: i8) {
if let Some(sum) = self.font_size_modifier.checked_add(delta) { // Saturating addition with minimum font size 1
self.font_size_modifier = sum; let new_size = self.font_size + Size::new(delta as f32);
self.dirty = true; self.font_size = max(new_size, Size::new(1.));
} self.dirty = true;
} }
pub fn reset_font_size(&mut self) { pub fn reset_font_size(&mut self) {
self.font_size_modifier = 0; self.font_size = self.original_font_size;
self.dirty = true; self.dirty = true;
} }