Skip to content

Commit df7050c

Browse files
committedApr 3, 2025·
WIP - Fonts: changing loader/backend or loader flags may be done without losing custom rects. Sharing more code.
1 parent 4be415f commit df7050c

File tree

3 files changed

+61
-46
lines changed

3 files changed

+61
-46
lines changed
 

‎imgui.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -21304,9 +21304,16 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas)
2130421304
ImFontAtlasBuildSetupFontLoader(atlas, loader_freetype);
2130521305
if (loader_current == loader_freetype)
2130621306
{
21307-
Text("Shared FreeType Loader Flags:");
21308-
if (ImGuiFreeType::DebugEditFontBuilderFlags(&atlas->FontBuilderFlags))
21309-
ImFontAtlasBuildReloadAll(atlas);
21307+
unsigned int loader_flags = atlas->FontBuilderFlags;
21308+
Text("Shared FreeType Loader Flags: 0x%08", loader_flags);
21309+
if (ImGuiFreeType::DebugEditFontBuilderFlags(&loader_flags))
21310+
{
21311+
for (ImFont* font : atlas->Fonts)
21312+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
21313+
atlas->FontBuilderFlags = loader_flags;
21314+
for (ImFont* font : atlas->Fonts)
21315+
ImFontAtlasBuildInitFontOutput(atlas, font);
21316+
}
2131021317
}
2131121318
#else
2131221319
BeginDisabled();
@@ -21333,8 +21340,9 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas)
2133321340
if (Button("Grow"))
2133421341
ImFontAtlasBuildGrowTexture(atlas);
2133521342
SameLine();
21336-
if (Button("Clear Output"))
21343+
if (Button("Clear All"))
2133721344
ImFontAtlasBuildClear(atlas);
21345+
SetItemTooltip("Destroy cache and custom rectangles.");
2133821346

