Skip to content

Commit 297c9cc

Browse files
Merge #7068: feat(qt): extract Dash-specific font infrastructure to qt/guiutil_font.{cpp,h}, encapsulate globals into Font{Info,Registry}, add arbitrary font support
b6d418a fix: always use `getFontNormal()` for Montserrat on macOS (Kittywhiskers Van Gogh) 4cc4040 fix: restore font on cancel, apply font when adjusting weight (Kittywhiskers Van Gogh) 7c2f616 fix: don't rely on `QSettings` weights if `-font-family` set (Kittywhiskers Van Gogh) d2b8c0a fix: don't display stale weight settings if `-font-family` set (Kittywhiskers Van Gogh) 3faa368 feat(qt): add arbitrary font loading support (Kittywhiskers Van Gogh) d3d72a2 refactor: miscellaneous refactoring (Kittywhiskers Van Gogh) cdd7b37 refactor: consolidate font attributes to struct (Kittywhiskers Van Gogh) 2f30002 fix: disable appearance widget field if overridden by command line (Kittywhiskers Van Gogh) 6c71be8 fix: respect overridden arguments, avoid font pop in appearance widget (Kittywhiskers Van Gogh) cc0665e fix: display error if `-font-family` set to nonexistent font (Kittywhiskers Van Gogh) 1f24526 fix: reintroduce fallbacks and assertion checks to avoid regressions (Kittywhiskers Van Gogh) e0eb254 refactor: drop `FontFamily`, identify font with string name instead (Kittywhiskers Van Gogh) b6a5bdb refactor: reduce `updateFonts()` invocations (Kittywhiskers Van Gogh) 96f5f93 refactor: use `g_font_registry` even more more extensively (Kittywhiskers Van Gogh) fb9fc20 refactor: use `g_font_registry` even more extensively (Kittywhiskers Van Gogh) e5ee874 refactor: use `g_font_registry` more extensively (Kittywhiskers Van Gogh) d25c853 refactor: encapsulate global font state (consolidate tunables) (Kittywhiskers Van Gogh) 8a93d51 refactor: encapsulate global font state (consolidate maps) (Kittywhiskers Van Gogh) 7687e01 refactor: reduce entanglement with `FontFamily` (Kittywhiskers Van Gogh) d21ceb1 refactor: use `strprintf` for `qDebug()` messages (Kittywhiskers Van Gogh) 1f239fc refactor: consolidate weight argument maps (Kittywhiskers Van Gogh) c17a7eb refactor: consolidate Montserrat weight and name maps (Kittywhiskers Van Gogh) 6f3b11d fix: use argument passed to lambda instead of global (Kittywhiskers Van Gogh) 9cd9d44 fix: fallback to default weight if setting incompatible with font (Kittywhiskers Van Gogh) 5d5bbff lint: add to `non-backported.txt`, apply some `clang-format` suggestions (Kittywhiskers Van Gogh) 84ae194 refactor: move Dash-specific font management to `qt/guiutil_font.{cpp,h}` (Kittywhiskers Van Gogh) Pull request description: ## Additional Information * Dependency for dashpay/dash#6831 * Additional fixes have been included to deal with crash cases encountered when working on [dash#6831](dashpay/dash#6831), the first fix uses fallback weights if `fontWeight{Bold,Normal}` is set to a value not supported by the font. This is possible if the values are manually edited or overridden. <details> <summary>Crash:</summary> ``` dash@8cf837862c23:/src/dash$ ./src/qt/dash-qt --testnet QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-dash' Assertion failure: assertion: nIndex != -1 file: qt/optionsmodel.cpp, line: 546 function: virtual QVariant OptionsModel::data(const QModelIndex &, int) const 0#: (0x55D83D7D9C71) stacktraces.cpp:656 - __wrap___assert_fail 1#: (0x55D83CF4D6D4) optionsmodel.cpp:553 - OptionsModel::data(QModelIndex const&, int) const 2#: (0x55D83E4C4608) <unknown-file> - ??? 3#: (0x55D83E4C9068) <unknown-file> - ??? 4#: (0x55D83E4CA06B) <unknown-file> - ??? 5#: (0x55D83CF0CF7E) guiutil.cpp - GUIUtil::setupAppearance(QWidget*, OptionsModel*) 6#: (0x55D83CEDB8D9) bitcoin.cpp:436 - BitcoinApplication::initializeResult(bool, interfaces::BlockAndHeaderTipInfo) 7#: (0x55D83CEE8DDD) qobjectdefs_impl.h:425 - QtPrivate::QSlotObject<void (BitcoinApplication::*)(bool, interfaces::BlockAndHeaderTipInfo), QtPrivate::List<bool, interfaces::BlockAndHeaderTipInfo>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) 8#: (0x55D83E0C288D) <unknown-file> - ??? 9#: (0x55D83DC5DAB9) <unknown-file> - ??? 10#: (0x55D83E2F4186) <unknown-file> - ??? 11#: (0x55D83E2F5BAC) <unknown-file> - ??? 12#: (0x55D83E2F6E7E) <unknown-file> - ??? 13#: (0x55D83E0A5668) <unknown-file> - ??? dash-qt: qt/optionsmodel.cpp:546: virtual QVariant OptionsModel::data(const QModelIndex &, int) const: Assertion `nIndex != -1' failed. Aborted (core dumped) ``` </details> The second fix uses the argument passed to the lambda `addBestDefaults` instead of a global font, this has also been flagged by CodeRabbit ([comment](dashpay/dash#6831 (comment))) <details> <summary>Crash:</summary> ``` dash@8cf837862c23:/src/dash$ ./src/qt/dash-qt --testnet QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-dash' Assertion failure: assertion: mapWeight.count(weight) file: qt/guiutil_font.cpp, line: 119 function: int GUIUtil::weightToArg(const QFont::Weight) 0#: (0x55B3C209CC31) stacktraces.cpp:656 - __wrap___assert_fail 1#: (0x55B3C17E17D6) basic_ios.h:50 - std::ctype<char> const& std::__check_facet<std::ctype<char> >(std::ctype<char> const*) 2#: (0x55B3C17E17D6) basic_ios.h:454 - std::basic_ios<char, std::char_traits<char> >::widen(char) const 3#: (0x55B3C17E17D6) ostream:742 - std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>>&) 4#: (0x55B3C17E17D6) ostream:121 - std::ostream::operator<<(std::ostream& (*)(std::ostream&)) 5#: (0x55B3C17E17D6) guiutil_font.cpp:118 - GUIUtil::weightToArg(QFont::Weight) 6#: (0x55B3C17A189A) bitcoin.cpp:702 - GuiMain(int, char**) 7#: (0x7F88E4B1E1CA) libc_start_call_main.h:74 - __libc_start_call_main 8#: (0x7F88E4B1E28B) libc-start.c:128 - call_init 9#: (0x7F88E4B1E28B) libc-start.c:347 - __libc_start_main_impl 10#: (0x55B3C179C545) <unknown-file> - ??? dash-qt: qt/guiutil_font.cpp:119: int GUIUtil::weightToArg(const QFont::Weight): Assertion `mapWeight.count(weight)' failed. Aborted (core dumped) ``` </details> * A helper, `qstrprintf()` has been introduced to streamline debug print logging based on a review suggestion ([comment](dashpay/dash#6831 (comment))). ## Breaking Changes None expected. ## How Has This Been Tested? On build 2dc48deb running Dash Qt on `./src/qt/dash-qt --testnet --font-family="Comic Sans MS"` ![Dash Qt running with Comic Sans](https://github.com/user-attachments/assets/90d40d00-24ed-4190-b2b4-bfee61933490) On build 2dc48deb running Dash Qt on `./src/qt/dash-qt --testnet --font-family="Fictional Sans Serif"` ![Dash Qt existing when attempting to run with invalid font](https://github.com/user-attachments/assets/0e6009ef-5046-4755-a692-2d5c5f368148) ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added or updated relevant unit/integration/functional/e2e tests **(note: N/A)** - [x] I have made corresponding changes to the documentation **(note: N/A)** - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ ACKs for top commit: UdjinM6: Light ACK b6d418a Tree-SHA512: defdbd3864405c22b4d3cd381a58331b1ad62785f9b037cbdc3e6fd67045039ec116cabb18ddef5296c0e9647c50efe7b22e685c994cf31606328d917a349747
2 parents 9d69043 + b6d418a commit 297c9cc

