WIP - Restore a functional AddCustomRectFontGlyph().

This commit is contained in:
ocornut 2025-02-05 17:51:46 +01:00
parent 9b583b9da2
commit 61d975c8f7
2 changed files with 29 additions and 19 deletions

17
imgui.h
View file

@ -3618,7 +3618,7 @@ struct ImFontAtlas
IMGUI_API void RemoveFont(ImFont* font);
IMGUI_API void Clear(); // Clear everything (input fonts, output glyphs/textures)
IMGUI_API void ClearCache(); // Clear cached glyphs and textures.
IMGUI_API void ClearCache(); // Clear cached glyphs and textures. Invalidates all AddCustomRectXXX return values.
// As we are transitioning toward a new font system, we expect to obsolete those soon:
IMGUI_API void ClearInputData(); // [OBSOLETE] Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts.
@ -3668,7 +3668,7 @@ struct ImFontAtlas
// You can request your rectangles to be mapped as font glyph (given a font + Unicode point),
// so you can render e.g. custom colorful icons and use them as regular glyphs.
// - Since 1.92.X, packing is done immediately in the function call. Returns >= on success, <0 on error.
// - You can render your pixels into the texture right after calling the AddCustomRectXXX functions, without waiting for the Build() call.
// - You can render your pixels into the texture right after calling the AddCustomRectXXX() functions.
// - If your backend supports ImGuiBackendFlags_RendererHasTextures:
// Texture may be resized, so you cannot cache UV coordinates: always use CalcCustomRectUV().
// - If you render colored output into your AddCustomRectRegular() rectangle: set 'atlas->TexPixelsUseColors = true'
@ -3676,7 +3676,10 @@ struct ImFontAtlas
// - Read docs/FONTS.md for more details about using colorful icons.
// - Note: this API may be redesigned later in order to support multi-monitor varying DPI settings.
IMGUI_API int AddCustomRectRegular(int width, int height);
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0, 0));
IMGUI_API int AddCustomRectFontGlyphForSize(ImFont* font, float font_size, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0, 0));
#endif
IMGUI_API ImTextureRect* GetCustomRectByIndex(int index);
IMGUI_API void CalcCustomRectUV(const ImTextureRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const;
@ -4154,13 +4157,11 @@ namespace ImGui
//static inline void SetScrollPosHere() { SetScrollHere(); } // OBSOLETED in 1.42
}
//-- OBSOLETED in 1.91.7 (from January 2025): ImFontAtlasCustomRect becomes ImTextureRect
// - ImFontAtlasCustomRect::X --> ImTextureRect::x
// - ImFontAtlasCustomRect::Y --> ImTextureRect::y
// - ImFontAtlasCustomRect::Width --> ImTextureRect::w
// - ImFontAtlasCustomRect::Height --> ImTextureRect::h
//-- OBSOLETED in 1.92.x: ImFontAtlasCustomRect becomes ImTextureRect
// - ImFontAtlasCustomRect::X,Y --> ImTextureRect::x,y
// - ImFontAtlasCustomRect::Width,Height --> ImTextureRect::w,h
// - ImFontAtlasCustomRect::GlyphColored --> if you need to write to this, instead you can write to 'font->Glyphs.back()->Colored' after calling AddCustomRectFontGlyph()
// We could make ImTextureRect an union to use old names, such 1) this would be confusing 2) the fix is easy 3) ImFontAtlasCustomRect was always a rather esoteric api.
// We could make ImTextureRect an union to use old names, but 1) this would be confusing 2) the fix is easy 3) ImFontAtlasCustomRect was always a rather esoteric api.
typedef ImTextureRect ImFontAtlasCustomRect;
/*struct ImFontAtlasCustomRect
{

View file

@ -3202,9 +3202,21 @@ int ImFontAtlas::AddCustomRectRegular(int width, int height)
return r_id;
}
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
// This API does not make sense anymore with scalable fonts.
// - Prefer adding a font source (ImFontConfig) using a custom/procedural loader.
// - You may use ImFontFlags_LockBakedSizes to limit an existing font to known baked sizes:
// ImFont* myfont = io.Fonts->AddFontFromFileTTF(....);
// myfont->GetFontBaked(16.0f);
// myfont->Flags |= ImFontFlags_LockBakedSizes;
int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset)
{
float font_size = font->Sources[0].SizePixels;
return AddCustomRectFontGlyphForSize(font, font_size, codepoint, width, height, advance_x, offset);
}
// FIXME: we automatically set glyph.Colored=true by default.
// If you need to alter this, you can write 'font->Glyphs.back()->Colored' after calling AddCustomRectFontGlyph().
int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset)
int ImFontAtlas::AddCustomRectFontGlyphForSize(ImFont* font, float font_size, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset)
{
#ifdef IMGUI_USE_WCHAR32
IM_ASSERT(codepoint <= IM_UNICODE_CODEPOINT_MAX);
@ -3212,7 +3224,9 @@ int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int wid
IM_ASSERT(font != NULL);
IM_ASSERT(width > 0 && width <= 0xFFFF);
IM_ASSERT(height > 0 && height <= 0xFFFF);
#if 0
ImFontBaked* baked = font->GetFontBaked(font_size);
ImFontAtlasRectId r_id = ImFontAtlasPackAddRect(this, width, height);
if (r_id < 0)
return -1;
@ -3220,8 +3234,8 @@ int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int wid
if (RendererHasTextures)
ImFontAtlasTextureBlockQueueUpload(this, TexData, r->x, r->y, r->w, r->h);
if (font->IsGlyphLoaded(codepoint))
ImFontAtlasBuildDiscardFontGlyph(this, font, (ImFontGlyph*)(void*)font->FindGlyph(codepoint));
if (baked->IsGlyphLoaded(codepoint))
ImFontAtlasBuildDiscardFontBakedGlyph(this, font, baked, (ImFontGlyph*)(void*)baked->FindGlyph(codepoint));
ImFontGlyph glyph;
glyph.Codepoint = codepoint;
@ -3233,15 +3247,10 @@ int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int wid
glyph.Visible = true;
glyph.Colored = true; // FIXME: Arbitrary
glyph.PackId = r_id;
ImFontAtlasBuildAddFontGlyph(this, font, &font->Sources[0], &glyph);
ImFontAtlasBakedAddFontGlyph(this, baked, &font->Sources[0], &glyph);
return r_id;
#endif
// FIXME-BAKED: Need a design for AddCustomRectFontGlyph()
IM_UNUSED(codepoint);
IM_UNUSED(offset);
IM_UNUSED(advance_x);
return -1;
}
#endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
ImTextureRect* ImFontAtlas::GetCustomRectByIndex(int idx)
{