From e10ceebd36e4333c3da6f0c2250204e136a98241 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 17 Mar 2017 21:29:57 +0100 Subject: [PATCH] ImFont: Store the texture surface used to get an idea of how costly each font is. --- imgui.h | 1 + imgui_demo.cpp | 3 ++- imgui_draw.cpp | 5 ++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/imgui.h b/imgui.h index d6100cd2..35886986 100644 --- a/imgui.h +++ b/imgui.h @@ -1367,6 +1367,7 @@ struct ImFont ImFontConfig* ConfigData; // // Pointer within ContainerAtlas->ConfigData ImFontAtlas* ContainerAtlas; // // What we has been loaded into float Ascent, Descent; // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] + int MetricsTotalSurface;// // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs) // Methods IMGUI_API ImFont(); diff --git a/imgui_demo.cpp b/imgui_demo.cpp index dda0a53d..58f7a795 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -1750,10 +1750,11 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SameLine(); ShowHelpMarker("Note than the default embedded font is NOT meant to be scaled.\n\nFont are currently rendered into bitmaps at a given size at the time of building the atlas. You may oversample them to get some flexibility with scaling. You can also render at multiple sizes and select which one to use at runtime.\n\n(Glimmer of hope: the atlas system should hopefully be rewritten in the future to make scaling more natural and automatic.)"); ImGui::Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent); ImGui::Text("Fallback character: '%c' (%d)", font->FallbackChar, font->FallbackChar); + ImGui::Text("Texture surface: %d pixels (approx)", font->MetricsTotalSurface); for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) { ImFontConfig* cfg = &font->ConfigData[config_i]; - ImGui::BulletText("Input %d: \'%s\'\nOversample: (%d,%d), PixelSnapH: %d", config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); + ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d", config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); } if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) { diff --git a/imgui_draw.cpp b/imgui_draw.cpp index b6188ce8..1b2e9e0c 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -1386,7 +1386,7 @@ bool ImFontAtlas::Build() { ImFontConfig& cfg = ConfigData[input_i]; ImFontTempBuildData& tmp = tmp_array[input_i]; - ImFont* dst_font = cfg.DstFont; + ImFont* dst_font = cfg.DstFont; // We can have multiple input fonts writing into a same destination font (when using MergeMode=true) float font_scale = stbtt_ScaleForPixelHeight(&tmp.FontInfo, cfg.SizePixels); int unscaled_ascent, unscaled_descent, unscaled_line_gap; @@ -1403,6 +1403,7 @@ bool ImFontAtlas::Build() dst_font->Ascent = ascent; dst_font->Descent = descent; dst_font->Glyphs.resize(0); + dst_font->MetricsTotalSurface = 0; } dst_font->ConfigDataCount++; float off_y = (cfg.MergeMode && cfg.MergeGlyphCenterV) ? (ascent - dst_font->Ascent) * 0.5f : 0.0f; @@ -1435,6 +1436,7 @@ bool ImFontAtlas::Build() glyph.XAdvance = (pc.xadvance + cfg.GlyphExtraSpacing.x); // Bake spacing into XAdvance if (cfg.PixelSnapH) glyph.XAdvance = (float)(int)(glyph.XAdvance + 0.5f); + dst_font->MetricsTotalSurface += (int)(glyph.X1 - glyph.X0 + 1.99f) * (int)(glyph.Y1 - glyph.Y0 + 1.99f); // +1 to account for average padding, +0.99 to round } } cfg.DstFont->BuildLookupTable(); @@ -1703,6 +1705,7 @@ void ImFont::Clear() ConfigData = NULL; ContainerAtlas = NULL; Ascent = Descent = 0.0f; + MetricsTotalSurface = 0; } void ImFont::BuildLookupTable()