/* $Revision Header * Header built automatically - do not edit! *************
 *
 *	(C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
 *
 *	Name .....: Scale.c
 *	Created ..: Monday 21-Jan-91 20:12
 *	Revision .: 0
 *
 *	Date            Author          Comment
 *	=========       ========        ====================
 *	21-Jan-91       Olsen           Created this file!
 *
 * $Revision Header ********************************************************/

#include "TermGlobal.h"

	/* Some static data required by the bitmap scaling routine. */

STATIC struct RastPort		*ScaleRPort;
STATIC struct BitMap		*ScaleSrcBitMap,*ScaleDstBitMap;
STATIC struct BitScaleArgs	*ScaleArgs;

STATIC UBYTE			 ScaleFontType = FONT_TOPAZ;

	/* DeleteScale():
	 *
	 *	Frees all the data associated with font scaling.
	 */

VOID
DeleteScale()
{
	if(ScaleArgs)
		FreeMem(ScaleArgs,sizeof(struct BitScaleArgs));

	ScaleArgs = NULL;

	if(ScaleDstBitMap)
	{
		if(ScaleDstBitMap -> Planes[0])
			FreeMem(ScaleDstBitMap -> Planes[0],2 * 16 * ScaleDstBitMap -> Depth);

		FreeMem(ScaleDstBitMap,sizeof(struct BitMap));
	}

	ScaleDstBitMap = NULL;

	if(ScaleSrcBitMap)
	{
		if(ScaleSrcBitMap -> Planes[0])
			FreeMem(ScaleSrcBitMap -> Planes[0],2 * 8 * ScaleSrcBitMap -> Depth);

		FreeMem(ScaleSrcBitMap,sizeof(struct BitMap));
	}

	ScaleSrcBitMap = NULL;

	if(ScaleRPort)
		FreeMem(ScaleRPort,sizeof(struct RastPort));

	ScaleRPort = NULL;
}

	/* CreateScale():
	 *
	 *	Sets up the data required for real-time font scaling
	 *	(bitmaps, rastports, etc.).
	 */

BYTE
CreateScale()
{
	SHORT i;

	if(ScaleRPort = (struct RastPort *)AllocMem(sizeof(struct RastPort),MEMF_PUBLIC|MEMF_CLEAR))
	{
		InitRastPort(ScaleRPort);

		if(ScaleSrcBitMap = (struct BitMap *)AllocMem(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR))
		{
			InitBitMap(ScaleSrcBitMap,Screen -> RastPort . BitMap -> Depth,16,8);

			if(ScaleDstBitMap = (struct BitMap *)AllocMem(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR))
			{
				InitBitMap(ScaleDstBitMap,Screen -> RastPort . BitMap -> Depth,16,16);

				ScaleRPort -> BitMap = ScaleSrcBitMap;

				if(ScaleSrcBitMap -> Planes[0] = (PLANEPTR)AllocMem(2 * 8 * ScaleSrcBitMap -> Depth,MEMF_CHIP|MEMF_CLEAR))
				{
					for(i = 1 ; i < ScaleSrcBitMap -> Depth ; i++)
						ScaleSrcBitMap -> Planes[i] = ScaleSrcBitMap -> Planes[0] + i * 2 * 8;

					if(ScaleDstBitMap -> Planes[0] = (PLANEPTR)AllocMem(2 * 16 * ScaleDstBitMap -> Depth,MEMF_CHIP|MEMF_CLEAR))
					{
						for(i = 1 ; i < ScaleDstBitMap -> Depth ; i++)
							ScaleDstBitMap -> Planes[i] = ScaleDstBitMap -> Planes[0] + i * 2 * 16;

						if(Config . Font == FONT_IBM && IBM)
							SetFont(ScaleRPort,IBM);
						else
							SetFont(ScaleRPort,Topaz);

						ScaleFontType = Config . Font;

						SetDrMd(ScaleRPort,JAM2);
						SetAPen(ScaleRPort,1);
						SetBPen(ScaleRPort,0);

						if(ScaleArgs = (struct BitScaleArgs *)AllocMem(sizeof(struct BitScaleArgs),MEMF_PUBLIC|MEMF_CLEAR))
						{
							ScaleArgs -> bsa_SrcWidth	= 8;
							ScaleArgs -> bsa_SrcHeight	= 8;

							ScaleArgs -> bsa_XSrcFactor	= 1;
							ScaleArgs -> bsa_YSrcFactor	= 1;

							ScaleArgs -> bsa_XDestFactor	= 2;

							ScaleArgs -> bsa_SrcBitMap	= ScaleSrcBitMap;
							ScaleArgs -> bsa_DestBitMap	= ScaleDstBitMap;

							return(TRUE);
						}
					}
				}
			}
		}
	}

	return(FALSE);
}

	/* PrintScaled(UBYTE Char):
	 *
	 *	This is the big one: since VT100 supports a number of
	 *	font sizes (double height, double width, 132 columns),
	 *	the approriate characters are scaled in real-time before
	 *	they are displayed.
	 */

