/*
PMP Mod
Copyright (C) 2006 Raphael

E-mail:   raphael@fx-world.org

This program 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; 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
font rendering system
*/

#ifndef gu_font_h__
#define gu_font_h__


#define MAX_NAME_SIZE 256
#define MAX_CACHE_LINES 8
#define MAX_CACHE_STRING 512
#define MAX_GLYPH_CACHES 16


#define FLAG_NONE 0
#define FLAG_ALIGN_LEFT 0
#define FLAG_ALIGN_CENTER 1
#define FLAG_ALIGN_RIGHT 2
#define FLAG_ALIGN_MASK 3
#define FLAG_SHADOW 4
#define FLAG_NOCACHE 8			// Bypass glyph caching (for text that changes every frame)


#define FLAG_CLIP_WRAP 0x10
#define FLAG_CLIP_CLAMP 0x20


// Only for internal use
#define GLYPH_CACHE_VRAM 0x1000
#define GLYPH_CACHE_SYSMEM 0x2000


#define FOURCC(A,B,C,D) (unsigned long)((unsigned char)A << 24 | (unsigned char)B << 16 | (unsigned char)C << 8 | (unsigned char)D)

#define BGR444(col) ((col & 0xF0) >> 4) | ((col & 0xF000) >> 8) | ((col & 0xF00000) >> 12)
#define BGR444_blend(col,i) (((col & 0xF0)*i/15) >> 4) | (((col & 0xF000)*i/15) >> 8) | (((col & 0xF00000)*i/15) >> 12)


extern unsigned short gu_font_clut[16];

struct gu_char_struct {
	char		byte_width;
	char		byte_height;
	char		x_offset;
	char		y_offset;
	char		pixel_width;
	char		pixel_height;
};


struct gu_font_struct {
	char					name[MAX_NAME_SIZE];
	int						id;
	/*unsigned int			color;
	unsigned int			border_color;
	unsigned int			border_enable;*/

	struct gu_char_struct	chars[256];
	unsigned char*			data;
	int						d_width;				// Bytewidth of bitmap: currently always 128
	int						d_height;				// Byteheight of bitmap: currently always 256
};


struct gu_font_list_struct {
	struct gu_font_struct	*font;
	struct gu_font_list_struct	*next;
};


struct gu_font_manager_struct {
	struct gu_font_list_struct*	list;
	int			num_fonts;
	int			id_counter;
};


struct gu_font_header_struct {
	char	ID[4];		// 'GUFn' n = version number
	char	reserved[12];
};



// Glyph caching structures

struct gu_glyph_cache_struct {
	void*			cacheptr;
	int				size;
	int				width;
	int				height;
	int				flags;
	char			string[MAX_CACHE_STRING];
	int				font_id;
	int				dirty;
};


struct gu_glyph_cache_list_struct {
	struct gu_glyph_cache_struct 		*cache;
	unsigned long	lru_index;
	
	struct gu_glyph_cache_list_struct	*next;
	struct gu_glyph_cache_list_struct	*prev;
};


struct gu_glyph_cache_manager_struct {
	struct gu_glyph_cache_list_struct *root;
	unsigned long		lru_counter;
	unsigned int		num_caches;
};



struct VertexInt
{
	unsigned short u, v;
	unsigned short color;
	short x, y, z;
};


extern struct gu_font_struct* gu_cur_font;

// GLYPH CACHE SYSTEM FUNCTIONS
// ** normally there should be no need to call them manually
#ifdef DEBUG
void gu_debug_print_glyph_cache();
void gu_debug_print_charset();
#endif
char* gu_glyph_cache_init();
void gu_glyph_cache_free();
struct gu_glyph_cache_struct* gu_glyph_cache_manager_get( char* s, int width, int height, int flags, int font_id );


char* gu_font_init();
void gu_font_close();
char* gu_font_load( char* name );
void gu_font_free( char* name );
void gu_font_free_all();


void gu_font_set( char* name );
void* gu_font_texture_get( char* name );		// to be able to display the texture of a specific font

void gu_font_border_enable( int enable );
void gu_font_border_color_set( int color );
void gu_font_color_set( int color );
int gu_font_line_width_get( char* s );
int gu_font_width_get( char* s, int flag );
int gu_font_height_get( char* s );
int gu_font_height();
int gu_char_width_get( char c );
int gu_char_height_get( char c );

void gu_font_printf( int x, int y, int flags, char* fmt, ... );

#endif
