/* << HighC V1.7 >> [FM-TOWNS] **********************************************
*
*	ｸﾞﾗﾌｨｯｸ処理(文字表示)
*	----------------------------------------------------------------------
*	Programmed by Y.Hirata ( NIFTY-ID: NAB03321  パオパオ )
*
*	NOTE: TAB=4
****************************************************************************/

#include <string.h>		/*  strlen,strcpy,memcpy	*/
#include <stdarg.h>		/*  va_list,va_*	*/
#include <stdio.h>		/*  vsprintf		*/
#include <msdos.cf>		/*  getds			*/
#include "egbtxt.h"		/*  egb.h,宣言		*/

char	Gwork[EgbWorkSize] ;					/*  EGB作業領域			*/
int		__gdotx = 8 ;							/*  文字の横ｻｲｽﾞ		*/
int		__gdoty = 16 ;							/*  文字の縦ｻｲｽﾞ		*/
int		__gdots = 0 ;							/*  文字間空白ﾄﾞｯﾄ数	*/
int		__gtype = F_ROM ;						/*  表示文字種			*/
char	__gstrbuf[128] ;						/*  文字列描画用		*/

#define	TRUE				1
#define	FALSE				0

void pset( int x,int y )
/*===========================================================================
*	点描画
===========================================================================*/
{
	struct {
		short int	n ;			/*  座標点数	*/
		short int	x ;			/*  X座標		*/
		short int	y ;			/*  Y座標		*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.n = 1 ;
	para.x = x ;
	para.y = y ;
	EGB_pset( Gwork,(char *)&para ) ;			/*  点描画				*/
}

void line( int x1,int y1,int x2,int y2 )
/*===========================================================================
*	直線描画
===========================================================================*/
{
	struct {
		short int	n ;			/*  座標点数	*/
		short int	x1, y1 ;	/*  第1点座標	*/
		short int	x2, y2 ;	/*  第2点座標	*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.n  = 2 ;
	para.x1 = x1 ;
	para.y1 = y1 ;
	para.x2 = x2 ;
	para.y2 = y2 ;
	EGB_connect( Gwork,(char *)&para ) ;		/*  連続線描画			*/
}

void triangle( int x1,int y1,int x2,int y2,int x3,int y3 )
/*===========================================================================
*	三角形描画
===========================================================================*/
{
	struct {
		short int	x1, y1 ;	/*  第1点座標	*/
		short int	x2, y2 ;	/*  第2点座標	*/
		short int	x3, y3 ;	/*  第3点座標	*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.x1 = x1 ;
	para.y1 = y1 ;
	para.x2 = x2 ;
	para.y2 = y2 ;
	para.x3 = x3 ;
	para.y3 = y3 ;
	EGB_triangle( Gwork,(char *)&para ) ;		/*  三角形描画			*/
}

void box( int x1,int y1,int x2,int y2 )
/*===========================================================================
*	矩形描画
===========================================================================*/
{
	struct {
		short int	x1, y1 ;	/*  第1点座標	*/
		short int	x2, y2 ;	/*  第2点座標	*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.x1 = x1 ;
	para.y1 = y1 ;
	para.x2 = x2 ;
	para.y2 = y2 ;
	EGB_rectangle( Gwork,(char *)&para ) ;		/*  矩形描画			*/
}

void ellipse( int x,int y,int dx,int dy )
/*===========================================================================
*	楕円描画
===========================================================================*/
{
	struct {
		short int	x, y ;		/*  中心点座標	*/
		short int	dx, dy ;	/*  X,Y成分		*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.x  = x ;
	para.y  = y ;
	para.dx = dx ;
	para.dy = dy ;
	EGB_ellipse( Gwork,(char *)&para ) ;		/*  楕円描画			*/
}

void paint( int x,int y,int color )
/*===========================================================================
*	面塗り
===========================================================================*/
{
	struct {
		short int	x, y ;		/*  X,Y座標	*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	EGB_color( Gwork,C_PAINT,color ) ;			/*  描画色設定			*/
	para.x  = x ;
	para.y  = y ;
	EGB_closePaint( Gwork,(char *)&para ) ;		/*  閉領域塗り潰し		*/
}

void glocate( int x,int y )
/*===========================================================================
*	ﾃｷｽﾄ表示位置設定(ｸﾞﾗﾌｨｯｸ座標)
===========================================================================*/
{
	struct {
		short int	x, y ;		/*  X,Y座標		*/
		short int	len ;		/*  文字列長	*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.x   = x ;
	para.y   = y ;
	para.len = 0 ;
	EGB_sjisString( Gwork,(char *)&para ) ;		/*  文字列の処理		*/
}

void locate( int clmn,int row )
/*===========================================================================
*	ﾃｷｽﾄ表示位置設定(ﾃｷｽﾄ座標)
*	< IN  >	: clmn	桁(1〜)
*			: row	行(1〜)
===========================================================================*/
{
	clmn = ( clmn - 1 ) * __gdotx ;				/*  桁→X座標			*/
	row  = row * __gdoty - 1 ;					/*  行→Y座標			*/
	glocate( clmn,row ) ;						/*  描画開始位置		*/
}

void _gputc( int ch )
/*===========================================================================
*	文字描画(ROMﾌｫﾝﾄ)
===========================================================================*/
{
	struct {
		short int	len ;
		char	str[2] ;
	} para ;									/*  文字列描画用		*/

	para.len = 1 ;
	para.str[0] = (char)ch ;
	para.str[1] = '\0' ;
	EGB_connectSjisString( Gwork,(char *)&para ) ;	/*  文字列表示		*/
}

void _gputs( char *str )
/*===========================================================================
*	文字列描画(ROMﾌｫﾝﾄ)
===========================================================================*/
{
	struct {
		short int	len ;
		char	str[128] ;
	} para ;									/*  文字列描画用		*/

	para.len = strlen( str ) ;
	strcpy( para.str,str ) ;
	EGB_connectSjisString( Gwork,(char *)&para ) ;	/*  文字列表示		*/
}

void _gprintf( char *form,... )
/*===========================================================================
*	書式付文字列描画(ROMﾌｫﾝﾄ)
===========================================================================*/
{
	va_list	argptr ;

	va_start( argptr,form ) ;
	vsprintf( __gstrbuf,form,argptr ) ;
	_gputs( __gstrbuf ) ;
	va_end( argptr ) ;
}

void gput( int x1,int y1,int x2,int y2,int pixel,unsigned int ptr )
/*===========================================================================
*	矩形域張り付け
===========================================================================*/
{
	struct {
		long int	ptr ;		/*  格納ｱﾄﾞﾚｽ	*/
		short int	ds ;		/*  ﾃﾞｰﾀｾﾚｸﾀ	*/
		short int	x1, y1 ;	/*  第1点座標	*/
		short int	x2, y2 ;	/*  第2点座標	*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.ptr = ptr ;
	para.ds  = getds() ;
	para.x1  = x1 ;
	para.y1  = y1 ;
	para.x2  = x2 ;
	para.y2  = y2 ;
	if ( pixel == 1 )
		EGB_putBlockColor( Gwork,1,(char *)&para ) ;	/*  単色		*/
	else
		EGB_putBlock( Gwork,1,(char *)&para ) ;			/*  多色		*/
}

void gget( int x1,int y1,int x2,int y2,unsigned int ptr )
/*===========================================================================
*	矩形域取り込み(多色ﾓｰﾄﾞ用)
===========================================================================*/
{
	struct {
		long int	ptr ;		/*  格納ｱﾄﾞﾚｽ	*/
		short int	ds ;		/*  ﾃﾞｰﾀｾﾚｸﾀ	*/
		short int	x1, y1 ;	/*  第1点座標	*/
		short int	x2, y2 ;	/*  第2点座標	*/
	} para ;									/*  EGB関数用ﾊﾟﾗﾒﾀ		*/

	para.ptr = ptr ;
	para.ds  = getds() ;
	para.x1  = x1 ;
	para.y1  = y1 ;
	para.x2  = x2 ;
	para.y2  = y2 ;
	EGB_getBlock( Gwork,(char *)&para ) ;
}

void convFontY( void )
/*===========================================================================
*	ROMﾌｫﾝﾄを縦24ﾄﾞｯﾄに加工
===========================================================================*/
{
	register int	sp, dp ;
	char	wk[32] ;

	memcpy( wk,FontData,32 ) ;
	for ( sp=0, dp=0; sp<32; sp+=4, dp+=6 ) {
		FontData[dp+0] = wk[sp] ;
		FontData[dp+1] = wk[sp+1] ;
		FontData[dp+2] = wk[sp+0] | wk[sp+2] ;
		FontData[dp+3] = wk[sp+1] | wk[sp+3] ;
		FontData[dp+4] = wk[sp+2] ;
		FontData[dp+5] = wk[sp+3] ;
	}
}

void dispAscii24( int code )
/*===========================================================================
*	ASCII文字表示(縦24ﾄﾞｯﾄ)
===========================================================================*/
{
	register int	off ;

	FNT_ankRead( 8,16,code,getds(),FontData ) ;
	for ( off=15; off>=0; off-- ) {
		FontData[off+off+1] = 0 ;
		FontData[off+off] = FontData[off] ;
	}
	convFontY() ;								/*  ROMﾌｫﾝﾄを縦24ﾄﾞｯﾄに	*/
	EGB_anyChar( Gwork,__gdotx+__gdotx,__gdoty,FontData ) ;
	EGB_textSpace( Gwork,-__gdotx+__gdots ) ;
}

void dispSjis24( int sjis )
/*===========================================================================
*	ｼﾌﾄJIS漢字表示(縦24ﾄﾞｯﾄ)
===========================================================================*/
{
	int		code ;

	code = FNT_sjisToJis( sjis ) ;
	FNT_kanjiRead( 16,16,code,getds(),FontData ) ;
	convFontY() ;								/*  ROMﾌｫﾝﾄを縦24ﾄﾞｯﾄに	*/
	EGB_anyChar( Gwork,__gdotx+__gdotx,__gdoty,FontData ) ;
	EGB_textSpace( Gwork,__gdots ) ;
}

void gputs24( const char *str )
/*===========================================================================
*	文字列表示(縦24ﾄﾞｯﾄ)
===========================================================================*/
{
	register int	kanji ;
	int		code ;

	EGB_textZoom( Gwork,0,8,24 ) ;				/*  ANK		*/
	EGB_textZoom( Gwork,1,16,24 ) ;				/*  漢字	*/
	while ( *str ) {
		if ( iskanji( *str ) ) {
			code = *str << 8 ;
			str++ ;
			code |= *str ;
			kanji = TRUE ;
		} else {
			code = *str ;
			kanji = FALSE ;
		}
		str++ ;
		if ( kanji ) {								/*  漢字	*/
			dispSjis24( code ) ;
		} else {									/*  ANK		*/
			dispAscii24( code ) ;
		}
	}
	EGB_textZoom( Gwork,0,__gdotx,__gdoty ) ;			/*  ANK		*/
	EGB_textZoom( Gwork,1,__gdotx+__gdotx,__gdoty ) ;	/*  漢字	*/
	EGB_textSpace( Gwork,__gdots ) ;
}

void gprintf24( char *form,... )
/*===========================================================================
*	書式付文字列描画(縦24ﾄﾞｯﾄ)
===========================================================================*/
{
	va_list	argptr ;

	va_start( argptr,form ) ;
	vsprintf( __gstrbuf,form,argptr ) ;
	gputs24( __gstrbuf ) ;
	va_end( argptr ) ;
}

void dispAscii( int code )
/*===========================================================================
*	ASCII文字表示(ﾏﾙﾁﾌｫﾝﾄ)
===========================================================================*/
{
	int		off, err=FALSE ;
	FILE	*fp ;
	int		xsize=FontDots, ysize=FontDots ;

	if ( xsize == 60 ) xsize = 64 ;
	if ( FontType == 0 || FontType == 1 ) {		/*  全角分	*/
		fp = FontFp[2] ;
		off = code * (FontSize+2) ;
	} else {									/*  半角分	*/
		fp = FontFp[0] ;
		off = code * (FontSize/2) + 16 ;
		xsize >>= 1 ;
		EGB_textZoom( Gwork,1,__gdotx,__gdoty ) ;
	}
	if ( fp == NULL ) {
		err = TRUE ;
	} else {
		if ( off < 0 ) {
			err = TRUE ;
		} else {
			if ( fseek( fp,off,SEEK_SET ) ) {
				err = TRUE ;
			} else {
				err = loadFont( fp,code,FALSE ) ;
			}
		}
	}
	if ( err ) {								/*  ROMﾌｫﾝﾄ				*/
		_gputc( code ) ;
		EGB_textSpace( Gwork,__gdots ) ;
	} else {									/*  任意ﾌｫﾝﾄ			*/
		EGB_anyChar( Gwork,xsize,ysize,FontData ) ;
		if ( xsize == 64 ) xsize = 60 ;
		if ( xsize == FontDots ) {				/*  全角分の時			*/
			EGB_textSpace( Gwork,-__gdotx+__gdots ) ;	/*  半角分戻す	*/
		} else {
			EGB_textSpace( Gwork,__gdots ) ;
		}
	}
	EGB_textZoom( Gwork,1,__gdotx+__gdotx,__gdoty ) ;	/*  漢字	*/
}

void dispSjis( int sjis )
/*===========================================================================
*	ｼﾌﾄJIS漢字表示(ﾏﾙﾁﾌｫﾝﾄ)
===========================================================================*/
{
	int		off, err=FALSE ;
	int		code ;
	FILE	*fp ;
	char	str[3] ;
	int		xsize=FontDots, ysize=FontDots ;

	if ( xsize == 60 ) xsize = 64 ;
	fp = FontFp[0] ;
	code = sjis ;
	off = getFontOffset( &code ) ;				/*  格納位置取得		*/
	if ( fp == NULL ) {
		err = TRUE ;
	} else {
		if ( off < 0 ) {
			err = TRUE ;
		} else {
			if ( fseek( fp,off,SEEK_SET ) ) {
				err = TRUE ;
			} else {
				err = loadFont( fp,code,TRUE ) ;
			}
		}
	}
	if ( err ) {
		str[0] = (char)( sjis >> 8 ) ;
		str[1] = (char)( sjis & 0xFF ) ;
		str[2] = '\0' ;
		_gputs( str ) ;
	} else {
		EGB_anyChar( Gwork,xsize,ysize,FontData ) ;
	}
	EGB_textSpace( Gwork,__gdots ) ;
}

void gputs2( const char *str )
/*===========================================================================
*	ﾏﾙﾁﾌｫﾝﾄ文字列表示
===========================================================================*/
{
	int		kanji ;
	int		code ;

	while ( *str ) {
		if ( iskanji( *str ) ) {
			code = *str << 8 ;
			str++ ;
			code |= *str ;
			kanji = TRUE ;
		} else {
			code = *str ;
			kanji = FALSE ;
		}
		str++ ;
		if ( kanji ) {							/*  漢字	*/
			dispSjis( code ) ;
		} else {								/*  ANK		*/
			dispAscii( code ) ;
		}
	}
	EGB_textSpace( Gwork,__gdots ) ;
}

void gprintf2( char *form,... )
/*===========================================================================
*	書式付文字列描画(ﾏﾙﾁﾌｫﾝﾄ)
===========================================================================*/
{
	va_list	argptr ;

	va_start( argptr,form ) ;
	vsprintf( __gstrbuf,form,argptr ) ;
	gputs2( __gstrbuf ) ;
	va_end( argptr ) ;
}

void setTextSpace( int size )
/*===========================================================================
*	文字間空白のﾄﾞｯﾄ数設定
===========================================================================*/
{
	__gdots = size ;
	EGB_textSpace( Gwork,__gdots ) ;			/*  文字間空白設定		*/
}

void setTextSize( int xsize,int ysize )
/*===========================================================================
*	文字ｻｲｽﾞ設定
*	< IN  >	: xsize	ANKの横ｻｲｽﾞ(倍値を漢字の横ｻｲｽﾞとする)
*			: ysize	ANK/漢字の縦ｻｲｽﾞ
===========================================================================*/
{
	__gdotx = xsize ;
	__gdoty = ysize ;
	EGB_textZoom( Gwork,0,__gdotx,__gdoty ) ;			/*  ANK		*/
	EGB_textZoom( Gwork,1,__gdotx+__gdotx,__gdoty ) ;	/*  漢字	*/
}

void setTextType( int type )
/*===========================================================================
*	表示文字種の設定
*	ﾏﾙﾁﾌｫﾝﾄ指定時は, fotnOpen関数をcallする必要あり
===========================================================================*/
{
	switch ( type ) {
	case F_ROM24:
	case F_FILE	:	__gtype = type ;		break ;
	case F_ROM	:
	default		:	__gtype = F_ROM ;
	}
}

void gputc( int ch )
/*===========================================================================
*	文字描画
===========================================================================*/
{
	switch ( __gtype ) {
	case F_ROM24:
		EGB_textZoom( Gwork,0,8,24 ) ;						/*  ANK		*/
		EGB_textZoom( Gwork,1,16,24 ) ;						/*  漢字	*/
		dispAscii24( ch ) ;
		EGB_textZoom( Gwork,0,__gdotx,__gdoty ) ;			/*  ANK		*/
		EGB_textZoom( Gwork,1,__gdotx+__gdotx,__gdoty ) ;	/*  漢字	*/
		break ;
	case F_FILE	:
		dispAscii( ch ) ;
		break ;
	case F_ROM	:
	default		:
		_gputc( ch ) ;
	}
	EGB_textSpace( Gwork,__gdots ) ;
}

void gputs( char *str )
/*===========================================================================
*	文字列描画
===========================================================================*/
{
	switch ( __gtype ) {
	case F_ROM24:	gputs24( str ) ;	break ;
	case F_FILE	:	gputs2( str ) ;		break ;
	case F_ROM	:
	default		:	_gputs( str ) ;
	}
}

void gprintf( char *form,... )
/*===========================================================================
*	書式付文字列描画
===========================================================================*/
{
	va_list	argptr ;

	va_start( argptr,form ) ;
	vsprintf( __gstrbuf,form,argptr ) ;
	gputs( __gstrbuf ) ;
	va_end( argptr ) ;
}

/*	HISTORY
-----------------------------------------------------------------------------
*	1991.04.13 : CREATE
*	1993.08.09 : remake
---------------------------------------------------------------------------*/
