Skip to content

Commit e4b08e8

Browse files
committed
WIP - Fonts: changing loader/backend or loader flags may be done without losing custom rects. Sharing more code.
1 parent 33e5db4 commit e4b08e8

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
@@ -21309,9 +21309,16 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas)
2130921309
ImFontAtlasBuildSetupFontLoader(atlas, loader_freetype);
2131021310
if (loader_current == loader_freetype)
2131121311
{
21312-
Text("Shared FreeType Loader Flags:");
21313-
if (ImGuiFreeType::DebugEditFontBuilderFlags(&atlas->FontBuilderFlags))
21314-
ImFontAtlasBuildReloadAll(atlas);
21312+
unsigned int loader_flags = atlas->FontBuilderFlags;
21313+
Text("Shared FreeType Loader Flags: 0x%08", loader_flags);
21314+
if (ImGuiFreeType::DebugEditFontBuilderFlags(&loader_flags))
21315+
{
21316+
for (ImFont* font : atlas->Fonts)
21317+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
21318+
atlas->FontBuilderFlags = loader_flags;
21319+
for (ImFont* font : atlas->Fonts)
21320+
ImFontAtlasBuildInitFontOutput(atlas, font);
21321+
}
2131521322
}
2131621323
#else
2131721324
BeginDisabled();
@@ -21338,8 +21345,9 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas)
2133821345
if (Button("Grow"))
2133921346
ImFontAtlasBuildGrowTexture(atlas);
2134021347
SameLine();
21341-
if (Button("Clear Output"))
21348+
if (Button("Clear All"))
2134221349
ImFontAtlasBuildClear(atlas);
21350+
SetItemTooltip("Destroy cache and custom rectangles.");
2134321351

