Forráskód Böngészése

Fonts: turn public facing BuildRegisterGlyph() into ImFontAtlasBuildAddFontGlyph() thats sets up UV.

ocornut 7 hónapja
szülő
commit
b06f3c6d1d
4 módosított fájl, 34 hozzáadás és 34 törlés
  1. 0 1
      imgui.h
  2. 29 26
      imgui_draw.cpp
  3. 2 0
      imgui_internal.h
  4. 3 7
      misc/freetype/imgui_freetype.cpp

+ 0 - 1
imgui.h

@@ -3687,7 +3687,6 @@ struct ImFont
     IMGUI_API void              AddRemapChar(ImWchar from_codepoint, ImWchar to_codepoint, bool overwrite_dst);// , bool overwrite_dst = true); // Makes 'dst' character/glyph points to 'src' character/glyph. Currently needs to be called AFTER fonts have been built.
     IMGUI_API bool              IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last);
     IMGUI_API ImFontGlyph*      BuildLoadGlyph(ImWchar c);
-    IMGUI_API void              BuildRegisterGlyph(ImFontConfig* src, const ImFontGlyph* glyph);
     IMGUI_API void              BuildGrowIndex(int new_size);
     IMGUI_API void              BuildClearGlyphs();
 };

+ 29 - 26
imgui_draw.cpp

@@ -3507,9 +3507,9 @@ bool ImFontAtlasBuildAddFont(ImFontAtlas* atlas, ImFontConfig* src)
 // Rasterize our own ellipsis character from a dot.
 // This may seem overly complicated right now but the point is to exercise and improve a technique which should be increasingly used.
 // FIXME-NEWATLAS: This borrows too much from FontBackend_FontAddGlyph() and suggest that we should add further helpers.
