mirror-chatterino2/src/singletons/Fonts.cpp

144 lines
3.9 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"
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-01-17 14:14:31 +01:00
#define DEFAULT_FONT_FAMILY "Segoe UI"
#define DEFAULT_FONT_SIZE 10
#else
2018-01-17 14:14:31 +01: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
{
2018-07-07 11:41:01 +02:00
this->fontsByType_.resize(size_t(EndType));
}
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
}
2018-06-28 19:51:07 +02:00
QFont Fonts::getFont(Fonts::Type 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
}
2018-06-28 19:51:07 +02:00
QFontMetrics Fonts::getFontMetrics(Fonts::Type 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;
}
2018-06-28 19:51:07 +02:00
Fonts::FontData &Fonts::getOrCreateFontData(Type type, float scale)
{
2018-06-26 17:06:17 +02:00
assertInGuiThread();
2018-05-23 04:22:17 +02:00
assert(type >= 0 && type < EndType);
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
}
2018-06-28 19:51:07 +02:00
Fonts::FontData Fonts::createFontData(Type type, float scale)
{
2018-05-23 04:22:17 +02:00
// check if it's a chat (scale the setting)
if (type >= ChatStart && type <= ChatEnd) {
static std::unordered_map<Type, ChatFontData> sizeScale{
{ChatSmall, {0.6f, false, QFont::Normal}},
{ChatMediumSmall, {0.8f, false, QFont::Normal}},
{ChatMedium, {1, false, QFont::Normal}},
2018-08-06 21:17:03 +02:00
{ChatMediumBold,
{1, false,
QFont::Weight(getApp()->settings->boldScale.getValue())}},
2018-05-23 04:22:17 +02:00
{ChatMediumItalic, {1, true, QFont::Normal}},
{ChatLarge, {1.2f, false, QFont::Normal}},
{ChatVeryLarge, {1.4f, false, QFont::Normal}},
};
2018-08-06 21:17:03 +02:00
sizeScale[ChatMediumBold] = {
1, false, QFont::Weight(getApp()->settings->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
2018-05-23 04:22:17 +02:00
static std::unordered_map<Type, UiFontData> defaultSize{
{Tiny, {8, "Monospace", false, QFont::Normal}},
2018-08-06 21:17:03 +02:00
{UiMedium,
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
{UiTabs,
{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