Ejemplo n.º 1
0
        void layoutWord(float offset, int layoutOffset,
                        TextBuff buff, int start, int wordCount, TextStyle style)
        {
            float wordSpacing =
                wordCount == 1 && LayoutUtils.isWordSpace(buff.charAt(start)) ? style.wordSpacing : 0;

            var font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;

            font.RequestCharactersInTextureSafe(buff.subBuff(start, wordCount).getString(),
                                                style.UnityFontSize,
                                                style.UnityFontStyle);
            float x                    = this._advance;
            float letterSpace          = style.letterSpacing;
            float letterSpaceHalfLeft  = letterSpace * 0.5f;
            float letterSpaceHalfRight = letterSpace - letterSpaceHalfLeft;

            for (int i = 0; i < wordCount; i++)
            {
                var ch = buff.charAt(start + i);
                if (i == 0)
                {
                    x += letterSpaceHalfLeft + wordSpacing;
                    this._advances[i + layoutOffset] += letterSpaceHalfLeft + wordSpacing;
                }
                else
                {
                    this._advances[i - 1 + layoutOffset] += letterSpaceHalfRight;
                    this._advances[i + layoutOffset]     += letterSpaceHalfLeft;
                    x += letterSpace;
                }

                var glyphInfo = font.getGlyphInfo(ch, style.UnityFontSize, style.UnityFontStyle);
                var rect      = glyphInfo.rect;
                rect = rect.translate(x, 0);
                if (this._bounds == null || this._bounds.isEmpty)
                {
                    this._bounds = rect;
                }
                else
                {
                    this._bounds = this._bounds.expandToInclude(rect);
                }

                this._positions[i + layoutOffset] = x;
                float advance = glyphInfo.advance;
                if (ch == '\t')
                {
                    advance = this._tabStops.nextTab((this._advance + offset)) - this._advance;
                }
                x += advance;
                this._advances[i + layoutOffset] += advance;
                if (i + 1 == wordCount)
                {
                    this._advances[i + layoutOffset] += letterSpaceHalfRight;
                    x += letterSpaceHalfRight;
                }
            }

            this._advance = x;
        }
Ejemplo n.º 2
0
        public void doLayout(float offset, TextBuff buff, int start, int count, TextStyle style)
        {
            this._start = start;
            this._count = count;
            this._advances.reset(count);
            this._positions.reset(count);
            this._advance = 0;
            this._bounds  = default;

            var font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;

            font.RequestCharactersInTextureSafe(buff.text, style.UnityFontSize, style.UnityFontStyle);

            int wordstart = start == buff.size
                ? start
                : LayoutUtils.getPrevWordBreakForCache(buff, start + 1);
            int wordend;

            for (int iter = start; iter < start + count; iter = wordend)
            {
                wordend = LayoutUtils.getNextWordBreakForCache(buff, iter);
                int wordCount = Mathf.Min(start + count, wordend) - iter;
                this.layoutWord(offset, iter - start, buff.subBuff(wordstart, wordend - wordstart),
                                iter - wordstart, wordCount, style, font);
                wordstart = wordend;
            }
            this._count = count;
        }
Ejemplo n.º 3
0
 public void finish()
 {
     this._wordBreaker.finish();
     this._width = 0;
     this._candidates.Clear();
     this._widths.Clear();
     this._breaks.Clear();
     this._textBuf = default;
 }
Ejemplo n.º 4
0
        public void setText(string text, int textOffset, int textLength)
        {
            this._textBuf = new TextBuff(text, textOffset, textLength);
            this._wordBreaker.setText(this._textBuf);
            this._wordBreaker.next();
            this._candidates.Clear();
            Candidate can = new Candidate {
                offset = 0, postBreak = 0, preBreak = 0, postSpaceCount = 0, preSpaceCount = 0, pre = 0
            };

            this._candidates.Add(can);
            this._lastBreak     = 0;
            this._bestBreak     = 0;
            this._bestScore     = ScoreInfty;
            this._preBreak      = 0;
            this.mFirstTabIndex = int.MaxValue;
            this._spaceCount    = 0;
        }
