Font - Horizon-NTH/HorizonGUI GitHub Wiki
The Font
class in the hgui::kernel
namespace is responsible for managing fonts in your application.
It provides the ability to load and use fonts from a specified font file. This class is particularly useful for
rendering text with different fonts and sizes. The Font
class operates in conjunction with the Character
structure,
which encapsulates information about individual characters of the loaded font. The class can be found in the header file
Font.h
and uses Freetype to load font.
The Character
structure is used to represent individual characters loaded from a font file.
It provides information about the character's rendering properties and information about the glyphs.
-
std::shared_ptr<Texture> texture
: The texture associated with the character. This texture is used when rendering the character to the screen. It contains the character's glyph or image. -
hgui::size size
: The size of the character. -
ivec2 bearing
: The bearing, represented as an instance of ivec2. The bearing is an offset from the baseline to the top-left corner of the character. It is important for positioning the character correctly in text rendering. -
unsigned int advance
: The advance value represents how far to move the cursor horizontally after rendering this character. It indicates the distance between the origin and the next character's origin.
Note: If you want to learn more about glyph, read this
-
explicit Font(std::string fontPath)
: Constructs aFont
object, specifying the file path of the font to be loaded.
-
Character get_char(char character, unsigned int size) const
: Retrieves aCharacter
struct for the specified character and font size. -
std::string get_printable_characters(unsigned int size) const
: Retrieves all the printable characters that are available in theFont
. -
void load_font(unsigned int size)
: Loads the font for a specific size. The loaded font can be accessed using theget_char
function. -
bool is_load(unsigned int size) const
: Checks if the font for a specific size is loaded and ready for use.
#include <hgui/header/Font.h>
// Example Usage of Font class
hgui::kernel::Font font("path/to/your/font.ttf");
// Load fonts for various sizes
font.load_font(12);
font.load_font(18);
// Check if a font is loaded
if (font.is_load(18))
{
// Render text using the loaded font
hgui::kernel::Character charInfo = font.get_char('A', 18);
// Retrieve texture, size, bearing, and advance
std::shared_ptr<Texture> texture = charInfo.texture;
hgui::size charSize = charInfo.size;
ivec2 bearing = charInfo.bearing;
unsigned int advance = charInfo.advance;
}
Note: In the "Example Usage" section, you should load fonts for various sizes as needed, and then check if a font is loaded for a specific size using
is_load
. Once loaded, you can retrieve character information usingget_char
, which provides details such as texture, size, bearing, and advance.