cangine
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
cangine::TextRenderer Class Reference

Singleton that loads fonts and renders text via FreeType. More...

#include <text_renderer.hpp>

Collaboration diagram for cangine::TextRenderer:
Collaboration graph
[legend]

Public Member Functions

void clearGlyphCache (Font *font)
 Clears the cached glyphs for a font.
 
Font * loadFont (std::string path, int height=40)
 Loads a font from disk (or returns the cached one).
 
void setFontHeight (Font *font, int heightIn)
 Changes a font's pixel height and clears its glyph cache.
 
bool loadGlyph (Font *font, uint32_t cp)
 Rasterizes and caches a single glyph.
 
void renderText (Font *font, const std::string &text, Vector3 pos, float scale, const Color &color)
 Draws a text string at a position.
 
Vector2 measureText (Font *font, const std::string &text, float scale)
 Measures the rendered size of a text string.
 
 TextRenderer ()
 Constructs the renderer and initializes FreeType.
 
 ~TextRenderer ()
 Releases FreeType resources.
 

Static Public Member Functions

static TextRenderer * getInstance ()
 Returns the singleton instance.
 

Public Attributes

bool valid
 Whether the renderer initialized successfully.
 

Private Member Functions

bool init_freetype ()
 Initializes the FreeType library.
 

Private Attributes

FT_Library ft
 FreeType library handle.
 
std::unordered_map< std::string, std::unique_ptr< Font > > fonts
 Loaded fonts keyed by path.
 

Static Private Attributes

static TextRenderer * instance = nullptr
 Singleton instance.
 

Detailed Description

Singleton that loads fonts and renders text via FreeType.

Rasterizes glyphs on demand, caches them per Font, and draws text as textured quads.

Constructor & Destructor Documentation

◆ TextRenderer()

cangine::TextRenderer::TextRenderer ( )

Constructs the renderer and initializes FreeType.

◆ ~TextRenderer()

cangine::TextRenderer::~TextRenderer ( )

Releases FreeType resources.

Member Function Documentation

◆ clearGlyphCache()

void cangine::TextRenderer::clearGlyphCache ( Font *  font)

Clears the cached glyphs for a font.

Parameters
fontFont whose glyph cache should be emptied.

◆ getInstance()

TextRenderer * cangine::TextRenderer::getInstance ( )
static

Returns the singleton instance.

Returns
Pointer to the TextRenderer instance.

◆ init_freetype()

bool cangine::TextRenderer::init_freetype ( )
private

Initializes the FreeType library.

Returns
true on success, false on failure.

◆ loadFont()

Font * cangine::TextRenderer::loadFont ( std::string  path,
int  height = 40 
)

Loads a font from disk (or returns the cached one).

Parameters
pathPath to the font file.
heightPixel height to render the font at.
Returns
Pointer to the loaded font, or nullptr on failure.

◆ loadGlyph()

bool cangine::TextRenderer::loadGlyph ( Font *  font,
uint32_t  cp 
)

Rasterizes and caches a single glyph.

Parameters
fontFont to load the glyph for.
cpUnicode codepoint of the glyph.
Returns
true if the glyph was loaded successfully.

◆ measureText()

Vector2 cangine::TextRenderer::measureText ( Font *  font,
const std::string &  text,
float  scale = 1.0f 
)

Measures the rendered size of a text string.

Parameters
fontFont that would render the text.
textUTF-8 text to measure.
scaleScale factor applied to the glyphs.
Returns
The width and height the text would occupy.

◆ renderText()

void cangine::TextRenderer::renderText ( Font *  font,
const std::string &  text,
Vector3  pos,
float  scale,
const Color &  color 
)

Draws a text string at a position.

Parameters
fontFont to render with.
textUTF-8 text to draw.
posBaseline position (z is the layer).
scaleScale factor applied to the glyphs.
colorText color.

◆ setFontHeight()

void cangine::TextRenderer::setFontHeight ( Font *  font,
int  heightIn 
)

Changes a font's pixel height and clears its glyph cache.

Parameters
fontFont to modify.
heightInNew pixel height.

Member Data Documentation

◆ fonts

std::unordered_map<std::string, std::unique_ptr<Font> > cangine::TextRenderer::fonts
private

Loaded fonts keyed by path.

◆ ft

FT_Library cangine::TextRenderer::ft
private

FreeType library handle.

◆ instance

TextRenderer * cangine::TextRenderer::instance = nullptr
staticprivate

Singleton instance.

◆ valid

bool cangine::TextRenderer::valid

Whether the renderer initialized successfully.


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