/****************************************************************************
 * This source file was written by Acorn Computers Limited. It is part of   *
 * the "DrawFile" library for rendering RISCOS Draw files from applications *
 * in C. It may be used freely in the creation of programs for Archimedes.  *
 * It should be used with Acorn's C Compiler Release 2 or later.            *
 *                                                                          *
 * No support can be given to programmers using this code and, while we     *
 * believe that it is correct, no correspondence can be entered into        *
 * concerning behaviour or bugs.                                            *
 *                                                                          *
 * Upgrades of this code may or may not appear, and while every effort will *
 * be made to keep such upgrades upwards compatible, no guarantees can be   *
 * given.                                                                   *
 ***************************************************************************/

/* -> h.DrawTypes
 *
 * Header file containing object types for DrawFile module, level 1
 * Version 0.0 : DAHE, 21 Nov 88: created
 *         0.1 : DAHE, 30 Nov 88: released
 *
 * The types defined here are used for detailed access to objects.
 * This will not normally be #included by a client of DrawFile - it gets
 * pulled in by h.drawLevel1.
 */

typedef int Draw_sizetyp;
typedef int Draw_coltyp;
typedef int Draw_pathwidth;   /* 1 word */
typedef Draw_box Draw_bboxtyp;

typedef enum
{ join_mitred   = 0,
  join_round    = 1,
  join_bevelled = 2
} Draw_jointyp;

typedef enum
{ cap_butt     = 0,
  cap_round    = 1,
  cap_square   = 2,
  cap_triangle = 3
} Draw_captyp;

typedef enum
{ wind_nonzero = 0,
  wind_evenodd = 1
} Draw_windtyp;

typedef enum
{ dash_absent  = 0,
  dash_present = 1
} Draw_dashtyp;

#define packmask_join     0x03
#define packmask_endcap   0x0C
#define packmask_startcap 0x30
#define packmask_windrule 0x40
#define packmask_dashed   0x80
#define packshft_join        0
#define packshft_endcap      2
#define packshft_startcap    4
#define packshft_windrule    6
#define packshft_dashed      7

typedef struct
{ unsigned char joincapwind;       /* 1 byte  */ /* bit 0..1 join         */
                                                 /* bit 2..3 end cap      */
                                                 /* bit 4..5 start cap    */
                                                 /* bit 6    winding rule */
                                                 /* bit 7    dashed       */
  unsigned char reserved8;         /* 1 byte  */
  unsigned char tricapwid;         /* 1 byte  */ /* 1/16th of line width */
  unsigned char tricaphei;         /* 1 byte  */ /* 1/16th of line width */
} Draw_pathstyle;

typedef char Draw_fontref;    /* 1 byte */

typedef struct
{ Draw_fontref fontref;             /* 1 byte  */
  char         reserved8;           /* 1 byte  */
  short        reserved16;          /* 2 bytes */
} Draw_textstyle;   /* 1 word */


typedef unsigned int Draw_fontsize;  /* 4 bytes, unsigned */

typedef struct
{ int typeface;     /* index into fontname table */
  int typesizex;
  int typesizey;
  Draw_coltyp textcolour;    /* text colour RGB */
  Draw_coltyp background;    /* hint for anti-aliased printing RGB */
} fontrec;

typedef enum
{ Draw_OBJFONTLIST = 0,
  Draw_OBJTEXT     = 1,
  Draw_OBJPATH     = 2,
  Draw_OBJSPRITE  = 5,
  Draw_OBJGROUP   = 6,
  Draw_OBJTEXTAREA = 9,
  Draw_OBJTEXTCOL  = 10
} Draw_tagtyp;

typedef struct { int x,y; } Draw_objcoord;

/* ------------------------------------------------------------------------- */
/*                                                                           */
/* File header                                                               */
/*                                                                           */

typedef struct
{ char title[4];
  int  majorstamp;
  int  minorstamp;
  char progident[12];
  Draw_bboxtyp   bbox;      /* 4 words */
} Draw_fileheader;


/* ------------------------------------------------------------------------- */
/* General header for graphic objects - on other objects, tag & size exist   */
/*                                      other fields are unused and may not  */
/*                                                                           */

