Skip to content

Commit

Permalink
Fix to hi-dpi font issue on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanDunfield committed Dec 29, 2024
1 parent 5c5df47 commit aed9e3f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion python/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def __init__(self, terminal):
self.apply_settings()

def apply_settings(self):
self.terminal.set_font(self['font'].as_tuple())
self.terminal.set_font(self['font'])
changed = self.changed()
IP = self.terminal.IP
self.terminal.quiet = True
Expand Down
30 changes: 2 additions & 28 deletions python/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,7 @@
import plistlib
from .gui import *
from .app_menus import ListedWindow

class FontChoice:
def __init__(self, family, size, weight, slant):
self.family = family
self.size = size
self.weight = weight
self.slant = slant
self.rest = f'{self.weight} {self.slant}'

def as_tuple(self):
size = self.size
if sys.platform == 'darwin' and Tk_.TkVersion >= 9.0:
size = int(size/1.3)
return (self.family, size, self.rest)

def __repr__(self):
return 'FontChoice' + repr((self.family, self.size, self.weight, self.slant))

from .tkterminal import FontChoice, default_terminal_font

class Settings:
def __init__(self):
Expand Down Expand Up @@ -55,16 +38,7 @@ def get(self, key, default):
return self.setting_dict.get(key, default)

def default_font(self):
size = 13 if sys.platform == 'darwin' else 11
family = Font(font='TkFixedFont').actual()['family']
if sys.platform == 'win32':
# Default is Courier New which is ugly and appears blurry.
available = font_families()
for better in ['Consolas', 'Cascadia Mono SemiLight']:
if better in available:
family = better

return FontChoice(family, size, 'normal', 'roman')
return default_terminal_font()

def find_settings(self):
if sys.platform == 'darwin':
Expand Down
46 changes: 40 additions & 6 deletions python/tkterminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,40 @@
delims = re.compile(r'[\s\[\]\{\}\(\)\+\-\=\'`~!@#\$\^\&\*]+')


class FontChoice:
def __init__(self, family, size, weight, slant):
self.family = family
self.size = size
self.weight = weight
self.slant = slant
self.rest = f'{self.weight} {self.slant}'

def as_tuple(self):
size = self.size
if sys.platform == 'darwin' and Tk_.TkVersion >= 9.0:
size = int(size/1.3)
return (self.family, size, self.rest)

def __repr__(self):
return 'FontChoice' + repr((self.family, self.size, self.weight, self.slant))

def bold(self):
return FontChoice(self.family, self.size, 'bold', self.slant)


def default_terminal_font():
size = 13 if sys.platform == 'darwin' else 11
family = Font(font='TkFixedFont').actual()['family']
if sys.platform == 'win32':
# Default is Courier New which is ugly and appears blurry.
available = font_families()
for better in ['Consolas', 'Cascadia Mono SemiLight']:
if better in available:
family = better

return FontChoice(family, size, 'normal', 'roman')


class Tk(Tk_.Tk):
def __init__(self, error_handler=None):
Tk_.Tk.__init__(self, className='snappy')
Expand All @@ -39,6 +73,7 @@ def __init__(self, error_handler=None):
if error_handler:
self.report_callback_exception = error_handler


class TkTerminalBase:
"""
A Tkinter terminal window that runs an IPython shell. This class
Expand Down Expand Up @@ -69,7 +104,7 @@ def __init__(self, shell, name='TkTerm'):
highlightthickness=0,
relief=Tk_.FLAT
)
self.set_font(Font(text, text.cget('font')))
self.set_font(default_terminal_font())
self.scroller = scroller = Tk_.Scrollbar(frame, command=text.yview)
text.config(yscrollcommand=scroller.set)
scroller.pack(side=Tk_.RIGHT, fill=Tk_.Y, pady=10)
Expand Down Expand Up @@ -237,11 +272,10 @@ def report_callback_exception(self, exc, value, traceback):
sys.last_traceback = traceback
self.IP.showtraceback()

def set_font(self, fontdesc):
self.text.config(font=fontdesc)
normal_font = Font(self.text, self.text.cget('font'))
self.bold_font = bold_font = Font(self.text, self.text.cget('font'))
self.bold_font.config(weight='bold')
def set_font(self, font_choice):
self.normal_font = normal_font = Font(font=font_choice.as_tuple())
self.bold_font = bold_font = Font(font=font_choice.bold().as_tuple())
self.text.config(font=normal_font)
self.char_size = normal_font.measure('M')
text = self.text
text.tag_config('output', font=normal_font)
Expand Down

0 comments on commit aed9e3f

Please sign in to comment.