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;
32 const std::vector<std::filesystem::path> candidates = {
33 std::filesystem::path(
"assets/font") / font_path,
34 std::filesystem::path(
"../assets/font") / font_path,
35 std::filesystem::path(
"../../assets/font") / font_path,
36 std::filesystem::path(
"../../../assets/font") / font_path,
38 for (
const auto& candidate : candidates) {
39 if (std::filesystem::exists(candidate)) {
40 return candidate.string();
43 return (std::filesystem::path(
"assets/font") / font_path).
string();
50 std::string bundle_path =
51 absl::StrCat(bundle_root,
"assets/font/", font_path);
52 if (std::filesystem::exists(bundle_path)) {
55 bundle_path = absl::StrCat(bundle_root, font_path);
56 if (std::filesystem::exists(bundle_path)) {
62 "Contents/Resources/font/", font_path);
63 if (std::filesystem::exists(bundle_path)) {
74 ImGuiIO& imgui_io = ImGui::GetIO();
78 if (!std::filesystem::exists(actual_font_path)) {
79 return absl::InternalError(
80 absl::StrFormat(
"Font file %s does not exist", actual_font_path));
83 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
85 return absl::InternalError(
86 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
88 return absl::OkStatus();
92 static const ImWchar icons_ranges[] = {
ICON_MIN_MD, 0xf900, 0};
93 ImFontConfig icons_config{};
94 icons_config.MergeMode =
true;
95 icons_config.GlyphOffset.y = 5.0F;
96 icons_config.GlyphMinAdvanceX = 13.0F;
97 icons_config.PixelSnapH =
true;
99 ImGuiIO& imgui_io = ImGui::GetIO();
100 if (!imgui_io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(),
101 ICON_FONT_SIZE, &icons_config,
103 return absl::InternalError(
"Failed to add icon fonts");
105 return absl::OkStatus();
109 ImFontConfig japanese_font_config{};
110 japanese_font_config.MergeMode =
true;
111 japanese_font_config.GlyphOffset.y = 5.0F;
112 japanese_font_config.GlyphMinAdvanceX = 13.0F;
113 japanese_font_config.PixelSnapH =
true;
114 std::string japanese_font_path =
SetFontPath(NOTO_SANS_JP);
115 ImGuiIO& imgui_io = ImGui::GetIO();
116 if (!imgui_io.Fonts->AddFontFromFileTTF(
117 japanese_font_path.data(), ICON_FONT_SIZE, &japanese_font_config,
118 imgui_io.Fonts->GetGlyphRangesJapanese())) {
119 return absl::InternalError(
"Failed to add Japanese fonts");
121 return absl::OkStatus();
131 FontConfig{KARLA_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
132 FontConfig{ROBOTO_MEDIUM, FONT_SIZE_DEFAULT, {}, {}},
133 FontConfig{COUSINE_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
134 FontConfig{IBM_PLEX_JP, FONT_SIZE_DEFAULT, {}, {}},
135 FontConfig{DROID_SANS, FONT_SIZE_DROID_SANS, {}, {}},
145 return absl::OkStatus();
149 ImGuiIO& imgui_io = ImGui::GetIO();
150 std::string actual_font_path = SetFontPath(config.
font_path);
151 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
153 return absl::InternalError(
154 absl::StrFormat(
"Failed to load font from %s", actual_font_path));
158 return absl::OkStatus();
162 const std::string& data,
float size_pixels) {
163 ImGuiIO& imgui_io = ImGui::GetIO();
166 void* font_data = ImGui::MemAlloc(data.size());
168 return absl::InternalError(
"Failed to allocate memory for font data");
170 std::memcpy(font_data, data.data(), data.size());
173 std::strncpy(config.Name, name.c_str(),
sizeof(config.Name) - 1);
174 config.Name[
sizeof(config.Name) - 1] = 0;
176 if (!imgui_io.Fonts->AddFontFromMemoryTTF(
177 font_data,
static_cast<int>(data.size()), size_pixels, &config)) {
178 ImGui::MemFree(font_data);
179 return absl::InternalError(
"Failed to load font from memory");
193 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)
#define RETURN_IF_ERROR(expr)
std::vector< FontConfig > fonts