2017-06-07 10:09:24 +02:00
|
|
|
#pragma once
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
2017-06-17 11:37:13 +02:00
|
|
|
#include <pajlada/settings/setting.hpp>
|
2017-10-27 21:02:58 +02:00
|
|
|
#include <pajlada/signals/signal.hpp>
|
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
|
|
|
|
|
|
|
class FontManager
|
|
|
|
{
|
2017-10-27 21:02:58 +02:00
|
|
|
FontManager(const FontManager &) = delete;
|
|
|
|
FontManager(FontManager &&) = delete;
|
2017-12-31 00:50:07 +01:00
|
|
|
FontManager();
|
2017-10-27 21:02:58 +02:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
public:
|
2017-06-17 11:37:13 +02:00
|
|
|
enum Type : uint8_t {
|
|
|
|
Small,
|
2017-12-23 22:17:38 +01:00
|
|
|
MediumSmall,
|
2017-06-17 11:37:13 +02:00
|
|
|
Medium,
|
|
|
|
MediumBold,
|
|
|
|
MediumItalic,
|
|
|
|
Large,
|
|
|
|
VeryLarge,
|
|
|
|
};
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-17 11:37:13 +02:00
|
|
|
// FontManager is initialized only once, on first use
|
2017-12-31 00:50:07 +01:00
|
|
|
static FontManager &getInstance();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
QFont &getFont(Type type, float dpi);
|
|
|
|
QFontMetrics &getFontMetrics(Type type, float dpi);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-17 11:37:13 +02:00
|
|
|
int getGeneration() const
|
2017-04-12 17:46:44 +02:00
|
|
|
{
|
2017-06-17 11:37:13 +02:00
|
|
|
return this->generation;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void incGeneration()
|
|
|
|
{
|
2017-06-17 11:37:13 +02:00
|
|
|
this->generation++;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 11:37:13 +02:00
|
|
|
pajlada::Settings::Setting<std::string> currentFontFamily;
|
|
|
|
pajlada::Settings::Setting<int> currentFontSize;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-27 21:02:58 +02:00
|
|
|
pajlada::Signals::NoArgSignal fontChanged;
|
|
|
|
|
2017-06-17 11:37:13 +02:00
|
|
|
private:
|
|
|
|
struct FontData {
|
|
|
|
FontData(QFont &&_font)
|
|
|
|
: font(_font)
|
|
|
|
, metrics(this->font)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont font;
|
|
|
|
QFontMetrics metrics;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Font {
|
|
|
|
Font() = delete;
|
|
|
|
|
|
|
|
explicit Font(const char *fontFamilyName, int mediumSize)
|
|
|
|
: small(QFont(fontFamilyName, mediumSize - 4))
|
2017-12-31 00:50:07 +01:00
|
|
|
, mediumSmall(QFont(fontFamilyName, mediumSize - 2))
|
2017-06-17 11:37:13 +02:00
|
|
|
, medium(QFont(fontFamilyName, mediumSize))
|
2017-12-23 23:48:10 +01:00
|
|
|
, mediumBold(QFont(fontFamilyName, mediumSize, QFont::DemiBold))
|
2017-06-17 11:37:13 +02:00
|
|
|
, mediumItalic(QFont(fontFamilyName, mediumSize, -1, true))
|
|
|
|
, large(QFont(fontFamilyName, mediumSize))
|
|
|
|
, veryLarge(QFont(fontFamilyName, mediumSize))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFamily(const char *newFamily)
|
|
|
|
{
|
|
|
|
this->small.font.setFamily(newFamily);
|
2017-12-23 22:17:38 +01:00
|
|
|
this->mediumSmall.font.setFamily(newFamily);
|
2017-06-17 11:37:13 +02:00
|
|
|
this->medium.font.setFamily(newFamily);
|
|
|
|
this->mediumBold.font.setFamily(newFamily);
|
|
|
|
this->mediumItalic.font.setFamily(newFamily);
|
|
|
|
this->large.font.setFamily(newFamily);
|
|
|
|
this->veryLarge.font.setFamily(newFamily);
|
|
|
|
|
|
|
|
this->updateMetrics();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSize(int newMediumSize)
|
|
|
|
{
|
|
|
|
this->small.font.setPointSize(newMediumSize - 4);
|
2017-12-23 22:17:38 +01:00
|
|
|
this->mediumSmall.font.setPointSize(newMediumSize - 2);
|
2017-06-17 11:37:13 +02:00
|
|
|
this->medium.font.setPointSize(newMediumSize);
|
|
|
|
this->mediumBold.font.setPointSize(newMediumSize);
|
|
|
|
this->mediumItalic.font.setPointSize(newMediumSize);
|
|
|
|
this->large.font.setPointSize(newMediumSize + 2);
|
|
|
|
this->veryLarge.font.setPointSize(newMediumSize + 4);
|
|
|
|
|
|
|
|
this->updateMetrics();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateMetrics()
|
|
|
|
{
|
|
|
|
this->small.metrics = QFontMetrics(this->small.font);
|
2017-12-23 22:17:38 +01:00
|
|
|
this->mediumSmall.metrics = QFontMetrics(this->mediumSmall.font);
|
2017-06-17 11:37:13 +02:00
|
|
|
this->medium.metrics = QFontMetrics(this->medium.font);
|
|
|
|
this->mediumBold.metrics = QFontMetrics(this->mediumBold.font);
|
|
|
|
this->mediumItalic.metrics = QFontMetrics(this->mediumItalic.font);
|
|
|
|
this->large.metrics = QFontMetrics(this->large.font);
|
|
|
|
this->veryLarge.metrics = QFontMetrics(this->veryLarge.font);
|
|
|
|
}
|
|
|
|
|
|
|
|
FontData &getFontData(Type type);
|
|
|
|
|
|
|
|
QFont &getFont(Type type);
|
|
|
|
QFontMetrics &getFontMetrics(Type type);
|
|
|
|
|
|
|
|
FontData small;
|
2017-12-23 22:17:38 +01:00
|
|
|
FontData mediumSmall;
|
2017-06-17 11:37:13 +02:00
|
|
|
FontData medium;
|
|
|
|
FontData mediumBold;
|
|
|
|
FontData mediumItalic;
|
|
|
|
FontData large;
|
|
|
|
FontData veryLarge;
|
|
|
|
};
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
Font &getCurrentFont(float dpi);
|
|
|
|
|
2017-06-17 11:37:13 +02:00
|
|
|
// Future plans:
|
|
|
|
// Could have multiple fonts in here, such as "Menu font", "Application font", "Chat font"
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
std::list<std::pair<float, Font>> currentFontByDpi;
|
2017-06-17 11:37:13 +02:00
|
|
|
|
|
|
|
int generation = 0;
|
2017-04-12 17:46:44 +02:00
|
|
|
};
|
2018-01-11 20:16:25 +01:00
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
typedef singletons::FontManager::Type FontStyle;
|
2017-06-07 10:09:24 +02:00
|
|
|
} // namespace chatterino
|