/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */
#ifndef KEOS_FONTMANAGER_H
#define KEOS_FONTMANAGER_H

#include "KeosPrerequisites.h"
#include "KeosSingleton.h"
#include "KeosDeclaration.h"
#include "KeosTexture.h"
#include "KeosBuffer.h"

namespace Keos
{
    //-----------------------------------------------------------------------
    /** Font manager.
     */
    class KEOS_EXPORT CFontManager : public CSingleton<CFontManager>
    {
    friend class CSingleton<CFontManager>;

    public :

        /** Load a font.
         *  @param strFontFile Name of the file with the font.
		 *	@param strFontName Name of the font.
         *  @param nQuality Size of the font.
         */
        void LoadFont(const String& strFontFile, const String& strFontName, int nQuality = 16);

        /** Unload all the fonts.
         */
        void UnloadFonts();

        /** Draw a string on the screen.
         *  @param String Graphic string to be displayed.
         */
        void DrawString(const CGraphicString& String);

        /** Return the size of a graphic string in pixels.
         *  @param String Graphic string.
         *  @return Size of the string in pixels.
         */
        TVector2I GetStringPixelSize(const CGraphicString& String);

		/** Return the height (pixels) of the font.
		 */
		size_t GetFontHeight(const String& strFontName);

    private :

        /** Default constructor.
         */
        CFontManager();

        /** Destructor.
         */
        ~CFontManager();

        /** Initialize data.
         */
        void Initialize();

        //! Structure of the vertices
        struct TVertex
        {
            TVector3F   Position;     //! Position
            ulong       Diffuse;      //! Diffuse color
            TVector2F   TexCoords;    //! Texture coordinates
        };

        //! Indices type
        typedef ushort TIndex;

        //! Glyph structure
        struct Glyph
        {
            float Tex_x1, Tex_y1, Tex_x2;
            size_t Advance;
        };

        //! Font structure
        struct TFont
        {
            //! Texture with the character of the font
            CTexture Texture;  
            //! An array to store the glyphs
            Glyph* Glyphs;
            //! A table to quickly get the glyph belonging to a character
            Glyph* GlyphsTable[256];
            //! Height of one line in the texture
            size_t LineHeight;
            //! Height of one line in the texture (texture coordinate)
            float TextureLineHeight;
        };

        typedef std::map<String, TFont> TFontsMap;

        static const unsigned int NbCharMax = 512;

        TFontsMap        m_Fonts;        //! Map containing the textures of fonts, associated to their name
        CBuffer<TVertex> m_VertexBuffer; //! Vertex buffer
        CBuffer<TIndex>  m_IndexBuffer;  //! Index buffer
        CDeclarationPtr  m_Declaration;  //! Vertex declaration
    };

} // namespace Keos

#endif // KEOS_FONTMANAGER_H
