-
Notifications
You must be signed in to change notification settings - Fork 33
/
FontManager.cpp
122 lines (107 loc) · 3.69 KB
/
FontManager.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
/*
* OldSchoolHack GUI
*
* by KN4CK3R https://www.oldschoolhack.me/
*
* See license in OSHGui.hpp
*/
#include "FontManager.hpp"
#include "../Misc/Exceptions.hpp"
#include "../Misc/HashTuple.hpp"
#include "FreeTypeFont.hpp"
#include <windows.h>
#include <algorithm>
#include <vector>
#include <sstream>
namespace OSHGui
{
namespace Drawing
{
std::unordered_map<std::tuple<Misc::AnsiString, float, bool>, std::weak_ptr<Drawing::Font>> FontManager::loadedFonts;
//---------------------------------------------------------------------------
FontPtr FontManager::LoadFont(Misc::AnsiString name, float pointSize, bool antiAliased)
{
if (name.empty())
{
throw Misc::ArgumentException();
}
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
std::vector<std::string> parts;
std::stringstream ss(name);
for (std::string s; ss >> s; parts.push_back(s));
FontPtr font = nullptr;
char keyNameBuffer[MAX_PATH];
char keyValueBuffer[MAX_PATH];
HKEY fontKey;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", 0, KEY_READ, &fontKey) == ERROR_SUCCESS)
{
int i = 0;
int lastError = ERROR_SUCCESS;
do
{
DWORD keyNameLength = sizeof(keyNameBuffer);
DWORD keyValueLength = sizeof(keyValueBuffer);
lastError = RegEnumValueA(fontKey, i, keyNameBuffer, &keyNameLength, nullptr, nullptr, reinterpret_cast<uint8_t*>(keyValueBuffer), &keyValueLength);
if (lastError == ERROR_SUCCESS)
{
std::string keyName(keyNameBuffer);
if (keyName.find(" (TrueType)") != std::string::npos || keyName.find(" (OpenType)") != std::string::npos)
{
keyName.erase(keyName.length() - 11);
}
if (keyName.length() == name.length())
{
std::transform(keyName.begin(), keyName.end(), keyName.begin(), ::tolower);
auto foundAll = true;
for (auto &part : parts)
{
if (keyName.find(part) == std::string::npos)
{
foundAll = false;
break;
}
}
if (foundAll)
{
char path[MAX_PATH] = { };
ExpandEnvironmentStringsA((std::string("%windir%\\fonts\\") + keyValueBuffer).c_str(), path, sizeof(path));
RegCloseKey(fontKey);
return LoadFontFromFile(path, pointSize, antiAliased);
}
}
}
++i;
} while (lastError != ERROR_NO_MORE_ITEMS);
RegCloseKey(fontKey);
}
throw Misc::ArgumentException();
}
//---------------------------------------------------------------------------
FontPtr FontManager::LoadFontFromFile(const Misc::AnsiString &filename, float pointSize, bool antiAliased)
{
auto cacheEntry = std::make_tuple(filename, pointSize, antiAliased);
const auto it = loadedFonts.find(cacheEntry);
if (it == std::end(loadedFonts) || it->second.expired())
{
auto font = std::make_shared<FreeTypeFont>(filename, pointSize, antiAliased);
loadedFonts[cacheEntry] = font;
return font;
}
return it->second.lock();
}
//---------------------------------------------------------------------------
FontPtr FontManager::LoadFontFromMemory(const Misc::RawDataContainer &data, float pointSize, bool antiAliased)
{
return std::make_shared<FreeTypeFont>(data, pointSize, antiAliased);
}
//---------------------------------------------------------------------------
void FontManager::DisplaySizeChanged(const SizeF &size)
{
for (auto &font : loadedFonts)
{
font.second.lock()->DisplaySizeChanged(size);
}
}
//---------------------------------------------------------------------------
}
}