示例#1
0
    public string GetEndOfLineThatFits(string text, float maxWidth, bool encoding, SymbolStyle symbolStyle)
    {
        if (this.mReplacement != null)
        {
            return(this.mReplacement.GetEndOfLineThatFits(text, maxWidth, encoding, symbolStyle));
        }
        int num = Mathf.RoundToInt(maxWidth * this.size);

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

        if (isDynamic)
        {
            this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
            this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
            this.mDynamicFont.textureRebuildCallback = null;
        }
        while ((offset > 0) && (num3 > 0))
        {
            char     index     = text[--offset];
            BMSymbol symbol    = !flag ? null : this.MatchSymbol(text, offset, length);
            int      mSpacingX = this.mSpacingX;
            if (!isDynamic)
            {
                if (symbol != null)
                {
                    mSpacingX += symbol.advance;
                    goto Label_017F;
                }
                BMGlyph glyph2 = this.mFont.GetGlyph(index);
                if (glyph2 != null)
                {
                    mSpacingX += glyph2.advance + ((glyph != null) ? glyph.GetKerning(index) : 0);
                    glyph      = glyph2;
                    goto Label_017F;
                }
                glyph = null;
                continue;
            }
            if (this.mDynamicFont.GetCharacterInfo(index, out mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
            {
                mSpacingX += (int)mChar.width;
            }
Label_017F:
            num3 -= mSpacingX;
        }
        if (num3 < 0)
        {
            offset++;
        }
        return(text.Substring(offset, length - offset));
    }
示例#2
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));
    }
示例#3
0
    // Token: 0x06000338 RID: 824 RVA: 0x000258A4 File Offset: 0x00023AA4
    public string GetEndOfLineThatFits(string text, float maxWidth, bool encoding, UIFont.SymbolStyle symbolStyle)
    {
        if (this.mReplacement != null)
        {
            return(this.mReplacement.GetEndOfLineThatFits(text, maxWidth, encoding, symbolStyle));
        }
        int num = Mathf.RoundToInt(maxWidth * (float)this.size);

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

        if (isDynamic)
        {
            this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
            this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
            this.mDynamicFont.textureRebuildCallback = null;
        }
        while (num3 > 0 && num2 > 0)
        {
            char     c        = text[--num3];
            BMSymbol bmsymbol = (!flag) ? null : this.MatchSymbol(text, num3, length);
            int      num4     = this.mSpacingX;
            if (!isDynamic)
            {
                if (bmsymbol != null)
                {
                    num4 += bmsymbol.advance;
                }
                else
                {
                    BMGlyph glyph = this.mFont.GetGlyph((int)c);
                    if (glyph == null)
                    {
                        bmglyph = null;
                        continue;
                    }
                    num4   += glyph.advance + ((bmglyph != null) ? bmglyph.GetKerning((int)c) : 0);
                    bmglyph = glyph;
                }
            }
            else if (this.mDynamicFont.GetCharacterInfo(c, out UIFont.mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
            {
                num4 += (int)UIFont.mChar.width;
            }
            num2 -= num4;
        }
        if (num2 < 0)
        {
            num3++;
        }
        return(text.Substring(num3, length - num3));
    }
示例#4
0
 static public int GetKerning(IntPtr l)
 {
     try {
         BMGlyph      self = (BMGlyph)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         var ret = self.GetKerning(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
    /// <summary>
    /// Get the width of the specified glyph. Returns zero if the glyph could not be retrieved.
    /// </summary>

    static public float GetGlyphWidth(int ch, int prev, float fontScale)
    {
#if NGUI_CUSTOM
        if (false)
#else
        if (bitmapFont != null)
#endif
        {
            bool thinSpace = false;

            if (ch == '\u2009')
            {
                thinSpace = true;
                ch        = ' ';
            }

            BMGlyph bmg = bitmapFont.bmFont.GetGlyph(ch);

            if (bmg != null)
            {
                int adv = bmg.advance;
                if (thinSpace)
                {
                    adv >>= 1;
                }
                return(fontScale * ((prev != 0) ? adv + bmg.GetKerning(prev) : bmg.advance));
            }
        }
#if DYNAMIC_FONT
        else if (dynamicFont != null)
        {
            if (dynamicFont.GetCharacterInfo((char)ch, out mTempChar, finalSize, fontStyle))
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
            { return(mTempChar.width * fontScale * pixelDensity); }
#else
            { return(mTempChar.advance * fontScale * pixelDensity); }
#endif
        }
#endif
        return(0f);
    }
示例#6
0
    /// <summary>
    /// Get the width of the specified glyph. Returns zero if the glyph could not be retrieved.
    /// </summary>

    static public float GetGlyphWidth(int ch, int prev)
    {
        if (bitmapFont != null)
        {
            BMGlyph bmg = bitmapFont.bmFont.GetGlyph(ch);

            if (bmg != null)
            {
                return((prev != 0) ? bmg.advance + bmg.GetKerning(prev) / pixelDensity : bmg.advance);
            }
        }
#if DYNAMIC_FONT
        else if (dynamicFont != null)
        {
            if (dynamicFont.GetCharacterInfo((char)ch, out mTempChar, finalSize, style))
            {
                return(Mathf.Round(mTempChar.width / pixelDensity));
            }
        }
#endif
        return(0f);
    }
示例#7
0
    /// <summary>
    /// Get the printed size of the specified string. The returned value is in local coordinates. Multiply by transform's scale to get pixels.
    /// </summary>

    public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.CalculatePrintedSize(text, encoding, symbolStyle));
        }

        Vector2 v = Vector2.zero;

        if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text))
        {
            if (encoding)
            {
                text = NGUITools.StripSymbols(text);
            }

            int length     = text.Length;
            int maxX       = 0;
            int x          = 0;
            int y          = 0;
            int prev       = 0;
            int lineHeight = (mFont.charSize + mSpacingY);

            for (int i = 0; i < length; ++i)
            {
                char c = text[i];

                // Start a new line
                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }
                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                // Skip invalid characters
                if (c < ' ')
                {
                    prev = 0; continue;
                }

                // See if there is a symbol matching this text
                BMSymbol symbol = (encoding && symbolStyle != SymbolStyle.None) ? mFont.MatchSymbol(text, i, length) : null;

                if (symbol == null)
                {
                    // Get the glyph for this character
                    BMGlyph glyph = mFont.GetGlyph(c);

                    if (glyph != null)
                    {
                        x   += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance);
                        prev = c;
                    }
                }
                else
                {
                    // Symbol found -- use it
                    x   += mSpacingX + symbol.width;
                    i   += symbol.length - 1;
                    prev = 0;
                }
            }

            // Convert from pixel coordinates to local coordinates
            float scale = (mFont.charSize > 0) ? 1f / mFont.charSize : 1f;
            v.x = scale * ((x > maxX) ? x : maxX);
            v.y = scale * (y + lineHeight);
        }
        return(v);
    }
示例#8
0
    /// <summary>
    /// Get the printed size of the specified string. The returned value is in pixels.
    /// </summary>

    public Vector2 CalculatePrintedSize(string text)
    {
        if (mReplacement != null)
        {
            return(mReplacement.CalculatePrintedSize(text));
        }

#if DYNAMIC_FONT
        if (isDynamic)
        {
            NGUIText.current.size  = mDynamicFontSize;
            NGUIText.current.style = mDynamicFontStyle;
            return(NGUIText.CalculatePrintedSize(mDynamicFont, text));
        }
#endif
        Vector2 v = Vector2.zero;

        if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text))
        {
            if (NGUIText.current.encoding)
            {
                text = NGUIText.StripSymbols(text);
            }

            int  x          = 0;
            int  y          = 0;
            int  prev       = 0;
            int  maxX       = 0;
            int  length     = text.Length;
            int  lineHeight = NGUIText.current.size + NGUIText.current.spacingY;
            bool useSymbols = NGUIText.current.encoding && NGUIText.current.symbolStyle != NGUIText.SymbolStyle.None && hasSymbols;

            for (int i = 0; i < length; ++i)
            {
                char c = text[i];

                // Start a new line
                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }
                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                // Skip invalid characters
                if (c < ' ')
                {
                    prev = 0; continue;
                }

                // See if there is a symbol matching this text
                BMSymbol symbol = useSymbols ? MatchSymbol(text, i, length) : null;

                if (symbol == null)
                {
                    // Get the glyph for this character
                    BMGlyph glyph = mFont.GetGlyph(c);

                    if (glyph != null)
                    {
                        x   += NGUIText.current.spacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance);
                        prev = c;
                    }
                }
                else
                {
                    // Symbol found -- use it
                    x   += NGUIText.current.spacingX + symbol.width;
                    i   += symbol.length - 1;
                    prev = 0;
                }
            }

            // Padding is always between characters, so it's one less than the number of characters
            v.x = ((x > maxX) ? x : maxX);
            v.y = (y + NGUIText.current.size);
        }
        return(v);
    }
示例#9
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));
    }
