Пример #1
0
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is <see langword="null"/></exception>
        internal Vector2 ProcessChars(string text, EachChar eachChar, Action newLineBegins)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            var  currentX      = 0;
            var  currentY      = 0;
            var  maxLineWidth  = 0;
            var  maxCharHeight = LineSpacing;
            char?previous      = null;

            foreach (var c in text)
            {
                if (c == '\r')
                {
                    continue;
                }
                if (c == '\n')
                {
                    currentX     -= (int)Spacing;
                    maxLineWidth  = Math.Max(maxLineWidth, currentX);
                    currentX      = 0;
                    currentY     += maxCharHeight;
                    maxCharHeight = LineSpacing;
                    newLineBegins?.Invoke();
                    continue;
                }

                var actual   = c;
                var charData = Characters.GetValue(actual);
                if (charData == null)
                {
                    if (!DefaultCharacter.HasValue)
                    {
                        throw new ArgumentException("Text contains characters that cannot be resolved by this font.",
                                                    nameof(text));
                    }

                    charData = Characters.GetValue(DefaultCharacter.Value);
                    actual   = DefaultCharacter.Value;
                }

                var kerning = 0;
                if (previous.HasValue)
                {
                    kerning = GetKerning(previous.Value, c);
                }

                var position = new Vector2(currentX + charData.XOffset + kerning, currentY + charData.YOffset);
                eachChar?.Invoke(actual, position, charData, previous);

                previous  = actual;
                currentX += charData.XAdvance + kerning + (int)Spacing;
                if (charData.Height > maxCharHeight)
                {
                    maxCharHeight = charData.Height;
                }
            }

            currentY += maxCharHeight;
            return(new Vector2(Math.Max(maxLineWidth, currentX), currentY));
        }
Пример #2
0
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is <see langword="null"/></exception>
        internal Vector2 ProcessChars(string text, EachChar eachChar, Action newLineBegins)
        {
            if (text == null) {
                throw new ArgumentNullException(nameof(text));
            }

            var currentX = 0;
            var currentY = 0;
            var maxLineWidth = 0;
            var maxCharHeight = LineSpacing;
            char? previous = null;

            foreach (var c in text) {
                if (c == '\r') {
                    continue;
                }
                if (c == '\n') {
                    currentX -= (int)Spacing;
                    maxLineWidth = Math.Max(maxLineWidth, currentX);
                    currentX = 0;
                    currentY += maxCharHeight;
                    maxCharHeight = LineSpacing;
                    newLineBegins?.Invoke();
                    continue;
                }

                var actual = c;
                var charData = Characters.GetValue(actual);
                if (charData == null) {
                    if (!DefaultCharacter.HasValue) {
                        throw new ArgumentException("Text contains characters that cannot be resolved by this font.",
                            nameof(text));
                    }

                    charData = Characters.GetValue(DefaultCharacter.Value);
                    actual = DefaultCharacter.Value;
                }

                var kerning = 0;
                if (previous.HasValue) {
                    kerning = GetKerning(previous.Value, c);
                }

                var position = new Vector2(currentX + charData.XOffset + kerning, currentY + charData.YOffset);
                eachChar?.Invoke(actual, position, charData, previous);

                previous = actual;
                currentX += charData.XAdvance + kerning + (int)Spacing;
                if (charData.Height > maxCharHeight) {
                    maxCharHeight = charData.Height;
                }
            }

            currentY += maxCharHeight;
            return new Vector2(maxLineWidth, currentY);
        }