1
0
Fork 0
mirror of https://github.com/ocornut/imgui.git synced 2026-01-11 00:04:24 +00:00

Fonts: added ImFontAtlas::SetFontLoader() to dynamically change font loader at runtime without using internal API. (#8752, #8465)

This commit is contained in:
ocornut 2025-06-30 20:01:02 +02:00
parent 8ccfdf7ba0
commit b7e5d76c79
5 changed files with 20 additions and 11 deletions

View file

@ -43,6 +43,8 @@ Breaking changes:
Other changes: Other changes:
- Fonts: added ImFontAtlas::SetFontLoader() to dynamically change font
loader at runtime without using internal API. (#8752, #8465)
- Textures: Fixed support for `#define ImTextureID_Invalid` to non-zero value: - Textures: Fixed support for `#define ImTextureID_Invalid` to non-zero value:
ImTextureData() was incorrectly cleared with zeroes. (#8745) [@rachit7645] ImTextureData() was incorrectly cleared with zeroes. (#8745) [@rachit7645]
- Demo: Added "Text -> Font Size" demo section. (#8738) [@Demonese] - Demo: Added "Text -> Font Size" demo section. (#8738) [@Demonese]
@ -211,7 +213,7 @@ Breaking changes:
- renamed/reworked ImFontBuilderIO into ImFontLoader, - renamed/reworked ImFontBuilderIO into ImFontLoader,
- renamed ImGuiFreeType::GetBuilderForFreeType() to ImGuiFreeType::GetFontLoader() - renamed ImGuiFreeType::GetBuilderForFreeType() to ImGuiFreeType::GetFontLoader()
- old: io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType() - old: io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()
- new: io.Fonts.FontLoader = ImGuiFreeType::GetFontLoader(); - new: io.Fonts->FontLoader = ImGuiFreeType::GetFontLoader()
- DrawList: Renamed ImDrawList::PushTextureID()/PopTextureID() to PushTexture()/PopTexture(). - DrawList: Renamed ImDrawList::PushTextureID()/PopTextureID() to PushTexture()/PopTexture().
- Fonts: (users of custom rectangles) - Fonts: (users of custom rectangles)
- Renamed AddCustomRectRegular() to AddCustomRect(). (#8466) - Renamed AddCustomRectRegular() to AddCustomRect(). (#8466)

View file

@ -453,7 +453,8 @@ IMPLEMENTING SUPPORT for ImGuiBackendFlags_RendererHasTextures:
- Fonts: (users of imgui_freetype): renamed ImFontAtlas::FontBuilderFlags to ImFontAtlas::FontLoaderFlags. Renamed ImFontConfig::FontBuilderFlags to ImFontConfig::FontLoaderFlags. Renamed ImGuiFreeTypeBuilderFlags to ImGuiFreeTypeLoaderFlags. - Fonts: (users of imgui_freetype): renamed ImFontAtlas::FontBuilderFlags to ImFontAtlas::FontLoaderFlags. Renamed ImFontConfig::FontBuilderFlags to ImFontConfig::FontLoaderFlags. Renamed ImGuiFreeTypeBuilderFlags to ImGuiFreeTypeLoaderFlags.
If you used runtime imgui_freetype selection rather than the default IMGUI_ENABLE_FREETYPE compile-time option: Renamed/reworked ImFontBuilderIO into ImFontLoader. Renamed ImGuiFreeType::GetBuilderForFreeType() to ImGuiFreeType::GetFontLoader(). If you used runtime imgui_freetype selection rather than the default IMGUI_ENABLE_FREETYPE compile-time option: Renamed/reworked ImFontBuilderIO into ImFontLoader. Renamed ImGuiFreeType::GetBuilderForFreeType() to ImGuiFreeType::GetFontLoader().
- old: io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType() - old: io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()
- new: io.Fonts.FontLoader = ImGuiFreeType::GetFontLoader() - new: io.Fonts->FontLoader = ImGuiFreeType::GetFontLoader()
- new: io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader()) to change dynamically at runtime [from 1.92.1]
- Fonts: (users of custom rectangles, see #8466): Renamed AddCustomRectRegular() to AddCustomRect(). Added GetCustomRect() as a replacement for GetCustomRectByIndex() + CalcCustomRectUV(). - Fonts: (users of custom rectangles, see #8466): Renamed AddCustomRectRegular() to AddCustomRect(). Added GetCustomRect() as a replacement for GetCustomRectByIndex() + CalcCustomRectUV().
- The output type of GetCustomRect() is now ImFontAtlasRect, which include UV coordinates. X->x, Y->y, Width->w, Height->h. - The output type of GetCustomRect() is now ImFontAtlasRect, which include UV coordinates. X->x, Y->y, Width->w, Height->h.
- old: - old:
@ -15902,7 +15903,7 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas)
#ifdef IMGUI_ENABLE_STB_TRUETYPE #ifdef IMGUI_ENABLE_STB_TRUETYPE
const ImFontLoader* loader_stbtruetype = ImFontAtlasGetFontLoaderForStbTruetype(); const ImFontLoader* loader_stbtruetype = ImFontAtlasGetFontLoaderForStbTruetype();
if (RadioButton("stb_truetype", loader_current == loader_stbtruetype)) if (RadioButton("stb_truetype", loader_current == loader_stbtruetype))
ImFontAtlasBuildSetupFontLoader(atlas, loader_stbtruetype); atlas->SetFontLoader(loader_stbtruetype);
#else #else
BeginDisabled(); BeginDisabled();
RadioButton("stb_truetype", false); RadioButton("stb_truetype", false);
@ -15913,7 +15914,7 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas)
#ifdef IMGUI_ENABLE_FREETYPE #ifdef IMGUI_ENABLE_FREETYPE
const ImFontLoader* loader_freetype = ImGuiFreeType::GetFontLoader(); const ImFontLoader* loader_freetype = ImGuiFreeType::GetFontLoader();
if (RadioButton("FreeType", loader_current == loader_freetype)) if (RadioButton("FreeType", loader_current == loader_freetype))
ImFontAtlasBuildSetupFontLoader(atlas, loader_freetype); atlas->SetFontLoader(loader_freetype);
if (loader_current == loader_freetype) if (loader_current == loader_freetype)
{ {
unsigned int loader_flags = atlas->FontLoaderFlags; unsigned int loader_flags = atlas->FontLoaderFlags;

View file

@ -3493,7 +3493,7 @@ struct ImFontConfig
// [Internal] // [Internal]
ImFontFlags Flags; // Font flags (don't use just yet, will be exposed in upcoming 1.92.X updates) ImFontFlags Flags; // Font flags (don't use just yet, will be exposed in upcoming 1.92.X updates)
ImFont* DstFont; // Target font (as we merging fonts, multiple ImFontConfig may target the same font) ImFont* DstFont; // Target font (as we merging fonts, multiple ImFontConfig may target the same font)
const ImFontLoader* FontLoader; // Custom font backend for this source (other use one stored in ImFontAtlas) const ImFontLoader* FontLoader; // Custom font backend for this source (default source is the one stored in ImFontAtlas)
void* FontLoaderData; // Font loader opaque storage (per font config) void* FontLoaderData; // Font loader opaque storage (per font config)
IMGUI_API ImFontConfig(); IMGUI_API ImFontConfig();
@ -3590,6 +3590,7 @@ struct ImFontAtlas
IMGUI_API void Clear(); // Clear everything (input fonts, output glyphs/textures) IMGUI_API void Clear(); // Clear everything (input fonts, output glyphs/textures)
IMGUI_API void CompactCache(); // Compact cached glyphs and texture. IMGUI_API void CompactCache(); // Compact cached glyphs and texture.
IMGUI_API void SetFontLoader(const ImFontLoader* font_loader); // Change font loader at runtime.
// As we are transitioning toward a new font system, we expect to obsolete those soon: // As we are transitioning toward a new font system, we expect to obsolete those soon:
IMGUI_API void ClearInputData(); // [OBSOLETE] Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts. IMGUI_API void ClearInputData(); // [OBSOLETE] Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts.
@ -3698,7 +3699,7 @@ struct ImFontAtlas
int FontNextUniqueID; // Next value to be stored in ImFont->FontID int FontNextUniqueID; // Next value to be stored in ImFont->FontID
ImVector<ImDrawListSharedData*> DrawListSharedDatas; // List of users for this atlas. Typically one per Dear ImGui context. ImVector<ImDrawListSharedData*> DrawListSharedDatas; // List of users for this atlas. Typically one per Dear ImGui context.
ImFontAtlasBuilder* Builder; // Opaque interface to our data that doesn't need to be public and may be discarded when rebuilding. ImFontAtlasBuilder* Builder; // Opaque interface to our data that doesn't need to be public and may be discarded when rebuilding.
const ImFontLoader* FontLoader; // Font loader opaque interface (default to stb_truetype, can be changed to use FreeType by defining IMGUI_ENABLE_FREETYPE). Don't set directly! const ImFontLoader* FontLoader; // Font loader opaque interface (default to use FreeType when IMGUI_ENABLE_FREETYPE is defined, otherwise default to use stb_truetype). Use SetFontLoader() to change this at runtime.
const char* FontLoaderName; // Font loader name (for display e.g. in About box) == FontLoader->Name const char* FontLoaderName; // Font loader name (for display e.g. in About box) == FontLoader->Name
void* FontLoaderData; // Font backend opaque storage void* FontLoaderData; // Font backend opaque storage
unsigned int FontLoaderFlags; // Shared flags (for all fonts) for font loader. THIS IS BUILD IMPLEMENTATION DEPENDENT (e.g. Per-font override is also available in ImFontConfig). unsigned int FontLoaderFlags; // Shared flags (for all fonts) for font loader. THIS IS BUILD IMPLEMENTATION DEPENDENT (e.g. Per-font override is also available in ImFontConfig).

View file

@ -2659,6 +2659,11 @@ void ImFontAtlas::CompactCache()
ImFontAtlasTextureCompact(this); ImFontAtlasTextureCompact(this);
} }
void ImFontAtlas::SetFontLoader(const ImFontLoader* font_loader)
{
ImFontAtlasBuildSetupFontLoader(this, font_loader);
}
void ImFontAtlas::ClearInputData() void ImFontAtlas::ClearInputData()
{ {
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!"); IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
@ -4178,9 +4183,9 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
if (atlas->FontLoader == NULL) if (atlas->FontLoader == NULL)
{ {
#ifdef IMGUI_ENABLE_FREETYPE #ifdef IMGUI_ENABLE_FREETYPE
ImFontAtlasBuildSetupFontLoader(atlas, ImGuiFreeType::GetFontLoader()); atlas->SetFontLoader(ImGuiFreeType::GetFontLoader());
#elif defined(IMGUI_ENABLE_STB_TRUETYPE) #elif defined(IMGUI_ENABLE_STB_TRUETYPE)
ImFontAtlasBuildSetupFontLoader(atlas, ImFontAtlasGetFontLoaderForStbTruetype()); atlas->SetFontLoader(ImFontAtlasGetFontLoaderForStbTruetype());
#else #else
IM_ASSERT(0); // Invalid Build function IM_ASSERT(0); // Invalid Build function
#endif #endif

View file

@ -8,7 +8,7 @@
// Usage: // Usage:
// - Add '#define IMGUI_ENABLE_FREETYPE' in your imconfig to automatically enable support // - Add '#define IMGUI_ENABLE_FREETYPE' in your imconfig to automatically enable support
// for imgui_freetype in imgui. It is equivalent to selecting the default loader with: // for imgui_freetype in imgui. It is equivalent to selecting the default loader with:
// io.Fonts.FontLoader = ImGuiFreeType::GetFontLoader() // io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader())
// Optional support for OpenType SVG fonts: // Optional support for OpenType SVG fonts:
// - Add '#define IMGUI_ENABLE_FREETYPE_PLUTOSVG' to use plutosvg (not provided). See #7927. // - Add '#define IMGUI_ENABLE_FREETYPE_PLUTOSVG' to use plutosvg (not provided). See #7927.
@ -62,7 +62,7 @@ namespace ImGuiFreeType
{ {
// This is automatically assigned when using '#define IMGUI_ENABLE_FREETYPE'. // This is automatically assigned when using '#define IMGUI_ENABLE_FREETYPE'.
// If you need to dynamically select between multiple builders: // If you need to dynamically select between multiple builders:
// - you can manually assign this builder with 'atlas->FontLoader = ImGuiFreeType::GetFontLoader()' // - you can manually assign this builder with 'atlas->SetFontLoader(ImGuiFreeType::GetFontLoader())'
// - prefer deep-copying this into your own ImFontLoader instance if you use hot-reloading that messes up static data. // - prefer deep-copying this into your own ImFontLoader instance if you use hot-reloading that messes up static data.
IMGUI_API const ImFontLoader* GetFontLoader(); IMGUI_API const ImFontLoader* GetFontLoader();
@ -75,7 +75,7 @@ namespace ImGuiFreeType
// Obsolete names (will be removed) // Obsolete names (will be removed)
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
//IMGUI_API const ImFontBuilderIO* GetBuilderForFreeType(); // Renamed/changed in 1.92. Change 'io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()' to 'io.Fonts.FontLoader = ImGuiFreeType::GetFontLoader()' if you need runtime selection. //IMGUI_API const ImFontBuilderIO* GetBuilderForFreeType(); // Renamed/changed in 1.92. Change 'io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()' to 'io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader())' if you need runtime selection.
//static inline bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int flags = 0) { atlas->FontBuilderIO = GetBuilderForFreeType(); atlas->FontLoaderFlags = flags; return atlas->Build(); } // Prefer using '#define IMGUI_ENABLE_FREETYPE' //static inline bool BuildFontAtlas(ImFontAtlas* atlas, unsigned int flags = 0) { atlas->FontBuilderIO = GetBuilderForFreeType(); atlas->FontLoaderFlags = flags; return atlas->Build(); } // Prefer using '#define IMGUI_ENABLE_FREETYPE'
#endif #endif
} }