Removed direct use of Font::wrapText() from TextComponent

This commit is contained in:
Leon Styhre 2024-08-12 22:03:17 +02:00
parent 1d3b2f8066
commit 3f8abb0807
3 changed files with 43 additions and 35 deletions

View file

@ -104,7 +104,7 @@ TextComponent::TextComponent(const std::string& text,
setHorizontalScrolling(mHorizontalScrolling); setHorizontalScrolling(mHorizontalScrolling);
setText(text, false, mMaxLength); setText(text, false, mMaxLength);
setPosition(pos); setPosition(pos);
if (mMaxLength == 0.0f) if (mMaxLength == 0.0f || mMaxLength > size.x)
setSize(size); setSize(size);
else else
setSize(glm::vec2 {mMaxLength, size.y}); setSize(glm::vec2 {mMaxLength, size.y});
@ -503,24 +503,20 @@ void TextComponent::onTextChanged()
if (lineHeight > mSize.y && mSize.y != 0.0f) if (lineHeight > mSize.y && mSize.y != 0.0f)
offsetY = (mSize.y - lineHeight) / 2.0f; offsetY = (mSize.y - lineHeight) / 2.0f;
mTextCache = std::shared_ptr<TextCache>(font->buildTextCache( mTextCache = std::shared_ptr<TextCache>(font->buildTextCache(
text, glm::vec2 {0.0f, offsetY}, mColor, 0.0f, ALIGN_LEFT, mLineSpacing)); text, glm::vec2 {0.0f, offsetY}, mColor, 0.0f, 0.0f, ALIGN_LEFT, mLineSpacing));
} }
else if (isMultiline && !isScrollable) { else if (isMultiline && !isScrollable) {
const std::string wrappedText {
font->wrapText(text, mSize.x * mRelativeScale,
(mVerticalAutoSizing ? 0.0f : (mSize.y * mRelativeScale) - lineHeight),
mLineSpacing, isMultiline)};
mTextCache = std::shared_ptr<TextCache>(font->buildTextCache( mTextCache = std::shared_ptr<TextCache>(font->buildTextCache(
wrappedText, glm::vec2 {0.0f, 0.0f}, mColor, mSize.x * mRelativeScale, text, glm::vec2 {0.0f, 0.0f}, mColor, mSize.x * mRelativeScale,
mHorizontalAlignment, mLineSpacing, mNoTopMargin)); (mVerticalAutoSizing ? 0.0f : (mSize.y * mRelativeScale) - lineHeight),
mHorizontalAlignment, mLineSpacing, mNoTopMargin, true, isMultiline));
} }
else { else {
if (!isMultiline && lineHeight > mSize.y) if (!isMultiline && lineHeight > mSize.y)
offsetY = (mSize.y - lineHeight) / 2.0f; offsetY = (mSize.y - lineHeight) / 2.0f;
mTextCache = std::shared_ptr<TextCache>( mTextCache = std::shared_ptr<TextCache>(font->buildTextCache(
font->buildTextCache(font->wrapText(text, mSize.x, 0.0f, mLineSpacing, isMultiline), text, glm::vec2 {0.0f, offsetY}, mColor, mSize.x, 0.0f, mHorizontalAlignment,
glm::vec2 {0.0f, offsetY}, mColor, mSize.x, mHorizontalAlignment, mLineSpacing, mNoTopMargin, true, isMultiline));
mLineSpacing, mNoTopMargin));
} }
if (mAutoCalcExtent.y) if (mAutoCalcExtent.y)

View file

