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 <filesystem>
4#include <string>
5#include <vector>
6#include <cstring>
7
8
9#include "absl/status/status.h"
10#include "absl/strings/str_cat.h"
11#include "absl/strings/str_format.h"
12#include "util/file_util.h"
13#include "app/gui/icons.h"
14#include "imgui/imgui.h"
15#include "util/macro.h"
16
17namespace yaze {
18
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";
25
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;
29
30namespace {
31
32std::string SetFontPath(const std::string& font_path) {
33#ifdef __APPLE__
34#if TARGET_OS_IOS == 1
35 const std::string kBundlePath = util::GetBundleResourcePath();
36 return kBundlePath + font_path;
37#else
38 return absl::StrCat(util::GetBundleResourcePath(), "Contents/Resources/font/",
39 font_path);
40#endif
41#else
42 return absl::StrCat("assets/font/", font_path);
43#endif
44}
45
46absl::Status LoadFont(const FontConfig& font_config) {
47 ImGuiIO& imgui_io = ImGui::GetIO();
48 std::string actual_font_path = SetFontPath(font_config.font_path);
49 // Check if the file exists with std library first, since ImGui IO will assert
50 // if the file does not exist
51 if (!std::filesystem::exists(actual_font_path)) {
52 return absl::InternalError(
53 absl::StrFormat("Font file %s does not exist", actual_font_path));
54 }
55
56 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
57 font_config.font_size)) {
58 return absl::InternalError(
59 absl::StrFormat("Failed to load font from %s", actual_font_path));
60 }
61 return absl::OkStatus();
62}
63
64absl::Status AddIconFont(const FontConfig& /*config*/) {
65 static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0};
66 ImFontConfig icons_config{};
67 icons_config.MergeMode = true;
68 icons_config.GlyphOffset.y = 5.0F;
69 icons_config.GlyphMinAdvanceX = 13.0F;
70 icons_config.PixelSnapH = true;
71 std::string icon_font_path = SetFontPath(FONT_ICON_FILE_NAME_MD);
72 ImGuiIO& imgui_io = ImGui::GetIO();
73 if (!imgui_io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(), ICON_FONT_SIZE,
74 &icons_config, 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(japanese_font_path.data(), ICON_FONT_SIZE,
89 &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 initialization
101 font_registry.fonts = {
102 FontConfig{KARLA_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
103 FontConfig{ROBOTO_MEDIUM, FONT_SIZE_DEFAULT, {}, {}},
104 FontConfig{COUSINE_REGULAR, FONT_SIZE_DEFAULT, {}, {}},
105 FontConfig{IBM_PLEX_JP, FONT_SIZE_DEFAULT, {}, {}},
106 FontConfig{DROID_SANS, FONT_SIZE_DROID_SANS, {}, {}},
107 };
108 }
109
110 // Load fonts with associated icon and Japanese merges
111 for (const auto& font_config : font_registry.fonts) {
112 RETURN_IF_ERROR(LoadFont(font_config));
113 RETURN_IF_ERROR(AddIconFont(font_config));
114 RETURN_IF_ERROR(AddJapaneseFont(font_config));
115 }
116 return absl::OkStatus();
117}
118
119absl::Status ReloadPackageFont(const FontConfig& config) {
120 ImGuiIO& imgui_io = ImGui::GetIO();
121 std::string actual_font_path = SetFontPath(config.font_path);
122 if (!imgui_io.Fonts->AddFontFromFileTTF(actual_font_path.data(),
123 config.font_size)) {
124 return absl::InternalError(
125 absl::StrFormat("Failed to load font from %s", actual_font_path));
126 }
127 RETURN_IF_ERROR(AddIconFont(config));
128 RETURN_IF_ERROR(AddJapaneseFont(config));
129 return absl::OkStatus();
130}
131
132#ifdef __linux__
133void LoadSystemFonts() {
134 // Load Linux System Fonts into ImGui
135 // System font loading is now handled by NFD (Native File Dialog)
136 // This function is kept for compatibility but does nothing
137}
138#endif
139
140} // namespace yaze
#define ICON_MIN_MD
Definition icons.h:8
#define FONT_ICON_FILE_NAME_MD
Definition icons.h:6
#define RETURN_IF_ERROR(expression)
Definition macro.h:53
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.
Main namespace for the application.
absl::Status ReloadPackageFont(const FontConfig &config)
absl::Status LoadPackageFonts()
void LoadSystemFonts()
const char * font_path
Definition font_loader.h:13