Exemplo n.º 1
0
        public Vector4 DrawString(SpriteBatch batch, Vector2 location, string text, Color color, float scale, float depth)
        {
            var loc     = location;
            var maxLocX = loc.X;

            for (int i = 0; i < text.Length; i++)
            {
                char c = text [i];
                if (c == '\n')
                {
                    loc.X  = location.X;
                    loc.Y += scale * Glyphs [' '].Height;
                }
                else
                {
                    if (!Glyphs.ContainsKey(c))
                    {
                        c = '?';
                    }
                    var rect = Glyphs [c];
                    rect.X += TextureOffsetX;
                    rect.Y += TextureOffsetY;
                    if (batch != null)
                    {
                        batch.Draw(Texture, loc, rect, color, 0.0f, Vector2.Zero, scale, SpriteEffects.None, depth);
                    }
                    loc.X  += scale * rect.Width;
                    maxLocX = Math.Max(loc.X, maxLocX);
                }
            }
            return(new Vector4(location, maxLocX, loc.Y + scale * Glyphs [' '].Height));
        }
Exemplo n.º 2
0
        private void DrawTextureAtlas(System.Drawing.Font font)
        {
            using (var g = System.Drawing.Graphics.FromImage(Image))
            {
                g.Clear(System.Drawing.Color.Transparent);
                g.SmoothingMode     = SmoothingMode.HighQuality;
                g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                var brString  = new SolidBrush(System.Drawing.Color.White);
                var brOutline = new SolidBrush(System.Drawing.Color.Black);

                for (int i = 0; i < chars.Length; i++)
                {
                    if (Glyphs.ContainsKey(chars[i]))
                    {
                        continue;
                    }

                    var x    = locations[i].X;
                    var y    = locations[i].Y;
                    var size = sizes[i];

                    var c = chars[i].ToString();

                    if (Outline)
                    {
                        g.DrawString(c, font, brOutline, x - 1, y, StringFormat.GenericTypographic);
                        g.DrawString(c, font, brOutline, x + 1, y, StringFormat.GenericTypographic);
                        g.DrawString(c, font, brOutline, x, y - 1, StringFormat.GenericTypographic);
                        g.DrawString(c, font, brOutline, x, y + 1, StringFormat.GenericTypographic);
                    }
                    g.DrawString(c, font, brString, x, y, StringFormat.GenericTypographic);

                    var glyph = new Glyph
                    {
                        Size = size,
                        UV   = new[]
                        {
                            new Vector2(x / Image.Width, y / Image.Height),
                            new Vector2(
                                (x + size.Width) / Image.Width,
                                (y + size.Height) / Image.Height
                                ),
                        },
                        Code = chars[i]
                    };

                    Glyphs.Add(chars[i], glyph);
                }

                brString.Dispose();
                brOutline.Dispose();
            }

            Image.Save(string.Format("{0} {1}.png", font.FontFamily.Name, (int)font.Size), ImageFormat.Png);
        }