示例#10
0
    public string WrapText(string text, float maxWidth, int maxLineCount, bool encoding, SymbolStyle symbolStyle)
    {
        if (this.mReplacement != null)
        {
            return(this.mReplacement.WrapText(text, maxWidth, maxLineCount, encoding, symbolStyle));
        }
        int num = Mathf.RoundToInt(maxWidth * this.size);

        if (num < 1)
        {
            return(text);
        }
        StringBuilder s            = new StringBuilder();
        int           length       = text.Length;
        int           num3         = num;
        int           previousChar = 0;
        int           startIndex   = 0;
        int           offset       = 0;
        bool          flag         = true;
        bool          flag2        = maxLineCount != 1;
        int           num7         = 1;
        bool          flag3        = (encoding && (symbolStyle != SymbolStyle.None)) && this.hasSymbols;
        bool          isDynamic    = this.isDynamic;

        if (isDynamic)
        {
            this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
            this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
            this.mDynamicFont.textureRebuildCallback = null;
        }
        while (offset < length)
        {
            char ch = text[offset];
            if (ch == '\n')
            {
                if (!flag2 || (num7 == maxLineCount))
                {
                    break;
                }
                num3 = num;
                if (startIndex < offset)
                {
                    s.Append(text.Substring(startIndex, (offset - startIndex) + 1));
                }
                else
                {
                    s.Append(ch);
                }
                flag = true;
                num7++;
                startIndex   = offset + 1;
                previousChar = 0;
                goto Label_03E7;
            }
            if (((ch == ' ') && (previousChar != 0x20)) && (startIndex < offset))
            {
                s.Append(text.Substring(startIndex, (offset - startIndex) + 1));
                flag         = false;
                startIndex   = offset + 1;
                previousChar = ch;
            }
            if ((encoding && (ch == '[')) && ((offset + 2) < length))
            {
                if ((text[offset + 1] == '-') && (text[offset + 2] == ']'))
                {
                    offset += 2;
                    goto Label_03E7;
                }
                if ((((offset + 7) < length) && (text[offset + 7] == ']')) && (NGUITools.EncodeColor(NGUITools.ParseColor(text, offset + 1)) == text.Substring(offset + 1, 6).ToUpper()))
                {
                    offset += 7;
                    goto Label_03E7;
                }
            }
            BMSymbol symbol    = !flag3 ? null : this.MatchSymbol(text, offset, length);
            int      mSpacingX = this.mSpacingX;
            if (!isDynamic)
            {
                if (symbol != null)
                {
                    mSpacingX += symbol.advance;
                }
                else
                {
                    BMGlyph glyph = (symbol != null) ? null : this.mFont.GetGlyph(ch);
                    if (glyph == null)
                    {
                        goto Label_03E7;
                    }
                    mSpacingX += (previousChar == 0) ? glyph.advance : (glyph.advance + glyph.GetKerning(previousChar));
                }
            }
            else if (this.mDynamicFont.GetCharacterInfo(ch, out mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
            {
                mSpacingX += Mathf.RoundToInt(mChar.width);
            }
            num3 -= mSpacingX;
            if (num3 < 0)
            {
                if ((flag || !flag2) || (num7 == maxLineCount))
                {
                    s.Append(text.Substring(startIndex, Mathf.Max(0, offset - startIndex)));
                    if (!flag2 || (num7 == maxLineCount))
                    {
                        startIndex = offset;
                        break;
                    }
                    EndLine(ref s);
                    flag = true;
                    num7++;
                    if (ch == ' ')
                    {
                        startIndex = offset + 1;
                        num3       = num;
                    }
                    else
                    {
                        startIndex = offset;
                        num3       = num - mSpacingX;
                    }
                    previousChar = 0;
                    goto Label_03C8;
                }
                while ((startIndex < length) && (text[startIndex] == ' '))
                {
                    startIndex++;
                }
                flag         = true;
                num3         = num;
                offset       = startIndex - 1;
                previousChar = 0;
                if (!flag2 || (num7 == maxLineCount))
                {
                    break;
                }
                num7++;
                EndLine(ref s);
                goto Label_03E7;
            }
            previousChar = ch;
Label_03C8:
            if (!isDynamic && (symbol != null))
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }
Label_03E7:
            offset++;
        }
        if (startIndex < offset)
        {
            s.Append(text.Substring(startIndex, offset - startIndex));
        }
        return(s.ToString());
    }
示例#11
0
    /// <summary>
    /// Get the printed size of the specified string. The returned value is in local coordinates. Multiply by transform's scale to get pixels.
    /// </summary>

    public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.CalculatePrintedSize(text, encoding, symbolStyle));
        }

        Vector2 v       = Vector2.zero;
        bool    dynamic = isDynamic;

#if DYNAMIC_FONT
        if (dynamic || (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text)))
#else
        if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text))
#endif
        {
            if (encoding)
            {
                text = NGUITools.StripSymbols(text);
            }
#if DYNAMIC_FONT
            if (mDynamicFont != null)
            {
                mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize);
            }
#endif
            int  length     = text.Length;
            int  maxX       = 0;
            int  x          = 0;
            int  y          = 0;
            int  prev       = 0;
            int  fs         = size;
            int  lineHeight = (fs + mSpacingY);
            bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols;

            for (int i = 0; i < length; ++i)
            {
                char c = text[i];

                // Start a new line
                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }
                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                // Skip invalid characters
                if (c < ' ')
                {
                    prev = 0; continue;
                }

                if (!dynamic)
                {
                    // See if there is a symbol matching this text
                    BMSymbol symbol = useSymbols ? MatchSymbol(text, i, length) : null;

                    if (symbol == null)
                    {
                        // Get the glyph for this character
                        BMGlyph glyph = mFont.GetGlyph(c);

                        if (glyph != null)
                        {
                            x   += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance);
                            prev = c;
                        }
                    }
                    else
                    {
                        // Symbol found -- use it
                        x   += mSpacingX + symbol.width;
                        i   += symbol.length - 1;
                        prev = 0;
                    }
                }
#if DYNAMIC_FONT
                else
                {
                    if (mDynamicFont.GetCharacterInfo(c, out mChar, mDynamicFontSize, mDynamicFontStyle))
                    {
                        x += (int)(mSpacingX + mChar.width);
                    }
                }
#endif
            }

            // Convert from pixel coordinates to local coordinates
            float scale = (fs > 0) ? 1f / fs : 1f;
            v.x = scale * ((x > maxX) ? x : maxX);
            v.y = scale * (y + lineHeight);
        }
        return(v);
    }
示例#12
0
    /// <summary>
    /// Text wrapping functionality. The 'width' and 'height' should be in pixels.
    /// </summary>

    public bool WrapText(string text, int size, out string finalText, int width, int height, int maxLines, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, size, out finalText, width, height, maxLines, encoding, symbolStyle));
        }

#if DYNAMIC_FONT
        if (isDynamic)
        {
            return(NGUIText.WrapText(text, mDynamicFont, size, mDynamicFontStyle, width, height, maxLines, encoding, out finalText));
        }
#endif
        if (width < 1 || height < 1)
        {
            finalText = "";
            return(false);
        }

        int maxLineCount = (maxLines > 0) ? maxLines : 999999;
        maxLineCount = Mathf.Min(maxLineCount, height / size);

        if (maxLineCount == 0)
        {
            finalText = "";
            return(false);
        }

        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = width;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;
        bool          multiline      = (maxLines != 1);
        int           lineCount      = 1;
        bool          useSymbols     = encoding && symbolStyle != SymbolStyle.None && hasSymbols;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline || lineCount == maxLineCount)
                {
                    break;
                }
                remainingWidth = width;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty = true;
                ++lineCount;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (NGUIText.ParseSymbol(text, ref offset))
            {
                --offset; continue;
            }

            // See if there is a symbol matching this text
            BMSymbol symbol = useSymbols ? MatchSymbol(text, offset, 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 = (symbol == null) ? mFont.GetGlyph(ch) : null;

                if (glyph != null)
                {
                    glyphWidth += (previousChar != 0) ?
                                  glyph.advance + glyph.GetKerning(previousChar) : glyph.advance;
                }

                else
                {
                    continue;
                }
            }

            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;

            // Doesn't fit?
            if (remainingWidth < 0)
            {
                // Can't start a new line
                if (lineIsEmpty || !multiline || lineCount == maxLineCount)
                {
                    // This is the first word on the line -- add it up to the character that fits
                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                    if (!multiline || lineCount == maxLineCount)
                    {
                        start = offset;
                        break;
                    }
                    NGUIText.EndLine(ref sb);

                    // Start a brand-new line
                    lineIsEmpty = true;
                    ++lineCount;

                    if (ch == ' ')
                    {
                        start          = offset + 1;
                        remainingWidth = width;
                    }
                    else
                    {
                        start          = offset;
                        remainingWidth = width - glyphWidth;
                    }
                    previousChar = 0;
                }
                else
                {
                    // Skip all spaces before the word
                    while (start < textLength && text[start] == ' ')
                    {
                        ++start;
                    }

                    // Revert the position to the beginning of the word and reset the line
                    lineIsEmpty    = true;
                    remainingWidth = width;
                    offset         = start - 1;
                    previousChar   = 0;

                    if (!multiline || lineCount == maxLineCount)
                    {
                        break;
                    }
                    ++lineCount;
                    NGUIText.EndLine(ref sb);
                    continue;
                }
            }
            else
            {
                previousChar = ch;
            }

            // Advance the offset past the symbol
            if (symbol != null)
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        finalText = sb.ToString();
        return(!multiline || offset == textLength || (maxLines > 0 && lineCount <= maxLines));
    }
示例#13
0
    /// <summary>
    /// Print the specified text into the buffers.
    /// Note: 'lineWidth' parameter should be in pixels.
    /// </summary>

    public void Print(string text, int size, Color32 color, BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols,
                      bool encoding, SymbolStyle symbolStyle, TextAlignment alignment, int lineWidth, bool premultiply)
    {
        if (mReplacement != null)
        {
            mReplacement.Print(text, size, color, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth, premultiply);
        }
        else if (text != null)
        {
            if (!isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
                return;
            }

#if DYNAMIC_FONT
            if (isDynamic)
            {
                NGUIText.Print(text, dynamicFont, size, mDynamicFontStyle, color, encoding,
                               alignment, lineWidth, premultiply, verts, uvs, cols);
                return;
            }
#endif
            mColors.Clear();
            mColors.Add(color);

            int     fs = size;
            int     indexOffset = verts.size;
            int     maxX = 0;
            int     x = 0;
            int     y = 0;
            int     prev = 0;
            int     lineHeight = (fs + mSpacingY);
            Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
            Vector2 u0 = Vector2.zero, u1 = Vector2.zero;

            float invX = uvRect.width / mFont.texWidth;
            float invY = mUVRect.height / mFont.texHeight;

            int  textLength = text.Length;
            bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols && sprite != null;

            for (int i = 0; i < textLength; ++i)
            {
                char c = text[i];

                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }

                    if (alignment != TextAlignment.Left)
                    {
                        NGUIText.Align(verts, indexOffset, alignment, x, lineWidth);
                        indexOffset = verts.size;
                    }

                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                if (c < ' ')
                {
                    prev = 0;
                    continue;
                }

                if (encoding && NGUIText.ParseSymbol(text, ref i, mColors, premultiply))
                {
                    color = mColors[mColors.size - 1];
                    --i;
                    continue;
                }

                // See if there is a symbol matching this text
                BMSymbol symbol = useSymbols ? MatchSymbol(text, i, textLength) : null;

                if (symbol == null)
                {
                    BMGlyph glyph = mFont.GetGlyph(c);
                    if (glyph == null)
                    {
                        continue;
                    }

                    if (prev != 0)
                    {
                        x += glyph.GetKerning(prev);
                    }

                    if (c == ' ')
                    {
                        x   += mSpacingX + glyph.advance;
                        prev = c;
                        continue;
                    }

                    v0.x = (x + glyph.offsetX);
                    v0.y = -(y + glyph.offsetY);

                    v1.x = v0.x + glyph.width;
                    v1.y = v0.y - glyph.height;

                    u0.x = mUVRect.xMin + invX * glyph.x;
                    u0.y = mUVRect.yMax - invY * glyph.y;

                    u1.x = u0.x + invX * glyph.width;
                    u1.y = u0.y - invY * glyph.height;

                    x   += mSpacingX + glyph.advance;
                    prev = c;

                    if (glyph.channel == 0 || glyph.channel == 15)
                    {
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(color);
                        }
                    }
                    else
                    {
                        // Packed fonts come as alpha masks in each of the RGBA channels.
                        // In order to use it we need to use a special shader.
                        //
                        // Limitations:
                        // - Effects (drop shadow, outline) will not work.
                        // - Should not be a part of the atlas (eastern fonts rarely are anyway).
                        // - Lower color precision

                        Color col = color;

                        col *= 0.49f;

                        switch (glyph.channel)
                        {
                        case 1: col.b += 0.51f; break;

                        case 2: col.g += 0.51f; break;

                        case 4: col.r += 0.51f; break;

                        case 8: col.a += 0.51f; break;
                        }

                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(col);
                        }
                    }
                }
                else
                {
                    v0.x = (x + symbol.offsetX);
                    v0.y = -(y + symbol.offsetY);

                    v1.x = v0.x + symbol.width;
                    v1.y = v0.y - symbol.height;

                    Rect uv = symbol.uvRect;

                    u0.x = uv.xMin;
                    u0.y = uv.yMax;
                    u1.x = uv.xMax;
                    u1.y = uv.yMin;

                    x   += mSpacingX + symbol.advance;
                    i   += symbol.length - 1;
                    prev = 0;

                    if (symbolStyle == SymbolStyle.Colored)
                    {
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(color);
                        }
                    }
                    else
                    {
                        Color32 col = Color.white;
                        col.a = color.a;
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(col);
                        }
                    }
                }

                verts.Add(new Vector3(v1.x, v0.y));
                verts.Add(new Vector3(v1.x, v1.y));
                verts.Add(new Vector3(v0.x, v1.y));
                verts.Add(new Vector3(v0.x, v0.y));

                uvs.Add(new Vector2(u1.x, u0.y));
                uvs.Add(new Vector2(u1.x, u1.y));
                uvs.Add(new Vector2(u0.x, u1.y));
                uvs.Add(new Vector2(u0.x, u0.y));
            }

            if (alignment != TextAlignment.Left && indexOffset < verts.size)
            {
                NGUIText.Align(verts, indexOffset, alignment, x, lineWidth);
                indexOffset = verts.size;
            }
        }
    }
示例#14
0
    /// <summary>
    /// Text wrapping functionality. The 'maxWidth' should be in local coordinates (take pixels and divide them by transform's scale).
    /// </summary>

    public string WrapText(string text, float maxWidth, int maxLineCount, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, maxWidth, maxLineCount, encoding, symbolStyle));
        }

        // Width of the line in pixels
        int lineWidth = Mathf.RoundToInt(maxWidth * size);

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

        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = lineWidth;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;
        bool          multiline      = (maxLineCount != 1);
        int           lineCount      = 1;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline || lineCount == maxLineCount)
                {
                    break;
                }
                remainingWidth = lineWidth;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty = true;
                ++lineCount;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (encoding && ch == '[')
            {
                if (offset + 2 < textLength)
                {
                    if (text[offset + 1] == '-' && text[offset + 2] == ']')
                    {
                        offset += 2;
                        continue;
                    }
                    else if (offset + 7 < textLength && text[offset + 7] == ']')
                    {
                        if (NGUITools.EncodeColor(NGUITools.ParseColor(text, offset + 1)) == text.Substring(offset + 1, 6).ToUpper())
                        {
                            offset += 7;
                            continue;
                        }
                    }
                }
            }

            // See if there is a symbol matching this text
            BMSymbol symbol = (encoding && symbolStyle != SymbolStyle.None) ? mFont.MatchSymbol(text, offset, textLength) : null;

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

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

            if (symbol != null)
            {
                glyphWidth += symbol.width;
            }
            else if (glyph != null)
            {
                glyphWidth += (previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance;
            }
            else
            {
                continue;
            }

            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;

            // Doesn't fit?
            if (remainingWidth < 0)
            {
                // Can't start a new line
                if (lineIsEmpty || !multiline || lineCount == maxLineCount)
                {
                    // This is the first word on the line -- add it up to the character that fits
                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                    if (!multiline || lineCount == maxLineCount)
                    {
                        start = offset;
                        break;
                    }
                    EndLine(ref sb);

                    // Start a brand-new line
                    lineIsEmpty = true;
                    ++lineCount;

                    if (ch == ' ')
                    {
                        start          = offset + 1;
                        remainingWidth = lineWidth;
                    }
                    else
                    {
                        start          = offset;
                        remainingWidth = lineWidth - glyphWidth;
                    }
                    previousChar = 0;
                }
                else
                {
                    // Skip all spaces before the word
                    while (start < textLength && text[start] == ' ')
                    {
                        ++start;
                    }

                    // Revert the position to the beginning of the word and reset the line
                    lineIsEmpty    = true;
                    remainingWidth = lineWidth;
                    offset         = start - 1;
                    previousChar   = 0;
                    if (!multiline || lineCount == maxLineCount)
                    {
                        break;
                    }
                    ++lineCount;
                    EndLine(ref sb);
                    continue;
                }
            }
            else
            {
                previousChar = ch;
            }

            // Advance the offset past the symbol
            if (symbol != null)
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        return(sb.ToString());
    }
示例#15
0
    // Token: 0x06000336 RID: 822 RVA: 0x000255F0 File Offset: 0x000237F0
    public Vector2 CalculatePrintedSize(string text, bool encoding, UIFont.SymbolStyle symbolStyle)
    {
        if (this.mReplacement != null)
        {
            return(this.mReplacement.CalculatePrintedSize(text, encoding, symbolStyle));
        }
        Vector2 zero      = Vector2.zero;
        bool    isDynamic = this.isDynamic;

        if (isDynamic || (this.mFont != null && this.mFont.isValid && !string.IsNullOrEmpty(text)))
        {
            if (encoding)
            {
                text = NGUITools.StripSymbols(text);
            }
            if (isDynamic)
            {
                this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
                this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
                this.mDynamicFont.textureRebuildCallback = null;
            }
            int  length = text.Length;
            int  num    = 0;
            int  num2   = 0;
            int  num3   = 0;
            int  num4   = 0;
            int  size   = this.size;
            int  num5   = size + this.mSpacingY;
            bool flag   = encoding && symbolStyle != UIFont.SymbolStyle.None && this.hasSymbols;
            for (int i = 0; i < length; i++)
            {
                char c = text[i];
                if (c == '\n')
                {
                    if (num2 > num)
                    {
                        num = num2;
                    }
                    num2  = 0;
                    num3 += num5;
                    num4  = 0;
                }
                else if (c < ' ')
                {
                    num4 = 0;
                }
                else if (!isDynamic)
                {
                    BMSymbol bmsymbol = (!flag) ? null : this.MatchSymbol(text, i, length);
                    if (bmsymbol == null)
                    {
                        BMGlyph glyph = this.mFont.GetGlyph((int)c);
                        if (glyph != null)
                        {
                            num2 += this.mSpacingX + ((num4 == 0) ? glyph.advance : (glyph.advance + glyph.GetKerning(num4)));
                            num4  = (int)c;
                        }
                    }
                    else
                    {
                        num2 += this.mSpacingX + bmsymbol.width;
                        i    += bmsymbol.length - 1;
                        num4  = 0;
                    }
                }
                else if (this.mDynamicFont.GetCharacterInfo(c, out UIFont.mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
                {
                    num2 += (int)((float)this.mSpacingX + UIFont.mChar.width);
                }
            }
            float num6 = (size <= 0) ? 1f : (1f / (float)size);
            zero.x = num6 * (float)((num2 <= num) ? num : num2);
            zero.y = num6 * (float)(num3 + num5);
        }
        return(zero);
    }
示例#16
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));
    }
示例#17
0
    /// <summary>
    /// Get the specified glyph.
    /// </summary>

    static public GlyphInfo GetGlyph(int ch, int prev)
    {
        if (bitmapFont != null)
        {
            BMGlyph bmg = bitmapFont.bmFont.GetGlyph(ch);

            if (bmg != null)
            {
                int kern = (prev != 0) ? bmg.GetKerning(prev) : 0;
                glyph.v0.x = (prev != 0) ? bmg.offsetX + kern : bmg.offsetX;
                glyph.v1.y = -bmg.offsetY;

                glyph.v1.x = glyph.v0.x + bmg.width;
                glyph.v0.y = glyph.v1.y - bmg.height;

                glyph.u0.x = bmg.x;
                glyph.u0.y = bmg.y + bmg.height;

                glyph.u1.x = bmg.x + bmg.width;
                glyph.u1.y = bmg.y;

                glyph.v0 /= pixelDensity;
                glyph.v1 /= pixelDensity;

                glyph.advance    = (bmg.advance + kern) / pixelDensity;
                glyph.channel    = bmg.channel;
                glyph.rotatedUVs = false;
                return(glyph);
            }
        }
#if DYNAMIC_FONT
        else if (dynamicFont != null)
        {
            if (dynamicFont.GetCharacterInfo((char)ch, out mTempChar, finalSize, style))
            {
                glyph.v0.x = mTempChar.vert.xMin;
                glyph.v1.x = glyph.v0.x + mTempChar.vert.width;

                glyph.v0.y = mTempChar.vert.yMax - baseline;
                glyph.v1.y = glyph.v0.y - mTempChar.vert.height;

                glyph.u0.x = mTempChar.uv.xMin;
                glyph.u0.y = mTempChar.uv.yMin;

                glyph.u1.x = mTempChar.uv.xMax;
                glyph.u1.y = mTempChar.uv.yMax;

                glyph.v0.x = Mathf.Round(glyph.v0.x) / pixelDensity;
                glyph.v0.y = Mathf.Round(glyph.v0.y) / pixelDensity;
                glyph.v1.x = Mathf.Round(glyph.v1.x) / pixelDensity;
                glyph.v1.y = Mathf.Round(glyph.v1.y) / pixelDensity;

                glyph.advance    = Mathf.Round(mTempChar.width) / pixelDensity;
                glyph.channel    = 0;
                glyph.rotatedUVs = mTempChar.flipped;
                return(glyph);
            }
        }
#endif
        return(null);
    }
    /// <summary>
    /// Get the specified glyph.
    /// </summary>

    static public GlyphInfo GetGlyph(int ch, int prev, float fontScale = 1f)
    {
#if NGUI_CUSTOM
        if (false)
#else
        if (bitmapFont != null)
#endif
        {
            bool thinSpace = false;

            if (ch == '\u2009')
            {
                thinSpace = true;
                ch        = ' ';
            }

            BMGlyph bmg = bitmapFont.bmFont.GetGlyph(ch);

            if (bmg != null)
            {
                int kern = (prev != 0) ? bmg.GetKerning(prev) : 0;
                glyph.v0.x = (prev != 0) ? bmg.offsetX + kern : bmg.offsetX;
                glyph.v1.y = -bmg.offsetY;

                glyph.v1.x = glyph.v0.x + bmg.width;
                glyph.v0.y = glyph.v1.y - bmg.height;

                glyph.u0.x = bmg.x;
                glyph.u0.y = bmg.y + bmg.height;

                glyph.u2.x = bmg.x + bmg.width;
                glyph.u2.y = bmg.y;

                glyph.u1.x = glyph.u0.x;
                glyph.u1.y = glyph.u2.y;

                glyph.u3.x = glyph.u2.x;
                glyph.u3.y = glyph.u0.y;

                int adv = bmg.advance;
                if (thinSpace)
                {
                    adv >>= 1;
                }
                glyph.advance = adv + kern;
                glyph.channel = bmg.channel;

                if (fontScale != 1f)
                {
                    glyph.v0      *= fontScale;
                    glyph.v1      *= fontScale;
                    glyph.advance *= fontScale;
                }
                return(glyph);
            }
        }
#if DYNAMIC_FONT
        else if (dynamicFont != null)
        {
            if (dynamicFont.GetCharacterInfo((char)ch, out mTempChar, finalSize, fontStyle))
            {
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
                glyph.v0.x = mTempChar.vert.xMin;
                glyph.v1.x = glyph.v0.x + mTempChar.vert.width;

                glyph.v0.y = mTempChar.vert.yMax - baseline;
                glyph.v1.y = glyph.v0.y - mTempChar.vert.height;

                glyph.u0.x = mTempChar.uv.xMin;
                glyph.u0.y = mTempChar.uv.yMin;

                glyph.u2.x = mTempChar.uv.xMax;
                glyph.u2.y = mTempChar.uv.yMax;

                if (mTempChar.flipped)
                {
                    glyph.u1 = new Vector2(glyph.u2.x, glyph.u0.y);
                    glyph.u3 = new Vector2(glyph.u0.x, glyph.u2.y);
                }
                else
                {
                    glyph.u1 = new Vector2(glyph.u0.x, glyph.u2.y);
                    glyph.u3 = new Vector2(glyph.u2.x, glyph.u0.y);
                }

                glyph.advance = mTempChar.width;
                glyph.channel = 0;
#else
                glyph.v0.x = mTempChar.minX;
                glyph.v1.x = mTempChar.maxX;

                glyph.v0.y = mTempChar.maxY - baseline;
                glyph.v1.y = mTempChar.minY - baseline;

                glyph.u0 = mTempChar.uvTopLeft;
                glyph.u1 = mTempChar.uvBottomLeft;
                glyph.u2 = mTempChar.uvBottomRight;
                glyph.u3 = mTempChar.uvTopRight;

                glyph.advance = mTempChar.advance;
                glyph.channel = 0;
#endif
                glyph.v0.x = Mathf.Round(glyph.v0.x);
                glyph.v0.y = Mathf.Round(glyph.v0.y);
                glyph.v1.x = Mathf.Round(glyph.v1.x);
                glyph.v1.y = Mathf.Round(glyph.v1.y);

                float pd = fontScale * pixelDensity;

                if (pd != 1f)
                {
                    glyph.v0      *= pd;
                    glyph.v1      *= pd;
                    glyph.advance *= pd;
                }
                return(glyph);
            }
        }
#endif
        return(null);
    }
