From 029ba192b6168e2bbe1b445c39ff126412177eb2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 24 Nov 2020 13:46:39 +0100 Subject: Move getting default system font to configutils For easier testability, as a preparation for #5663. --- qutebrowser/config/configtypes.py | 45 +++------------------------------ qutebrowser/config/configutils.py | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 0b4ed4d3b..6328c3140 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -56,8 +56,8 @@ from typing import (Any, Callable, Dict as DictType, Iterable, Iterator, import attr import yaml from PyQt5.QtCore import QUrl, Qt -from PyQt5.QtGui import QColor, QFontDatabase -from PyQt5.QtWidgets import QTabWidget, QTabBar, QApplication +from PyQt5.QtGui import QColor +from PyQt5.QtWidgets import QTabWidget, QTabBar from PyQt5.QtNetwork import QNetworkProxy from qutebrowser.misc import objects, debugcachestats @@ -1159,50 +1159,11 @@ class FontBase(BaseType): If the given family value (fonts.default_family in the config) is unset, a system-specific default monospace font is used. - - Note that (at least) three ways of getting the default monospace font - exist: - - 1) f = QFont() - f.setStyleHint(QFont.Monospace) - print(f.defaultFamily()) - - 2) f = QFont() - f.setStyleHint(QFont.TypeWriter) - print(f.defaultFamily()) - - 3) f = QFontDatabase.systemFont(QFontDatabase.FixedFont) - print(f.family()) - - They yield different results depending on the OS: - - QFont.Monospace | QFont.TypeWriter | QFontDatabase - ------------------------------------------------------ - Windows: Courier New | Courier New | Courier New - Linux: DejaVu Sans Mono | DejaVu Sans Mono | monospace - macOS: Menlo | American Typewriter | Monaco - - Test script: https://p.cmpl.cc/d4dfe573 - - On Linux, it seems like both actually resolve to the same font. - - On macOS, "American Typewriter" looks like it indeed tries to imitate a - typewriter, so it's not really a suitable UI font. - - Looking at those Wikipedia articles: - - https://en.wikipedia.org/wiki/Monaco_(typeface) - https://en.wikipedia.org/wiki/Menlo_(typeface) - - the "right" choice isn't really obvious. Thus, let's go for the - QFontDatabase approach here, since it's by far the simplest one. """ if default_family: families = configutils.FontFamilies(default_family) else: - assert QApplication.instance() is not None - font = QFontDatabase.systemFont(QFontDatabase.FixedFont) - families = configutils.FontFamilies([font.family()]) + families = configutils.FontFamilies.from_system_default() cls.default_family = families.to_str(quote=True) cls.default_size = default_size diff --git a/qutebrowser/config/configutils.py b/qutebrowser/config/configutils.py index 1d98db3e7..b4eb004c2 100644 --- a/qutebrowser/config/configutils.py +++ b/qutebrowser/config/configutils.py @@ -29,6 +29,8 @@ from typing import ( MutableMapping) from PyQt5.QtCore import QUrl +from PyQt5.QtGui import QFontDatabase +from PyQt5.QtWidgets import QApplication from qutebrowser.utils import utils, urlmatch, usertypes, qtutils from qutebrowser.config import configexc @@ -295,6 +297,57 @@ class FontFamilies: families = self._quoted_families() if quote else self._families return ', '.join(families) + @classmethod + def from_system_default( + cls, + font_type: QFontDatabase.SystemFont = QFontDatabase.FixedFont, + ) -> 'FontFamilies': + """Get a FontFamilies object for the default system font. + + By default, the monospace font is returned, though via the "font_type" argument, + other types can be requested as well. + + Note that (at least) three ways of getting the default monospace font + exist: + + 1) f = QFont() + f.setStyleHint(QFont.Monospace) + print(f.defaultFamily()) + + 2) f = QFont() + f.setStyleHint(QFont.TypeWriter) + print(f.defaultFamily()) + + 3) f = QFontDatabase.systemFont(QFontDatabase.FixedFont) + print(f.family()) + + They yield different results depending on the OS: + + QFont.Monospace | QFont.TypeWriter | QFontDatabase + ------------------------------------------------------ + Windows: Courier New | Courier New | Courier New + Linux: DejaVu Sans Mono | DejaVu Sans Mono | monospace + macOS: Menlo | American Typewriter | Monaco + + Test script: https://p.cmpl.cc/d4dfe573 + + On Linux, it seems like both actually resolve to the same font. + + On macOS, "American Typewriter" looks like it indeed tries to imitate a + typewriter, so it's not really a suitable UI font. + + Looking at those Wikipedia articles: + + https://en.wikipedia.org/wiki/Monaco_(typeface) + https://en.wikipedia.org/wiki/Menlo_(typeface) + + the "right" choice isn't really obvious. Thus, let's go for the + QFontDatabase approach here, since it's by far the simplest one. + """ + assert QApplication.instance() is not None + font = QFontDatabase.systemFont(font_type) + return cls([font.family()]) + @classmethod def from_str(cls, family_str: str) -> 'FontFamilies': """Parse a CSS-like string of font families.""" -- cgit v1.2.3-54-g00ecf