Patitotective / ImThemes

Dear ImGui style browser and editor written in Nim
MIT License
348 stars 74 forks source link

How to set text font size #13

Closed rainzee closed 1 year ago

rainzee commented 1 year ago

I have a very basic question, I want to create a text like a title that is more eye-catching than normal ImGui::Text, with a larger font and different colors.

I checked the documentation and this can be achieved with different fonts.

Load multiple fonts:

// Init
ImGuiIO& io = ImGui::GetIO();
ImFont* font1 = io.Fonts->AddFontFromFileTTF("font.ttf", size_pixels);
ImFont* font2 = io.Fonts->AddFontFromFileTTF("anotherfont.otf", size_pixels);
// In application loop: select font at runtime
ImGui::Text("Hello"); // use the default font (which is the first loaded font)
ImGui::PushFont(font2);
ImGui::Text("Hello with another font");
ImGui::PopFont();

As I was on an open source project to make some modifications, The project is structured like this ├─Project │ │ GUI.cpp │ │ GUI.hpp │ │ Hooks.cpp │ │ Hooks.hpp

In GUI.cpp

ImGui::Begin();
//Title ImGui Text Here
ImGui::PushFont(font_title);
ImGui::Text("Title");
ImGui::PopFont();
//Normal ImGui Text Here
ImGui::Text("Normal");
ImGui::End();

In Hook.cpp

static void init_imgui(void* device, bool is_d3d11 = false) noexcept
    {
        ImGui::CreateContext();
        auto& style{ ImGui::GetStyle() };
        // init style

        auto& io{ ImGui::GetIO() };
        io.IniFilename = nullptr;
        io.LogFilename = nullptr;
        io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;

        if (PWSTR pathToFonts; SUCCEEDED(::SHGetKnownFolderPath(FOLDERID_Fonts, 0, nullptr, &pathToFonts))) {
            const std::filesystem::path path{ pathToFonts };
            ::CoTaskMemFree(pathToFonts);
            ImFontConfig cfg;
            cfg.SizePixels = 15.0f;
            io.Fonts->AddFontFromFileTTF((path / "tahoma.ttf").string().c_str(), 15.0f, &cfg, ranges);
            cfg.MergeMode = true;
            io.Fonts->AddFontFromFileTTF((path / "malgun.ttf").string().c_str(), 16.0f, &cfg, getFontGlyphRangesKr());
            io.Fonts->AddFontFromFileTTF((path / "simhei.ttf").string().c_str(), 12.0f, &cfg, io.Fonts->GetGlyphRangesChineseFull());
            cfg.MergeMode = false;
        }
                // init d3d device
    }

Now my question is where should I add my title font and how do I use it in GUI.cpp.

Sorry, this seems like a very rookie question, but I have checked all the documentation I can read, and since this is a small open source project and not all code in one file, So I don't quite understand how to do.

Patitotective commented 1 year ago

I don't see how this can be an issue of ImThemes. Ask in stackoverflow or imgui's repo discussions section if you want help about imgui. Also ImThemes is written in Nim, I don't use C++ directly.