Example #1
0
        // Measure the length of the span of the text. Currently, this is only used to compute the length
        // of ellipsis, assuming that the ellipsis does not contain any tab, tab is not considered for simplicity
        public static float measureText(string text, TextStyle style)
        {
            char  startingChar = text[0];
            float totalWidth   = 0;

            if (char.IsHighSurrogate(startingChar) || EmojiUtils.isSingleCharEmoji(startingChar))
            {
                float advance = style.fontSize * EmojiUtils.advanceFactor + style.letterSpacing;
                for (int i = 0; i < text.Length; i++)
                {
                    char ch = text[i];
                    if (char.IsHighSurrogate(ch) || EmojiUtils.isSingleCharNonEmptyEmoji(ch))
                    {
                        totalWidth += advance;
                    }
                }
            }
            else
            {
                Font font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;
                font.RequestCharactersInTextureSafe(text, style.UnityFontSize, style.UnityFontStyle);
                for (int i = 0; i < text.Length; i++)
                {
                    char ch = text[i];
                    if (font.getGlyphInfo(ch, out var glyphInfo, style.UnityFontSize, style.UnityFontStyle))
                    {
                        totalWidth += glyphInfo.advance + style.letterSpacing;
                    }
Example #2
0
        void layoutEmoji(string text, TextStyle style, Font font, int start, int count)
        {
            for (int i = 0; i < count; i++)
            {
                char  c = text[i];
                float x = this._advance;
                if (EmojiUtils.isSingleCharNonEmptyEmoji(c) || char.IsHighSurrogate(c))
                {
                    float letterSpace          = style.letterSpacing;
                    float letterSpaceHalfLeft  = letterSpace * 0.5f;
                    float letterSpaceHalfRight = letterSpace - letterSpaceHalfLeft;

                    x += letterSpaceHalfLeft;
                    this._advances[i] += letterSpaceHalfLeft;

                    var metrics = FontMetrics.fromFont(font, style.UnityFontSize);

                    var minX = x;
                    var maxX = metrics.descent - metrics.ascent + x;
                    var minY = metrics.ascent;
                    var maxY = metrics.descent;

                    if (this._bounds.width <= 0 || this._bounds.height <= 0)
                    {
                        this._bounds = UnityEngine.Rect.MinMaxRect(
                            minX, minY, maxX, maxY);
                    }
                    else
                    {
                        if (minX < this._bounds.x)
                        {
                            this._bounds.x = minX;
                        }

                        if (minY < this._bounds.y)
                        {
                            this._bounds.y = minY;
                        }

                        if (maxX > this._bounds.xMax)
                        {
                            this._bounds.xMax = maxX;
                        }

                        if (maxY > this._bounds.yMax)
                        {
                            this._bounds.yMax = maxY;
                        }
                    }

                    this._positions[i] = x;
                    float advance = style.fontSize;
                    x += advance;

                    this._advances[i] += advance;
                    this._advances[i] += letterSpaceHalfRight;
                    x += letterSpaceHalfRight;
                }
                else
                {
                    this._advances[i]  = 0;
                    this._positions[i] = x;
                }

                this._advance = x;
            }
        }