mirror-chatterino2/src/singletons/FontManager.cpp

136 lines
3.7 KiB
C++
Raw Normal View History

2017-12-31 00:50:07 +01:00
#include "singletons/fontmanager.hpp"
2017-04-12 17:46:44 +02:00
#include <QDebug>
#include <QtGlobal>
#include "application.hpp"
2018-05-23 04:22:17 +02:00
#include "util/assertinguithread.hpp"
#include "windowmanager.hpp"
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-12-31 22:58:35 +01:00
namespace singletons {
2017-04-12 17:46:44 +02:00
FontManager::FontManager()
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
{
qDebug() << "init FontManager";
this->chatFontFamily.connect([this](const std::string &, auto) {
2018-05-23 04:22:17 +02:00
util::assertInGuiThread();
if (getApp()->windows) {
getApp()->windows->incGeneration();
}
2018-05-23 04:22:17 +02:00
for (auto &map : this->fontsByType) {
map.clear();
}
this->fontChanged.invoke();
});
this->chatFontSize.connect([this](const int &, auto) {
2018-05-23 04:22:17 +02:00
util::assertInGuiThread();
if (getApp()->windows) {
getApp()->windows->incGeneration();
}
2018-05-23 04:22:17 +02:00
for (auto &map : this->fontsByType) {
map.clear();
}
this->fontChanged.invoke();
});
2018-05-23 04:22:17 +02:00
this->fontsByType.resize(size_t(EndType));
2017-04-12 17:46:44 +02:00
}
2018-05-23 04:22:17 +02:00
QFont FontManager::getFont(FontManager::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-05-23 04:22:17 +02:00
QFontMetrics FontManager::getFontMetrics(FontManager::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-05-23 04:22:17 +02:00
FontManager::FontData &FontManager::getOrCreateFontData(Type type, float scale)
{
2018-05-23 04:22:17 +02:00
util::assertInGuiThread();
assert(type >= 0 && type < EndType);
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-05-23 04:22:17 +02:00
FontManager::FontData FontManager::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}},
{ChatMediumBold, {1, false, QFont::Medium}},
{ChatMediumItalic, {1, true, QFont::Normal}},
{ChatLarge, {1.2f, false, QFont::Normal}},
{ChatVeryLarge, {1.4f, false, QFont::Normal}},
};
auto data = sizeScale[type];
return FontData(QFont(QString::fromStdString(this->chatFontFamily.getValue()),
int(this->chatFontSize.getValue() * data.scale * scale), data.weight,
2018-05-23 04:22:17 +02:00
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-06-11 15:04:54 +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);
}
}
2018-01-17 14:14:31 +01:00
} // namespace singletons
} // namespace chatterino