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"
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;
38 return found->string();
41 const std::vector<std::filesystem::path> candidates = {
42 std::filesystem::path(
"assets/font") / font_path,
43 std::filesystem::path(
"../assets/font") / font_path,
44 std::filesystem::path(
"../../assets/font") / font_path,
45 std::filesystem::path(
"../../../assets/font") / font_path,
47 for (
const auto& candidate : candidates) {
48 if (std::filesystem::exists(candidate)) {
49 return candidate.string();
52 return (std::filesystem::path(
"assets/font") / font_path).
string();
59 std::string bundle_path =
60 absl::StrCat(bundle_root,
"assets/font/", font_path);
61 if (std::filesystem::exists(bundle_path)) {
64 bundle_path = absl::StrCat(bundle_root, font_path);
65 if (std::filesystem::exists(bundle_path)) {
71 "Contents/Resources/font/", font_path);
72 if (std::filesystem::exists(bundle_path)) {
83 ImGuiIO& imgui_io = ImGui::GetIO();
87 if (!std::filesystem::exists(actual_font_path)) {
88 return absl::InternalError(
89 absl::StrFormat(
"Font file %s does not exist", actual_font_path));
92 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
94 return absl::InternalError(
95 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
97 return absl::OkStatus();
101 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
102 ImFontConfig icons_config{};
103 icons_config.MergeMode =
true;
104 icons_config.GlyphOffset.y = 5.0F;
105 icons_config.GlyphMinAdvanceX = 13.0F;
106 icons_config.PixelSnapH =
true;
108 ImGuiIO& imgui_io = ImGui::GetIO();
109 if (!imgui_io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(),
110 ICON_FONT_SIZE, &icons_config,
112 return absl::InternalError(
"Failed to add icon fonts");
114 return absl::OkStatus();
118 ImFontConfig japanese_font_config{};
119 japanese_font_config.MergeMode =
true;
120 japanese_font_config.GlyphOffset.y = 5.0F;
121 japanese_font_config.GlyphMinAdvanceX = 13.0F;
122 japanese_font_config.PixelSnapH =
true;
123 std::string japanese_font_path =
SetFontPath(NOTO_SANS_JP);
124 ImGuiIO& imgui_io = ImGui::GetIO();
125 if (!imgui_io.Fonts->AddFontFromFileTTF(
126 japanese_font_path.data(), ICON_FONT_SIZE, &japanese_font_config,
127 imgui_io.Fonts->GetGlyphRangesJapanese())) {
128 return absl::InternalError(
"Failed to add Japanese fonts");
130 return absl::OkStatus();
140 FontConfig{KARLA_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
141 FontConfig{ROBOTO_MEDIUM, FONT_SIZE_DEFAULT, {}, {}},
142 FontConfig{COUSINE_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
143 FontConfig{IBM_PLEX_JP, FONT_SIZE_DEFAULT, {}, {}},
144 FontConfig{DROID_SANS, FONT_SIZE_DROID_SANS, {}, {}},
154 return absl::OkStatus();
158 ImGuiIO& io = ImGui::GetIO();
159 if (io.Fonts ==
nullptr || io.Fonts->Fonts.Size == 0) {
162 if (index < 0 || index >= io.Fonts->Fonts.Size) {
165 io.FontDefault = io.Fonts->Fonts[index];
169 ImGuiIO& imgui_io = ImGui::GetIO();
170 std::string actual_font_path = SetFontPath(config.
font_path);
171 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
173 return absl::InternalError(
174 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
178 return absl::OkStatus();
182 const std::string& data,
float size_pixels) {
183 ImGuiIO& imgui_io = ImGui::GetIO();
186 void* font_data = ImGui::MemAlloc(data.size());
188 return absl::InternalError(
"Failed to allocate memory for font data");
190 std::memcpy(font_data, data.data(), data.size());
193 std::strncpy(config.Name, name.c_str(),
sizeof(config.Name) - 1);
194 config.Name[
sizeof(config.Name) - 1] = 0;
196 if (!imgui_io.Fonts->AddFontFromMemoryTTF(
197 font_data,
static_cast<int>(data.size()), size_pixels, &config)) {
198 ImGui::MemFree(font_data);
199 return absl::InternalError(
"Failed to load font from memory");
213 return absl::OkStatus();
#define FONT_ICON_FILE_NAME_MD
absl::Status AddJapaneseFont(const FontConfig &)
std::string ResolveRepoFontPath(const std::string &font_path)
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)
void SetActiveFontIndex(int index)
#define RETURN_IF_ERROR(expr)
std::vector< FontConfig > fonts