Imgui: Load font for ImGui::Begin

Created on 28 Oct 2017  路  13Comments  路  Source: ocornut/imgui

Dear Ocornut,

I'm using your lib with DirectX9.
I have the case, that I need to load a special font only for one window (ImGui::Begin)
..
What I did:
ImFont* pFont = io.Fonts->AddFontFromFileTTF("C:\Windows\Fonts\Ruda-Bold.ttf", 50);
ImGui::PushFont(pFont);

I've load the font, location seems to be correct. If I try to push font.. I get some strange errors. How can I load a front for a window and change after this specific window back to default font?

fontext

All 13 comments

Hello,

Your font is not loaded. When you say "a strange error" you need to specify what and provide all the information otherwhise we cannot help you.

In c++ a single \ in a string will inhibit the next character. Use \ or /.

image

My code:
if (ImGui::Begin(nameChar, &Vars.Menu.Opened, ImVec2(windowWidth, windowHeight), 1.0F, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_ShowBorders | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollWithMouse))
{
ImVec2 lPos = ImGui::GetCursorPos();
ImGuiStyle& style = ImGui::GetStyle();

        ImFont* pFont = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Ruda-Bold.ttf", 50);
        ImGui::PushFont(pFont);

For the records if I try to load this file here:
https://github.com/google/fonts/blob/master/ofl/ruda/Ruda-Bold.ttf
It works here. So I suspect either your path either your file may be wrong. Use a debugger to trace into AddFontFromFileTTF() to see what is happening.

(You should add an assert that pFont isn't NULL. Going to add that in the examples now, it would be helpful for everyone)

actually I searched arround and you cannot push for a new font!
There are some unresolved issues open for exactly this topic.

If yours is working, would be so kind and share the code with me inside a ImGui::Begin?!

I don't want what unresolved issue you are referring to. PushFont() work the only limitation is that you cannot call it before calling NewFrame() at least once.

Test loading 2 fonts during init:

    // Load Fonts
    ImGuiIO& io = ImGui::GetIO();
    ImFont* font1 = io.Fonts->AddFontDefault();
    ImFont* font2 = io.Fonts->AddFontFromFileTTF("../../extra_fonts/Ruda-Bold.ttf", 16.0f);

Then in main loop:

        ImGui::Begin("Test #1397");
        ImGui::Text("Hello!");
        ImGui::PushFont(font2);
        ImGui::Text("Hello!");
        ImGui::PopFont();
        ImGui::End();

image

Alternatively if you only load 1 font (the font you want) you don't need to call PushFont.

Make sure you load font during init and not every frame, your snippet above may suggest you are reloading a font every frame.

If you do:
Begin ... LoadFontXXX .. PushFont then the font doesn't have a chance to be built.

Generally people do:
LoadFontXXX (during init) ... Build fonts .. Main Loop

People who need to reload fonts during the application execution (which you don't appear to need) can load them at the beginning of the main loop and then rebuild the fonts.

HI,
Sorry for posting in a closed issue. My Question is where should I add the font to be found by AddFontFromFileTTF? I'm trying absolute paths and relative as well, but always end up here:

IM_ASSERT(0); // Could not load file.

What I'm missing?
This is an amazing library by the way!

AddFontFromFileTTF? I'm trying absolute paths and relative as well, but always end up here:
IM_ASSERT(0); // Could not load file.

Most likely your paths are wrong but there is a small chance the font itself can't be loaded for another reason. Without providing any information (your code, which font and settings) we can't guess.

Are you aware that backslashes needs to be quoted in a C/C++ string literal? What does your code looks like?

The demo main.cpp files have those comments:

// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. 
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. 
// - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Read 'misc/fonts/README.txt' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);

I have a very simple example in openframeworks. It compiles well but the font is not yet placed. Looks I'm just missing the part that loads the font?

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
gui.setup();
    //imageButtonSource.load("of.png");
    imageButtonID = gui.loadImage(imageButtonSource);
    ImGuiIO& io = ImGui::GetIO();

   // font1 = io.Fonts->AddFontDefault();
    font1 = io.Fonts->AddFontFromFileTTF("/Users/David/Openframeworks/apps/myApps/imGui/bin/data/fonts/Verdana.ttf", 18.0f);
   // ImGui::Fonts->GetTexDataAsRGBA32;
    //ImGui::PushFont(font1);
}

//--------------------------------------------------------------
void ofApp::update(){

}
ImVec4 clear_color = ImVec4(0.99f, 0.55f, 0.10f, 1.00f);
//--------------------------------------------------------------
void ofApp::draw(){
    //required to call this at beginning
    gui.begin();

    bool show_another_window = true;
    ImGui::SetNextWindowSize(ofVec2f(400,400), ImGuiSetCond_FirstUseEver);
    ImGui::SetNextWindowPos(ofVec2f(100,100));
    ImGui::Begin("Final Result", &show_another_window);
    ImGui::BeginChild("Scrolling");

   // ImGui::PushFont(font1);
    for (int n = 0; n < 50; n++)
        ImGui::TextColored(clear_color, "%04d: Some text");
    ImGui::EndChild();
    //ImGui::PopFont();

    //ImGui::ImageButton((ImTextureID)(uintptr_t)imageButtonID, ImVec2(300, 300));
    ImGui::End();
    gui.end();
}

Use a debugger to find out what's wrong with the AddFontFromFileTTF() call.
Make sure AddFontXXX is called before Build/GetTexDataAsRGBA32 (find where that is called in your binding)
Call ImGui::ShowStyleEditor() to browse/see the list of loaded fonts.

do you have an example using AddFontXXX? I think is that line what is missing:

it appears this message:
IM_ASSERT(font && font->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?

do you have an example using AddFontXXX? I think is that line what is missing:

The examples/ apps and misc/fonts/README have enough infos. Find out where your binding is building the font atlas and load fonts before that..

actually one of the reasons I'm trying to change the font is because I need to show text with bigger font size, to do so then I will need to change the scaling as well?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mnemode2 picture mnemode2  路  3Comments

SlNPacifist picture SlNPacifist  路  3Comments

BlackWatersInc picture BlackWatersInc  路  3Comments

DarkLinux picture DarkLinux  路  3Comments

namuda picture namuda  路  3Comments