Browse Source

ImFont: IndexLookup stores short instead of int, so typical ascii-set lookup fits in 256 bytes

ocornut 9 years ago
parent
commit
727ca4bd17
2 changed files with 6 additions and 5 deletions
  1. 1 1
      imgui.h
  2. 5 4
      imgui_draw.cpp

+ 1 - 1
imgui.h

@@ -1300,7 +1300,7 @@ struct ImFont
     ImVec2                      DisplayOffset;      // = (0.f,1.f)  // Offset font rendering by xx pixels
     ImVec2                      DisplayOffset;      // = (0.f,1.f)  // Offset font rendering by xx pixels
     ImVector<Glyph>             Glyphs;             //              // All glyphs.
     ImVector<Glyph>             Glyphs;             //              // All glyphs.
     ImVector<float>             IndexXAdvance;      //              // Sparse. Glyphs->XAdvance in a directly indexable way (more cache-friendly, for CalcTextSize functions which are often bottleneck in large UI).
     ImVector<float>             IndexXAdvance;      //              // Sparse. Glyphs->XAdvance in a directly indexable way (more cache-friendly, for CalcTextSize functions which are often bottleneck in large UI).
-    ImVector<int>               IndexLookup;        //              // Sparse. Index glyphs by Unicode code-point.
+    ImVector<short>             IndexLookup;        //              // Sparse. Index glyphs by Unicode code-point.
     const Glyph*                FallbackGlyph;      // == FindGlyph(FontFallbackChar)
     const Glyph*                FallbackGlyph;      // == FindGlyph(FontFallbackChar)
     float                       FallbackXAdvance;   // == FallbackGlyph->XAdvance
     float                       FallbackXAdvance;   // == FallbackGlyph->XAdvance
     ImWchar                     FallbackChar;       // = '?'        // Replacement glyph if one isn't found. Only set via SetFallbackChar()
     ImWchar                     FallbackChar;       // = '?'        // Replacement glyph if one isn't found. Only set via SetFallbackChar()

+ 5 - 4
imgui_draw.cpp

@@ -1675,6 +1675,7 @@ void ImFont::BuildLookupTable()
     for (int i = 0; i != Glyphs.Size; i++)
     for (int i = 0; i != Glyphs.Size; i++)
         max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint);
         max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint);
 
 
+    IM_ASSERT(Glyphs.Size < 32*1024);
     IndexXAdvance.clear();
     IndexXAdvance.clear();
     IndexXAdvance.resize(max_codepoint + 1);
     IndexXAdvance.resize(max_codepoint + 1);
     IndexLookup.clear();
     IndexLookup.clear();
@@ -1682,13 +1683,13 @@ void ImFont::BuildLookupTable()
     for (int i = 0; i < max_codepoint + 1; i++)
     for (int i = 0; i < max_codepoint + 1; i++)
     {
     {
         IndexXAdvance[i] = -1.0f;
         IndexXAdvance[i] = -1.0f;
-        IndexLookup[i] = -1;
+        IndexLookup[i] = (short)-1;
     }
     }
     for (int i = 0; i < Glyphs.Size; i++)
     for (int i = 0; i < Glyphs.Size; i++)
     {
     {
         int codepoint = (int)Glyphs[i].Codepoint;
         int codepoint = (int)Glyphs[i].Codepoint;
         IndexXAdvance[codepoint] = Glyphs[i].XAdvance;
         IndexXAdvance[codepoint] = Glyphs[i].XAdvance;
-        IndexLookup[codepoint] = i;
+        IndexLookup[codepoint] = (short)i;
     }
     }
 
 
     // Create a glyph to handle TAB
     // Create a glyph to handle TAB
@@ -1702,7 +1703,7 @@ void ImFont::BuildLookupTable()
         tab_glyph.Codepoint = '\t';
         tab_glyph.Codepoint = '\t';
         tab_glyph.XAdvance *= 4;
         tab_glyph.XAdvance *= 4;
         IndexXAdvance[(int)tab_glyph.Codepoint] = (float)tab_glyph.XAdvance;
         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;
     FallbackGlyph = NULL;
@@ -1723,7 +1724,7 @@ const ImFont::Glyph* ImFont::FindGlyph(unsigned short c) const
 {
 {
     if (c < IndexLookup.Size)
     if (c < IndexLookup.Size)
     {
     {
-        const int i = IndexLookup[c];
+        const short i = IndexLookup[c];
         if (i != -1)
         if (i != -1)
             return &Glyphs.Data[i];
             return &Glyphs.Data[i];
     }
     }