-static void ImFontAtlasBuildSetupFontCreateEllipsisFromDot(ImFontAtlas* atlas, ImFontConfig* cfg, const ImFontGlyph* dot_glyph)
+static void ImFontAtlasBuildSetupFontCreateEllipsisFromDot(ImFontAtlas* atlas, ImFontConfig* src, const ImFontGlyph* dot_glyph)
 {
-    ImFont* font = cfg->DstFont;
+    ImFont* font = src->DstFont;
 
     ImFontAtlasRect* dot_r = ImFontAtlasPackGetRect(atlas, dot_glyph->PackId);
     const int dot_spacing = 1;
@@ -3525,13 +3525,9 @@ static void ImFontAtlasBuildSetupFontCreateEllipsisFromDot(ImFontAtlas* atlas, I
     glyph.Y0 = dot_glyph->Y0;
     glyph.X1 = dot_glyph->X0 + dot_step * 3 - dot_spacing;
     glyph.Y1 = dot_glyph->Y1;
-    glyph.U0 = (r->x) * atlas->TexUvScale.x;
-    glyph.V0 = (r->y) * atlas->TexUvScale.y;
-    glyph.U1 = (r->x + r->w) * atlas->TexUvScale.x;
-    glyph.V1 = (r->y + r->h) * atlas->TexUvScale.y;
     glyph.Visible = true;
     glyph.PackId = pack_id;
-    font->BuildRegisterGlyph(cfg, &glyph);
+    ImFontAtlasBuildAddFontGlyph(atlas, font, NULL, &glyph);
     font->EllipsisChar = (ImWchar)glyph.Codepoint;
 
     // Copy to texture, post-process and queue update for backend
@@ -3577,7 +3573,7 @@ void ImFontAtlasBuildSetupFontSpecialGlyphs(ImFontAtlas* atlas, ImFontConfig* sr
         ImFontGlyph tab_glyph;
         tab_glyph.Codepoint = '\t';
         tab_glyph.AdvanceX = space_glyph->AdvanceX * IM_TABSIZE;
-        font->BuildRegisterGlyph(font->Sources, &tab_glyph);
+        ImFontAtlasBuildAddFontGlyph(atlas, font, src, &tab_glyph);
     }
 
     // Setup Ellipsis character. It is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis).
@@ -4282,13 +4278,9 @@ static bool ImGui_ImplStbTrueType_FontAddGlyph(ImFontAtlas* atlas, ImFont* font,
         glyph.Y0 = y0 * recip_v + font_off_y;
         glyph.X1 = (x0 + (int)r->w) * recip_h + font_off_x;
         glyph.Y1 = (y0 + (int)r->h) * recip_v + font_off_y;
-        glyph.U0 = (r->x) * atlas->TexUvScale.x;
-        glyph.V0 = (r->y) * atlas->TexUvScale.y;
-        glyph.U1 = (r->x + r->w) * atlas->TexUvScale.x;
-        glyph.V1 = (r->y + r->h) * atlas->TexUvScale.y;
         glyph.Visible = true;
         glyph.PackId = pack_id;
-        font->BuildRegisterGlyph(src, &glyph);
+        ImFontAtlasBuildAddFontGlyph(atlas, font, src, &glyph);
 
         // Copy to texture, post-process and queue update for backend
         ImTextureData* tex = atlas->TexData;
@@ -4300,7 +4292,7 @@ static bool ImGui_ImplStbTrueType_FontAddGlyph(ImFontAtlas* atlas, ImFont* font,
     }
     else
     {
-        font->BuildRegisterGlyph(src, &glyph);
+        ImFontAtlasBuildAddFontGlyph(atlas, font, src, &glyph);
     }
 
     return true;
@@ -4699,12 +4691,23 @@ void ImFont::BuildGrowIndex(int new_size)
 // x0/y0/x1/y1 are offset from the character upper-left layout position, in pixels. Therefore x0/y0 are often fairly close to zero.
 // Not to be mistaken with texture coordinates, which are held by u0/v0/u1/v1 in normalized format (0.0..1.0 on each texture axis).
 // 'src' is not necessarily == 'this->Sources' because multiple source fonts+configs can be used to build one target font.
-void ImFont::BuildRegisterGlyph(ImFontConfig* src, const ImFontGlyph* in_glyph)
+ImFontGlyph* ImFontAtlasBuildAddFontGlyph(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src, const ImFontGlyph* in_glyph)
 {
-    int glyph_idx = Glyphs.Size;
-    Glyphs.push_back(*in_glyph);
-    ImFontGlyph& glyph = Glyphs[glyph_idx];
-    IM_ASSERT(Glyphs.Size < 0xFFFE); // IndexLookup[] hold 16-bit values and -1/-2 are reserved.
+    int glyph_idx = font->Glyphs.Size;
+    font->Glyphs.push_back(*in_glyph);
+    ImFontGlyph& glyph = font->Glyphs[glyph_idx];
+    IM_ASSERT(font->Glyphs.Size < 0xFFFE); // IndexLookup[] hold 16-bit values and -1/-2 are reserved.
+
+    // Set UV from packed rectangle
+    if (in_glyph->PackId >= 0)
+    {
+        ImFontAtlasRect* r = ImFontAtlasPackGetRect(atlas, in_glyph->PackId);
+        IM_ASSERT(in_glyph->U0 == 0.0f && in_glyph->V0 == 0.0f && in_glyph->U1 == 0.0f && in_glyph->V1 == 0.0f);
+        glyph.U0 = (r->x) * atlas->TexUvScale.x;
+        glyph.V0 = (r->y) * atlas->TexUvScale.y;
+        glyph.U1 = (r->x + r->w) * atlas->TexUvScale.x;
+        glyph.V1 = (r->y + r->h) * atlas->TexUvScale.y;
+    }
 
     if (src != NULL)
     {
@@ -4725,17 +4728,17 @@ void ImFont::BuildRegisterGlyph(ImFontConfig* src, const ImFontGlyph* in_glyph)
         glyph.AdvanceX = advance_x + src->GlyphExtraAdvanceX;
     }
     if (glyph.Colored)
-        ContainerAtlas->TexPixelsUseColors = ContainerAtlas->TexData->UseColors = true;
+        atlas->TexPixelsUseColors = atlas->TexData->UseColors = true;
 
     // Update lookup tables
     int codepoint = glyph.Codepoint;
-    BuildGrowIndex(codepoint + 1);
-    IndexAdvanceX[codepoint] = glyph.AdvanceX;
-    IndexLookup[codepoint] = (ImU16)glyph_idx;
-
-    // Mark 4K page as used
+    font->BuildGrowIndex(codepoint + 1);
+    font->IndexAdvanceX[codepoint] = glyph.AdvanceX;
+    font->IndexLookup[codepoint] = (ImU16)glyph_idx;
     const int page_n = codepoint / 8192;
-    Used8kPagesMap[page_n >> 3] |= 1 << (page_n & 7);
+    font->Used8kPagesMap[page_n >> 3] |= 1 << (page_n & 7);
+
+    return &glyph;
 }
 
 void ImFont::AddRemapChar(ImWchar from_codepoint, ImWchar to_codepoint, bool overwrite_dst)

+ 2 - 0
imgui_internal.h

@@ -3721,6 +3721,8 @@ IMGUI_API void              ImFontAtlasBuildReloadFont(ImFontAtlas* atlas, ImFon
 IMGUI_API void              ImFontAtlasBuildPreloadAllGlyphRanges(ImFontAtlas* atlas); // Legacy
 IMGUI_API void              ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, int* out_oversample_h, int* out_oversample_v);
 
+IMGUI_API ImFontGlyph*      ImFontAtlasBuildAddFontGlyph(ImFontAtlas* atlas, ImFont* font, ImFontConfig* cfg, const ImFontGlyph* in_glyph);
+
 IMGUI_API void              ImFontAtlasPackInit(ImFontAtlas* atlas);
 IMGUI_API ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFontAtlasRectEntry* overwrite_entry = NULL);
 IMGUI_API void              ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id);

+ 3 - 7
misc/freetype/imgui_freetype.cpp

@@ -529,14 +529,10 @@ bool ImGui_ImplFreeType_FontAddGlyph(ImFontAtlas* atlas, ImFont* font, ImFontCon
         glyph.Y0 = glyph_off_y * recip_v + font_off_y;
         glyph.X1 = (glyph_off_x + w) * recip_h + font_off_x;
         glyph.Y1 = (glyph_off_y + h) * recip_v + font_off_y;
-        glyph.U0 = (r->x) * atlas->TexUvScale.x;
-        glyph.V0 = (r->y) * atlas->TexUvScale.y;
-        glyph.U1 = (r->x + r->w) * atlas->TexUvScale.x;
-        glyph.V1 = (r->y + r->h) * atlas->TexUvScale.y;
-        glyph.PackId = pack_id;
         glyph.Visible = true;
         glyph.Colored = (ft_bitmap->pixel_mode == FT_PIXEL_MODE_BGRA);
-        font->BuildRegisterGlyph(src, &glyph);
+        glyph.PackId = pack_id;
+        ImFontAtlasBuildAddFontGlyph(atlas, font, src, &glyph);
 
         // Copy to texture, post-process and queue update for backend
         ImTextureData* tex = atlas->TexData;
@@ -548,7 +544,7 @@ bool ImGui_ImplFreeType_FontAddGlyph(ImFontAtlas* atlas, ImFont* font, ImFontCon
     }
     else
     {
-        font->BuildRegisterGlyph(src, &glyph);
+        ImFontAtlasBuildAddFontGlyph(atlas, font, src, &glyph);
     }
     return true;
 }