VOID
PrintScaled(UBYTE Char)
{
	STATIC UBYTE LocalFgPen = 1,LocalBgPen = 0;

		/* Look for the font type to scale. */

	if(Config . Font != ScaleFontType)
	{
		if(Config . Font == FONT_IBM && IBM)
			SetFont(ScaleRPort,IBM);
		else
			SetFont(ScaleRPort,Topaz);

		ScaleFontType = Config . Font;
	}

		/* Set the approriate colours. */

	if(ScaleRPort -> FgPen != LocalFgPen)
	{
		SetAPen(ScaleRPort,FgPen);

		LocalFgPen = FgPen;
	}

	if(ScaleRPort -> BgPen != LocalBgPen)
	{
		LocalBgPen = BgPen;

		SetBPen(ScaleRPort,BgPen);
	}

		/* Print the character to be scaled into the
		 * invisible drawing area.
		 */

	Move(ScaleRPort,0,6);

	Text(ScaleRPort,&Char,1);

		/* Determine the scale of the destination character. */

	switch(Config . FontScale)
	{
			/* Twice as wide as the source data. */

		case SCALE_2X:	ScaleArgs -> bsa_YDestFactor = 1;

				break;

			/* Half as wide as the source data. */

		case SCALE_HALF:ScaleArgs -> bsa_YDestFactor = 1;
				ScaleArgs -> bsa_XDestFactor = 1;

				ScaleArgs -> bsa_XSrcFactor = 2;

				break;

			/* Twice as high as the source data. */

		default:	ScaleArgs -> bsa_YDestFactor = 2;

				break;
	}

		/* Scale the font. */

	BitMapScale(ScaleArgs);

		/* Render the character. */

	switch(Config . FontScale)
	{
		case SCALE_TOP2X:	BltBitMapRastPort(ScaleDstBitMap,0,0,RPort,CursorX * 8,CursorY * 8,16,8,0xC0);

					RasterPutString(&Char,1);

					CursorX++;

					RasterPutString(&Char,1);

					CursorX++;

					break;

		case SCALE_BOT2X:	BltBitMapRastPort(ScaleDstBitMap,0,8,RPort,CursorX * 8,CursorY * 8,16,8,0xC0);

					RasterPutString(&Char,1);

					CursorX++;

					RasterPutString(&Char,1);

					CursorX++;

					break;

		case SCALE_2X:		BltBitMapRastPort(ScaleDstBitMap,0,0,RPort,CursorX * 8,CursorY * 8,16,8,0xC0);

					RasterPutString(&Char,1);

					CursorX++;

					RasterPutString(" ",1);

					CursorX++;

					break;

		case SCALE_HALF:	BltBitMapRastPort(ScaleDstBitMap,0,0,RPort,CursorX * 4,CursorY * 8,4,8,0xC0);

					RasterPutString(&Char,1);

					CursorX++;

					ScaleArgs -> bsa_XDestFactor	= 2;
					ScaleArgs -> bsa_XSrcFactor	= 1;

					break;
	}

		/* And set the new cursor position. */

	SetCursor();
}
