
#include "intuition/intuitionbase.h"
#include "libraries/dosextens.h"
#include "libraries/diskfont.h"

#define Wr(fh,string)       Write(fh,string,(long)sizeof(string))
#define Wrlen(fh,string)    Write(fh,string,(long)strlen(string))

#define FBUFSIZE 40

long DiskfontBase = 0;

struct TextAttr ta =
{
    0,   /* name must be installed manually */
    8,
    0,
    FPF_DISKFONT
};

struct NewWindow nwindow =
{
    0,0,30,30,
    0,0,
    NULL,
    RMBTRAP | SMART_REFRESH | NOCAREREFRESH | BORDERLESS,
    NULL, NULL,
    NULL,
    NULL, NULL, -1, -1, -1, -1, WBENCHSCREEN
};

char plea[] = "Please use Kickstart/Workbench 1.2 for latest ";

struct IntuitionBase *IntuitionBase=NULL;
struct GfxBase *GfxBase=NULL;

char charcodes[] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-=\\_+|[{]};:'\",<.>/?";

void _wb_parse() {}

main(argc,argv)
int argc;
char *argv[];
{
    register struct RastPort *rp;
    register int count, x;

    UBYTE buffer[8];
    register UBYTE *buf = buffer;
    register char *charcode = charcodes;

    struct FileHandle *fp=NULL;
    struct Window *w = NULL;        /* this is the one I open */

    struct FileHandle *Output();
    struct FileHandle *out = Output();

    char filename[128];

    struct TextFont *oldfont,*font=NULL;
    UBYTE fontname[40];

    if (argc == 2)
    {
       strncpy(fontname, argv[1], FBUFSIZE);
       strncat(fontname, ".font", FBUFSIZE);
    }
    else
    {
        Wr(out,"\nUsage: snipgen <fontname>\nYou must have the font present in FONTS:\nPut the output file in current directory or in S: for SNIP to use.\n");
        goto breakpoint;
    }

    if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",33L)))
    {
        Wrlen(out,plea);
        Wr(out,"Intuition.\n");
        goto breakpoint;
    }

    if (!(GfxBase=(struct GfxBase *) OpenLibrary("graphics.library",33L)))
    {
        Wrlen(out,plea);
        Wr(out,"Graphics.\n");
        goto breakpoint;
    }

    if (!(DiskfontBase = (long)OpenLibrary("diskfont.library", 33L)))
    {
        Wrlen(out,plea);
        Wr(out,"Fonts.\n");
        goto breakpoint;
    }

    ta.ta_Name = fontname;

    if (!(font = OpenDiskFont(&ta)))
    {
        Wr(out,"Can't open font\n");
        goto breakpoint;
    }

    strcpy(filename,argv[1]);
    strcat(filename,".fontdef");

    if (!(fp = Open(filename,MODE_NEWFILE)))
    {
        Wr(out, "Unable to open output file\n");
        goto breakpoint;
    }

    if (!(w = OpenWindow(&nwindow)))
    {
        Wr(out,"Can't open window\n");
        goto breakpoint;
    }

    rp = w->RPort;

    oldfont = rp->Font;

    /* If the SetFont() works, on cleanup, CloseFont(oldfont) */

    if (SetFont(rp, font))
        font = oldfont;

    Wr(out,"Encoding 95 characters (should be 760 bytes)\n");
    Wr(out,"Saving in file ");
    Wrlen(out,filename);
    Wr(out,"\n");

    for (count = 0; count < 95; count++)
    {
        SetAPen(rp,0L);
        SetDrMd(rp,JAM1);
        RectFill(rp,10L,10L,18L,18L);
        SetAPen(rp,1L);
        Move(rp,10L,16L);
        Text(rp,&charcode[count],1L);
        get_byte(rp,10,10,buf);
        for (x = 0; x < 8; x++)
        {
            Write(fp,&buf[x],1L);
        }
    }

breakpoint:
    if (fp)             Close(fp);
    if (w)              CloseWindow(w);
    if (IntuitionBase)  CloseLibrary(IntuitionBase);
    if (GfxBase)        CloseLibrary(GfxBase);
    if (font)           CloseFont(font);
    if (DiskfontBase)   CloseLibrary(DiskfontBase);
    exit(0);
}

/*
 *  Given the (x,y) coordinate of the upper left of an 8x8 character position
 *  within a window, this fills the 8-byte buffer with the bitmap of the
 *  character at that position.
 *
 */

get_byte(rp, xstart, ystart, buf)
register struct RastPort *rp;
register int xstart, ystart;
register UBYTE buf[8];
{
    register int x,y;
    register UBYTE tx_byte;

    for (y=0; y<8; y++)
    {
        tx_byte = 0;

        for (x=0; x<8; x++)
        {
            /* NB: I match _any_ colour here */

            if (ReadPixel(rp, (long)(xstart+x), (long)(ystart+y)))
            {
                tx_byte |= (1 << (7-x));
            }
        }

        buf[y] = tx_byte;
    }
}

