/*
 *	Copyright (c) 1992, Mats Bergstr"om.
 *	All Rights Reserved.
 *  See 'TGIFCRTL.license' for complete information.
 *
 *	TGIFCRTL - Run Time Library in C to create TGIF object files.
 *
 *	Author:	Mats Bergst"om, University of Lund, Sweden.
 *			(Mats.Bergstrom@kosufy.lu.se)
 *
 */

/*	If indetation looks bad, try 4 column tabs! */

#ifndef __TGIFCRTL_H__
#define __TGIFCRTL_H__

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define TR_NAME			"TGIFCRTL"
#define TR_VER			"1.1"
#define OBJ_FILE_EXT	"TGIF"
#define TR_DOTS_PER_MM	(5.0)
#define TR_PAPER_WIDTH	(1056)
#define	TR_PAPER_HEIGHT	(1408)


typedef unsigned int TRbp_t;			/* Basic paramter type.				*/
typedef int TRpc_t;						/* Paper coordinate type.			*/


#define IGNORE 			(0xffff)		/* To ignore parameter!				*/



struct TRline_s {

	TRbp_t	width;						/* Width of lines.					*/
#define LIW_MIN				1
#define LIW_MAX				8

	TRbp_t	style;						/* Line style.						*/
#define LIS_SOLID			0
#define LIS_DOT				1
#define LIS_SHORTDASH		2
#define LIS_DASH			3
#define LIS_DASHDOT			4
#define LIS_DOTDOT			5
#define LIS_DASHDOTDOT		6
#define LIS_LONGDASH		7
#define LIS_SHORTDOT		8
#define LIS_MAX				9

	TRbp_t	type;						/* Type of line, Straight or spline	*/
#define LIT_STRAIGHT		0
#define LIT_SPLINE			1
#define LIT_MAX				2
};



struct TRarrow_s {

	TRbp_t	type;						/* Where an arrow head can occur.	*/
#define ART_PLAIN			0
#define ART_RIGHT			1
#define ART_LEFT			2
#define ART_DOUBLE			3
#define ART_MAX				4

	TRbp_t	state;						/* Scale state.						*/
#define	ARS_DEFAULT			0				/* Use TGIF Built-in values.	*/
#define ARS_EXPLICIT		1				/* Use w/h as is.				*/
#define ARS_SCALE			2				/* Scale w/h with line.width	*/
#define ARS_MAX				3

	TRbp_t	width;
#define ARW_MAX				256

	TRbp_t	height;
#define ARH_MAX				256
};




struct TRfill_s {

	TRbp_t	colour;						/* Fill colour (also line colour)	*/
#define	FIC_MAGNETA 		0
#define FIC_RED				1
#define FIC_GREEN			2
#define FIC_BLUE			3
#define FIC_YELLOW			4
#define FIC_PINK			5
#define FIC_CYAN			6
#define FIC_CADETBLUE		7
#define FIC_WHITE			8
#define FIC_BLACK			9
#define FIC_DARKSLATEGRAY   10
#define FIC_MAX				11

	TRbp_t	pen;						/* Pen pattern.						*/
	TRbp_t	pat;						/* Fill pattern.					*/
#define	FIP_NONE			 0
#define FIP_SOLID			 1
#define FIP_SHADE10			 1
#define FIP_BACKGR			 2
#define FIP_SHADE0			 2
#define FIP_SHADE1			 3
#define FIP_SHADE2			 4
#define FIP_SHADE3			 5
#define FIP_SHADE4			 6
#define FIP_SHADE5			 7
#define FIP_SHADE6			 8
#define FIP_SHADE7			 9
#define FIP_SHADE8			10
#define FIP_SHADE9			11
#define FIP_TWEED			12
#define FIP_TOPPED			13
#define FIP_CHECK			14
#define FIP_BRICK			15
#define FIP_CANVAS			16
#define FIP_SLANTSCALE		17
#define FIP_SLANTEDBRICK	18
#define FIP_SCALE			19
#define FIP_RSLANT1			20
#define FIP_RSLANT2			21
#define FIP_RSLANT3			22
#define FIP_RSLANT4			23
#define FIP_RSLANT5			24
#define FIP_RSLANT6			25
#define FIP_LSLANT1			26
#define FIP_LSLANT2			27
#define FIP_LSLANT3			28
#define FIP_LSLANT4			29
#define FIP_LSLANT5			30
#define FIP_LSLANT6			31
#define FIP_MAX				32
};



