8#include "absl/status/status.h"
9#include "absl/strings/str_cat.h"
10#include "absl/strings/str_format.h"
12#include "imgui/imgui.h"
18static const char* KARLA_REGULAR =
"Karla-Regular.ttf";
19static const char* ROBOTO_MEDIUM =
"Roboto-Medium.ttf";
20static const char* COUSINE_REGULAR =
"Cousine-Regular.ttf";
21static const char* DROID_SANS =
"DroidSans.ttf";
22static const char* NOTO_SANS_JP =
"NotoSansJP.ttf";
23static const char* IBM_PLEX_JP =
"IBMPlexSansJP-Bold.ttf";
25static const float FONT_SIZE_DEFAULT = 16.0F;
26static const float FONT_SIZE_DROID_SANS = 18.0F;
27static const float ICON_FONT_SIZE = 18.0F;
35 std::string bundle_path =
36 absl::StrCat(bundle_root,
"assets/font/", font_path);
37 if (std::filesystem::exists(bundle_path)) {
40 bundle_path = absl::StrCat(bundle_root, font_path);
41 if (std::filesystem::exists(bundle_path)) {
44 return absl::StrCat(
"assets/font/", font_path);
47 "Contents/Resources/font/", font_path);
48 if (std::filesystem::exists(bundle_path)) {
51 return absl::StrCat(
"assets/font/", font_path);
54 return absl::StrCat(
"assets/font/", font_path);
59 ImGuiIO& imgui_io = ImGui::GetIO();
63 if (!std::filesystem::exists(actual_font_path)) {
66 std::string fallback_path =
67 absl::StrCat(
"assets/font/", font_config.
font_path);
68 if (std::filesystem::exists(fallback_path)) {
69 actual_font_path = fallback_path;
71 return absl::InternalError(
72 absl::StrFormat(
"Font file %s does not exist", actual_font_path));
75 return absl::InternalError(
76 absl::StrFormat(
"Font file %s does not exist", actual_font_path));
80 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
82 return absl::InternalError(
83 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
85 return absl::OkStatus();
89 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
90 ImFontConfig icons_config{};
91 icons_config.MergeMode =
true;
92 icons_config.GlyphOffset.y = 5.0F;
93 icons_config.GlyphMinAdvanceX = 13.0F;
94 icons_config.PixelSnapH =
true;
96 ImGuiIO& imgui_io = ImGui::GetIO();
97 if (!imgui_io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(),
98 ICON_FONT_SIZE, &icons_config,
100 return absl::InternalError(
"Failed to add icon fonts");
102 return absl::OkStatus();
106 ImFontConfig japanese_font_config{};
107 japanese_font_config.MergeMode =
true;
108 japanese_font_config.GlyphOffset.y = 5.0F;
109 japanese_font_config.GlyphMinAdvanceX = 13.0F;
110 japanese_font_config.PixelSnapH =
true;
111 std::string japanese_font_path =
SetFontPath(NOTO_SANS_JP);
112 ImGuiIO& imgui_io = ImGui::GetIO();
113 if (!imgui_io.Fonts->AddFontFromFileTTF(
114 japanese_font_path.data(), ICON_FONT_SIZE, &japanese_font_config,
115 imgui_io.Fonts->GetGlyphRangesJapanese())) {
116 return absl::InternalError(
"Failed to add Japanese fonts");
118 return absl::OkStatus();
128 FontConfig{KARLA_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
129 FontConfig{ROBOTO_MEDIUM, FONT_SIZE_DEFAULT, {}, {}},
130 FontConfig{COUSINE_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
131 FontConfig{IBM_PLEX_JP, FONT_SIZE_DEFAULT, {}, {}},
132 FontConfig{DROID_SANS, FONT_SIZE_DROID_SANS, {}, {}},
142 return absl::OkStatus();
146 ImGuiIO& imgui_io = ImGui::GetIO();
147 std::string actual_font_path = SetFontPath(config.
font_path);
148 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
150 return absl::InternalError(
151 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
155 return absl::OkStatus();
159 const std::string& data,
float size_pixels) {
160 ImGuiIO& imgui_io = ImGui::GetIO();
163 void* font_data = ImGui::MemAlloc(data.size());
165 return absl::InternalError(
"Failed to allocate memory for font data");
167 std::memcpy(font_data, data.data(), data.size());
170 std::strncpy(config.Name, name.c_str(),
sizeof(config.Name) - 1);
171 config.Name[
sizeof(config.Name) - 1] = 0;
173 if (!imgui_io.Fonts->AddFontFromMemoryTTF(
174 font_data,
static_cast<int>(data.size()), size_pixels, &config)) {
175 ImGui::MemFree(font_data);
176 return absl::InternalError(
"Failed to load font from memory");
190 return absl::OkStatus();
#define FONT_ICON_FILE_NAME_MD
absl::Status AddJapaneseFont(const FontConfig &)
std::string SetFontPath(const std::string &font_path)
absl::Status LoadFont(const FontConfig &font_config)
absl::Status AddIconFont(const FontConfig &)
std::string GetBundleResourcePath()
GetBundleResourcePath returns the path to the bundle resource directory. Specific to MacOS.
absl::Status ReloadPackageFont(const FontConfig &config)
absl::Status LoadPackageFonts()
absl::Status LoadFontFromMemory(const std::string &name, const std::string &data, float size_pixels)
#define RETURN_IF_ERROR(expr)
std::vector< FontConfig > fonts