typedef struct
{ Draw_tagtyp    tag;       /* 1 word  */
  Draw_sizetyp   size;      /* 1 word  */
  Draw_bboxtyp   bbox;      /* 4 words */
} Draw_objhdr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* A font list                                                               */
/*                                                                           */

typedef struct
{ Draw_tagtyp    tag;       /* 1 word  */
  Draw_sizetyp   size;      /* 1 word  */
} Draw_fontliststrhdr;

typedef struct
{ Draw_tagtyp    tag;       /* 1 word  */
  Draw_sizetyp   size;      /* 1 word  */

  Draw_fontref   fontref;       /* >>>>> check size        */
  char           fontname[1];   /* String, null terminated */
} Draw_fontliststr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* A line of text                                                            */
/*                                                                           */

typedef struct
{ Draw_tagtyp    tag;        /* 1 word  */
  Draw_sizetyp   size;       /* 1 word  */
  Draw_bboxtyp   bbox;       /* 4 words */
  Draw_coltyp    textcolour; /* 1 word  */
  Draw_coltyp    background; /* 1 word  */
  Draw_textstyle textstyle;  /* 1 word  */
  Draw_fontsize  fsizex;     /* 1 word, unsigned */
  Draw_fontsize  fsizey;     /* 1 word, unsigned */
  Draw_objcoord  coord;      /* 2 words */
} Draw_textstrhdr;

typedef struct
{ Draw_tagtyp    tag;        /* 1 word  */
  Draw_sizetyp   size;       /* 1 word  */
  Draw_bboxtyp   bbox;       /* 4 words */
  Draw_coltyp    textcolour; /* 1 word  */
  Draw_coltyp    background; /* 1 word  */
  Draw_textstyle textstyle;  /* 1 word  */
  Draw_fontsize  fsizex;     /* 1 word, unsigned */
  Draw_fontsize  fsizey;     /* 1 word, unsigned */
  Draw_objcoord  coord;      /* 2 words */

  char           text[1];   /* String, null terminated */
} Draw_textstr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* Elements within a path                                                    */
/*                                                                           */

typedef enum
 { Draw_PathTERM  = 0,     /* end of path                                   */
   Draw_PathMOVE  = 2,     /* move to (x,y), starts new subpath             */
   Draw_PathLINE  = 8,     /* line to (x,y)                                 */
   Draw_PathCURVE = 6,     /* bezier curve to (x3,y3) with 2 control points */
   Draw_PathCLOSE = 5      /* close current subpath with a line             */
} Draw_path_tagtype;

typedef struct { Draw_path_tagtype tag; int x,y; } Path_movestr;

typedef struct { Draw_path_tagtype tag; int x,y; } Path_linestr;

typedef struct { Draw_path_tagtype tag; int x1,y1;
                                        int x2,y2; int x3,y3; } Path_curvestr;

typedef struct { Draw_path_tagtype tag; } Path_closestr;

typedef struct { Draw_path_tagtype tag; } Path_termstr;

typedef union           /* Use ONLY for space checking purposes */
{ Path_movestr a;Path_linestr b;Path_curvestr c;Path_closestr d;Path_termstr e;
} Largest_path_str;


typedef union
{ Path_movestr  *move;
  Path_linestr  *line;
  Path_curvestr *curve;
  Path_closestr *close;
  Path_termstr  *term;

  char *bytep;
  int  *wordp;
} Path_eleptr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* Optional dashpattern                                                      */
/*                                                                           */

typedef struct
{ int dashstart;       /* 1 word */           /* distance into pattern */
  int dashcount;       /* 1 word */           /* number of elements    */
} Draw_dashstrhdr;

typedef struct
{ int dashstart;       /* 1 word */           /* distance into pattern */
  int dashcount;       /* 1 word */           /* number of elements    */
  int dashelements[6]; /* dashcount words */  /* elements of pattern   */
} Draw_dashstr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* Unpacked joint,start cap,end cap structure - as fed to draw_stroke etc    */
/*                                                                           */

typedef struct
{ char join;
  char endcap;
  char startcap;
  char reserved;  /* must be zero */
  int  mitrelimit;
  short endtricapwid;
  short endtricaphei;
  short starttricapwid;
  short starttricaphei;
} Draw_jointspec;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* A path                                                                    */
/*                                                                           */

