utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
font.hpp
1/*
2** ETIB PROJECT, 2026
3** utility
4** File description:
5** font
6*/
7
8#pragma once
9
10#include <string>
11#include <map>
12#include <memory>
13#include <functional>
14#include <iostream>
15#include <cstdint>
16#include <vector>
17
18#include <utility/system_io/file.hpp>
19
20#include <utility/graphic/text/font_sized.hpp>
21
22namespace utility::graphic
23{
32 class Font
33 {
34 public:
39 struct FontSizedKey {
44 std::string faceName;
45
49 uint32_t fontSize = 0;
50
59 bool operator<(const FontSizedKey &other) const
60 {
61 if (faceName != other.faceName) {
62 return faceName < other.faceName;
63 }
64 return fontSize < other.fontSize;
65 }
66 };
67
78 Font(const std::vector<File> &fontAssets);
79
86 ~Font();
87
99 float getAscender(uint32_t fontSize) const;
100
112 float getDescender(uint32_t fontSize) const;
113
124 float getLineHeight(uint32_t fontSize) const;
125
143 std::vector<Glyph> processCodePoints(uint32_t fontSize,
144 const codePointString &codePoints);
145
155 math::Vector2F measureText(uint32_t fontSize,
156 const codePointString &codePoints) const;
157
168 std::vector<std::string> getFontPaths(void) const;
169
180 bool isLoaded(void) const;
181
196 bool hasGlyph(char32_t codepoint) const;
197
209 std::function<void(std::string, std::shared_ptr<Texture>)>
211
212 protected:
228 std::string _getFaceNameForGlyph(uint32_t codePoint) const;
229
237
245 std::map<std::string, void *> _faces;
246
260 std::map<FontSizedKey, std::shared_ptr<FontSized>> _sizes;
261
270 std::map<std::string, std::shared_ptr<std::vector<uint8_t>>>
272 };
273} // namespace utility::graphic
The Font class represents a font resource that can be used to render text.
Definition font.hpp:33
std::vector< std::string > getFontPaths(void) const
Retrieves the paths of the loaded font assets.
Definition font.cpp:172
float getLineHeight(uint32_t fontSize) const
Retrieves the line height for the specified font size.
Definition font.cpp:79
std::map< FontSizedKey, std::shared_ptr< FontSized > > _sizes
Map of font sizes to their corresponding FontSized objects.
Definition font.hpp:260
bool hasGlyph(char32_t codepoint) const
Checks if the font contains a glyph for the specified Unicode code point.
Definition font.cpp:193
~Font()
Destructs the Font object, releasing any allocated resources.
Definition font.cpp:44
float getAscender(uint32_t fontSize) const
Retrieves the ascender value for the specified font size.
Definition font.cpp:61
bool isLoaded(void) const
Checks if the font has been successfully loaded.
Definition font.cpp:181
std::string _getFaceNameForGlyph(uint32_t codePoint) const
Retrieves the font face name associated with a specific Unicode code point.
Definition font.cpp:210
std::map< std::string, void * > _faces
Map of font paths to their corresponding FreeType face objects.
Definition font.hpp:245
std::function< void(std::string, std::shared_ptr< Texture >)> onNewTextureCreated
Member function to set a callback that is called when a new texture atlas is created for a font size.
Definition font.hpp:210
std::map< std::string, std::shared_ptr< std::vector< uint8_t > > > _faceBuffers
In-memory buffers backing FreeType faces.
Definition font.hpp:271
float getDescender(uint32_t fontSize) const
Retrieves the descender value for the specified font size.
Definition font.cpp:70
math::Vector2F measureText(uint32_t fontSize, const codePointString &codePoints) const
Measure the bounding size of a string of code points without rasterizing or mutating any glyph atlas.
Definition font.cpp:132
void * _ftLibrary
FreeType library instance used for managing font resources.
Definition font.hpp:236
std::vector< Glyph > processCodePoints(uint32_t fontSize, const codePointString &codePoints)
Processes a string of Unicode code points to generate glyphs for rendering text.
Definition font.cpp:89
3D vector class inheriting from glm::vec3.
Definition vector.hpp:83
std::vector< uint32_t > codePointString
A type representing a string of Unicode code points.
A struct representing a unique key for identifying a specific font face and size combination.
Definition font.hpp:39
uint32_t fontSize
The size of the font in points (e.g., 12, 14, 16).
Definition font.hpp:49
std::string faceName
The name of the font face (e.g., "Arial", "Times New Roman").
Definition font.hpp:44
bool operator<(const FontSizedKey &other) const
Comparison operator to allow FontSizedKey to be used as a key in std::map or other associative contai...
Definition font.hpp:59