BakkesMod Programming Wiki
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Custom Fonts

Want to add a custom fancy font to your plugin? It’s really easy! You can use any .ttf font, although many have copyrights. Check out https://fonts.google.com/ for a bunch of free fonts to use.

First, make a pointer to save your font. Do this in your .h as a variable

1
ImFont* myFont;

Next load the font using the GUIManager. This can be done pretty much anywhere, but I recommend using your OnLoad or SetImGuiContext functions. OnLoad happens at startup of the plugin, and SetImGuiContext happens when the plugin’s ImGui is initialized. Either will hopefully ensure your font is loaded before you use it (barring any errors)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
auto gui = gameWrapper->GetGUIManager();

// This syntax requires c++17
// Pick any name you want for the font here
auto [res, font] = gui.LoadFont("font.tff 40px", "font.ttf", 40);

if (res == 0) {
    LOG("Failed to load the font!");
} else if (res == 1) {
    LOG("The font will be loaded");
} else if (res == 2 && font) {
    myFont = font;
}

Finally use the font in your ImGui code. Remember that when you push a font, you have to pop it when you’re done.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// First ensure the font is actually loaded
if (!myFont) {
    auto gui = gameWrapper->GetGUIManager();
    myFont = gui.GetFont("font.tff 40px");
}

// Now use the font
if (myFont) {
    ImGui::PushFont(myFont);
    ImGui::Text("This is using a custom font");
    ImGui::PopFont();
} else {
    ImGui::Text("The custom font haven't been loaded yet");
}

Here’s an example using the open source Ubuntu font, as compared to the ImGui default An example of custom font

The code for this is taken from Martinn’s awesome custom font example. I just made this page to get more visibility for it.


Written by ubelhjCollaborations from Martinn