public string GetEndOfLineThatFits(string text, float maxWidth, bool encoding, SymbolStyle symbolStyle)
{
if (mReplacement != null) return mReplacement.GetEndOfLineThatFits(text, maxWidth, encoding, symbolStyle);
int lineWidth = Mathf.RoundToInt(maxWidth * size);
if (lineWidth < 1) return text;
int textLength = text.Length;
int remainingWidth = lineWidth;
BMGlyph followingGlyph = null;
int currentCharacterIndex = textLength;
bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols;
while (currentCharacterIndex > 0 && remainingWidth > 0)
{
char currentCharacter = text[--currentCharacterIndex];
// See if there is a symbol matching this text
BMSymbol symbol = useSymbols ? MatchSymbol(text, currentCharacterIndex, textLength) : null;
// Calculate how wide this symbol or character is going to be
int glyphWidth = mSpacingX;
if (symbol != null)
{
glyphWidth += symbol.advance;
}
else
{
// Find the glyph for this character
BMGlyph glyph = mFont.GetGlyph(currentCharacter);
if (glyph != null)
{
glyphWidth += glyph.advance + ((followingGlyph == null) ? 0 : followingGlyph.GetKerning(currentCharacter));
followingGlyph = glyph;
}
else
{
followingGlyph = null;
continue;
}
}
// Remaining width after this glyph gets printed
remainingWidth -= glyphWidth;
}
if (remainingWidth < 0) ++currentCharacterIndex;
return text.Substring(currentCharacterIndex, textLength - currentCharacterIndex);
}