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 kBundlePath = util::GetBundleResourcePath();
35 return kBundlePath + font_path;
36#else
37 return absl::StrCat(util::GetBundleResourcePath(), "Contents/Resources/font/",
38 font_path);
39#endif
40#else
41 return absl::StrCat("assets/font/", font_path);
42#endif
43}
44
45absl::Status LoadFont(const FontConfig& font_config) {
46 ImGuiIO& imgui_io = ImGui::GetIO();
47 std::string actual_font_path = SetFontPath(font_config.font_path);
48 // Check if the file exists with std library first, since ImGui IO will assert
49 // if the file does not exist
50 if (!std::filesystem::exists(actual_font_path)) {
51 return absl::InternalError(
52 absl::StrFormat("Font file %s does not exist", actual_font_path));
53 }
54
55 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
56 font_config.font_size)) {
57 return absl::InternalError(
58 absl::StrFormat("Failed to load font from %s", actual_font_path));
59 }
60 return absl::OkStatus();
61}
62
63absl::Status AddIconFont(const FontConfig& /*config*/) {
64 static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0};
65 ImFontConfig icons_config{};
66 icons_config.MergeMode = true;
67 icons_config.GlyphOffset.y = 5.0F;
68 icons_config.GlyphMinAdvanceX = 13.0F;
69 icons_config.PixelSnapH = true;
70 std::string icon_font_path = SetFontPath(FONT_ICON_FILE_NAME_MD);
71 ImGuiIO& imgui_io = ImGui::GetIO();
72 if (!imgui_io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(),
73 ICON_FONT_SIZE, &icons_config,
74 icons_ranges)) {
75 return absl::InternalError("Failed to add icon fonts");
76 }
77 return absl::OkStatus();
78}
79
80absl::Status AddJapaneseFont(const FontConfig& /*config*/) {
81 ImFontConfig japanese_font_config{};
82 japanese_font_config.MergeMode = true;
83 japanese_font_config.GlyphOffset.y = 5.0F;
84 japanese_font_config.GlyphMinAdvanceX = 13.0F;
85 japanese_font_config.PixelSnapH = true;
86 std::string japanese_font_path = SetFontPath(NOTO_SANS_JP);
87 ImGuiIO& imgui_io = ImGui::GetIO();
88 if (!imgui_io.Fonts->AddFontFromFileTTF(
89 japanese_font_path.data(), ICON_FONT_SIZE, &japanese_font_config,
90 imgui_io.Fonts->GetGlyphRangesJapanese())) {
91 return absl::InternalError("Failed to add Japanese fonts");
92 }
93 return absl::OkStatus();
94}
95
96} // namespace
97
98absl::Status LoadPackageFonts() {
99 if (font_registry.fonts.empty()) {
100 // Initialize the font names and sizes with proper ImFontConfig
101 // initialization
102 font_registry.fonts = {
103 FontConfig{KARLA_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
104 FontConfig{ROBOTO_MEDIUM, FONT_SIZE_DEFAULT, {}, {}},
105 FontConfig{COUSINE_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
106 FontConfig{IBM_PLEX_JP, FONT_SIZE_DEFAULT, {}, {}},
107 FontConfig{DROID_SANS, FONT_SIZE_DROID_SANS, {}, {}},
108 };
109 }
110
111 // Load fonts with associated icon and Japanese merges
112 for (const auto& font_config : font_registry.fonts) {
113 RETURN_IF_ERROR(LoadFont(font_config));
114 RETURN_IF_ERROR(AddIconFont(font_config));
115 RETURN_IF_ERROR(AddJapaneseFont(font_config));
116 }
117 return absl::OkStatus();
118}
119
120absl::Status ReloadPackageFont(const FontConfig& config) {
121 ImGuiIO& imgui_io = ImGui::GetIO();
122 std::string actual_font_path = SetFontPath(config.font_path);
123 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
124 config.font_size)) {
125 return absl::InternalError(
126 absl::StrFormat("Failed to load font from %s", actual_font_path));
127 }
128 RETURN_IF_ERROR(AddIconFont(config));
129 RETURN_IF_ERROR(AddJapaneseFont(config));
130 return absl::OkStatus();
131}
132
133absl::Status LoadFontFromMemory(const std::string& name,
134 const std::string& data, float size_pixels) {
135 ImGuiIO& imgui_io = ImGui::GetIO();
136
137 // ImGui takes ownership of the data and will free it
138 void* font_data = ImGui::MemAlloc(data.size());
139 if (!font_data) {
140 return absl::InternalError("Failed to allocate memory for font data");
141 }
142 std::memcpy(font_data, data.data(), data.size());
143
144 ImFontConfig config;
145 std::strncpy(config.Name, name.c_str(), sizeof(config.Name) - 1);
146 config.Name[sizeof(config.Name) - 1] = 0;
147
148 if (!imgui_io.Fonts->AddFontFromMemoryTTF(font_data,
149 static_cast<int>(data.size()),
150 size_pixels, &config)) {
151 ImGui::MemFree(font_data);
152 return absl::InternalError("Failed to load font from memory");
153 }
154
155 // We also need to add icons and Japanese characters to this new font
156 // Note: This is a simplified version of AddIconFont/AddJapaneseFont that
157 // works with the current font being added (since we can't easily merge into
158 // a font that's just been added without rebuilding atlas immediately)
159 // For now, we'll just load the base font. Merging requires more complex logic.
160
161 // Important: We must rebuild the font atlas!
162 // This is usually done by the backend, but since we changed fonts at runtime...
163 // Ideally, this should be done before NewFrame().
164 // If called during a frame, changes won't appear until texture is rebuilt.
165
166 return absl::OkStatus();
167}
168
169#ifdef __linux__
170void LoadSystemFonts() {
171 // Load Linux System Fonts into ImGui
172 // System font loading is now handled by NFD (Native File Dialog)
173 // This function is kept for compatibility but does nothing
174}
175#endif
176
177} // 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