From 3f8abb0807d90070fd8b90db3de295cc1dcc4f2d Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Mon, 12 Aug 2024 22:03:17 +0200 Subject: [PATCH] Removed direct use of Font::wrapText() from TextComponent --- es-core/src/components/TextComponent.cpp | 20 ++++------ es-core/src/resources/Font.cpp | 47 ++++++++++++++---------- es-core/src/resources/Font.h | 11 ++++-- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/es-core/src/components/TextComponent.cpp b/es-core/src/components/TextComponent.cpp index 531214b43..c5843e647 100644 --- a/es-core/src/components/TextComponent.cpp +++ b/es-core/src/components/TextComponent.cpp @@ -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(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(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( - 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(font->buildTextCache( + text, glm::vec2 {0.0f, offsetY}, mColor, mSize.x, 0.0f, mHorizontalAlignment, + mLineSpacing, mNoTopMargin, true, isMultiline)); } if (mAutoCalcExtent.y) diff --git a/es-core/src/resources/Font.cpp b/es-core/src/resources/Font.cpp index 360404b32..afa90a7d1 100644 --- a/es-core/src/resources/Font.cpp +++ b/es-core/src/resources/Font.cpp @@ -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,10 +527,10 @@ TextCache* Font::buildTextCache(const std::string& text, if (!segment.doShape && character == '\n') { y += getHeight(lineSpacing); x = offset[0] + - (xLen != 0 ? getNewlineStartOffset( - text, static_cast(segment.startPos + 1), - xLen, alignment) : - 0); + (length != 0 ? getNewlineStartOffset( + text, static_cast(segment.startPos + 1), + 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,20 +1159,20 @@ float Font::getNewlineStartOffset(const std::string& text, case ALIGN_CENTER: { int endChar {0}; endChar = static_cast(text.find('\n', charStart)); - return (xLen - sizeText(text.substr(charStart, - static_cast(endChar) != std::string::npos ? - endChar - charStart : - endChar)) - .x) / + return (length - sizeText(text.substr(charStart, static_cast(endChar) != + std::string::npos ? + endChar - charStart : + endChar)) + .x) / 2.0f; } case ALIGN_RIGHT: { int endChar = static_cast(text.find('\n', charStart)); - return xLen - (sizeText(text.substr(charStart, - static_cast(endChar) != std::string::npos ? - endChar - charStart : - endChar)) - .x); + return length - (sizeText(text.substr(charStart, static_cast(endChar) != + std::string::npos ? + endChar - charStart : + endChar)) + .x); } default: return 0; diff --git a/es-core/src/resources/Font.h b/es-core/src/resources/Font.h index 9de0f33e9..8912f9eb4 100644 --- a/es-core/src/resources/Font.h +++ b/es-core/src/resources/Font.h @@ -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};