mirror-chatterino2/src/singletons/Fonts.cpp

144 lines
4.1 KiB
C++
Raw Normal View History

2018-06-28 19:46:45 +02:00
#include "singletons/Fonts.hpp"
2017-04-12 17:46:44 +02:00
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2018-06-26 17:20:03 +02:00
#include "debug/AssertInGuiThread.hpp"
#include "singletons/Settings.hpp"
2018-07-15 14:03:41 +02:00
#include "singletons/WindowManager.hpp"
#include <QDebug>
#include <QtGlobal>
2018-05-23 04:22:17 +02:00
#ifdef Q_OS_WIN32
2018-08-15 22:46:20 +02:00
# define DEFAULT_FONT_FAMILY "Segoe UI"
# define DEFAULT_FONT_SIZE 10
#else
2018-08-15 22:46:20 +02:00
# 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
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-04-12 17:46:44 +02:00
2018-06-28 19:51:07 +02:00
Fonts::Fonts()
2018-05-23 04:22:17 +02:00
: chatFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY)
, chatFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE)
2017-04-12 17:46:44 +02:00
{
this->fontsByType_.resize(size_t(FontStyle::EndType));
2018-07-07 11:41:01 +02:00
}
2018-08-02 14:23:27 +02:00
void Fonts::initialize(Settings &, Paths &)
2018-07-07 11:41:01 +02:00
{
2018-08-02 14:23:27 +02:00
this->chatFontFamily.connect([this](const std::string &, auto) {
2018-06-26 17:06:17 +02:00
assertInGuiThread();
2018-05-23 04:22:17 +02:00
2018-07-06 19:23:47 +02:00
for (auto &map : this->fontsByType_) {
2018-05-23 04:22:17 +02:00
map.clear();
}
this->fontChanged.invoke();
});
2018-08-02 14:23:27 +02:00
this->chatFontSize.connect([this](const int &, auto) {
2018-06-26 17:06:17 +02:00
assertInGuiThread();
2018-05-23 04:22:17 +02:00
2018-07-06 19:23:47 +02:00
for (auto &map : this->fontsByType_) {
2018-05-23 04:22:17 +02:00
map.clear();
}
this->fontChanged.invoke();
});
2018-08-06 18:41:30 +02:00
getSettings()->boldScale.connect([this](const int &, auto) {
assertInGuiThread();
2018-08-06 18:41:30 +02:00
getApp()->windows->incGeneration();
for (auto &map : this->fontsByType_) {
map.clear();
}
this->fontChanged.invoke();
});
2017-04-12 17:46:44 +02:00
}
QFont Fonts::getFont(FontStyle type, float scale)
2017-04-12 17:46:44 +02:00
{
2018-05-23 04:22:17 +02:00
return this->getOrCreateFontData(type, scale).font;
2017-04-12 17:46:44 +02:00
}
QFontMetrics Fonts::getFontMetrics(FontStyle type, float scale)
2017-04-12 17:46:44 +02:00
{
2018-05-23 04:22:17 +02:00
return this->getOrCreateFontData(type, scale).metrics;
}
Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale)
{
2018-06-26 17:06:17 +02:00
assertInGuiThread();
2018-05-23 04:22:17 +02:00
assert(type < FontStyle::EndType);
2018-05-23 04:22:17 +02:00
2018-07-06 19:23:47 +02:00
auto &map = this->fontsByType_[size_t(type)];
2018-05-23 04:22:17 +02:00
// find element
auto it = map.find(scale);
if (it != map.end()) {
// return if found
return it->second;
}
// emplace new element
auto result = map.emplace(scale, this->createFontData(type, scale));
assert(result.second);
return result.first->second;
2017-04-12 17:46:44 +02:00
}
Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
{
2018-05-23 04:22:17 +02:00
// check if it's a chat (scale the setting)
if (type >= FontStyle::ChatStart && type <= FontStyle::ChatEnd) {
static std::unordered_map<FontStyle, ChatFontData> sizeScale{
{FontStyle::ChatSmall, {0.6f, false, QFont::Normal}},
{FontStyle::ChatMediumSmall, {0.8f, false, QFont::Normal}},
{FontStyle::ChatMedium, {1, false, QFont::Normal}},
{FontStyle::ChatMediumBold,
2018-08-15 22:46:20 +02:00
{1, false, QFont::Weight(getSettings()->boldScale.getValue())}},
{FontStyle::ChatMediumItalic, {1, true, QFont::Normal}},
{FontStyle::ChatLarge, {1.2f, false, QFont::Normal}},
{FontStyle::ChatVeryLarge, {1.4f, false, QFont::Normal}},
2018-05-23 04:22:17 +02:00
};
sizeScale[FontStyle::ChatMediumBold] = {
1, false, QFont::Weight(getSettings()->boldScale.getValue())};
2018-05-23 04:22:17 +02:00
auto data = sizeScale[type];
2018-08-06 21:17:03 +02:00
return FontData(
QFont(QString::fromStdString(this->chatFontFamily.getValue()),
int(this->chatFontSize.getValue() * data.scale * scale),
data.weight, data.italic));
}
2018-05-23 04:22:17 +02:00
// normal Ui font (use pt size)
{
#ifdef Q_OS_MAC
constexpr float multiplier = 0.8f;
#else
constexpr float multiplier = 1.f;
#endif
static std::unordered_map<FontStyle, UiFontData> defaultSize{
{FontStyle::Tiny, {8, "Monospace", false, QFont::Normal}},
{FontStyle::UiMedium,
2018-08-06 21:17:03 +02:00
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
{FontStyle::UiTabs,
2018-08-06 21:17:03 +02:00
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
2018-05-23 04:22:17 +02:00
};
UiFontData &data = defaultSize[type];
QFont font(data.name, int(data.size * scale), data.weight, data.italic);
2018-05-23 04:22:17 +02:00
return FontData(font);
}
}
} // namespace chatterino