5#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"
19static const char* KARLA_REGULAR =
"Karla-Regular.ttf";
20static const char* ROBOTO_MEDIUM =
"Roboto-Medium.ttf";
21static const char* COUSINE_REGULAR =
"Cousine-Regular.ttf";
22static const char* DROID_SANS =
"DroidSans.ttf";
23static const char* NOTO_SANS_JP =
"NotoSansJP.ttf";
24static const char* IBM_PLEX_JP =
"IBMPlexSansJP-Bold.ttf";
26static const float FONT_SIZE_DEFAULT = 16.0f;
27static const float FONT_SIZE_DROID_SANS = 18.0f;
28static const float ICON_FONT_SIZE = 18.0f;
36 return kBundlePath + font_path;
42 return absl::StrCat(
"assets/font/", font_path);
47 ImGuiIO& io = ImGui::GetIO();
51 if (!std::filesystem::exists(actual_font_path)) {
52 return absl::InternalError(
53 absl::StrFormat(
"Font file %s does not exist", actual_font_path));
56 if (!io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
58 return absl::InternalError(
59 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
61 return absl::OkStatus();
65 ImGuiIO& io = ImGui::GetIO();
66 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
67 ImFontConfig icons_config;
68 icons_config.MergeMode =
true;
69 icons_config.GlyphOffset.y = 5.0f;
70 icons_config.GlyphMinAdvanceX = 13.0f;
71 icons_config.PixelSnapH =
true;
73 if (!io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(), ICON_FONT_SIZE,
74 &icons_config, icons_ranges)) {
75 return absl::InternalError(
"Failed to add icon fonts");
77 return absl::OkStatus();
81 ImGuiIO& io = ImGui::GetIO();
82 ImFontConfig japanese_font_config;
83 japanese_font_config.MergeMode =
true;
84 japanese_font_config.GlyphOffset.y = 5.0f;
85 japanese_font_config.GlyphMinAdvanceX = 13.0f;
86 japanese_font_config.PixelSnapH =
true;
87 std::string japanese_font_path =
SetFontPath(NOTO_SANS_JP);
88 if (!io.Fonts->AddFontFromFileTTF(japanese_font_path.data(), ICON_FONT_SIZE,
89 &japanese_font_config,
90 io.Fonts->GetGlyphRangesJapanese())) {
91 return absl::InternalError(
"Failed to add Japanese fonts");
93 return absl::OkStatus();
99 ImGuiIO& io = ImGui::GetIO();
102 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
103 ImFontConfig icons_config;
104 icons_config.MergeMode =
true;
105 icons_config.GlyphOffset.y = 5.0f;
106 icons_config.GlyphMinAdvanceX = 13.0f;
107 icons_config.PixelSnapH =
true;
110 ImFontConfig japanese_font_config;
111 japanese_font_config.MergeMode =
true;
112 icons_config.GlyphOffset.y = 5.0f;
113 icons_config.GlyphMinAdvanceX = 13.0f;
114 icons_config.PixelSnapH =
true;
117 std::vector<const char*> font_paths = {
118 KARLA_REGULAR, ROBOTO_MEDIUM, COUSINE_REGULAR, IBM_PLEX_JP, DROID_SANS};
121 for (
const auto& font_path : font_paths) {
123 (font_path == DROID_SANS) ? FONT_SIZE_DROID_SANS : FONT_SIZE_DEFAULT;
125 FontConfig font_config = {font_path, font_size};
134 return absl::OkStatus();
138 ImGuiIO& io = ImGui::GetIO();
139 std::string actual_font_path = SetFontPath(config.
font_path);
140 if (!io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
142 return absl::InternalError(
143 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
147 return absl::OkStatus();
153int CALLBACK EnumFontFamExProc(
const LOGFONT* lpelfe,
const TEXTMETRIC* lpntme,
154 DWORD FontType, LPARAM lParam) {
156 ImGuiIO& io = ImGui::GetIO();
157 io.Fonts->AddFontFromFileTTF(lpelfe->lfFaceName, 16.0f);
164 std::vector<std::string> fontPaths;
169 TEXT(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"), 0,
170 KEY_READ, &hKey) == ERROR_SUCCESS) {
172 DWORD maxValueNameSize;
173 DWORD maxValueDataSize;
176 RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &valueCount,
177 &maxValueNameSize, &maxValueDataSize, NULL, NULL);
179 char* valueName =
new char[maxValueNameSize + 1];
180 BYTE* valueData =
new BYTE[maxValueDataSize + 1];
183 for (DWORD i = 0; i < valueCount; i++) {
184 DWORD valueNameSize = maxValueNameSize + 1;
185 DWORD valueDataSize = maxValueDataSize + 1;
189 memset(valueName, 0, valueNameSize);
190 memset(valueData, 0, valueDataSize);
193 if (RegEnumValue(hKey, i, valueName, &valueNameSize, NULL, &valueType,
194 valueData, &valueDataSize) == ERROR_SUCCESS) {
195 if (valueType == REG_SZ) {
197 std::string fontPath(
reinterpret_cast<char*
>(valueData),
200 fontPaths.push_back(fontPath);
211 ImGuiIO& io = ImGui::GetIO();
214 static const std::unordered_set<std::string> commonFontFaceNames = {
226 "Lucida Sans Unicode",
230 for (
auto& fontPath : fontPaths) {
232 if (fontPath.substr(0, 2) !=
"C:") {
234 fontPath = absl::StrFormat(
"C:\\Windows\\Fonts\\%s", fontPath.c_str());
238 std::string extension = fontPath.substr(fontPath.find_last_of(
".") + 1);
239 if (extension ==
"ttf" || extension ==
"TTF") {
241 std::string fontFaceName =
242 fontPath.substr(fontPath.find_last_of(
"\\/") + 1);
243 fontFaceName = fontFaceName.substr(0, fontFaceName.find_last_of(
"."));
246 if (commonFontFaceNames.find(fontFaceName) != commonFontFaceNames.end()) {
247 io.Fonts->AddFontFromFileTTF(fontPath.c_str(), 16.0f);
251 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
252 ImFontConfig icons_config;
253 static const float ICON_FONT_SIZE = 18.0f;
254 icons_config.MergeMode =
true;
255 icons_config.GlyphOffset.y = 5.0f;
256 icons_config.GlyphMinAdvanceX = 13.0f;
257 icons_config.PixelSnapH =
true;
259 &icons_config, icons_ranges);
265#elif defined(__linux__)
#define FONT_ICON_FILE_NAME_MD
#define RETURN_IF_ERROR(expression)
std::string SetFontPath(const std::string &font_path)
absl::Status AddIconFont()
absl::Status AddJapaneseFont()
absl::Status LoadFont(const FontConfig &font_config)
std::string GetBundleResourcePath()
GetBundleResourcePath returns the path to the bundle resource directory. Specific to MacOS.
absl::Status ReloadPackageFont(const FontConfig &config)
absl::Status LoadPackageFonts()
Main namespace for the application.