示例#19
0
    public void Print(string text, Color32 color, BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols,
                      bool encoding, SymbolStyle symbolStyle, Alignment alignment, int lineWidth)
#endif
    {
        if (mReplacement != null)
        {
            mReplacement.Print(text, color, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth);
        }
        else if (mFont != null && text != null)
        {
            if (!mFont.isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
                return;
            }

            mColors.Clear();
            mColors.Add(color);

            Vector2 scale = mFont.charSize > 0 ? new Vector2(1f / mFont.charSize, 1f / mFont.charSize) : Vector2.one;

            int     indexOffset = verts.size;
            int     maxX = 0;
            int     x = 0;
            int     y = 0;
            int     prev = 0;
            int     lineHeight = (mFont.charSize + mSpacingY);
            Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
            Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
            float   invX       = uvRect.width / mFont.texWidth;
            float   invY       = mUVRect.height / mFont.texHeight;
            int     textLength = text.Length;

            for (int i = 0; i < textLength; ++i)
            {
                char c = text[i];

                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }

                    if (alignment != Alignment.Left)
                    {
                        Align(verts, indexOffset, alignment, x, lineWidth);
                        indexOffset = verts.size;
                    }

                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                if (c < ' ')
                {
                    prev = 0;
                    continue;
                }

                if (encoding && c == '[')
                {
                    int retVal = NGUITools.ParseSymbol(text, i, mColors);

                    if (retVal > 0)
                    {
                        color = mColors[mColors.Count - 1];
                        i    += retVal - 1;
                        continue;
                    }
                }

                // See if there is a symbol matching this text
                BMSymbol symbol = (encoding && symbolStyle != SymbolStyle.None) ? mFont.MatchSymbol(text, i, textLength) : null;

                if (symbol == null)
                {
                    BMGlyph glyph = mFont.GetGlyph(c);
                    if (glyph == null)
                    {
                        continue;
                    }

                    if (prev != 0)
                    {
                        x += glyph.GetKerning(prev);
                    }

                    if (c == ' ')
                    {
                        x   += mSpacingX + glyph.advance;
                        prev = c;
                        continue;
                    }

                    v0.x = scale.x * (x + glyph.offsetX);
                    v0.y = -scale.y * (y + glyph.offsetY);

                    v1.x = v0.x + scale.x * glyph.width;
                    v1.y = v0.y - scale.y * glyph.height;

                    u0.x = mUVRect.xMin + invX * glyph.x;
                    u0.y = mUVRect.yMax - invY * glyph.y;

                    u1.x = u0.x + invX * glyph.width;
                    u1.y = u0.y - invY * glyph.height;

                    x   += mSpacingX + glyph.advance;
                    prev = c;

                    if (glyph.channel == 0 || glyph.channel == 15)
                    {
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(color);
                        }
                    }
                    else
                    {
                        // Packed fonts come as alpha masks in each of the RGBA channels.
                        // In order to use it we need to use a special shader.
                        //
                        // Limitations:
                        // - Effects (drop shadow, outline) will not work.
                        // - Should not be a part of the atlas (eastern fonts rarely are anyway).
                        // - Lower color precision

                        Color col = color;

                        col *= 0.49f;

                        switch (glyph.channel)
                        {
                        case 1: col.b += 0.51f; break;

                        case 2: col.g += 0.51f; break;

                        case 4: col.r += 0.51f; break;

                        case 8: col.a += 0.51f; break;
                        }

                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(col);
                        }
                    }
                }
                else
                {
                    v0.x = scale.x * x;
                    v0.y = -scale.y * y;

                    v1.x = v0.x + scale.x * symbol.width;
                    v1.y = v0.y - scale.y * symbol.height;

                    u0.x = mUVRect.xMin + invX * symbol.x;
                    u0.y = mUVRect.yMax - invY * symbol.y;

                    u1.x = u0.x + invX * symbol.width;
                    u1.y = u0.y - invY * symbol.height;

                    x   += mSpacingX + symbol.width;
                    i   += symbol.length - 1;
                    prev = 0;

                    if (symbolStyle == SymbolStyle.Colored)
                    {
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(color);
                        }
                    }
                    else
                    {
                        Color32 col = Color.white;
                        col.a = color.a;
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(col);
                        }
                    }
                }

                verts.Add(new Vector3(v1.x, v0.y));
                verts.Add(new Vector3(v1.x, v1.y));
                verts.Add(new Vector3(v0.x, v1.y));
                verts.Add(new Vector3(v0.x, v0.y));

                uvs.Add(new Vector2(u1.x, u0.y));
                uvs.Add(new Vector2(u1.x, u1.y));
                uvs.Add(new Vector2(u0.x, u1.y));
                uvs.Add(new Vector2(u0.x, u0.y));
            }

            if (alignment != Alignment.Left && indexOffset < verts.size)
            {
                Align(verts, indexOffset, alignment, x, lineWidth);
                indexOffset = verts.size;
            }
        }
    }
示例#20
0
    // Token: 0x06000339 RID: 825 RVA: 0x00025A5C File Offset: 0x00023C5C
    public string WrapText(string text, float maxWidth, int maxLineCount, bool encoding, UIFont.SymbolStyle symbolStyle)
    {
        if (this.mReplacement != null)
        {
            return(this.mReplacement.WrapText(text, maxWidth, maxLineCount, encoding, symbolStyle));
        }
        int num = Mathf.RoundToInt(maxWidth * (float)this.size);

        if (num < 1)
        {
            return(text);
        }
        StringBuilder stringBuilder = new StringBuilder();
        int           length        = text.Length;
        int           num2          = num;
        int           num3          = 0;
        int           num4          = 0;
        int           i             = 0;
        bool          flag          = true;
        bool          flag2         = maxLineCount != 1;
        int           num5          = 1;
        bool          flag3         = encoding && symbolStyle != UIFont.SymbolStyle.None && this.hasSymbols;
        bool          isDynamic     = this.isDynamic;

        if (isDynamic)
        {
            this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
            this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
            this.mDynamicFont.textureRebuildCallback = null;
        }
        while (i < length)
        {
            char c = text[i];
            if (c == '\n')
            {
                if (!flag2 || num5 == maxLineCount)
                {
                    break;
                }
                num2 = num;
                if (num4 < i)
                {
                    stringBuilder.Append(text.Substring(num4, i - num4 + 1));
                }
                else
                {
                    stringBuilder.Append(c);
                }
                flag = true;
                num5++;
                num4 = i + 1;
                num3 = 0;
            }
            else
            {
                if (c == ' ' && num3 != 32 && num4 < i)
                {
                    stringBuilder.Append(text.Substring(num4, i - num4 + 1));
                    flag = false;
                    num4 = i + 1;
                    num3 = (int)c;
                }
                if (encoding && c == '[' && i + 2 < length)
                {
                    if (text[i + 1] == '-' && text[i + 2] == ']')
                    {
                        i += 2;
                        goto IL_3E7;
                    }
                    if (i + 7 < length && text[i + 7] == ']' && NGUITools.EncodeColor(NGUITools.ParseColor(text, i + 1)) == text.Substring(i + 1, 6).ToUpper())
                    {
                        i += 7;
                        goto IL_3E7;
                    }
                }
                BMSymbol bmsymbol = (!flag3) ? null : this.MatchSymbol(text, i, length);
                int      num6     = this.mSpacingX;
                if (!isDynamic)
                {
                    if (bmsymbol != null)
                    {
                        num6 += bmsymbol.advance;
                    }
                    else
                    {
                        BMGlyph bmglyph = (bmsymbol != null) ? null : this.mFont.GetGlyph((int)c);
                        if (bmglyph == null)
                        {
                            goto IL_3E7;
                        }
                        num6 += ((num3 == 0) ? bmglyph.advance : (bmglyph.advance + bmglyph.GetKerning(num3)));
                    }
                }
                else if (this.mDynamicFont.GetCharacterInfo(c, out UIFont.mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
                {
                    num6 += Mathf.RoundToInt(UIFont.mChar.width);
                }
                num2 -= num6;
                if (num2 < 0)
                {
                    if (flag || !flag2 || num5 == maxLineCount)
                    {
                        stringBuilder.Append(text.Substring(num4, Mathf.Max(0, i - num4)));
                        if (!flag2 || num5 == maxLineCount)
                        {
                            num4 = i;
                            break;
                        }
                        UIFont.EndLine(ref stringBuilder);
                        flag = true;
                        num5++;
                        if (c == ' ')
                        {
                            num4 = i + 1;
                            num2 = num;
                        }
                        else
                        {
                            num4 = i;
                            num2 = num - num6;
                        }
                        num3 = 0;
                    }
                    else
                    {
                        while (num4 < length && text[num4] == ' ')
                        {
                            num4++;
                        }
                        flag = true;
                        num2 = num;
                        i    = num4 - 1;
                        num3 = 0;
                        if (!flag2 || num5 == maxLineCount)
                        {
                            break;
                        }
                        num5++;
                        UIFont.EndLine(ref stringBuilder);
                        goto IL_3E7;
                    }
                }
                else
                {
                    num3 = (int)c;
                }
                if (!isDynamic && bmsymbol != null)
                {
                    i   += bmsymbol.length - 1;
                    num3 = 0;
                }
            }
IL_3E7:
            i++;
        }
        if (num4 < i)
        {
            stringBuilder.Append(text.Substring(num4, i - num4));
        }
        return(stringBuilder.ToString());
    }
示例#21
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);
    }
