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


		#include "squirrel_binding.h"
		#include "binding_helpers.h"
		#include "font/font_renderer.h"

		using namespace GS;
		using namespace GS::Script;


void				IterateTextParameters(HSQUIRRELVM vm, int idx, TextState &state);

//------------------------------------------------------------------------------
SQInteger			FontSetFallback(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(font, FontEx, typetag_Font)
	__SQ_GETSAFEPTR(fbck, FontEx, typetag_Font)
	__SQ_GETEND
	font->fallback = fbck;
	__SQ_RETURN
}
SQInteger			FontSetParametersOffset(HSQUIRRELVM vm)
{
	__SQ_GETSTART(4)
	__SQ_GETSAFEPTR(font, FontEx, typetag_Font)
	__SQ_GETFLOAT(size)
	__SQ_GETFLOAT(tracking)
	__SQ_GETFLOAT(leading)
	__SQ_GETEND
	font->size_multiplier = size;
	font->tracking_offset = tracking;
	font->leading_offset = leading;
	__SQ_RETURN
}
SQInteger			FontComputeRect(HSQUIRRELVM vm)
{
	__SQ_GETSTART(4)
	__SQ_GETRECT(clip_rect)
	__SQ_GETSTRING(text)
	__SQ_GETSAFEPTR(font, FontEx, typetag_Font)

	TextState state;
	IterateTextParameters(vm, __sq_stackpos, state);
	__SQ_GETUPDATESTACK

	state.font = font;
	iRect out_rect = FontRenderer::Format(text, state, clip_rect);

	__SQ_GETEND
	__SQ_RETURNRECT(out_rect)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void				RegisterFontBinding(HSQUIRRELVM vm)
{
/*#
	Topic: Font
	Type: Font
	Desc: A truetype font that can be used to render to a picture object.
	Related: Picture,Project
#*/
/*#
	Section: FontGeneral
	Desc: Font Settings
#*/
	/*#
		Func: FontSetParametersOffset
		Proto: void:Font,float size_multiplier,float tracking_offset,float leading_offset
		Desc: Set font-specific properties offset. This function is usually used to normalize font characteristics when porting an application to a different locale.
	#*/
	sq_register(vm, FontSetParametersOffset, "FontSetParametersOffset", _SC(".xnnn"));
	sq_register(vm, FontSetParametersOffset, "UIFontSetParametersOffset", _SC(".xnnn"));
	/*#
		Func: FontSetFallback
		Proto: void:Font font,Font fallback
		Desc: Set a font to query when a glyph is missing from this font.
	#*/
	sq_register(vm, FontSetFallback, "FontSetFallback", _SC(".xx"));
	sq_register(vm, FontSetFallback, "UIFontSetFallback", _SC(".xx"));
	/*#
		Func: FontComputeRect
		Proto: rect:rect clip_rect,string text,Font font,table param
		Desc: Compute the bounding rect of a formatted text string, does not perform any graphic output.
		<br>
		The following table keys are available:<br>
		<ul>
		<li><b>'color'</b>: Hexadecimal RGBA (eg. Red: xff0000ff)
		<li><b>'align'</b>: TextAlign
		<li><b>'format'</b>: TextFormat
		<li><b>'tracking'</b>: Integer value specifying an extra space between glyphs.
		<li><b>'heading'</b>: Integer value specifying an extra space between lines.
		</ul>
	#*/
	sq_register(vm, FontComputeRect, "FontComputeRect", _SC(".xsxt"));
	sq_register(vm, FontComputeRect, "UIFontComputeRect", _SC(".xsxt"));
}
//------------------------------------------------------------------------------
