From 0bb29b821dbaca6880eb8646de6efcb471696a58 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 13 Mar 2025 16:50:19 +0100 Subject: [PATCH] WIP - Fonts: change uses of ImFontAtlasRect to ImTextureRect for simplicity. --- imgui.cpp | 2 +- imgui_draw.cpp | 40 ++++++++++++++------------------ imgui_internal.h | 12 +++------- misc/freetype/imgui_freetype.cpp | 2 +- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 34dd707b9..6f47fee04 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -22478,7 +22478,7 @@ void ImGui::DebugNodeFontGlyph(ImFont* font, const ImFontGlyph* glyph) Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); if (glyph->PackId >= 0) { - ImFontAtlasRect* r = ImFontAtlasPackGetRect(font->ContainerAtlas, glyph->PackId); + ImTextureRect* r = ImFontAtlasPackGetRect(font->ContainerAtlas, glyph->PackId); Text("PackId: %d (%dx%d rect at %d,%d)", glyph->PackId, r->w, r->h, r->x, r->y); } } diff --git a/imgui_draw.cpp b/imgui_draw.cpp index a06943094..8fa12109a 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -3240,7 +3240,7 @@ int ImFontAtlas::AddCustomRectRegular(int width, int height) ImFontAtlasRectId r_id = ImFontAtlasPackAddRect(this, width, height); if (r_id < 0) return -1; - ImFontAtlasRect* r = ImFontAtlasPackGetRect(this, r_id); + ImTextureRect* r = ImFontAtlasPackGetRect(this, r_id); if (RendererHasTextures) ImFontAtlasTextureBlockQueueUpload(this, TexData, r->x, r->y, r->w, r->h); return r_id; @@ -3274,7 +3274,7 @@ int ImFontAtlas::AddCustomRectFontGlyphForSize(ImFont* font, float font_size, Im ImFontAtlasRectId r_id = ImFontAtlasPackAddRect(this, width, height); if (r_id < 0) return -1; - ImFontAtlasRect* r = ImFontAtlasPackGetRect(this, r_id); + ImTextureRect* r = ImFontAtlasPackGetRect(this, r_id); if (RendererHasTextures) ImFontAtlasTextureBlockQueueUpload(this, TexData, r->x, r->y, r->w, r->h); @@ -3298,11 +3298,7 @@ int ImFontAtlas::AddCustomRectFontGlyphForSize(ImFont* font, float font_size, Im ImTextureRect* ImFontAtlas::GetCustomRectByIndex(int idx) { - IM_STATIC_ASSERT(offsetof(ImTextureRect, x) == offsetof(ImFontAtlasRect, x)); - IM_STATIC_ASSERT(offsetof(ImTextureRect, y) == offsetof(ImFontAtlasRect, y)); - IM_STATIC_ASSERT(offsetof(ImTextureRect, w) == offsetof(ImFontAtlasRect, w)); - IM_STATIC_ASSERT(offsetof(ImTextureRect, h) == offsetof(ImFontAtlasRect, h)); - return (ImTextureRect*)(void*)ImFontAtlasPackGetRect(this, idx); + return ImFontAtlasPackGetRect(this, idx); } void ImFontAtlas::CalcCustomRectUV(const ImTextureRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const @@ -3319,7 +3315,7 @@ bool ImFontAtlasGetMouseCursorTexData(ImFontAtlas* atlas, ImGuiMouseCursor curso if (atlas->Flags & ImFontAtlasFlags_NoMouseCursors) return false; - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, atlas->Builder->PackIdMouseCursors); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, atlas->Builder->PackIdMouseCursors); ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r->x, (float)r->y); ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1]; *out_size = size; @@ -3456,7 +3452,7 @@ static void ImFontAtlasBuildUpdateBasicTexData(ImFontAtlas* atlas, bool add_and_ builder->PackIdMouseCursors = ImFontAtlasPackAddRect(atlas, pack_size.x, pack_size.y); if (builder->PackIdMouseCursors < 0) return; - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, builder->PackIdMouseCursors); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, builder->PackIdMouseCursors); // Draw to texture if (add_and_draw) @@ -3492,7 +3488,7 @@ static void ImFontAtlasBuildUpdateLinesTexData(ImFontAtlas* atlas, bool add_and_ builder->PackIdLinesTexData = ImFontAtlasPackAddRect(atlas, pack_size.x, pack_size.y); if (builder->PackIdLinesTexData < 0) return; - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, builder->PackIdLinesTexData); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, builder->PackIdLinesTexData); // Register texture region for thick lines // The +2 here is to give space for the end caps, whilst height +1 is to accommodate the fact we have a zero-width row @@ -3602,12 +3598,12 @@ static ImFontGlyph* ImFontAtlasBuildSetupFontBakedEllipsis(ImFontAtlas* atlas, I if (dot_glyph == NULL) return NULL; ImFontAtlasRectId dot_r_id = dot_glyph->PackId; // Deep copy to avoid invalidation of glyphs and rect pointers - ImFontAtlasRect* dot_r = ImFontAtlasPackGetRect(atlas, dot_r_id); + ImTextureRect* dot_r = ImFontAtlasPackGetRect(atlas, dot_r_id); const int dot_spacing = 1; const float dot_step = (dot_glyph->X1 - dot_glyph->X0) + dot_spacing; ImFontAtlasRectId pack_id = ImFontAtlasPackAddRect(atlas, (dot_r->w * 3 + dot_spacing * 2), dot_r->h); - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, pack_id); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id); ImFontGlyph glyph_in = {}; ImFontGlyph* glyph = &glyph_in; @@ -3964,7 +3960,7 @@ void ImFontAtlasBuildRepackTexture(ImFontAtlas* atlas, int w, int h) // FIXME-NEWATLAS-V2: Repacking in batch would be beneficial to packing heuristic, and fix stability. // FIXME-NEWATLAS-TESTS: Test calling RepackTexture with size too small to fits existing rects. ImFontAtlasPackInit(atlas); - ImVector old_rects; + ImVector old_rects; ImVector old_index = builder->RectsIndex; old_rects.swap(builder->Rects); @@ -3972,7 +3968,7 @@ void ImFontAtlasBuildRepackTexture(ImFontAtlas* atlas, int w, int h) { if (index_entry.Used == false) continue; - ImFontAtlasRect& old_r = old_rects[index_entry.TargetIndex]; + ImTextureRect& old_r = old_rects[index_entry.TargetIndex]; if (old_r.w == 0 && old_r.h == 0) continue; ImFontAtlasRectId new_r_id = ImFontAtlasPackAddRect(atlas, old_r.w, old_r.h, &index_entry); @@ -3989,7 +3985,7 @@ void ImFontAtlasBuildRepackTexture(ImFontAtlas* atlas, int w, int h) return; } IM_ASSERT(new_r_id == builder->RectsIndex.index_from_ptr(&index_entry)); - ImFontAtlasRect* new_r = ImFontAtlasPackGetRect(atlas, new_r_id); + ImTextureRect* new_r = ImFontAtlasPackGetRect(atlas, new_r_id); ImFontAtlasTextureBlockCopy(old_tex, old_r.x, old_r.y, new_tex, new_r->x, new_r->y, new_r->w, new_r->h); } IM_ASSERT(old_rects.Size == builder->Rects.Size + builder->RectsDiscardedCount); @@ -4001,7 +3997,7 @@ void ImFontAtlasBuildRepackTexture(ImFontAtlas* atlas, int w, int h) for (ImFontGlyph& glyph : builder->BakedPool[baked_n].Glyphs) if (glyph.PackId != -1) { - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, glyph.PackId); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, glyph.PackId); glyph.U0 = (r->x) * atlas->TexUvScale.x; glyph.V0 = (r->y) * atlas->TexUvScale.y; glyph.U1 = (r->x + r->w) * atlas->TexUvScale.x; @@ -4247,7 +4243,7 @@ void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id) ImFontAtlasRectEntry* index_entry = &builder->RectsIndex[id]; IM_ASSERT(index_entry->Used && index_entry->TargetIndex >= 0); - ImFontAtlasRect* rect = ImFontAtlasPackGetRect(atlas, id); + ImTextureRect* rect = ImFontAtlasPackGetRect(atlas, id); index_entry->Used = false; index_entry->TargetIndex = builder->RectsIndexFreeListStart; @@ -4271,7 +4267,7 @@ ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFon builder->MaxRectSize.y = ImMax(builder->MaxRectSize.y, h); // Pack - ImFontAtlasRect r = { 0, 0, (unsigned short)w, (unsigned short)h }; + ImTextureRect r = { 0, 0, (unsigned short)w, (unsigned short)h }; for (int attempts_remaining = 3; attempts_remaining >= 0; attempts_remaining--) { // Try packing @@ -4315,7 +4311,7 @@ ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFon } // Important: don'return pointer valid until next call to AddRect(), e.g. FindGlyph(), CalcTextSize() can all potentially invalidate previous pointers. -ImFontAtlasRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id) +ImTextureRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id) { IM_ASSERT(id >= 0); ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder; @@ -4554,7 +4550,7 @@ static ImFontGlyph* ImGui_ImplStbTrueType_FontBakedLoadGlyph(ImFontAtlas* atlas, IM_ASSERT_USER_ERROR(pack_id >= 0, "Out of texture memory."); return NULL; } - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, pack_id); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id); // Render stbtt_GetGlyphBitmapBox(&bd_font_data->FontInfo, glyph_index, scale_for_raster_x, scale_for_raster_y, &x0, &y0, &x1, &y1); @@ -5002,7 +4998,7 @@ ImFontGlyph* ImFontAtlasBakedAddFontGlyph(ImFontAtlas* atlas, ImFontBaked* baked // Set UV from packed rectangle if (in_glyph->PackId >= 0) { - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, in_glyph->PackId); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, in_glyph->PackId); IM_ASSERT(in_glyph->U0 == 0.0f && in_glyph->V0 == 0.0f && in_glyph->U1 == 0.0f && in_glyph->V1 == 0.0f); glyph.U0 = (r->x) * atlas->TexUvScale.x; glyph.V0 = (r->y) * atlas->TexUvScale.y; @@ -5045,7 +5041,7 @@ ImFontGlyph* ImFontAtlasBakedAddFontGlyph(ImFontAtlas* atlas, ImFontBaked* baked } // Copy to texture, post-process and queue update for backend -void ImFontAtlasBakedSetFontGlyphBitmap(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImFontGlyph* glyph, ImFontAtlasRect* r, const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch) +void ImFontAtlasBakedSetFontGlyphBitmap(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImFontGlyph* glyph, ImTextureRect* r, const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch) { ImTextureData* tex = atlas->TexData; IM_ASSERT(r->x + r->w <= tex->Width && r->y + r->h <= tex->Height); diff --git a/imgui_internal.h b/imgui_internal.h index fb9cac3dd..61861e0ad 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -143,7 +143,6 @@ struct ImDrawDataBuilder; // Helper to build a ImDrawData instance struct ImDrawListSharedData; // Data shared between all ImDrawList instances struct ImFontAtlasBuilder; // Internal storage for incrementally packing and building a ImFontAtlas struct ImFontAtlasPostProcessData; // Data available to potential texture post-processing functions -struct ImFontAtlasRect; // Packed rectangle (same as ImTextureRect) struct ImFontAtlasRectEntry; // Packed rectangle lookup entry // ImGui @@ -3978,11 +3977,6 @@ IMGUI_API const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype(); // [SECTION] ImFontAtlas internal API //----------------------------------------------------------------------------- -struct ImFontAtlasRect -{ - unsigned short x, y; - unsigned short w, h; -}; typedef int ImFontAtlasRectId; // <0 when invalid // Packed rectangle lookup entry (we need an indirection to allow removing/reordering rectangles) @@ -4020,7 +4014,7 @@ struct ImFontAtlasBuilder { stbrp_context_opaque PackContext; // Actually 'stbrp_context' but we don't want to define this in the header file. ImVector PackNodes; - ImVector Rects; + ImVector Rects; ImVector RectsIndex; // ImFontAtlasRectId -> index into Rects[] ImVector TempBuffer; // Misc scratch buffer int RectsIndexFreeListStart;// First unused entry @@ -4077,12 +4071,12 @@ IMGUI_API void ImFontAtlasBuildPreloadAllGlyphRanges(ImFontAtlas* a IMGUI_API void ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, float size, int* out_oversample_h, int* out_oversample_v); IMGUI_API ImFontGlyph* ImFontAtlasBakedAddFontGlyph(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, const ImFontGlyph* in_glyph); -IMGUI_API void ImFontAtlasBakedSetFontGlyphBitmap(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImFontGlyph* glyph, ImFontAtlasRect* r, const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch); +IMGUI_API void ImFontAtlasBakedSetFontGlyphBitmap(ImFontAtlas* atlas, ImFontBaked* baked, ImFontConfig* src, ImFontGlyph* glyph, ImTextureRect* r, const unsigned char* src_pixels, ImTextureFormat src_fmt, int src_pitch); IMGUI_API ImGuiID ImFontAtlasBakedGetId(ImGuiID font_id, float baked_size); IMGUI_API void ImFontAtlasPackInit(ImFontAtlas* atlas); IMGUI_API ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFontAtlasRectEntry* overwrite_entry = NULL); -IMGUI_API ImFontAtlasRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id); +IMGUI_API ImTextureRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id); IMGUI_API void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id); IMGUI_API void ImFontAtlasUpdateNewFrame(ImFontAtlas* atlas, int frame_count); diff --git a/misc/freetype/imgui_freetype.cpp b/misc/freetype/imgui_freetype.cpp index 2ae99284f..c0ded827c 100644 --- a/misc/freetype/imgui_freetype.cpp +++ b/misc/freetype/imgui_freetype.cpp @@ -534,7 +534,7 @@ ImFontGlyph* ImGui_ImplFreeType_FontBakedLoadGlyph(ImFontAtlas* atlas, ImFontCon IM_ASSERT_USER_ERROR(pack_id >= 0, "Out of texture memory."); return NULL; } - ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, pack_id); + ImTextureRect* r = ImFontAtlasPackGetRect(atlas, pack_id); // Render pixels to our temporary buffer atlas->Builder->TempBuffer.resize(w * h * 4);