Przeglądaj źródła

Fonts: fixed memory leaks, shutting down font loader, and on AddFont() failure in FreeType backend.

ocornut 5 miesięcy temu
rodzic
commit
144f444217
3 zmienionych plików z 13 dodań i 8 usunięć
  1. 1 2
      imgui.h
  2. 8 6
      imgui_draw.cpp
  3. 4 0
      misc/freetype/imgui_freetype.cpp

+ 1 - 2
imgui.h

@@ -3711,7 +3711,7 @@ struct ImFont
     IMGUI_API ImFontBaked*      GetFontBaked(float font_size);  // Get or create baked data for given size
     IMGUI_API bool              IsGlyphInFont(ImWchar c);
     bool                        IsLoaded() const                { return ContainerAtlas != NULL; }
-    const char*                 GetDebugName() const            { return Sources ? Sources->Name : "<unknown>"; }
+    const char*                 GetDebugName() const            { return Sources ? Sources[0].Name : "<unknown>"; }
 
     // [Internal] Don't use!
     // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
@@ -3720,7 +3720,6 @@ struct ImFont
     IMGUI_API const char*       CalcWordWrapPosition(float size, const char* text, const char* text_end, float wrap_width);
     IMGUI_API void              RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c, const ImVec4* cpu_fine_clip = NULL);
     IMGUI_API void              RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false);
-
 #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
     inline const char*          CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) { return CalcWordWrapPosition(Sources[0].SizePixels * scale, text, text_end, wrap_width); }
 #endif

+ 8 - 6
imgui_draw.cpp

@@ -3034,7 +3034,10 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
         new_font_cfg.GlyphExcludeRanges = (ImWchar*)ImMemdup(font_cfg->GlyphExcludeRanges, sizeof(font_cfg->GlyphExcludeRanges[0]) * (size + 1));
     }
     if (font_cfg->FontLoader != NULL)
+    {
         IM_ASSERT(font_cfg->FontLoader->FontBakedLoadGlyph != NULL);
+        IM_ASSERT(font_cfg->FontLoader->LoaderInit == NULL && font_cfg->FontLoader->LoaderShutdown == NULL); // FIXME-NEWATLAS: Unsupported yet.
+    }
     IM_ASSERT(font_cfg->FontLoaderData == NULL);
 
     // Round font size
@@ -3369,11 +3372,6 @@ void ImFontAtlasBuildSetupFontLoader(ImFontAtlas* atlas, const ImFontLoader* fon
     ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(atlas);
     ImFontAtlasBuildDestroy(atlas);
 
-    if (atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
-    {
-        atlas->FontLoader->LoaderShutdown(atlas);
-        IM_ASSERT(atlas->FontLoaderData == NULL);
-    }
     atlas->FontLoader = font_loader;
     atlas->FontLoaderName = font_loader ? font_loader->Name : "NULL";
     if (atlas->FontLoader && atlas->FontLoader->LoaderInit)
@@ -4187,7 +4185,11 @@ void ImFontAtlasBuildDestroy(ImFontAtlas* atlas)
         if (loader && loader->FontSrcDestroy != NULL)
             loader->FontSrcDestroy(atlas, &font_cfg);
     }
-
+    if (atlas->FontLoader && atlas->FontLoader->LoaderShutdown)
+    {
+        atlas->FontLoader->LoaderShutdown(atlas);
+        IM_ASSERT(atlas->FontLoaderData == NULL);
+    }
     IM_DELETE(atlas->Builder);
     atlas->Builder = NULL;
 }

+ 4 - 0
misc/freetype/imgui_freetype.cpp

@@ -416,7 +416,11 @@ bool ImGui_ImplFreeType_FontSrcInit(ImFontAtlas* atlas, ImFontConfig* src)
     src->FontLoaderData = bd_font_data;
 
     if (!bd_font_data->InitFont(bd->Library, src, atlas->FontBuilderFlags))
+    {
+        IM_DELETE(bd_font_data);
+        src->FontLoaderData = NULL;
         return false;
+    }
 
     return true;
 }