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
utility::graphic::Font Class Reference

The Font class represents a font resource that can be used to render text. More...

#include <headers/utility/graphic/text/font.hpp>

Classes

struct  FontSizedKey
 A struct representing a unique key for identifying a specific font face and size combination. More...
 

Public Member Functions

 Font (const std::vector< File > &fontAssets)
 Constructs a Font object by loading font data from the provided file assets.
 
 ~Font ()
 Destructs the Font object, releasing any allocated resources.
 
float getAscender (uint32_t fontSize) const
 Retrieves the ascender value for the specified font size.
 
float getDescender (uint32_t fontSize) const
 Retrieves the descender value for the specified font size.
 
float getLineHeight (uint32_t fontSize) const
 Retrieves the line height for the specified font size.
 
std::vector< GlyphprocessCodePoints (uint32_t fontSize, const codePointString &codePoints)
 Processes a string of Unicode code points to generate glyphs for rendering text.
 
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.
 
std::vector< std::string > getFontPaths (void) const
 Retrieves the paths of the loaded font assets.
 
bool isLoaded (void) const
 Checks if the font has been successfully loaded.
 
bool hasGlyph (char32_t codepoint) const
 Checks if the font contains a glyph for the specified Unicode code point.
 

Public Attributes

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.
 

Protected Member Functions

std::string _getFaceNameForGlyph (uint32_t codePoint) const
 Retrieves the font face name associated with a specific Unicode code point.
 

Protected Attributes

void * _ftLibrary
 FreeType library instance used for managing font resources.
 
std::map< std::string, void * > _faces
 Map of font paths to their corresponding FreeType face objects.
 
std::map< FontSizedKey, std::shared_ptr< FontSized > > _sizes
 Map of font sizes to their corresponding FontSized objects.
 
std::map< std::string, std::shared_ptr< std::vector< uint8_t > > > _faceBuffers
 In-memory buffers backing FreeType faces.
 

Detailed Description

The Font class represents a font resource that can be used to render text.

The Font class manages font faces and sizes, allowing you to retrieve glyph information for rendering text. It uses the FreeType library to load and manage font data.

Definition at line 32 of file font.hpp.

Constructor & Destructor Documentation

◆ Font()

utility::graphic::Font::Font ( const std::vector< File > &  fontAssets)

Constructs a Font object by loading font data from the provided file assets.

Parameters
fontAssetsA vector of File objects containing the font data to load.
Exceptions
std::runtime_errorif the FreeType library cannot be initialized or if any of the font assets cannot be loaded.

Definition at line 18 of file font.cpp.

◆ ~Font()

utility::graphic::Font::~Font ( )

Destructs the Font object, releasing any allocated resources.

This destructor cleans up the FreeType library and any loaded font faces to prevent memory leaks.

Definition at line 44 of file font.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ _getFaceNameForGlyph()

std::string utility::graphic::Font::_getFaceNameForGlyph ( uint32_t  codePoint) const
protected

Retrieves the font face name associated with a specific Unicode code point.

This method determines which font face contains the glyph for the given Unicode code point and returns the corresponding font path. It is used internally to manage multiple font faces and ensure that the correct glyphs are retrieved for rendering text.

Parameters
codePointThe Unicode code point for which to retrieve the associated font face name.
Returns
A string containing the path of the font face associated with the specified Unicode code point.

Definition at line 210 of file font.cpp.

Here is the caller graph for this function:

◆ getAscender()

float utility::graphic::Font::getAscender ( uint32_t  fontSize) const

Retrieves the ascender value for the specified font size.

The ascender value represents the distance from the baseline to the highest point of the font. It is used in text layout calculations to determine the vertical positioning of text.

Parameters
fontSizeThe font size for which to retrieve the ascender value.
Returns
The ascender value for the specified font size.

Definition at line 61 of file font.cpp.

◆ getDescender()

float utility::graphic::Font::getDescender ( uint32_t  fontSize) const

Retrieves the descender value for the specified font size.

The descender value represents the distance from the baseline to the lowest point of the font. It is used in text layout calculations to determine the vertical positioning of text.

Parameters
fontSizeThe font size for which to retrieve the descender value.
Returns
The descender value for the specified font size.

Definition at line 70 of file font.cpp.

◆ getFontPaths()

std::vector< std::string > utility::graphic::Font::getFontPaths ( void  ) const

Retrieves the paths of the loaded font assets.

This method returns a vector of strings containing the paths of the font assets that were loaded into this Font object. The paths are extracted from the File objects used to initialize the Font.

