Skip to main content

FreeTypeFontResourceManager Class

FreeType-based implementation of `FontResourceProvider` for OpenGL. More...

Declaration

class helios::ext::opengl::rendering::FreeTypeFontResourceManager { ... }

Base class

classFontResourceProvider

Abstract interface for loading fonts and providing glyph data. More...

Public Destructor Index

~FreeTypeFontResourceManager () override

Destructor that cleans up all OpenGL textures. More...

Public Member Functions Index

voidloadFont (const helios::engine::core::data::FontId fontId, const unsigned int pixelHeight, const std::string_view pathToFont) override

Loads a font from a file and caches its glyphs as OpenGL textures. More...

helios::rendering::text::Glyphglyph (const char c, const helios::engine::core::data::FontId fontId) override

Retrieves the cached glyph data for a character. More...

Private Member Attributes Index

std::unordered_map< helios::engine::core::data::FontId, FontCache >fontCache_

Map of font IDs to their cached glyph data. More...

Description

FreeType-based implementation of `FontResourceProvider` for OpenGL.

`FreeTypeFontResourceManager` uses the FreeType library to load TrueType fonts, rasterize glyphs, and cache them as OpenGL textures. Each glyph is stored as a separate texture for simplicity.

## Responsibilities

  • **Font Loading:** Parse font files using FreeType and extract glyph metrics.
  • **Texture Creation:** Upload glyph bitmaps to GPU as GL_R8 textures.
  • **Caching:** Store glyph data per font for efficient repeated access.
  • **Resource Cleanup:** Delete OpenGL textures when the manager is destroyed.

## Usage

```cpp FreeTypeFontResourceManager fontManager; fontManager.loadFont(FontId{"roboto"}, "fonts/Roboto-Regular.ttf");

// Retrieve a glyph for rendering Glyph g = fontManager.glyph('A', FontId{"roboto"}); ```

info

Currently loads ASCII characters 0-127 with a fixed pixel size of 24.

See Also

FontResourceProvider

See Also

Glyph

See Also

OpenGLGlyphTextRenderer

Definition at line 60 of file FreeTypeFontResourceManager.ixx.

Public Destructor

~FreeTypeFontResourceManager()

helios::ext::opengl::rendering::FreeTypeFontResourceManager::~FreeTypeFontResourceManager ()
inline

Destructor that cleans up all OpenGL textures.

Iterates through all cached fonts and deletes the associated glyph textures to free GPU memory.

Definition at line 88 of file FreeTypeFontResourceManager.ixx.

89
90 // Cleanup texture memory for all loaded fonts
91 for (const auto& cache: fontCache_ | std::views::values) {
92 for (const auto& glyph : cache.characters) {
93 // glDeleteTextures ignores 0, so this is safe even if textureId is uninitialized (0)
94 glDeleteTextures(1, &glyph.textureId);
95 }
96 }
97 }

Reference glyph.

Public Member Functions

glyph()

helios::rendering::text::Glyph helios::ext::opengl::rendering::FreeTypeFontResourceManager::glyph (const char c, const helios::engine::core::data::FontId fontId)
inline virtual

Retrieves the cached glyph data for a character.

Returns the glyph metrics and texture ID for the specified character from the font's cache. The font must have been previously loaded via `loadFont()`.

Parameters
c

The character to retrieve the glyph for.

fontId

The font identifier to use.

Returns

The cached `Glyph` data for the character.

Precondition

The font must already be loaded (asserts in debug builds).

Definition at line 197 of file FreeTypeFontResourceManager.ixx.

198 const char c,
200 ) override {
201
202 assert(fontCache_.contains(fontId) && "Font cache entry not found for given FontId (font not loaded?)");
203
204 return fontCache_[fontId].characters[c];
205 }

Referenced by ~FreeTypeFontResourceManager.

loadFont()

void helios::ext::opengl::rendering::FreeTypeFontResourceManager::loadFont (const helios::engine::core::data::FontId fontId, const unsigned int pixelHeight, const std::string_view pathToFont)
inline virtual

Loads a font from a file and caches its glyphs as OpenGL textures.

Uses FreeType to parse the font file and rasterize ASCII characters 0-127. Each glyph bitmap is uploaded to the GPU as a GL_R8 texture.

Parameters
fontId

Unique identifier for this font.

pixelHeight

The height of the font, in pixels.

pathToFont

File system path to the font file (e.g., `.ttf`).

Exceptions
std::runtime_error

If FreeType initialization fails.

std::runtime_error

If the font file cannot be loaded.

std::runtime_error

If a glyph cannot be loaded.

Precondition

The font ID must not already be loaded (asserts in debug builds).

Definition at line 115 of file FreeTypeFontResourceManager.ixx.

117 const unsigned int pixelHeight,
118 const std::string_view pathToFont
119 ) override {
120
121 const int begin = 0;
122 const int end = 128;
123
124 assert(!fontCache_.contains(fontId) && "FontCache already existing");
125
126 fontCache_.emplace(fontId, FontCache{});
127
128 FT_Library ft{};
129
130 FT_Face face{};
131
132 if (FT_Init_FreeType(&ft)) {
133 throw std::runtime_error("ERROR::FREETYPE: Could not init FreeType Library");
134
135 }
136
137 if (FT_New_Face(ft, pathToFont.data(), 0, &face)) {
138 throw std::runtime_error("ERROR::FREETYPE: Failed to load font");
139 }
140
141 FT_Set_Pixel_Sizes(face, 0, pixelHeight);
142 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
143 glPixelStorei(GL_UNPACK_ROW_LENGTH, face->glyph->bitmap.pitch);
144
145 for (unsigned short c = begin; c < end; ++c) {
146
147 if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
148 throw std::runtime_error("ERROR::FREETYTPE: Failed to load Glyph");
149 }
150
151 unsigned int texture;
152 glGenTextures(1, &texture);
153 glBindTexture(GL_TEXTURE_2D, texture);
154 glTexImage2D(
155 GL_TEXTURE_2D,
156 0,
157 GL_R8,
158 face->glyph->bitmap.width,
159 face->glyph->bitmap.rows,
160 0,
161 GL_RED,
162 GL_UNSIGNED_BYTE,
163 face->glyph->bitmap.buffer
164 );
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
169
170 fontCache_[fontId].characters[c - begin] = {
171 texture,
172 {face->glyph->bitmap.width, face->glyph->bitmap.rows},
173 {face->glyph->bitmap_left, face->glyph->bitmap_top},
174 face->glyph->advance.x
175 };
176
177 }
178
179 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
180 FT_Done_Face(face);
181 FT_Done_FreeType(ft);
182 };

Private Member Attributes

fontCache_

std::unordered_map< helios::engine::core::data::FontId, FontCache > helios::ext::opengl::rendering::FreeTypeFontResourceManager::fontCache_

Map of font IDs to their cached glyph data.

Definition at line 77 of file FreeTypeFontResourceManager.ixx.

77 > fontCache_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.