37 files changed

+927
-815
lines changed

src/Makefile.qt.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ BITCOIN_QT_H = \
139139
qt/proposalwizard.h \
140140
qt/guiconstants.h \
141141
qt/guiutil.h \
142+
qt/guiutil_font.h \
142143
qt/initexecutor.h \
143144
qt/intro.h \
144145
qt/macdockiconhandler.h \
@@ -230,6 +231,7 @@ BITCOIN_QT_BASE_CPP = \
230231
qt/clientmodel.cpp \
231232
qt/csvmodelwriter.cpp \
232233
qt/guiutil.cpp \
234+
qt/guiutil_font.cpp \
233235
qt/initexecutor.cpp \
234236
qt/intro.cpp \
235237
qt/modaloverlay.cpp \

src/qt/addressbookpage.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <qt/csvmodelwriter.h>
1515
#include <qt/editaddressdialog.h>
1616
#include <qt/guiutil.h>
17+
#include <qt/guiutil_font.h>
1718
#include <qt/optionsmodel.h>
1819
#include <qt/qrdialog.h>
1920

src/qt/addresstablemodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <qt/addresstablemodel.h>
77

88
#include <qt/guiutil.h>
9+
#include <qt/guiutil_font.h>
910
#include <qt/walletmodel.h>
1011

