/**
 ** Copyright (c) 2006 Ingo Ruhnke <grumbel@gmail.com>
 **
 ** This software is provided 'as-is', without any express or implied
 ** warranty. In no event will the authors be held liable for any
 ** damages arising from the use of this software.
 **
 ** Permission is granted to anyone to use this software for any
 ** purpose, including commercial applications, and to alter it and
 ** redistribute it freely, subject to the following restrictions:
 **
 **   1. The origin of this software must not be misrepresented; you
 **      must not claim that you wrote the original software. If you
 **      use this software in a product, an acknowledgment in the
 **      product documentation would be appreciated but is not
 **      required.
 **
 **   2. Altered source versions must be plainly marked as such, and
 **      must not be misrepresented as being the original software.
 **
 **   3. This notice may not be removed or altered from any source
 **      distribution.
 **
 */

#ifndef _SDL_FNT_H
#define _SDL_FNT_H

#include <SDL.h>

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif

/**
 */
typedef struct TTY_Font
{
  /**
   *  Surface containing the glyphs, glyphs have to be fixed width and
   *  run from left to right, separating the glyphs to multiple lines
   *  is allowed
   */
  SDL_Surface* surface;

  /**
   *  Map characters to there index inside the font file,
   *  ie. translable['a'] will give the index of 'a'
   */
  char transtbl[256];

  int  glyph_width;
  int  glyph_height;
}  TTY_Font;

/** Create a font from a .h file generated by fontdump, \a name is the
    unquoted name of the font. A TTY_Font* gets returned. */
#define FNT_CreateFromData(name) \
  FNT_Create(SDL_CreateRGBSurfaceFrom( name##_data,                 \
                                           name##_width, name##_height, name##_bpp, name##_pitch, \
                                           name##_rmask, name##_gmask,  name##_bmask, name##_amask ), \
                 name##_glyph_width, name##_glyph_height,               \
                 name##_glyphs)

/**
 *  Creates a font from an SDL_Surface. The letter with index 0 is at
 *  the top/left of the image,
 *
 *  @param surface      The SDL_Surface that contains all letters
 *  @param glyph_width  The width of a glyph
 *  @param glyph_height The height of a glyph
 *  @param letters      The letters that are present in the font
 */
TTY_Font* FNT_Create(SDL_Surface* surface, int glyph_width, int glyph_height, const char* letters);
void      FNT_Free(TTY_Font* font);

/**
 *  Calculate the position of character \a idx in the surface used by
 *  \a font and write the result to \a rect
 */
void FNT_GetGlyph(TTY_Font* font, char idx, SDL_Rect* rect);

enum {
  FNT_ALIGN_LEFT     = (1<<0),
  FNT_ALIGN_RIGHT    = (1<<1),
  FNT_ALIGN_H_CENTER = FNT_ALIGN_LEFT | FNT_ALIGN_RIGHT,

  FNT_ALIGN_TOP      = (1<<2),
  FNT_ALIGN_BOTTOM   = (1<<3),
  FNT_ALIGN_V_CENTER = FNT_ALIGN_TOP | FNT_ALIGN_BOTTOM,

  FNT_ALIGN_CENTER = FNT_ALIGN_H_CENTER | FNT_ALIGN_V_CENTER
};

/**
 * Return the width of the glyph in pixels
 */
int FNT_GetGlyphWidth(TTY_Font* font, char c);

/**
 * Return the width of the glyph in pixels
 */
int FNT_GetGlyphHeight(TTY_Font* font, char c);

/**
 * Return the height, ie. lines * font_height, of a given text in
 * pixel
 */
int FNT_GetTextHeight(TTY_Font* font, const char* text);

/**
 * Return the width, ie. the width of the longest line, of a given
 * text in pixel
 */
int FNT_GetTextWidth(TTY_Font* font, const char* text);

/**
 * Return the width of the given line (everything after a \0 or a \n is ignored)
 */
int FNT_GetTextLineWidth(TTY_Font* font, const char* text);

/**
 *  Print the given string to the screen at the given coordinates using \a font.
 */
void FNT_Print(TTY_Font* font, SDL_Surface* screen, int x, int y, Uint32 flags, const char *str);

/**
 *  Print to the screen at the given coordinates, while handling \a
 *  fmt in a printf like manner
 */
void FNT_Printf(TTY_Font* font, SDL_Surface* screen, int x, int y, Uint32 flags, const char *fmt, ...)
  __attribute__ ((format (printf, 6, 7)));

/** Formats the format string \a fmt and returns the result, the user
    must free() the result  */
char* FNT_create_string(const char *fmt, va_list ap);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif

#endif

/* EOF */
