Fonts: rework toward reducing reliance on ImFontConfig::DstFont since we ought to separate them.

This commit is contained in:
ocornut
2025-04-24 18:27:55 +02:00
parent 2b0d49a905
commit 5310f5fba3
2 changed files with 23 additions and 15 deletions

View File

@@ -2998,10 +2998,14 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg_in)
font = Fonts.back();
}
// Add to list
Sources.push_back(*font_cfg_in);
ImFontConfig* font_cfg = &Sources.back();
if (font_cfg->DstFont == NULL)
font_cfg->DstFont = font;
font->Sources.push_back(font_cfg);
ImFontAtlasBuildUpdatePointers(this); // Pointers to Sources are otherwise dangling after we called Sources.push_back().
if (font_cfg->FontDataOwnedByAtlas == false)
{
font_cfg->FontDataOwnedByAtlas = true;
@@ -3025,10 +3029,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg_in)
}
IM_ASSERT(font_cfg->FontLoaderData == NULL);
// Pointers to Sources are otherwise dangling
font->Sources.push_back(font_cfg);
ImFontAtlasBuildUpdatePointers(this);
if (!ImFontAtlasFontInitSource(this, font_cfg))
if (!ImFontAtlasFontSourceInit(this, font_cfg))
{
// Rollback (this is a fragile/rarely exercised code-path. TestSuite's "misc_atlas_add_invalid_font" aim to test this)
ImFontAtlasFontDestroySourceData(this, font_cfg);
@@ -3041,6 +3042,8 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg_in)
}
return NULL;
}
ImFontAtlasFontSourceAddToFont(this, font, font_cfg);
return font;
}
@@ -3563,9 +3566,16 @@ void ImFontAtlasFontDestroyOutput(ImFontAtlas* atlas, ImFont* font)
//-----------------------------------------------------------------------------------------------------------------------------
bool ImFontAtlasFontInitSource(ImFontAtlas* atlas, ImFontConfig* src)
bool ImFontAtlasFontSourceInit(ImFontAtlas* atlas, ImFontConfig* src)
{
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
if (loader->FontSrcInit != NULL && !loader->FontSrcInit(atlas, src))
return false;
return true;
}
void ImFontAtlasFontSourceAddToFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src)
{
ImFont* font = src->DstFont;
if (src->MergeMode == false)
{
font->ClearOutputData();
@@ -3573,14 +3583,8 @@ bool ImFontAtlasFontInitSource(ImFontAtlas* atlas, ImFontConfig* src)
font->ContainerAtlas = atlas;
IM_ASSERT(font->Sources[0] == src);
}
const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader;
if (loader->FontSrcInit != NULL && !loader->FontSrcInit(atlas, src))
return false;
atlas->TexIsBuilt = false; // For legacy backends
ImFontAtlasBuildSetupFontSpecialGlyphs(atlas, font, src);
return true;
}
void ImFontAtlasFontDestroySourceData(ImFontAtlas* atlas, ImFontConfig* src)
@@ -4104,6 +4108,11 @@ void ImFontAtlasBuildClear(ImFontAtlas* atlas)
ImFontAtlasBuildDestroy(atlas);
ImFontAtlasTextureAdd(atlas, new_tex_size.x, new_tex_size.y);
ImFontAtlasBuildInit(atlas);
for (ImFontConfig& src : atlas->Sources)
ImFontAtlasFontSourceInit(atlas, &src);
for (ImFont* font : atlas->Fonts)
for (ImFontConfig* src : font->Sources)
ImFontAtlasFontSourceAddToFont(atlas, font, src);
}
// You should not need to call this manually!
@@ -4159,8 +4168,6 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
// Register fonts
ImFontAtlasBuildUpdatePointers(atlas);
for (ImFontConfig& cfg : atlas->Sources)
ImFontAtlasFontInitSource(atlas, &cfg);
// Update UV coordinates etc. stored in bound ImDrawListSharedData instance
ImFontAtlasUpdateDrawListsSharedData(atlas);

View File

@@ -3784,7 +3784,8 @@ IMGUI_API void ImFontAtlasBuildPreloadAllGlyphRanges(ImFontAtlas* a
IMGUI_API void ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, float size, int* out_oversample_h, int* out_oversample_v);
IMGUI_API void ImFontAtlasBuildDiscardBakes(ImFontAtlas* atlas, int unused_frames);
IMGUI_API bool ImFontAtlasFontInitSource(ImFontAtlas* atlas, ImFontConfig* src);
IMGUI_API bool ImFontAtlasFontSourceInit(ImFontAtlas* atlas, ImFontConfig* src);
IMGUI_API void ImFontAtlasFontSourceAddToFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src);
IMGUI_API void ImFontAtlasFontDestroySourceData(ImFontAtlas* atlas, ImFontConfig* src);
IMGUI_API bool ImFontAtlasFontInitOutput(ImFontAtlas* atlas, ImFont* font); // Using FontDestroyOutput/FontInitOutput sequence useful notably if font loader params have changed
IMGUI_API void ImFontAtlasFontDestroyOutput(ImFontAtlas* atlas, ImFont* font);