2134421352
for (int tex_n = 0; tex_n < atlas->TexList.Size; tex_n++)
2134521353
{
@@ -22407,9 +22415,14 @@ void ImGui::DebugNodeFont(ImFont* font)
2240722415
#ifdef IMGUI_ENABLE_FREETYPE
2240822416
if (loader->Name != NULL && strcmp(loader->Name, "FreeType") == 0)
2240922417
{
22410-
Text("FreeType Loader Flags: 0x%08X", src->FontBuilderFlags);
22411-
if (ImGuiFreeType::DebugEditFontBuilderFlags(&src->FontBuilderFlags))
22412-
ImFontAtlasBuildReloadFont(atlas, src);
22418+
unsigned int loader_flags = src->FontBuilderFlags;
22419+
Text("FreeType Loader Flags: 0x%08X", loader_flags);
22420+
if (ImGuiFreeType::DebugEditFontBuilderFlags(&loader_flags))
22421+
{
22422+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
22423+
src->FontBuilderFlags = loader_flags;
22424+
ImFontAtlasBuildInitFontOutput(atlas, font);
22425+
}
2241322426
}
2241422427
#endif
2241522428
TreePop();

imgui_draw.cpp

+39-37
Original file line numberDiff line numberDiff line change
@@ -2654,14 +2654,11 @@ void ImFontAtlas::CompactCache()
26542654
void ImFontAtlas::ClearInputData()
26552655
{
26562656
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
2657+
2658+
for (ImFont* font : Fonts)
2659+
ImFontAtlasBuildDestroyFontOutput(this, font);
26572660
for (ImFontConfig& font_cfg : Sources)
2658-
{
2659-
const ImFontLoader* loader = font_cfg.FontLoader ? font_cfg.FontLoader : FontLoader;
2660-
if (loader && loader->FontSrcDestroy != NULL)
2661-
loader->FontSrcDestroy(this, &font_cfg);
26622661
ImFontAtlasBuildDestroyFontSourceData(this, &font_cfg);
2663-
}
2664-
26652662
for (ImFont* font : Fonts)
26662663
{
26672664
// When clearing this we lose access to the font name and other information used to build the font.
@@ -3203,15 +3200,9 @@ void ImFontAtlas::RemoveFont(ImFont* font)
32033200
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas!");
32043201
font->ClearOutputData();
32053202

3203+
ImFontAtlasBuildDestroyFontOutput(this, font);
32063204
for (int src_n = 0; src_n < font->SourcesCount; src_n++)
3207-
{
3208-
ImFontConfig* src = (ImFontConfig*)(void*)&font->Sources[src_n];
3209-
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : FontLoader;
3210-
if (loader && loader->FontSrcDestroy != NULL)
3211-
loader->FontSrcDestroy(this, src);
3212-
if (src->FontData != NULL && src->FontDataOwnedByAtlas)
3213-
IM_FREE(src->FontData);
3214-
}
3205+
ImFontAtlasBuildDestroyFontSourceData(this, &font->Sources[src_n]);
32153206

32163207
bool removed = Fonts.find_erase(font);
32173208
IM_ASSERT(removed);
@@ -3384,13 +3375,20 @@ void ImFontAtlasBuildSetupFontLoader(ImFontAtlas* atlas, const ImFontLoader* fon
33843375

33853376
// Note that texture size estimate is likely incorrect in this situation, as FreeType backend doesn't use oversampling.
33863377
ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(atlas);
3387-
ImFontAtlasBuildDestroy(atlas);
3378+
3379+
for (ImFont* font : atlas->Fonts)
3380+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
3381+
if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
3382+
atlas->FontLoader->LoaderShutdown(atlas);
33883383

33893384
atlas->FontLoader = font_loader;
33903385
atlas->FontLoaderName = font_loader ? font_loader->Name : "NULL";
3386+
IM_ASSERT(atlas->FontLoaderData == NULL);
33913387

3392-
ImFontAtlasBuildAddTexture(atlas, new_tex_size.x, new_tex_size.y);
3393-
ImFontAtlasBuildInit(atlas);
3388+
if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderInit)
3389+
atlas->FontLoader->LoaderInit(atlas);
3390+
for (ImFont* font : atlas->Fonts)
3391+
ImFontAtlasBuildInitFontOutput(atlas, font);
33943392
}
33953393

33963394
// Preload all glyph ranges for legacy backends.
@@ -3568,18 +3566,31 @@ static void ImFontAtlasBuildUpdateLinesTexData(ImFontAtlas* atlas, bool add_and_
35683566

35693567
//-----------------------------------------------------------------------------------------------------------------------------
35703568

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

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

35853596
//-----------------------------------------------------------------------------------------------------------------------------
@@ -4154,13 +4165,8 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41544165
#else
41554166
IM_ASSERT(0); // Invalid Build function
41564167
#endif
4157-
return; // ImFontAtlasBuildSetupFontLoader() automatically call ImFontAtlasBuildInit()
41584168
}
41594169

4160-
IM_ASSERT(atlas->FontLoaderData == NULL);
4161-
if (atlas->FontLoader->LoaderInit)
4162-
atlas->FontLoader->LoaderInit(atlas);
4163-
41644170
// Create initial texture size
41654171
if (atlas->TexData == NULL || atlas->TexData->Pixels == NULL)
41664172
ImFontAtlasBuildAddTexture(atlas, ImUpperPowerOfTwo(atlas->TexMinWidth), ImUpperPowerOfTwo(atlas->TexMinHeight));
@@ -4171,6 +4177,8 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41714177
{
41724178
IM_ASSERT(atlas->Builder == NULL);
41734179
builder = atlas->Builder = IM_NEW(ImFontAtlasBuilder)();
4180+
if (atlas->FontLoader->LoaderInit)
4181+
atlas->FontLoader->LoaderInit(atlas);
41744182
}
41754183

41764184
ImFontAtlasBuildUpdateRendererHasTexturesFromContext(atlas);
@@ -4199,13 +4207,7 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41994207
void ImFontAtlasBuildDestroy(ImFontAtlas* atlas)
42004208
{
42014209
for (ImFont* font : atlas->Fonts)
4202-
font->ClearOutputData();
4203-
for (ImFontConfig& font_cfg : atlas->Sources)
4204-
{
4205-
const ImFontLoader* loader = font_cfg.FontLoader ? font_cfg.FontLoader : atlas->FontLoader;
4206-
if (loader && loader->FontSrcDestroy != NULL)
4207-
loader->FontSrcDestroy(atlas, &font_cfg);
4208-
}
4210+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
42094211
if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
42104212
{
42114213
atlas->FontLoader->LoaderShutdown(atlas);

imgui_internal.h

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

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

0 commit comments

Comments
 (0)