Skip to content

Commit b37a8c1

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 b37a8c1

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
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

+38-39
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);
@@ -3382,15 +3373,19 @@ void ImFontAtlasBuildSetupFontLoader(ImFontAtlas* atlas, const ImFontLoader* fon
33823373
return;
33833374
IM_ASSERT(!atlas->Locked && "Cannot modify a locked ImFontAtlas!");
33843375

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

33893381
atlas->FontLoader = font_loader;
33903382
atlas->FontLoaderName = font_loader ? font_loader->Name : "NULL";
3383+
IM_ASSERT(atlas->FontLoaderData == NULL);
33913384

3392-
ImFontAtlasBuildAddTexture(atlas, new_tex_size.x, new_tex_size.y);
3393-
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);
33943389
}
33953390

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

35693564
//-----------------------------------------------------------------------------------------------------------------------------
35703565

3571-
void ImFontAtlasBuildReloadAll(ImFontAtlas* atlas)
3566+
bool ImFontAtlasBuildInitFontOutput(ImFontAtlas* atlas, ImFont* font)
35723567
{
3573-
const ImFontLoader* main_loader = atlas->FontLoader;
3574-
ImFontAtlasBuildSetupFontLoader(atlas, NULL);
3575-
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;
35763578
}
35773579

3578-
void ImFontAtlasBuildReloadFont(ImFontAtlas* atlas, ImFontConfig* src)
3580+
// Keep source/input FontData
3581+
void ImFontAtlasBuildDestroyFontOutput(ImFontAtlas* atlas, ImFont* font)
35793582
{
3580-
// FIXME-NEWATLAS: rebuild single font not supported yet.
3581-
IM_UNUSED(src);
3582-
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+
}
35833591
}
35843592

35853593
//-----------------------------------------------------------------------------------------------------------------------------
@@ -4154,13 +4162,8 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41544162
#else
41554163
IM_ASSERT(0); // Invalid Build function
41564164
#endif
4157-
return; // ImFontAtlasBuildSetupFontLoader() automatically call ImFontAtlasBuildInit()
41584165
}
41594166

4160-
IM_ASSERT(atlas->FontLoaderData == NULL);
4161-
if (atlas->FontLoader->LoaderInit)
4162-
atlas->FontLoader->LoaderInit(atlas);
4163-
41644167
// Create initial texture size
41654168
if (atlas->TexData == NULL || atlas->TexData->Pixels == NULL)
41664169
ImFontAtlasBuildAddTexture(atlas, ImUpperPowerOfTwo(atlas->TexMinWidth), ImUpperPowerOfTwo(atlas->TexMinHeight));
@@ -4171,6 +4174,8 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41714174
{
41724175
IM_ASSERT(atlas->Builder == NULL);
41734176
builder = atlas->Builder = IM_NEW(ImFontAtlasBuilder)();
4177+
if (atlas->FontLoader->LoaderInit)
4178+
atlas->FontLoader->LoaderInit(atlas);
41744179
}
41754180

41764181
ImFontAtlasBuildUpdateRendererHasTexturesFromContext(atlas);
@@ -4199,13 +4204,7 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
41994204
void ImFontAtlasBuildDestroy(ImFontAtlas* atlas)
42004205
{
42014206
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-
}
4207+
ImFontAtlasBuildDestroyFontOutput(atlas, font);
42094208
if (atlas->Builder && atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
42104209
{
42114210
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)