Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
FontManager.cpp
Go to the documentation of this file.
1/**
2 * @file FontManager.cpp
3 * @brief Implementation of font management system
4 * @author Olympe Engine
5 * @date 2026
6 */
7
8#include "FontManager.h"
9#include "IconsFontAwesome6.h"
10#include "../third_party/imgui/imgui.h"
11#include "../system/system_utils.h"
12#include <fstream>
13
14namespace Olympe {
15
17{
18 static FontManager instance;
19 return instance;
20}
21
23{
24 if (m_Initialized) {
25 return;
26 }
27
28 // Store default font
29 m_DefaultFont = ImGui::GetFont();
30 m_Initialized = true;
31
32 SYSTEM_LOG << "[FontManager] Initialized" << std::endl;
33}
34
35bool FontManager::LoadFontAwesome(const std::string& fontPath, float fontSize)
36{
37 if (!m_Initialized) {
38 SYSTEM_LOG << "[FontManager] ERROR: Not initialized. Call Initialize() first." << std::endl;
39 return false;
40 }
41
42 // Check if file exists
43 std::ifstream file(fontPath);
44 if (!file.good()) {
45 SYSTEM_LOG << "[FontManager] ERROR: Font file not found: " << fontPath << std::endl;
46 return false;
47 }
48 file.close();
49
50 // Get ImGui font atlas
51 ImGuiIO& io = ImGui::GetIO();
52
53 // Configure font with icon range
55
57 config.MergeMode = false;
58 config.PixelSnapH = true;
59 config.GlyphMinAdvanceX = fontSize; // Use fontSize as minimum width
60 config.OversampleH = 1;
61 config.OversampleV = 1;
62
63 // Load Font Awesome as separate font
64 m_FontAwesome = io.Fonts->AddFontFromFileTTF(
65 fontPath.c_str(),
66 fontSize,
67 &config,
69 );
70
71 if (m_FontAwesome == nullptr) {
72 SYSTEM_LOG << "[FontManager] ERROR: Failed to load Font Awesome from: " << fontPath << std::endl;
73 return false;
74 }
75
76 // Rebuild font atlas
77 io.Fonts->Build();
78
79 SYSTEM_LOG << "[FontManager] Font Awesome loaded successfully from: " << fontPath << std::endl;
80 return true;
81}
82
83ImFont* FontManager::LoadFont(const std::string& fontPath, float fontSize, const std::string& fontName)
84{
85 if (!m_Initialized) {
86 SYSTEM_LOG << "[FontManager] ERROR: Not initialized. Call Initialize() first." << std::endl;
87 return nullptr;
88 }
89
90 // Check if file exists
91 std::ifstream file(fontPath);
92 if (!file.good()) {
93 SYSTEM_LOG << "[FontManager] ERROR: Font file not found: " << fontPath << std::endl;
94 return nullptr;
95 }
96 file.close();
97
98 ImGuiIO& io = ImGui::GetIO();
99 ImFont* font = io.Fonts->AddFontFromFileTTF(fontPath.c_str(), fontSize);
100
101 if (font == nullptr) {
102 SYSTEM_LOG << "[FontManager] ERROR: Failed to load font from: " << fontPath << std::endl;
103 return nullptr;
104 }
105
106 // Rebuild font atlas
107 io.Fonts->Build();
108
109 std::string name = fontName.empty() ? fontPath : fontName;
110 SYSTEM_LOG << "[FontManager] Font '" << name << "' loaded successfully" << std::endl;
111
112 return font;
113}
114
115} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Font management system for ImGui.
Font Awesome 6 Free icons for ImGui.
#define FONT_ICON_RANGE_FA_MIN
#define FONT_ICON_RANGE_FA_MAX
Singleton manager for custom fonts.
Definition FontManager.h:24
void Initialize()
Initialize font system Must be called after ImGui context creation but before first frame.
ImFont * LoadFont(const std::string &fontPath, float fontSize, const std::string &fontName="")
Load a custom font.
static FontManager & Get()
Get singleton instance.
bool LoadFontAwesome(const std::string &fontPath, float fontSize=16.0f)
Load Font Awesome font.
< Provides AssetID and INVALID_ASSET_ID
#define SYSTEM_LOG