Beispiel #1
0
        // Todo: Optimize drawing text.
        public void Draw(SpriteBatch _spriteBatch, string text, Vector2 position, Color color, float rotation, float size = 1)
        {
            if (isDistanceField)
            {
                distanceFontEffect.Parameters["scale"].SetValue(size);
                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, SamplerState.AnisotropicClamp, null, null, distanceFontEffect);
            }

            float x = position.X;
            float y = position.Y;

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

                if (ch == '\n' || ch == '\r')
                {
                    x = position.X;
                    y += (Info.LineHeight + Info.SpacingV) * size;
                    continue;
                }
                else if (ch == '\t')
                {
                    CharInfo spaceInfo = Info.CharInfos[' '];
                    x += (spaceInfo.XAdvance + Info.SpacingH) * size * 4;
                    continue;
                }

                if (!Info.CharInfos.ContainsKey(ch))
                {
                    ch = Info.CharInfos.ContainsKey('?') ? '?' : ' ';
                }

                CharInfo chInfo = Info.CharInfos[ch];
                var chKern = new KerningPair();
                if (i < text.Length - 1 && Info.HasKerning(text[i], text[i + 1]))
                {
                    chKern = Info.GetKerning(text[i], text[i + 1]);
                }

                var source = new Rectangle(chInfo.X, chInfo.Y, chInfo.Width, chInfo.Height);
                var texture = pageTextures[chInfo.Page];

                var drawPosition = new Vector2(x + (chInfo.XOffset * size), y + (chInfo.YOffset * size));

                if (isDistanceField)
                    spriteBatch.Draw(texture, drawPosition, source, color, 0f, Vector2.Zero, size, SpriteEffects.None, 0);
                else
                    _spriteBatch.Draw(texture, drawPosition, source, color, 0f, Vector2.Zero, size, SpriteEffects.None, 0);

                x += (chInfo.XAdvance + chKern.Amount + Info.SpacingH) * size;
            }

            if (isDistanceField)
                spriteBatch.End();
        }
Beispiel #2
0
        // Todo: Optimize measuring text.
        public Vector2 MeasureString(string text)
        {
            int width = 0;
            int longestWidth = 0;
            int height = Info.Base;

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

                if (ch == '\n' || ch == '\r')
                {
                    if (width > longestWidth)
                        longestWidth = width;

                    width = 0;
                    height += Info.LineHeight + Info.SpacingV;
                    continue;
                }
                else if (ch == '\t')
                {
                    CharInfo spaceInfo = Info.CharInfos[' '];
                    width += (spaceInfo.XAdvance + Info.SpacingH) * 4;
                    continue;
                }

                if (!Info.CharInfos.ContainsKey(ch))
                {
                    ch = Info.CharInfos.ContainsKey('?') ? '?' : ' ';
                }

                CharInfo chInfo = Info.CharInfos[ch];
                var chKern = new KerningPair();
                if (i < text.Length - 1 && Info.HasKerning(text[i], text[i + 1]))
                {
                    chKern = Info.GetKerning(text[i], text[i + 1]);
                }

                width += chInfo.XAdvance + chKern.Amount + Info.SpacingH;
            }

            longestWidth = width;
            return new Vector2(longestWidth, height);
        }