FontManager¶
Overview¶
-
class FontManager¶
Font manager for handling multiple fonts and sizes in ImGui.
The
omdi::FontManagerclass manages a set of ImGui fonts (and optional icon fonts) grouped by name and size. It provides:Registration of fonts from memory or from files.
A fixed set of logical sizes (e.g. Small, Normal, Medium, Large, Huge).
Access to fonts and icon fonts by name and size.
Tracking of the currently active font and size.
Helper functions to build and clear the ImGui font atlas.
Internally, it uses maps from font names to size-specific
ImFontpointers, and integrates withImGuiIOfor font configuration.The available sizes are stored in
omdi::FontManager::SIZES, which typically containsSmall,Normal,Medium,Large, andHuge.
Usage example¶
Basic usage:
auto fontManager = omdi::FontManager();
auto managers = omdi::managers_t {
{ "font_manager", &fontManager },
// ... other managers
};
app.Init(&state, managers);
app.Render(&state, [&]() {
// render callback
},
components,
managers);
Advanced usage to load custom fonts:
auto fontManager = omdi::FontManager();
// add from a ttf file
fontManager.add(&ImGui::GetIO(), "CustomFont", omdi::fonts::Normal, "path/to/font.ttf", nullptr);
fontManager.build(&ImGui::GetIO());
// size_index = 2 corresponds to 'Normal' (16px)
fontManager.setActiveFont(&ImGui::GetIO(), /*font_index=*/0, /*size_index=*/2);
Public interface¶
Sizes and active font state¶
-
std::vector<Size> FontManager::SIZES¶
List of supported font sizes.
Typically contains logical size values such as
Small,Normal,Medium,Large, andHuge. The exactSizevalues are determined by your application.
-
std::string FontManager::active_font() const¶
Get the name of the currently active font.
- Returns:
Name of the active font, or an empty string if none is active.
-
int FontManager::active_font_idx() const¶
Get the index of the currently active font in the internal font list.
- Returns:
Zero-based index of the active font, or
-1if none is active.
-
Size FontManager::active_font_size() const¶
Get the currently active font size.
- Returns:
The active
Sizevalue, orSize::Noneif no size is active.
Font and icon access¶
-
ImFont *FontManager::font(const std::string &name, Size size)¶
Retrieve a font by name and size.
- Parameters:
name – Font family/name identifier (e.g.
"Roboto").size – Logical size (e.g.
Size::Normal).
- Returns:
Pointer to the corresponding
ImFont, ornullptrif the font/size combination is not available.
-
ImFont *FontManager::icon(const std::string &name, Size size)¶
Retrieve an icon font by name and size.
- Parameters:
name – Icon font identifier (e.g.
"FontAwesome"or similar).size – Logical size.
- Returns:
Pointer to the corresponding
ImFontused for icons, ornullptrif not available.
-
std::vector<const char*> FontManager::fontnames() const¶
Get a list of available font names.
- Returns:
A vector of C-string pointers, each representing a font name. The lifetime of these strings is tied to the
omdi::FontManagerinstance.
Initialization and configuration¶
-
void FontManager::initFonts(ImGuiIO *io)¶
Initialize font-related data using the given ImGui IO object.
- Parameters:
io – Pointer to
ImGuiIOused by the current ImGui context.
This function typically prepares internal structures and may configure default fonts or font settings before fonts are added.
-
void FontManager::setActiveFont(ImGuiIO *io, int font_index, int size_index)¶
Set the active font and size, and update ImGui to use them.
- Parameters:
io – Pointer to
ImGuiIOfor the current ImGui context.font_index – Index into the internal font list (e.g. as returned by
omdi::FontManager::fontnames()).size_index – Index into
omdi::FontManager::SIZES.
This function updates
ImGui::GetFont()/ ImGui style to use the selected font/size combination as the main UI font.
Managing the font atlas¶
-
void FontManager::clear(ImGuiIO *io) const¶
Clear fonts from the ImGui IO font atlas.
- Parameters:
io – Pointer to
ImGuiIO.
Clears font data from the ImGui font atlas. Typically used before rebuilding or reconfiguring fonts.
-
void FontManager::build(ImGuiIO *io) const¶
Build the ImGui font atlas after fonts have been added.
- Parameters:
io – Pointer to
ImGuiIO.
This calls the underlying ImGui font-building routines to bake the registered fonts into a texture atlas. Must be called after all desired fonts are added, and before rendering with them.
Adding fonts¶
-
void FontManager::add(ImGuiIO *io, const std::string &name, Size size, const void *data, int data_size, const ImWchar *glyph_ranges = nullptr)¶
Add a font from in-memory data.
- Parameters:
io – Pointer to
ImGuiIO.name – Font name identifier (e.g.
"Roboto").size – Logical font size.
data – Pointer to the font data (e.g. TTF bytes in memory).
data_size – Size of data in bytes.
glyph_ranges – Optional ImGui glyph range (see ImGui font API).
- Returns:
(none)
Use this to load fonts that are bundled directly in your binary or loaded into memory by other means.
-
void FontManager::add(ImGuiIO *io, const std::string &name, Size size, const std::string &filename, const ImWchar *glyph_ranges = nullptr)¶
Add a font from a file.
- Parameters:
io – Pointer to
ImGuiIO.name – Font name identifier.
size – Logical font size.
filename – Path to the font file (e.g. TTF/OTF).
glyph_ranges – Optional ImGui glyph range.
- Returns:
(none)
This overload loads font data from disk and registers it under the given name and size.
Notes¶
omdi::FontManagerdoes not own theImGuiIOobject; it only uses it to configure fonts and build the font atlas.After adding or changing fonts via
omdi::FontManager::add(), you should callomdi::FontManager::build()to rebuild the atlas.The
Sizetype is assumed to be defined elsewhere (e.g. in the same namespace) with values such asSmall,Normal,Medium,Large,Huge, andNone.