示例#22
0
    // Token: 0x0600033E RID: 830 RVA: 0x00025F34 File Offset: 0x00024134
    public void Print(string text, Color32 color, BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols, bool encoding, UIFont.SymbolStyle symbolStyle, UIFont.Alignment alignment, int lineWidth, bool premultiply)
    {
        if (this.mReplacement != null)
        {
            this.mReplacement.Print(text, color, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth, premultiply);
        }
        else if (text != null)
        {
            if (!this.isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
                return;
            }
            bool isDynamic = this.isDynamic;
            if (isDynamic)
            {
                this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
                this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
                this.mDynamicFont.textureRebuildCallback = null;
            }
            this.mColors.Clear();
            this.mColors.Add(color);
            int     size   = this.size;
            Vector2 vector = (size <= 0) ? Vector2.one : new Vector2(1f / (float)size, 1f / (float)size);
            int     size2  = verts.size;
            int     num    = 0;
            int     num2   = 0;
            int     num3   = 0;
            int     num4   = 0;
            int     num5   = size + this.mSpacingY;
            Vector3 zero   = Vector3.zero;
            Vector3 zero2  = Vector3.zero;
            Vector2 zero3  = Vector2.zero;
            Vector2 zero4  = Vector2.zero;
            float   num6   = this.uvRect.width / (float)this.mFont.texWidth;
            float   num7   = this.mUVRect.height / (float)this.mFont.texHeight;
            int     length = text.Length;
            bool    flag   = encoding && symbolStyle != UIFont.SymbolStyle.None && this.hasSymbols && this.sprite != null;
            for (int i = 0; i < length; i++)
            {
                char c = text[i];
                if (c == '\n')
                {
                    if (num2 > num)
                    {
                        num = num2;
                    }
                    if (alignment != UIFont.Alignment.Left)
                    {
                        this.Align(verts, size2, alignment, num2, lineWidth);
                        size2 = verts.size;
                    }
                    num2  = 0;
                    num3 += num5;
                    num4  = 0;
                }
                else if (c < ' ')
                {
                    num4 = 0;
                }
                else
                {
                    if (encoding && c == '[')
                    {
                        int num8 = NGUITools.ParseSymbol(text, i, this.mColors, premultiply);
                        if (num8 > 0)
                        {
                            color = this.mColors[this.mColors.Count - 1];
                            i    += num8 - 1;
                            goto IL_96C;
                        }
                    }
                    if (!isDynamic)
                    {
                        BMSymbol bmsymbol = (!flag) ? null : this.MatchSymbol(text, i, length);
                        if (bmsymbol == null)
                        {
                            BMGlyph glyph = this.mFont.GetGlyph((int)c);
                            if (glyph == null)
                            {
                                goto IL_96C;
                            }
                            if (num4 != 0)
                            {
                                num2 += glyph.GetKerning(num4);
                            }
                            if (c == ' ')
                            {
                                num2 += this.mSpacingX + glyph.advance;
                                num4  = (int)c;
                                goto IL_96C;
                            }
                            zero.x  = vector.x * (float)(num2 + glyph.offsetX);
                            zero.y  = -vector.y * (float)(num3 + glyph.offsetY);
                            zero2.x = zero.x + vector.x * (float)glyph.width;
                            zero2.y = zero.y - vector.y * (float)glyph.height;
                            zero3.x = this.mUVRect.xMin + num6 * (float)glyph.x;
                            zero3.y = this.mUVRect.yMax - num7 * (float)glyph.y;
                            zero4.x = zero3.x + num6 * (float)glyph.width;
                            zero4.y = zero3.y - num7 * (float)glyph.height;
                            num2   += this.mSpacingX + glyph.advance;
                            num4    = (int)c;
                            if (glyph.channel == 0 || glyph.channel == 15)
                            {
                                for (int j = 0; j < 4; j++)
                                {
                                    cols.Add(color);
                                }
                            }
                            else
                            {
                                Color color2 = color;
                                color2 *= 0.49f;
                                switch (glyph.channel)
                                {
                                case 1:
                                    color2.b += 0.51f;
                                    break;

                                case 2:
                                    color2.g += 0.51f;
                                    break;

                                case 4:
                                    color2.r += 0.51f;
                                    break;

                                case 8:
                                    color2.a += 0.51f;
                                    break;
                                }
                                for (int k = 0; k < 4; k++)
                                {
                                    cols.Add(color2);
                                }
                            }
                        }
                        else
                        {
                            zero.x  = vector.x * (float)(num2 + bmsymbol.offsetX);
                            zero.y  = -vector.y * (float)(num3 + bmsymbol.offsetY);
                            zero2.x = zero.x + vector.x * (float)bmsymbol.width;
                            zero2.y = zero.y - vector.y * (float)bmsymbol.height;
                            Rect uvRect = bmsymbol.uvRect;
                            zero3.x = uvRect.xMin;
                            zero3.y = uvRect.yMax;
                            zero4.x = uvRect.xMax;
                            zero4.y = uvRect.yMin;
                            num2   += this.mSpacingX + bmsymbol.advance;
                            i      += bmsymbol.length - 1;
                            num4    = 0;
                            if (symbolStyle == UIFont.SymbolStyle.Colored)
                            {
                                for (int l = 0; l < 4; l++)
                                {
                                    cols.Add(color);
                                }
                            }
                            else
                            {
                                Color32 item = Color.white;
                                item.a = color.a;
                                for (int m = 0; m < 4; m++)
                                {
                                    cols.Add(item);
                                }
                            }
                        }
                        verts.Add(new Vector3(zero2.x, zero.y));
                        verts.Add(new Vector3(zero2.x, zero2.y));
                        verts.Add(new Vector3(zero.x, zero2.y));
                        verts.Add(new Vector3(zero.x, zero.y));
                        uvs.Add(new Vector2(zero4.x, zero3.y));
                        uvs.Add(new Vector2(zero4.x, zero4.y));
                        uvs.Add(new Vector2(zero3.x, zero4.y));
                        uvs.Add(new Vector2(zero3.x, zero3.y));
                    }
                    else if (this.mDynamicFont.GetCharacterInfo(c, out UIFont.mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
                    {
                        zero.x  = vector.x * ((float)num2 + UIFont.mChar.vert.xMin);
                        zero.y  = -vector.y * ((float)num3 - UIFont.mChar.vert.yMax + this.mDynamicFontOffset);
                        zero2.x = zero.x + vector.x * UIFont.mChar.vert.width;
                        zero2.y = zero.y - vector.y * UIFont.mChar.vert.height;
                        zero3.x = UIFont.mChar.uv.xMin;
                        zero3.y = UIFont.mChar.uv.yMin;
                        zero4.x = UIFont.mChar.uv.xMax;
                        zero4.y = UIFont.mChar.uv.yMax;
                        num2   += this.mSpacingX + (int)UIFont.mChar.width;
                        for (int n = 0; n < 4; n++)
                        {
                            cols.Add(color);
                        }
                        if (UIFont.mChar.flipped)
                        {
                            uvs.Add(new Vector2(zero3.x, zero4.y));
                            uvs.Add(new Vector2(zero3.x, zero3.y));
                            uvs.Add(new Vector2(zero4.x, zero3.y));
                            uvs.Add(new Vector2(zero4.x, zero4.y));
                        }
                        else
                        {
                            uvs.Add(new Vector2(zero4.x, zero3.y));
                            uvs.Add(new Vector2(zero3.x, zero3.y));
                            uvs.Add(new Vector2(zero3.x, zero4.y));
                            uvs.Add(new Vector2(zero4.x, zero4.y));
                        }
                        verts.Add(new Vector3(zero2.x, zero.y));
                        verts.Add(new Vector3(zero.x, zero.y));
                        verts.Add(new Vector3(zero.x, zero2.y));
                        verts.Add(new Vector3(zero2.x, zero2.y));
                    }
                }
                IL_96C :;
            }
            if (alignment != UIFont.Alignment.Left && size2 < verts.size)
            {
                this.Align(verts, size2, alignment, num2, lineWidth);
                size2 = verts.size;
            }
        }
    }
示例#23
0
    /// <summary>
    /// Print the specified text into the buffers.
    /// Note: 'lineWidth' parameter should be in pixels.
    /// </summary>

    public void Print(string text, Color32 color, Color32 gradualcolor, bool gradual, BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols,
                      bool encoding, SymbolStyle symbolStyle, Alignment alignment, int lineWidth, bool premultiply)
    {
        if (mReplacement != null)
        {
            mReplacement.Print(text, color, gradualcolor, gradual, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth, premultiply);
        }
        else if (text != null)
        {
            if (!isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
                return;
            }

#if DYNAMIC_FONT
            if (mDynamicFont != null)
            {
                mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize);
            }
#endif
            // Make sure the characters are present in the dynamic font before printing them
            bool dynamic = isDynamic;

            mColors.Clear();
            mColors.Add(color);

            int     fs      = size;
            Vector2 invSize = fs > 0 ? new Vector2(1f / fs, 1f / fs) : Vector2.one;

            int     indexOffset = verts.size;
            int     maxX = 0;
            int     x = 0;
            int     y = 0;
            int     prev = 0;
            int     lineHeight = (fs + mSpacingY);
            Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
            Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
            float   invX       = uvRect.width / mFont.texWidth;
            float   invY       = mUVRect.height / mFont.texHeight;
            int     textLength = text.Length;
            bool    useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols && sprite != null;

            for (int i = 0; i < textLength; ++i)
            {
                char c = text[i];

                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }

                    if (alignment != Alignment.Left)
                    {
                        Align(verts, indexOffset, alignment, x, lineWidth);
                        indexOffset = verts.size;
                    }

                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                if (c < ' ')
                {
                    prev = 0;
                    continue;
                }

                if (encoding && c == '[')
                {
                    int retVal = NGUITools.ParseSymbol(text, i, mColors, premultiply);

                    if (retVal > 0)
                    {
                        color = mColors[mColors.Count - 1];
                        i    += retVal - 1;
                        continue;
                    }
                }

                if (!dynamic)
                {
                    // See if there is a symbol matching this text
                    BMSymbol symbol = useSymbols ? MatchSymbol(text, i, textLength) : null;

                    if (symbol == null)
                    {
                        BMGlyph glyph = mFont.GetGlyph(c);
                        if (glyph == null)
                        {
                            continue;
                        }

                        if (prev != 0)
                        {
                            x += glyph.GetKerning(prev);
                        }

                        if (c == ' ')
                        {
                            x   += mSpacingX + glyph.advance;
                            prev = c;
                            continue;
                        }

                        v0.x = invSize.x * (x + glyph.offsetX);
                        v0.y = -invSize.y * (y + glyph.offsetY);

                        v1.x = v0.x + invSize.x * glyph.width;
                        v1.y = v0.y - invSize.y * glyph.height;

                        u0.x = mUVRect.xMin + invX * glyph.x;
                        u0.y = mUVRect.yMax - invY * glyph.y;

                        u1.x = u0.x + invX * glyph.width;
                        u1.y = u0.y - invY * glyph.height;

                        x   += mSpacingX + glyph.advance;
                        prev = c;

                        if (glyph.channel == 0 || glyph.channel == 15)
                        {
                            for (int b = 0; b < 4; ++b)
                            {
                                cols.Add(color);
                            }
                        }
                        else
                        {
                            // Packed fonts come as alpha masks in each of the RGBA channels.
                            // In order to use it we need to use a special shader.
                            //
                            // Limitations:
                            // - Effects (drop shadow, outline) will not work.
                            // - Should not be a part of the atlas (eastern fonts rarely are anyway).
                            // - Lower color precision

                            Color col = color;

                            col *= 0.49f;

                            switch (glyph.channel)
                            {
                            case 1: col.b += 0.51f; break;

                            case 2: col.g += 0.51f; break;

                            case 4: col.r += 0.51f; break;

                            case 8: col.a += 0.51f; break;
                            }

                            for (int b = 0; b < 4; ++b)
                            {
                                cols.Add(col);
                            }
                        }
                    }
                    else
                    {
                        v0.x = invSize.x * (x + symbol.offsetX);
                        v0.y = -invSize.y * (y + symbol.offsetY);

                        v1.x = v0.x + invSize.x * symbol.width;
                        v1.y = v0.y - invSize.y * symbol.height;

                        Rect uv = symbol.uvRect;

                        u0.x = uv.xMin;
                        u0.y = uv.yMax;
                        u1.x = uv.xMax;
                        u1.y = uv.yMin;

                        x   += mSpacingX + symbol.advance;
                        i   += symbol.length - 1;
                        prev = 0;

                        if (symbolStyle == SymbolStyle.Colored)
                        {
                            for (int b = 0; b < 4; ++b)
                            {
                                cols.Add(color);
                            }
                        }
                        else
                        {
                            Color32 col = Color.white;
                            col.a = color.a;
                            for (int b = 0; b < 4; ++b)
                            {
                                cols.Add(col);
                            }
                        }
                    }

                    verts.Add(new Vector3(v1.x, v0.y));
                    verts.Add(new Vector3(v1.x, v1.y));
                    verts.Add(new Vector3(v0.x, v1.y));
                    verts.Add(new Vector3(v0.x, v0.y));

                    uvs.Add(new Vector2(u1.x, u0.y));
                    uvs.Add(new Vector2(u1.x, u1.y));
                    uvs.Add(new Vector2(u0.x, u1.y));
                    uvs.Add(new Vector2(u0.x, u0.y));
                }
#if DYNAMIC_FONT
                else
                {
                    if (!mDynamicFont.GetCharacterInfo(c, out mChar, mDynamicFontSize, mDynamicFontStyle))
                    {
                        continue;
                    }

                    v0.x = invSize.x * (x + mChar.vert.xMin);
                    v0.y = -invSize.y * (y - mChar.vert.yMax + mDynamicFontOffset);

                    v1.x = v0.x + invSize.x * mChar.vert.width;
                    v1.y = v0.y - invSize.y * mChar.vert.height;

                    u0.x = mChar.uv.xMin;
                    u0.y = mChar.uv.yMin;
                    u1.x = mChar.uv.xMax;
                    u1.y = mChar.uv.yMax;

                    x += mSpacingX + (int)mChar.width;

                    if (gradual)
                    {
                        Color32 topCol    = Color32.Lerp(gradualcolor, color, -v1.y);
                        Color32 bottomCol = Color32.Lerp(gradualcolor, color, -v0.y);
                        cols.Add(bottomCol);
                        cols.Add(bottomCol);
                        cols.Add(topCol);
                        cols.Add(topCol);
                    }
                    else
                    {
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(color);
                        }
                    }

                    if (mChar.flipped)
                    {
                        uvs.Add(new Vector2(u0.x, u1.y));
                        uvs.Add(new Vector2(u0.x, u0.y));
                        uvs.Add(new Vector2(u1.x, u0.y));
                        uvs.Add(new Vector2(u1.x, u1.y));
                    }
                    else
                    {
                        uvs.Add(new Vector2(u1.x, u0.y));
                        uvs.Add(new Vector2(u0.x, u0.y));
                        uvs.Add(new Vector2(u0.x, u1.y));
                        uvs.Add(new Vector2(u1.x, u1.y));
                    }

                    verts.Add(new Vector3(v1.x, v0.y));
                    verts.Add(new Vector3(v0.x, v0.y));
                    verts.Add(new Vector3(v0.x, v1.y));
                    verts.Add(new Vector3(v1.x, v1.y));
                }
#endif
            }

            if (alignment != Alignment.Left && indexOffset < verts.size)
            {
                Align(verts, indexOffset, alignment, x, lineWidth);
                indexOffset = verts.size;
            }
        }
    }