Returns
A const reference to a vector of strings containing the paths of the loaded font assets.

Definition at line 172 of file font.cpp.

◆ getLineHeight()

float utility::graphic::Font::getLineHeight ( uint32_t  fontSize) const

Retrieves the line height for the specified font size.

The line height represents the vertical distance between lines of text. It is used in text layout calculations to determine the spacing between lines of text.

Parameters
fontSizeThe font size for which to retrieve the line height.
Returns
The line height for the specified font size.

Definition at line 79 of file font.cpp.

◆ hasGlyph()

bool utility::graphic::Font::hasGlyph ( char32_t  codepoint) const

Checks if the font contains a glyph for the specified Unicode code point.

This method checks if the font has a glyph corresponding to the given Unicode code point. It can be used to determine if a particular character can be rendered using this font.

Parameters
codepointThe Unicode code point to check for a corresponding glyph in the font.
Returns
true if the font contains a glyph for the specified code point, false otherwise.

Definition at line 193 of file font.cpp.

Here is the call graph for this function:

◆ isLoaded()

bool utility::graphic::Font::isLoaded ( void  ) const

Checks if the font has been successfully loaded.

This method checks if the FreeType library was initialized and if at least one font face was loaded. It returns true if the font is ready to be used for rendering, and false otherwise.

Returns
true if the font is loaded and ready for use, false otherwise.

Definition at line 181 of file font.cpp.

Here is the caller graph for this function:

◆ measureText()

math::Vector2F utility::graphic::Font::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.

This is a side-effect-free measurement based on glyph metrics.

Parameters
fontSizeThe font size to measure with.
codePointsThe code points to measure.
Returns
The width and height (in pixels) the text would occupy.

Definition at line 132 of file font.cpp.

Here is the call graph for this function:

◆ processCodePoints()

std::vector< Glyph > utility::graphic::Font::processCodePoints ( uint32_t  fontSize,
const codePointString codePoints 
)

Processes a string of Unicode code points to generate glyphs for rendering text.

This method takes a string of Unicode code points and generates the corresponding glyphs for rendering text. It uses the FreeType library to load and render the glyphs, and it returns a vector of Glyph objects containing the necessary information for rendering each character.

Parameters
fontSizeThe font size to use for generating the glyphs.
codePointsA string of Unicode code points representing the characters for which to generate glyphs.
Returns
A vector of Glyph objects containing the information needed to render each character in the input string.

Definition at line 89 of file font.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ _faceBuffers

std::map<std::string, std::shared_ptr<std::vector<uint8_t> > > utility::graphic::Font::_faceBuffers
protected

In-memory buffers backing FreeType faces.

FreeType's memory-face loader does not copy the provided bytes, so these buffers must remain alive for as long as the corresponding face exists. Buffers are heap-pinned via shared_ptr to guarantee stable storage.

Definition at line 271 of file font.hpp.

◆ _faces

std::map<std::string, void *> utility::graphic::Font::_faces
protected

Map of font paths to their corresponding FreeType face objects.

Faces are stored as opaque handles to keep FreeType types out of the public API.

Definition at line 245 of file font.hpp.

◆ _ftLibrary

void* utility::graphic::Font::_ftLibrary
protected

FreeType library instance used for managing font resources.

Stored as an opaque handle to keep FreeType types out of the public API.

Definition at line 236 of file font.hpp.

◆ _sizes

std::map<FontSizedKey, std::shared_ptr<FontSized> > utility::graphic::Font::_sizes
protected

Map of font sizes to their corresponding FontSized objects.

This map stores FontSized objects for different font sizes, allowing the Font class to manage multiple sizes of the same font. Each entry maps a font size (as a uint32_t) to its corresponding FontSized object.

FontSized objects are created on demand when the getSize method is called, and they are stored in this map for future retrieval. The Font class is responsible for managing the lifetime of these FontSized objects to ensure proper resource management.

Definition at line 260 of file font.hpp.

◆ onNewTextureCreated

std::function<void(std::string, std::shared_ptr<Texture>)> utility::graphic::Font::onNewTextureCreated

Member function to set a callback that is called when a new texture atlas is created for a font size.

This callback function is invoked whenever a new texture atlas is generated for a specific font size. It allows the caller to receive the path of the font face associated with the new atlas and a shared pointer to the Texture object representing the atlas. This can be useful for updating rendering resources or performing additional processing when new glyphs are added to the atlas.

Definition at line 210 of file font.hpp.


The documentation for this class was generated from the following files: