/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


#ifndef	__FONTRENDERER__
#define	__FONTRENDERER__


		#include "font/font_extended.h"
		#include "font/font_cache.h"
		#include "math/vector.h"


namespace GS {
	class Picture;
	struct SubString;

/// Text widget state structure.
class	TextState
{
		int			size;
		float		tracking,			///< Text tracking (horizontal offset between two characters).
					leading;			///< Text heading (vertical offset between lines).

public:

enum	Format
{
		Line = 0,
		Paragraph,
		Column
};

enum	Alignment
{
		Left = 0,
		Right,
		Center,
		Justify
};

		sFontEx		font;
		Color		color;

		Format		format;				///< Text format.
		Alignment	alignment;			///< Text alignment.
		int			column_width;		///< Text column width.

		int			GetSize() const { return font.IsValid() ? (int)(size * font->size_multiplier) : size; }
		float		GetTracking() const { return font.IsValid() ? tracking + font->tracking_offset : tracking; }
		float		GetLeading() const { return font.IsValid() ? leading + font->leading_offset : leading; }

		void		SetSize(int s) { size = s; }
		void		SetTracking(float t) { tracking = t; }
		void		SetLeading(float l) { leading = l; }

		TextState()
		{
			size = 16;
			color = Color::Black;

			format = Line;
			alignment = Left;

			column_width = 80;
			tracking = 0;
			leading = 0;
		}
};

/*
	@short	Font renderer.
	@author	Emmanuel Julien (ejulien@nworks.fr)
*/
class	FontRenderer
{
public:

enum	CommandCode
{
		CommandNone = 0,
		CommandColor,
		CommandFont,
		CommandSize,

		CommandParseError
};

static	float		default_text_tracking;
static	float		default_text_heading;

/// Renderer command.
struct	Command
{
		CommandCode	code;
		Vector4		vector;
		sIFont		font;
};

protected:

		/// Check current string for a command.
static	const char	*CheckCommand(const char *string, Command &command);
		/// Get the next sub-string from the current text buffer.
static	const char	*FetchSubString(const char *string, SubString &sstr, TextState &state, int max_width, int max_character);
		/// Draw a substring to a picture.
static	void		DrawSubString(SubString &sub_string, Picture &output, TextState &state, const iRect &out_rect, const iRect &clip_rect, int justification);

public:

		/// Format a string using a text state, return the required output rectangle.
static	iRect		Format(const char *text, const TextState &in_state, const iRect &out_rect);
		/// Compose a string using a text state.
static	iRect		Compose(Picture &output, const char *text, const TextState &state, const iRect &out_rect, const iRect &clip_rect);
};

} // GS


#endif	// __FONTRENDERER__
