Skip to content

Commit b35ec2a

Browse files
committed
GPU Display: Wrap lines of help text if they do not fit on screen
1 parent a571628 commit b35ec2a

10 files changed

Lines changed: 43 additions & 15 deletions

‎GPU/GPUTracking/display/GPUDisplay.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class GPUDisplay : public GPUDisplayInterface
233233
void PrintHelp();
234234
void createQuaternionFromMatrix(float* v, const float* mat);
235235
void drawVertices(const vboList& v, const GPUDisplayBackend::drawType t);
236-
void OpenGLPrint(const char* s, float x, float y, float r, float g, float b, float a, bool fromBotton = true);
236+
void GLPrintText(const char* s, float x, float y, float r, float g, float b, float a, uint32_t nCharacters = 0, bool fromBotton = true); // nCharacters > 0: print at most that many characters
237237

238238
GPUDisplayFrontend* mFrontend = nullptr;
239239
std::unique_ptr<GPUDisplayBackend> mBackend;

‎GPU/GPUTracking/display/backend/GPUDisplayBackend.cxx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ void GPUDisplayBackend::loadFont()
140140
return;
141141
}
142142
const auto& glyph = face->glyph;
143+
if (i == 'M') {
144+
mFontCharWidth = (glyph->advance.x >> 6) * (smoothFont() ? 0.25f : 1.f) / mFontRasterScale; // As in GLPrintText of the backends
145+
}
143146
addFontSymbol(i, glyph->bitmap.width, glyph->bitmap.rows, glyph->bitmap_left, glyph->bitmap_top, glyph->advance.x, glyph->bitmap.buffer);
144147
}
145148
initializeTextDrawing();

‎GPU/GPUTracking/display/backend/GPUDisplayBackend.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class GPUDisplayBackend
9999
const char* backendName() const { return mBackendName; }
100100
virtual void resizeScene(uint32_t width, uint32_t height) {}
101101
virtual size_t needMultiVBO() { return 0; }
102-
virtual void OpenGLPrint(const char* s, float x, float y, float* color, float scale) = 0;
102+
virtual void GLPrintText(const char* s, float x, float y, float* color, float scale, uint32_t nCharacters) = 0; // nCharacters > 0: at most that many characters, otherwise until the terminating 0
103103
static GPUDisplayBackend* getBackend(const char* type);
104104
std::vector<char> getPixels();
105105
virtual float getYFactor() const { return 1.0f; }
@@ -120,6 +120,7 @@ class GPUDisplayBackend
120120
vecpod<DrawArraysIndirectCommand> mCmdBuffer;
121121
bool mFreetypeInitialized = false;
122122
float mFontRasterScale = 1.f; // Display scale the font was rasterized for
123+
float mFontCharWidth = 0.f; // Advance of a character of the (monospace) font in logical pixels, 0 without Freetype font
123124
std::vector<char> mScreenshotPixels;
124125

125126
static constexpr float SMOOTH_POINT_EXTRA_SIZE = 0.6f;

‎GPU/GPUTracking/display/backend/GPUDisplayBackendNone.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GPUDisplayBackendNone : public GPUDisplayBackend
3939
void finishText() override {}
4040
void pointSizeFactor(float factor) override {}
4141
void lineWidthFactor(float factor) override {}
42-
void OpenGLPrint(const char* s, float x, float y, float* color, float scale) override {}
42+
void GLPrintText(const char* s, float x, float y, float* color, float scale, uint32_t nCharacters) override {}
4343
void addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data) override {}
4444
void initializeTextDrawing() override {}
4545
};

‎GPU/GPUTracking/display/backend/GPUDisplayBackendOpenGL.cxx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ void GPUDisplayBackendOpenGL::initializeTextDrawing()
668668
CHKERR(glBindVertexArray(0));
669669
}
670670

