Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to perform an AddFontFromMemoryTTF instead of addFont/AddFontFromFileTTF #13

Closed
moebiussurfing opened this issue Jan 8, 2023 · 4 comments

Comments

@moebiussurfing
Copy link
Collaborator

moebiussurfing commented Jan 8, 2023

Hello @Daandelange ,

I am trying to integrate that repo:
https://github.com/patrickcjk/imgui-notify

--

It's working well here on nightly builds, as uses C++17.
But I am not being able to enable the font icons.
(without crashing or breaking the atlas to white squares)

The repo adds the fonts as cpp .h files instead as in your implemented method to load ttf files:

customFont = gui.addFont("Roboto-Medium.ttf",16.f, nullptr, true?polishCharRanges:normalCharRanges);

ImFont* font = io.Fonts->AddFontFromFileTTF(filePath.c_str(), fontSize, _fontConfig, _glyphRanges);

--

Do you have any clue to make that work?

Here are some notes by the author:

Requirements
You must use a font other than the default one. Font-Awesome icons used in this library cannot be merged with default font. You can use Tahoma (provided in the example project).
If you load the font using AddFontFromMemoryTTF (from memory, instead of from a file on disk) and memory is read-only as in the example, you must pass a ImFontConfig structure with FontDataOwnedByAtlas = false to prevent imgui from attempting to free the buffer (which will lead into a crash).

And the code that must be initiated in some way:
Initialisation (after impl call, e.g ImGui_ImplDX12_Init)

ImGuiIO* io = &ImGui::GetIO();

ImFontConfig font_cfg;
font_cfg.FontDataOwnedByAtlas = false;
io->Fonts->AddFontFromMemoryTTF((void*)tahoma, sizeof(tahoma), 17.f, &font_cfg);

// Initialize notify
ImGui::MergeIconsWithLatestFont(16.f, false);

// If you use multiple fonts, repeat the same thing!
// io->Fonts->AddFontFromMemoryTTF((void*)another_font, sizeof(another_font), 17.f, &font_cfg);
// ImGui::MergeIconsWithLatestFont(16.f, false);

--

I am trying to figure out something here, but no success:
https://github.com/ocornut/imgui/blob/master/docs/FONTS.md
https://github.com/Daandelange/ofxImGui/tree/master/example-fonts

Regards

@Daandelange
Copy link
Owner

Daandelange commented Jan 8, 2023

Hey Manu,
Oh nice, oF nightly builds now support C++17 :)

It looks like the .cpp file is a TTF file too, they're just using AddFontFromMemoryTTF (), probably to bundle the font within the app binary.
https://github.com/patrickcjk/imgui-notify/blob/2e2eba3d1814ef886401120f7248b6db86714dbc/example/main.cpp#L68

ofxImGui uses AddFontFromFileTTF().

ImFont* font = io.Fonts->AddFontFromFileTTF(filePath.c_str(), fontSize, _fontConfig, _glyphRanges);

So I think it would make sense to duplicate the addFont() function to provide an alternative from memory within ofxImGui.

ImFont* addFont(const std::string & fontPath, float fontSize = 13.0f, const ImFontConfig* _fontConfig = nullptr, const ImWchar* _glyphRanges = nullptr, bool _setAsDefaultFont=false );

On the other side, you might not need to modify ofxImGui, you could call the init() function within ofxImGui. Did you try to call imgui-notify::init() after ofxImGui::setup() ?
Providing a custom ImFontConfig is already possible in ofxImGui too, so even the native functions should work if you get a tahoma.ttf file.

I got to load the tahoma font in both ways mentioned above using example-fonts.
I was not able to call ImGui::MergeIconsWithLatestFont(16.f, false); as that requires the extension.
I'm not able to compile the extension as it requires C++17 an I'm on osx atm.

ImFontConfig font_cfg;
font_cfg.FontDataOwnedByAtlas = false;
gui.addFontFromMemory((void*)tahoma, sizeof(tahoma), 17.f, &font_cfg);
// ImGui::MergeIconsWithLatestFont(16.f, false);

Daandelange added a commit that referenced this issue Jan 8, 2023
@moebiussurfing
Copy link
Collaborator Author

cool thanks, @Daandelange

It worked after adding the memory-based addFont method

I was getting crashes, black panels, and white squares on fonts.

I copy pasted it here just in case or do you prefer a pull maybe?

Gui.h

    public:
        ImFont* addFont(void* ttf_data, int ttf_size, float fontSize = 13.0f, const ImFontConfig* _fontConfig = nullptr, const ImWchar* _glyphRanges = nullptr, bool _setAsDefaultFont = false);

Gui.cpp

    //--------------------------------------------------------------
    ImFont* Gui::addFont(void* ttf_data, int ttf_size, float fontSize, const ImFontConfig* _fontConfig, const ImWchar* _glyphRanges, bool _setAsDefaultFont) {

        if (context == nullptr) {
            ofLogWarning() << "You must load fonts after gui.setup() ! (ignoring this call)";
            return nullptr;
        }

        //ImFontConfig structure allows you to configure oversampling.
        //By default OversampleH = 3 and OversampleV = 1 which will make your font texture data 3 times larger
        //than necessary, so you may reduce that to 1.

        ImGui::SetCurrentContext(context);
        ImGuiIO& io = ImGui::GetIO();
        //std::string filePath = ofFilePath::getAbsolutePath(fontPath);

        // ensure default font gets loaded once
        if (io.Fonts->Fonts.size() == 0) io.Fonts->AddFontDefault();

        ImFont* font = io.Fonts->AddFontFromMemoryTTF(ttf_data, ttf_size, fontSize, _fontConfig, _glyphRanges);

        if (io.Fonts->Fonts.size() > 0) {
            io.Fonts->Build();
            if (engine.updateFontsTexture()) {
                if (_setAsDefaultFont) setDefaultFont(font);
                return font;
            }
            else return nullptr;
        }
        else {
            return nullptr;
        }
    }

@Daandelange
Copy link
Owner

Hey, Yes it already is committed to develop (0c12244), I thought you'd see the mention above your post...
And you're right, the ofxImGui::addFont way also updates the fonts texture.

@Daandelange
Copy link
Owner

Daandelange added a commit that referenced this issue Jan 9, 2023
AddFontFromMemory fonts-example (commented). #13
Daandelange added a commit that referenced this issue Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants