yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
font_loader.cc
Go to the documentation of this file.
2
3#include <cstring>
4#include <filesystem>
5#include <string>
6#include <vector>
7
8#include "absl/status/status.h"
9#include "absl/strings/str_cat.h"
10#include "absl/strings/str_format.h"
11#include "app/gui/core/icons.h"
12#include "imgui/imgui.h"
13#include "util/file_util.h"
14#include "util/macro.h"
15
16namespace yaze {
17
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";
24
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;
28
29namespace {
30
31std::string SetFontPath(const std::string& font_path) {
32#ifdef __APPLE__
33#if TARGET_OS_IOS == 1
34 const std::string bundle_root = util::GetBundleResourcePath();
35 std::string bundle_path =
36 absl::StrCat(bundle_root, "assets/font/", font_path);
37 if (std::filesystem::exists(bundle_path)) {
38 return bundle_path;
39 }
40 bundle_path = absl::StrCat(bundle_root, font_path);
41 if (std::filesystem::exists(bundle_path)) {
42 return bundle_path;
43 }
44 return absl::StrCat("assets/font/", font_path);
45#else
46 std::string bundle_path = absl::StrCat(util::GetBundleResourcePath(),
47 "Contents/Resources/font/", font_path);
48 if (std::filesystem::exists(bundle_path)) {
49 return bundle_path;
50 }
51 return absl::StrCat("assets/font/", font_path);
52#endif
53#else
54 return absl::StrCat("assets/font/", font_path);
55#endif
56}
57
58absl::Status LoadFont(const FontConfig& font_config) {
59 ImGuiIO& imgui_io = ImGui::GetIO();
60 std::string actual_font_path = SetFontPath(font_config.font_path);
61 // Check if the file exists with std library first, since ImGui IO will assert
62 // if the file does not exist
63 if (!std::filesystem::exists(actual_font_path)) {
64#ifdef __APPLE__
65 // Allow CLI/test runs to use repo assets when no app bundle is present.
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;
70 } else {
71 return absl::InternalError(
72 absl::StrFormat("Font file %s does not exist", actual_font_path));
73 }
74#else
75 return absl::InternalError(
76 absl::StrFormat("Font file %s does not exist", actual_font_path));
77#endif
78 }
79
80 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
81 font_config.font_size)) {
82 return absl::InternalError(
83 absl::StrFormat("Failed to load font from %s", actual_font_path));
84 }
85 return absl::OkStatus();
86}
87
88absl::Status AddIconFont(const FontConfig& /*config*/) {
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;
95 std::string icon_font_path = SetFontPath(FONT_ICON_FILE_NAME_MD);
96 ImGuiIO& imgui_io = ImGui::GetIO();
97 if (!imgui_io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(),
98 ICON_FONT_SIZE, &icons_config,
99 icons_ranges)) {
100 return absl::InternalError("Failed to add icon fonts");
101 }
102 return absl::OkStatus();
103}
104
105absl::Status AddJapaneseFont(const FontConfig& /*config*/) {
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");
117 }
118 return absl::OkStatus();
119}
120
121} // namespace
122
123absl::Status LoadPackageFonts() {
124 if (font_registry.fonts.empty()) {
125 // Initialize the font names and sizes with proper ImFontConfig
126 // initialization
127 font_registry.fonts = {
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, {}, {}},
133 };
134 }
135
136 // Load fonts with associated icon and Japanese merges
137 for (const auto& font_config : font_registry.fonts) {
138 RETURN_IF_ERROR(LoadFont(font_config));
139 RETURN_IF_ERROR(AddIconFont(font_config));
140 RETURN_IF_ERROR(AddJapaneseFont(font_config));
141 }
142 return absl::OkStatus();
143}
144
145absl::Status ReloadPackageFont(const FontConfig& config) {
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(),
149 config.font_size)) {
150 return absl::InternalError(
151 absl::StrFormat("Failed to load font from %s", actual_font_path));
152 }
153 RETURN_IF_ERROR(AddIconFont(config));
154 RETURN_IF_ERROR(AddJapaneseFont(config));
155 return absl::OkStatus();
156}
157
158absl::Status LoadFontFromMemory(const std::string& name,
159 const std::string& data, float size_pixels) {
160 ImGuiIO& imgui_io = ImGui::GetIO();
161
162 // ImGui takes ownership of the data and will free it
163 void* font_data = ImGui::MemAlloc(data.size());
164 if (!font_data) {
165 return absl::InternalError("Failed to allocate memory for font data");
166 }
167 std::memcpy(font_data, data.data(), data.size());
168
169 ImFontConfig config;
170 std::strncpy(config.Name, name.c_str(), sizeof(config.Name) - 1);
171 config.Name[sizeof(config.Name) - 1] = 0;
172
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");
177 }
178
179 // We also need to add icons and Japanese characters to this new font
180 // Note: This is a simplified version of AddIconFont/AddJapaneseFont that
181 // works with the current font being added (since we can't easily merge into
182 // a font that's just been added without rebuilding atlas immediately)
183 // For now, we'll just load the base font. Merging requires more complex logic.
184
185 // Important: We must rebuild the font atlas!
186 // This is usually done by the backend, but since we changed fonts at runtime...
187 // Ideally, this should be done before NewFrame().
188 // If called during a frame, changes won't appear until texture is rebuilt.
189
190 return absl::OkStatus();
191}
192
193#ifdef __linux__
194void LoadSystemFonts() {
195 // Load Linux System Fonts into ImGui
196 // System font loading is now handled by NFD (Native File Dialog)
197 // This function is kept for compatibility but does nothing
198}
199#endif
200
201} // namespace yaze
#define ICON_MIN_MD
Definition icons.h:10
#define FONT_ICON_FILE_NAME_MD
Definition icons.h:8
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()
void LoadSystemFonts()
absl::Status LoadFontFromMemory(const std::string &name, const std::string &data, float size_pixels)
#define RETURN_IF_ERROR(expr)
Definition snes.cc:22
const char * font_path
Definition font_loader.h:12