Skip to content

Commit 74b53b4

Browse files
committed
WIP - Fonts: added RemoveCustomRect().
+ internally add ImFontAtlasPackReuseRectEntry()
1 parent 124dcd3 commit 74b53b4

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

imgui.h

+1
Original file line numberDiff line numberDiff line change
@@ -3731,6 +3731,7 @@ struct ImFontAtlas
37313731
// - AddCustomRectFontGlyph() --> Prefer using custom ImFontLoader inside ImFontConfig
37323732
// - ImFontAtlasCustomRect --> Renamed to ImFontAtlasRect
37333733
IMGUI_API ImFontAtlasRectId AddCustomRect(int width, int height); // Register a rectangle. Return -1 (ImFontAtlasRectId_Invalid) on error.
3734+
IMGUI_API void RemoveCustomRect(ImFontAtlasRectId id); // Unregister a rectangle. Existing pixels will stay in texture until resized / garbage collected.
37343735
IMGUI_API bool GetCustomRect(ImFontAtlasRectId id, ImFontAtlasRect* out_r) const; // Get rectangle coordinates for current texture. Valid immediately, never store this (read above)!
37353736

37363737
//-------------------------------------------

imgui_draw.cpp

+19-10
Original file line numberDiff line numberDiff line change
@@ -2501,8 +2501,10 @@ void ImTextureData::DestroyPixels()
25012501
// - ImFontAtlasBuildNotifySetFont()
25022502
//-----------------------------------------------------------------------------
25032503
// - ImFontAtlas::AddCustomRect()
2504-
// - ImFontAtlas::AddCustomRectFontGlyph()
2504+
// - ImFontAtlas::RemoveCustomRect()
25052505
// - ImFontAtlas::GetCustomRect()
2506+
// - ImFontAtlas::AddCustomRectFontGlyph() [legacy]
2507+
// - ImFontAtlas::AddCustomRectFontGlyphForSize() [legacy]
25062508
// - ImFontAtlasGetMouseCursorTexData()
25072509
//-----------------------------------------------------------------------------
25082510
// - ImFontAtlasBuildMain()
@@ -2540,6 +2542,7 @@ void ImTextureData::DestroyPixels()
25402542
//-----------------------------------------------------------------------------
25412543
// - ImFontAtlasPackInit()
25422544
// - ImFontAtlasPackAllocRectEntry()
2545+
// - ImFontAtlasPackReuseRectEntry()
25432546
// - ImFontAtlasPackDiscardRect()
25442547
// - ImFontAtlasPackAddRect()
25452548
// - ImFontAtlasPackGetRect()
@@ -3242,6 +3245,12 @@ ImFontAtlasRectId ImFontAtlas::AddCustomRect(int width, int height)
32423245
return r_id;
32433246
}
32443247

3248+
void ImFontAtlas::RemoveCustomRect(ImFontAtlasRectId id)
3249+
{
3250+
IM_ASSERT(id != ImFontAtlasRectId_Invalid);
3251+
ImFontAtlasPackDiscardRect(this, id);
3252+
}
3253+
32453254
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
32463255
// This API does not make sense anymore with scalable fonts.
32473256
// - Prefer adding a font source (ImFontConfig) using a custom/procedural loader.
@@ -4098,7 +4107,7 @@ ImVec2i ImFontAtlasBuildGetTextureSizeEstimate(ImFontAtlas* atlas)
40984107
return ImVec2i(new_tex_w, new_tex_h);
40994108
}
41004109

4101-
// Clear all output. Invalidates all AddCustomRectXXX return values.
4110+
// Clear all output. Invalidates all AddCustomRect() return values!
41024111
void ImFontAtlasBuildClear(ImFontAtlas* atlas)
41034112
{
41044113
ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(atlas);
@@ -4240,6 +4249,13 @@ static ImFontAtlasRectId ImFontAtlasPackAllocRectEntry(ImFontAtlas* atlas, int r
42404249
return (ImFontAtlasRectId)index_idx;
42414250
}
42424251

4252+
static ImFontAtlasRectId ImFontAtlasPackReuseRectEntry(ImFontAtlas* atlas, ImFontAtlasRectEntry* overwrite_entry)
4253+
{
4254+
IM_ASSERT(overwrite_entry->Used);
4255+
overwrite_entry->TargetIndex = atlas->Builder->Rects.Size - 1;
4256+
return atlas->Builder->RectsIndex.index_from_ptr(overwrite_entry);
4257+
}
4258+
42434259
// This is expected to be called in batches and followed by a repack
42444260
void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id)
42454261
{
@@ -4303,16 +4319,9 @@ ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFon
43034319

43044320
builder->Rects.push_back(r);
43054321
if (overwrite_entry != NULL)
4306-
{
4307-
// Write into an existing entry instead of adding one (used during repack)
4308-
IM_ASSERT(overwrite_entry->Used);
4309-
overwrite_entry->TargetIndex = builder->Rects.Size - 1;
4310-
return builder->RectsIndex.index_from_ptr(overwrite_entry);
4311-
}
4322+
return ImFontAtlasPackReuseRectEntry(atlas, overwrite_entry); // Write into an existing entry instead of adding one (used during repack)
43124323
else
4313-
{
43144324
return ImFontAtlasPackAllocRectEntry(atlas, builder->Rects.Size - 1);
4315-
}
43164325
}
43174326

43184327
// Important: don'return pointer valid until next call to AddRect(), e.g. FindGlyph(), CalcTextSize() can all potentially invalidate previous pointers.

imgui_internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -3988,8 +3988,8 @@ IMGUI_API const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype();
39883988
// Having this also makes it easier to e.g. sort rectangles during repack.
39893989
struct ImFontAtlasRectEntry
39903990
{
3991-
int TargetIndex : 31; // When Used: ImFontAtlasRectId -> into Rects[]. When unused: index to next unused RectsIndex[] slot to consume free-list.
3992-
unsigned int Used : 1;
3991+
int TargetIndex : 31; // When Used: ImFontAtlasRectId -> into Rects[]. When unused: index to next unused RectsIndex[] slot to consume free-list.
3992+
unsigned int Used : 1;
39933993
};
39943994

39953995
// Data available to potential texture post-processing functions

0 commit comments

Comments
 (0)