Esempio n. 1
0
        public float DrawText(SpriteBatch batch, float x, float y, string str, float depth, Color color, Vector2 origin, float scaleX, float scaleY, SpriteEffects spriteEffects)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(0.0f);
            }

            var collection = GetGlyphsCollection(FontSize);

            // Determine ascent and lineHeight from first character
            float ascent = 0, lineHeight = 0;

            for (var i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                var glyph = GetGlyph(batch.GraphicsDevice, collection, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                ascent     = glyph.Font.Ascent;
                lineHeight = glyph.Font.LineHeight;
                break;
            }

            var q = new FontGlyphSquad();

            var originX = -origin.X;
            var originY = -origin.Y;

            originY += ascent;

            FontGlyph prevGlyph = null;

            for (var i = 0; i < str.Length; i += char.IsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = char.ConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    originX   = 0.0f;
                    originY  += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(batch.GraphicsDevice, collection, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, collection, Spacing, ref originX, ref originY, &q);

                var sourceRect = new Rectangle((int)(q.S0 * _size.X),
                                               (int)(q.T0 * _size.Y),
                                               (int)((q.S1 - q.S0) * _size.X),
                                               (int)((q.T1 - q.T0) * _size.Y));

                batch.Draw(glyph.Atlas.Texture,
                           new Vector2(x + q.X0 * scaleX, y + q.Y0 * scaleY),
                           sourceRect,
                           color,
                           0f,
                           Vector2.Zero,
                           new Vector2(scaleX, scaleY),
                           spriteEffects,
                           depth);

                prevGlyph = glyph;
            }

            return(x);
        }
Esempio n. 2
0
        public float TextBounds(float x, float y, StringBuilder str, ref Bounds bounds)
        {
            if (str == null || str.Length <= 0)
            {
                return(0.0f);
            }

            var collection = GetGlyphsCollection(FontSize);

            // Determine ascent and lineHeight from first character
            float ascent = 0, lineHeight = 0;

            for (var i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                var glyph = GetGlyph(null, collection, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                ascent     = glyph.Font.Ascent;
                lineHeight = glyph.Font.LineHeight;
                break;
            }


            var   q       = new FontGlyphSquad();
            float startx  = 0;
            float advance = 0;

            y += ascent;

            float minx, maxx, miny, maxy;

            minx   = maxx = x;
            miny   = maxy = y;
            startx = x;

            FontGlyph prevGlyph = null;

            for (var i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    x         = startx;
                    y        += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(null, collection, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, collection, Spacing, ref x, ref y, &q);
                if (q.X0 < minx)
                {
                    minx = q.X0;
                }
                if (x > maxx)
                {
                    maxx = x;
                }
                if (q.Y0 < miny)
                {
                    miny = q.Y0;
                }
                if (q.Y1 > maxy)
                {
                    maxy = q.Y1;
                }

                prevGlyph = glyph;
            }

            advance = x - startx;

            bounds.X  = minx;
            bounds.Y  = miny;
            bounds.X2 = maxx;
            bounds.Y2 = maxy;

            return(advance);
        }
Esempio n. 3
0
        public float DrawText(SpriteBatch batch, float x, float y, StringBuilder str, float depth)
        {
            if (str.Length == 0)
            {
                return(0.0f);
            }

            var collection = GetGlyphsCollection(FontSize);

            // Determine ascent and lineHeight from first character
            float ascent = 0, lineHeight = 0;

            for (var i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                var glyph = GetGlyph(batch.GraphicsDevice, collection, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                ascent     = glyph.Font.Ascent;
                lineHeight = glyph.Font.LineHeight;
                break;
            }

            var q = new FontGlyphSquad();

            var originX = 0.0f;
            var originY = 0.0f;

            originY += ascent;

            FontGlyph prevGlyph = null;

            for (var i = 0; i < str.Length; i += StringBuilderIsSurrogatePair(str, i) ? 2 : 1)
            {
                var codepoint = StringBuilderConvertToUtf32(str, i);

                if (codepoint == '\n')
                {
                    originX   = 0.0f;
                    originY  += lineHeight;
                    prevGlyph = null;
                    continue;
                }

                var glyph = GetGlyph(batch.GraphicsDevice, collection, codepoint);
                if (glyph == null)
                {
                    continue;
                }

                GetQuad(glyph, prevGlyph, collection, Spacing, ref originX, ref originY, &q);

                q.X0 = (int)(q.X0 * Scale.X);
                q.X1 = (int)(q.X1 * Scale.X);
                q.Y0 = (int)(q.Y0 * Scale.Y);
                q.Y1 = (int)(q.Y1 * Scale.Y);

                var destRect = new Rectangle((int)(x + q.X0),
                                             (int)(y + q.Y0),
                                             (int)(q.X1 - q.X0),
                                             (int)(q.Y1 - q.Y0));

                var sourceRect = new Rectangle((int)(q.S0 * _size.X),
                                               (int)(q.T0 * _size.Y),
                                               (int)((q.S1 - q.S0) * _size.X),
                                               (int)((q.T1 - q.T0) * _size.Y));

                batch.Draw(glyph.Atlas.Texture,
                           destRect,
                           sourceRect,
                           Color,
                           0f,
                           Vector2.Zero,
                           SpriteEffects.None,
                           depth);

                prevGlyph = glyph;
            }

            return(x);
        }