示例#24
0
    /// <summary>
    /// Get the printed size of the specified string. The returned value is in local coordinates. Multiply by transform's scale to get pixels.
    /// </summary>

    public Vector2 CalculatePrintedSize(string text, bool encoding)
    {
        if (mReplacement != null)
        {
            return(mReplacement.CalculatePrintedSize(text, encoding));
        }

        Vector2 v = Vector2.zero;

        if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text))
        {
            if (encoding)
            {
                text = NGUITools.StripSymbols(text);
            }

            int maxX       = 0;
            int x          = 0;
            int y          = 0;
            int prev       = 0;
            int lineHeight = (mFont.charSize + mSpacingY);

            for (int i = 0, imax = text.Length; i < imax; ++i)
            {
                char c = text[i];

                // Start a new line
                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }
                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                // Skip invalid characters
                if (c < ' ')
                {
                    prev = 0; continue;
                }

                BMGlyph glyph = mFont.GetGlyph(c);

                if (glyph != null)
                {
                    x   += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance);
                    prev = c;
                }
            }

            // Convert from pixel coordinates to local coordinates
            float scale = (mFont.charSize > 0) ? 1f / mFont.charSize : 1f;
            v.x = scale * ((x > maxX) ? x : maxX);
            v.y = scale * (y + lineHeight);
        }
        return(v);
    }
示例#25
0
    /// <summary>
    /// Text wrapping functionality. The 'maxWidth' should be in local coordinates (take pixels and divide them by transform's scale).
    /// </summary>

    public bool WrapText(string text, out string finalText, float width, float height, int lines, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, out finalText, width, height, lines, encoding, symbolStyle));
        }

        // Zero means unlimited
        if (width == 0f)
        {
            width = 100000f;
        }
        if (height == 0f)
        {
            height = 100000f;
        }

        // Width and height of the line in pixels
        int lineWidth  = Mathf.FloorToInt(width * size);
        int lineHeight = Mathf.FloorToInt(height * size);

        if (lineWidth < 1 || lineHeight < 1)
        {
            finalText = "";
            return(false);
        }

        int maxLineCount = (lines > 0) ? lines : 999999;

        if (height != 0f)
        {
            maxLineCount = Mathf.Min(maxLineCount, Mathf.FloorToInt(height));

            if (maxLineCount == 0)
            {
                finalText = "";
                return(false);
            }
        }

#if DYNAMIC_FONT
        if (mDynamicFont != null)
        {
            mDynamicFont.RequestCharactersInTexture(text, mDynamicFontSize);
        }
