WIP - Fonts: fixed crashing password fields.

This commit is contained in:
ocornut 2025-03-17 17:18:05 +01:00
parent 0a91619273
commit e9eb986f65
4 changed files with 30 additions and 22 deletions

View file

@ -4141,6 +4141,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
MouseCursor = ImGuiMouseCursor_Arrow;
MouseStationaryTimer = 0.0f;
InputTextPasswordFontBackupFlags = ImFontFlags_None;
TempInputId = 0;
memset(&DataTypeZeroValue, 0, sizeof(DataTypeZeroValue));
BeginMenuDepth = BeginComboDepth = 0;
@ -4357,7 +4358,6 @@ void ImGui::Shutdown()
g.MenusIdSubmittedThisFrame.clear();
g.InputTextState.ClearFreeMemory();
g.InputTextDeactivatedState.ClearFreeMemory();
g.InputTextPasswordFont.ContainerAtlas = NULL;
g.SettingsWindows.clear();
g.SettingsHandlers.clear();

View file

@ -4139,7 +4139,7 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas)
}
IM_ASSERT(atlas->FontLoaderData == NULL);
if (atlas->FontLoader && atlas->FontLoader->LoaderInit)
if (atlas->FontLoader->LoaderInit)
atlas->FontLoader->LoaderInit(atlas);
// Create initial texture size

View file

@ -2584,7 +2584,8 @@ struct ImGuiContext
// Widget state
ImGuiInputTextState InputTextState;
ImGuiInputTextDeactivatedState InputTextDeactivatedState;
ImFont InputTextPasswordFont;
ImFontBaked InputTextPasswordFontBackupBaked;
ImFontFlags InputTextPasswordFontBackupFlags;
ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc.
ImGuiDataTypeStorage DataTypeZeroValue; // 0 for all data types
int BeginMenuDepth;
@ -3341,6 +3342,7 @@ namespace ImGui
IMGUI_API void UpdateCurrentFontSize();
inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; }
IMGUI_API void PushPasswordFont();
IMGUI_API void PopPasswordFont();
inline ImDrawList* GetForegroundDrawList(ImGuiWindow* window) { return GetForegroundDrawList(window->Viewport); }
IMGUI_API void AddDrawListToDrawDataEx(ImDrawData* draw_data, ImVector<ImDrawList*>* out_list, ImDrawList* draw_list);

View file

@ -4279,23 +4279,29 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons
void ImGui::PushPasswordFont()
{
ImGuiContext& g = *GImGui;
ImFont* in_font = g.Font;
ImFontBaked* in_baked = g.FontBaked;
ImFontGlyph glyph = *in_baked->FindGlyph('*');
glyph.PackId = -1;
ImFont* out_font = &g.InputTextPasswordFont;
out_font->Scale = in_font->Scale;
out_font->ContainerAtlas = in_font->ContainerAtlas;
out_font->Flags |= ImFontFlags_NoLoadGlyphs;
ImFontBaked* out_baked = out_font->GetFontBaked(in_baked->Size);
IM_ASSERT(out_baked->Glyphs.Size <= 1 && out_baked->IndexAdvanceX.Size == 0 && out_baked->IndexLookup.Size == 0);
out_baked->Ascent = in_baked->Ascent;
out_baked->Descent = in_baked->Descent;
out_baked->Glyphs.resize(0);
out_baked->Glyphs.push_back(glyph);
out_baked->FallbackGlyphIndex = 0;
out_baked->FallbackAdvanceX = glyph.AdvanceX;
PushFont(out_font);
ImFontBaked* backup = &g.InputTextPasswordFontBackupBaked;
IM_ASSERT(backup->IndexAdvanceX.Size == 0 && backup->IndexLookup.Size == 0);
ImFontGlyph* glyph = g.FontBaked->FindGlyph('*');
g.InputTextPasswordFontBackupFlags = g.Font->Flags;
backup->FallbackGlyphIndex = g.FontBaked->FallbackGlyphIndex;
backup->FallbackAdvanceX = g.FontBaked->FallbackAdvanceX;
backup->IndexLookup.swap(g.FontBaked->IndexLookup);
backup->IndexAdvanceX.swap(g.FontBaked->IndexAdvanceX);
g.Font->Flags |= ImFontFlags_NoLoadGlyphs;
g.FontBaked->FallbackGlyphIndex = g.FontBaked->Glyphs.index_from_ptr(glyph);
g.FontBaked->FallbackAdvanceX = glyph->AdvanceX;
}
void ImGui::PopPasswordFont()
{
ImGuiContext& g = *GImGui;
ImFontBaked* backup = &g.InputTextPasswordFontBackupBaked;
g.Font->Flags = g.InputTextPasswordFontBackupFlags;
g.FontBaked->FallbackGlyphIndex = backup->FallbackGlyphIndex;
g.FontBaked->FallbackAdvanceX = backup->FallbackAdvanceX;
g.FontBaked->IndexLookup.swap(backup->IndexLookup);
g.FontBaked->IndexAdvanceX.swap(backup->IndexAdvanceX);
IM_ASSERT(backup->IndexAdvanceX.Size == 0 && backup->IndexLookup.Size == 0);
}
// Return false to discard a character.
@ -5199,7 +5205,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
if (new_is_displaying_hint != is_displaying_hint)
{
if (is_password && !is_displaying_hint)
PopFont();
PopPasswordFont();
is_displaying_hint = new_is_displaying_hint;
if (is_password && !is_displaying_hint)
PushPasswordFont();
@ -5386,7 +5392,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
}
if (is_password && !is_displaying_hint)
PopFont();
PopPasswordFont();
if (is_multiline)
{