Skip to content

Commit

Permalink
refactor: Fonts (Chatterino#5228)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Mar 10, 2024
1 parent e56f713 commit e750833
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 145 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
- Dev: Move `clang-tidy` checker to its own CI job. (#4996)
- Dev: Refactored the Image Uploader feature. (#4971)
- Dev: Refactored the SplitOverlay code. (#5082)
- Dev: Refactored the Fonts code, making it less of a singleton. (#5228)
- Dev: Refactored the TwitchBadges structure, making it less of a singleton. (#5096, #5144)
- Dev: Refactored emotes out of TwitchIrcServer. (#5120, #5146)
- Dev: Refactored the ChatterinoBadges structure, making it less of a singleton. (#5103)
Expand Down
5 changes: 4 additions & 1 deletion mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "singletons/Paths.hpp"
#include "singletons/Updates.hpp"

#include <QTemporaryDir>

namespace chatterino::mock {

class EmptyApplication : public IApplication
Expand Down Expand Up @@ -235,7 +237,8 @@ class EmptyApplication : public IApplication
return nullptr;
}

private:
protected:
QTemporaryDir settingsDir;
Paths paths_;
Args args_;
Updates updates_;
Expand Down
6 changes: 4 additions & 2 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Application::Application(Settings &_settings, const Paths &paths,
: paths_(paths)
, args_(_args)
, themes(&this->emplace<Theme>())
, fonts(&this->emplace<Fonts>())
, fonts(new Fonts(_settings))
, emotes(&this->emplace<Emotes>())
, accounts(&this->emplace<AccountController>())
, hotkeys(&this->emplace<HotkeyController>())
Expand Down Expand Up @@ -170,6 +170,7 @@ void Application::fakeDtor()
this->bttvEmotes.reset();
this->ffzEmotes.reset();
this->seventvEmotes.reset();
this->fonts.reset();
}

void Application::initialize(Settings &settings, const Paths &paths)
Expand Down Expand Up @@ -335,8 +336,9 @@ Theme *Application::getThemes()
Fonts *Application::getFonts()
{
assertInGuiThread();
assert(this->fonts);

return this->fonts;
return this->fonts.get();
}

IEmotes *Application::getEmotes()
Expand Down
2 changes: 1 addition & 1 deletion src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Application : public IApplication

private:
Theme *const themes{};
Fonts *const fonts{};
std::unique_ptr<Fonts> fonts{};
Emotes *const emotes{};
AccountController *const accounts{};
HotkeyController *const hotkeys{};
Expand Down
165 changes: 58 additions & 107 deletions src/singletons/Fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}};

const int target = getSettings()->boldScale.getValue();

int result = QFont::Medium;
int closestDist = INT_MAX;

// Go through and find the closest mapped value
for (const auto [weightOld, weightNew] : legacyToOpenTypeMap)
// 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();

int result = QFont::Medium;
int closestDist = INT_MAX;

// Go through and find the closest mapped value
for (const auto [weightOld, weightNew] : legacyToOpenTypeMap)
{
const int dist = qAbs(weightOld - target);
if (dist < closestDist)
{
result = weightNew;
closestDist = dist;
}
else
{
const int dist = qAbs(weightOld - target);
if (dist < closestDist)
{
result = weightNew;
closestDist = dist;
}
else
{
// Break early since following values will be further away
break;
}
// Break early since following values will be further away
break;
}
}

return result;
return result;
#else
return getSettings()->boldScale.getValue();
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();

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
this->fontChangedListener.setCB([this] {
assertInGuiThread();

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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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));
}

Expand Down Expand Up @@ -205,9 +161,4 @@ Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
}
}

Fonts *getFonts()
{
return Fonts::instance;
}

} // namespace chatterino
20 changes: 6 additions & 14 deletions src/singletons/Fonts.hpp
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -85,8 +77,8 @@ class Fonts final : public Singleton
FontData createFontData(FontStyle type, float scale);

std::vector<std::unordered_map<float, FontData>> fontsByType_;
};

Fonts *getFonts();
pajlada::SettingListener fontChangedListener;
};

} // namespace chatterino
21 changes: 21 additions & 0 deletions src/singletons/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -134,6 +147,14 @@ class Settings

// 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};
Expand Down
1 change: 0 additions & 1 deletion src/singletons/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,8 @@ void BaseWindow::scaleChangedEvent(float scale)
this->calcButtonsSizes();
#endif

this->setFont(getFonts()->getFont(FontStyle::UiTabs, this->qtFontScale()));
this->setFont(
getIApp()->getFonts()->getFont(FontStyle::UiTabs, this->qtFontScale()));
}

void BaseWindow::paintEvent(QPaintEvent *)
Expand Down
Loading

0 comments on commit e750833

Please sign in to comment.