/*
 * lilpage.c : title page printing program. Create a banner page
 *               for listing heading. (test version)
 *
 *       by Joel Swank 9-23-88
 */

#include <stdio.h>
#include <graphics/text.h>
#include <libraries/diskfont.h>
#include <functions.h>

#ifdef NULL
#undef NULL
#endif
#define NULL ((void *)0)

char printbuf[1000];   /* byffer for output line */
char *pbptr;           /* pointer into printbuf  */
char *printstring;     /* pointer to printstring */

int maxline = 80;      /* max output line size */
int i,j;
unsigned short *chardata;    /* pointer to array of character patterns */
unsigned short *charspace;   /* pointer to array of character widths   */

struct charDef {       /* from RKM Devices & Libraries page 207  */
	WORD charOffset;   /* bit offset to character in chardata    */
	WORD charBitWidth; /* bit width of character data            */
	};

struct charDef *charloc; /* pointer to array of chardefs */
struct TextFont *myfont = NULL; /* pointer to open font  */
struct GfxBase *GfxBase = NULL;
FILE *out;


struct	TextAttr myattr = {
    (STRPTR)("opal.font"),
    12,
    0,
    FPB_DISKFONT};

struct Library *DiskfontBase = NULL;

main(argc,argv)
int argc;
char *argv[];
{
	printstring = "XYZAdgq";
	out = stdout;

	GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
	if (GfxBase == NULL) done(22);

	DiskfontBase = (struct Library *)OpenLibrary("diskfont.library", NULL);
	if (DiskfontBase == NULL) {
		fprintf(stderr,"titlep:cannot open font library\n");
		exit(1);
		}

	if (DiskfontBase != NULL) {
		if ((myfont = (struct TextFont *)OpenDiskFont(&myattr)) == NULL) {
			fprintf(stderr,"titlep:cannot find puny font\n");
			done(2);
		}
	}

	charspace = (unsigned short *) myfont->tf_CharSpace;
	charloc = (struct charDef *) myfont->tf_CharLoc;

	/* do a string */
	for (i=0; i<myfont->tf_YSize; i++)
		{
		for (j=0; j<maxline; j++) printbuf[j] =' ';
		do_row(i);
		printbuf[maxline] ='\0';
		fputs(printbuf,out);
		aputc('\n',out);
		}

done(0);
}


/*
 * do_row : build one row of printdata.
 */

do_row(row)
int row;   /* which row to do */
{
	int i;
	register unsigned short next16;
	char *pstrptr;

	pbptr = printbuf;
	pstrptr = printstring;
	chardata = (unsigned short *) myfont->tf_CharData + 
					(myfont->tf_Modulo * row)/2;

	/* do a charcter  */
	while (*pstrptr != '\0')
		{
		long bitoff, wordoff, bitnum, bitcnt, charoff;
		charoff = (unsigned char) *pstrptr - myfont->tf_LoChar;
		bitoff = charloc[charoff].charOffset;
		bitcnt = charloc[charoff].charBitWidth;
		wordoff = bitoff/16;
		bitnum = bitoff%16;

		while (bitcnt > 0)    /* do up to 16 bits */
			{
			char *cptr;
			/* construct dot info in next16 */
			next16 = chardata[wordoff];
			if (bitnum != 0) next16 = (next16 << bitnum) 
				| (chardata[wordoff+1] >> (16-bitnum));
			cptr = pbptr;
			for (i=0; i<16; i++)
				{
				*cptr = (next16 & 0x08000) ? *pstrptr : ' ';
				cptr++;
				if (--bitcnt < 1) break;
				next16 = next16 << 1;
				}
			wordoff++;
			}
		pbptr += charspace[charoff]; /* bump output ptr */
		pstrptr++;                   /* bump input ptr */
		if (pbptr > printbuf + maxline) break;
		}
}

/*
 * done - just clean up that which is open, and then leave.
 */
done(how)
int how;
    {
    if (GfxBase) CloseLibrary(GfxBase) ;
    if (DiskfontBase) CloseLibrary(DiskfontBase) ;
	if (myfont   != NULL) CloseFont( myfont );
    exit(how) ;
    }

