1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-10-09 15:47:26 -04:00

fix vertical text scaling and offset

This commit is contained in:
epezent 2020-08-24 08:51:03 -05:00
parent 5cc168dbf0
commit 1b4a2049a7
4 changed files with 35 additions and 20 deletions

View File

@ -252,22 +252,37 @@ static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx) {
// Generic Helpers // Generic Helpers
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
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) {
pos.x = IM_ROUND(pos.x); if (!text_end)
pos.y = IM_ROUND(pos.y); text_end = text_begin + strlen(text_begin);
ImFont* font = GImGui->Font; ImGuiContext& g = *GImGui;
const ImFontGlyph *glyph; ImFont* font = g.Font;
for (char c = *text++; c; c = *text++) { pos.x = IM_FLOOR(pos.x + font->DisplayOffset.y);
glyph = font->FindGlyph(c); pos.y = IM_FLOOR(pos.y + font->DisplayOffset.x);
if (!glyph) 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; continue;
DrawList->PrimReserve(6, 4); DrawList->PrimQuadUV(pos + ImVec2(glyph->Y0, -glyph->X0) * scale, pos + ImVec2(glyph->Y0, -glyph->X1) * scale,
DrawList->PrimQuadUV( pos + ImVec2(glyph->Y1, -glyph->X1) * scale, pos + ImVec2(glyph->Y1, -glyph->X0) * scale,
pos + ImVec2(glyph->Y0, -glyph->X0), pos + ImVec2(glyph->Y0, -glyph->X1), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V0),
pos + ImVec2(glyph->Y1, -glyph->X1), pos + ImVec2(glyph->Y1, -glyph->X0), ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1),
ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V0), col);
ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1), text_color); pos.y -= glyph->AdvanceX * scale;
pos.y -= glyph->AdvanceX;
} }
} }
@ -1222,8 +1237,8 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons
if (y_label) { if (y_label) {
const ImVec2 yLabel_size = CalcTextSizeVertical(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); 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 // render tick labels
ImGui::PushClipRect(gp.BB_Frame.Min, gp.BB_Frame.Max, true); ImGui::PushClipRect(gp.BB_Frame.Min, gp.BB_Frame.Max, true);

View File

@ -548,7 +548,7 @@ void ShowDemoWindow(bool* p_open) {
ImPlot::PlotText("Filled Markers", 2.5f, 6.0f); ImPlot::PlotText("Filled Markers", 2.5f, 6.0f);
ImPlot::PlotText("Open Markers", 7.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); ImPlot::PlotText("Fancy Markers", 5.0f, 6.0f, true);
ImGui::PopStyleColor(); ImGui::PopStyleColor();

View File

@ -457,7 +457,7 @@ double NiceNum(double x, bool round);
// Updates axis ticks, lins, and label colors // Updates axis ticks, lins, and label colors
void UpdateAxisColors(int axis_flag, ImPlotAxisColor* col); void UpdateAxisColors(int axis_flag, ImPlotAxisColor* col);
// Draws vertical text. The position is the bottom left of the text rect. // 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 // Calculates the size of vertical text
ImVec2 CalcTextSizeVertical(const char *text); ImVec2 CalcTextSizeVertical(const char *text);
// Returns white or black text given background color // Returns white or black text given background color

View File

@ -1470,7 +1470,7 @@ void PlotText(const char* text, double x, double y, bool vertical, const ImVec2&
if (vertical) { if (vertical) {
ImVec2 ctr = CalcTextSizeVertical(text) * 0.5f; ImVec2 ctr = CalcTextSizeVertical(text) * 0.5f;
ImVec2 pos = PlotToPixels(ImPlotPoint(x,y)) + ImVec2(-ctr.x, ctr.y) + pixel_offset; ImVec2 pos = PlotToPixels(ImPlotPoint(x,y)) + ImVec2(-ctr.x, ctr.y) + pixel_offset;
AddTextVertical(&DrawList, text, pos, colTxt); AddTextVertical(&DrawList, pos, colTxt, text);
} }
else { else {
ImVec2 pos = PlotToPixels(ImPlotPoint(x,y)) - ImGui::CalcTextSize(text) * 0.5f + pixel_offset; ImVec2 pos = PlotToPixels(ImPlotPoint(x,y)) - ImGui::CalcTextSize(text) * 0.5f + pixel_offset;