Browse Source

ImFont: Cleanup to be compatible with over-sampling (not enabled)

ocornut 10 years ago
parent
commit
c02f9b58ef
2 changed files with 17 additions and 16 deletions
  1. 15 14
      imgui.cpp
  2. 2 2
      imgui.h

+ 15 - 14
imgui.cpp

@@ -9660,6 +9660,7 @@ struct ImFontAtlas::ImFontAtlasData
     stbrp_rect*         Rects;
     stbrp_rect*         Rects;
     stbtt_pack_range*   Ranges;
     stbtt_pack_range*   Ranges;
     int                 RangesCount;
     int                 RangesCount;
+    int                 OversampleH, OversampleV;
 };
 };
 
 
 ImFontAtlas::ImFontAtlas()
 ImFontAtlas::ImFontAtlas()
@@ -9762,7 +9763,6 @@ ImFont* ImFontAtlas::AddFontDefault()
     const void* ttf_compressed;
     const void* ttf_compressed;
     GetDefaultCompressedFontDataTTF(&ttf_compressed, &ttf_compressed_size);
     GetDefaultCompressedFontDataTTF(&ttf_compressed, &ttf_compressed_size);
     ImFont* font = AddFontFromMemoryCompressedTTF(ttf_compressed, ttf_compressed_size, 13.0f, GetGlyphRangesDefault(), 0);
     ImFont* font = AddFontFromMemoryCompressedTTF(ttf_compressed, ttf_compressed_size, 13.0f, GetGlyphRangesDefault(), 0);
-    font->DisplayOffset.y += 1;
     return font;
     return font;
 }
 }
 
 
@@ -9795,6 +9795,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float si
     data->TTFData = ttf_data;
     data->TTFData = ttf_data;
     data->TTFDataSize = ttf_size;
     data->TTFDataSize = ttf_size;
     data->SizePixels = size_pixels;
     data->SizePixels = size_pixels;
+    data->OversampleH = data->OversampleV = 1;
     data->GlyphRanges = glyph_ranges;
     data->GlyphRanges = glyph_ranges;
     data->FontNo = font_no;
     data->FontNo = font_no;
     InputData.push_back(data);
     InputData.push_back(data);
@@ -9854,11 +9855,11 @@ bool    ImFontAtlas::Build()
     stbtt_pack_context spc;
     stbtt_pack_context spc;
     int ret = stbtt_PackBegin(&spc, NULL, TexWidth, max_tex_height, 0, 1, NULL);
     int ret = stbtt_PackBegin(&spc, NULL, TexWidth, max_tex_height, 0, 1, NULL);
     IM_ASSERT(ret);
     IM_ASSERT(ret);
-    stbtt_PackSetOversampling(&spc, 1, 1);
 
 
     // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
     // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).
     ImVector<stbrp_rect> extra_rects;
     ImVector<stbrp_rect> extra_rects;
     RenderCustomTexData(0, &extra_rects);
     RenderCustomTexData(0, &extra_rects);
+    stbtt_PackSetOversampling(&spc, 1, 1);
     stbrp_pack_rects((stbrp_context*)spc.pack_info, &extra_rects[0], extra_rects.Size);
     stbrp_pack_rects((stbrp_context*)spc.pack_info, &extra_rects[0], extra_rects.Size);
     for (int i = 0; i < extra_rects.Size; i++)
     for (int i = 0; i < extra_rects.Size; i++)
         if (extra_rects[i].was_packed)
         if (extra_rects[i].was_packed)
@@ -9903,6 +9904,7 @@ bool    ImFontAtlas::Build()
         // Pack
         // Pack
         data.Rects = buf_rects + buf_rects_n;
         data.Rects = buf_rects + buf_rects_n;
         buf_rects_n += glyph_count;
         buf_rects_n += glyph_count;
+        stbtt_PackSetOversampling(&spc, data.OversampleH, data.OversampleV);
         const int n = stbtt_PackFontRangesGatherRects(&spc, &data.FontInfo, data.Ranges, data.RangesCount, data.Rects);
         const int n = stbtt_PackFontRangesGatherRects(&spc, &data.FontInfo, data.Ranges, data.RangesCount, data.Rects);
         stbrp_pack_rects((stbrp_context*)spc.pack_info, data.Rects, n);
         stbrp_pack_rects((stbrp_context*)spc.pack_info, data.Rects, n);
 
 
@@ -9926,6 +9928,7 @@ bool    ImFontAtlas::Build()
     for (int input_i = 0; input_i < InputData.Size; input_i++)
     for (int input_i = 0; input_i < InputData.Size; input_i++)
     {
     {
         ImFontAtlasData& data = *InputData[input_i];
         ImFontAtlasData& data = *InputData[input_i];
+        stbtt_PackSetOversampling(&spc, data.OversampleH, data.OversampleV);
         ret = stbtt_PackFontRangesRenderIntoRects(&spc, &data.FontInfo, data.Ranges, data.RangesCount, data.Rects);
         ret = stbtt_PackFontRangesRenderIntoRects(&spc, &data.FontInfo, data.Ranges, data.RangesCount, data.Rects);
         data.Rects = NULL;
         data.Rects = NULL;
     }
     }
