#ifndef __TTF_FONT_H
#define __TTF_FONT_H

/*
   The names of the structures and its members are according to Microsoft's
   TTF font reference manual, so you can learn the meaning of variables
   by reading the reference.
*/

#ifndef __TTF_H
#include "ttf.h"
#endif

#ifndef __TTF_BITMAP_H
#include "bitmap.h"
#endif

typedef struct {
  BYTE valid;
  SHORT numberOfContours;
  FWord xMin,yMin,xMax,yMax;
  USHORT *endPtsOfContours;
  USHORT instructionLength;
  BYTE *instruction;
  BYTE *flags;
  LONG *xCoordinates;
  LONG *yCoordinates;
} OUTLINE;

#define FLAG_ON_CURVE                   1
#define FLAG_X_SHORT_VECTOR             2
#define FLAG_Y_SHORT_VECTOR             4
#define FLAG_REPEAT                     8
#define FLAG_X_SAME                    0x10
#define FLAG_Y_SAME                    0x20
 
typedef struct {
  Fixed fontRevision;
  USHORT flags;
  USHORT unitsPerEm;
  FWord xMin,yMin,xMax,yMax;
  USHORT macStyle;
  USHORT lowestRecPPEM;
  SHORT fontDirectionHint;
  SHORT indexToLocFormat;
  SHORT glyphDataFormat;
} HEAD;

typedef struct {
  uFWord advanceWidth;
  FWord lsb;
} HorMetric;

typedef struct {
  FWord ascender;
  FWord decender;
  FWord lineGap;
  uFWord advanceWidthMax;
  FWord minLeftSideBearing;
  FWord minRightSideBearing;
  FWord xMaxExtent;
  SHORT caretSlopeRise;
  SHORT caretSlopeRun;
  SHORT metricDataFormat;
  USHORT numberOfHMetrics;
  HorMetric *longHorMetrics;
  FWord *leftSideBearing;
} HORIZON;

typedef struct {
  USHORT numglyphs;
  USHORT maxPoints;
  USHORT maxContours;
  USHORT maxCompositePoints;
  USHORT maxCompositeContours;
  USHORT maxZones;
  USHORT maxTwilightPoints;
  USHORT maxStorage;
  USHORT maxFunctionDefs;
  USHORT maxInstructionDefs;
  USHORT maxStackElements;
  USHORT maxSizeOfInstructions;
  USHORT maxComponentElements;
  USHORT maxComponentDepth;
} PROFILE;

typedef struct {
  BYTE glyphIdArray[256];
} CMAP0;

typedef struct {
  USHORT subHeaderKeys[256];
  USHORT *data,*glyphIdArray;
} CMAP2;

typedef struct {
  USHORT format;
  USHORT PlateformID,EncodingID;
  union {
    CMAP0 *cmap0;
    CMAP2 *cmap2;
#if 0
    CMAP4 *cmap4;
    CMAP6 *cmap6;
#endif
  } map;
} CMAP;

typedef struct {
  Fixed version;
  char *name;
  int res,EM;
  FILE *fp;
  /* curmap is the active CMAP encoding */
  CMAP *cmap,*curmap;
  USHORT numberOfEncoding;
  ULONG glyfoff;
  OUTLINE *glyf;
  HEAD *head;
  HORIZON *horz;
  ULONG *indexToLocation;
  PROFILE *profile;
  USHORT *cvt;
  BYTE *fpgm,*prep;
  USHORT cvtlen,fpgmlen,preplen;
} Font;


void expandfont(int fw,int fh,int fg,void *f1,void *f2);
Font *Font_Init(char *fontFileName);
void Font_select_CMAP(Font *font,USHORT plateID,USHORT encordID);
int Font_getFontOffset(Font *font,BYTE *cc);
void Font_Render_Char(Font *font,BITMAP *bit,BYTE *cc);

#endif
