Skip to content

Commit

Permalink
ImFont: added GetCharAdvance() helper. Exposed font Ascent and font D…
Browse files Browse the repository at this point in the history
…escent.
  • Loading branch information
ocornut committed Jun 14, 2015
1 parent b503e5e commit 6eb1fec
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 4 additions & 3 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2703,7 +2703,7 @@ static void RenderCheckMark(ImVec2 pos, ImU32 col)
a.x = pos.x + start_x;
b.x = a.x + rem_third;
c.x = a.x + rem_third * 3.0f;
b.y = pos.y + (float)(int)(g.Font->BaseLine * (g.FontSize / g.Font->FontSize) + 0.5f) + (float)(int)(g.Font->DisplayOffset.y);
b.y = pos.y + (float)(int)(g.Font->Ascent * (g.FontSize / g.Font->FontSize) + 0.5f) + (float)(int)(g.Font->DisplayOffset.y);
a.y = b.y - rem_third;
c.y = b.y - rem_third * 2.0f;

Expand Down Expand Up @@ -9215,7 +9215,8 @@ bool ImFontAtlas::Build()
const float font_scale = stbtt_ScaleForPixelHeight(&data.FontInfo, data.SizePixels);
int font_ascent, font_descent, font_line_gap;
stbtt_GetFontVMetrics(&data.FontInfo, &font_ascent, &font_descent, &font_line_gap);
data.OutFont->BaseLine = (font_ascent * font_scale);
data.OutFont->Ascent = (font_ascent * font_scale);
data.OutFont->Descent = (font_descent * font_scale);
data.OutFont->Glyphs.resize(0);

const float uv_scale_x = 1.0f / TexWidth;
Expand Down Expand Up @@ -9374,7 +9375,7 @@ void ImFont::Clear()
{
FontSize = 0.0f;
DisplayOffset = ImVec2(-0.5f, 0.5f);
BaseLine = 0.0f;
Ascent = Descent = 0.0f;
ContainerAtlas = NULL;
Glyphs.clear();
FallbackGlyph = NULL;
Expand Down
6 changes: 4 additions & 2 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,8 @@ struct ImFont
signed short XOffset, YOffset;
float U0, V0, U1, V1; // Texture coordinates
};
float BaseLine; // Distance from top to bottom of e.g. 'A' [0..FontSize]
float Ascent; // Distance from top to bottom of e.g. 'A' [0..FontSize]
float Descent; //
ImFontAtlas* ContainerAtlas; // What we has been loaded into
ImVector<Glyph> Glyphs;
const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar)
Expand All @@ -1127,9 +1128,10 @@ struct ImFont
IMGUI_API ~ImFont();
IMGUI_API void Clear();
IMGUI_API void BuildLookupTable();
IMGUI_API float GetCharAdvance(unsigned short c) const { return ((size_t)c < IndexXAdvance.size()) ? IndexXAdvance[(size_t)c] : FallbackXAdvance; }
IMGUI_API const Glyph* FindGlyph(unsigned short c) const;
IMGUI_API void SetFallbackChar(ImWchar c);
IMGUI_API bool IsLoaded() const { return ContainerAtlas != NULL; }
IMGUI_API bool IsLoaded() const { return ContainerAtlas != NULL; }

// 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
// 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
Expand Down

0 comments on commit 6eb1fec

Please sign in to comment.