[Option][GUI] Font options

This commit is contained in:
Eric Kotato 2022-09-15 23:17:03 +03:00 committed by Ilya Fedin
parent f69d4a4398
commit 990c4472c4
2 changed files with 28 additions and 6 deletions

View file

@ -11,6 +11,7 @@
#include "base/variant.h" #include "base/variant.h"
#include "base/base_file_utilities.h" #include "base/base_file_utilities.h"
#include "ui/integration.h" #include "ui/integration.h"
#include "ui/style/style_core_scale.h"
#include <QtCore/QMap> #include <QtCore/QMap>
#include <QtCore/QVector> #include <QtCore/QVector>
@ -38,6 +39,7 @@ namespace style {
namespace { namespace {
QString Custom; QString Custom;
CustomFontSettings CustomSettings;
} // namespace } // namespace
@ -50,6 +52,10 @@ void SetCustomFont(const QString &font) {
Custom = font; Custom = font;
} }
void SetCustomFontSettings(const CustomFontSettings &settings) {
CustomSettings = settings;
}
namespace internal { namespace internal {
struct ResolvedFont { struct ResolvedFont {
@ -125,6 +131,11 @@ bool LoadCustomFont(const QString &filePath) {
} }
#endif // !LIB_UI_USE_PACKAGED_FONTS #endif // !LIB_UI_USE_PACKAGED_FONTS
bool TryFont(const QString &attempt) {
const auto resolved = QFontInfo(QFont(attempt)).family();
return !resolved.trimmed().compare(attempt, Qt::CaseInsensitive);
}
[[nodiscard]] QString SystemMonospaceFont() { [[nodiscard]] QString SystemMonospaceFont() {
const auto type = QFontDatabase::FixedFont; const auto type = QFontDatabase::FixedFont;
return QFontDatabase::systemFont(type).family(); return QFontDatabase::systemFont(type).family();
@ -139,8 +150,7 @@ bool LoadCustomFont(const QString &filePath) {
u"Courier"_q, u"Courier"_q,
}; };
for (const auto &family : kTryFirst) { for (const auto &family : kTryFirst) {
const auto resolved = QFontInfo(QFont(family)).family(); if (TryFont(family)) {
if (resolved.trimmed().startsWith(family, Qt::CaseInsensitive)) {
return family; return family;
} }
} }
@ -149,6 +159,10 @@ bool LoadCustomFont(const QString &filePath) {
[[nodiscard]] QString MonospaceFont() { [[nodiscard]] QString MonospaceFont() {
static const auto family = [&]() -> QString { static const auto family = [&]() -> QString {
if (TryFont(CustomSettings.monospaceFont)) {
return CustomSettings.monospaceFont;
}
const auto manual = ManualMonospaceFont(); const auto manual = ManualMonospaceFont();
const auto system = SystemMonospaceFont(); const auto system = SystemMonospaceFont();
@ -330,7 +344,7 @@ struct Metrics {
font.setFeature("ss03", true); font.setFeature("ss03", true);
#endif // Qt >= 6.7.0 #endif // Qt >= 6.7.0
} }
font.setPixelSize(size); font.setPixelSize(size + ConvertScale(CustomSettings.fontSize));
const auto adjust = (overriden || system); const auto adjust = (overriden || system);
const auto metrics = ComputeMetrics(font, adjust); const auto metrics = ComputeMetrics(font, adjust);
@ -341,9 +355,10 @@ struct Metrics {
: QFont::Normal); : QFont::Normal);
if (font.bold()) { if (font.bold()) {
const auto style = QFontInfo(font).styleName(); const auto style = QFontInfo(font).styleName();
if (!style.isEmpty() && !style.startsWith( if (CustomSettings.semiboldIsBold
|| (!style.isEmpty() && !style.startsWith(
"Semibold", "Semibold",
Qt::CaseInsensitive)) { Qt::CaseInsensitive))) {
font.setBold(true); font.setBold(true);
} }
} }

View file

@ -16,8 +16,15 @@
namespace style { namespace style {
struct CustomFontSettings {
QString monospaceFont;
int fontSize = 0;
bool semiboldIsBold = false;
};
[[nodiscard]] const QString &SystemFontTag(); [[nodiscard]] const QString &SystemFontTag();
void SetCustomFont(const QString &font); void SetCustomFont(const QString &font);
void SetCustomFontSettings(const CustomFontSettings &settings);
enum class FontFlag : uchar { enum class FontFlag : uchar {
Bold = 0x01, Bold = 0x01,