Skip to content
realonepiecefreak edited this page Oct 3, 2024 · 3 revisions

Most text-based components allow the setting of a specific font, if it shouldn't use the default font.
The default font of ImGui.Forms and Dear ImGui is ProggyClean at size 13.

Usage

Fonts, other than the default one, have to be registered at the beginning of the application lifetime.
They can be registered from a file or an embedded resource and need to be in the TTF format.

FontFactory.RegisterFromFile("FontName1", @"font/path.ttf");
FontFactory.RegisterFromResource("FontName2", Assembly.GetExecutingAssembly(), "resources.font.ttf");

// If no assembly is given, the calling assembly is assumed
FontFactory.RegisterFromResource("FontName2", "resources.font.ttf");

At any point in the application lifetime, after they were registered, fonts can be retrieved by their registered name with a given size.

label.Font = FontFactory.Get("FontName1", 18);

To retrieve and set the default font:

label.Font = FontFactory.GetDefault(18);

Glyph Ranges

Glyph ranges determine the characters a font is expected to support.
By default, a font is expected to support all glyph ranges.

When registering a font, either by file or by resource, its glyph ranges can be specified:

FontFactory.RegisterFromFile("FontName1", @"font/path.ttf", FontGlyphRange.Latin);
FontFactory.RegisterFromFile("FontName2", @"font/path2.ttf", FontGlyphRange.Latin | FontGlyphRange.Cyrillic);

The following glyph ranges exist, as per Dear ImGui:

  • Latin: Mostly characters supported by the ANSI encoding
  • Cyrillic: All cyrillic letters and symbols
  • ChineseJapaneseKorean: All Hiragana, Katakana, Kanji, Hangul and similar symbol systems
  • Greek: All greek letters and symbols
  • Thai: All thai letters and symbols
  • Vietnamese: All vietnamese letters and symbols
  • Symbols: Punctuations, number symbols, and miscellanious symbols

Additional Characters

For symbols and characters, that are not supported by any given glyph range, a string of additional characters can be passed and will be added to the range of supported glyphs.

FontFactory.RegisterFromFile("FontName1", @"font/path.ttf", FontGlyphRange.All, "❝❞");

Fallbacks

When a font doesn't support a given glyph, Dear ImGui will use the fallback character of that font instead.

However, when getting a font, a fallback font can be specified. It will be used instead to try and render a given glyph.
This can be used to combine multiple, different fonts when they support a different glyph range each.

FontFactory.RegisterFromFile("Roboto", @"roboto.ttf", FontGlyphRange.Latin);
FontFactory.RegisterFromFile("NotoJp", @"notojp.ttf", FontGlyphRange.ChineseJapaneseKorean);

// ...

var latinFallback = FontFactory.Get("Roboto", 14);
FontFactory.Get("NotoJp", 14, latinFallback);

It is recommended to only combine fallback fonts of the same size.

TextMeasurer

Provides methods to calculate the width and height of a given text in a given font.

Usage

To measure the text with the font currently placed on top of the font stack of Dear ImGui:

Vector2 size = TextMeasurer.MeasureText("Text1");

To measure the text with a given font:

Vector2 size = TextMeasurer.MeasureText("Text1", FontFactory.Get("FontName1", 14));

To get the width of a text with the font currently placed on top of the font stack of Dear ImGui:

int height = TextMeasurer.GetCurrentLineWidth("Text1");

To get the width of a text with a given font:

int height = TextMeasurer.GetCurrentLineWidth("Text1", FontFactory.Get("FontName1", 14));

To get the height of a single line with the font currently placed on top of the font stack of Dear ImGui:

int height = TextMeasurer.GetCurrentLineHeight();

To get the height of a single line with a given font:

int height = TextMeasurer.GetCurrentLineHeight(FontFactory.Get("FontName1", 14));
Clone this wiki locally