Exemplo n.º 1
0
    /// <summary>
    /// Calculate the character index offset required to print the end of the specified text.
    /// Originally contributed by MightyM: http://www.tasharen.com/forum/index.php?topic=1049.0
    /// </summary>

    public int CalculateOffsetToFit(string text, int size, int lineWidth, bool encoding, SymbolStyle symbolStyle)
    {
        if (lineWidth < 1)
        {
            return(0);
        }
        if (mReplacement != null)
        {
            return(mReplacement.CalculateOffsetToFit(text, size, lineWidth, encoding, symbolStyle));
        }

#if DYNAMIC_FONT
        if (isDynamic)
        {
            return(NGUIText.CalculateOffsetToFit(text, mDynamicFont, size, mDynamicFontStyle, lineWidth));
        }
#endif
        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(currentCharacterIndex);
    }