/* << HighC V1.7 >> [FM-TOWNS] **********************************************
*
*	ﾌｫﾝﾄﾌｧｲﾙ処理
*	----------------------------------------------------------------------
*	Programmed by Y.Hirata ( NIFTY-ID: NAB03321  パオパオ )
*
*	NOTE: TAB=4
****************************************************************************/

#include <ctype.h>		/*  islower		*/
#include <string.h>		/*  str*,mem*	*/
#include "egbtxt.h"		/*  FNT,EGB		*/

#define	TRUE				1
#define	FALSE				0

const char	*FONT1[] = {	"MINN",	"GOTH",	"KYOU",	"MARU",	"MOUH"	} ;
const char	*FONTS[] = {	"24",	"32",	"48",	"60"	} ;
const char	*FONT2[] = {	"",		"HH",	"HK",	"QH",	"QK"	} ;
const int	FONTSZ[] = {	72,		128,	288,	480		} ;
const char	*TYPENAME[] = {	"FONT    ",
							"FONT1   ",
							"FONT2   "
} ;

char	FontPath[NFONT2][_MAX_PATH] ;			/*  ﾌｫﾝﾄﾌｧｲﾙﾊﾟｽ			*/
char	FontData[64/8*60] ;						/*  ﾌｫﾝﾄ読み込み用		*/
int		FontType ;								/*  ﾌｫﾝﾄﾌｧｲﾙ種別		*/
int		FontNo ;								/*  ﾌｫﾝﾄ種別			*/
int		FontDots=24 ;							/*  ﾌｫﾝﾄｻｲｽﾞ(dots)		*/
int		FontSize=72 ;							/*  ﾌｫﾝﾄｻｲｽﾞ(byte)		*/
int		FontSizeNo=0 ;							/*  ﾌｫﾝﾄｻｲｽﾞNo.			*/
FILE	*FontFp[NFONT2] = { NULL } ;			/*  ﾌｫﾝﾄﾌｧｲﾙﾎﾟｲﾝﾀ		*/

void strupper( char *str )
/*===========================================================================
*	文字列中の小文字を大文字に変換
===========================================================================*/
{
	register int	cnt ;

	for ( cnt=0; cnt<strlen( str ); cnt++ ) {	/*  小文字 -> 大文字	*/
		if ( iskanji( str[cnt] ) ) {
			cnt++ ;
		} else {
			if ( islower( str[cnt] ) ) str[cnt] -= ' ' ;
		}
	}
}

void makeFontPath( void )
/*===========================================================================
*	ﾌｫﾝﾄﾌｧｲﾙのﾌﾙﾊﾟｽを作成
===========================================================================*/
{
	register int	cp, term=FALSE ;
	int		len ;
	char	*path ;

	strcpy( FontPath[0],"" ) ;
	if ( (path=getenv( "FNT" )) != NULL ) {		/*  環境変数'FNT='		*/
		len = strlen( path ) ;
		if ( path[len-1] == '\\' ) {
			for ( cp=0; cp<len; cp++ ) {
				if ( iskanji( path[cp] ) ) {		/*  漢字	*/
					term = FALSE ;
					++cp ;
				} else {
					term = TRUE ;
				}
			}
		}
		strcpy( FontPath[0],path ) ;
		strupper( FontPath[0] ) ;
		if ( !term ) {
			FontPath[0][len]   = '\\' ;			/*  '\'の追加			*/
			FontPath[0][len+1] = '\0' ;
		}
	}
	strcat( FontPath[0],FONT1[FontNo] )	;
	strcat( FontPath[0],FONTS[FontSizeNo] )	;
	for ( cp=1; cp<NFONT2; cp++ ) {
		strcpy( FontPath[cp],FontPath[0] ) ;
		strcat( FontPath[cp],FONT2[cp] ) ;
	}
	strcat( FontPath[0],FONT2[0] ) ;
	for ( cp=0; cp<NFONT2; cp++ ) strcat( FontPath[cp],".FNT" ) ;
}

