Created
January 26, 2021 16:17
-
-
Save ocornut/b3a9ecf13502fd818799a452969649ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test helper for imgui_freetype | |
#include "misc/freetype/imgui_freetype.h" | |
struct FreeTypeTest | |
{ | |
enum FontBuildMode { FontBuildMode_FreeType, FontBuildMode_Stb }; | |
FontBuildMode BuildMode = FontBuildMode_FreeType; | |
bool WantRebuild = true; | |
float RasterizerMultiply = 1.0f; | |
unsigned int FreeTypeBuilderFlags = 0; | |
// Call _BEFORE_ NewFrame() | |
bool PreNewFrame() | |
{ | |
if (!WantRebuild) | |
return false; | |
ImFontAtlas* atlas = ImGui::GetIO().Fonts; | |
for (int n = 0; n < atlas->ConfigData.Size; n++) | |
((ImFontConfig*)&atlas->ConfigData[n])->RasterizerMultiply = RasterizerMultiply; | |
// Allow for dynamic selection of the builder. | |
// In real code you are likely to just define IMGUI_ENABLE_FREETYPE and never assign to FontBuilderIO. | |
#ifdef IMGUI_ENABLE_FREETYPE | |
if (BuildMode == FontBuildMode_FreeType) | |
{ | |
atlas->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType(); | |
atlas->FontBuilderFlags = FreeTypeBuilderFlags; | |
} | |
#endif | |
#ifdef IMGUI_ENABLE_STB_TRUETYPE | |
if (BuildMode == FontBuildMode_Stb) | |
{ | |
atlas->FontBuilderIO = ImFontAtlasGetBuilderForStbTruetype(); | |
atlas->FontBuilderFlags = 0; | |
} | |
#endif | |
atlas->Build(); | |
WantRebuild = false; | |
return true; | |
} | |
// Call to draw UI | |
void ShowFontsOptionsWindow() | |
{ | |
ImFontAtlas* atlas = ImGui::GetIO().Fonts; | |
ImGui::Begin("FreeType Options"); | |
ImGui::ShowFontSelector("Fonts"); | |
WantRebuild |= ImGui::RadioButton("FreeType", (int*)&BuildMode, FontBuildMode_FreeType); | |
ImGui::SameLine(); | |
WantRebuild |= ImGui::RadioButton("Stb (Default)", (int*)&BuildMode, FontBuildMode_Stb); | |
WantRebuild |= ImGui::DragInt("TexGlyphPadding", &atlas->TexGlyphPadding, 0.1f, 1, 16); | |
WantRebuild |= ImGui::DragFloat("RasterizerMultiply", &RasterizerMultiply, 0.001f, 0.0f, 2.0f); | |
ImGui::Separator(); | |
if (BuildMode == FontBuildMode_FreeType) | |
{ | |
#ifndef IMGUI_ENABLE_FREETYPE | |
ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.5f, 1.0f), "Error: FreeType builder not compiled!"); | |
#endif | |
WantRebuild |= ImGui::CheckboxFlags("NoHinting", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_NoHinting); | |
WantRebuild |= ImGui::CheckboxFlags("NoAutoHint", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_NoAutoHint); | |
WantRebuild |= ImGui::CheckboxFlags("ForceAutoHint", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_ForceAutoHint); | |
WantRebuild |= ImGui::CheckboxFlags("LightHinting", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_LightHinting); | |
WantRebuild |= ImGui::CheckboxFlags("MonoHinting", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_MonoHinting); | |
WantRebuild |= ImGui::CheckboxFlags("Bold", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_Bold); | |
WantRebuild |= ImGui::CheckboxFlags("Oblique", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_Oblique); | |
WantRebuild |= ImGui::CheckboxFlags("Monochrome", &FreeTypeBuilderFlags, ImGuiFreeTypeBuilderFlags_Monochrome); | |
} | |
if (BuildMode == FontBuildMode_Stb) | |
{ | |
#ifndef IMGUI_ENABLE_STB_TRUETYPE | |
ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.5f, 1.0f), "Error: stb_truetype builder not compiled!"); | |
#endif | |
} | |
ImGui::End(); | |
} | |
}; | |
[....] | |
// Application Init: Load various small fonts | |
ImGuiIO& io = ImGui::GetIO(); | |
io.Fonts->AddFontFromFileTTF("NotoSans-Regular.ttf", 13.0f); | |
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 13.0f); | |
io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 13.0f); | |
io.Fonts->AddFontDefault(); | |
FreeTypeTest freetype_test; | |
[....] | |
// Application main loop | |
while (1) | |
{ | |
if (freetype_test.PreNewFrame()) | |
{ | |
// REUPLOAD FONT TEXTURE TO GPU | |
ImGui_ImplXXX_DestroyDeviceObjects(); | |
ImGui_ImplXXX_CreateDeviceObjects(); | |
} | |
ImGui::NewFrame(); | |
freetype_test.ShowFontsOptionsWindow(); | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment