From 5b0ff5e36aa3ce3b34fb54a1075a6588f2e7a5e6 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 7 Feb 2025 18:48:01 +0100 Subject: [PATCH] WIP - Rework ImFontLoader signatures. InitBaked may return false to signify this size is not supported. --- imgui_draw.cpp | 23 ++++++++++------------- imgui_internal.h | 4 ++-- misc/freetype/imgui_freetype.cpp | 22 ++++++++++------------ 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 2d11c7c43..6312f69cb 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -3008,7 +3008,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) IM_ASSERT((size <= 64) && "GlyphExcludeRanges[] size must be small!"); } if (font_cfg->FontLoader != NULL) - IM_ASSERT(font_cfg->FontLoader->FontBakedAddGlyph != NULL); + IM_ASSERT(font_cfg->FontLoader->FontBakedLoadGlyph != NULL); IM_ASSERT(font_cfg->FontLoaderData == NULL); // Round font size @@ -4291,12 +4291,8 @@ ImFontGlyph* ImFontBaked::BuildLoadGlyph(ImWchar codepoint) ImFontConfig* src = &srcs[src_n]; const ImFontLoader* loader = src->FontLoader ? src->FontLoader : atlas->FontLoader; if (!src->GlyphExcludeRanges || ImFontAtlasBuildAcceptCodepointForSource(src, codepoint)) - if (loader->FontBakedAddGlyph(atlas, src, baked, loader_user_data_p, codepoint)) - { - // FIXME: Add hooks for e.g. #7962 - ImFontGlyph* glyph = &baked->Glyphs.back(); - return glyph; - } + if (ImFontGlyph* glyph = loader->FontBakedLoadGlyph(atlas, src, baked, loader_user_data_p, codepoint)) + return glyph; // FIXME: Add hooks for e.g. #7962 loader_user_data_p += loader->FontBakedSrcLoaderDataSize; } @@ -4409,7 +4405,7 @@ static bool ImGui_ImplStbTrueType_FontSrcContainsGlyph(ImFontAtlas* atlas, ImFon return glyph_index != 0; } -static void ImGui_ImplStbTrueType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*) +static bool ImGui_ImplStbTrueType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*) { IM_UNUSED(atlas); @@ -4424,16 +4420,17 @@ static void ImGui_ImplStbTrueType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig baked->Ascent = ImCeil(unscaled_ascent * scale_for_layout); baked->Descent = ImFloor(unscaled_descent * scale_for_layout); } + return true; } -static bool ImGui_ImplStbTrueType_FontBakedAddGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*, ImWchar codepoint) +static ImFontGlyph* ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void*, ImWchar codepoint) { // Search for first font which has the glyph ImGui_ImplStbTrueType_FontSrcData* bd_font_data = (ImGui_ImplStbTrueType_FontSrcData*)src->FontLoaderData; IM_ASSERT(bd_font_data); int glyph_index = stbtt_FindGlyphIndex(&bd_font_data->FontInfo, (int)codepoint); if (glyph_index == 0) - return false; // Not found + return NULL; // Fonts unit to pixels int oversample_h, oversample_v; @@ -4466,7 +4463,7 @@ static bool ImGui_ImplStbTrueType_FontBakedAddGlyph(ImFontAtlas* atlas, ImFontCo { // Pathological out of memory case (TexMaxWidth/TexMaxHeight set too small?) IM_ASSERT_USER_ERROR(pack_id >= 0, "Out of texture memory."); - return false; + return NULL; } ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, pack_id); @@ -4515,7 +4512,7 @@ static bool ImGui_ImplStbTrueType_FontBakedAddGlyph(ImFontAtlas* atlas, ImFontCo glyph = ImFontAtlasBakedAddFontGlyph(atlas, baked, src, glyph); } - return true; + return glyph; } const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype() @@ -4527,7 +4524,7 @@ const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype() loader.FontSrcContainsGlyph = ImGui_ImplStbTrueType_FontSrcContainsGlyph; loader.FontBakedInit = ImGui_ImplStbTrueType_FontBakedInit; loader.FontBakedDestroy = NULL; - loader.FontBakedAddGlyph = ImGui_ImplStbTrueType_FontBakedAddGlyph; + loader.FontBakedLoadGlyph = ImGui_ImplStbTrueType_FontBakedLoadGlyph; return &loader; } diff --git a/imgui_internal.h b/imgui_internal.h index cc737d372..e4b6f8fe5 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -3954,9 +3954,9 @@ struct ImFontLoader bool (*FontSrcInit)(ImFontAtlas* atlas, ImFontConfig* src); void (*FontSrcDestroy)(ImFontAtlas* atlas, ImFontConfig* src); bool (*FontSrcContainsGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint); - void (*FontBakedInit)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src); + bool (*FontBakedInit)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src); void (*FontBakedDestroy)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src); - bool (*FontBakedAddGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint); + ImFontGlyph* (*FontBakedLoadGlyph)(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint); // Size of backend data, Per Baked * Per Source. Buffers are managed by core to avoid excessive allocations. // FIXME: At this point the two other types of buffers may be managed by core to be consistent? diff --git a/misc/freetype/imgui_freetype.cpp b/misc/freetype/imgui_freetype.cpp index dcd684adf..763ed474e 100644 --- a/misc/freetype/imgui_freetype.cpp +++ b/misc/freetype/imgui_freetype.cpp @@ -437,7 +437,7 @@ void ImGui_ImplFreeType_FontSrcDestroy(ImFontAtlas* atlas, ImFontConfig* src) src->FontLoaderData = NULL; } -void ImGui_ImplFreeType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src) +bool ImGui_ImplFreeType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src) { IM_UNUSED(atlas); const float size = baked->Size; @@ -479,6 +479,7 @@ void ImGui_ImplFreeType_FontBakedInit(ImFontAtlas* atlas, ImFontConfig* src, ImF baked->Ascent = bd_baked_data->Ascender; baked->Descent = bd_baked_data->Descender; } + return true; } void ImGui_ImplFreeType_FontBakedDestroy(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src) @@ -492,12 +493,12 @@ void ImGui_ImplFreeType_FontBakedDestroy(ImFontAtlas* atlas, ImFontConfig* src, bd_baked_data->~ImGui_ImplFreeType_FontSrcBakedData(); // ~IM_PLACEMENT_DELETE() } -bool ImGui_ImplFreeType_FontBakedAddGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint) +ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImFontBaked* baked, void* loader_data_for_baked_src, ImWchar codepoint) { ImGui_ImplFreeType_FontSrcData* bd_font_data = (ImGui_ImplFreeType_FontSrcData*)src->FontLoaderData; uint32_t glyph_index = FT_Get_Char_Index(bd_font_data->FtFace, codepoint); if (glyph_index == 0) - return false; // Not found + return NULL; if (bd_font_data->BakedLastActivated != baked) { @@ -509,18 +510,15 @@ bool ImGui_ImplFreeType_FontBakedAddGlyph(ImFontAtlas* atlas, ImFontConfig* src, const FT_Glyph_Metrics* metrics = bd_font_data->LoadGlyph(codepoint); if (metrics == NULL) - return false; + return NULL; // Render glyph into a bitmap (currently held by FreeType) FT_Face face = bd_font_data->FtFace; FT_GlyphSlot slot = face->glyph; FT_Error error = FT_Render_Glyph(slot, bd_font_data->RenderMode); - if (error != 0) - return false; - const FT_Bitmap* ft_bitmap = &slot->bitmap; - if (ft_bitmap == nullptr) - return false; + if (error != 0 || ft_bitmap == nullptr) + return NULL; const int w = (int)ft_bitmap->width; const int h = (int)ft_bitmap->rows; @@ -540,7 +538,7 @@ bool ImGui_ImplFreeType_FontBakedAddGlyph(ImFontAtlas* atlas, ImFontConfig* src, { // Pathological out of memory case (TexMaxWidth/TexMaxHeight set too small?) IM_ASSERT_USER_ERROR(pack_id >= 0, "Out of texture memory."); - return false; + return NULL; } ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, pack_id); @@ -576,7 +574,7 @@ bool ImGui_ImplFreeType_FontBakedAddGlyph(ImFontAtlas* atlas, ImFontConfig* src, { glyph = ImFontAtlasBakedAddFontGlyph(atlas, baked, src, glyph); } - return true; + return glyph; } bool ImGui_ImplFreetype_FontSrcContainsGlyph(ImFontAtlas* atlas, ImFontConfig* src, ImWchar codepoint) @@ -598,7 +596,7 @@ const ImFontLoader* ImGuiFreeType::GetFontLoader() loader.FontSrcContainsGlyph = ImGui_ImplFreetype_FontSrcContainsGlyph; loader.FontBakedInit = ImGui_ImplFreeType_FontBakedInit; loader.FontBakedDestroy = ImGui_ImplFreeType_FontBakedDestroy; - loader.FontBakedAddGlyph = ImGui_ImplFreeType_FontBakedAddGlyph; + loader.FontBakedLoadGlyph = ImGui_ImplFreeType_FontBakedLoadGlyph; loader.FontBakedSrcLoaderDataSize = sizeof(ImGui_ImplFreeType_FontSrcBakedData); return &loader; }