Skip to main content

Overview

Dear ImGui manages fonts through ImFontAtlas, which loads and rasterizes multiple TTF/OTF fonts into a single texture. Since version 1.92.0, fonts can be rendered at any size dynamically.
ImFontAtlas automatically loads a default embedded font if you didn’t load one manually.

Adding Fonts

AddFontDefault

Add default font. Selects between embedded vector font (recommended) and bitmap font.
const ImFontConfig*
default:"NULL"
Optional font configuration
ImFont*
Pointer to loaded font
Example:

AddFontFromFileTTF

Load font from TTF/OTF file.
const char*
Path to .ttf or .otf file
float
default:"0.0f"
Legacy base size. Use 0.0f for default size (13px), or specify a size for legacy behavior.
const ImFontConfig*
default:"NULL"
Optional font configuration
const ImWchar*
default:"NULL"
Glyph ranges to load (NULL = default ranges)
ImFont*
Pointer to loaded font, or NULL on error
Example:

AddFontFromMemoryTTF

Load font from memory. Note: Transfers ownership of font_data to ImFontAtlas!
void*
Font data in memory (ownership transferred unless FontDataOwnedByAtlas=false)
int
Size of font data in bytes
By default, AddFontFromMemoryTTF() takes ownership of the data and will free it. Set font_cfg->FontDataOwnedByAtlas = false to keep ownership.

AddFontFromMemoryCompressedTTF

Load font from compressed data. Data is still owned by caller.

Font Configuration

ImFontConfig Structure

Example with configuration:

Font Merging

Merge multiple fonts into one to combine different Unicode ranges:

Glyph Ranges

GetGlyphRangesDefault

Get Basic Latin + Extended Latin glyph ranges.

Other Glyph Ranges

Example:

Custom Glyph Ranges

ImFontGlyphRangesBuilder

Build custom glyph ranges from text:

Using Fonts

PushFont

Set current font. In 1.92+, you must specify both font and size.
ImFont*
Font to use (NULL = keep current font)
float
Base font size before global scaling (0.0f = keep current size)
Example:

PopFont

Restore previous font.

GetFont

Get current font.

GetFontSize

Get current scaled font size (height in pixels). Do not pass this to PushFont()!

ImFont Structure

Text Measurement

CalcTextSize

Calculate text size at current font size.
const char*
Text to measure
const char*
default:"NULL"
End of text (NULL for null-terminated)
bool
default:"false"
Hide text after ##
float
default:"-1.0f"
Width for text wrapping (-1.0f = no wrapping)
ImVec2
Text size in pixels
Example:

ImFontAtlas

Custom Rectangles

Add custom graphics to font atlas:
Example:

Font Scaling

Global Font Scaling

Per-Font Size (1.92+)

Common Patterns

Multiple Font Sizes

Icon Fonts

ShowFontSelector

Add font selector block (not a window), essentially a combo listing loaded fonts. Example:

See Also

  • Style - Font size and scaling in style
  • Text - Text rendering functions