| 1999 | } |
| 2000 | |
| 2001 | int findSplitPositionForChar(Label* label, |
| 2002 | const StringUtils::StringUTF8& text, |
| 2003 | int estimatedIdx, |
| 2004 | float originalLeftSpaceWidth, |
| 2005 | float newLineWidth) |
| 2006 | { |
| 2007 | bool startingNewLine = (newLineWidth == originalLeftSpaceWidth); |
| 2008 | |
| 2009 | int stringLength = static_cast<int>(text.length()); |
| 2010 | int leftLength = estimatedIdx; |
| 2011 | |
| 2012 | // The adjustment of the new line position |
| 2013 | std::string leftStr = text.getAsCharSequence(0, leftLength); |
| 2014 | label->setString(leftStr); |
| 2015 | float textRendererWidth = label->getContentSize().width; |
| 2016 | if (originalLeftSpaceWidth < textRendererWidth) // Have protruding |
| 2017 | { |
| 2018 | while (leftLength-- > 0) |
| 2019 | { |
| 2020 | // try to erase a char |
| 2021 | auto& ch = text.getString().at(leftLength); |
| 2022 | leftStr.erase(leftStr.end() - ch._char.length(), leftStr.end()); |
| 2023 | label->setString(leftStr); |
| 2024 | textRendererWidth = label->getContentSize().width; |
| 2025 | if (textRendererWidth <= originalLeftSpaceWidth) // is fitted |
| 2026 | break; |
| 2027 | } |
| 2028 | } |
| 2029 | else if (textRendererWidth < originalLeftSpaceWidth) // A wide margin |
| 2030 | { |
| 2031 | while (leftLength < stringLength) |
| 2032 | { |
| 2033 | // try to append a char |
| 2034 | auto& ch = text.getString().at(leftLength); |
| 2035 | ++leftLength; |
| 2036 | leftStr.append(ch._char); |
| 2037 | label->setString(leftStr); |
| 2038 | textRendererWidth = label->getContentSize().width; |
| 2039 | if (originalLeftSpaceWidth < textRendererWidth) // protruded, undo add |
| 2040 | { |
| 2041 | --leftLength; |
| 2042 | break; |
| 2043 | } |
| 2044 | else if (originalLeftSpaceWidth == textRendererWidth) // quite fit |
| 2045 | { |
| 2046 | break; |
| 2047 | } |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | if (leftLength <= 0) |
| 2052 | return (startingNewLine) ? 1 : 0; |
| 2053 | return leftLength; |
| 2054 | } |
| 2055 | } // namespace |
| 2056 | |
| 2057 | void RichText::handleTextRenderer(std::string_view text, |
no test coverage detected