lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
font.hpp
Go to the documentation of this file.
1
11
12#pragma once
13
15
16#include <glm/glm.hpp>
17#include <memory>
18#include <string>
19#include <unordered_map>
20
21namespace lmgl {
22
23namespace ui {
24
30struct Glyph {
32 glm::vec2 tex_coord_min;
33
35 glm::vec2 tex_coord_max;
36
38 glm::vec2 size;
39
41 glm::vec2 bearing;
42
44 float advance;
45};
46
53class Font {
54 public:
61 Font(const std::string &filepath, unsigned int font_size = 48);
62
66 ~Font() = default;
67
74 const Glyph &get_glyph(char c) const;
75
81 inline std::shared_ptr<renderer::Texture> get_atlas() const { return m_atlas; }
82
88 inline unsigned int get_font_size() const { return m_font_size; }
89
95 inline float get_line_height() const { return m_line_height; }
96
103 float measure_text(const std::string &text) const;
104
105 private:
107 unsigned int m_font_size;
108
110 float m_line_height;
111
113 std::shared_ptr<renderer::Texture> m_atlas;
114
116 std::unordered_map<char, Glyph> m_glyphs;
117
123 void generate_atlas(const std::string &filepath);
124};
125
131class FontManager {
132 public:
138 static FontManager &get();
139
148 std::shared_ptr<Font> load(const std::string &name, const std::string &filepath, unsigned int font_size = 48);
149
156 std::shared_ptr<Font> get_font(const std::string &name);
157
164 bool exists(const std::string &name) const;
165
169 void clear();
170
171 private:
172 FontManager() = default;
173 ~FontManager() = default;
174
176 static std::unordered_map<std::string, std::shared_ptr<Font>> s_fonts;
177};
178
179} // namespace ui
180
181} // namespace lmgl
void clear()
Clear all cached fonts.
Definition font.cpp:193
std::shared_ptr< Font > load(const std::string &name, const std::string &filepath, unsigned int font_size=48)
Load a font or get it from cache.
Definition font.cpp:164
static FontManager & get()
Get the singleton instance.
Definition font.cpp:159
std::shared_ptr< Font > get_font(const std::string &name)
Get a previously loaded font.
Definition font.cpp:181
bool exists(const std::string &name) const
Check if a font exists in the cache.
Definition font.cpp:189
Font(const std::string &filepath, unsigned int font_size=48)
Load a font from file.
Definition font.cpp:23
~Font()=default
Destructor.
std::shared_ptr< renderer::Texture > get_atlas() const
Get the texture atlas containing all glyphs.
Definition font.hpp:81
const Glyph & get_glyph(char c) const
Get glyph metadata for a character.
Definition font.cpp:137
unsigned int get_font_size() const
Get the font size in pixels.
Definition font.hpp:88
float get_line_height() const
Get the line height for this font.
Definition font.hpp:95
float measure_text(const std::string &text) const
Calculate the width of a text string in pixels.
Definition font.cpp:147
Definition button.cpp:15
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12
Glyph metadata for a single character.
Definition font.hpp:30
float advance
Horizontal offset to advance to next glyph.
Definition font.hpp:44
glm::vec2 bearing
Offset from baseline to top-left of glyph.
Definition font.hpp:41
glm::vec2 tex_coord_max
UV coordinates in texture atlas (max).
Definition font.hpp:35
glm::vec2 size
Size of glyph in pixels.
Definition font.hpp:38
glm::vec2 tex_coord_min
UV coordinates in texture atlas (min).
Definition font.hpp:32
Texture class for managing OpenGL textures.