From 727ca4bd17da6c07bf375e336062574cf1751f19 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sat, 23 Apr 2016 11:37:18 +0200 Subject: [PATCH] ImFont: IndexLookup stores short instead of int, so typical ascii-set lookup fits in 256 bytes --- imgui.h | 2 +- imgui_draw.cpp | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/imgui.h b/imgui.h index b66204903..9e4431891 100644 --- a/imgui.h +++ b/imgui.h @@ -1300,7 +1300,7 @@ struct ImFont ImVec2 DisplayOffset; // = (0.f,1.f) // Offset font rendering by xx pixels ImVector Glyphs; // // All glyphs. ImVector IndexXAdvance; // // Sparse. Glyphs->XAdvance in a directly indexable way (more cache-friendly, for CalcTextSize functions which are often bottleneck in large UI). - ImVector IndexLookup; // // Sparse. Index glyphs by Unicode code-point. + ImVector IndexLookup; // // Sparse. Index glyphs by Unicode code-point. const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar) float FallbackXAdvance; // == FallbackGlyph->XAdvance ImWchar FallbackChar; // = '?' // Replacement glyph if one isn't found. Only set via SetFallbackChar() diff --git a/imgui_draw.cpp b/imgui_draw.cpp index c3aec55dd..1afa8d2b4 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -1675,6 +1675,7 @@ void ImFont::BuildLookupTable() for (int i = 0; i != Glyphs.Size; i++) max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint); + IM_ASSERT(Glyphs.Size < 32*1024); IndexXAdvance.clear(); IndexXAdvance.resize(max_codepoint + 1); IndexLookup.clear(); @@ -1682,13 +1683,13 @@ void ImFont::BuildLookupTable() for (int i = 0; i < max_codepoint + 1; i++) { IndexXAdvance[i] = -1.0f; - IndexLookup[i] = -1; + IndexLookup[i] = (short)-1; } for (int i = 0; i < Glyphs.Size; i++) { int codepoint = (int)Glyphs[i].Codepoint; IndexXAdvance[codepoint] = Glyphs[i].XAdvance; - IndexLookup[codepoint] = i; + IndexLookup[codepoint] = (short)i; } // Create a glyph to handle TAB @@ -1702,7 +1703,7 @@ void ImFont::BuildLookupTable() tab_glyph.Codepoint = '\t'; tab_glyph.XAdvance *= 4; IndexXAdvance[(int)tab_glyph.Codepoint] = (float)tab_glyph.XAdvance; - IndexLookup[(int)tab_glyph.Codepoint] = (int)(Glyphs.Size-1); + IndexLookup[(int)tab_glyph.Codepoint] = (short)(Glyphs.Size-1); } FallbackGlyph = NULL; @@ -1723,7 +1724,7 @@ const ImFont::Glyph* ImFont::FindGlyph(unsigned short c) const { if (c < IndexLookup.Size) { - const int i = IndexLookup[c]; + const short i = IndexLookup[c]; if (i != -1) return &Glyphs.Data[i]; }