yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
font_loader.cc
Go to the documentation of this file.
2
3#include <string>
4#include <unordered_set>
5#include <vector>
6
7#include "absl/strings/str_format.h"
8#include "app/gui/icons.h"
9#include "imgui/imgui.h"
10
11#ifdef _WIN32
12#include <Windows.h>
13
14int CALLBACK EnumFontFamExProc(const LOGFONT* lpelfe, const TEXTMETRIC* lpntme,
15 DWORD FontType, LPARAM lParam) {
16 // Step 3: Load the font into ImGui
17 ImGuiIO& io = ImGui::GetIO();
18 io.Fonts->AddFontFromFileTTF(lpelfe->lfFaceName, 16.0f);
19
20 return 1;
21}
22
23void LoadSystemFonts() {
24 HKEY hKey;
25 std::vector<std::string> fontPaths;
26
27 // Open the registry key where fonts are listed
28 if (RegOpenKeyEx(
29 HKEY_LOCAL_MACHINE,
30 TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"), 0,
31 KEY_READ, &hKey) == ERROR_SUCCESS) {
32 DWORD valueCount;
33 DWORD maxValueNameSize;
34 DWORD maxValueDataSize;
35
36 // Query the number of entries and the maximum size of the names and values
37 RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &valueCount,
38 &maxValueNameSize, &maxValueDataSize, NULL, NULL);
39
40 char* valueName = new char[maxValueNameSize + 1]; // +1 for null terminator
41 BYTE* valueData = new BYTE[maxValueDataSize + 1]; // +1 for null terminator
42
43 // Enumerate all font entries
44 for (DWORD i = 0; i < valueCount; i++) {
45 DWORD valueNameSize = maxValueNameSize + 1; // +1 for null terminator
46 DWORD valueDataSize = maxValueDataSize + 1; // +1 for null terminator
47 DWORD valueType;
48
49 // Clear buffers
50 memset(valueName, 0, valueNameSize);
51 memset(valueData, 0, valueDataSize);
52
53 // Get the font name and file path
54 if (RegEnumValue(hKey, i, valueName, &valueNameSize, NULL, &valueType,
55 valueData, &valueDataSize) == ERROR_SUCCESS) {
56 if (valueType == REG_SZ) {
57 // Add the font file path to the vector
58 std::string fontPath(reinterpret_cast<char*>(valueData),
59 valueDataSize);
60
61 fontPaths.push_back(fontPath);
62 }
63 }
64 }
65
66 delete[] valueName;
67 delete[] valueData;
68
69 RegCloseKey(hKey);
70 }
71
72 ImGuiIO& io = ImGui::GetIO();
73
74 // List of common font face names
75 static const std::unordered_set<std::string> commonFontFaceNames = {
76 "arial",
77 "times",
78 "cour",
79 "verdana",
80 "tahoma",
81 "comic",
82 "Impact",
83 "ariblk",
84 "Trebuchet MS",
85 "Georgia",
86 "Palatino Linotype",
87 "Lucida Sans Unicode",
88 "Tahoma",
89 "Lucida Console"};
90
91 for (auto& fontPath : fontPaths) {
92 // Check if the font path has a "C:\" prefix
93 if (fontPath.substr(0, 2) != "C:") {
94 // Add "C:\Windows\Fonts\" prefix to the font path
95 fontPath = absl::StrFormat("C:\\Windows\\Fonts\\%s", fontPath.c_str());
96 }
97
98 // Check if the font file has a .ttf or .TTF extension
99 std::string extension = fontPath.substr(fontPath.find_last_of(".") + 1);
100 if (extension == "ttf" || extension == "TTF") {
101 // Get the font face name from the font path
102 std::string fontFaceName =
103 fontPath.substr(fontPath.find_last_of("\\/") + 1);
104 fontFaceName = fontFaceName.substr(0, fontFaceName.find_last_of("."));
105
106 // Check if the font face name is in the common font face names list
107 if (commonFontFaceNames.find(fontFaceName) != commonFontFaceNames.end()) {
108 io.Fonts->AddFontFromFileTTF(fontPath.c_str(), 16.0f);
109
110 // Merge icon set
111 // Icon configuration
112 static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0};
113 ImFontConfig icons_config;
114 static const float ICON_FONT_SIZE = 18.0f;
115 icons_config.MergeMode = true;
116 icons_config.GlyphOffset.y = 5.0f;
117 icons_config.GlyphMinAdvanceX = 13.0f;
118 icons_config.PixelSnapH = true;
119 io.Fonts->AddFontFromFileTTF(FONT_ICON_FILE_NAME_MD, ICON_FONT_SIZE,
120 &icons_config, icons_ranges);
121 }
122 }
123 }
124}
125
126#elif defined(__linux__)
127
128void LoadSystemFonts() {
129 // Load Linux System Fonts into ImGui
130 // ...
131}
132
133#endif
void LoadSystemFonts()
#define ICON_MIN_MD
Definition icons.h:8
#define FONT_ICON_FILE_NAME_MD
Definition icons.h:6