#endif
        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = lineWidth;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;
        bool          multiline      = (lines != 1);
        int           lineCount      = 1;
        bool          useSymbols     = encoding && symbolStyle != SymbolStyle.None && hasSymbols;
        bool          dynamic        = isDynamic;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline || lineCount == maxLineCount)
                {
                    break;
                }
                remainingWidth = lineWidth;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty = true;
                ++lineCount;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (encoding && ch == '[')
            {
                if (offset + 2 < textLength)
                {
                    if (text[offset + 1] == '-' && text[offset + 2] == ']')
                    {
                        offset += 2;
                        continue;
                    }
                    else if (offset + 7 < textLength && text[offset + 7] == ']')
                    {
                        if (NGUITools.EncodeColor(NGUITools.ParseColor(text, offset + 1)) == text.Substring(offset + 1, 6).ToUpper())
                        {
                            offset += 7;
                            continue;
                        }
                    }
                }
            }

            // See if there is a symbol matching this text
            BMSymbol symbol = useSymbols ? MatchSymbol(text, offset, 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 = (symbol == null) ? mFont.GetGlyph(ch) : null;

                    if (glyph != null)
                    {
                        glyphWidth += (previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
#if DYNAMIC_FONT
            else
            {
                if (mDynamicFont.GetCharacterInfo(ch, out mChar, mDynamicFontSize, mDynamicFontStyle))
                {
                    glyphWidth += Mathf.RoundToInt(mChar.width);
                }
            }
#endif
            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;

            // Doesn't fit?
            if (remainingWidth < 0)
            {
                // Can't start a new line
                if (lineIsEmpty || !multiline || lineCount == maxLineCount)
                {
                    // This is the first word on the line -- add it up to the character that fits
                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                    if (!multiline || lineCount == maxLineCount)
                    {
                        start = offset;
                        break;
                    }
                    EndLine(ref sb);

                    // Start a brand-new line
                    lineIsEmpty = true;
                    ++lineCount;

                    if (ch == ' ')
                    {
                        start          = offset + 1;
                        remainingWidth = lineWidth;
                    }
                    else
                    {
                        start          = offset;
                        remainingWidth = lineWidth - glyphWidth;
                    }
                    previousChar = 0;
                }
                else
                {
                    // Skip all spaces before the word
                    while (start < textLength && text[start] == ' ')
                    {
                        ++start;
                    }

                    // Revert the position to the beginning of the word and reset the line
                    lineIsEmpty    = true;
                    remainingWidth = lineWidth;
                    offset         = start - 1;
                    previousChar   = 0;

                    if (!multiline || lineCount == maxLineCount)
                    {
                        break;
                    }
                    ++lineCount;
                    EndLine(ref sb);
                    continue;
                }
            }
            else
            {
                previousChar = ch;
            }

            // Advance the offset past the symbol
            if (!dynamic && symbol != null)
            {
                offset      += symbol.length - 1;
                previousChar = 0;
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        finalText = sb.ToString();
        return(!multiline || offset == textLength || (lines > 0 && lineCount <= lines));
    }
示例#26
0
    /// <summary>
    /// Text wrapping functionality. The 'maxWidth' should be in local coordinates (take pixels and divide them by transform's scale).
    /// </summary>

    public string WrapText(string text, float maxWidth, bool multiline, bool encoding)
    {
        if (mReplacement != null)
        {
            return(mReplacement.WrapText(text, maxWidth, multiline, encoding));
        }

        // Width of the line in pixels
        int lineWidth = Mathf.RoundToInt(maxWidth * size);

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

        StringBuilder sb             = new StringBuilder();
        int           textLength     = text.Length;
        int           remainingWidth = lineWidth;
        int           previousChar   = 0;
        int           start          = 0;
        int           offset         = 0;
        bool          lineIsEmpty    = true;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline)
                {
                    break;
                }
                remainingWidth = lineWidth;

                // Add the previous word to the final string
                if (start < offset)
                {
                    sb.Append(text.Substring(start, offset - start + 1));
                }
                else
                {
                    sb.Append(ch);
                }

                lineIsEmpty  = true;
                start        = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty  = false;
                start        = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (encoding && ch == '[')
            {
                if (offset + 2 < textLength)
                {
                    if (text[offset + 1] == '-' && text[offset + 2] == ']')
                    {
                        offset += 2;
                        continue;
                    }
                    else if (offset + 7 < textLength && text[offset + 7] == ']')
                    {
                        offset += 7;
                        continue;
                    }
                }
            }

            BMGlyph glyph = mFont.GetGlyph(ch);

            if (glyph != null)
            {
                int charSize = mSpacingX + ((previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance);

                remainingWidth -= charSize;

                // Doesn't fit?
                if (remainingWidth < 0)
                {
                    if (lineIsEmpty || !multiline)
                    {
                        // This is the first word on the line -- add it up to the character that fits
                        sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                        if (!multiline)
                        {
                            start = offset;
                            break;
                        }
                        EndLine(ref sb);

                        // Start a brand-new line
                        lineIsEmpty = true;

                        if (ch == ' ')
                        {
                            start          = offset + 1;
                            remainingWidth = lineWidth;
                        }
                        else
                        {
                            start          = offset;
                            remainingWidth = lineWidth - charSize;
                        }
                        previousChar = 0;
                    }
                    else
                    {
                        // Skip all spaces before the word
                        while (start < textLength && text[start] == ' ')
                        {
                            ++start;
                        }

                        // Revert the position to the beginning of the word and reset the line
                        lineIsEmpty    = true;
                        remainingWidth = lineWidth;
                        offset         = start - 1;
                        previousChar   = 0;
                        if (!multiline)
                        {
                            break;
                        }
                        EndLine(ref sb);
                        continue;
                    }
                }
                else
                {
                    previousChar = ch;
                }
            }
        }

        if (start < offset)
        {
            sb.Append(text.Substring(start, offset - start));
        }
        return(sb.ToString());
    }
示例#27
0
    public void Print(string text, Color32 color, BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols, bool encoding, SymbolStyle symbolStyle, Alignment alignment, int lineWidth, bool premultiply)
    {
        if (this.mReplacement != null)
        {
            this.mReplacement.Print(text, color, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth, premultiply);
        }
        else if (text != null)
        {
            if (!this.isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
            }
            else
            {
                bool isDynamic = this.isDynamic;
                if (isDynamic)
                {
                    this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
                    this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
                    this.mDynamicFont.textureRebuildCallback = null;
                }
                this.mColors.Clear();
                this.mColors.Add((Color)color);
                int     size         = this.size;
                Vector2 vector       = (size <= 0) ? Vector2.one : new Vector2(1f / ((float)size), 1f / ((float)size));
                int     indexOffset  = verts.size;
                int     num3         = 0;
                int     x            = 0;
                int     num5         = 0;
                int     previousChar = 0;
                int     num7         = size + this.mSpacingY;
                Vector3 zero         = Vector3.zero;
                Vector3 vector3      = Vector3.zero;
                Vector2 vector4      = Vector2.zero;
                Vector2 vector5      = Vector2.zero;
                float   num8         = this.uvRect.width / ((float)this.mFont.texWidth);
                float   num9         = this.mUVRect.height / ((float)this.mFont.texHeight);
                int     length       = text.Length;
                bool    flag2        = ((encoding && (symbolStyle != SymbolStyle.None)) && this.hasSymbols) && (this.sprite != null);
                for (int i = 0; i < length; i++)
                {
                    char index = text[i];
                    if (index == '\n')
                    {
                        if (x > num3)
                        {
                            num3 = x;
                        }
                        if (alignment != Alignment.Left)
                        {
                            this.Align(verts, indexOffset, alignment, x, lineWidth);
                            indexOffset = verts.size;
                        }
                        x            = 0;
                        num5        += num7;
                        previousChar = 0;
                        continue;
                    }
                    if (index < ' ')
                    {
                        previousChar = 0;
                        continue;
                    }
                    if (encoding && (index == '['))
                    {
                        int num12 = NGUITools.ParseSymbol(text, i, this.mColors, premultiply);
                        if (num12 > 0)
                        {
                            color = this.mColors[this.mColors.Count - 1];
                            i    += num12 - 1;
                            continue;
                        }
                    }
                    if (!isDynamic)
                    {
                        BMSymbol symbol = !flag2 ? null : this.MatchSymbol(text, i, length);
                        if (symbol == null)
                        {
                            BMGlyph glyph = this.mFont.GetGlyph(index);
                            if (glyph == null)
                            {
                                continue;
                            }
                            if (previousChar != 0)
                            {
                                x += glyph.GetKerning(previousChar);
                            }
                            if (index == ' ')
                            {
                                x           += this.mSpacingX + glyph.advance;
                                previousChar = index;
                                continue;
                            }
                            zero.x       = vector.x * (x + glyph.offsetX);
                            zero.y       = -vector.y * (num5 + glyph.offsetY);
                            vector3.x    = zero.x + (vector.x * glyph.width);
                            vector3.y    = zero.y - (vector.y * glyph.height);
                            vector4.x    = this.mUVRect.xMin + (num8 * glyph.x);
                            vector4.y    = this.mUVRect.yMax - (num9 * glyph.y);
                            vector5.x    = vector4.x + (num8 * glyph.width);
                            vector5.y    = vector4.y - (num9 * glyph.height);
                            x           += this.mSpacingX + glyph.advance;
                            previousChar = index;
                            if ((glyph.channel == 0) || (glyph.channel == 15))
                            {
                                for (int j = 0; j < 4; j++)
                                {
                                    cols.Add(color);
                                }
                            }
                            else
                            {
                                Color item = (Color)color;
                                item = (Color)(item * 0.49f);
                                switch (glyph.channel)
                                {
                                case 1:
                                    item.b += 0.51f;
                                    break;

                                case 2:
                                    item.g += 0.51f;
                                    break;

                                case 4:
                                    item.r += 0.51f;
                                    break;

                                case 8:
                                    item.a += 0.51f;
                                    break;
                                }
                                for (int k = 0; k < 4; k++)
                                {
                                    cols.Add(item);
                                }
                            }
                        }
                        else
                        {
                            zero.x    = vector.x * (x + symbol.offsetX);
                            zero.y    = -vector.y * (num5 + symbol.offsetY);
                            vector3.x = zero.x + (vector.x * symbol.width);
                            vector3.y = zero.y - (vector.y * symbol.height);
                            Rect uvRect = symbol.uvRect;
                            vector4.x    = uvRect.xMin;
                            vector4.y    = uvRect.yMax;
                            vector5.x    = uvRect.xMax;
                            vector5.y    = uvRect.yMin;
                            x           += this.mSpacingX + symbol.advance;
                            i           += symbol.length - 1;
                            previousChar = 0;
                            if (symbolStyle == SymbolStyle.Colored)
                            {
                                for (int m = 0; m < 4; m++)
                                {
                                    cols.Add(color);
                                }
                            }
                            else
                            {
                                Color32 white = Color.white;
                                white.a = color.a;
                                for (int n = 0; n < 4; n++)
                                {
                                    cols.Add(white);
                                }
                            }
                        }
                        verts.Add(new Vector3(vector3.x, zero.y));
                        verts.Add(new Vector3(vector3.x, vector3.y));
                        verts.Add(new Vector3(zero.x, vector3.y));
                        verts.Add(new Vector3(zero.x, zero.y));
                        uvs.Add(new Vector2(vector5.x, vector4.y));
                        uvs.Add(new Vector2(vector5.x, vector5.y));
                        uvs.Add(new Vector2(vector4.x, vector5.y));
                        uvs.Add(new Vector2(vector4.x, vector4.y));
                        continue;
                    }
                    if (this.mDynamicFont.GetCharacterInfo(index, out mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
                    {
                        zero.x    = vector.x * (x + mChar.vert.xMin);
                        zero.y    = -vector.y * ((num5 - mChar.vert.yMax) + this.mDynamicFontOffset);
                        vector3.x = zero.x + (vector.x * mChar.vert.width);
                        vector3.y = zero.y - (vector.y * mChar.vert.height);
                        vector4.x = mChar.uv.xMin;
                        vector4.y = mChar.uv.yMin;
                        vector5.x = mChar.uv.xMax;
                        vector5.y = mChar.uv.yMax;
                        x        += this.mSpacingX + ((int)mChar.width);
                        for (int num18 = 0; num18 < 4; num18++)
                        {
                            cols.Add(color);
                        }
                        if (mChar.flipped)
                        {
                            uvs.Add(new Vector2(vector4.x, vector5.y));
                            uvs.Add(new Vector2(vector4.x, vector4.y));
                            uvs.Add(new Vector2(vector5.x, vector4.y));
                            uvs.Add(new Vector2(vector5.x, vector5.y));
                        }
                        else
                        {
                            uvs.Add(new Vector2(vector5.x, vector4.y));
                            uvs.Add(new Vector2(vector4.x, vector4.y));
                            uvs.Add(new Vector2(vector4.x, vector5.y));
                            uvs.Add(new Vector2(vector5.x, vector5.y));
                        }
                        verts.Add(new Vector3(vector3.x, zero.y));
                        verts.Add(new Vector3(zero.x, zero.y));
                        verts.Add(new Vector3(zero.x, vector3.y));
                        verts.Add(new Vector3(vector3.x, vector3.y));
                    }
                }
                if ((alignment != Alignment.Left) && (indexOffset < verts.size))
                {
                    this.Align(verts, indexOffset, alignment, x, lineWidth);
                    indexOffset = verts.size;
                }
            }
        }
    }
示例#28
0
    /// <summary>
    /// Print the specified text into the buffers.
    /// Note: 'lineWidth' parameter should be in pixels.
    /// </summary>

    public void Print(string text, Color color, BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color> cols,
                      bool encoding, Alignment alignment, int lineWidth)
    {
        if (mReplacement != null)
        {
            mReplacement.Print(text, color, verts, uvs, cols, encoding, alignment, lineWidth);
        }
        else if (mFont != null && text != null)
        {
            if (!mFont.isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
                return;
            }

            mColors.Clear();
            mColors.Add(color);

            Vector2 scale = mFont.charSize > 0 ? new Vector2(1f / mFont.charSize, 1f / mFont.charSize) : Vector2.one;

            int     indexOffset = verts.size;
            int     maxX = 0;
            int     x = 0;
            int     y = 0;
            int     prev = 0;
            int     lineHeight = (mFont.charSize + mSpacingY);
            Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
            Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
            float   invX = uvRect.width / mFont.texWidth;
            float   invY = mUVRect.height / mFont.texHeight;

            for (int i = 0, imax = text.Length; i < imax; ++i)
            {
                char c = text[i];

                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }

                    if (alignment != Alignment.Left)
                    {
                        Align(verts, indexOffset, alignment, x, lineWidth);
                        indexOffset = verts.size;
                    }

                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                if (c < ' ')
                {
                    prev = 0;
                    continue;
                }

                if (encoding && c == '[')
                {
                    int retVal = NGUITools.ParseSymbol(text, i, mColors);

                    if (retVal > 0)
                    {
                        color = mColors[mColors.Count - 1];
                        i    += retVal - 1;
                        continue;
                    }
                }

                BMGlyph glyph = mFont.GetGlyph(c);

                if (glyph != null)
                {
                    if (prev != 0)
                    {
                        x += glyph.GetKerning(prev);
                    }

                    if (c != ' ')
                    {
                        v0.x = scale.x * (x + glyph.offsetX);
                        v0.y = -scale.y * (y + glyph.offsetY);

                        v1.x = v0.x + scale.x * glyph.width;
                        v1.y = v0.y - scale.y * glyph.height;

                        u0.x = mUVRect.xMin + invX * glyph.x;
                        u0.y = mUVRect.yMax - invY * glyph.y;

                        u1.x = u0.x + invX * glyph.width;
                        u1.y = u0.y - invY * glyph.height;

                        verts.Add(new Vector3(v1.x, v0.y));
                        verts.Add(new Vector3(v1.x, v1.y));
                        verts.Add(new Vector3(v0.x, v1.y));
                        verts.Add(new Vector3(v0.x, v0.y));

                        uvs.Add(new Vector2(u1.x, u0.y));
                        uvs.Add(new Vector2(u1.x, u1.y));
                        uvs.Add(new Vector2(u0.x, u1.y));
                        uvs.Add(new Vector2(u0.x, u0.y));

                        cols.Add(color);
                        cols.Add(color);
                        cols.Add(color);
                        cols.Add(color);
                    }
                    x   += mSpacingX + glyph.advance;
                    prev = c;
                }
            }

            if (alignment != Alignment.Left && indexOffset < verts.size)
            {
                Align(verts, indexOffset, alignment, x, lineWidth);
                indexOffset = verts.size;
            }
        }
    }
示例#29
0
    public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle)
    {
        if (this.mReplacement != null)
        {
            return(this.mReplacement.CalculatePrintedSize(text, encoding, symbolStyle));
        }
        Vector2 zero      = Vector2.zero;
        bool    isDynamic = this.isDynamic;

        if (isDynamic || (((this.mFont != null) && this.mFont.isValid) && !string.IsNullOrEmpty(text)))
        {
            if (encoding)
            {
                text = NGUITools.StripSymbols(text);
            }
            if (isDynamic)
            {
                this.mDynamicFont.textureRebuildCallback = new Font.FontTextureRebuildCallback(this.OnFontChanged);
                this.mDynamicFont.RequestCharactersInTexture(text, this.mDynamicFontSize, this.mDynamicFontStyle);
                this.mDynamicFont.textureRebuildCallback = null;
            }
            int  length       = text.Length;
            int  num2         = 0;
            int  num3         = 0;
            int  num4         = 0;
            int  previousChar = 0;
            int  size         = this.size;
            int  num7         = size + this.mSpacingY;
            bool flag2        = (encoding && (symbolStyle != SymbolStyle.None)) && this.hasSymbols;
            for (int i = 0; i < length; i++)
            {
                char index = text[i];
                if (index == '\n')
                {
                    if (num3 > num2)
                    {
                        num2 = num3;
                    }
                    num3         = 0;
                    num4        += num7;
                    previousChar = 0;
                }
                else if (index < ' ')
                {
                    previousChar = 0;
                }
                else if (!isDynamic)
                {
                    BMSymbol symbol = !flag2 ? null : this.MatchSymbol(text, i, length);
                    if (symbol == null)
                    {
                        BMGlyph glyph = this.mFont.GetGlyph(index);
                        if (glyph != null)
                        {
                            num3        += this.mSpacingX + ((previousChar == 0) ? glyph.advance : (glyph.advance + glyph.GetKerning(previousChar)));
                            previousChar = index;
                        }
                    }
                    else
                    {
                        num3        += this.mSpacingX + symbol.width;
                        i           += symbol.length - 1;
                        previousChar = 0;
                    }
                }
                else if (this.mDynamicFont.GetCharacterInfo(index, out mChar, this.mDynamicFontSize, this.mDynamicFontStyle))
                {
                    num3 += this.mSpacingX + ((int)mChar.width);
                }
            }
            float num9 = (size <= 0) ? 1f : (1f / ((float)size));
            zero.x = num9 * ((num3 <= num2) ? ((float)num2) : ((float)num3));
            zero.y = num9 * (num4 + num7);
        }
        return(zero);
    }
示例#30
0
    /// <summary>
    /// Print the specified text into the buffers.
    /// Note: 'lineWidth' parameter should be in pixels.
    /// </summary>

    public void Print(string text, BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols)
    {
        if (mReplacement != null)
        {
            mReplacement.Print(text, verts, uvs, cols);
        }
        else if (!string.IsNullOrEmpty(text))
        {
            if (!isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
                return;
            }

#if DYNAMIC_FONT
            if (isDynamic)
            {
                // NOTE: This shouldn't be used anymore. All dynamic font printing goes directly through NGUIText instead.
                NGUIText.current.size  = mDynamicFontSize;
                NGUIText.current.style = mDynamicFontStyle;
                NGUIText.Print(dynamicFont, text, verts, uvs, cols);
                return;
            }
#endif
            mColors.Add(Color.white);

            int     fs = NGUIText.current.size;
            int     indexOffset = verts.size;
            int     maxX = 0;
            int     x = 0;
            int     y = 0;
            int     prev = 0;
            int     lineHeight = (fs + NGUIText.current.spacingY);
            Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
            Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
            Color   gb = NGUIText.current.tint * NGUIText.current.gradientBottom;
            Color   gt = NGUIText.current.tint * NGUIText.current.gradientTop;
            Color32 uc = NGUIText.current.tint;

            float invX = uvRect.width / mFont.texWidth;
            float invY = mUVRect.height / mFont.texHeight;

            int  textLength = text.Length;
            bool useSymbols = NGUIText.current.encoding && NGUIText.current.symbolStyle != NGUIText.SymbolStyle.None && hasSymbols && sprite != null;

            for (int i = 0; i < textLength; ++i)
            {
                char c = text[i];

                if (c == '\n')
                {
                    if (x > maxX)
                    {
                        maxX = x;
                    }

                    if (NGUIText.current.alignment != TextAlignment.Left)
                    {
                        NGUIText.Align(verts, indexOffset, x - NGUIText.current.spacingX);
                        indexOffset = verts.size;
                    }

                    x    = 0;
                    y   += lineHeight;
                    prev = 0;
                    continue;
                }

                if (c < ' ')
                {
                    prev = 0;
                    continue;
                }

                bool isMineColor = false;

                if (NGUIText.current.encoding && NGUIText.ParseSymbol(text, ref i, mColors, NGUIText.current.premultiply, ref isMineColor))
                {
                    Color fc = mColors[mColors.size - 1];

                    if (isMineColor)
                    {
                        fc *= NGUIText.current.tint;
                    }

                    // *******************
                    // add sj.
                    // *******************
                    fc.a = uc.a / 255.0f;
                    // *******************
                    uc = fc;

                    if (NGUIText.current.gradient)
                    {
                        gb = NGUIText.current.gradientBottom * fc;
                        gt = NGUIText.current.gradientTop * fc;
                    }
                    --i;
                    continue;
                }

                // See if there is a symbol matching this text
                BMSymbol symbol = useSymbols ? MatchSymbol(text, i, textLength) : null;

                if (symbol == null)
                {
                    BMGlyph glyph = mFont.GetGlyph(c);
                    if (glyph == null)
                    {
                        continue;
                    }

                    if (prev != 0)
                    {
                        x += glyph.GetKerning(prev);
                    }

                    if (c == ' ')
                    {
                        x   += NGUIText.current.spacingX + glyph.advance;
                        prev = c;
                        continue;
                    }

                    v0.x = (x + glyph.offsetX);
                    v0.y = -(y + glyph.offsetY);

                    v1.x = v0.x + glyph.width;
                    v1.y = v0.y - glyph.height;

                    u0.x = mUVRect.xMin + invX * glyph.x;
                    u0.y = mUVRect.yMax - invY * glyph.y;

                    u1.x = u0.x + invX * glyph.width;
                    u1.y = u0.y - invY * glyph.height;

                    x   += NGUIText.current.spacingX + glyph.advance;
                    prev = c;

                    if (NGUIText.current.gradient)
                    {
                        float min = NGUIText.current.size - glyph.offsetY;
                        float max = min - glyph.height;

                        min /= NGUIText.current.size;
                        max /= NGUIText.current.size;

                        s_c0 = Color.Lerp(gb, gt, min);
                        s_c1 = Color.Lerp(gb, gt, max);

                        cols.Add(s_c0);
                        cols.Add(s_c1);
                        cols.Add(s_c1);
                        cols.Add(s_c0);
                    }
                    else
                    {
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(uc);
                        }
                    }
                }
                else
                {
                    v0.x = (x + symbol.offsetX);
                    v0.y = -(y + symbol.offsetY);

                    v1.x = v0.x + symbol.width;
                    v1.y = v0.y - symbol.height;

                    Rect uv = symbol.uvRect;

                    u0.x = uv.xMin;
                    u0.y = uv.yMax;
                    u1.x = uv.xMax;
                    u1.y = uv.yMin;

                    x   += NGUIText.current.spacingX + symbol.advance;
                    i   += symbol.length - 1;
                    prev = 0;

                    if (NGUIText.current.symbolStyle == NGUIText.SymbolStyle.Colored)
                    {
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(uc);
                        }
                    }
                    else
                    {
                        Color32 col = Color.white;
                        col.a = uc.a;
                        for (int b = 0; b < 4; ++b)
                        {
                            cols.Add(col);
                        }
                    }
                }

                verts.Add(new Vector3(v1.x, v0.y));
                verts.Add(new Vector3(v1.x, v1.y));
                verts.Add(new Vector3(v0.x, v1.y));
                verts.Add(new Vector3(v0.x, v0.y));

                uvs.Add(new Vector2(u1.x, u0.y));
                uvs.Add(new Vector2(u1.x, u1.y));
                uvs.Add(new Vector2(u0.x, u1.y));
                uvs.Add(new Vector2(u0.x, u0.y));
            }

            if (NGUIText.current.alignment != TextAlignment.Left && indexOffset < verts.size)
            {
                NGUIText.Align(verts, indexOffset, x - NGUIText.current.spacingX);
                indexOffset = verts.size;
            }
            mColors.Clear();
        }
    }