typedef struct
{ Draw_tagtyp    tag;        /* 1 word  */
  Draw_sizetyp   size;       /* 1 word  */
  Draw_bboxtyp   bbox;       /* 4 words */
  Draw_coltyp    fillcolour; /* 1 word  */
  Draw_coltyp    pathcolour; /* 1 word  */
  Draw_pathwidth pathwidth;  /* 1 word  */
  Draw_pathstyle pathstyle;  /* 1 word  */
} Draw_pathstrhdr;

typedef struct
{ Draw_tagtyp    tag;        /* 1 word  */
  Draw_sizetyp   size;       /* 1 word  */
  Draw_bboxtyp   bbox;       /* 4 words */
  Draw_coltyp    fillcolour; /* 1 word  */
  Draw_coltyp    pathcolour; /* 1 word  */
  Draw_pathwidth pathwidth;  /* 1 word  */
  Draw_pathstyle pathstyle;  /* 1 word  */

  Draw_dashstr data;      /* optional dash pattern, then path elements */
 int PATH;
} Draw_pathstr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* A sprite                                                                  */
/*                                                                           */

typedef struct
{ Draw_tagtyp    tag;       /* 1 word  */  /* NB fileIO_xxx routines assume  */
  Draw_sizetyp   size;      /* 1 word  */  /*    a draw_spristrhdr is all    */
  Draw_bboxtyp   bbox;      /* 4 words */  /*    that needs adding to change */
} Draw_spristrhdr;

typedef struct
{ Draw_tagtyp    tag;       /* 1 word  */
  Draw_sizetyp   size;      /* 1 word  */
  Draw_bboxtyp   bbox;      /* 4 words */
  sprite_header  sprite;
  int            palette[1]; /* depends on sprite.mode (or not present) */
} Draw_spristr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* A grouping                                                                */
/*                                                                           */

typedef struct { char ch[12]; } Draw_groupnametyp;  /* 12 bytes */

typedef struct
{ Draw_tagtyp       tag;   /* 1 word   */
  Draw_sizetyp      size;  /* 1 word   */
  Draw_bboxtyp      bbox;  /* 4 words  */
  Draw_groupnametyp name;  /* 12 bytes */
} Draw_groustr;                             /* >>>> would a field for the  */
                                            /*      first object be useful */


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* A text column                                                             */
/*                                                                           */
/* These only appear nested within a text area                               */

typedef struct
{ Draw_tagtyp    tag;        /* 1 word  */
  Draw_sizetyp   size;       /* 1 word  */
  Draw_bboxtyp   bbox;       /* 4 words */
} Draw_textcolhdr;


/* ------------------------------------------------------------------------- */
/*                                                                           */
/* A text area object (new style)                                            */
/*                                                                           */
/*                                                                           */

typedef struct
{ Draw_tagtyp    tag;        /* 1 word  */
  Draw_sizetyp   size;       /* 1 word  */
  Draw_bboxtyp   bbox;       /* 4 words */
  Draw_textcolhdr column;    /* Hook for pointing to text columns */
} Draw_textareastrhdr;

typedef struct
{ Draw_tagtyp    tag;        /* 1 word  */
  Draw_sizetyp   size;       /* 1 word  */
  Draw_bboxtyp   bbox;       /* 4 words */
  /* Text columns go in here */
} Draw_textareahdr;

/* End structure - follows all the column */
typedef struct               /* Structure for getting size */
{
  int            endmark;    /* 1 word, always 0 */
  int            blank1;     /* 1 word, reserved for future expansion */
  int            blank2;     /* 1 word, reserved for future expansion */
  Draw_coltyp    textcolour; /* 1 word */
  Draw_coltyp    backcolour; /* 1 word */
  /* String goes in here */
} Draw_textareastrend;

typedef struct
{
  int            endmark;    /* 1 word, always 0 */
  int            blank1;     /* 1 word, reserved for future expansion */
  int            blank2;     /* 1 word, reserved for future expansion */
  Draw_coltyp    textcolour; /* 1 word */
  Draw_coltyp    backcolour; /* 1 word */
  char           text[1];    /* String, null terminated */
} Draw_textareaend;
