2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/fontmanager.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>
|
|
|
|
|
|
|
|
#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-12-31 22:58:35 +01:00
|
|
|
namespace singletons {
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
FontManager::FontManager()
|
2018-01-07 20:54:44 +01:00
|
|
|
: currentFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY)
|
|
|
|
, currentFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE)
|
2017-12-23 21:18:13 +01:00
|
|
|
// , currentFont(this->currentFontFamily.getValue().c_str(), currentFontSize.getValue())
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-17 03:37:46 +01:00
|
|
|
this->currentFontFamily.connect([this](const std::string &newValue, auto) {
|
2017-12-17 03:26:23 +01:00
|
|
|
this->incGeneration();
|
2017-12-23 21:18:13 +01:00
|
|
|
// this->currentFont.setFamily(newValue.c_str());
|
2018-01-17 14:14:31 +01:00
|
|
|
this->currentFontByScale.clear();
|
2017-10-27 21:02:58 +02:00
|
|
|
this->fontChanged.invoke();
|
2017-06-17 11:37:13 +02:00
|
|
|
});
|
2017-12-17 03:37:46 +01:00
|
|
|
this->currentFontSize.connect([this](const int &newValue, auto) {
|
2017-12-17 03:26:23 +01:00
|
|
|
this->incGeneration();
|
2017-12-23 21:18:13 +01:00
|
|
|
// this->currentFont.setSize(newValue);
|
2018-01-17 14:14:31 +01:00
|
|
|
this->currentFontByScale.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
|
|
|
}
|
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
FontManager &FontManager::getInstance()
|
|
|
|
{
|
|
|
|
static FontManager instance;
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:48:17 +01:00
|
|
|
QFont &FontManager::getFont(FontManager::Type type, float scale)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-23 21:18:13 +01:00
|
|
|
// return this->currentFont.getFont(type);
|
2018-01-17 14:14:31 +01:00
|
|
|
return this->getCurrentFont(scale).getFont(type);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 14:48:17 +01:00
|
|
|
QFontMetrics &FontManager::getFontMetrics(FontManager::Type type, float scale)
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-12-23 21:18:13 +01:00
|
|
|
// return this->currentFont.getFontMetrics(type);
|
2018-01-17 14:14:31 +01:00
|
|
|
return this->getCurrentFont(scale).getFontMetrics(type);
|
2017-06-17 11:37:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 14:48:17 +01:00
|
|
|
FontManager::FontData &FontManager::Font::getFontData(FontManager::Type type)
|
2017-06-17 11:37:13 +02:00
|
|
|
{
|
|
|
|
switch (type) {
|
2018-01-17 14:14:31 +01:00
|
|
|
case Tiny:
|
|
|
|
return this->tiny;
|
2017-06-17 11:37:13 +02:00
|
|
|
case Small:
|
|
|
|
return this->small;
|
2017-12-23 22:17:38 +01:00
|
|
|
case MediumSmall:
|
|
|
|
return this->mediumSmall;
|
2017-06-17 11:37:13 +02:00
|
|
|
case Medium:
|
|
|
|
return this->medium;
|
|
|
|
case MediumBold:
|
|
|
|
return this->mediumBold;
|
|
|
|
case MediumItalic:
|
|
|
|
return this->mediumItalic;
|
|
|
|
case Large:
|
|
|
|
return this->large;
|
|
|
|
case VeryLarge:
|
|
|
|
return this->veryLarge;
|
|
|
|
default:
|
|
|
|
qDebug() << "Unknown font type:" << type << ", defaulting to medium";
|
|
|
|
return this->medium;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont &FontManager::Font::getFont(Type type)
|
|
|
|
{
|
|
|
|
return this->getFontData(type).font;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFontMetrics &FontManager::Font::getFontMetrics(Type type)
|
|
|
|
{
|
|
|
|
return this->getFontData(type).metrics;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
FontManager::Font &FontManager::getCurrentFont(float scale)
|
2017-12-23 21:18:13 +01:00
|
|
|
{
|
2018-01-17 14:14:31 +01:00
|
|
|
for (auto it = this->currentFontByScale.begin(); it != this->currentFontByScale.end(); it++) {
|
|
|
|
if (it->first == scale) {
|
2017-12-23 21:18:13 +01:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 14:14:31 +01:00
|
|
|
this->currentFontByScale.push_back(
|
|
|
|
std::make_pair(scale, Font(this->currentFontFamily.getValue().c_str(),
|
|
|
|
this->currentFontSize.getValue() * scale)));
|
2017-12-23 21:18:13 +01:00
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
return this->currentFontByScale.back().second;
|
2017-12-23 21:18:13 +01:00
|
|
|
}
|
2018-04-03 02:55:32 +02:00
|
|
|
|
2018-01-17 14:14:31 +01:00
|
|
|
} // namespace singletons
|
2017-06-07 10:09:24 +02:00
|
|
|
} // namespace chatterino
|