refactor: Fonts (#5228)
This commit is contained in:
+56
-105
@@ -8,119 +8,73 @@
|
||||
#include <QDebug>
|
||||
#include <QtGlobal>
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
# define DEFAULT_FONT_FAMILY "Segoe UI"
|
||||
# define DEFAULT_FONT_SIZE 10
|
||||
#else
|
||||
# ifdef Q_OS_MACOS
|
||||
# define DEFAULT_FONT_FAMILY "Helvetica Neue"
|
||||
# define DEFAULT_FONT_SIZE 12
|
||||
# else
|
||||
# define DEFAULT_FONT_FAMILY "Arial"
|
||||
# define DEFAULT_FONT_SIZE 11
|
||||
# endif
|
||||
#endif
|
||||
|
||||
namespace chatterino {
|
||||
namespace {
|
||||
int getBoldness()
|
||||
{
|
||||
|
||||
using namespace chatterino;
|
||||
|
||||
int getBoldness()
|
||||
{
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||
// From qfont.cpp
|
||||
// https://github.com/qt/qtbase/blob/589c6d066f84833a7c3dda1638037f4b2e91b7aa/src/gui/text/qfont.cpp#L143-L169
|
||||
static constexpr std::array<std::array<int, 2>, 9> legacyToOpenTypeMap{{
|
||||
{0, QFont::Thin},
|
||||
{12, QFont::ExtraLight},
|
||||
{25, QFont::Light},
|
||||
{50, QFont::Normal},
|
||||
{57, QFont::Medium},
|
||||
{63, QFont::DemiBold},
|
||||
{75, QFont::Bold},
|
||||
{81, QFont::ExtraBold},
|
||||
{87, QFont::Black},
|
||||
}};
|
||||
// From qfont.cpp
|
||||
// https://github.com/qt/qtbase/blob/589c6d066f84833a7c3dda1638037f4b2e91b7aa/src/gui/text/qfont.cpp#L143-L169
|
||||
static constexpr std::array<std::array<int, 2>, 9> legacyToOpenTypeMap{{
|
||||
{0, QFont::Thin},
|
||||
{12, QFont::ExtraLight},
|
||||
{25, QFont::Light},
|
||||
{50, QFont::Normal},
|
||||
{57, QFont::Medium},
|
||||
{63, QFont::DemiBold},
|
||||
{75, QFont::Bold},
|
||||
{81, QFont::ExtraBold},
|
||||
{87, QFont::Black},
|
||||
}};
|
||||
|
||||
const int target = getSettings()->boldScale.getValue();
|
||||
const int target = getSettings()->boldScale.getValue();
|
||||
|
||||
int result = QFont::Medium;
|
||||
int closestDist = INT_MAX;
|
||||
int result = QFont::Medium;
|
||||
int closestDist = INT_MAX;
|
||||
|
||||
// Go through and find the closest mapped value
|
||||
for (const auto [weightOld, weightNew] : legacyToOpenTypeMap)
|
||||
// Go through and find the closest mapped value
|
||||
for (const auto [weightOld, weightNew] : legacyToOpenTypeMap)
|
||||
{
|
||||
const int dist = qAbs(weightOld - target);
|
||||
if (dist < closestDist)
|
||||
{
|
||||
const int dist = qAbs(weightOld - target);
|
||||
if (dist < closestDist)
|
||||
{
|
||||
result = weightNew;
|
||||
closestDist = dist;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Break early since following values will be further away
|
||||
break;
|
||||
}
|
||||
result = weightNew;
|
||||
closestDist = dist;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Break early since following values will be further away
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
return getSettings()->boldScale.getValue();
|
||||
#endif
|
||||
}
|
||||
|
||||
return result;
|
||||
#else
|
||||
return getSettings()->boldScale.getValue();
|
||||
#endif
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Fonts *Fonts::instance = nullptr;
|
||||
namespace chatterino {
|
||||
|
||||
Fonts::Fonts()
|
||||
: chatFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY)
|
||||
, chatFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE)
|
||||
Fonts::Fonts(Settings &settings)
|
||||
{
|
||||
Fonts::instance = this;
|
||||
|
||||
this->fontsByType_.resize(size_t(FontStyle::EndType));
|
||||
}
|
||||
|
||||
void Fonts::initialize(Settings &, const Paths &)
|
||||
{
|
||||
this->chatFontFamily.connect(
|
||||
[this]() {
|
||||
assertInGuiThread();
|
||||
this->fontChangedListener.setCB([this] {
|
||||
assertInGuiThread();
|
||||
|
||||
for (auto &map : this->fontsByType_)
|
||||
{
|
||||
map.clear();
|
||||
}
|
||||
this->fontChanged.invoke();
|
||||
},
|
||||
false);
|
||||
|
||||
this->chatFontSize.connect(
|
||||
[this]() {
|
||||
assertInGuiThread();
|
||||
|
||||
for (auto &map : this->fontsByType_)
|
||||
{
|
||||
map.clear();
|
||||
}
|
||||
this->fontChanged.invoke();
|
||||
},
|
||||
false);
|
||||
|
||||
#ifdef CHATTERINO
|
||||
getSettings()->boldScale.connect(
|
||||
[this]() {
|
||||
assertInGuiThread();
|
||||
|
||||
// REMOVED
|
||||
getIApp()->getWindows()->incGeneration();
|
||||
|
||||
for (auto &map : this->fontsByType_)
|
||||
{
|
||||
map.clear();
|
||||
}
|
||||
this->fontChanged.invoke();
|
||||
},
|
||||
false);
|
||||
#endif
|
||||
for (auto &map : this->fontsByType_)
|
||||
{
|
||||
map.clear();
|
||||
}
|
||||
this->fontChanged.invoke();
|
||||
});
|
||||
this->fontChangedListener.addSetting(settings.chatFontFamily);
|
||||
this->fontChangedListener.addSetting(settings.chatFontSize);
|
||||
this->fontChangedListener.addSetting(settings.boldScale);
|
||||
}
|
||||
|
||||
QFont Fonts::getFont(FontStyle type, float scale)
|
||||
@@ -159,6 +113,8 @@ Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale)
|
||||
|
||||
Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
|
||||
{
|
||||
auto *settings = getSettings();
|
||||
|
||||
// check if it's a chat (scale the setting)
|
||||
if (type >= FontStyle::ChatStart && type <= FontStyle::ChatEnd)
|
||||
{
|
||||
@@ -176,8 +132,8 @@ Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
|
||||
QFont::Weight(getBoldness())};
|
||||
auto data = sizeScale[type];
|
||||
return FontData(
|
||||
QFont(this->chatFontFamily.getValue(),
|
||||
int(this->chatFontSize.getValue() * data.scale * scale),
|
||||
QFont(settings->chatFontFamily.getValue(),
|
||||
int(settings->chatFontSize.getValue() * data.scale * scale),
|
||||
data.weight, data.italic));
|
||||
}
|
||||
|
||||
@@ -205,9 +161,4 @@ Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
|
||||
}
|
||||
}
|
||||
|
||||
Fonts *getFonts()
|
||||
{
|
||||
return Fonts::instance;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/ChatterinoSetting.hpp"
|
||||
#include "common/Singleton.hpp"
|
||||
#include "pajlada/settings/settinglistener.hpp"
|
||||
|
||||
#include <pajlada/signals/signal.hpp>
|
||||
#include <QFont>
|
||||
#include <QFontDatabase>
|
||||
#include <QFontMetrics>
|
||||
|
||||
#include <array>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
@@ -38,23 +36,17 @@ enum class FontStyle : uint8_t {
|
||||
ChatEnd = ChatVeryLarge,
|
||||
};
|
||||
|
||||
class Fonts final : public Singleton
|
||||
class Fonts final
|
||||
{
|
||||
public:
|
||||
Fonts();
|
||||
|
||||
void initialize(Settings &settings, const Paths &paths) override;
|
||||
explicit Fonts(Settings &settings);
|
||||
|
||||
// font data gets set in createFontData(...)
|
||||
|
||||
QFont getFont(FontStyle type, float scale);
|
||||
QFontMetrics getFontMetrics(FontStyle type, float scale);
|
||||
|
||||
QStringSetting chatFontFamily;
|
||||
IntSetting chatFontSize;
|
||||
|
||||
pajlada::Signals::NoArgSignal fontChanged;
|
||||
static Fonts *instance;
|
||||
|
||||
private:
|
||||
struct FontData {
|
||||
@@ -85,8 +77,8 @@ private:
|
||||
FontData createFontData(FontStyle type, float scale);
|
||||
|
||||
std::vector<std::unordered_map<float, FontData>> fontsByType_;
|
||||
|
||||
pajlada::SettingListener fontChangedListener;
|
||||
};
|
||||
|
||||
Fonts *getFonts();
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -25,6 +25,19 @@ using TimeoutButton = std::pair<QString, int>;
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
# define DEFAULT_FONT_FAMILY "Segoe UI"
|
||||
# define DEFAULT_FONT_SIZE 10
|
||||
#else
|
||||
# ifdef Q_OS_MACOS
|
||||
# define DEFAULT_FONT_FAMILY "Helvetica Neue"
|
||||
# define DEFAULT_FONT_SIZE 12
|
||||
# else
|
||||
# define DEFAULT_FONT_FAMILY "Arial"
|
||||
# define DEFAULT_FONT_SIZE 11
|
||||
# endif
|
||||
#endif
|
||||
|
||||
void _actuallyRegisterSetting(
|
||||
std::weak_ptr<pajlada::Settings::SettingData> setting);
|
||||
|
||||
@@ -134,6 +147,14 @@ public:
|
||||
|
||||
// BoolSetting collapseLongMessages =
|
||||
// {"/appearance/messages/collapseLongMessages", false};
|
||||
QStringSetting chatFontFamily{
|
||||
"/appearance/currentFontFamily",
|
||||
DEFAULT_FONT_FAMILY,
|
||||
};
|
||||
IntSetting chatFontSize{
|
||||
"/appearance/currentFontSize",
|
||||
DEFAULT_FONT_SIZE,
|
||||
};
|
||||
BoolSetting hideReplyContext = {"/appearance/hideReplyContext", false};
|
||||
BoolSetting showReplyButton = {"/appearance/showReplyButton", false};
|
||||
BoolSetting stripReplyMention = {"/appearance/stripReplyMention", true};
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include "providers/irc/IrcChannel2.hpp"
|
||||
#include "providers/irc/IrcServer.hpp"
|
||||
#include "providers/twitch/TwitchIrcServer.hpp"
|
||||
#include "singletons/Fonts.hpp"
|
||||
#include "singletons/Paths.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "singletons/Theme.hpp"
|
||||
|
||||
Reference in New Issue
Block a user