GetEndOfLineThatFits() public method

Different line wrapping functionality -- contributed by MightyM. http://www.tasharen.com/forum/index.php?topic=1049.0
public GetEndOfLineThatFits ( string text, float maxWidth, bool encoding, SymbolStyle symbolStyle ) : string
text string
maxWidth float
encoding bool
symbolStyle SymbolStyle
return string
Beispiel #1
0
    /// <summary>
    /// Different line wrapping functionality -- contributed by MightyM.
    /// http://www.tasharen.com/forum/index.php?topic=1049.0
    /// </summary>

    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 && symbol.Validate(atlas))
            {
                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));
    }
Beispiel #2
0
    private void UpdateLabel()
    {
        if (mDoInit)
        {
            initMain();
        }
        if (maxChars > 0 && mText.Length > maxChars)
        {
            mText = mText.Substring(0, maxChars);
        }
        if (label.font == null)
        {
            return;
        }
        string str;

        if (isPassword && selected)
        {
            str = string.Empty;
            int i = 0;
            for (int length = mText.Length; i < length; i++)
            {
                str += "*";
            }
            str = str + Input.compositionString + caratChar;
        }
        else
        {
            str = ((!selected) ? mText : (mText + Input.compositionString + caratChar));
        }
        label.supportEncoding = false;
        if (!label.shrinkToFit)
        {
            if (label.multiLine)
            {
                UIFont  font       = label.font;
                string  text       = str;
                float   num        = label.lineWidth;
                Vector3 localScale = label.cachedTransform.localScale;
                str = font.WrapText(text, num / localScale.x, 0, encoding: false, UIFont.SymbolStyle.None);
            }
            else
            {
                UIFont  font2             = label.font;
                string  text2             = str;
                float   num2              = label.lineWidth;
                Vector3 localScale2       = label.cachedTransform.localScale;
                string  endOfLineThatFits = font2.GetEndOfLineThatFits(text2, num2 / localScale2.x, encoding: false, UIFont.SymbolStyle.None);
                if (endOfLineThatFits != str)
                {
                    str = endOfLineThatFits;
                    Vector3 localPosition = label.cachedTransform.localPosition;
                    localPosition.x = mPosition + (float)label.lineWidth;
                    if (mPivot == UIWidget.Pivot.Left)
                    {
                        label.pivot = UIWidget.Pivot.Right;
                    }
                    else if (mPivot == UIWidget.Pivot.TopLeft)
                    {
                        label.pivot = UIWidget.Pivot.TopRight;
                    }
                    else if (mPivot == UIWidget.Pivot.BottomLeft)
                    {
                        label.pivot = UIWidget.Pivot.BottomRight;
                    }
                    label.cachedTransform.localPosition = localPosition;
                }
                else
                {
                    RestoreLabel();
                }
            }
        }
        label.text = str;
        label.showLastPasswordChar = selected;
    }
Beispiel #3
0
    /// <summary>
    /// Different line wrapping functionality -- contributed by MightyM.
    /// http://www.tasharen.com/forum/index.php?topic=1049.0
    /// </summary>

    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;
        bool    dynamic = isDynamic;

#if DYNAMIC_FONT
        if (dynamic)
        {
            mDynamicFont.textureRebuildCallback = OnFontChanged;
            mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle);
            mDynamicFont.textureRebuildCallback = null;
        }
#endif
        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 (!dynamic)
            {
                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;
                    }
                }
            }
#if DYNAMIC_FONT
            else
            {
                if (mDynamicFont.GetCharacterInfo(currentCharacter, out mChar, mDynamicFontSize, mDynamicFontStyle))
                {
                    glyphWidth += (int)mChar.width;
                }
            }
#endif
            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;
        }
        if (remainingWidth < 0)
        {
            ++currentCharacterIndex;
        }
        return(text.Substring(currentCharacterIndex, textLength - currentCharacterIndex));
    }
