/*  GadTools layout toolkit
**
**  Copyright © 1993-1995 by Olaf `Olsen' Barthel
**  Freely distributable.
*/

#include "gtlayout_global.h"

VOID __regargs
LTP_GetDefaultFont(struct TTextAttr *TextAttr)
{
	struct TextFont *Font = GfxBase -> DefaultFont;

	TextAttr -> tta_Name	= Font -> tf_Message . mn_Node . ln_Name;
	TextAttr -> tta_YSize	= Font -> tf_YSize;
	TextAttr -> tta_Style	= Font -> tf_Style;
	TextAttr -> tta_Flags	= Font -> tf_Flags & ~FPF_REMOVED;

	if(Font -> tf_Style & FSF_TAGGED)
		TextAttr -> tta_Tags = ((struct TextFontExtension *)Font -> tf_Extension) -> tfe_Tags;
	else
		TextAttr -> tta_Tags = NULL;
}


/*****************************************************************************/


struct TextFont * __regargs
LTP_OpenFont(struct TextAttr *TextAttr)
{
	struct TextFont *Font;

		// Let's see if this one is special

	if(TextAttr == (struct TextAttr *)~0)
	{
		struct TTextAttr LocalTextAttr;

		memset(&LocalTextAttr,0,sizeof(LocalTextAttr));

		Forbid();

			// Borrow the system default font data

		LTP_GetDefaultFont(&LocalTextAttr);

			// Let's hope the font will open...

		Font = OpenFont((struct TextAttr *)&LocalTextAttr);

		Permit();
	}
	else
	{
		if(!(Font = OpenFont(TextAttr)))
		{
			if(SysBase -> ThisTask -> tc_Node . ln_Type != NT_TASK)
			{
				struct Library *DiskfontBase;

				if(DiskfontBase = OpenLibrary("diskfont.library",0))
				{
					Font = OpenDiskFont(TextAttr);

					CloseLibrary(DiskfontBase);
				}
			}
		}
	}

	return(Font);
}


/*****************************************************************************/


BOOLEAN __regargs
LTP_GlyphSetup(struct LayoutHandle *Handle,struct TextAttr *TextAttr)
{
	struct TextFont *Font;
	struct RastPort *RPort;

	RPort = &Handle -> RPort;

	if(TextAttr)
	{
		Handle -> TextAttr = TextAttr;

		if(Font = LTP_OpenFont(Handle -> TextAttr))
		{
			if(Handle -> CloseFont)
				CloseFont(RPort -> Font);
			else
				Handle -> CloseFont = TRUE;
		}
	}
	else
	{
		Handle -> TextAttr = Handle -> Screen -> Font;

		Font = Handle -> DrawInfo -> dri_Font;

		if(Handle -> CloseFont)
		{
			CloseFont(RPort -> Font);

			Handle -> CloseFont = FALSE;
		}
	}

	if(Font)
	{
		LONG	Count = 0;
		LONG	Total = 0;
		UBYTE	Glyph;
		UWORD	i;
		ULONG	OldStyle;

		LTP_SetFont(Handle,Font);

		OldStyle = SetSoftStyle(RPort,FSF_BOLD,AskSoftStyle(RPort));

			// Ok, first cover the standard ASCII character set

		for(i = 32 ; i < 127 ; i++,Count++)
		{
			Glyph = i;

			Total += TextLength(RPort,&Glyph,1);
		}

			// Now for the ISO part...

		for(i = 160 ; i < 256 ; i++,Count++)
		{
			Glyph = i;

			Total += TextLength(RPort,&Glyph,1);
		}

		Handle -> GlyphWidth = Total / Count;

			// Now for the numeric characters

		for(Total = 0, i = '0' ; i <= '9' ; i++)
		{
			Glyph = i;

			Total += TextLength(RPort,&Glyph,1);
		}

			// Actually, the numeric character should be
			// just as wide as the space character. Let's
			// hope for the best.

		Count = TextLength(RPort," ",1);

		Total /= 10;

		if(Total < Count)
			Total = Count;

		if(Total > Handle -> GlyphWidth)
			Handle -> GlyphWidth = Total;

		if(Handle -> GlyphWidth <= 8)
			Handle -> InterWidth = 4;
		else
			Handle -> InterWidth = Handle -> GlyphWidth / 2;

		if(RPort -> TxHeight <= 8)
			Handle -> InterHeight = 2;
		else
			Handle -> InterHeight = RPort -> TxHeight / 4;

		SetSoftStyle(RPort,OldStyle,~0);

		return(TRUE);
	}
	else
		return(FALSE);
}
