/* This is a program to demonstrate reversed fonts.  It was compiled */
/* under Greenhills C, and works under V1.1 and V1.2		     */
/* This program is only a quick hack, so it isn't the neatest thing  */
/* in the world.						     */
/* (C) 1986 Commodore/Amiga					     */
/* Use all or part of this program in any way that makes you happy   */
/* andy finkel							     */

/* By the way, if you just want to see it work, and don't want to change   */
/* the font header file (ruby), just rename your real ruby 12 as something */
/* else, call this one 12, and take out the REVPATH flag in the 	   */
/* OpenDiskFont call.							   */
/* If you do add another entry for it, call it 12r			   */

#include "exec/types.h"
#include "graphics/gfx.h"
#include "graphics/text.h"
#include "intuition/intuition.h"
#include "libraries/dos.h"
#include "libraries/diskfont.h"
#include "workbench/workbench.h"
 
#define BLUE 0 
#define WHITE 1 
#define BLACK 2 
#define RED 3 

extern struct Library *OpenLibrary();
extern struct Window *OpenWindow();

struct Window *window;
struct TextFont *tf;
struct AvailFontsHeader *afh;
struct AvailFonts *af;
struct TextAttr ta;
int afSize;

ULONG *IntuitionBase;
ULONG *DosBase;
ULONG *GfxBase;
ULONG *DiskfontBase;

struct NewWindow nw = {
        0, 0,        /* starting position (left,top) */
        640,200,        /* width, height */
        BLUE,WHITE,     /* detailpen, blockpen */
	 /* idcmp flags */
        NULL,
	/* window gadget flags */
        WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|ACTIVATE,
        NULL,           /* pointer to 1st user gadget */
        NULL,           /* pointer to user check */
        "Font Test V1.0", /* title */
        NULL,           /* pointer to window screen */
        NULL,           /* pointer to super bitmap */
        100,45,         /* min width, height */
        640,-1,        /* max width, height */
        WBENCHSCREEN};
 
main(argc,argv)
int argc;
char *argv[];
{
tf=NULL;
afh=NULL;
window=NULL;

if((IntuitionBase=OpenLibrary("intuition.library",0)) == NULL) Error();

if((DosBase = OpenLibrary(DOSNAME, 0)) == NULL) Error();

if((GfxBase = OpenLibrary("graphics.library", 0)) == NULL) Error();

if((DiskfontBase = OpenLibrary("diskfont.library", 0)) == NULL)	Error();

if((window=OpenWindow(&nw)) == NULL) Error();

SetDrMd(window->RPort,JAM2);
SetAPen(window->RPort,WHITE);
SetBPen(window->RPort,BLUE);

initFonts();

ta.ta_Name= "ruby.font";
ta.ta_YSize=12;
ta.ta_Style =0;
ta.ta_Flags = FPF_ROMFONT|FPF_DISKFONT|FPF_PROPORTIONAL|FPF_DESIGNED|FPF_REVPATH;

tf = (struct TextFont *) OpenDiskFont(&ta);
if(tf!=NULL) {
SetFont(window->RPort, tf);

Move(window->RPort,500,100);
Text(window->RPort,"This is the Reversed Ruby Font",30);
Move(window->RPort,500,120);
Text(window->RPort,"Demonstrating that Reversed Fonts Do Work",41);

Wait(500);
}
cleanup();
exit(0);
}

Error()
{
cleanup();
exit(-1);
}

cleanup()
{
struct Process *process;

if(tf != NULL)CloseFont(tf);

if(afh != NULL)FreeMem(afh, afSize);

if(window != NULL){
        CloseWindow(window);
}

if(GfxBase != NULL)CloseLibrary(GfxBase);
if(DosBase != NULL)CloseLibrary(DosBase);
if(DiskfontBase != NULL)CloseLibrary(DiskfontBase);

OpenWorkBench(); /* just in case */
if(IntuitionBase != NULL)CloseLibrary(IntuitionBase);
return(0);
}

initFonts()
{
struct TextAttr ta;
int status;

if(afh != NULL)FreeMem(afh, afSize); /* this now can be called again */
if(tf != NULL)CloseFont(tf);

afSize = 600; /* initial buffer size */
if((afh = (struct AvailFontsHeader *) AllocMem(afSize, 0))==0) {
        Error();
}

status=AvailFonts(afh, afSize, AFF_MEMORY|AFF_DISK);
if (status) { /* we need more memory */
        FreeMem(afh, afSize);
        afSize +=status;

        if((afh = (struct AvailFontsHeader *) AllocMem(afSize, 0))==0) {
                Error();
	}
status=AvailFonts(afh, afSize, AFF_MEMORY|AFF_DISK);
}
}
