private void OutputFiles(string assetsFolderPath, SpriteFontBuildSpec buildSpec, Bitmap bmp, List <CharacterSprite> charSprites) { string textureFilePath = Path.Combine(assetsFolderPath, buildSpec.textureRelativeOutputFilePath); EnsureDirectoryExists(textureFilePath); bmp.Save(textureFilePath, ImageFormat.Png); string ssFilePath = Path.Combine(assetsFolderPath, buildSpec.ssRelativeOutputFilePath); EnsureDirectoryExists(ssFilePath); using (var sw = new StreamWriter(ssFilePath, false, Encoding.ASCII)) { sw.WriteLine("texture-asset-ref " + buildSpec.textureRelativeOutputFilePath); foreach (var charSprite in charSprites) { string c = charSprite.c.ToString(); if (c == " ") { c = "SPACE"; } sw.WriteLine("sprite " + c + " " + charSprite.x + " " + charSprite.y + " " + charSprite.width + " " + charSprite.height); } } }
private void BuildSpriteFont(string assetsFolderPath, SpriteFontBuildSpec buildSpec) { var bmp = new Bitmap(buildSpec.textureWidth, buildSpec.textureHeight); var gfx = Graphics.FromImage(bmp); var font = new Font(buildSpec.fontName, buildSpec.fontEmSize); var brush = new SolidBrush(Color.FromArgb(buildSpec.fontColour.red, buildSpec.fontColour.green, buildSpec.fontColour.blue)); gfx.Clear(Color.Transparent); gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; var chars = new List <char>(); for (int i = 32; i < 127; i++) { chars.Add((char)i); } var charSprites = new List <CharacterSprite>(); var charPoint = new Point(0, 0); foreach (var c in chars) { // The dimensions come out wierd for spaces, so use 'i' instead. string measurementString = c != ' ' ? c.ToString() : 'i'.ToString(); string renderString = c.ToString(); var charSize = gfx.MeasureString(measurementString, font, new Point(0, 0), StringFormat.GenericTypographic); if (charPoint.X + charSize.Width > bmp.Width) { charPoint.X = 0; charPoint.Y += (int)charSize.Height; } gfx.DrawString(renderString, font, brush, charPoint, StringFormat.GenericTypographic); var charSprite = new CharacterSprite(); charSprite.c = c; charSprite.x = charPoint.X; charSprite.y = charPoint.Y; charSprite.width = (int)charSize.Width; charSprite.height = (int)charSize.Height; charSprites.Add(charSprite); charPoint.X += (int)charSize.Width; } OutputFiles(assetsFolderPath, buildSpec, bmp, charSprites); }