int chkFontFile( void )
/*===========================================================================
*	ﾌｫﾝﾄﾌｧｲﾙ種別のﾁｪｯｸ
===========================================================================*/
{
	register int	cnt ;
	char	type[8] ;

	if ( FontFp[0] == NULL ) {
		FontType = -1 ;
		return 1 ;
	}
	rewind( FontFp[0] ) ;
	if ( fread( type,1,8,FontFp[0] ) == 8 ) {
		for ( cnt=0; cnt<NTYPE; cnt++ ) {
			if ( !memcmp( type,TYPENAME[cnt],8 ) ) {
				FontType = cnt ;
				return 0 ;
			}
		}
	}
	FontType = -1 ;
	return 1 ;
}

int getFontOffset( int *sjis )
/*===========================================================================
*	ﾌｫﾝﾄﾌｧｲﾙ中のｵﾌｾｯﾄ
===========================================================================*/
{
	char	h, l ;
	int		size=FontSize ;
	int		hsize=16 ;

	*sjis = FNT_sjisToJis( *sjis ) ;
	l = *sjis & 0xFF ;
	if ( l < 0x21 || l > 0x7E ) return -1 ;
	h = (unsigned short int)*sjis >> 8 ;
	if ( h < 0x21 || h > 0x74 ) return -1 ;
	switch ( FontType ) {
	case 0:
	case 1:	size += 2 ;		break ;
	case 2:	hsize += ( size * 128 ) ;
	}
	return ( ( (h-0x21) * 94 + (l-0x21) ) * size + hsize ) ;
}

int loadFont( FILE *fp,int code,int kanji )
/*===========================================================================
*	ﾌｫﾝﾄの読み込み
===========================================================================*/
{
	register int	ch, size=FontSize ;

	if ( FontType == 0 || FontType == 1 ) {
		ch = fgetc( fp ) << 8 ;
		ch |= fgetc( fp ) ;
		if ( feof( fp ) ) return 1 ;
		if ( ch != code ) return 2 ;
	} else {
		if ( !kanji ) size /= 2 ;
	}
	if ( fread( FontData,1,size,fp ) < size ) return 3 ;
	return 0 ;
}

int fontOpen( int fno,int dots )
/*===========================================================================
*	ﾌｫﾝﾄﾌｧｲﾙｵｰﾌﾟﾝ
*	< RET >	: 設定条件に誤りがなければ 0, 誤りならば 1 を返す.
===========================================================================*/
{
	int		cnt ;

	if ( fno < 0 || fno > F_MOUH ) return 1 ;
	for ( cnt=0; cnt<NSIZE; cnt++ ) {
		if ( atoi( FONTS[cnt] ) == dots ) break;
	}
	if ( cnt >= NSIZE ) return 1 ;
	FontSizeNo = cnt ;							/*  ﾌｫﾝﾄｻｲｽﾞ種別		*/
	FontNo     = fno ;							/*  ﾌｫﾝﾄ種別			*/
	FontDots   = dots ;							/*  ﾄﾞｯﾄｻｲｽﾞ			*/
	FontSize = FONTSZ[FontSizeNo] ;				/*  ﾌｫﾝﾄﾃﾞｰﾀｻｲｽﾞ		*/
	makeFontPath() ;							/*  ﾌｫﾝﾄﾌｧｲﾙのﾊﾟｽ作成	*/
	for ( cnt=0; cnt<NFONT2; cnt++ ) {
		if ( FontFp[cnt] != NULL ) fclose( FontFp[cnt] ) ;
		FontFp[cnt] = fopen( FontPath[cnt],"rb" ) ;
	}
	chkFontFile() ;								/*  ﾌｫﾝﾄﾌｧｲﾙの種別ﾁｪｯｸ	*/
	return 0 ;
}

void fontClose( void )
/*===========================================================================
*	ﾌｫﾝﾄﾌｧｲﾙｸﾛｰｽﾞ
===========================================================================*/
{
	int		cnt ;

	for ( cnt=0; cnt<NFONT2; cnt++ )
		if ( FontFp[cnt] != NULL ) fclose( FontFp[cnt] ) ;
}

/*	HISTORY
-----------------------------------------------------------------------------
*	1991.04.13 : CREATE
*	1993.08.09 : remake
---------------------------------------------------------------------------*/