1112
#include <key_io.h>

src/qt/appearancewidget.cpp

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020

2121
AppearanceWidget::AppearanceWidget(QWidget* parent) :
2222
QWidget(parent),
23-
ui{new Ui::AppearanceWidget()}
23+
ui{new Ui::AppearanceWidget()},
24+
prevTheme{GUIUtil::getActiveTheme()},
25+
prevScale{GUIUtil::g_font_registry.GetFontScale()},
26+
prevFontFamily{GUIUtil::g_font_registry.GetFont()},
27+
prevWeightNormal{GUIUtil::g_font_registry.GetWeightNormal()},
28+
prevWeightBold{GUIUtil::g_font_registry.GetWeightBold()}
2429
{
2530
ui->setupUi(this);
2631

2732
for (const QString& entry : GUIUtil::listThemes()) {
2833
ui->theme->addItem(entry, QVariant(entry));
2934
}
3035

31-
GUIUtil::FontFamily fontSystem = GUIUtil::FontFamily::SystemDefault;
32-
GUIUtil::FontFamily fontMontserrat = GUIUtil::FontFamily::Montserrat;
33-
34-
ui->fontFamily->addItem(GUIUtil::fontFamilyToString(fontSystem), QVariant(static_cast<int>(fontSystem)));
35-
ui->fontFamily->addItem(GUIUtil::fontFamilyToString(fontMontserrat), QVariant(static_cast<int>(fontMontserrat)));
36+
for (size_t idx{0}; idx < GUIUtil::g_fonts_known.size(); idx++) {
37+
ui->fontFamily->addItem(GUIUtil::g_fonts_known[idx], QVariant((uint16_t)idx));
38+
}
3639

3740
updateWeightSlider();
3841

@@ -61,34 +64,74 @@ AppearanceWidget::~AppearanceWidget()
6164
if (prevTheme != GUIUtil::getActiveTheme()) {
6265
updateTheme(prevTheme);
6366
}
64-
if (prevFontFamily != GUIUtil::getFontFamily()) {
65-
GUIUtil::setFontFamily(prevFontFamily);
67+
if (prevFontFamily != GUIUtil::g_font_registry.GetFont()) {
68+
const bool setfont_ret{GUIUtil::g_font_registry.SetFont(prevFontFamily)};
69+
assert(setfont_ret);
70+
GUIUtil::setApplicationFont();
6671
}
67-
if (prevScale != GUIUtil::getFontScale()) {
68-
GUIUtil::setFontScale(prevScale);
72+
if (prevScale != GUIUtil::g_font_registry.GetFontScale()) {
73+
GUIUtil::g_font_registry.SetFontScale(prevScale);
6974
}
70-
if (prevWeightNormal != GUIUtil::getFontWeightNormal()) {
71-
GUIUtil::setFontWeightNormal(prevWeightNormal);
75+
if (prevWeightNormal != GUIUtil::g_font_registry.GetWeightNormal()) {
76+
GUIUtil::g_font_registry.SetWeightNormal(prevWeightNormal);
7277
}
73-
if (prevWeightBold != GUIUtil::getFontWeightBold()) {
74-
GUIUtil::setFontWeightBold(prevWeightBold);
78+
if (prevWeightBold != GUIUtil::g_font_registry.GetWeightBold()) {
79+
GUIUtil::g_font_registry.SetWeightBold(prevWeightBold);
7580
}
81+
GUIUtil::setApplicationFont();
82+
GUIUtil::updateFonts();
7683
}
7784
delete ui;
7885
}
7986

8087
void AppearanceWidget::setModel(OptionsModel* _model)
8188
{
8289
this->model = _model;
90+
if (!_model) return;
91+
92+
mapper->setModel(_model);
93+
mapper->addMapping(ui->theme, OptionsModel::Theme);
94+
mapper->addMapping(ui->fontFamily, OptionsModel::FontFamily);
95+
mapper->addMapping(ui->fontScaleSlider, OptionsModel::FontScale);
96+
mapper->addMapping(ui->fontWeightNormalSlider, OptionsModel::FontWeightNormal);
97+
mapper->addMapping(ui->fontWeightBoldSlider, OptionsModel::FontWeightBold);
98+
99+
const QSignalBlocker fontFamilyBlocker(ui->fontFamily);
100+
const QSignalBlocker fontScaleBlocker(ui->fontScaleSlider);
101+
const QSignalBlocker fontWeightNormalBlocker(ui->fontWeightNormalSlider);
102+
const QSignalBlocker fontWeightBoldBlocker(ui->fontWeightBoldSlider);
103+
104+
mapper->toFirst();
105+
106+
const bool override_family{_model->isOptionOverridden("-font-family")};
107+
if (override_family) {
108+
ui->fontFamily->setEnabled(false);
109+
if (const auto idx{ui->fontFamily->findText(GUIUtil::g_font_registry.GetFont())}; idx != -1) {
110+
ui->fontFamily->setCurrentIndex(idx);
111+
}
112+
}
113+
114+
if (_model->isOptionOverridden("-font-scale")) {
115+
ui->fontScaleSlider->setEnabled(false);
116+
ui->fontScaleSlider->setValue(GUIUtil::g_font_registry.GetFontScale());
117+
}
83118

84-
if (_model) {
85-
mapper->setModel(_model);
86-
mapper->addMapping(ui->theme, OptionsModel::Theme);
87-
mapper->addMapping(ui->fontFamily, OptionsModel::FontFamily);
88-
mapper->addMapping(ui->fontScaleSlider, OptionsModel::FontScale);
89-
mapper->addMapping(ui->fontWeightNormalSlider, OptionsModel::FontWeightNormal);
90-
mapper->addMapping(ui->fontWeightBoldSlider, OptionsModel::FontWeightBold);
91-
mapper->toFirst();
119+
if (bool is_overridden{_model->isOptionOverridden("-font-weight-normal")}; is_overridden || override_family) {
120+
if (is_overridden) {
121+
ui->fontWeightNormalSlider->setEnabled(false);
122+
}
123+
if (const auto idx{GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightNormal())}; idx != -1) {
124+
ui->fontWeightNormalSlider->setValue(idx);
125+
}
126+
}
127+
128+
if (bool is_overridden{_model->isOptionOverridden("-font-weight-bold")}; is_overridden || override_family) {
129+
if (is_overridden) {
130+
ui->fontWeightBoldSlider->setEnabled(false);
131+
}
132+
if (const auto idx{GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightBold())}; idx != -1) {
133+
ui->fontWeightBoldSlider->setValue(idx);
134+
}
92135
}
93136
}
94137

@@ -111,13 +154,17 @@ void AppearanceWidget::updateTheme(const QString& theme)
111154

112155
void AppearanceWidget::updateFontFamily(int index)
113156
{
114-
GUIUtil::setFontFamily(static_cast<GUIUtil::FontFamily>(ui->fontFamily->itemData(index).toInt()));
157+
const bool setfont_ret{GUIUtil::g_font_registry.SetFont(GUIUtil::g_fonts_known[ui->fontFamily->itemData(index).toInt()])};
158+
assert(setfont_ret);
159+
GUIUtil::setApplicationFont();
160+
GUIUtil::updateFonts();
115161
updateWeightSlider(true);
116162
}
117163

118164
void AppearanceWidget::updateFontScale(int nScale)
119165
{
120-
GUIUtil::setFontScale(nScale);
166+
GUIUtil::g_font_registry.SetFontScale(nScale);
167+
GUIUtil::updateFonts();
121168
}
122169

123170
void AppearanceWidget::updateFontWeightNormal(int nValue, bool fForce)
@@ -128,7 +175,9 @@ void AppearanceWidget::updateFontWeightNormal(int nValue, bool fForce)
128175
}
129176
const QSignalBlocker blocker(ui->fontWeightNormalSlider);
130177
ui->fontWeightNormalSlider->setValue(nSliderValue);
131-
GUIUtil::setFontWeightNormal(GUIUtil::supportedWeightFromIndex(ui->fontWeightNormalSlider->value()));
178+
GUIUtil::g_font_registry.SetWeightNormal(GUIUtil::g_font_registry.IdxToWeight(ui->fontWeightNormalSlider->value()));
179+
GUIUtil::setApplicationFont();
180+
GUIUtil::updateFonts();
132181
}
133182

