
/**********************************************************************/
/*                                                                    */
/* PrFont - Version 1.3                                               */
/*                                                                    */
/*  by Joel Swank 1/12/89                                             */
/*                                                                    */
/**********************************************************************/

/**********************************************************************/
/*                                                                    */
/* PrFont  - Prints a sample of all fonts in the fonts: directory     */
/*                                                                    */
/**********************************************************************/
/*                                                                    */
/*    Modification log:                                               */
/*                                                                    */
/*  2/5/89    Original Idea implemented                               */
/*  2/12/89   Switched to Availfonts from directory scan              */
/*  5/21/89   Adjust screen/window parameters                         */
/*  5/30/89   fix open before getargs bug                             */
/*                                                                    */
/**********************************************************************/

#include <stdio.h>
#include <functions.h>
#include "myscreen.h"
#include "defines.h"


/*
 *  Some Font stuff 
 */
struct TextFont *myfont = NULL;  /* pointer to open font  */
struct TextAttr *myattr;         /* pointer to Attribute struct */
struct AvailFontsHeader *avlbuf = NULL; /* pointer to availfonts buffer */
struct AvailFonts *af;           /* pointer to availfonts entries */

/* Intuition pointers */
struct Window	*Wind = NULL ;
struct Screen	*MyScreen = NULL ;
struct RastPort *rp;

/* Library pointers */
struct Library *DiskfontBase = NULL;
struct IntuitionBase	*IntuitionBase ;
struct DosBase *DosBase = NULL;
struct GfxBase *GfxBase = NULL;

char *rindex();

long screenpos;      /* current screen Y location */
long all = FALSE;    /* flag - show all chars - set in doargs */
int bufsize = 0;     /* size of availfonts buffer */

main(argc, argv)
int argc;
char *argv[];
{
	char tb;
	register i, j;
	int err;

	screenpos = SCREENPOSINIT;

	if (argc == 0) getWBargs();
	else getCLIargs(argc,argv);

	open_all();  /* Open everything */

	/* find size of buffer needed */
	bufsize = AvailFonts(&tb,1L,AFF_MEMORY | AFF_DISK)+2;

	/* get a buffer of that size */
	avlbuf = (struct AvailFontsHeader *) AllocMem(bufsize,MEMF_PUBLIC);
	if (avlbuf == NULL)
		{
		AutoRequest(Wind,&memmsg,0L,&oktxt,0L,0L,300L,75L);
		done(26);
		}

	/* get list of all the system fonts */
	err = AvailFonts(avlbuf,bufsize,AFF_MEMORY | AFF_DISK);
	if (err != NULL)
		{
		AutoRequest(Wind,&availmsg,0L,&oktxt,0L,0L,300L,75L);
		done(28);
		}

	af = (struct Availfonts *) &(avlbuf[1]);
	for (j=0; j<avlbuf->afh_NumEntries; j++,af++)
		{          /* weed out duplicates */
		if ((af->af_Attr.ta_Flags & FPF_REMOVED) ||  /* fi removed ...  */
		    (af->af_Attr.ta_Flags & FPF_REVPATH) ||  /* or reverse ...  */
		    ((af->af_Type & AFF_MEMORY) &&   /* or NOT memory AND disk  */
		    (af->af_Attr.ta_Flags & FPF_DISKFONT))); /* then skip */
		else {
			myattr = &(af->af_Attr);
			dofont();
			}
		}


	if (screenpos > SCREENPOSINIT) do_print();
	done(0);
}

/*
 * do font described by myattr
 */

