Skip to content

Commit 85b49b9

Browse files
committed
qt: add "Roboto Mono" as non-selectable font
1 parent c40f3db commit 85b49b9

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

src/qt/appearancewidget.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ AppearanceWidget::AppearanceWidget(QWidget* parent) :
3434
}
3535

3636
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));
37+
const auto& [font, selectable] = GUIUtil::g_fonts_known[idx];
38+
if (selectable) { ui->fontFamily->addItem(font, QVariant((uint16_t)idx)); }
3839
}
3940

4041
updateWeightSlider();
@@ -154,7 +155,7 @@ void AppearanceWidget::updateTheme(const QString& theme)
154155

155156
void AppearanceWidget::updateFontFamily(int index)
156157
{
157-
const bool setfont_ret{GUIUtil::g_font_registry.SetFont(GUIUtil::g_fonts_known[ui->fontFamily->itemData(index).toInt()])};
158+
const bool setfont_ret{GUIUtil::g_font_registry.SetFont(GUIUtil::g_fonts_known[ui->fontFamily->itemData(index).toInt()].first)};
158159
assert(setfont_ret);
159160
GUIUtil::setApplicationFont();
160161
GUIUtil::updateFonts();

src/qt/bitcoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ static void SetupUIArgs(ArgsManager& argsman)
489489
{
490490
argsman.AddArg("-choosedatadir", strprintf(QObject::tr("Choose data directory on startup (default: %u)").toStdString(), DEFAULT_CHOOSE_DATADIR), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
491491
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);
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);
492+
argsman.AddArg("-font-family", QObject::tr("Set the font family. Possible values: %1. (default: %2)").arg(Join(GUIUtil::getFonts(/*selectable_only=*/true), ", ")).arg(GUIUtil::FontRegistry::DEFAULT_FONT).toStdString(), ArgsManager::ALLOW_ANY, OptionsCategory::GUI);
493493
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);
494494
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);
495495
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);
@@ -673,7 +673,7 @@ int GuiMain(int argc, char* argv[])
673673
// Validate/set font family
674674
if (gArgs.IsArgSet("-font-family")) {
675675
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)) {
676+
if (!GUIUtil::g_font_registry.RegisterFont(family, /*selectable=*/true) || !GUIUtil::g_font_registry.SetFont(family)) {
677677
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: Font \"%1\" could not be loaded.").arg(family));
678678
return EXIT_FAILURE;
679679
}

src/qt/guiutil_font.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ QString qstrprintf(const std::string& fmt, const Args&... args)
116116
} // anonymous namespace
117117

118118
namespace GUIUtil {
119-
// Fonts known by the client
120-
std::vector<QString> g_fonts_known{
121-
OS_FONT_STR.toUtf8(),
122-
MONTSERRAT_FONT_STR.toUtf8(),
119+
//! Fonts known by the client
120+
std::vector<std::pair<QString, /*selectable=*/bool>> g_fonts_known{
121+
{MONTSERRAT_FONT_STR.toUtf8(), true},
122+
{OS_FONT_STR.toUtf8(), true},
123+
{ROBOTO_MONO_FONT_STR.toUtf8(), false},
123124
};
124125

125126
FontRegistry g_font_registry;
@@ -134,12 +135,15 @@ FontInfo::FontInfo(const QString& font_name)
134135

135136
FontInfo::~FontInfo() = default;
136137

137-
bool FontRegistry::RegisterFont(const QString& font, bool skip_checks)
138+
bool FontRegistry::RegisterFont(const QString& font, bool selectable, bool skip_checks)
138139
{
139-
const bool font_known{std::find(g_fonts_known.begin(), g_fonts_known.end(), font) != g_fonts_known.end()};
140+
const auto font_strs{getFonts(/*selectable_only=*/false)};
141+
auto font_it{std::find(font_strs.begin(), font_strs.end(), font)};
140142
if (m_weights.count(font)) {
141143
// Font's already registered
142-
assert(font_known);
144+
assert(font_it != font_strs.end());
145+
// Overwrite selectable flag
146+
g_fonts_known.at(std::distance(font_strs.begin(), font_it)).second = selectable;
143147
return true;
144148
}
145149
if (!skip_checks) {
@@ -150,8 +154,8 @@ bool FontRegistry::RegisterFont(const QString& font, bool skip_checks)
150154
}
151155
}
152156
m_weights.emplace(font, FontInfo(font));
153-
if (!font_known) {
154-
g_fonts_known.push_back(font);
157+
if (font_it == font_strs.end()) {
158+
g_fonts_known.emplace_back(font, selectable);
155159
}
156160
return true;
157161
}
@@ -290,8 +294,8 @@ bool loadFonts()
290294
}
291295
#endif // QT_NO_DEBUG
292296

293-
for (const auto& fonts : g_fonts_known) {
294-
assert(g_font_registry.RegisterFont(fonts, /*skip_checks=*/true));
297+
for (const auto& [fonts, selectable] : g_fonts_known) {
298+
assert(g_font_registry.RegisterFont(fonts, selectable, /*skip_checks=*/true));
295299
}
296300

297301
return true;
@@ -474,6 +478,10 @@ QFont getFont(const QString& font_name, const FontAttrib& font_attrib)
474478
font.setFamily(font_name);
475479
}
476480

481+
if (font_name == ROBOTO_MONO_FONT_STR) {
482+
font.setStyleHint(QFont::Monospace);
483+
}
484+
477485
#ifdef Q_OS_MACOS
478486
if (font_name != MONTSERRAT_FONT_STR)
479487
#endif // Q_OS_MACOS
@@ -500,6 +508,15 @@ QFont getFont(const FontAttrib& font_attrib)
500508
return getFont(g_font_registry.GetFont(), font_attrib);
501509
}
502510

