forked from Chatterino/chatterino2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fonts.cpp
161 lines (133 loc) · 4.58 KB
/
Fonts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "singletons/Fonts.hpp"
#include "Application.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "singletons/Settings.hpp"
#include "singletons/WindowManager.hpp"
#include <QDebug>
#include <QtGlobal>
namespace {
using namespace chatterino;
int getBoldness()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
// From qfont.cpp
// https://github.com/qt/qtbase/blob/589c6d066f84833a7c3dda1638037f4b2e91b7aa/src/gui/text/qfont.cpp#L143-L169
static constexpr std::array<std::array<int, 2>, 9> legacyToOpenTypeMap{{
{0, QFont::Thin},
{12, QFont::ExtraLight},
{25, QFont::Light},
{50, QFont::Normal},
{57, QFont::Medium},
{63, QFont::DemiBold},
{75, QFont::Bold},
{81, QFont::ExtraBold},
{87, QFont::Black},
}};
const int target = getSettings()->boldScale.getValue();
int result = QFont::Medium;
int closestDist = INT_MAX;
// Go through and find the closest mapped value
for (const auto [weightOld, weightNew] : legacyToOpenTypeMap)
{
const int dist = qAbs(weightOld - target);
if (dist < closestDist)
{
result = weightNew;
closestDist = dist;
}
else
{
// Break early since following values will be further away
break;
}
}
return result;
#else
return getSettings()->boldScale.getValue();
#endif
}
} // namespace
namespace chatterino {
Fonts::Fonts(Settings &settings)
{
this->fontsByType_.resize(size_t(FontStyle::EndType));
this->fontChangedListener.setCB([this] {
assertInGuiThread();
for (auto &map : this->fontsByType_)
{
map.clear();
}
this->fontChanged.invoke();
});
this->fontChangedListener.addSetting(settings.chatFontFamily);
this->fontChangedListener.addSetting(settings.chatFontSize);
this->fontChangedListener.addSetting(settings.boldScale);
}
QFont Fonts::getFont(FontStyle type, float scale, QWidget *widget)
{
return this->getOrCreateFontData(type, scale, widget).font;
}
QFontMetrics Fonts::getFontMetrics(FontStyle type, float scale, QWidget *widget)
{
return this->getOrCreateFontData(type, scale, widget).metrics;
}
Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale,
QWidget *widget)
{
assertInGuiThread();
assert(type < FontStyle::EndType);
// use DPI scaling if widget was passed
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
if (widget != nullptr)
{
scale = scale * 96.f /
std::max<float>(
0.01f, widget->logicalDpiX() * widget->devicePixelRatioF());
}
#endif
auto &map = this->fontsByType_[size_t(type)];
// 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;
}
Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
{
// check if it's a chat
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,
{1, false, QFont::Weight(getBoldness())}},
{FontStyle::ChatMediumItalic, {1, true, QFont::Normal}},
{FontStyle::ChatLarge, {1.2f, false, QFont::Normal}},
{FontStyle::ChatVeryLarge, {1.4f, false, QFont::Normal}},
};
ChatFontData data = sizeScale[type];
return FontData(QFont(
getSettings()->chatFontFamily.getValue(),
int(getSettings()->chatFontSize.getValue() * data.scale * scale),
data.weight, data.italic));
}
// normal UI font
static std::unordered_map<FontStyle, UiFontData> defaultSize{
{FontStyle::Tiny, {8, "Monospace", false, QFont::Normal}},
{FontStyle::UiMedium, {9, DEFAULT_FONT_FAMILY, false, QFont::Normal}},
{FontStyle::UiMediumBold, {9, DEFAULT_FONT_FAMILY, false, QFont::Bold}},
{FontStyle::UiTabs, {9, DEFAULT_FONT_FAMILY, false, QFont::Normal}},
};
UiFontData &data = defaultSize[type];
return FontData(
QFont(data.name, int(data.size * scale), data.weight, data.italic));
}
} // namespace chatterino