Ejemplo n.º 5
0
        public static float measureText(float offset, TextBuff buff, int start, int count, TextStyle style,
                                        List <float> advances, int advanceOffset, TabStops tabStops)
        {
            Layout layout = new Layout();

            layout.setTabStops(tabStops);
            layout.doLayout(offset, buff, start, count, style);
            if (advances != null)
            {
                var layoutAdv = layout.getAdvances();
                for (int i = 0; i < count; i++)
                {
                    advances[i + advanceOffset] = layoutAdv[i];
                }
            }

            return(layout.getAdvance());
        }
Ejemplo n.º 6
0
        public void doLayout(float offset, TextBuff buff, int start, int count, TextStyle style)
        {
            this._start = start;
            this._count = count;
            this._advances.resize(count, 0);
            this._positions.resize(count, 0);
            this._advance = 0;
            this._bounds  = null;

            int wordstart = start == buff.size
                ? start
                : LayoutUtils.getPrevWordBreakForCache(buff, start + 1);
            int wordend;

            for (int iter = start; iter < start + count; iter = wordend)
            {
                wordend = LayoutUtils.getNextWordBreakForCache(buff, iter);
                int wordCount = Math.Min(start + count, wordend) - iter;
                this.layoutWord(offset, iter - start, buff.subBuff(wordstart, wordend - wordstart),
                                iter - wordstart, wordCount, style);
                wordstart = wordend;
            }
            this._count = count;
        }
Ejemplo n.º 7
0
        public void doLayout(float offset, TextBuff buff, int start, int count, TextStyle style)
        {
            this._start = start;
            this._count = count;
            this._advances.reset(count);
            this._positions.reset(count);
            this._advance = 0;
            this._bounds  = default;

            Font font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;

            char startingChar = buff.text[buff.offset + start];

            if (char.IsHighSurrogate(startingChar) || EmojiUtils.isSingleCharEmoji(startingChar))
            {
                this.layoutEmoji(buff.text.Substring(buff.offset + start, count), style, font, start, count);
            }
            else
            {
                font.RequestCharactersInTextureSafe(buff.text, style.UnityFontSize, style.UnityFontStyle);

                int wordstart = start == buff.size
                    ? start
                    : LayoutUtils.getPrevWordBreakForCache(buff, start + 1);
                int wordend;
                for (int iter = start; iter < start + count; iter = wordend)
                {
                    wordend = LayoutUtils.getNextWordBreakForCache(buff, iter);
                    int wordCount = Mathf.Min(start + count, wordend) - iter;
                    this.layoutWord(offset, iter - start, buff.subBuff(wordstart, wordend - wordstart),
                                    iter - wordstart, wordCount, style, font);
                    wordstart = wordend;
                }
            }
            this._count = count;
        }
Ejemplo n.º 8
0
        void layoutWord(float offset, int layoutOffset,
                        TextBuff buff, int start, int wordCount, TextStyle style, Font font)
        {
            float wordSpacing =
                wordCount == 1 && LayoutUtils.isWordSpace(buff.charAt(start)) ? style.wordSpacing : 0;

            float x                    = this._advance;
            float letterSpace          = style.letterSpacing;
            float letterSpaceHalfLeft  = letterSpace * 0.5f;
            float letterSpaceHalfRight = letterSpace - letterSpaceHalfLeft;

            for (int i = 0; i < wordCount; i++)
            {
                var ch = buff.charAt(start + i);
                if (i == 0)
                {
                    x += letterSpaceHalfLeft + wordSpacing;
                    this._advances[i + layoutOffset] += letterSpaceHalfLeft + wordSpacing;
                }
                else
                {
                    this._advances[i - 1 + layoutOffset] += letterSpaceHalfRight;
                    this._advances[i + layoutOffset]     += letterSpaceHalfLeft;
                    x += letterSpace;
                }

                if (font.getGlyphInfo(ch, out var glyphInfo, style.UnityFontSize, style.UnityFontStyle))
                {
                    var minX = glyphInfo.minX + x;
                    var maxX = glyphInfo.maxX + x;
                    var minY = -glyphInfo.maxY;
                    var maxY = -glyphInfo.minY;

                    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 + layoutOffset] = x;
                float advance = glyphInfo.advance;
                if (ch == '\t')
                {
                    advance = this._tabStops.nextTab((this._advance + offset)) - this._advance;
                }
                x += advance;
                this._advances[i + layoutOffset] += advance;
                if (i + 1 == wordCount)
                {
                    this._advances[i + layoutOffset] += letterSpaceHalfRight;
                    x += letterSpaceHalfRight;
                }
            }

            this._advance = x;
        }