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-08-11 22:23:06 +02:00
|
|
|
#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
|
|
|
|
2018-01-07 20:54:44 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
2018-08-15 22:46:20 +02:00
|
|
|
# define DEFAULT_FONT_FAMILY "Segoe UI"
|
|
|
|
# define DEFAULT_FONT_SIZE 10
|
2018-01-07 20:54:44 +01:00
|
|
|
#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
|
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-08-11 22:23:06 +02:00
|
|
|
this->fontsByType_.resize(size_t(FontStyle::EndType));
|
2018-07-07 11:41:01 +02:00
|
|
|
}
|
2018-04-26 18:10:26 +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();
|
|
|
|
}
|
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-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();
|
|
|
|
}
|
2017-10-27 21:02:58 +02:00
|
|
|
this->fontChanged.invoke();
|
2017-06-17 11:37:13 +02:00
|
|
|
});
|
2018-08-06 16:41:27 +02:00
|
|
|
|
2018-08-06 18:41:30 +02:00
|
|
|
getSettings()->boldScale.connect([this](const int &, auto) {
|
2018-08-06 16:41:27 +02:00
|
|
|
assertInGuiThread();
|
|
|
|
|
2018-08-06 18:41:30 +02:00
|
|
|
getApp()->windows->incGeneration();
|
2018-08-06 16:41:27 +02:00
|
|
|
|
|
|
|
for (auto &map : this->fontsByType_) {
|
|
|
|
map.clear();
|
|
|
|
}
|
|
|
|
this->fontChanged.invoke();
|
|
|
|
});
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 22:23:06 +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
|
|
|
}
|
|
|
|
|
2018-08-11 22:23:06 +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;
|
2017-06-17 11:37:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 22:23:06 +02:00
|
|
|
Fonts::FontData &Fonts::getOrCreateFontData(FontStyle 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
|
|
|
|
2018-08-11 22:23:06 +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
|
|
|
}
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2018-08-11 22:23:06 +02:00
|
|
|
Fonts::FontData Fonts::createFontData(FontStyle 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)
|
2018-08-11 22:23:06 +02:00
|
|
|
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())}},
|
2018-08-11 22:23:06 +02:00
|
|
|
{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
|
|
|
};
|
2018-08-11 22:23:06 +02:00
|
|
|
sizeScale[FontStyle::ChatMediumBold] = {
|
2018-08-12 12:56:28 +02:00
|
|
|
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));
|
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-08-11 22:23:06 +02:00
|
|
|
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}},
|
2018-08-11 22:23:06 +02:00
|
|
|
{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];
|
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
|