1
0
Fork 0
mirror of https://github.com/ocornut/imgui.git synced 2026-01-11 00:04:24 +00:00

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

This commit is contained in:
ocornut 2025-01-09 20:48:50 +01:00
parent 4ff1631b31
commit b06f3c6d1d
4 changed files with 34 additions and 34 deletions

View file

@ -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 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 bool IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last);
IMGUI_API ImFontGlyph* BuildLoadGlyph(ImWchar c); 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 BuildGrowIndex(int new_size);
IMGUI_API void BuildClearGlyphs(); IMGUI_API void BuildClearGlyphs();
}; };

View file

@ -3507,9 +3507,9 @@ bool ImFontAtlasBuildAddFont(ImFontAtlas* atlas, ImFontConfig* src)
// Rasterize our own ellipsis character from a dot. // 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. // 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. // 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); ImFontAtlasRect* dot_r = ImFontAtlasPackGetRect(atlas, dot_glyph->PackId);
const int dot_spacing = 1; const int dot_spacing = 1;
@ -3525,13 +3525,9 @@ static void ImFontAtlasBuildSetupFontCreateEllipsisFromDot(ImFontAtlas* atlas, I
glyph.Y0 = dot_glyph->Y0; glyph.Y0 = dot_glyph->Y0;
glyph.X1 = dot_glyph->X0 + dot_step * 3 - dot_spacing; glyph.X1 = dot_glyph->X0 + dot_step * 3 - dot_spacing;
glyph.Y1 = dot_glyph->Y1; 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.Visible = true;
glyph.PackId = pack_id; glyph.PackId = pack_id;
font->BuildRegisterGlyph(cfg, &glyph); ImFontAtlasBuildAddFontGlyph(atlas, font, NULL, &glyph);
font->EllipsisChar = (ImWchar)glyph.Codepoint; font->EllipsisChar = (ImWchar)glyph.Codepoint;
// Copy to texture, post-process and queue update for backend // Copy to texture, post-process and queue update for backend
@ -3577,7 +3573,7 @@ void ImFontAtlasBuildSetupFontSpecialGlyphs(ImFontAtlas* atlas, ImFontConfig* sr
ImFontGlyph tab_glyph; ImFontGlyph tab_glyph;
tab_glyph.Codepoint = '\t'; tab_glyph.Codepoint = '\t';
tab_glyph.AdvanceX = space_glyph->AdvanceX * IM_TABSIZE; 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). // 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.Y0 = y0 * recip_v + font_off_y;
glyph.X1 = (x0 + (int)r->w) * recip_h + font_off_x; glyph.X1 = (x0 + (int)r->w) * recip_h + font_off_x;
glyph.Y1 = (y0 + (int)r->h) * recip_v + font_off_y; 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.Visible = true;
glyph.PackId = pack_id; glyph.PackId = pack_id;
font->BuildRegisterGlyph(src, &glyph); ImFontAtlasBuildAddFontGlyph(atlas, font, src, &glyph);
// Copy to texture, post-process and queue update for backend // Copy to texture, post-process and queue update for backend
ImTextureData* tex = atlas->TexData; ImTextureData* tex = atlas->TexData;
@ -4300,7 +4292,7 @@ static bool ImGui_ImplStbTrueType_FontAddGlyph(ImFontAtlas* atlas, ImFont* font,
} }
else else
{ {
font->BuildRegisterGlyph(src, &glyph); ImFontAtlasBuildAddFontGlyph(atlas, font, src, &glyph);
} }
return true; 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. // 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). // 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. // '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; int glyph_idx = font->Glyphs.Size;
Glyphs.push_back(*in_glyph); font->Glyphs.push_back(*in_glyph);
ImFontGlyph& glyph = Glyphs[glyph_idx]; ImFontGlyph& glyph = font->Glyphs[glyph_idx];
IM_ASSERT(Glyphs.Size < 0xFFFE); // IndexLookup[] hold 16-bit values and -1/-2 are reserved. 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) if (src != NULL)
{ {
@ -4725,17 +4728,17 @@ void ImFont::BuildRegisterGlyph(ImFontConfig* src, const ImFontGlyph* in_glyph)
glyph.AdvanceX = advance_x + src->GlyphExtraAdvanceX; glyph.AdvanceX = advance_x + src->GlyphExtraAdvanceX;
} }
if (glyph.Colored) if (glyph.Colored)
ContainerAtlas->TexPixelsUseColors = ContainerAtlas->TexData->UseColors = true; atlas->TexPixelsUseColors = atlas->TexData->UseColors = true;
// Update lookup tables // Update lookup tables
int codepoint = glyph.Codepoint; int codepoint = glyph.Codepoint;
BuildGrowIndex(codepoint + 1); font->BuildGrowIndex(codepoint + 1);
IndexAdvanceX[codepoint] = glyph.AdvanceX; font->IndexAdvanceX[codepoint] = glyph.AdvanceX;
IndexLookup[codepoint] = (ImU16)glyph_idx; font->IndexLookup[codepoint] = (ImU16)glyph_idx;
// Mark 4K page as used
const int page_n = codepoint / 8192; 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) void ImFont::AddRemapChar(ImWchar from_codepoint, ImWchar to_codepoint, bool overwrite_dst)

View file

@ -3721,6 +3721,8 @@ IMGUI_API void ImFontAtlasBuildReloadFont(ImFontAtlas* atlas, ImFon
IMGUI_API void ImFontAtlasBuildPreloadAllGlyphRanges(ImFontAtlas* atlas); // Legacy IMGUI_API void ImFontAtlasBuildPreloadAllGlyphRanges(ImFontAtlas* atlas); // Legacy
IMGUI_API void ImFontAtlasBuildGetOversampleFactors(ImFontConfig* src, int* out_oversample_h, int* out_oversample_v); 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 void ImFontAtlasPackInit(ImFontAtlas* atlas);
IMGUI_API ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFontAtlasRectEntry* overwrite_entry = NULL); IMGUI_API ImFontAtlasRectId ImFontAtlasPackAddRect(ImFontAtlas* atlas, int w, int h, ImFontAtlasRectEntry* overwrite_entry = NULL);
IMGUI_API void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id); IMGUI_API void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id);

View file

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