@ -477,15 +477,24 @@ size_t Font::getTotalMemUsage()
return total; return total;
} }
TextCache* Font::buildTextCache(const std::string& text, TextCache* Font::buildTextCache(const std::string& textArg,
glm::vec2 offset, glm::vec2 offset,
unsigned int color, unsigned int color,
float xLen, float length,
float height,
Alignment alignment, Alignment alignment,
float lineSpacing, float lineSpacing,
bool noTopMargin) bool noTopMargin,
bool doWrapText,
bool multiLine)
{ {
float x {offset.x + (xLen != 0 ? getNewlineStartOffset(text, 0, xLen, alignment) : 0)}; std::string text;
if (doWrapText)
text = wrapText(textArg, length, height, lineSpacing, multiLine);
else
text = textArg;
float x {offset.x + (length != 0 ? getNewlineStartOffset(text, 0, length, alignment) : 0)};
int yTop {0}; int yTop {0};
float yBot {0.0f}; float yBot {0.0f};
@ -518,9 +527,9 @@ TextCache* Font::buildTextCache(const std::string& text,
if (!segment.doShape && character == '\n') { if (!segment.doShape && character == '\n') {
y += getHeight(lineSpacing); y += getHeight(lineSpacing);
x = offset[0] + x = offset[0] +
(xLen != 0 ? getNewlineStartOffset( (length != 0 ? getNewlineStartOffset(
text, static_cast<const unsigned int>(segment.startPos + 1), text, static_cast<const unsigned int>(segment.startPos + 1),
xLen, alignment) : length, alignment) :
0); 0);
continue; continue;
} }
@ -1140,7 +1149,7 @@ Font::Glyph* Font::getGlyphByIndex(const unsigned int id, hb_font_t* fontArg, in
float Font::getNewlineStartOffset(const std::string& text, float Font::getNewlineStartOffset(const std::string& text,
const unsigned int& charStart, const unsigned int& charStart,
const float& xLen, const float& length,
const Alignment& alignment) const Alignment& alignment)
{ {
switch (alignment) { switch (alignment) {
@ -1150,8 +1159,8 @@ float Font::getNewlineStartOffset(const std::string& text,
case ALIGN_CENTER: { case ALIGN_CENTER: {
int endChar {0}; int endChar {0};
endChar = static_cast<int>(text.find('\n', charStart)); endChar = static_cast<int>(text.find('\n', charStart));
return (xLen - sizeText(text.substr(charStart, return (length - sizeText(text.substr(charStart, static_cast<size_t>(endChar) !=
static_cast<size_t>(endChar) != std::string::npos ? std::string::npos ?
endChar - charStart : endChar - charStart :
endChar)) endChar))
.x) / .x) /
@ -1159,8 +1168,8 @@ float Font::getNewlineStartOffset(const std::string& text,
} }
case ALIGN_RIGHT: { case ALIGN_RIGHT: {
int endChar = static_cast<int>(text.find('\n', charStart)); int endChar = static_cast<int>(text.find('\n', charStart));
return xLen - (sizeText(text.substr(charStart, return length - (sizeText(text.substr(charStart, static_cast<size_t>(endChar) !=
static_cast<size_t>(endChar) != std::string::npos ? std::string::npos ?
endChar - charStart : endChar - charStart :
endChar)) endChar))
.x); .x);

View file

@ -124,13 +124,16 @@ public:
static size_t getTotalMemUsage(); static size_t getTotalMemUsage();
protected: protected:
TextCache* buildTextCache(const std::string& text, TextCache* buildTextCache(const std::string& textArg,
glm::vec2 offset, glm::vec2 offset,
unsigned int color, unsigned int color,
float xLen, float length,
float height,
Alignment alignment = ALIGN_LEFT, Alignment alignment = ALIGN_LEFT,
float lineSpacing = 1.5f, float lineSpacing = 1.5f,
bool noTopMargin = false); bool noTopMargin = false,
bool doWrapText = false,
bool multiLine = false);
void renderTextCache(TextCache* cache); void renderTextCache(TextCache* cache);
@ -227,7 +230,7 @@ private:
float getNewlineStartOffset(const std::string& text, float getNewlineStartOffset(const std::string& text,
const unsigned int& charStart, const unsigned int& charStart,
const float& xLen, const float& length,
const Alignment& alignment); const Alignment& alignment);
static inline FT_Library sLibrary {nullptr}; static inline FT_Library sLibrary {nullptr};