#include "WimpLib:Font.h"
#include "WimpLib:Task.h"

#include <string.h>
#include "swis.h"

#include "WimpLib:Exception.h"

const _kernel_oserror* Font_Find(const char* name,  int xsize, int ysize, int xres, int yres, HFont* pf)
{
	const _kernel_oserror* err;

	err= _swix(Font_FindFont, _INR(1,5)|_OUT(0), name, xsize, ysize, xres, yres, pf);
	if (err) *pf = 0;

	return err;
}

const _kernel_oserror* Font_Lose(HFont f)
{
	return _swix(Font_LoseFont, _IN(0), f);
}

const _kernel_oserror* Font_SetColours(HFont f, int fg, int bg)
{
	return _swix(ColourTrans_SetFontColours, _INR(0,3), f, bg, fg, 14);
}

int Font_GetSplitPosition(HFont f, const char* string, int os_width)
{
	if (f == 0)
	{
		int len = strlen(string);
		if (len > (os_width / 16))
			len = os_width / 16;

		return len;
	}

	const Mode_Info* pMode = Task_GetModeInfo();
	char* pos;

	_swi(Font_ScanString, _INR(0,4)|_OUT(1), f, string, 0x100
		, os_width * pMode->millix // millipoints
		, 256 * pMode->milliy // wide enough
		, &pos);

	return (pos - string);
}
