2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Fonts.hpp"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-17 11:37:13 +02:00
|
|
|
#include <QDebug>
|
2018-01-07 20:54:44 +01:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
|
|
|
#include "WindowManager.hpp"
|
2018-06-26 17:20:03 +02:00
|
|
|
#include "debug/AssertInGuiThread.hpp"
|
2018-05-23 04:22:17 +02:00
|
|
|
|
2018-01-07 20:54:44 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
2018-01-17 14:14:31 +01:00
|
|
|
#define DEFAULT_FONT_FAMILY "Segoe UI"
|
|
|
|
#define DEFAULT_FONT_SIZE 10
|
2018-01-07 20:54:44 +01:00
|
|
|
#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
|
2018-01-07 20:54:44 +01:00
|
|
|
#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-04-26 18:10:26 +02:00
|
|
|
|
2018-07-07 11:41:01 +02:00
|
|
|
void Fonts::initialize(Application &app)
|
|
|
|
{
|
|
|
|
this->chatFontFamily.connect([this, &app](const std::string &, auto) {
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-23 04:22:17 +02:00
|
|
|
|
2018-07-07 11:41:01 +02:00
|
|
|
if (app.windows) {
|
|
|
|
app.windows->incGeneration();
|
2018-06-04 16:10:54 +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();
|
|
|
|
}
|
2017-10-27 21:02:58 +02:00
|
|
|
this->fontChanged.invoke();
|
2017-06-17 11:37:13 +02:00
|
|
|
});
|
2018-04-28 15:20:18 +02:00
|
|
|
|
2018-07-07 11:41:01 +02:00
|
|
|
this->chatFontSize.connect([this, &app](const int &, auto) {
|
2018-06-26 17:06:17 +02:00
|
|
|
assertInGuiThread();
|
2018-05-23 04:22:17 +02:00
|
|
|
|
2018-07-07 11:41:01 +02:00
|
|
|
if (app.windows) {
|
|
|
|
app.windows->incGeneration();
|
2018-06-04 16:10:54 +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();
|
|
|
|
}
|
2017-10-27 21:02:58 +02:00
|
|
|
this->fontChanged.invoke();
|
2017-06-17 11:37:13 +02:00
|
|
|
});
|
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;
|
2017-06-17 11:37:13 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 19:51:07 +02:00
|
|
|
Fonts::FontData &Fonts::getOrCreateFontData(Type type, float scale)
|
2017-06-17 11:37:13 +02:00
|
|
|
{
|
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
|
|
|
}
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2018-06-28 19:51:07 +02:00
|
|
|
Fonts::FontData Fonts::createFontData(Type type, float scale)
|
2017-12-23 21:18:13 +01:00
|
|
|
{
|
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()),
|
2018-06-04 16:10:54 +02:00
|
|
|
int(this->chatFontSize.getValue() * data.scale * scale), data.weight,
|
2018-05-23 04:22:17 +02:00
|
|
|
data.italic));
|
2017-12-23 21:18:13 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 04:22:17 +02:00
|
|
|
// normal Ui font (use pt size)
|
|
|
|
{
|
2018-05-24 22:58:07 +02:00
|
|
|
#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}},
|
2018-05-24 22:58:07 +02:00
|
|
|
{UiTabs, {int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
|
2018-05-23 04:22:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
UiFontData &data = defaultSize[type];
|
2018-06-04 16:10:54 +02:00
|
|
|
QFont font(data.name, int(data.size * scale), data.weight, data.italic);
|
2018-05-23 04:22:17 +02:00
|
|
|
return FontData(font);
|
|
|
|
}
|
2017-12-23 21:18:13 +01:00
|
|
|
}
|
2018-04-03 02:55:32 +02:00
|
|
|
|
2017-06-07 10:09:24 +02:00
|
|
|
} // namespace chatterino
|