struct TRtext0_s {

	TRbp_t	font;						/* Text font.						*/
#define TXF_TIMES			0
#define TXF_COURIER			1
#define TXF_HELVETICA		2
#define TXF_NEW_CENTURY		3
#define TXF_SYMBOLIC		4
#define TXF_MAX				5

	TRbp_t	style;						/* Text font styles.				*/
#define TXS_ROMAN			0
#define TXS_BOLD			1
#define TXS_ITALIC			2
#define TXS_BOLD_ITALIC		3
#define TXS_MAX				4

	TRbp_t	align;						/* Text alignment.					*/
#define TXA_LEFT			0
#define TXA_CENTER			1
#define TXA_RIGHT			2
#define TXA_MAX				3

	TRbp_t	points;						/* Text point size.					*/
#define	TXP_8				0
#define TXP_10				1
#define TXP_11				2
#define TXP_12				3
#define TXP_14x				4
#define TXP_14				5
#define TXP_17				6
#define TXP_18				7
#define TXP_20				8
#define TXP_24				9
#define TXP_25			   10
#define TXP_34			   11
#define TXP_MAX			   12

	TRbp_t	rot;						/* Text path rotation.				*/
#define TXR_0				0
#define TXR_90				1
#define TXR_180				2
#define TXR_270				3
#define TXR_MAX				4

	TRbp_t	space;						/* unused.							*/
	TRbp_t	asc;						/* unused.							*/
	TRbp_t	des;						/* unused.							*/
};



struct TRmark0_s {

	TRbp_t	type;						/* Marker type.						*/
#define MKT_CIRCLE			0
#define	MKT_BOX				1
#define MKT_MAX				2

	TRbp_t	size;						/* Marker size.						*/
#define MKS_MIN				1
#define MKS_MAX			  512
};



struct TRlock_s {

	TRbp_t	state;						/* Lock state of object.			*/
#define LCK_UNLOCKED		0
#define LCK_LOCKED			1
};



struct TRrcbox_s {
	TRbp_t	radius;						/* Radius of corners.				*/
#define	RCR_DEFAULT	   		16
#define RCR_MIN				 4
#define RCR_MAX			   256
};

struct TRrot_s {
	TRbp_t	rot;						/* unused.							*/
};

struct TRtrans_s {						/* Transformation parameters.		*/
											/* UNITS: h0,v0:[DOTS]			*/
											/* .. h1,v1:[DOTS/USER_SPACE]	*/
	double	h0, h1;						/* Horizontal transformation.		*/
	double	v0, v1;						/* vertical transformation.			*/
};

typedef struct TRline_s  TRline_t;
typedef struct TRarrow_s TRarrow_t;
typedef struct TRfill_s  TRfill_t;
typedef struct TRtext0_s TRtext0_t;
typedef struct TRmark0_s TRmark0_t;
typedef struct TRlock0_s TRlock0_t;
typedef struct TRrc0_s   TRrc0_t;


struct TRopen_s {						/* Line and Arc data.				*/
	struct TRarrow_s	arrow;
	struct TRline_s		line;
	struct TRfill_s		fill;
};

struct TRclosed_s {						/* Box, Oval, Polygon, Rcbox.		*/
	struct TRline_s		line;
	struct TRfill_s		fill;
	struct TRrcbox_s	rc;
};

struct TRtext_s {						/* Text data.						*/
	struct TRtext0_s	text;
	struct TRfill_s		fill;
};

struct TRmark_s {						/* Marker data.						*/
	struct TRmark0_s	mark;
	struct TRarrow_s	arrow;
	struct TRline_s		line;
	struct TRfill_s		fill;
	struct TRtext0_s	text;
};


struct TRdata_s {						/* All data.						*/
	struct TRopen_s		open;
	struct TRclosed_s	closed;
	struct TRtext_s		text;
	struct TRmark_s		mark;
	struct TRlock_s		lock;
	struct TRrot_s		rot;
	struct TRtrans_s	trans;
	struct TRdata_s		*next;
};

typedef struct TRdata_s TR_DATA;

struct TRfile_s {
	FILE *			fp;					/* File descriptor pointer.			*/
	int				err;				/* Error status variable.			*/

	TRbp_t			id;					/* Id number of object.				*/
	TRbp_t			is_first;			/* Flag =1 if the obj is the first	*/
											/* in a file or group.			*/

