Font fixes for horizontal centering within frames

This commit is contained in:
ocornut 2015-01-17 15:44:25 +00:00
parent edee014ab8
commit 014f88b1fc
2 changed files with 16 additions and 7 deletions

View File

@ -876,7 +876,7 @@ struct ImGuiState
ImGuiIO IO; ImGuiIO IO;
ImGuiStyle Style; ImGuiStyle Style;
ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()
float FontSize; // (Shortcut) == IO.FontGlobalScale * (Font->Scale * Font->FontSize). Vertical distance between two lines of text, aka == CalcTextSize(" ").y float FontSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Size of characters.
ImVec2 FontTexUvWhitePixel; // (Shortcut) == Font->TexUvForWhite ImVec2 FontTexUvWhitePixel; // (Shortcut) == Font->TexUvForWhite
float Time; float Time;
@ -6270,7 +6270,6 @@ void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& a, const Im
ImFont::ImFont() ImFont::ImFont()
{ {
Scale = 1.0f; Scale = 1.0f;
DisplayOffset = ImVec2(0.5f, 0.5f);
FallbackChar = (ImWchar)'?'; FallbackChar = (ImWchar)'?';
TexPixelsAlpha8 = NULL; TexPixelsAlpha8 = NULL;
@ -6328,14 +6327,14 @@ void ImFont::ClearTextureData()
void ImFont::Clear() void ImFont::Clear()
{ {
DisplayOffset = ImVec2(0.5f, 0.5f); FontSize = 0.0f;
DisplayOffset = ImVec2(-0.5f, 0.5f);
ClearTextureData(); ClearTextureData();
TexID = NULL; TexID = NULL;
TexWidth = TexHeight = 0; TexWidth = TexHeight = 0;
TexExtraDataPos = TexUvWhitePixel = ImVec2(0, 0); TexExtraDataPos = TexUvWhitePixel = ImVec2(0, 0);
FontSize = 0.0f;
Glyphs.clear(); Glyphs.clear();
IndexLookup.clear(); IndexLookup.clear();
FallbackGlyph = NULL; FallbackGlyph = NULL;
@ -6947,6 +6946,11 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons
text_size.y += line_height; text_size.y += line_height;
} }
// Cancel out character spacing for the last character of a line (it is baked into glyph->XAdvance field)
const float character_spacing_x = 1.0f * scale;
if (text_size.x > 0.0f)
text_size.x -= character_spacing_x;
if (remaining) if (remaining)
*remaining = s; *remaining = s;
@ -7003,6 +7007,11 @@ ImVec2 ImFont::CalcTextSizeW(float size, float max_width, const ImWchar* text_be
text_size.y += line_height; text_size.y += line_height;
} }
// Cancel out character spacing for the last character of a line (it is baked into glyph->XAdvance field)
const float character_spacing_x = 1.0f * scale;
if (text_size.x > 0.0f)
text_size.x -= character_spacing_x;
if (remaining) if (remaining)
*remaining = s; *remaining = s;
@ -7321,7 +7330,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
{ {
static float window_scale = 1.0f; static float window_scale = 1.0f;
ImFont* font = ImGui::GetIO().Font; ImFont* font = ImGui::GetIO().Font;
ImGui::Text("Font Size: %.2f", font->FontSize); ImGui::Text("Base Font Size: %.2f", font->FontSize);
ImGui::SliderFloat("window scale", &window_scale, 0.3f, 2.0f, "%.1f"); // scale only this window ImGui::SliderFloat("window scale", &window_scale, 0.3f, 2.0f, "%.1f"); // scale only this window
ImGui::SliderFloat("font scale", &font->Scale, 0.3f, 2.0f, "%.1f"); // scale only this font ImGui::SliderFloat("font scale", &font->Scale, 0.3f, 2.0f, "%.1f"); // scale only this font
ImGui::SliderFloat("global scale", &ImGui::GetIO().FontGlobalScale, 0.3f, 2.0f, "%.1f"); // scale everything ImGui::SliderFloat("global scale", &ImGui::GetIO().FontGlobalScale, 0.3f, 2.0f, "%.1f"); // scale everything

View File

@ -755,6 +755,7 @@ struct ImDrawList
struct ImFont struct ImFont
{ {
// Settings // Settings
float FontSize; // <user set> // Height of characters, set during loading (don't change after loading)
float Scale; // = 1.0f // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale() float Scale; // = 1.0f // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale()
ImVec2 DisplayOffset; // = (0.0f,0.0f) // Offset font rendering by xx pixels ImVec2 DisplayOffset; // = (0.0f,0.0f) // Offset font rendering by xx pixels
ImWchar FallbackChar; // = '?' // Replacement glyph if one isn't found. ImWchar FallbackChar; // = '?' // Replacement glyph if one isn't found.
@ -812,7 +813,6 @@ struct ImFont
}; };
// Runtime data // Runtime data
float FontSize; // Height of characters
ImVector<Glyph> Glyphs; ImVector<Glyph> Glyphs;
ImVector<int> IndexLookup; ImVector<int> IndexLookup;
const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar) const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar)