#ifndef VFONT_H
#define VFONT_H

#ifndef EXEC_TYPES_H
#include <exec/types.h>
#endif
#ifndef EXEC_LISTS_H
#include <exec/lists.h>
#endif
#ifndef EXEC_LIBRARIES_H
#include <exec/libraries.h>
#endif
#ifndef EXEC_SEMAPHORES_H
#include <exec/semaphores.h>
#endif
#ifndef GRAPHICS_GFX_H
#include <graphics/gfx.h>
#endif
#ifndef GRAPHICS_RASTPORT_H
#include <graphics/rastport.h>
#endif
#ifndef VFONT_CURVE_H
#include <vfont/curve.h>
#endif

/*****************\
|* Debug macros. *|
\*****************/

#define Trace \
if (vFontBase==NULL) KPutStr("NULL-Base!"); \
else if (vFontBase->Flags&VFB_DEBUG) KPrintF("%s %s.\n",__FILE__,__FUNC__)
#define LibStart    geta4(); Trace;
#define Show(fmt,str) \
if (vFontBase==NULL) KPutStr("NULL-Base!"); \
else if (vFontBase->Flags&VFB_DEBUG) KPrintF("%s %s:" fmt,__FILE__,__FUNC__,str)



/**********************\
|* VFont Class stuff. *|
\**********************/

#define CharWidth(f,c)  ((f)->Class->Chars[c].Width)
#define CharHeight(f,c) ((f)->Class->Chars[c].Height)

#define VC_FILL         0x01        /* Draw solid characters. */
#define VC_WIRE         0x02        /* Draw wire character. Contour otherwise*/
#define VC_CONTINUES    0x04        /* Continue on next curve. */    
#define VC_BEND         0x08        /* Bend the line. */
#define VC_CURVE        0x10        /* No end points. */
#define VC_CONTINUED    0x20        /* Continued from the previous curve. */
#define VC_ROUND        0x40        /* Bended line with no end points. */
#define VC_THINCURVE    0x80        /* Should be shrunk when mapped. */

typedef struct VCurve {
    struct  VCurve *Next;           /* The next. */
    Point   *Verticies;             /* The verticies. */
    UWORD   Used;                   /* The number of verticies. */
    UWORD   Size;                   /* The actual space for the curve. */
    WORD    R;                      /* The roundness. */
    UBYTE   E;                      /* The edginess. */
    UBYTE   Flags;                  /* Flags. */
    BYTE    FgPen;                  /* Foreground color. */
    BYTE    BgPen;                  /* Background color. */
    BYTE    OlPen;                  /* Outline color. */
    BYTE    DMode;                  /* Draw mode */
    WORD    sym_x;                  /* Symetry point. */
    WORD    sym_y;                  /* -"- */
} VCurve;

typedef struct VClassChar {
    VCurve  *Curve;
    UWORD   Width;
    UWORD   Height;
} VClassChar;


typedef struct VFontClass {
    struct VFontClass *Next;    /* Next font class. */
    UBYTE       *Name;          /* Symbolic name of the font class. */
    UBYTE       *Author;        /* The name of the author. */
    UWORD       XSize;          /* Size of the average template chars. */
    UWORD       YSize;          /* Size of the average template chars. */
    FIX         UpVector;       /* Default Up direction of char. */
    FIX         Tilt;	        /* Factor of italic text. */
    UBYTE       Flags;          /* The flags. */
    UBYTE       Style;          /* The styles. */
    UBYTE       Locked;         /* Semaphore of exlusive use of class. */
    UBYTE       LowChar;        /* First defined character. */
    UBYTE       HighChar;       /* The last defined character. */
    UBYTE       Space;          /* Default spacing between char. */
    VClassChar  *Chars;         /* The actual definition of chars. */
    UWORD       Baseline;       /* Baseline. */
    UWORD       Users;          /* Pad. */
} VFontClass;



/************************\
|* VFont instance stuff *|
\************************/

#define VFC_FILL        0x01        /* Area fill the character. */
#define VFC_MAPPED      0x02        /* Character is mapped. */
#define VFC_NOCOOKIECUT 0x04        /* Don't render a'la cookie-cut. */
#define VFC_CACHED      0x08
#define VFC_RESERVED    0xf0        /* Reserved for future use. */

#define VCM_NAME        0x01U
#define VCM_SIZE        0x02U
#define VCM_STYLE       0x04U
#define VCM_UP          0x08U
#define VCM_PATH        0x10U
#define VCM_TILT        0x20U
#define VCM_BOLD        0x40U


typedef struct Lines {
    struct  Lines *Next;
    Point   *XY;
    UWORD   Count;
    BYTE    FgPen;
    BYTE    BgPen;
    BYTE    OlPen;
    BYTE    DMode;
} Lines;

typedef struct charDef {
	WORD charOffset;
	WORD charBitWidth;
} CharDef;

typedef struct VFontChar {
    Lines   *Lines;
    FIX     DeltaX;
    FIX     DeltaY;
    UBYTE   Flags;
    UBYTE   pad[3];
} VFontChar;

typedef struct VFont {

    /* struct TextFont compatible part */

    struct Node *ln_Succ;
    struct Node *ln_Pred;
    UBYTE   ln_Type;
    BYTE    ln_Pri;
    UBYTE   *Name;
    struct  MsgPort *ReplyPort;
    UWORD   Length;

    UWORD   TFYSize;
    UBYTE   Style;
    UBYTE   Flags;
    UWORD   TFXSize;
    UWORD   TFBaseline;
    UWORD   BoldSmear;
    UWORD   Accessors;
    UBYTE   LoChar;
    UBYTE   HiChar;
    APTR    CharData;
    UWORD   Modulo;
    CharDef *CharLoc;
    WORD    *CharSpace;
    WORD    *CharKern; 

    /* struct ColorTextFont compatible extension. */
    UWORD   ColorFlags;
    UBYTE   Depth;
    UBYTE   FgColor;
    UBYTE   Low;
    UBYTE   High;
    UBYTE   PlanePick;
    UBYTE   PlaneOnOff;
    struct ColorFontColors *ColorFontColors;
    APTR    ColorCharData[8];

    /* The Vector specific part. */
    UWORD       XSize;
    UWORD       YSize;
    UWORD       Baseline;
    Point       *BasePoints;    /* The origin of each character within CharData.*/
    FIX     	UpVector;	    /* The Up direction of char. */
    FIX     	AlfaPath;       /* The direction of text: X-Y angle */
    FIX         BetaPath;       /* The direction of text: Y-Z angle */
    FIX     	Tilt;	        /* Factor of italic text. */
    struct SignalSemaphore Lock;/* Semaphore for mutual exclusion. */
    VFontChar   *Chars;	        /* Character data. */
    VFontClass  *Class;         /* The definition of the font. */
    struct BitMap BM;           /* A bitmap */
    UWORD       FreeCharOffset; /* Next free bitoffset in the bitmap. */
    UWORD       CacheSize;      /* Size of the cache. */
    struct VFont *Next;
} VFont;



/********************\
|* RasterPort hook. *|
\********************/

#define MAXBINDS 256

typedef struct VFontBind {
    VFont               *VFont;     /* The font. */
    struct  RastPort    *RPort;     /* The rastport. */
    struct  VFontBind   *Next;      /* The next. */
} VFontBind;



/*****************\
|* LoadSeg hook. *|
\*****************/

typedef struct LoadSegs {
    struct LoadSegs     *Next;      /* The next loadseg. */
    ULONG               Seg;        /* The Segment list. */
} LoadSegs;



/*****************************\
|* VFont library base stuff. *|
\*****************************/

#define MyOpenDiskFont  (*(vFontBase->OldOpenDisk))

#define VFB_MUTE        0x0000
#define VFB_DEBUG       0x0001
#define VFB_VERBOSE     0x0002
#define VFB_FLUSHCLASS  0x0004
#define VFB_FLUSHFONTS  0x0008
#define VFB_MAPPED      0x0010
#define VFB_EXCLUSIVE   0x0020
#define VFB_FORCED      0x0040
#define VFB_NOCONVERT   0x0080
#define VFB_ANYSIZE     0x0100
#define VFB_NOBITCACHE  0x0200
#define VFB_RESERVED    0xfc00

typedef struct vFontBase {
    struct      Library Lib;        /* Library stuff. */
    ULONG       SegList;
    VFontClass  *VFontClass;        /* List of font classes. */
    VFontBind   *VFontBinds;        /* List of binding RPort<->VFont. */
    VFont       *DefVFont;          /* List of instansiated fonts. */
    LoadSegs    *DiskFonts;         /* List of loadsegs of diskfonts. */
    struct      List mpool;         /* List with MemLists */
    struct      List epool;         /* List with MemEntries. */
    WORD        Flags;              /* For debug purpose etc. */
    struct Layer *drawable;         /* Private. */
    struct Layer_Info *li;
    ULONG       Users;
    struct SignalSemaphore Lock;    /* Semaphore for mutual exclusion. */
} VFontBase;



/**********************\
|* Vector Text stuff. *|
\**********************/

typedef struct TextVAttr {
    UBYTE   *Name;          /* The name of the desired class or Bitmap font. */
    unsigned YSize : 15;    /* The height. */
    unsigned VectorFont : 1;/* TRUE if vector font. */
    UBYTE   Style;          /* The desired style. */
    UBYTE   Flags;          /* Flags. */
    UBYTE   *FontName;      /* The vector font name. */
    UWORD   XSize;          /* Horizontal size. */
    UWORD   VFlags;         /* Flags for vector fonts. */
    UBYTE   *Type;          /* Indicates the storage format. */
} TextVAttr;

typedef struct VectorText {
    UBYTE       FrontPen, BackPen;
    UBYTE       DrawMode;
    WORD        LeftEdge, TopEdge;
    TextVAttr   *VFontAttr;
    UBYTE       *Text;
    struct      VectorText   *Next;
} VectorText;

/*******************\
|* Usefull macros. *|
\*******************/

#define SetVSize(font, sx, sy, map) {struct VFont req; req.XSize = sx; req.YSize = sy; ChangeVFont(font, (UBYTE)VCM_SIZE, &req, (long)map); }
#endif
