galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Font.cpp
Go to the documentation of this file.
1
7
8#include "galaxy/core/ServiceLocator.hpp"
10#include "galaxy/utils/Globals.hpp"
11
12#include "Font.hpp"
13
14namespace galaxy
15{
16 namespace graphics
17 {
19 : m_font {nullptr}
20 , m_face {nullptr}
21 {
22 }
23
25 {
26 if (this->m_font != nullptr)
27 {
28 msdfgl_destroy_font(this->m_font);
29 }
30
31 this->m_font = f.m_font;
32 this->m_face = f.m_face;
33
34 f.m_font = nullptr;
35 f.m_face = nullptr;
36 }
37
39 {
40 if (this != &f)
41 {
42 if (this->m_font != nullptr)
43 {
44 msdfgl_destroy_font(this->m_font);
45 }
46
47 this->m_font = f.m_font;
48 this->m_face = f.m_face;
49
50 f.m_font = nullptr;
51 f.m_face = nullptr;
52 }
53
54 return *this;
55 }
56
58 {
59 if (m_font != nullptr)
60 {
61 msdfgl_destroy_font(m_font);
62
63 m_face = nullptr;
64 }
65 }
66
67 bool Font::load(const std::string& file)
68 {
69 auto result = false;
70 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
71
72 auto data = fs.read_binary(file);
73 if (!data.empty())
74 {
75 auto& fc = core::ServiceLocator<FontContext>::ref();
76 m_face = msdfgl_load_font_mem(fc.context(), data.data(), data.size());
77
78 result = true;
79 }
80 else
81 {
82 GALAXY_LOG(GALAXY_ERROR, "Failed to read font '{0}'.", file);
83 }
84
85 return result;
86 }
87
88 bool Font::load(unsigned char* buffer, const unsigned int size)
89 {
90 auto result = false;
91
92 if (buffer && size > 0)
93 {
94 auto& fc = core::ServiceLocator<FontContext>::ref();
95 m_face = msdfgl_load_font_mem(fc.context(), buffer, size);
96
97 result = true;
98 }
99 else
100 {
101 GALAXY_LOG(GALAXY_ERROR, "Failed to load font from memory due to empty or null buffer");
102 }
103
104 return result;
105 }
106
108 {
109 auto& fc = core::ServiceLocator<FontContext>::ref();
110
111 m_font = _msdfgl_init_font_internal(fc.context(), &m_face, GALAXY_FONT_MSDF_RANGE, GALAXY_FONT_MSDF_SCALE, nullptr);
112 if (!m_font)
113 {
114 GALAXY_LOG(GALAXY_FATAL, "Failed to build font atlas for {0}.", m_face->family_name);
115 }
116 else
117 {
118 msdfgl_generate_ascii_ext(m_font);
119 }
120
121 // NOW OWNED BY MSDFGL
122 m_face = nullptr;
123 }
124
125 float Font::vertical_advance(const float size) const
126 {
127 return msdfgl_vertical_advance(m_font, size);
128 }
129
130 glm::vec2 Font::get_text_size(const std::string& text, const float size)
131 {
132 glm::vec2 vec;
133
134 std::size_t start = 0;
135 std::size_t end = text.find('\n');
136
137 while (end != std::string::npos)
138 {
139 vec.y += msdfgl_vertical_advance(m_font, size);
140
141 const auto block = text.substr(start, end);
142
143 float x = 0.0f, y = 0.0f;
144 msdfgl_geometry(&x, &y, m_font, size, static_cast<msdfgl_printf_flags>(MSDFGL_UTF8 | MSDFGL_KERNING), block.c_str());
145
146 if (x > vec.x)
147 {
148 vec.x = x;
149 }
150
151 start = end + 1;
152 end = text.find('\n', start);
153 }
154
155 const auto last_block = text.substr(start, end);
156
157 float x = 0.0f, y = 0.0f;
158 msdfgl_geometry(&x, &y, m_font, size, static_cast<msdfgl_printf_flags>(MSDFGL_UTF8 | MSDFGL_KERNING), last_block.c_str());
159
160 if (x > vec.x)
161 {
162 vec.x = x;
163 }
164
165 vec.y += msdfgl_vertical_advance(m_font, size);
166
167 return vec;
168 }
169
170 msdfgl_font_t Font::handle()
171 {
172 return m_font;
173 }
174 } // namespace graphics
175} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_FATAL
Definition Log.hpp:25
#define GALAXY_ERROR
Definition Log.hpp:24
A font is a MSDF atlas of glyphs.
Definition Font.hpp:25
Font & operator=(Font &&)
Move assignment operator.
Definition Font.cpp:38
msdfgl_font_t handle()
Get handle to internal font object.
Definition Font.cpp:170
msdfgl_font_t m_font
Font object.
Definition Font.hpp:110
Font()
Constructor.
Definition Font.cpp:18
void build()
Build the font atlas.
Definition Font.cpp:107
bool load(const std::string &file)
Loads the font and sets up characters.
Definition Font.cpp:67
FT_Face m_face
Freetype face.
Definition Font.hpp:117
~Font()
Destructor.
Definition Font.cpp:57
glm::vec2 get_text_size(const std::string &text, const float size)
Query text size with font.
Definition Font.cpp:130
float vertical_advance(const float size) const
Get vertical advance for drawing multiline text.
Definition Font.cpp:125
Animated.cpp galaxy.
Definition Animated.cpp:16