Explorar o código

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

ocornut hai 4 meses
pai
achega
5310f5fba3
Modificáronse 2 ficheiros con 23 adicións e 15 borrados
  1. 21 14
      imgui_draw.cpp
  2. 2 1
      imgui_internal.h

+ 21 - 14
imgui_draw.cpp

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

+ 2 - 1
imgui_internal.h

@@ -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              ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, float size, int* out_oversample_h, int* out_oversample_v);
 IMGUI_API void              ImFontAtlasBuildDiscardBakes(ImFontAtlas* atlas, int unused_frames);
 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 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 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);
 IMGUI_API void              ImFontAtlasFontDestroyOutput(ImFontAtlas* atlas, ImFont* font);