Beispiel #4
0
    public string GetEndOfLineThatFits(string text, float maxWidth, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.GetEndOfLineThatFits(text, maxWidth, encoding, symbolStyle));
        }

        var num = Mathf.RoundToInt(maxWidth * size);

        if (num < 1)
        {
            return(text);
        }

        var     length    = text.Length;
        var     num3      = num;
        BMGlyph glyph     = null;
        var     offset    = length;
        var     flag      = encoding && symbolStyle != SymbolStyle.None && hasSymbols;
        var     isDynamic = this.isDynamic;

        if (isDynamic)
        {
            mDynamicFont.textureRebuildCallback = OnFontChanged;
            mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle);
            mDynamicFont.textureRebuildCallback = null;
        }

        while (offset > 0 && num3 > 0)
        {
            var index     = text[--offset];
            var symbol    = !flag ? null : MatchSymbol(text, offset, length);
            var mSpacingX = this.mSpacingX;
            if (!isDynamic)
            {
                if (symbol != null)
                {
                    mSpacingX += symbol.advance;
                    goto Label_017F;
                }

                var glyph2 = mFont.GetGlyph(index);
                if (glyph2 != null)
                {
                    mSpacingX += glyph2.advance + (glyph != null ? glyph.GetKerning(index) : 0);
                    glyph      = glyph2;
                    goto Label_017F;
                }

                glyph = null;
                continue;
            }

            if (mDynamicFont.GetCharacterInfo(index, out mChar, mDynamicFontSize, mDynamicFontStyle))
            {
                mSpacingX += (int)mChar.width;
            }

Label_017F:
            num3 -= mSpacingX;
        }

        if (num3 < 0)
        {
            offset++;
        }

        return(text.Substring(offset, length - offset));
    }
Beispiel #5
0
    /// <summary>
    /// Different line wrapping functionality -- contributed by MightyM.
    /// http://www.tasharen.com/forum/index.php?topic=1049.0
    /// </summary>

    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;

        if (UseDynamicFont)
        {
            dynamicFont.RequestCharactersInTexture(text, dynamicFontSize, dynamicFontStyle);
        }
        CharacterInfo charInfo;

        while (currentCharacterIndex > 0 && remainingWidth > 0)
        {
            char currentCharacter = text[--currentCharacterIndex];

            // Calculate how wide this symbol or character is going to be
            int glyphWidth = mSpacingX;

            if (UseDynamicFont)
            {
                if (dynamicFont.GetCharacterInfo(currentCharacter, out charInfo, dynamicFontSize, dynamicFontStyle))
                {
                    glyphWidth += (int)charInfo.width;
                }
            }
            else
            {
                // See if there is a symbol matching this text
                BMSymbol symbol = (encoding && symbolStyle != SymbolStyle.None) ? mFont.MatchSymbol(text, currentCharacterIndex, textLength) : null;

                // Find the glyph for this character
                BMGlyph glyph = (symbol == null) ? mFont.GetGlyph(currentCharacter) : null;

                if (symbol != null)
                {
                    glyphWidth += symbol.width;
                }
                else 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));
    }
Beispiel #6
0
    public string GetEndOfLineThatFits(string text, float maxWidth, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.GetEndOfLineThatFits(text, maxWidth, encoding, symbolStyle));
        }
        int num = Mathf.RoundToInt(maxWidth * (float)size);

        if (num < 1)
        {
            return(text);
        }
        int     length    = text.Length;
        int     num2      = num;
        BMGlyph bMGlyph   = null;
        int     num3      = length;
        bool    flag      = encoding && symbolStyle != 0 && hasSymbols;
        bool    isDynamic = this.isDynamic;

        if (isDynamic)
        {
            Font.textureRebuilt += OnFontChanged;
            // mDynamicFont.textureRebuildCallback = OnFontChanged;
            mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize, mDynamicFontStyle);
            // mDynamicFont.textureRebuildCallback = null;
            Font.textureRebuilt -= OnFontChanged;
        }
        while (num3 > 0 && num2 > 0)
        {
            char     c        = text[--num3];
            BMSymbol bMSymbol = (!flag) ? null : MatchSymbol(text, num3, length);
            int      num4     = mSpacingX;
            if (!isDynamic)
            {
                if (bMSymbol != null)
                {
                    num4 += bMSymbol.advance;
                }
                else
                {
                    BMGlyph glyph = mFont.GetGlyph(c);
                    if (glyph == null)
                    {
                        bMGlyph = null;
                        continue;
                    }
                    num4   += glyph.advance + (bMGlyph?.GetKerning(c) ?? 0);
                    bMGlyph = glyph;
                }
            }
            else if (mDynamicFont.GetCharacterInfo(c, out mChar, mDynamicFontSize, mDynamicFontStyle))
            {
                num4 += (int)mChar.width;
            }
            num2 -= num4;
        }
        if (num2 < 0)
        {
            num3++;
        }
        return(text.Substring(num3, length - num3));
    }