134183
void AppearanceWidget::updateFontWeightBold(int nValue, bool fForce)
@@ -139,22 +188,24 @@ void AppearanceWidget::updateFontWeightBold(int nValue, bool fForce)
139188
}
140189
const QSignalBlocker blocker(ui->fontWeightBoldSlider);
141190
ui->fontWeightBoldSlider->setValue(nSliderValue);
142-
GUIUtil::setFontWeightBold(GUIUtil::supportedWeightFromIndex(ui->fontWeightBoldSlider->value()));
191+
GUIUtil::g_font_registry.SetWeightBold(GUIUtil::g_font_registry.IdxToWeight(ui->fontWeightBoldSlider->value()));
192+
GUIUtil::setApplicationFont();
193+
GUIUtil::updateFonts();
143194
}
144195

145196
void AppearanceWidget::updateWeightSlider(const bool fForce)
146197
{
147-
int nMaximum = GUIUtil::getSupportedWeights().size() - 1;
198+
int nMaximum = GUIUtil::g_font_registry.GetSupportedWeights().size() - 1;
148199

149200
ui->fontWeightNormalSlider->setMinimum(0);
150201
ui->fontWeightNormalSlider->setMaximum(nMaximum);
151202

152203
ui->fontWeightBoldSlider->setMinimum(0);
153204
ui->fontWeightBoldSlider->setMaximum(nMaximum);
154205

155-
if (fForce || !GUIUtil::isSupportedWeight(prevWeightNormal) || !GUIUtil::isSupportedWeight(prevWeightBold)) {
156-
int nIndexNormal = GUIUtil::supportedWeightToIndex(GUIUtil::getSupportedFontWeightNormalDefault());
157-
int nIndexBold = GUIUtil::supportedWeightToIndex(GUIUtil::getSupportedFontWeightBoldDefault());
206+
if (fForce || !GUIUtil::g_font_registry.IsValidWeight(prevWeightNormal) || !GUIUtil::g_font_registry.IsValidWeight(prevWeightBold)) {
207+
int nIndexNormal = GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightNormalDefault());
208+
int nIndexBold = GUIUtil::g_font_registry.WeightToIdx(GUIUtil::g_font_registry.GetWeightBoldDefault());
158209
assert(nIndexNormal != -1 && nIndexBold != -1);
159210
updateFontWeightNormal(nIndexNormal, true);
160211
updateFontWeightBold(nIndexBold, true);

src/qt/appearancewidget.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <QWidget>
99

1010
#include <qt/guiutil.h>
11+
#include <qt/guiutil_font.h>
1112

1213
namespace Ui {
1314
class AppearanceWidget;
@@ -47,11 +48,11 @@ private Q_SLOTS:
4748
QDataWidgetMapper* mapper;
4849
OptionsModel* model;
4950
bool fAcceptChanges{false};
50-
QString prevTheme{GUIUtil::getActiveTheme()};
51-
int prevScale{GUIUtil::getFontScale()};
52-
GUIUtil::FontFamily prevFontFamily{GUIUtil::getFontFamily()};
53-
QFont::Weight prevWeightNormal{GUIUtil::getFontWeightNormal()};
54-
QFont::Weight prevWeightBold{GUIUtil::getFontWeightBold()};
51+
QString prevTheme;
52+
int prevScale;
53+
QString prevFontFamily;
54+
QFont::Weight prevWeightNormal;
55+
QFont::Weight prevWeightBold;
5556

5657
void updateWeightSlider(bool fForce = false);
5758
};

src/qt/askpassphrasedialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <qt/guiconstants.h>
1414
#include <qt/guiutil.h>
15+
#include <qt/guiutil_font.h>
1516
#include <qt/walletmodel.h>
1617

1718
#include <support/allocators/secure.h>
@@ -30,7 +31,7 @@ AskPassphraseDialog::AskPassphraseDialog(Mode _mode, QWidget *parent, SecureStri
3031
{
3132
ui->setupUi(this);
3233

33-
GUIUtil::setFont({ui->capsLabel}, GUIUtil::FontWeight::Bold);
34+
GUIUtil::setFont({ui->capsLabel}, {GUIUtil::g_font_registry.GetWeightBold()});
3435

3536
GUIUtil::updateFonts();
3637

src/qt/bitcoin.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <qt/clientmodel.h>
2323
#include <qt/guiconstants.h>
2424
#include <qt/guiutil.h>
25+
#include <qt/guiutil_font.h>
2526
#include <qt/initexecutor.h>
2627
#include <qt/intro.h>
2728
#include <qt/networkstyle.h>
@@ -488,10 +489,10 @@ static void SetupUIArgs(ArgsManager& argsman)
488489
{
489490
argsman.AddArg("-choosedatadir", strprintf(QObject::tr("Choose data directory on startup (default: %u)").toStdString(), DEFAULT_CHOOSE_DATADIR), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
490491
argsman.AddArg("-custom-css-dir", "Set a directory which contains custom css files. Those will be used as stylesheets for the UI.", ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
491-
argsman.AddArg("-font-family", QObject::tr("Set the font family. Possible values: %1. (default: %2)").arg("SystemDefault, Montserrat").arg(GUIUtil::fontFamilyToString(GUIUtil::getFontFamilyDefault())).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
492-
argsman.AddArg("-font-scale", QObject::tr("Set a scale factor which gets applied to the base font size. Possible range %1 (smallest fonts) to %2 (largest fonts). (default: %3)").arg(-100).arg(100).arg(GUIUtil::getFontScaleDefault()).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
493-
argsman.AddArg("-font-weight-bold", QObject::tr("Set the font weight for bold texts. Possible range %1 to %2 (default: %3)").arg(0).arg(8).arg(GUIUtil::weightToArg(GUIUtil::getFontWeightBoldDefault())).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
494-
argsman.AddArg("-font-weight-normal", QObject::tr("Set the font weight for normal texts. Possible range %1 to %2 (default: %3)").arg(0).arg(8).arg(GUIUtil::weightToArg(GUIUtil::getFontWeightNormalDefault())).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
492+
argsman.AddArg("-font-family", QObject::tr("Set the font family. Possible values: %1. (default: %2)").arg(Join(GUIUtil::g_fonts_known, ", ")).arg(GUIUtil::FontRegistry::DEFAULT_FONT).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
493+
argsman.AddArg("-font-scale", QObject::tr("Set a scale factor which gets applied to the base font size. Possible range %1 (smallest fonts) to %2 (largest fonts). (default: %3)").arg(-100).arg(100).arg(GUIUtil::FontRegistry::DEFAULT_FONT_SCALE).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
494+
argsman.AddArg("-font-weight-bold", QObject::tr("Set the font weight for bold texts. Possible range %1 to %2 (default: %3)").arg(0).arg(8).arg(GUIUtil::weightToArg(GUIUtil::FontRegistry::TARGET_WEIGHT_BOLD)).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
495+
argsman.AddArg("-font-weight-normal", QObject::tr("Set the font weight for normal texts. Possible range %1 to %2 (default: %3)").arg(0).arg(8).arg(GUIUtil::weightToArg(GUIUtil::FontRegistry::TARGET_WEIGHT_NORMAL)).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
495496
argsman.AddArg("-lang=<lang>", QObject::tr("Set language, for example \"de_DE\" (default: system locale)").toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
496497
argsman.AddArg("-min", QObject::tr("Start minimized").toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
497498
argsman.AddArg("-resetguisettings", QObject::tr("Reset all settings changed in the GUI").toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
@@ -664,56 +665,53 @@ int GuiMain(int argc, char* argv[])
664665
GUIUtil::LogQtInfo();
665666
// Load custom application fonts and setup font management
666667
if (!GUIUtil::loadFonts()) {
667-
QMessageBox::critical(nullptr, PACKAGE_NAME,
668-
QObject::tr("Error: Failed to load application fonts."));
668+
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: Failed to load application fonts."));
669669
return EXIT_FAILURE;
670670
}
671671
// Load GUI settings from QSettings
672672
app.createOptionsModel(gArgs.GetBoolArg("-resetguisettings", false));
673673
// Validate/set font family
674674
if (gArgs.IsArgSet("-font-family")) {
675-
GUIUtil::FontFamily family;
676-
QString strFamily = gArgs.GetArg("-font-family", GUIUtil::fontFamilyToString(GUIUtil::getFontFamilyDefault()).toStdString()).c_str();
677-
try {
678-
family = GUIUtil::fontFamilyFromString(strFamily);
679-
} catch (const std::exception& e) {
680-
QMessageBox::critical(nullptr, PACKAGE_NAME,
681-
QObject::tr("Error: Specified font-family invalid. Valid values: %1.").arg("SystemDefault, Montserrat"));
675+
QString family = gArgs.GetArg("-font-family", GUIUtil::FontRegistry::DEFAULT_FONT.toUtf8().toStdString()).c_str();
676+
if (!GUIUtil::g_font_registry.RegisterFont(family) || !GUIUtil::g_font_registry.SetFont(family)) {
677+
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: Font \"%1\" could not be loaded.").arg(family));
682678
return EXIT_FAILURE;
683679
}
684-
GUIUtil::setFontFamily(family);
685680
}
686681
// Validate/set normal font weight
687682
if (gArgs.IsArgSet("-font-weight-normal")) {
688683
QFont::Weight weight;
689-
if (!GUIUtil::weightFromArg(gArgs.GetIntArg("-font-weight-normal", GUIUtil::weightToArg(GUIUtil::getFontWeightNormal())), weight)) {
684+
if (!GUIUtil::weightFromArg(gArgs.GetIntArg("-font-weight-normal", GUIUtil::weightToArg(GUIUtil::g_font_registry.GetWeightNormal())), weight)) {
690685
QMessageBox::critical(nullptr, PACKAGE_NAME,
691686
QObject::tr("Error: Specified font-weight-normal invalid. Valid range %1 to %2.").arg(0).arg(8));
692687
return EXIT_FAILURE;
693688
}
694-
GUIUtil::setFontWeightNormal(weight);
689+
GUIUtil::g_font_registry.SetWeightNormal(weight);
695690
}
696691
// Validate/set bold font weight
697692
if (gArgs.IsArgSet("-font-weight-bold")) {
698693
QFont::Weight weight;
699-
if (!GUIUtil::weightFromArg(gArgs.GetIntArg("-font-weight-bold", GUIUtil::weightToArg(GUIUtil::getFontWeightBold())), weight)) {
694+
if (!GUIUtil::weightFromArg(gArgs.GetIntArg("-font-weight-bold", GUIUtil::weightToArg(GUIUtil::g_font_registry.GetWeightBold())), weight)) {
700695
QMessageBox::critical(nullptr, PACKAGE_NAME,
701696
QObject::tr("Error: Specified font-weight-bold invalid. Valid range %1 to %2.").arg(0).arg(8));
702697
return EXIT_FAILURE;
703698
}
704-
GUIUtil::setFontWeightBold(weight);
699+
GUIUtil::g_font_registry.SetWeightBold(weight);
705700
}
706701
// Validate/set font scale
707702
if (gArgs.IsArgSet("-font-scale")) {
708703
const int nScaleMin = -100, nScaleMax = 100;
709-
int nScale = gArgs.GetIntArg("-font-scale", GUIUtil::getFontScale());
704+
int nScale = gArgs.GetIntArg("-font-scale", GUIUtil::g_font_registry.GetFontScale());
710705
if (nScale < nScaleMin || nScale > nScaleMax) {
711706
QMessageBox::critical(nullptr, PACKAGE_NAME,
712707
QObject::tr("Error: Specified font-scale invalid. Valid range %1 to %2.").arg(nScaleMin).arg(nScaleMax));
713708
return EXIT_FAILURE;
714709
}
715-
GUIUtil::setFontScale(nScale);
710+
GUIUtil::g_font_registry.SetFontScale(nScale);
716711
}
712+
// Apply font changes
713+
GUIUtil::updateFonts();
714+
GUIUtil::setApplicationFont();
717715
// Validate/set custom css directory
718716
if (gArgs.IsArgSet("-custom-css-dir")) {
719717
fs::path customDir = gArgs.GetPathArg("-custom-css-dir");

src/qt/bitcoingui.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <qt/createwalletdialog.h>
1111
#include <qt/guiconstants.h>
1212
#include <qt/guiutil.h>
13+
#include <qt/guiutil_font.h>
1314
#include <qt/modaloverlay.h>
1415
#include <qt/networkstyle.h>
1516
#include <qt/notificator.h>
@@ -757,7 +758,7 @@ void BitcoinGUI::createToolBars()
757758
connect(tabGroup, qOverload<QAbstractButton *, bool>(&QButtonGroup::buttonToggled), this, &BitcoinGUI::highlightTabButton);
758759

759760
for (auto button : tabGroup->buttons()) {
760-
GUIUtil::setFont({button}, GUIUtil::FontWeight::Normal, 16);
761+
GUIUtil::setFont({button}, {GUIUtil::g_font_registry.GetWeightNormal(), 16});
761762
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
762763
button->setToolTip(button->statusTip());
763764
button->setCheckable(true);
@@ -1239,7 +1240,7 @@ void BitcoinGUI::openClicked()
12391240

12401241
void BitcoinGUI::highlightTabButton(QAbstractButton *button, bool checked)
12411242
{
1242-
GUIUtil::setFont({button}, checked ? GUIUtil::FontWeight::Bold : GUIUtil::FontWeight::Normal, 16);
1243+
GUIUtil::setFont({button}, {checked ? GUIUtil::g_font_registry.GetWeightBold() : GUIUtil::g_font_registry.GetWeightNormal(), 16});
12431244
GUIUtil::updateFonts();
12441245
}
12451246

src/qt/coincontroldialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <qt/addresstablemodel.h>
1414
#include <qt/bitcoinunits.h>
1515
#include <qt/guiutil.h>
16+
#include <qt/guiutil_font.h>
1617
#include <qt/optionsmodel.h>
1718
#include <qt/walletmodel.h>
1819

@@ -57,7 +58,7 @@ CoinControlDialog::CoinControlDialog(CCoinControl& coin_control, WalletModel* _m
5758
ui->labelCoinControlFeeText,
5859
ui->labelCoinControlAfterFeeText,
5960
ui->labelCoinControlChangeText
60-
}, GUIUtil::FontWeight::Bold);
61+
}, {GUIUtil::g_font_registry.GetWeightBold()});
6162

6263
GUIUtil::updateFonts();
6364

0 commit comments

Comments
 (0)