Refactor font loading: enhance font management by passing FontConfig to AddIconFont and AddJapaneseFont functions, and streamline font initialization in LoadPackageFonts

This commit is contained in:
scawful
2025-02-27 19:16:37 -05:00
parent df2bc1035c
commit 790f105013
2 changed files with 22 additions and 26 deletions

View File

@@ -61,8 +61,7 @@ absl::Status LoadFont(const FontConfig& font_config) {
return absl::OkStatus(); return absl::OkStatus();
} }
absl::Status AddIconFont() { absl::Status AddIconFont(const FontConfig& config) {
ImGuiIO& io = ImGui::GetIO();
static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0}; static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0};
ImFontConfig icons_config; ImFontConfig icons_config;
icons_config.MergeMode = true; icons_config.MergeMode = true;
@@ -70,6 +69,7 @@ absl::Status AddIconFont() {
icons_config.GlyphMinAdvanceX = 13.0f; icons_config.GlyphMinAdvanceX = 13.0f;
icons_config.PixelSnapH = true; icons_config.PixelSnapH = true;
std::string icon_font_path = SetFontPath(FONT_ICON_FILE_NAME_MD); std::string icon_font_path = SetFontPath(FONT_ICON_FILE_NAME_MD);
ImGuiIO& io = ImGui::GetIO();
if (!io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(), ICON_FONT_SIZE, if (!io.Fonts->AddFontFromFileTTF(icon_font_path.c_str(), ICON_FONT_SIZE,
&icons_config, icons_ranges)) { &icons_config, icons_ranges)) {
return absl::InternalError("Failed to add icon fonts"); return absl::InternalError("Failed to add icon fonts");
@@ -77,14 +77,14 @@ absl::Status AddIconFont() {
return absl::OkStatus(); return absl::OkStatus();
} }
absl::Status AddJapaneseFont() { absl::Status AddJapaneseFont(const FontConfig& config) {
ImGuiIO& io = ImGui::GetIO();
ImFontConfig japanese_font_config; ImFontConfig japanese_font_config;
japanese_font_config.MergeMode = true; japanese_font_config.MergeMode = true;
japanese_font_config.GlyphOffset.y = 5.0f; japanese_font_config.GlyphOffset.y = 5.0f;
japanese_font_config.GlyphMinAdvanceX = 13.0f; japanese_font_config.GlyphMinAdvanceX = 13.0f;
japanese_font_config.PixelSnapH = true; japanese_font_config.PixelSnapH = true;
std::string japanese_font_path = SetFontPath(NOTO_SANS_JP); std::string japanese_font_path = SetFontPath(NOTO_SANS_JP);
ImGuiIO& io = ImGui::GetIO();
if (!io.Fonts->AddFontFromFileTTF(japanese_font_path.data(), ICON_FONT_SIZE, if (!io.Fonts->AddFontFromFileTTF(japanese_font_path.data(), ICON_FONT_SIZE,
&japanese_font_config, &japanese_font_config,
io.Fonts->GetGlyphRangesJapanese())) { io.Fonts->GetGlyphRangesJapanese())) {
@@ -96,25 +96,22 @@ absl::Status AddJapaneseFont() {
} // namespace } // namespace
absl::Status LoadPackageFonts() { absl::Status LoadPackageFonts() {
ImGuiIO& io = ImGui::GetIO(); if (font_registry.fonts.empty()) {
// Initialize the font names and sizes
// List of fonts to be loaded font_registry.fonts = {
std::vector<const char*> font_paths = { {KARLA_REGULAR, FONT_SIZE_DEFAULT},
KARLA_REGULAR, ROBOTO_MEDIUM, COUSINE_REGULAR, IBM_PLEX_JP, DROID_SANS}; {ROBOTO_MEDIUM, FONT_SIZE_DEFAULT},
{COUSINE_REGULAR, FONT_SIZE_DEFAULT},
{IBM_PLEX_JP, FONT_SIZE_DEFAULT},
{DROID_SANS, FONT_SIZE_DROID_SANS},
};
}
// Load fonts with associated icon and Japanese merges // Load fonts with associated icon and Japanese merges
for (const auto& font_path : font_paths) { for (const auto& font_config : font_registry.fonts) {
float font_size =
(font_path == DROID_SANS) ? FONT_SIZE_DROID_SANS : FONT_SIZE_DEFAULT;
FontConfig font_config = {font_path, font_size};
RETURN_IF_ERROR(LoadFont(font_config)); RETURN_IF_ERROR(LoadFont(font_config));
RETURN_IF_ERROR(AddIconFont(font_config));
// Merge icon set RETURN_IF_ERROR(AddJapaneseFont(font_config));
RETURN_IF_ERROR(AddIconFont());
// Merge Japanese font
RETURN_IF_ERROR(AddJapaneseFont());
} }
return absl::OkStatus(); return absl::OkStatus();
} }
@@ -127,8 +124,8 @@ absl::Status ReloadPackageFont(const FontConfig& config) {
return absl::InternalError( return absl::InternalError(
absl::StrFormat("Failed to load font from %s", actual_font_path)); absl::StrFormat("Failed to load font from %s", actual_font_path));
} }
RETURN_IF_ERROR(AddIconFont()); RETURN_IF_ERROR(AddIconFont(config));
RETURN_IF_ERROR(AddJapaneseFont()); RETURN_IF_ERROR(AddJapaneseFont(config));
return absl::OkStatus(); return absl::OkStatus();
} }

View File

@@ -4,15 +4,14 @@
#include <vector> #include <vector>
#include "absl/status/status.h" #include "absl/status/status.h"
#include "imgui/imgui.h" #include "imgui/imgui.h"
namespace yaze { namespace yaze {
namespace core { namespace core {
struct FontConfig { struct FontConfig {
const char* font_path; const char* font_path;
float font_size; float font_size;
ImFontConfig im_font_config; ImFontConfig im_font_config;
ImFontConfig jp_conf_config; ImFontConfig jp_conf_config;
}; };
@@ -21,7 +20,7 @@ struct FontState {
std::vector<FontConfig> fonts; std::vector<FontConfig> fonts;
}; };
static FontState global_font_state; static FontState font_registry;
absl::Status LoadPackageFonts(); absl::Status LoadPackageFonts();