511+
std::vector<QString> getFonts(bool selectable_only)
512+
{
513+
std::vector<QString> ret;
514+
for (const auto& [font, selectable] : g_fonts_known) {
515+
if (selectable || !selectable_only) { ret.emplace_back(font); }
516+
}
517+
return ret;
518+
}
519+
503520
QFont getFontNormal()
504521
{
505522
return getFont({g_font_registry.GetWeightNormal()});

src/qt/guiutil_font.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace GUIUtil {
1919
// TODO: Switch to QUtf8StringView when we switch to Qt 6
2020
constexpr QStringView MONTSERRAT_FONT_STR{u"Montserrat"};
2121
constexpr QStringView OS_FONT_STR{u"SystemDefault"};
22+
constexpr QStringView ROBOTO_MONO_FONT_STR{u"Roboto Mono"};
2223

2324
enum class FontWeight : uint8_t {
2425
Normal,
@@ -63,7 +64,7 @@ class FontRegistry {
6364
};
6465

6566
public:
66-
[[nodiscard]] bool RegisterFont(const QString& font, bool skip_checks = false);
67+
[[nodiscard]] bool RegisterFont(const QString& font, bool selectable, bool skip_checks = false);
6768

6869
bool IsValidWeight(const QFont::Weight& weight) const { return WeightToIdx(weight) != -1; }
6970
int WeightToIdx(const QFont::Weight& weight) const;
@@ -123,7 +124,7 @@ class FontRegistry {
123124

124125
extern FontRegistry g_font_registry;
125126

126-
extern std::vector<QString> g_fonts_known;
127+
extern std::vector<std::pair<QString, /*selectable=*/bool>> g_fonts_known;
127128

128129
/** Convert weight value from args (0-8) to QFont::Weight */
129130
bool weightFromArg(int nArg, QFont::Weight& weight);
@@ -147,6 +148,9 @@ void setFont(const std::vector<QWidget*>& vecWidgets, const FontAttrib& font_att
147148
GUIUtil::setFont */
148149
void updateFonts();
149150

151+
/** Get list of all selectable fonts */
152+
std::vector<QString> getFonts(bool selectable_only);
153+
150154
/** Get a properly weighted QFont object with the selected font. */
151155
QFont getFont(const QString& font_name, const FontAttrib& font_attrib);
152156
QFont getFont(const FontAttrib& font_attrib);

src/qt/optionsmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void OptionsModel::Init(bool resetSettings)
106106
}
107107
if (GUIUtil::fontsLoaded()) {
108108
if (auto font_name = QString::fromStdString(gArgs.GetArg("-font-family", settings.value("fontFamily").toString().toStdString()));
109-
GUIUtil::g_font_registry.RegisterFont(font_name) && GUIUtil::g_font_registry.SetFont(font_name)) {
109+
GUIUtil::g_font_registry.RegisterFont(font_name, /*selectable=*/true) && GUIUtil::g_font_registry.SetFont(font_name)) {
110110
GUIUtil::setApplicationFont();
111111
}
112112
}

0 commit comments

Comments
 (0)