671-
void GPUDisplayBackendOpenGL::OpenGLPrint(const char* s, float x, float y, float* color, float scale)
671+
void GPUDisplayBackendOpenGL::GLPrintText(const char* s, float x, float y, float* color, float scale, uint32_t nCharacters)
672672
{
673673
if (!mFreetypeInitialized) {
674674
return;
@@ -678,7 +678,7 @@ void GPUDisplayBackendOpenGL::OpenGLPrint(const char* s, float x, float y, float
678678
if (smoothFont()) {
679679
scale *= 0.25f; // Font size is 48 to have nice bitmap, scale to size 12
680680
}
681-
for (const unsigned char* c = (const unsigned char*)s; *c; c++) {
681+
for (const unsigned char* c = (const unsigned char*)s; *c && (nCharacters == 0 || c < (const unsigned char*)s + nCharacters); c++) {
682682
if ((int32_t)*c >= (int32_t)mFontSymbols.size()) {
683683
GPUError("Trying to draw unsupported symbol: %d > %d\n", (int32_t)*c, (int32_t)mFontSymbols.size());
684684
continue;

‎GPU/GPUTracking/display/backend/GPUDisplayBackendOpenGL.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class GPUDisplayBackendOpenGL : public GPUDisplayBackend
7979
void addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data) override;
8080
void initializeTextDrawing() override;
8181
void clearFontSymbols() override;
82-
void OpenGLPrint(const char* s, float x, float y, float* color, float scale) override;
82+
void GLPrintText(const char* s, float x, float y, float* color, float scale, uint32_t nCharacters) override;
8383

8484
struct FontSymbolOpenGL : public FontSymbol {
8585
uint32_t texId;

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.cxx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ void GPUDisplayBackendVulkan::clearFontSymbols()
18881888
mFontSymbols.clear();
18891889
}
18901890

1891-
void GPUDisplayBackendVulkan::OpenGLPrint(const char* s, float x, float y, float* color, float scale)
1891+
void GPUDisplayBackendVulkan::GLPrintText(const char* s, float x, float y, float* color, float scale, uint32_t nCharacters)
18921892
{
18931893
if (!mFreetypeInitialized) {
18941894
return;
@@ -1899,7 +1899,7 @@ void GPUDisplayBackendVulkan::OpenGLPrint(const char* s, float x, float y, float
18991899
scale *= 0.25f; // Font size is 48 to have nice bitmap, scale to size 12
19001900
}
19011901

1902-
for (const unsigned char* c = (const unsigned char*)s; *c; c++) {
1902+
for (const unsigned char* c = (const unsigned char*)s; *c && (nCharacters == 0 || c < (const unsigned char*)s + nCharacters); c++) {
19031903
if ((int32_t)*c >= (int32_t)mFontSymbols.size()) {
19041904
GPUError("Trying to draw unsupported symbol: %d > %d\n", (int32_t)*c, (int32_t)mFontSymbols.size());
19051905
continue;

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class GPUDisplayBackendVulkan : public GPUDisplayBackend
136136
void addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data) override;
137137
void initializeTextDrawing() override;
138138
void clearFontSymbols() override;
139-
void OpenGLPrint(const char* s, float x, float y, float* color, float scale) override;
139+
void GLPrintText(const char* s, float x, float y, float* color, float scale, uint32_t nCharacters) override;
140140

141141
uint32_t mGraphicsFamily;
142142
SwapChainSupportDetails mSwapChainDetails;

‎GPU/GPUTracking/display/frontend/GPUDisplayHelp.cxx‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "GPUDisplay.h"
1616
#include "GPULogging.h"
1717

18+
#include <string>
19+
1820
using namespace o2::gpu;
1921

2022
void GPUDisplay::PrintHelp()
@@ -28,7 +30,29 @@ void GPUDisplay::PrintHelp()
2830
void GPUDisplay::PrintGLHelpText(float colorValue)
2931
{
3032
const auto& lines = mFrontend->getHelpText();
31-
for (uint32_t i = 0; i < lines.size(); i++) {
32-
OpenGLPrint(lines[i].c_str(), 40.f, 35 + std::max(20, mDrawTextFontSize + 4) * (1 + i), colorValue, colorValue, colorValue, mInfoHelpTimer.GetCurrentElapsedTime() >= 5 ? (6 - mInfoHelpTimer.GetCurrentElapsedTime()) : 1, false);
33+
const int32_t maxChars = mBackend->mFontCharWidth > 0.f ? (mBackend->mScreenWidth / mFrontend->getDisplayScale() - 80.f) / mBackend->mFontCharWidth : 0; // Characters fitting between the margins, 0 = unknown (no Freetype font), no wrapping
34+
uint32_t row = 0;
35+
auto print = [&](const char* s, uint32_t nCharacters, uint32_t indent) { GLPrintText(s, 40.f + indent * mBackend->mFontCharWidth, 35 + std::max(20, mDrawTextFontSize + 4) * (1 + row++), colorValue, colorValue, colorValue, mInfoHelpTimer.GetCurrentElapsedTime() >= 5 ? (6 - mInfoHelpTimer.GetCurrentElapsedTime()) : 1, nCharacters, false); };
36+
for (const auto& line : lines) {
37+
if (maxChars <= 0 || line.size() <= (size_t)maxChars) {
38+
print(line.c_str(), 0, 0);
39+
continue;
40+
}
41+
const size_t textStart = line.find(": ");
42+
const size_t indent = textStart != std::string::npos && textStart + 2 < (size_t)maxChars / 2 ? textStart + 2 : 0; // Continuation lines aligned with the text after the keys, if that leaves enough space
43+
size_t pos = 0; // Start of the part not yet printed
44+
for (bool first = true;; first = false) { // Wrap at the last space that fits, or hard if there is none (also if it is only in the keys)
45+
const size_t lineIndent = first ? 0 : indent, available = maxChars - lineIndent;
46+
if (line.size() - pos <= available) {
47+
print(line.c_str() + pos, line.size() - pos, lineIndent);
48+
break;
49+
}
50+
size_t cut = line.rfind(' ', pos + available);
51+
if (cut == std::string::npos || cut <= pos + (first ? indent : 0)) {
52+
cut = pos + available;
53+
}
54+
print(line.c_str() + pos, cut - pos, lineIndent);
55+
pos = line[cut] == ' ' ? cut + 1 : cut;
56+
}
3357
}
3458
}

‎GPU/GPUTracking/display/helpers/GPUDisplayHelpers.cxx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ void GPUDisplay::showInfo(const char* info)
102102
mBackend->updateFontScale(mFrontend->getDisplayScale());
103103
mBackend->prepareText();
104104
float colorValue = mCfgL.invertColors ? 0.f : 1.f;
105-
OpenGLPrint(info, 40.f, 20.f + std::max(20, mDrawTextFontSize + 4), colorValue, colorValue, colorValue, 1);
105+
GLPrintText(info, 40.f, 20.f + std::max(20, mDrawTextFontSize + 4), colorValue, colorValue, colorValue, 1);
106106
if (mInfoText2Timer.IsRunning()) {
107107
if (mInfoText2Timer.GetCurrentElapsedTime() >= 6) {
108108
mInfoText2Timer.Reset();
109109
} else {
110-
OpenGLPrint(mInfoText2, 40.f, 20.f, colorValue, colorValue, colorValue, 6 - mInfoText2Timer.GetCurrentElapsedTime());
110+
GLPrintText(mInfoText2, 40.f, 20.f, colorValue, colorValue, colorValue, 6 - mInfoText2Timer.GetCurrentElapsedTime());
111111
}
112112
}
113113
if (mInfoHelpTimer.IsRunning() || mCfgC.mPrintInfoTextAlways) {
@@ -120,7 +120,7 @@ void GPUDisplay::showInfo(const char* info)
120120
mBackend->finishText();
121121
}
122122

123-
void GPUDisplay::OpenGLPrint(const char* s, float x, float y, float r, float g, float b, float a, bool fromBotton)
123+
void GPUDisplay::GLPrintText(const char* s, float x, float y, float r, float g, float b, float a, uint32_t nCharacters, bool fromBotton)
124124
{
125125
if (mBackend->mFreetypeInitialized) {
126126
const float scale = mFrontend->getDisplayScale();
@@ -130,6 +130,6 @@ void GPUDisplay::OpenGLPrint(const char* s, float x, float y, float r, float g,
130130
y = mBackend->mScreenHeight - y;
131131
}
132132
float color[4] = {r, g, b, a};
133-
mBackend->OpenGLPrint(s, x, y, color, scale / mBackend->mFontRasterScale);
133+
mBackend->GLPrintText(s, x, y, color, scale / mBackend->mFontRasterScale, nCharacters);
134134
}
135135
}

0 commit comments

Comments
 (0)