MCPcopy Create free account
hub / github.com/axmolengine/axmol / findSplitPositionForWord

Function findSplitPositionForWord

core/ui/UIRichText.cpp:1941–1999  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1939}
1940
1941int findSplitPositionForWord(Label* label,
1942 const StringUtils::StringUTF8& text,
1943 int estimatedIdx,
1944 float originalLeftSpaceWidth,
1945 float newLineWidth)
1946{
1947 const bool startingNewLine = (newLineWidth == originalLeftSpaceWidth);
1948 if (!isWrappable(text))
1949 return (startingNewLine ? static_cast<int>(text.length()) : 0);
1950
1951 // The adjustment of the new line position
1952 int idx = getNextWordPos(text, estimatedIdx);
1953 std::string leftStr = text.getAsCharSequence(0, idx);
1954 label->setString(leftStr);
1955 float textRendererWidth = label->getContentSize().width;
1956 if (originalLeftSpaceWidth < textRendererWidth) // Have protruding
1957 {
1958 while (1)
1959 {
1960 // try to erase a word
1961 int newidx = getPrevWordPos(text, idx);
1962 if (newidx >= 0)
1963 {
1964 leftStr = text.getAsCharSequence(0, newidx);
1965 label->setString(leftStr);
1966 textRendererWidth = label->getContentSize().width;
1967 if (textRendererWidth <= originalLeftSpaceWidth) // is fitted
1968 return newidx;
1969 idx = newidx;
1970 continue;
1971 }
1972 // newidx < 0 means no prev word
1973 return (startingNewLine ? idx : 0);
1974 }
1975 }
1976 else if (textRendererWidth < originalLeftSpaceWidth) // A wide margin
1977 {
1978 while (1)
1979 {
1980 // try to append a word
1981 int newidx = getNextWordPos(text, idx);
1982 leftStr = text.getAsCharSequence(0, newidx);
1983 label->setString(leftStr);
1984 textRendererWidth = label->getContentSize().width;
1985 if (textRendererWidth < originalLeftSpaceWidth)
1986 {
1987 // the whole string is tested
1988 if (newidx == static_cast<int>(text.length()))
1989 return newidx;
1990 idx = newidx;
1991 continue;
1992 }
1993 // protruded ? undo add, or quite fit
1994 return (textRendererWidth > originalLeftSpaceWidth ? idx : newidx);
1995 }
1996 }
1997
1998 return idx;

Callers 2

handleTextRendererMethod · 0.85
handleTextRendererMethod · 0.85

Calls 7

getNextWordPosFunction · 0.85
getPrevWordPosFunction · 0.85
getAsCharSequenceMethod · 0.80
isWrappableFunction · 0.70
lengthMethod · 0.45
setStringMethod · 0.45
getContentSizeMethod · 0.45

Tested by

no test coverage detected