From 2cc20e4b272147124ddfb4ba502f19388f75c828 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 31 Mar 2017 10:00:56 +0530 Subject: [PATCH] Allow changing font size in a running terminal using keyboard shortcuts. Fixes #57 --- kitty/boss.py | 24 ++++++++++++++++++++++++ kitty/config.py | 4 +++- kitty/fonts/core_text.py | 7 ++++--- kitty/fonts/freetype.py | 4 ++-- kitty/kitty.conf | 8 ++++++++ 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index bfb5a4aea..a15185b0e 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -14,6 +14,7 @@ from time import monotonic from queue import Queue, Empty from gettext import gettext as _ +from .config import MINIMUM_FONT_SIZE from .constants import ( viewport_size, set_boss, wakeup, cell_size, MODIFIER_KEYS, main_thread, mouse_button_pressed, mouse_cursor_pos @@ -86,6 +87,7 @@ class Boss(Thread): self.pending_ui_thread_calls = Queue() self.write_dispatch_map = {} set_boss(self) + self.current_font_size = opts.font_size cell_size.width, cell_size.height = set_font_family(opts) self.opts, self.args = opts, args self.glfw_window = glfw_window @@ -228,6 +230,28 @@ class Boss(Thread): self.pending_resize = False glfw_post_empty_event() + def increase_font_size(self): + self.change_font_size(min(self.opts.font_size * 5, self.current_font_size + self.opts.font_size_delta)) + + def decrease_font_size(self): + self.change_font_size(max(MINIMUM_FONT_SIZE, self.current_font_size - self.opts.font_size_delta)) + + def restore_font_size(self): + self.change_font_size(self.opts.font_size) + + def change_font_size(self, new_size): + if new_size == self.current_font_size: + return + self.current_font_size = new_size + cell_size.width, cell_size.height = set_font_family( + self.opts, override_font_size=self.current_font_size) + self.sprites.do_layout(cell_size.width, cell_size.height) + self.queue_action(self.resize_windows_after_font_size_change) + + def resize_windows_after_font_size_change(self): + self.tab_manager.resize() + glfw_post_empty_event() + def tabbar_visibility_changed(self): self.tab_manager.resize(only_tabs=True) glfw_post_empty_event() diff --git a/kitty/config.py b/kitty/config.py index 33f6a0558..20c46b550 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -17,10 +17,11 @@ from .layout import all_layouts from .utils import safe_print, to_color key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$') +MINIMUM_FONT_SIZE = 6 def to_font_size(x): - return max(6, float(x)) + return max(MINIMUM_FONT_SIZE, float(x)) cshapes = { @@ -153,6 +154,7 @@ type_map = { 'scrollback_pager': shlex.split, 'scrollback_in_new_tab': to_bool, 'font_size': to_font_size, + 'font_size_delta': float, 'cursor_shape': to_cursor_shape, 'cursor_opacity': to_opacity, 'open_url_modifiers': to_open_url_modifiers, diff --git a/kitty/fonts/core_text.py b/kitty/fonts/core_text.py index 90c0d257a..8932a72a3 100644 --- a/kitty/fonts/core_text.py +++ b/kitty/fonts/core_text.py @@ -19,7 +19,7 @@ def install_symbol_map(val, font_size, dpi): symbol_map[ch] = family_map[family] -def set_font_family(opts, ignore_dpi_failure=False): +def set_font_family(opts, override_font_size=None, ignore_dpi_failure=False): global cell_width, cell_height, baseline, CellTexture, WideCellTexture, underline_thickness, underline_position try: dpi = get_logical_dpi() @@ -37,11 +37,12 @@ def set_font_family(opts, ignore_dpi_failure=False): if ans == 'auto' and (bold or italic): ans = get_family(False, False) return ans + font_size = override_font_size or opts.font_size for bold in (False, True): for italic in (False, True): - main_font[(bold, italic)] = Face(get_family(bold, italic), bold, italic, True, opts.font_size, dpi) - install_symbol_map(opts.symbol_map, opts.font_size, dpi) + main_font[(bold, italic)] = Face(get_family(bold, italic), bold, italic, True, font_size, dpi) + install_symbol_map(opts.symbol_map, font_size, dpi) mf = main_font[(False, False)] cell_width, cell_height = mf.cell_size() CellTexture = ctypes.c_ubyte * (cell_width * cell_height) diff --git a/kitty/fonts/freetype.py b/kitty/fonts/freetype.py index a1cae4d6e..848cb90fd 100644 --- a/kitty/fonts/freetype.py +++ b/kitty/fonts/freetype.py @@ -71,10 +71,10 @@ def font_units_to_pixels(x, units_per_em, size_in_pts, dpi): return ceil_int(x * ((size_in_pts * dpi) / (72 * units_per_em))) -def set_font_family(opts): +def set_font_family(opts, override_font_size=None): global current_font_family, current_font_family_name, cff_size, cell_width, cell_height, CharTexture, baseline global underline_position, underline_thickness - size_in_pts = opts.font_size + size_in_pts = override_font_size or opts.font_size current_font_family = get_font_files(opts) current_font_family_name = opts.font_family dpi = get_logical_dpi() diff --git a/kitty/kitty.conf b/kitty/kitty.conf index 484a49abf..d168356f7 100644 --- a/kitty/kitty.conf +++ b/kitty/kitty.conf @@ -15,6 +15,10 @@ bold_italic_font auto # Font size (in pts) font_size 11.0 +# The amount the font size is changed by (in pts) when increasing/decreasing +# the font size in a running terminal. +font_size_delta 2 + # The foreground color foreground #dddddd @@ -215,6 +219,10 @@ map ctrl+shift+l next_layout map ctrl+shift+. move_tab_forward map ctrl+shift+, move_tab_backward +# Miscellaneous +map ctrl+shift+equal increase_font_size +map ctrl+shift+minus decrease_font_size +map ctrl+shift+backspace restore_font_size # Symbol mapping (special font for specified unicode code points). Map the # specified unicode codepoints to a particular font. Useful if you need special