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

View file

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

View file

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