implement dynamic text wrapping based on pixel width
This commit is contained in:
1 parent
0a073243ad
commit
dbf0af2cb2
4 files changed
+132
-6
No files matched your search
@@ -347,6 +347,38 @@ bool TextRenderer::loadGlyphs(const std::string& ttfPath, int pixelSize, const s
|
||||
return true;
|
||||
}
|
||||
|
||||
float TextRenderer::measureTextWidth(const std::string& text, float scale) const
|
||||
{
|
||||
if (text.empty()) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
float penX = 0.0f;
|
||||
float maxLineWidth = 0.0f;
|
||||
|
||||
size_t textPos = 0;
|
||||
while (textPos < text.size()) {
|
||||
uint32_t cp = nextUtf8Codepoint(text, textPos);
|
||||
|
||||
if (cp == '\n') {
|
||||
maxLineWidth = max(maxLineWidth, penX);
|
||||
penX = 0.0f;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto it = glyphs.find(cp);
|
||||
if (it == glyphs.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const GlyphInfo& g = it->second;
|
||||
penX += static_cast<float>(g.advance >> 6) * scale;
|
||||
}
|
||||
|
||||
maxLineWidth = max(maxLineWidth, penX);
|
||||
return maxLineWidth;
|
||||
}
|
||||
|
||||
void TextRenderer::drawText(const std::string& text, float x, float y, float scale, bool centered, std::array<float, 4> color)
|
||||
{
|
||||
if (!r || text.empty() || !atlasTexture) return;
|
||||
|
||||
@@ -29,6 +29,8 @@ public:
|
||||
bool init(Renderer& renderer, const std::string& ttfPath, int pixelSize, const std::string& zipfilename);
|
||||
void drawText(const std::string& text, float x, float y, float scale, bool centered, std::array<float, 4> color = { 1.f,1.f,1.f,1.f });
|
||||
|
||||
float measureTextWidth(const std::string& text, float scale = 1.0f) const;
|
||||
|
||||
// Clear cached meshes (call on window resize / DPI change)
|
||||
void ClearCache();
|
||||
|
||||
|
||||
Reference in new issue
Block a user