	TRbp_t			glevel;				/* Group level.						*/
	struct TRlock_s	glock;				/* Group lock memory.				*/

	struct TRdata_s	gs;					/* Graphic state data.				*/
	TRbp_t			gsdepth;			/* Depth of graphics state stack.	*/
};

typedef struct TRfile_s TR_FILE;



/*
 *	Basics: open, close, error report.
 */

TR_FILE *	TRopen(char *fnam);			/* Open a file.						*/
void		TRclose(TR_FILE *tp);		/* Close a file.					*/
int			TRerr(TR_FILE *tp);			/* Return 0 if all is ok.			*/


/*
 *	Control: set_xxx, push, pop.
 *
 *  In all set_xxx a parameter can be ignored by use of the macro IGNORE.
 */

void TRset_default(TR_FILE *tp);		/* Set control values to default	*/

void TRset_htrans(TR_FILE *tp,			/* Set transformation horizontally	*/
				 double pmin,				/* min paper coordinate			*/
				 double pmax,				/* max paper coordinate			*/
				 double cmin,				/* min coordinate.				*/
				 double cmax	);			/* max coordinate.				*/

void TRset_vtrans(TR_FILE *tp,			/* Set transformation vertically	*/
				 double pmin,				/* min paper coordinate			*/
				 double pmax,				/* max paper coordinate			*/
				 double cmin,				/* min coordinate.				*/
				 double cmax	);			/* max coordinate.				*/

void TRset_lock(TR_FILE * tp);			/* Objects are locked.				*/
void TRset_unlock(TR_FILE * tp);		/* Objects are unlocked.			*/



void TRset_open_line(TR_FILE *tp,		/* Set graphic state for open shapes*/
				TRbp_t	l_w,				/*  1 Line width				*/
				TRbp_t	l_s,				/*  2 Line style				*/
				TRbp_t	l_t );				/*  3 Line type					*/

void TRset_open_fill(TR_FILE *tp,		/* Set graphic state for open shapes*/
				TRbp_t	f_c,				/*  4 Fill colour				*/
				TRbp_t	f_p,				/*  5 Fill pattern				*/
				TRbp_t	f_l);				/*  6 Fill line-pattern			*/

void TRset_open_arrow(TR_FILE *tp,		/* Set graphic state for open shapes*/
				TRbp_t	a_t,				/*  7 Arrow type				*/
				TRbp_t	a_s,				/*  8 Arrow style				*/
				TRbp_t	a_w,				/*  9 Arrow width				*/
				TRbp_t	a_h );				/* 10 Arrow height				*/


void TRset_closed_line(TR_FILE *tp,		/* Set Graphic state for closed shps*/
				TRbp_t	l_w,				/*  1 Line width				*/
				TRbp_t	l_s,				/*  2 Line style				*/
				TRbp_t	l_t);				/*  3 Line type					*/

void TRset_closed_fill(TR_FILE *tp,		/* Set Graphic state for closed shps*/
				TRbp_t	f_c,				/*  4 Fill colour				*/
				TRbp_t	f_p,				/*  5 Fill pattern				*/
				TRbp_t	f_l);				/*  6 Fill line-pattern			*/

void TRset_closed_rc(TR_FILE *tp,		/* Set Graphic state for closed shps*/
				TRbp_t	c_r );				/*  7 RC Box corner radius		*/


void TRset_text_text(TR_FILE *tp,		/* Set graphic state for text.		*/
				TRbp_t	t_f,				/*  1 Text font					*/
				TRbp_t	t_s,				/*  2 Text Style				*/
				TRbp_t	t_a,				/*  3 Text Alignment			*/
				TRbp_t	t_p,				/*  4 Text Point Size			*/
				TRbp_t	t_r);				/*  5 Text Rotation.			*/

void TRset_text_fill(TR_FILE *tp,		/* Set graphic state for text.		*/
				TRbp_t	f_c,				/*  6 Fill colour				*/
				TRbp_t	f_p,				/*  7 Fill pattern				*/
				TRbp_t	f_l );				/*  8 Fill line-pattern			*/


void TRset_mark_mark(TR_FILE *tp,		/* Set Graphic state for Markers.	*/
				TRbp_t	m_t,				/*  1 Marker Type				*/
				TRbp_t	m_s );				/*  2 Marker Size				*/

