/*
	font.l
	95/04/19
*/

/*
#f font.h

#include "bits.h"

#define tstbit(b,x) ( \
	*( ((byte *) b ) + ( (x) / 8 ) ) & \
		( 0x80 >> ( (x) % 8 ) ) \
	)

#define setbit(b,x) ( \
	*( ((byte *) b ) + ( (x) / 8 ) ) |= \
		( 0x80 >> ( (x) % 8 ) ) \
	)

#define tstbit2(b,x,y,z) ( \
	*( ((byte *) b ) + ( (x) / 8 ) + (y) * ( (z) / 8 ) ) & \
		( 0x80 >> ( (x) % 8 ) ) \
	)

#define setbit2(b,x,y,z) ( \
	*( ((byte *) b ) + ( (x) / 8 ) + (y) * ( (z) / 8 ) ) |= \
		( 0x80 >> ( (x) % 8 ) ) \
	)

typedef struct {
	FILE *fp;
	byte *b;
	BITS *bp;
	int ofs,size;
	byte width,type;
} FONT;

#end
*/

--
#include <stdio.h>
#include <malloc.h>
#include "font.h"
-fontini.c
FONT *fontini( char *fn, byte width, byte type ) {

	FONT *f;
	struct {
		byte id[9];
		word a,b;
		byte c;
		word d;
	} fh;
	char *a;

	if((f = (FONT *) malloc(sizeof(FONT)))== NULL ) return NULL;
	if(( f->fp = fopen( fn,"rb" ))== NULL ) return NULL;
	fread(&fh,sizeof(fh),1,f->fp);

	a = fh.id;
	if( *a++ == 'F' && *a++ == 'O' && *a++ == 'N' && *a++ == 'T' ) {
		f->type = ( *a == '1' ? 1 : ( *a == '2' ? 2 : 0 ));
		f->width = fh.b;
		if( fh.d != 0x8422 ) return NULL;
	} else {
		f->type = type;
		f->width = width;
	}

	if(( f->bp = bitsini(f->width,f->width))== NULL ) return NULL;
	f->size = f->width * (( f->width + 7 )/8);
	if(( f->b = (byte *) malloc(f->size) )== NULL ) return NULL;

	switch ( f->type ) {
	case 1:
		f->ofs = 16 + 2;
		f->size += 2;
		break;
	case 2:
		f->ofs = 16 + f->size * 128;
		break;
	case 5:
	case 3:
		f->ofs = 0;
		break;
	case 4:
		f->ofs = f->size * 128;
		break;
	default:
		return NULL;
		break;
	}
	return f;
}

--
#include <stdio.h>
#include "gds.h"
#include "font.h"
-putfont.c
void putfont( FONT *f, int xx, int yy, byte cf, byte cb, byte pg ){

	int x,y,w;
	w = ( f->width + 7 ) / 8 * 8;
	for( y = 0 ; y < f->width ; y++ ) {
		for( x = 0 ; x < w ; x++ ) {
			setbitg(f->bp,x,y,tstbit2(f->b,x,y,w) ? cf : cb );
		}
	}
	Putg(f->bp,xx,yy,pg);
}

--
#include <stdio.h>
#include "font.h"

-getfont.c
void getfont( FONT *f, word k ) {

	long o;

	if( f->type == 5 ) {
		o = ( 1024 / f->size );
		o = f->ofs + ( k / o ) * 1024 + ( k % o ) * f->size;
	} else {
		o = f->ofs + k * f->size;
	}
	/* s-jis to k ... */
	fseek(f->fp,o,SEEK_SET);
	fread(f->b,f->size,1,f->fp);
}