@@ -9949,8 +9952,6 @@ bool    ImFontAtlas::Build()
         data.OutFont->Descent = (font_descent * font_scale);
         data.OutFont->Descent = (font_descent * font_scale);
         data.OutFont->Glyphs.resize(0);
         data.OutFont->Glyphs.resize(0);
 
 
-        const float uv_scale_x = 1.0f / TexWidth;
-        const float uv_scale_y = 1.0f / TexHeight;
         const int character_spacing_x = 1;
         const int character_spacing_x = 1;
         for (int i = 0; i < data.RangesCount; i++)
         for (int i = 0; i < data.RangesCount; i++)
         {
         {
@@ -9960,19 +9961,19 @@ bool    ImFontAtlas::Build()
                 const stbtt_packedchar& pc = range.chardata_for_range[char_idx];
                 const stbtt_packedchar& pc = range.chardata_for_range[char_idx];
                 if (!pc.x0 && !pc.x1 && !pc.y0 && !pc.y1)
                 if (!pc.x0 && !pc.x1 && !pc.y0 && !pc.y1)
                     continue;
                     continue;
+ 
+                stbtt_aligned_quad q;
+                float dummy_x = 0.0f, dummy_y = 0.0f;
+                stbtt_GetPackedQuad(range.chardata_for_range, TexWidth, TexHeight, char_idx, &dummy_x, &dummy_y, &q, 0);
 
 
                 data.OutFont->Glyphs.resize(data.OutFont->Glyphs.Size + 1);
                 data.OutFont->Glyphs.resize(data.OutFont->Glyphs.Size + 1);
                 ImFont::Glyph& glyph = data.OutFont->Glyphs.back();
                 ImFont::Glyph& glyph = data.OutFont->Glyphs.back();
                 glyph.Codepoint = (ImWchar)(range.first_unicode_char_in_range + char_idx);
                 glyph.Codepoint = (ImWchar)(range.first_unicode_char_in_range + char_idx);
-                glyph.X0 = (signed short)(pc.xoff);
-                glyph.Y0 = (signed short)(pc.yoff + (int)(font_ascent * font_scale));
-                glyph.X1 = glyph.X0 + ((signed short)pc.x1 - pc.x0 + 1);
-                glyph.Y1 = glyph.Y0 + ((signed short)pc.y1 - pc.y0 + 1);
-                glyph.XAdvance = (signed short)(pc.xadvance + character_spacing_x);  // Bake spacing into XAdvance
-                glyph.U0 = ((float)pc.x0 - 0.5f) * uv_scale_x;
-                glyph.V0 = ((float)pc.y0 - 0.5f) * uv_scale_y;
-                glyph.U1 = ((float)pc.x0 - 0.5f + (glyph.X1 - glyph.X0)) * uv_scale_x;
-                glyph.V1 = ((float)pc.y0 - 0.5f + (glyph.Y1 - glyph.Y0)) * uv_scale_y;
+                glyph.X0 = q.x0; glyph.Y0 = q.y0; glyph.X1 = q.x1; glyph.Y1 = q.y1;                
+                glyph.U0 = q.s0; glyph.V0 = q.t0; glyph.U1 = q.s1; glyph.V1 = q.t1;
+                glyph.Y0 += (float)(int)(data.OutFont->Ascent + 0.5f);
+                glyph.Y1 += (float)(int)(data.OutFont->Ascent + 0.5f);
+                glyph.XAdvance = (float)(int)(pc.xadvance + character_spacing_x + 0.5f);  // Bake spacing into XAdvance
             }
             }
         }
         }
 
 
@@ -10202,7 +10203,7 @@ ImFont::~ImFont()
 void    ImFont::Clear()
 void    ImFont::Clear()
 {
 {
     FontSize = 0.0f;
     FontSize = 0.0f;
-    DisplayOffset = ImVec2(-0.5f, 0.5f);
+    DisplayOffset = ImVec2(0.0f, 1.0f);
     Ascent = Descent = 0.0f;
     Ascent = Descent = 0.0f;
     ContainerAtlas = NULL;
     ContainerAtlas = NULL;
     Glyphs.clear();
     Glyphs.clear();

+ 2 - 2
imgui.h

@@ -1184,8 +1184,8 @@ struct ImFont
     struct Glyph
     struct Glyph
     {
     {
         ImWchar         Codepoint;
         ImWchar         Codepoint;
-        signed short    XAdvance;
-        signed short    X0, Y0, X1, Y1;
+        float           XAdvance;
+        float           X0, Y0, X1, Y1;
         float           U0, V0, U1, V1;     // Texture coordinates
         float           U0, V0, U1, V1;     // Texture coordinates
     };
     };
     float               Ascent;             // Distance from top to bottom of e.g. 'A' [0..FontSize]
     float               Ascent;             // Distance from top to bottom of e.g. 'A' [0..FontSize]