2133921347
for (int tex_n = 0; tex_n < atlas->TexList.Size; tex_n++)
2134021348
{
@@ -22402,9 +22410,14 @@ void ImGui::DebugNodeFont(ImFont* font)
2240222410
#ifdef IMGUI_ENABLE_FREETYPE
2240322411
if (loader->Name != NULL && strcmp(loader->Name, "FreeType") == 0)
2240422412
{
22405-
Text("FreeType Loader Flags: 0x%08X", src->FontBuilderFlags);
22406-
if (ImGuiFreeType::DebugEditFontBuilderFlags(&src->FontBuilderFlags))
22407-
ImFontAtlasBuildReloadFont(atlas, src);
22413+
unsigned int loader_flags = src->FontBuilderFlags;
22414+
Text("FreeType Loader Flags: 0x%08X", loader_flags);
22415+
if (ImGuiFreeType::DebugEditFontBuilderFlags(&loader_flags))
22416+
{
22417+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
22418+
src->FontBuilderFlags = loader_flags;
22419+
ImFontAtlasBuildInitFontOutput(atlas, font);
22420+
}
2240822421
}
2240922422
#endif
2241022423
TreePop();

‎imgui_draw.cpp

+39-37
Original file line numberDiff line numberDiff line change
@@ -2651,14 +2651,11 @@ void ImFontAtlas::CompactCache()
26512651
void ImFontAtlas::ClearInputData()
26522652
{
26532653
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
2654+
2655+
for (ImFont* font : Fonts)
2656+
ImFontAtlasBuildDestroyFontOutput(this, font);
26542657
for (ImFontConfig& font_cfg : Sources)
2655-
{
2656-
const ImFontLoader* loader = font_cfg.FontLoader ? font_cfg.FontLoader : FontLoader;
2657-
if (loader && loader->FontSrcDestroy != NULL)
2658-
loader->FontSrcDestroy(this, &font_cfg);
26592658
ImFontAtlasBuildDestroyFontSourceData(this, &font_cfg);
2660-
}
2661-
26622659
for (ImFont* font : Fonts)
26632660
{
26642661
// When clearing this we lose access to the font name and other information used to build the font.
@@ -3200,15 +3197,9 @@ void ImFontAtlas::RemoveFont(ImFont* font)
32003197
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
32013198
font->ClearOutputData();
32023199

3200+
ImFontAtlasBuildDestroyFontOutput(this, font);
32033201
for (int src_n = 0; src_n < font->SourcesCount; src_n++)
3204-
{
3205-
ImFontConfig* src = (ImFontConfig*)(void*)&font->Sources[src_n];
3206-
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : FontLoader;
3207-
if (loader && loader->FontSrcDestroy != NULL)
3208-
loader->FontSrcDestroy(this, src);
3209-
if (src->FontData != NULL && src->FontDataOwnedByAtlas)
3210-
IM_FREE(src->FontData);
3211-
}
3202+
ImFontAtlasBuildDestroyFontSourceData(this, &font->Sources[src_n]);
32123203

32133204
bool removed = Fonts.find_erase(font);
32143205
IM_ASSERT(removed);
@@ -3381,13 +3372,20 @@ void ImFontAtlasBuildSetupFontLoader(ImFontAtlas* atlas, const ImFontLoader* fon
33813372

33823373
// Note that texture size estimate is likely incorrect in this situation, as FreeType backend doesn't use oversampling.
33833374
ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(atlas);
3384-
ImFontAtlasBuildDestroy(atlas);
3375+
3376+
for (ImFont* font : atlas->Fonts)
3377+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
3378+
if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
3379+
atlas->FontLoader->LoaderShutdown(atlas);
33853380

33863381
atlas->FontLoader = font_loader;
33873382
atlas->FontLoaderName = font_loader ? font_loader->Name : "NULL";
3383+
IM_ASSERT(atlas->FontLoaderData == NULL);
33883384

3389-
ImFontAtlasBuildAddTexture(atlas, new_tex_size.x, new_tex_size.y);
3390-
ImFontAtlasBuildInit(atlas);
3385+
if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderInit)
3386+
atlas->FontLoader->LoaderInit(atlas);
3387+
for (ImFont* font : atlas->Fonts)
3388+
ImFontAtlasBuildInitFontOutput(atlas, font);
33913389
}
33923390

33933391
// Preload all glyph ranges for legacy backends.
@@ -3565,18 +3563,31 @@ static void ImFontAtlasBuildUpdateLinesTexData(ImFontAtlas* atlas, bool add_and_
35653563

35663564
//-----------------------------------------------------------------------------------------------------------------------------
35673565

3568-
void ImFontAtlasBuildReloadAll(ImFontAtlas* atlas)
3566+
bool ImFontAtlasBuildInitFontOutput(ImFontAtlas* atlas, ImFont* font)
35693567
{
3570-
const ImFontLoader* main_loader = atlas->FontLoader;
3571-
ImFontAtlasBuildSetupFontLoader(atlas, NULL);
3572-
ImFontAtlasBuildSetupFontLoader(atlas, main_loader);
3568+
bool ret = true;
3569+
for (int src_n = 0; src_n < font->SourcesCount; src_n++)
3570+
{
3571+
ImFontConfig* src = &font->Sources[src_n];
3572+
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
3573+
if (loader && loader->FontSrcInit != NULL && !loader->FontSrcInit(atlas, src))
3574+
ret = false;
3575+
}
3576+
IM_ASSERT(ret); // Unclear how to react to this meaningfully. Assume that result will be same as initial AddFont() call.
3577+
return ret;
35733578
}
35743579

3575-
void ImFontAtlasBuildReloadFont(ImFontAtlas* atlas, ImFontConfig* src)
3580+
// Keep source/input FontData
3581+
void ImFontAtlasBuildDestroyFontOutput(ImFontAtlas* atlas, ImFont* font)
35763582
{
3577-
// FIXME-NEWATLAS: rebuild single font not supported yet.
3578-
IM_UNUSED(src);
3579-
ImFontAtlasBuildReloadAll(atlas);
3583+
font->ClearOutputData();
3584+
for (int src_n = 0; src_n < font->SourcesCount; src_n++)
3585+
{
3586+
ImFontConfig* src = &font->Sources[src_n];
3587+
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
3588+
if (loader && loader->FontSrcDestroy != NULL)
3589+
loader->FontSrcDestroy(atlas, src);
3590+
}
35803591
}
35813592

35823593
//-----------------------------------------------------------------------------------------------------------------------------
@@ -4151,13 +4162,8 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41514162
#else
41524163
IM_ASSERT(0); // Invalid Build function
41534164
#endif
4154-
return; // ImFontAtlasBuildSetupFontLoader() automatically call ImFontAtlasBuildInit()
41554165
}
41564166

4157-
IM_ASSERT(atlas->FontLoaderData == NULL);
4158-
if (atlas->FontLoader->LoaderInit)
4159-
atlas->FontLoader->LoaderInit(atlas);
4160-
41614167
// Create initial texture size
41624168
if (atlas->TexData == NULL || atlas->TexData->Pixels == NULL)
41634169
ImFontAtlasBuildAddTexture(atlas, ImUpperPowerOfTwo(atlas->TexMinWidth), ImUpperPowerOfTwo(atlas->TexMinHeight));
@@ -4168,6 +4174,8 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41684174
{
41694175
IM_ASSERT(atlas->Builder == NULL);
41704176
builder = atlas->Builder = IM_NEW(ImFontAtlasBuilder)();
4177+
if (atlas->FontLoader->LoaderInit)
4178+
atlas->FontLoader->LoaderInit(atlas);
41714179
}
41724180

41734181
ImFontAtlasBuildUpdateRendererHasTexturesFromContext(atlas);
@@ -4196,13 +4204,7 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41964204
void ImFontAtlasBuildDestroy(ImFontAtlas* atlas)
41974205
{
41984206
for (ImFont* font : atlas->Fonts)
4199-
font->ClearOutputData();
4200-
for (ImFontConfig& font_cfg : atlas->Sources)
4201-
{
4202-
const ImFontLoader* loader = font_cfg.FontLoader ? font_cfg.FontLoader : atlas->FontLoader;
4203-
if (loader && loader->FontSrcDestroy != NULL)
4204-
loader->FontSrcDestroy(atlas, &font_cfg);
4205-
}
4207+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
42064208
if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
42074209
{
42084210
atlas->FontLoader->LoaderShutdown(atlas);

‎imgui_internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -4061,9 +4061,9 @@ IMGUI_API bool ImFontAtlasBuildAddFont(ImFontAtlas* atlas, ImFontCo
40614061
IMGUI_API void ImFontAtlasBuildSetupFontSpecialGlyphs(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src);
40624062
IMGUI_API void ImFontAtlasBuildPreloadAllGlyphRanges(ImFontAtlas* atlas); // Legacy
40634063
IMGUI_API void ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, float size, int* out_oversample_h, int* out_oversample_v);
4064+
IMGUI_API bool ImFontAtlasBuildInitFontOutput(ImFontAtlas* atlas, ImFont* font); // Using DestroyFontOutput/InitFontOutput sequence useful notably if font loader params have changed
4065+
IMGUI_API void ImFontAtlasBuildDestroyFontOutput(ImFontAtlas* atlas, ImFont* font);
40644066
IMGUI_API void ImFontAtlasBuildDestroyFontSourceData(ImFontAtlas* atlas, ImFontConfig* src);
4065-
IMGUI_API void ImFontAtlasBuildReloadAll(ImFontAtlas* atlas); // Reinit/rebuild, notably if font loader params have changed.
4066-
IMGUI_API void ImFontAtlasBuildReloadFont(ImFontAtlas* atlas, ImFontConfig* src); // Reinit/rebuild, notably if font loader params have changed.
40674067

40684068
IMGUI_API ImFontBaked* ImFontAtlasBuildAddFontBaked(ImFontAtlas* atlas, ImFont* font, float font_size, ImGuiID baked_id);
40694069
IMGUI_API ImFontBaked* ImFontAtlasBuildGetClosestFontBakedMatch(ImFontAtlas* atlas, ImFont* font, float font_size);

0 commit comments

Comments
 (0)
Please sign in to comment.