dofont()
{

	char stringbuf[500];
	char fontsave[50];
	int istart;
	register long fheight;
	register long  i;
	register char *j;

	/* get a copy of name */
	strcpy(fontsave,myattr->ta_Name);
	j = rindex(fontsave,'.');
	if (j != NULL) *j = '\0';

	if ((myfont = (struct TextFont *) OpenDiskFont(myattr)) == NULL) 
		{
		sprintf(msgfilename,"%s %ld\n",fontsave,
			myattr->ta_YSize);
		AutoRequest(Wind,&openfomsg,0L,&oktxt,0L,0L,300L,75L);
		}
	else
		{
		fheight = myfont->tf_YSize; /* get actual size */
		/* not the requested  font, skippit */
		if (fheight != myattr->ta_YSize) return;
		SetFont(rp,myfont);
		/* build string to print */
		sprintf(stringbuf,"%s %ld ",fontsave,fheight);
		j = stringbuf+strlen(stringbuf);
		istart=myfont->tf_LoChar;
		if (istart <=0) istart = 1;
		for (i=istart; i<= myfont->tf_HiChar; i++)
			*j++ = (char) i;
		stringbuf[j++] = '\0';
		if (all)
			{
			display_font(stringbuf);
			}
		else
			{
			/* move down enough for  */
			screenpos += fheight+2;
			if (screenpos > MyScreen->Height) 
				{      /* when screen gets full, print it */
				do_print();
				screenpos = SCREENPOSINIT+fheight;
				clr_scr();
				}
			/* move to baseline of charcter */
			Move(rp,2,screenpos-fheight+myfont->tf_Baseline);
			Text(rp,stringbuf,strlen(stringbuf));
			}
		CloseFont(myfont);
		myfont = NULL;
		}
}

/*
 *  display all characters of a font
 */

display_font(buf)
register char *buf;
{
	register int start = 0;
	register int i;
	int indent = 2;
	int fheight;
	while (1)
		{
		i=0;
		while(buf[i+start] != '\0')
			{
			if (TextLength(rp,&buf[start],i) >= Wind->Width-indent)
				{
				i--;
				break;
				}
			i++;
			}
		if (i==0) break;
		fheight = myfont->tf_YSize; /* get actual size */
		/* move down enough for  */
		screenpos += fheight+2;
		if (screenpos > MyScreen->Height) 
			{      /* when screen gets full, print it */
			do_print();
			screenpos = SCREENPOSINIT+fheight;
			clr_scr();
		}
		/* move to baseline of charcter */
		Move(rp,indent,screenpos-fheight+myfont->tf_Baseline);
		Text(rp,&buf[start],i);
		indent = 30;
		start += i;
		}
}


/*
 *   Open all the stuff I need
 */
open_all()
{

	/*    Open the screen and window   */

	if ((IntuitionBase = (struct IntuitionBase *)
		OpenLibrary("intuition.library", NULL)) == NULL)
		done(21); /* if no intuition I can't even autorequest */

	if ((MyScreen = (struct Screen *)
		OpenScreen(&NewScreenStructure)) == NULL)
		{
		AutoRequest(0L,&screenfail,0L,&oktxt,0L,0L,300L,75L);
		done(23);
		}

	New_Window.Screen = MyScreen;

	if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
		{
		AutoRequest(0L,&windfail,0L,&oktxt,0L,0L,300L,75L);
		done(25);
		}

	rp = Wind->RPort;

	/**** Open the Libraries   ****/

	GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
	if (GfxBase == NULL)
		{
		strcpy(msgfilename,"Graphics");
		AutoRequest(Wind,&libfailmsg,0L,&oktxt,0L,0L,300L,75L);
		done(20);
		}

	DiskfontBase = (struct Library *)OpenLibrary("diskfont.library", NULL);
	if (DiskfontBase == NULL) {
		strcpy(msgfilename,"DiskFont");
		AutoRequest(Wind,&libfailmsg,0L,&oktxt,0L,0L,300L,75L);
		done(22);
		}


	if ((DosBase = (struct DosBase *)
		OpenLibrary("dos.library", 0)) == NULL)
		{
		strcpy(msgfilename,"Dos");
		AutoRequest(Wind,&libfailmsg,0L,&oktxt,0L,0L,300L,75L);
		done(27);
		}

}
/*
 * done - just clean up that which is open, and then leave.
 */
done(how)
int how;
    {
	if (avlbuf) FreeMem(avlbuf, (long) (bufsize));
	if (myfont != NULL) CloseFont(myfont);
    if (Wind) CloseWindow(Wind) ;
    if (MyScreen) CloseScreen(MyScreen) ;
    if (IntuitionBase) CloseLibrary(IntuitionBase) ;
    if (GfxBase) CloseLibrary(GfxBase) ;
    if (DosBase) CloseLibrary(DosBase) ;
    if (DiskfontBase) CloseLibrary(DiskfontBase) ;
    exit(how) ;
    }



/*
 * clr_scr : clear the window area of the screen
 *
 */

clr_scr()
{
	SetAPen(rp,0L);
	RectFill(rp,0L,SCREENPOSINIT,Wind->Width,Wind->Height);
	SetAPen(rp,1L);
}

_abort()
{
	done(24);
}