void TRset_mark_line(TR_FILE *tp,		/* Set Graphic state for Markers.	*/
				TRbp_t	l_w,				/*  3 Line width				*/
				TRbp_t	l_s,				/*  4 Line style				*/
				TRbp_t	l_t );				/*  5 Line type					*/

void TRset_mark_fill(TR_FILE *tp,		/* Set Graphic state for Markers.	*/
				TRbp_t	f_c,				/*  6 Fill colour				*/
				TRbp_t	f_p,				/*  7 Fill pattern				*/
				TRbp_t	f_l );				/*  8 Fill line-pattern			*/

void TRset_mark_arrow(TR_FILE *tp,		/* Set Graphic state for Markers.	*/
				TRbp_t	a_t,				/*  9 Arrow type				*/
				TRbp_t	a_s,				/* 10 Arrow style				*/
				TRbp_t	a_w,				/* 11 Arrow width				*/
				TRbp_t	a_h );				/* 12 Arrow height				*/

void TRset_mark_text(TR_FILE *tp,		/* Set Graphic state for Markers.	*/
				TRbp_t	t_f,				/* 13 Text font					*/
				TRbp_t	t_s,				/* 14 Text Style				*/
				TRbp_t	t_a,				/* 15 Text Alignment			*/
				TRbp_t	t_p,				/* 16 Text Point Size			*/
				TRbp_t	t_r );				/* 17 Text Rotation.			*/



void TRpush(TR_FILE *tp);				/* Push/Pop the current graphics	*/
void TRpop(TR_FILE *tp);					/* on the graphisc stack.		*/



/*
 *	Drawing: polyline, polygon, oval, box, rcbox, text.
 */

void TRpolyline(TR_FILE *tp,			/* Write a poly line.				*/
				int n,						/* No of points.				*/
				double x[],					/* horizontal points.			*/
				double y[] );				/* vertical ponts.				*/

void TRpolygon(TR_FILE *tp,				/* Write a polygon					*/
				int n,						/* No of points.				*/
				double x[],					/* horizontal points.			*/
				double y[] );				/* vertical ponts.				*/

void TRoval(TR_FILE *tp,				/* Write oval.						*/
				double x0,					/* one corner					*/
				double y0,
				double x1,					/* opposite corner.				*/
				double y1 );

void TRbox(TR_FILE *tp,					/* Write box.						*/
				double x0,					/* one corner					*/
				double y0,
				double x1,					/* opposite corner.				*/
				double y1 );

void TRrcbox(TR_FILE *tp,				/* Write rounded-corner box.		*/
				double x0,					/* one corner					*/
				double y0,
				double x1,					/* opposite corner.				*/
				double y1 );

void TRtext(TR_FILE *tp,				/* Write text.						*/
				double x,					/* position.					*/
				double y,
				char *s  );					/* The string.					*/

/*
 *	Grouping: begin, end.
 */

void TRgroup_begin(TR_FILE *tp);		/* Begin a group object.			*/
void TRgroup_end(TR_FILE *tp);			/* End a group object				*/

/*
 *	Specials: line.
 */

void TRline(TR_FILE *tp,				/* Draw a single line element		*/
				double x0,					/* start and end position		*/
				double y0,
				double x1,
				double y1 );

void TRmark( TR_FILE *tp,				/* Draw a marker at position		*/
				double x0,
				double y0 );

void TRpolymark(TR_FILE *tp,			/* Mark may points.					*/
				int n,						/* No of points.				*/
				double x[],					/* horizontal points.			*/
				double y[]	);				/* vertical ponts.				*/

/*
 *	Misc:	htrans, vtrans, hmm, vmm.
 */

TRpc_t TRhtrans(TR_FILE *tp,			/* Return horiz. paper coordinate.	*/
				double uc   );				/* from user coordinate.		*/

TRpc_t TRvtrans(TR_FILE *tp,			/* Return vert. paper coordinate.	*/
				double uc   );				/* from user coordinate.		*/

double TRhmm(TR_FILE *tp,				/* Transform x to x horizonal mm's	*/
				double x	);
double TRvmm(TR_FILE *tp,				/* Transform x to x vertical mm's	*/
				double x	);

double TRhdot(TR_FILE *tp,				/* Transform x to horizonal dots	*/
				double x	);
double TRvdot(TR_FILE *tp,				/* Transform x to vertical dots		*/
				double x	);

#ifdef __cplusplus
}
#endif

#endif /* __TGIFCRTL_H__ */
