From 1b4a2049a77f5b3b0688d6f9536f3614e44b7c4c Mon Sep 17 00:00:00 2001 From: epezent Date: Mon, 24 Aug 2020 08:51:03 -0500 Subject: [PATCH] fix vertical text scaling and offset --- implot.cpp | 49 +++++++++++++++++++++++++++++++---------------- implot_demo.cpp | 2 +- implot_internal.h | 2 +- implot_items.cpp | 2 +- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/implot.cpp b/implot.cpp index b6d738d..8962b69 100644 --- a/implot.cpp +++ b/implot.cpp @@ -252,22 +252,37 @@ static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx) { // Generic Helpers //----------------------------------------------------------------------------- -void AddTextVertical(ImDrawList *DrawList, const char *text, ImVec2 pos, ImU32 text_color) { - pos.x = IM_ROUND(pos.x); - pos.y = IM_ROUND(pos.y); - ImFont* font = GImGui->Font; - const ImFontGlyph *glyph; - for (char c = *text++; c; c = *text++) { - glyph = font->FindGlyph(c); - if (!glyph) +void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char *text_begin, const char* text_end) { + if (!text_end) + text_end = text_begin + strlen(text_begin); + ImGuiContext& g = *GImGui; + ImFont* font = g.Font; + pos.x = IM_FLOOR(pos.x + font->DisplayOffset.y); + pos.y = IM_FLOOR(pos.y + font->DisplayOffset.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); + const float scale = g.FontSize / font->FontSize; + while (s < text_end) { + unsigned int c = (unsigned int)*s; + if (c < 0x80) { + s += 1; + } + else { + s += ImTextCharFromUtf8(&c, s, text_end); + if (c == 0) // Malformed UTF-8? + break; + } + const ImFontGlyph * glyph = font->FindGlyph((ImWchar)c); + if (glyph == NULL) continue; - DrawList->PrimReserve(6, 4); - DrawList->PrimQuadUV( - pos + ImVec2(glyph->Y0, -glyph->X0), pos + ImVec2(glyph->Y0, -glyph->X1), - pos + ImVec2(glyph->Y1, -glyph->X1), pos + ImVec2(glyph->Y1, -glyph->X0), - ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V0), - ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1), text_color); - pos.y -= glyph->AdvanceX; + 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; } } @@ -1222,8 +1237,8 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons if (y_label) { const ImVec2 yLabel_size = CalcTextSizeVertical(y_label); const ImVec2 yLabel_pos(gp.BB_Canvas.Min.x, gp.BB_Plot.GetCenter().y + yLabel_size.y * 0.5f); - AddTextVertical(&DrawList, y_label, yLabel_pos, gp.Col_Y[0].MajTxt); - } + AddTextVertical(&DrawList, yLabel_pos, gp.Col_Y[0].MajTxt, y_label); + } // render tick labels ImGui::PushClipRect(gp.BB_Frame.Min, gp.BB_Frame.Max, true); diff --git a/implot_demo.cpp b/implot_demo.cpp index ae4ffac..9627772 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -548,7 +548,7 @@ void ShowDemoWindow(bool* p_open) { ImPlot::PlotText("Filled Markers", 2.5f, 6.0f); ImPlot::PlotText("Open Markers", 7.5f, 6.0f); - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0,1,0,1)); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,0,1,1)); ImPlot::PlotText("Fancy Markers", 5.0f, 6.0f, true); ImGui::PopStyleColor(); diff --git a/implot_internal.h b/implot_internal.h index f4c4237..db71914 100644 --- a/implot_internal.h +++ b/implot_internal.h @@ -457,7 +457,7 @@ double NiceNum(double x, bool round); // Updates axis ticks, lins, and label colors void UpdateAxisColors(int axis_flag, ImPlotAxisColor* col); // Draws vertical text. The position is the bottom left of the text rect. -void AddTextVertical(ImDrawList *DrawList, const char *text, ImVec2 pos, ImU32 text_color); +void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char* text_begin, const char* text_end = NULL); // Calculates the size of vertical text ImVec2 CalcTextSizeVertical(const char *text); // Returns white or black text given background color diff --git a/implot_items.cpp b/implot_items.cpp index b593504..52ac0e0 100644 --- a/implot_items.cpp +++ b/implot_items.cpp @@ -1470,7 +1470,7 @@ void PlotText(const char* text, double x, double y, bool vertical, const ImVec2& if (vertical) { ImVec2 ctr = CalcTextSizeVertical(text) * 0.5f; ImVec2 pos = PlotToPixels(ImPlotPoint(x,y)) + ImVec2(-ctr.x, ctr.y) + pixel_offset; - AddTextVertical(&DrawList, text, pos, colTxt); + AddTextVertical(&DrawList, pos, colTxt, text); } else { ImVec2 pos = PlotToPixels(ImPlotPoint(x,y)) - ImGui::CalcTextSize(text) * 0.5f + pixel_offset;