/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM 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 General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#ifndef _BITMAPFONT_HPP_
#define _BITMAPFONT_HPP_

#include "nmm/nmm_prefix.hpp"

#include "BitmapReader.hpp"
#include "nmm/Result.hpp"

namespace NMM {

  class BitmapFont{
    typedef struct {
      unsigned int   width;
      unsigned int   height;
      unsigned char* pixeldata;
    } char_t;

  private:
    char_t       chars[256];

    /* the maximum width of a character */
    unsigned int max_width;

    /* the maximum height of a character */
    unsigned int max_height;

    /* space(pixels) between 2 lines */
    unsigned int line_space;

    /**
     * captures a given character out of a bitmap and places it into the chars structure
     * \param c index of the chars array
     * \param data pointer to a array with all rows of the font bitmap
     * \param xpos x position of the upper left corner where the character begins in the bitmap
     * \param ypos y position of the upper left corner where the character begins
     * \param bitmapwidth width of the source bitmap
     * \param width width of the bitmap character
     * \param height height of the bitmap character
     */
    void captureChar( unsigned char c, unsigned char* data, unsigned int bitmapwidth, int xpos, int ypos, int width, int height );

    /**
     * deallocates memory of the bitmapdata of the given character and fills chars structure with zeros
     * \param c character that should be deleted
     */
    void freeChar( unsigned char c );

  public:
    BitmapFont();
    ~BitmapFont();

    /**
     * loads a font that is described in a XML file
     * \param xmfilename filename of the XML file where the font is described
     * \param fontname as a XML font file can describe several fonts, this parameter describes
     *        the desired fontname, if this name is empty, the first font is loaded
     */
    Result load( std::string xmlfilename, std::string fontname );

    /**
     * gets width and height of a character
     * \param c character that dimension is desired
     * \param x here the width is saved
     * \param y here the height is saved
     */
    void checkDimension( unsigned char c, unsigned int *x, unsigned int *y );

    /**
     * gets a pointer to the pixel data of a character
     * \param c character that pixel data is needed
     * \returns pointer to the pixel data
     */
    unsigned char* getPixelData( unsigned char c );

    /**
     * \returns the maximum width of a character
     */
    unsigned int getMaxWidth();

    /**
     * \returns the maximum height of a character
     */
    unsigned int getMaxHeight();

    /**
     * set space (pixels) between 2 lines
     */
    void setLineSpace( unsigned int space );

    /**
     * get line space
     */
    unsigned int getLineSpace();

  };

}

#include "nmm/nmm_suffix.hpp"

#endif
