4#include <unordered_set>
8#include "absl/status/status.h"
9#include "absl/strings/str_cat.h"
10#include "absl/strings/str_format.h"
13#include "imgui/imgui.h"
20 ImGuiIO &io = ImGui::GetIO();
22 static const char *KARLA_REGULAR =
"Karla-Regular.ttf";
23 static const char *ROBOTO_MEDIUM =
"Roboto-Medium.ttf";
24 static const char *COUSINE_REGULAR =
"Cousine-Regular.ttf";
25 static const char *DROID_SANS =
"DroidSans.ttf";
26 static const char *NOTO_SANS_JP =
"NotoSansJP.ttf";
27 static const char *IBM_PLEX_JP =
"IBMPlexSansJP-Bold.ttf";
28 static const float FONT_SIZE_DEFAULT = 16.0f;
29 static const float FONT_SIZE_DROID_SANS = 18.0f;
30 static const float ICON_FONT_SIZE = 18.0f;
33 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
34 ImFontConfig icons_config;
35 icons_config.MergeMode =
true;
36 icons_config.GlyphOffset.y = 5.0f;
37 icons_config.GlyphMinAdvanceX = 13.0f;
38 icons_config.PixelSnapH =
true;
41 ImFontConfig japanese_font_config;
42 japanese_font_config.MergeMode =
true;
43 icons_config.GlyphOffset.y = 5.0f;
44 icons_config.GlyphMinAdvanceX = 13.0f;
45 icons_config.PixelSnapH =
true;
48 std::vector<const char *> font_paths = {
49 KARLA_REGULAR, ROBOTO_MEDIUM, COUSINE_REGULAR, IBM_PLEX_JP, DROID_SANS};
52 for (
const auto &font_path : font_paths) {
54 (font_path == DROID_SANS) ? FONT_SIZE_DROID_SANS : FONT_SIZE_DEFAULT;
56 std::string actual_font_path;
60 actual_font_path = kBundlePath + font_path;
63 "Contents/Resources/font/", font_path);
66 actual_font_path = std::filesystem::absolute(font_path).string();
69 if (!io.Fonts->AddFontFromFileTTF(actual_font_path.data(), font_size)) {
70 return absl::InternalError(
71 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
75 std::string actual_icon_font_path =
"";
77#if defined(__APPLE__) && defined(__MACH__)
80 actual_icon_font_path = kIconBundlePath +
"MaterialIcons-Regular.ttf";
82 actual_icon_font_path =
84 "Contents/Resources/font/MaterialIcons-Regular.ttf");
87 actual_icon_font_path = std::filesystem::absolute(icon_font_path).string();
89 io.Fonts->AddFontFromFileTTF(actual_icon_font_path.data(), ICON_FONT_SIZE,
90 &icons_config, icons_ranges);
93 std::string actual_japanese_font_path =
"";
94 const char *japanese_font_path = NOTO_SANS_JP;
95#if defined(__APPLE__) && defined(__MACH__)
98 actual_japanese_font_path = kJapaneseBundlePath + japanese_font_path;
100 actual_japanese_font_path =
105 actual_japanese_font_path =
106 std::filesystem::absolute(japanese_font_path).string();
108 io.Fonts->AddFontFromFileTTF(actual_japanese_font_path.data(), 18.0f,
109 &japanese_font_config,
110 io.Fonts->GetGlyphRangesJapanese());
112 return absl::OkStatus();
118int CALLBACK EnumFontFamExProc(
const LOGFONT *lpelfe,
const TEXTMETRIC *lpntme,
119 DWORD FontType, LPARAM lParam) {
121 ImGuiIO &io = ImGui::GetIO();
122 io.Fonts->AddFontFromFileTTF(lpelfe->lfFaceName, 16.0f);
129 std::vector<std::string> fontPaths;
134 TEXT(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"), 0,
135 KEY_READ, &hKey) == ERROR_SUCCESS) {
137 DWORD maxValueNameSize;
138 DWORD maxValueDataSize;
141 RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &valueCount,
142 &maxValueNameSize, &maxValueDataSize, NULL, NULL);
144 char *valueName =
new char[maxValueNameSize + 1];
145 BYTE *valueData =
new BYTE[maxValueDataSize + 1];
148 for (DWORD i = 0; i < valueCount; i++) {
149 DWORD valueNameSize = maxValueNameSize + 1;
150 DWORD valueDataSize = maxValueDataSize + 1;
154 memset(valueName, 0, valueNameSize);
155 memset(valueData, 0, valueDataSize);
158 if (RegEnumValue(hKey, i, valueName, &valueNameSize, NULL, &valueType,
159 valueData, &valueDataSize) == ERROR_SUCCESS) {
160 if (valueType == REG_SZ) {
162 std::string fontPath(
reinterpret_cast<char *
>(valueData),
165 fontPaths.push_back(fontPath);
176 ImGuiIO &io = ImGui::GetIO();
179 static const std::unordered_set<std::string> commonFontFaceNames = {
191 "Lucida Sans Unicode",
195 for (
auto &fontPath : fontPaths) {
197 if (fontPath.substr(0, 2) !=
"C:") {
199 fontPath = absl::StrFormat(
"C:\\Windows\\Fonts\\%s", fontPath.c_str());
203 std::string extension = fontPath.substr(fontPath.find_last_of(
".") + 1);
204 if (extension ==
"ttf" || extension ==
"TTF") {
206 std::string fontFaceName =
207 fontPath.substr(fontPath.find_last_of(
"\\/") + 1);
208 fontFaceName = fontFaceName.substr(0, fontFaceName.find_last_of(
"."));
211 if (commonFontFaceNames.find(fontFaceName) != commonFontFaceNames.end()) {
212 io.Fonts->AddFontFromFileTTF(fontPath.c_str(), 16.0f);
216 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
217 ImFontConfig icons_config;
218 static const float ICON_FONT_SIZE = 18.0f;
219 icons_config.MergeMode =
true;
220 icons_config.GlyphOffset.y = 5.0f;
221 icons_config.GlyphMinAdvanceX = 13.0f;
222 icons_config.PixelSnapH =
true;
224 &icons_config, icons_ranges);
230#elif defined(__linux__)
#define FONT_ICON_FILE_NAME_MD
absl::Status LoadPackageFonts()
std::string GetBundleResourcePath()
GetBundleResourcePath returns the path to the bundle resource directory. Specific to MacOS.