using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace FontCreator
{
    public partial class Form1 : Form
    {
        [DllImport("gdi32")]
        static extern void GetCharWidth32(IntPtr hdcImage, int firstChar, int lastChar, out int charWidth);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            const int size = 10;
            const string name = "Tahoma", outFile = "segoesmall";
            const int spaceBetweenChars = 4, horizontalOffset = -1, verticalSpaceAdd = 2, verticalOffset = -3;

            int totalWidth = 0, currentWidth = 0;
            Font f = new Font(name, size, FontStyle.Regular, GraphicsUnit.Pixel);
            StreamWriter w = new StreamWriter(outFile + ".txt");

            CreateGraphics().GetHdc();

            w.WriteLine(size);
            for (int i = 0; i < 256; i++)
            {
                int val = TextRenderer.MeasureText(" " + (char)i, f).Width - TextRenderer.MeasureText(" ", f).Width;
                w.WriteLine(val);
                totalWidth += val + spaceBetweenChars;
            }

            Bitmap b = new Bitmap(totalWidth, size + verticalSpaceAdd);
            Graphics g = Graphics.FromImage(b);

            g.Clear(Color.Black);
            for (int i = 0; i < 256; i++)
            {
                char c = (char)i;
                g.DrawString("" + c, f, new SolidBrush(Color.White), horizontalOffset + currentWidth, verticalOffset);
                currentWidth += spaceBetweenChars + TextRenderer.MeasureText(" " + c, f).Width - TextRenderer.MeasureText(" ", f).Width;
            }
            for (int i = 0; i < b.Width; i++)
                for (int j = 0; j < b.Height; j++)
                    b.SetPixel(i, j, Color.FromArgb(b.GetPixel(i, j).G << 24 | 0xffffff));
//            this.CreateGraphics().DrawImage(b, new Point(0, 0));
            b.Save(outFile + ".png");
            w.Close();
        }
    }
}
