/*
	Bitmap Font Tool.
	Copyright (c) 2008, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Xml;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace BitmapFontTool
{
    static class Program
    {
        public static Form s_form = null;
        
        public static TextBox s_file_text_box = null;
        public static Button s_file_button = null;
        public static Label s_file_label = null;

        public static TextBox s_xna_file_text_box = null;
        public static Label s_xna_file_label = null;

        public static TextBox s_size_text_box = null;
        public static Label s_size_label = null;
        public static TextBox s_r_text_box = null;
        public static Label s_r_label = null;
        public static TextBox s_g_text_box = null;
        public static Label s_g_label = null;
        public static TextBox s_b_text_box = null;
        public static Label s_b_label = null;
        public static TextBox s_a_text_box = null;
        public static Label s_a_label = null;
        public static TextBox s_char_spacing_text_box = null;
        public static Label s_char_spacing_label = null;
        public static TextBox s_line_spacing_text_box = null;
        public static Label s_line_spacing_label = null;

        public static TextBox s_pixel_shader_text_box = null;
        public static Label s_pixel_shader_label = null;
        public static TextBox s_vertex_shader_text_box = null;
        public static Label s_vertex_shader_label = null;

        public static TextBox s_space_width_text_box = null;
        public static Label s_space_width_label = null;

        public static TextBox s_shader_transform_uniform_text_box = null;
        public static Label s_shader_transform_uniform_label = null;

        public static Button s_run = null;

            /// <summary> A structure representing the size and position of a character in the font. </summary>

            public struct CharacterMetrics
            {
                /// <summary> Position of the character in the texture (X). 0-1 texture coordinate. </summary>

                public float tc_x;

                /// <summary> Position of the character in the texture (Y). 0-1 texture coordinate. </summary>

                public float tc_y;

                /// <summary> Width of the character in the texture. 0-1 texture coordinate. </summary>

                public float tc_w;

                /// <summary> Height of the character in the texture. 0-1 texture coordinate. </summary>

                public float tc_h;
            }

         static Game game = new Game();

        static GraphicsDeviceManager gdm = new GraphicsDeviceManager(game);

        public static GraphicsDevice gd = null;

        [STAThread] static void Main(string[] args)
        {
            game.Window.Title = "BitmapFontTool font tool: Please close this window to proceed";
            game.Run();

            s_form = new Form(); 

            s_form.SetDesktopBounds(0,0,640,480);
            s_form.Show();
            s_form.Text = "BitmapFontTool XML font data Generator";

            s_file_label = new Label();
            s_file_label.SetBounds(20,20,100,20);
            s_file_label.Text = "Font texture file:";
            s_file_label.Parent = s_form;

            s_file_text_box = new TextBox();
            s_file_text_box.SetBounds(140,20,400,20);
            s_file_text_box.Parent = s_form;

            s_file_button = new Button();
            s_file_button.SetBounds(540,20,40,20);
            s_file_button.Text = "...";
            s_file_button.Parent = s_form;

            s_xna_file_label = new Label();
            s_xna_file_label.SetBounds(20,40,200,20);
            s_xna_file_label.Text = "Texture path (relative to game dir):";
            s_xna_file_label.Parent = s_form;

            s_xna_file_text_box = new TextBox();
            s_xna_file_text_box.SetBounds(240,40,300,20);
            s_xna_file_text_box.Parent = s_form;

            s_file_button.Click += new EventHandler(s_file_button_Click);

            s_size_label = new Label();
            s_size_label.SetBounds(20,60,100,20);
            s_size_label.Text = "Font size:";
            s_size_label.Parent = s_form;

            s_size_text_box = new TextBox();
            s_size_text_box.SetBounds(140,60,400,20);
            s_size_text_box.Parent = s_form;
            s_size_text_box.Text = "16";

            s_r_label = new Label();
            s_r_label.SetBounds(20,80,100,20);
            s_r_label.Text = "Font color r:";
            s_r_label.Parent = s_form;

            s_r_text_box = new TextBox();
            s_r_text_box.SetBounds(140,80,400,20);
            s_r_text_box.Parent = s_form;
            s_r_text_box.Text = "1";

            s_g_label = new Label();
            s_g_label.SetBounds(20,100,100,20);
            s_g_label.Text = "Font color g:";
            s_g_label.Parent = s_form;

            s_g_text_box = new TextBox();
            s_g_text_box.SetBounds(140,100,400,20);
            s_g_text_box.Parent = s_form;
            s_g_text_box.Text = "1";

            s_b_label = new Label();
            s_b_label.SetBounds(20,120,100,20);
            s_b_label.Text = "Font color b:";
            s_b_label.Parent = s_form;

            s_b_text_box = new TextBox();
            s_b_text_box.SetBounds(140,120,400,20);
            s_b_text_box.Parent = s_form;
            s_b_text_box.Text = "1";

            s_a_label = new Label();
            s_a_label.SetBounds(20,140,100,20);
            s_a_label.Text = "Font color a:";
            s_a_label.Parent = s_form;

            s_a_text_box = new TextBox();
            s_a_text_box.SetBounds(140,140,400,20);
            s_a_text_box.Parent = s_form;
            s_a_text_box.Text = "1";

            s_char_spacing_label = new Label();
            s_char_spacing_label.SetBounds(20,160,100,20);
            s_char_spacing_label.Text = "Font char spacing:";
            s_char_spacing_label.Parent = s_form;

            s_char_spacing_text_box = new TextBox();
            s_char_spacing_text_box.SetBounds(140,160,400,20);
            s_char_spacing_text_box.Parent = s_form;
            s_char_spacing_text_box.Text = "2";

            s_line_spacing_label = new Label();
            s_line_spacing_label.SetBounds(20,180,100,20);
            s_line_spacing_label.Text = "Font line spacing:";
            s_line_spacing_label.Parent = s_form;

            s_line_spacing_text_box = new TextBox();
            s_line_spacing_text_box.SetBounds(140,180,400,20);
            s_line_spacing_text_box.Parent = s_form;
            s_line_spacing_text_box.Text = "-4";

            s_pixel_shader_label = new Label();
            s_pixel_shader_label.SetBounds(20, 200, 200, 20);
            s_pixel_shader_label.Text = "Pixel shader (relative to game dir):";
            s_pixel_shader_label.Parent = s_form;

            s_pixel_shader_text_box = new TextBox();
            s_pixel_shader_text_box.SetBounds(240, 200, 300, 20);
            s_pixel_shader_text_box.Parent = s_form;
            s_pixel_shader_text_box.Text = "Shaders\\Textured.psh.bin";

            s_vertex_shader_label = new Label();
            s_vertex_shader_label.SetBounds(20, 220, 200, 20);
            s_vertex_shader_label.Text = "Vertex shader (relative to game dir):";
            s_vertex_shader_label.Parent = s_form;

            s_vertex_shader_text_box = new TextBox();
            s_vertex_shader_text_box.SetBounds(240, 220, 300, 20);
            s_vertex_shader_text_box.Parent = s_form;
            s_vertex_shader_text_box.Text = "Shaders\\Textured.vsh.bin";

            s_space_width_label = new Label();
            s_space_width_label.SetBounds(20, 240, 300, 20);
            s_space_width_label.Text = "White Space width (in character width percent, 0-1):";
            s_space_width_label.Parent = s_form;

            s_space_width_text_box = new TextBox();
            s_space_width_text_box.SetBounds(340, 240, 200, 20);
            s_space_width_text_box.Parent = s_form;
            s_space_width_text_box.Text = "0.35";

            s_shader_transform_uniform_label = new Label();
            s_shader_transform_uniform_label.SetBounds(20, 260, 300, 20);
            s_shader_transform_uniform_label.Text = "Shader 'transform' uniform variable name:";
            s_shader_transform_uniform_label.Parent = s_form;

            s_shader_transform_uniform_text_box = new TextBox();
            s_shader_transform_uniform_text_box.SetBounds(340, 260, 200, 20);
            s_shader_transform_uniform_text_box.Parent = s_form;
            s_shader_transform_uniform_text_box.Text = "Transform";

            s_run = new Button();
            s_run.SetBounds(20,290,400,40);
            s_run.Text = "Generate font xml descriptor";
            s_run.Parent = s_form;
            s_run.Click += new EventHandler( RunTool );

            while ( s_form.IsDisposed == false )
            {            
                System.Windows.Forms.Application.DoEvents();
            }
        }

        static void s_file_button_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            fd.Multiselect = false;
            fd.RestoreDirectory = true;
            fd.SupportMultiDottedExtensions = true;
            fd.Filter = "TGA textures (*.tga)|*.tga|Gif textures (*.gif)|*.gif|DDS Textures|*.dds";

            fd.FileName = "";
            fd.Title = "Select texture file for font";

            fd.ShowDialog();

            s_file_text_box.Text = fd.FileName;
            s_xna_file_text_box.Text = fd.FileName;

            int c_index = s_file_text_box.Text.LastIndexOf("Content\\",StringComparison.OrdinalIgnoreCase);

            if ( c_index >= 0 )
            {
                s_xna_file_text_box.Text = s_xna_file_text_box.Text.Substring( c_index + "Content\\".Length );

                int e_index = s_xna_file_text_box.Text.LastIndexOf(".",StringComparison.OrdinalIgnoreCase);

                if ( e_index >= 0 )
                {
                    s_xna_file_text_box.Text = s_xna_file_text_box.Text.Remove(e_index);
                }
            }
        }

        static void RunTool(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();

            sd.RestoreDirectory = true;
            sd.SupportMultiDottedExtensions = true;
            sd.Filter = "XML Font descriptors (*.xml)|*.xml";
            sd.Title = "Select xml to save font description and metrics to";

            sd.ShowDialog();

            try
            {
                XmlDocument doc = new XmlDocument();

                doc.AppendChild( doc.CreateElement("data") );

                XmlNode r = doc.FirstChild;

                {
                    XmlElement elem = doc.CreateElement("FontTexture");
                    elem.InnerText = s_xna_file_text_box.Text;
                    r.AppendChild(elem);
                }


                {
                    XmlElement elem = doc.CreateElement("FontPixelShader");
                    elem.InnerText = s_pixel_shader_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontVertexShader");
                    elem.InnerText = s_vertex_shader_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontSize");
                    elem.InnerText = s_size_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontCharSpacing");
                    elem.InnerText = s_char_spacing_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontLineSpacing");
                    elem.InnerText = s_line_spacing_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontColorR");
                    elem.InnerText = s_r_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontColorG");
                    elem.InnerText = s_g_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontColorB");
                    elem.InnerText = s_b_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("FontColorA");
                    elem.InnerText = s_a_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("WhiteSpacePercentWidth");
                    elem.InnerText = s_space_width_text_box.Text;
                    r.AppendChild(elem);
                }

                {
                    XmlElement elem = doc.CreateElement("ShaderTransformUniformVariable");
                    elem.InnerText = s_shader_transform_uniform_text_box.Text;
                    r.AppendChild(elem);
                }

                Texture2D texture = Texture2D.FromFile(game.GraphicsDevice ,s_file_text_box.Text);

                CharacterMetrics[] metrics = GenerateFontMetrics(texture);

                for ( int i = 0 ; i < metrics.Length ; i++ )
                {
                    XmlElement elem = doc.CreateElement("CharMetric");
                    elem.SetAttribute("Code",i.ToString());

                    elem.SetAttribute("U",metrics[i].tc_x.ToString());
                    elem.SetAttribute("V",metrics[i].tc_y.ToString());
                    elem.SetAttribute("S",metrics[i].tc_w.ToString());
                    elem.SetAttribute("T",metrics[i].tc_h.ToString());

                    r.AppendChild(elem);
                }

                doc.Save(sd.FileName);

            }
            catch ( Exception ex ){ MessageBox.Show("Exception raised!:" + ex.Message); }
        }

        public static CharacterMetrics[] GenerateFontMetrics( Texture2D texture )
        {
            // If we have no texture then abort:

            if ( texture == null ) return null;

            CharacterMetrics[] metrics = new CharacterMetrics[256];

            for ( int i = 0 ; i < 256 ; i++ )
            {
                int row = ( i >> 4 ) & 0x0F;
                int col = ( i      ) & 0x0F;

                metrics[i].tc_x = (float)(col) / 16.0f;
                metrics[i].tc_y = (float)(row) / 16.0f;
                
                metrics[i].tc_w = 1.0f / 16.0f;
                metrics[i].tc_h = 1.0f / 16.0f;
            }

            // Get the array of pixels from the texture:

            Color[] pixels = new Color[ texture.Width * texture.Height ]; 
                
                // Get the data
            
                texture.GetData<Color>( pixels );

            // Do each character in the char set:

            for ( int i = 0 ; i < 256 ; i++ )
            {
                // Calculate the row and column of this char in the texture

                int row = ( i >> 4 ) & 0x0F;
                int col = ( i      ) & 0x0F;

                // Calculate the width of each character in the texture:

                int w = texture.Width  >> 4;
                int h = texture.Height >> 4;
                
                // Calculate the x and y position of the top left pixel in the texture:

                int x = col * w;
                int y = row * h;

                // Calculate the right and bottom pixel boundaries for this character in the texture:

                int r = x + w;
                int b = y + h;

                // Now calculate the largest and smallest x and y values for the character 

                int s_x = x + w;
                int l_x = x;
                int s_y = y + h;
                int l_y = y;

                bool found_opaque_pixel = false;

                    // Run through all the pixels for this character:
                    
                    for ( int pos_x = x ; pos_x < r ; pos_x ++ )
                    for ( int pos_y = y ; pos_y < b ; pos_y ++ )
                    {
                        // See if this is a transparent pixel:

                        if ( ( pixels[ ( pos_y * texture.Width ) + pos_x ].A ) > 5 )
                        {
                            found_opaque_pixel = true;

                            // Not transparent: see if this x and y value is smaller or bigger than current:

                            if ( pos_x < s_x ) s_x = pos_x;
                            if ( pos_x > l_x ) l_x = pos_x;
                            if ( pos_y < s_y ) s_y = pos_y;
                            if ( pos_y > l_y ) l_y = pos_y;
                        }
                    }

                if ( found_opaque_pixel == false )
                {
                    s_x = ( x + r ) / 2;
                    l_x = ( x + r ) / 2;
                    s_y = ( y + b ) / 2;
                    l_y = ( y + b ) / 2;
                }

                
                // Save the metrics of this character: add one extra pixel of spacing to be safe

                s_x-=1;
                l_x+=1;
                s_y-=1;
                l_y+=1;

                metrics[i].tc_x = (float)( s_x        ) / (float)( texture.Width     );
                metrics[i].tc_y = (float)( s_y        ) / (float)( texture.Height    );
                metrics[i].tc_w = (float)( l_x - s_x  ) / (float)( texture.Width     );
                metrics[i].tc_h = (float)( l_y - s_y  ) / (float)( texture.Height    );
            }

            return metrics;
        }

    }
}

