GUI - Fonts

- Config - more sensible font selection list
  - Linux - Attempt to be more consistent on default font
This commit is contained in:
torzdf 2020-08-22 11:02:15 +00:00
parent 877b90b60a
commit cbcd301150
2 changed files with 45 additions and 8 deletions

View File

@ -4,7 +4,8 @@
import logging
import sys
import os
from tkinter import font
from tkinter import font as tk_font
from matplotlib import font_manager
from lib.config import FaceswapConfig
@ -91,8 +92,22 @@ def get_commands():
def get_clean_fonts():
""" Return the font list with any @prefixed or non-Unicode characters stripped
and default prefixed """
cleaned_fonts = sorted([fnt for fnt in font.families()
if not fnt.startswith("@") and not any([ord(c) > 127 for c in fnt])])
return ["default"] + cleaned_fonts
""" Return a sane list of fonts for the system that has both regular and bold variants.
Pre-pend "default" to the beginning of the list.
Returns
-------
list:
A list of valid fonts for the system
"""
fmanager = font_manager.FontManager()
fonts = dict()
for font in fmanager.ttflist:
if str(font.weight) in ("400", "normal", "regular"):
fonts.setdefault(font.name, dict())["regular"] = True
if str(font.weight) in ("700", "bold"):
fonts.setdefault(font.name, dict())["bold"] = True
valid_fonts = {key for key, val in fonts.items() if len(val) == 2}
retval = sorted(list(valid_fonts.intersection(tk_font.families())))
return ["default"] + retval

View File

@ -2,11 +2,14 @@
""" Utility functions for the GUI """
import logging
import os
import platform
import sys
import tkinter as tk
from tkinter import filedialog
from threading import Event, Thread
from queue import Queue
import numpy as np
from PIL import Image, ImageDraw, ImageTk
@ -767,10 +770,11 @@ class Config():
def __init__(self, root, cli_opts, statusbar, session):
logger.debug("Initializing %s: (root %s, cli_opts: %s, statusbar: %s, session: %s)",
self.__class__.__name__, root, cli_opts, statusbar, session)
self._default_font = self._set_default_font()
self._constants = dict(
root=root,
scaling_factor=self._get_scaling(root),
default_font=tk.font.nametofont("TkDefaultFont").configure()["family"])
default_font=self._default_font)
self._gui_objects = dict(
cli_opts=cli_opts,
tk_vars=self._set_tk_vars(),
@ -781,7 +785,6 @@ class Config():
command_notebook=None) # set in command.py
self._user_config = UserConfig(None)
self.session = session
self._default_font = tk.font.nametofont("TkDefaultFont").configure()["family"]
logger.debug("Initialized %s", self.__class__.__name__)
# Constants
@ -891,6 +894,25 @@ class Config():
logger.debug("dpi: %s, scaling: %s'", dpi, scaling)
return scaling
@classmethod
def _set_default_font(cls):
""" Set the default font.
For macOS and Windows, this just pulls back the system default font.
For Linux, quite often the default is not ideal, so we try to pull a sane default from
installed fonts.
"""
if platform.system() == "Linux":
for family in ("DejaVu Sans", "Noto Sans", "Nimbus Sans"):
if family in tk.font.families():
logger.debug("Setting default font to: '%s'", family)
tk.font.nametofont("TkDefaultFont").configure(family=family)
tk.font.nametofont("TkHeadingFont").configure(family=family)
tk.font.nametofont("TkMenuFont").configure(family=family)
break
return tk.font.nametofont("TkDefaultFont").configure()["family"]
def set_default_options(self):
""" Set the default options for :mod:`lib.gui.projects`