1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-23 02:38:53 -05:00

add PrimUnreserve to AddTextVertical when the render characters is less than the expected characters (fixes utf8 issues)

This commit is contained in:
epezent 2021-01-19 19:42:22 -06:00
parent c73509d6d1
commit 6e2499093b

View File

@ -282,17 +282,21 @@ static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx) {
//-----------------------------------------------------------------------------
void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char *text_begin, const char* text_end) {
// the code below is based loosely on ImFont::RenderText
if (!text_end)
text_end = text_begin + strlen(text_begin);
ImGuiContext& g = *GImGui;
ImFont* font = g.Font;
pos.x = IM_FLOOR(pos.x); // + font->ConfigData->GlyphOffset.y);
pos.y = IM_FLOOR(pos.y); // + font->ConfigData->GlyphOffset.x);
const char* s = text_begin;
const int vtx_count = (int)(text_end - s) * 4;
const int idx_count = (int)(text_end - s) * 6;
DrawList->PrimReserve(idx_count, vtx_count);
// Align to be pixel perfect
pos.x = IM_FLOOR(pos.x);
pos.y = IM_FLOOR(pos.y);
const float scale = g.FontSize / font->FontSize;
const char* s = text_begin;
int chars_exp = (int)(text_end - s);
int chars_rnd = 0;
const int vtx_count_max = chars_exp * 4;
const int idx_count_max = chars_exp * 6;
DrawList->PrimReserve(idx_count_max, vtx_count_max);
while (s < text_end) {
unsigned int c = (unsigned int)*s;
if (c < 0x80) {
@ -304,15 +308,20 @@ void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char *te
break;
}
const ImFontGlyph * glyph = font->FindGlyph((ImWchar)c);
if (glyph == NULL)
if (glyph == NULL) {
continue;
}
DrawList->PrimQuadUV(pos + ImVec2(glyph->Y0, -glyph->X0) * scale, pos + ImVec2(glyph->Y0, -glyph->X1) * scale,
pos + ImVec2(glyph->Y1, -glyph->X1) * scale, pos + ImVec2(glyph->Y1, -glyph->X0) * scale,
ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V0),
ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1),
col);
pos.y -= glyph->AdvanceX * scale;
chars_rnd++;
}
// Give back unused vertices
int chars_skp = chars_exp-chars_rnd;
DrawList->PrimUnreserve(chars_skp*6, chars_skp*4);
}
double NiceNum(double x, bool round) {