|
| 1 | +# imgui_freetype |
| 2 | + |
| 3 | +This is an attempt to replace stb_truetype (the default imgui's font rasterizer) with FreeType. |
| 4 | +Currently not optimal and probably has some limitations or bugs. |
| 5 | +By [Vuhdo](https://github.com/Vuhdo) (Aleksei Skriabin). Improvements by @mikesart. Maintained by @ocornut. |
| 6 | + |
| 7 | +**Usage** |
| 8 | +1. Get latest FreeType binaries or build yourself. |
| 9 | +2. Add imgui_freetype.h/cpp alongside your imgui sources. |
| 10 | +3. Include imgui_freetype.h after imgui.h. |
| 11 | +4. Call ImGuiFreeType::BuildFontAtlas() *BEFORE* calling ImFontAtlas::GetTexDataAsRGBA32() or ImFontAtlas::Build() (so normal Build() won't be called): |
| 12 | + |
| 13 | +```cpp |
| 14 | +// See ImGuiFreeType::RasterizationFlags |
| 15 | +unsigned int flags = ImGuiFreeType::DisableHinting; |
| 16 | +ImGuiFreeType::BuildFontAtlas(io.Fonts, flags); |
| 17 | +io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); |
| 18 | +``` |
| 19 | +
|
| 20 | +**Test code Usage** |
| 21 | +```cpp |
| 22 | +#include "misc/freetype/imgui_freetype.h" |
| 23 | +#include "misc/freetype/imgui_freetype.cpp" |
| 24 | +
|
| 25 | +// Load various small fonts |
| 26 | +ImGuiIO& io = ImGui::GetIO(); |
| 27 | +io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 13.0f); |
| 28 | +io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 13.0f); |
| 29 | +io.Fonts->AddFontDefault(); |
| 30 | +
|
| 31 | +FreeTypeTest freetype_test; |
| 32 | +
|
| 33 | +// Main Loop |
| 34 | +while (true) |
| 35 | +{ |
| 36 | + if (freetype_test.UpdateRebuild()) |
| 37 | + { |
| 38 | + // REUPLOAD FONT TEXTURE TO GPU |
| 39 | + // e.g ImGui_ImplGlfwGL3_InvalidateDeviceObjects() + ImGui_ImplGlfwGL3_CreateDeviceObjects() |
| 40 | + } |
| 41 | + ImGui::NewFrame(); |
| 42 | + freetype_test.ShowFreetypeOptionsWindow(); |
| 43 | + ... |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +**Test code** |
| 49 | +```cpp |
| 50 | +#include "misc/freetype/imgui_freetype.h" |
| 51 | +#include "misc/freetype/imgui_freetype.cpp" |
| 52 | + |
| 53 | +struct FreeTypeTest |
| 54 | +{ |
| 55 | + enum FontBuildMode |
| 56 | + { |
| 57 | + FontBuildMode_FreeType, |
| 58 | + FontBuildMode_Stb, |
| 59 | + }; |
| 60 | + |
| 61 | + FontBuildMode BuildMode; |
| 62 | + bool WantRebuild; |
| 63 | + float FontsMultiply; |
| 64 | + unsigned int FontsFlags; |
| 65 | + |
| 66 | + FreeTypeTest() |
| 67 | + { |
| 68 | + BuildMode = FontBuildMode_FreeType; |
| 69 | + WantRebuild = true; |
| 70 | + FontsMultiply = 1.0f; |
| 71 | + FontsFlags = 0; |
| 72 | + } |
| 73 | + |
| 74 | + // Call _BEFORE_ NewFrame() |
| 75 | + bool UpdateRebuild() |
| 76 | + { |
| 77 | + if (!WantRebuild) |
| 78 | + return false; |
| 79 | + ImGuiIO& io = ImGui::GetIO(); |
| 80 | + for (int n = 0; n < io.Fonts->Fonts.Size; n++) |
| 81 | + { |
| 82 | + io.Fonts->Fonts[n]->ConfigData->RasterizerMultiply = FontsMultiply; |
| 83 | + io.Fonts->Fonts[n]->ConfigData->RasterizerFlags = (BuildMode == FontBuildMode_FreeType) ? FontsFlags : 0x00; |
| 84 | + } |
| 85 | + if (BuildMode == FontBuildMode_FreeType) |
| 86 | + ImGuiFreeType::BuildFontAtlas(io.Fonts, FontsFlags); |
| 87 | + else if (BuildMode == FontBuildMode_Stb) |
| 88 | + io.Fonts->Build(); |
| 89 | + WantRebuild = false; |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + // Call to draw interface |
| 94 | + void ShowFreetypeOptionsWindow() |
| 95 | + { |
| 96 | + ImGui::Begin("FreeType Options"); |
| 97 | + ImGui::ShowFontSelector("Fonts"); |
| 98 | + WantRebuild |= ImGui::RadioButton("FreeType", (int*)&BuildMode, FontBuildMode_FreeType); |
| 99 | + ImGui::SameLine(); |
| 100 | + WantRebuild |= ImGui::RadioButton("Stb (Default)", (int*)&BuildMode, FontBuildMode_Stb); |
| 101 | + WantRebuild |= ImGui::DragFloat("Multiply", &FontsMultiply, 0.001f, 0.0f, 2.0f); |
| 102 | + if (BuildMode == FontBuildMode_FreeType) |
| 103 | + { |
| 104 | + WantRebuild |= ImGui::CheckboxFlags("NoHinting", &FontsFlags, ImGuiFreeType::NoHinting); |
| 105 | + WantRebuild |= ImGui::CheckboxFlags("NoAutoHint", &FontsFlags, ImGuiFreeType::NoAutoHint); |
| 106 | + WantRebuild |= ImGui::CheckboxFlags("ForceAutoHint", &FontsFlags, ImGuiFreeType::ForceAutoHint); |
| 107 | + WantRebuild |= ImGui::CheckboxFlags("LightHinting", &FontsFlags, ImGuiFreeType::LightHinting); |
| 108 | + WantRebuild |= ImGui::CheckboxFlags("MonoHinting", &FontsFlags, ImGuiFreeType::MonoHinting); |
| 109 | + WantRebuild |= ImGui::CheckboxFlags("Bold", &FontsFlags, ImGuiFreeType::Bold); |
| 110 | + WantRebuild |= ImGui::CheckboxFlags("Oblique", &FontsFlags, ImGuiFreeType::Oblique); |
| 111 | + } |
| 112 | + ImGui::End(); |
| 113 | + } |
| 114 | +}; |
| 115 | +``` |
| 116 | +
|
| 117 | +**Known issues** |
| 118 | +- Output texture has excessive resolution (lots of vertical waste) |
| 119 | +- FreeType's memory allocator is not overridden. |
| 120 | +
|
| 121 | +**Obligatory comparison screenshots** |
| 122 | +
|
| 123 | +Using Windows built-in segoeui.ttf font. Open in new browser tabs, view at 1080p+. |
| 124 | +
|
| 125 | + |
| 126 | +
|
0 commit comments