#include <exec/types.h>
#include <graphics/gfx.h>
#include <graphics/text.h>
#include <intuition/intuition.h>
#include <libraries/diskfont.h>
#include <functions.h>

struct GfxBase       *GfxBase;
struct IntuitionBase *IntuitionBase;
ULONG                *DiskfontBase;
struct Window        *Window;
struct TextFont      *TextFont;

#define INTUITION_REV 33L

struct TextAttr TA =
{ NULL,
  NULL,
  FS_NORMAL,
  FPF_DISKFONT
};

struct NewWindow NewWindow =
{ 0, 0, 640, 30,
  0, 1,
  CLOSEWINDOW,
  NULL,
  NULL,
  NULL,
  (UBYTE *)"FontSize",
  NULL,
  NULL,
  0, 0, 640, 30,
  WBENCHSCREEN
};

main()
{

 char strng[80];
 char Fname[30];
 char *s, *e;
 struct RastPort *Rport;
 struct TextFont *Fptr;
 USHORT          *Sptr;

 USHORT Pixels, Length, Size, i, j;
 USHORT Offset;

 OpenAll();

 Rport = (struct RastPort *) (*Window).RPort;

 FOREVER
  {
   for(i=0; i != 80; strng[i++] = NULL);

   printf("input your Scan Line :\n");

   gets(strng);
   if(strng[0] == '/')
   {
    switch ((char) (strng[1]))
    {
     case 'i':
     case 'I':
      {
       AskFont(Rport, &TA);
       printf("%s, %d\n", TA.ta_Name, TA.ta_YSize);
       break;
      }
     case 'f':
     case 'F':
      {
       s = strng + 3;
       while(*(s++) != ' ');
       s--;
       *(s++) = NULL;
       strcpy(Fname, &strng[3]);
       Size = atoi(s);
       TA.ta_YSize = Size;
       TA.ta_Name = Fname;
       printf("%s\n  %d\n",Fname, Size);
       TextFont = (struct TextFont *)
                   OpenDiskFont(&TA);
       if (TextFont == NULL)
        printf("Font not found.\n");
       else
        SetFont(Rport, TextFont);
       break;
      }
     case 'q':
     case 'Q':
      {
       CloseAll();
       exit(TRUE);
       break;
      }
     default:
     { 
      printf("Unrecognized command!\n");
      break;
     }
    }
   }
   else
   {
    Length = strlen(strng);
    printf("%d\n", Length);
    Fptr = (*(*Window).RPort).Font;
    Sptr = (*Fptr).tf_CharSpace;

    if ( Sptr == NULL )
      Pixels = ((*Fptr).tf_XSize) * Length;
    else
    {
      for(i=0; !(i == Length) ; i++)
        if((strng[i] < (*Fptr).tf_LoChar) || (strng[i] > (*Fptr).tf_HiChar))
        {
          printf("Character \"%s\" is not a member of the font!\n", strng[i]);
          goto Stop;
          break;
        }
      Pixels = 0;
      for(i=0; !(i == Length) ; i++)
      {
        Offset = strng[i] - (*Fptr).tf_LoChar;
        Pixels = Pixels + (*Sptr + Offset);
      }
    }
    printf("Text \"%s\" is %d letters, %d pixels wide.\n", strng, Length, Pixels);
   }
Stop:;             /* Who says Gotos are evil? */
  }
}

CloseAll()
{
if(TextFont)      CloseFont(TextFont);
if(Window)        CloseWindow(Window);
if(DiskfontBase)  CloseLibrary(DiskfontBase);
if(IntuitionBase) CloseLibrary(IntuitionBase);
if(GfxBase)       CloseLibrary(GfxBase);
}

OpenAll()
{
 if ((GfxBase = (struct GfxBase *)
      OpenLibrary("graphics.library", INTUITION_REV)) == NULL )
  { printf("graphics\n");
    CloseAll();
    exit(FALSE); }

printf("1");

 if ((IntuitionBase = (struct IntuitionBase *)
      OpenLibrary("intuition.library", INTUITION_REV)) == NULL )
  { printf("intuition\n");
    CloseAll();
    exit(FALSE); }

printf("2");

 if ((DiskfontBase =
      OpenLibrary("diskfont.library",INTUITION_REV)) == NULL )
  { printf("diskfont\n");
    CloseAll();
    exit(FALSE); }

printf("3");

 if ((Window = OpenWindow(&NewWindow)) == NULL )
  { printf("window\n");
    CloseAll();
    exit(FALSE); }
}

