mirror of
https://github.com/gwm17/implot.git
synced 2024-11-13 22:48:50 -05:00
add PrimUnreserve to AddTextVertical when the render characters is less than the expected characters (fixes utf8 issues)
This commit is contained in:
parent
c73509d6d1
commit
6e2499093b
23
implot.cpp
23
implot.cpp
|
@ -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) {
|
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)
|
if (!text_end)
|
||||||
text_end = text_begin + strlen(text_begin);
|
text_end = text_begin + strlen(text_begin);
|
||||||
ImGuiContext& g = *GImGui;
|
ImGuiContext& g = *GImGui;
|
||||||
ImFont* font = g.Font;
|
ImFont* font = g.Font;
|
||||||
pos.x = IM_FLOOR(pos.x); // + font->ConfigData->GlyphOffset.y);
|
// Align to be pixel perfect
|
||||||
pos.y = IM_FLOOR(pos.y); // + font->ConfigData->GlyphOffset.x);
|
pos.x = IM_FLOOR(pos.x);
|
||||||
const char* s = text_begin;
|
pos.y = IM_FLOOR(pos.y);
|
||||||
const int vtx_count = (int)(text_end - s) * 4;
|
|
||||||
const int idx_count = (int)(text_end - s) * 6;
|
|
||||||
DrawList->PrimReserve(idx_count, vtx_count);
|
|
||||||
const float scale = g.FontSize / font->FontSize;
|
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) {
|
while (s < text_end) {
|
||||||
unsigned int c = (unsigned int)*s;
|
unsigned int c = (unsigned int)*s;
|
||||||
if (c < 0x80) {
|
if (c < 0x80) {
|
||||||
|
@ -304,15 +308,20 @@ void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char *te
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const ImFontGlyph * glyph = font->FindGlyph((ImWchar)c);
|
const ImFontGlyph * glyph = font->FindGlyph((ImWchar)c);
|
||||||
if (glyph == NULL)
|
if (glyph == NULL) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
DrawList->PrimQuadUV(pos + ImVec2(glyph->Y0, -glyph->X0) * scale, pos + ImVec2(glyph->Y0, -glyph->X1) * scale,
|
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,
|
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->U0, glyph->V0), ImVec2(glyph->U1, glyph->V0),
|
||||||
ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1),
|
ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1),
|
||||||
col);
|
col);
|
||||||
pos.y -= glyph->AdvanceX * scale;
|
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) {
|
double NiceNum(double x, bool round) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user