Scintilla/imgui: removed getFontInfo(), using data exposed by imgui

This commit is contained in:
ocornut 2015-06-14 17:31:44 -06:00
parent 3d54383dd9
commit d9b0daa0bc
3 changed files with 19 additions and 43 deletions

View file

@ -2703,7 +2703,7 @@ static void RenderCheckMark(ImVec2 pos, ImU32 col)
a.x = pos.x + start_x; a.x = pos.x + start_x;
b.x = a.x + rem_third; b.x = a.x + rem_third;
c.x = a.x + rem_third * 3.0f; 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; a.y = b.y - rem_third;
c.y = b.y - rem_third * 2.0f; c.y = b.y - rem_third * 2.0f;
@ -8910,12 +8910,6 @@ struct ImFontAtlas::ImFontAtlasData
int RangesCount; int RangesCount;
}; };
const stbtt_fontinfo& getFontInfo(int index)
{
const ImGuiIO& io = ImGui::GetIO();
return io.Fonts[index].InputData[0]->FontInfo;
}
ImFontAtlas::ImFontAtlas() ImFontAtlas::ImFontAtlas()
{ {
TexID = NULL; TexID = NULL;
@ -9215,7 +9209,8 @@ bool ImFontAtlas::Build()
const float font_scale = stbtt_ScaleForPixelHeight(&data.FontInfo, data.SizePixels); const float font_scale = stbtt_ScaleForPixelHeight(&data.FontInfo, data.SizePixels);
int font_ascent, font_descent, font_line_gap; int font_ascent, font_descent, font_line_gap;
stbtt_GetFontVMetrics(&data.FontInfo, &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); data.OutFont->Glyphs.resize(0);
const float uv_scale_x = 1.0f / TexWidth; const float uv_scale_x = 1.0f / TexWidth;
@ -9374,7 +9369,7 @@ void ImFont::Clear()
{ {
FontSize = 0.0f; FontSize = 0.0f;
DisplayOffset = ImVec2(-0.5f, 0.5f); DisplayOffset = ImVec2(-0.5f, 0.5f);
BaseLine = 0.0f; Ascent = Descent = 0.0f;
ContainerAtlas = NULL; ContainerAtlas = NULL;
Glyphs.clear(); Glyphs.clear();
FallbackGlyph = NULL; FallbackGlyph = NULL;

View file

@ -1106,7 +1106,8 @@ struct ImFont
signed short XOffset, YOffset; signed short XOffset, YOffset;
float U0, V0, U1, V1; // Texture coordinates 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 ImFontAtlas* ContainerAtlas; // What we has been loaded into
ImVector<Glyph> Glyphs; ImVector<Glyph> Glyphs;
const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar) const Glyph* FallbackGlyph; // == FindGlyph(FontFallbackChar)
@ -1119,9 +1120,10 @@ struct ImFont
IMGUI_API ~ImFont(); IMGUI_API ~ImFont();
IMGUI_API void Clear(); IMGUI_API void Clear();
IMGUI_API void BuildLookupTable(); 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 const Glyph* FindGlyph(unsigned short c) const;
IMGUI_API void SetFallbackChar(ImWchar c); 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. // '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. // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.

View file

@ -58,8 +58,6 @@
#define IMGUI_NEW(type) new (ImGui::MemAlloc(sizeof(type) ) ) type #define IMGUI_NEW(type) new (ImGui::MemAlloc(sizeof(type) ) ) type
#define IMGUI_DELETE(type, obj) reinterpret_cast<type*>(obj)->~type(), ImGui::MemFree(obj) #define IMGUI_DELETE(type, obj) reinterpret_cast<type*>(obj)->~type(), ImGui::MemFree(obj)
const stbtt_fontinfo& getFontInfo(int index);
static void fillRectangle(Scintilla::PRectangle _rc, Scintilla::ColourDesired _color) static void fillRectangle(Scintilla::PRectangle _rc, Scintilla::ColourDesired _color)
{ {
const uint32_t abgr = (uint32_t)_color.AsLong(); const uint32_t abgr = (uint32_t)_color.AsLong();
@ -82,8 +80,8 @@ static inline uint32_t makeRgba(uint32_t r, uint32_t g, uint32_t b, uint32_t a =
struct FontInt struct FontInt
{ {
stbtt_fontinfo m_fontInfo; ImFont* m_font;
float m_scale; float m_scale;
float m_fontSize; float m_fontSize;
}; };
@ -238,31 +236,19 @@ public:
virtual Scintilla::XYPOSITION WidthChar(Scintilla::Font& _font, char ch) BX_OVERRIDE virtual Scintilla::XYPOSITION WidthChar(Scintilla::Font& _font, char ch) BX_OVERRIDE
{ {
FontInt* fi = (FontInt*)_font.GetID(); FontInt* fi = (FontInt*)_font.GetID();
return fi->m_font->GetCharAdvance((unsigned int)ch) * fi->m_scale;
int advance, leftBearing;
stbtt_GetCodepointHMetrics(&fi->m_fontInfo, ch, &advance, &leftBearing);
return advance * fi->m_scale;
} }
virtual Scintilla::XYPOSITION Ascent(Scintilla::Font& _font) BX_OVERRIDE virtual Scintilla::XYPOSITION Ascent(Scintilla::Font& _font) BX_OVERRIDE
{ {
FontInt* fi = (FontInt*)_font.GetID(); FontInt* fi = (FontInt*)_font.GetID();
return fi->m_font->Ascent * fi->m_scale;
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&fi->m_fontInfo, &ascent, &descent, &lineGap);
return ascent * fi->m_scale;
} }
virtual Scintilla::XYPOSITION Descent(Scintilla::Font& _font) BX_OVERRIDE virtual Scintilla::XYPOSITION Descent(Scintilla::Font& _font) BX_OVERRIDE
{ {
int ascent, descent, lineGap;
FontInt* fi = (FontInt*)_font.GetID(); FontInt* fi = (FontInt*)_font.GetID();
return -fi->m_font->Descent * fi->m_scale;
stbtt_GetFontVMetrics(&fi->m_fontInfo, &ascent, &descent, &lineGap);
return -descent * fi->m_scale;
} }
virtual Scintilla::XYPOSITION InternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE virtual Scintilla::XYPOSITION InternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
@ -270,12 +256,9 @@ public:
return 0; return 0;
} }
virtual Scintilla::XYPOSITION ExternalLeading(Scintilla::Font& _font) BX_OVERRIDE virtual Scintilla::XYPOSITION ExternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
{ {
FontInt* fi = (FontInt*)_font.GetID(); return 0;
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&fi->m_fontInfo, &ascent, &descent, &lineGap);
return lineGap * fi->m_scale;
} }
virtual Scintilla::XYPOSITION Height(Scintilla::Font& _font) BX_OVERRIDE virtual Scintilla::XYPOSITION Height(Scintilla::Font& _font) BX_OVERRIDE
@ -314,9 +297,8 @@ private:
FontInt* fi = (FontInt*)_font.GetID(); FontInt* fi = (FontInt*)_font.GetID();
ImVec2 pos = ImGui::GetCursorScreenPos(); ImVec2 pos = ImGui::GetCursorScreenPos();
ImFont* imFont = ImGui::GetWindowFont();
ImDrawList* drawList = ImGui::GetWindowDrawList(); ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddText(imFont drawList->AddText(fi->m_font
, fi->m_fontSize , fi->m_fontSize
, ImVec2(xt + pos.x, yt + pos.y - fi->m_fontSize) , ImVec2(xt + pos.x, yt + pos.y - fi->m_fontSize)
, fore , fore
@ -872,15 +854,12 @@ namespace Scintilla
} }
void Font::Create(const FontParameters& fp) void Font::Create(const FontParameters& fp)
{ {
FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) ); FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) );
fid = newFont; fid = newFont;
newFont->m_font = ImGui::GetIO().Fonts->Fonts[0];
const stbtt_fontinfo& fontInfo = getFontInfo(0);
memcpy(&newFont->m_fontInfo, &fontInfo, sizeof(stbtt_fontinfo) );
newFont->m_scale = stbtt_ScaleForPixelHeight(&fontInfo, fp.size);
newFont->m_fontSize = fp.size; newFont->m_fontSize = fp.size;
newFont->m_scale = fp.size / newFont->m_font->FontSize;
} }
void Font::Release() void Font::Release()