/*
 * $Id: tgif.trm%v 3.50 1993/07/09 05:35:24 woo Exp $
 */

/* N O T   F I N I S H E D ! ! ! ! ! ! ! */

/* GNUPLOT - tgif.trm */
/*
 * Copyright (C) 1990   
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted, 
 * provided that the above copyright notice appear in all copies and 
 * that both that copyright notice and this permission notice appear 
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *  
 * This software  is provided "as is" without express or implied warranty.
 * 
 * This file is included by ../term.c.
 *
 * This terminal driver supports:
 *     tgif
 *
 * AUTHORS
 *  Neal Holtz
 * 
 * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
 * 
 */


/* tgif driver by Neal Holtz, holtz@civeng.carleton.ca */

#include "tgif.h"

#define TGIF_XMAX 750
#define TGIF_YMAX 750

#define TGIF_XLAST (TGIF_XMAX - 1)
#define TGIF_YLAST (TGIF_YMAX - 1)

#define TGIF_VTIC (TGIF_YMAX/80)
#define TGIF_HTIC (TGIF_YMAX/80)

/* TGIF Courier size 5 = 15x24, size 4 = 11x19, size 3 = 9x16
    ascender+descender = 19+5,           15+4,           13+3 */

#define TGIF_VCHAR1	19
#define TGIF_HCHAR1	11
#define TGIF_CHAR_ASC	15
#define TGIF_CHAR_DESC 	(TGIF_VCHAR1-TGIF_CHAR_ASC)

#define YTRANS(y) (TGIF_YMAX-(y))
                         
typedef struct {
	int	x, y;
} Tgif_point;

int	tgif_fileVersion = 10;
int	tgif_just = JUST_L;
int	tgif_ang = ROTATE0;
int	tgif_font = FONT_HEL;
int	tgif_style = STYLE_NR;	/* text style */
int	tgif_size = 4;		/* text size */
int	tgif_asc = TGIF_CHAR_ASC;		/* text ascender, descender */
int	tgif_des = TGIF_CHAR_DESC;

int	tgif_linewidth = LINE_MEDIUM;
int	tgif_dash = 0;

char	*tgif_colour = "yellow";
int	tgif_objId = 100;

#define TGIF_VSINIT	100
#define TGIF_VSINCR	100
int	tgif_vsmax = 0;			/* max possible number of vertices */
int	tgif_nv = 0;			/* current number of vertices */
Tgif_point	*tgif_v = NULL;		/* coordinates of vertices of polyline */



TGIF_AddVertex( x, y )
int	x, y;
{
	Tgif_point	*p;

	if( tgif_v == NULL ) {
		tgif_v = (Tgif_point *)malloc( TGIF_VSINIT*sizeof(Tgif_point) );
		tgif_nv = 0;
		if( tgif_v == NULL ) {
			fprintf( stderr, "Unable to malloc() space for vertices.\n" );
			exit( 1 );
		}
		tgif_vsmax = TGIF_VSINIT;
	}
	if( tgif_nv >= tgif_vsmax ) {
		tgif_v = (Tgif_point *)realloc( tgif_v, (tgif_vsmax+TGIF_VSINCR)*sizeof(Tgif_point) ); 
		if( tgif_v == NULL ) {
			fprintf( stderr, "Unable to realloc() space for vertices.\n" );
			exit( 1 );
		}
		tgif_vsmax += TGIF_VSINCR;
	}
	p = tgif_v + tgif_nv++;
	p->x = x;
	p->y = YTRANS(y);
	if( tgif_nv > 1 && p[-1].x == p[0].x && p[-1].y == p[0].y )
		tgif_nv--;
}


TGIF_Flush( lstyle )
int	lstyle;		/* line style */
{
	int	i;

	if( tgif_nv >= 2 ) {
		fprintf( outfile, "poly('%s',%d,[%d,%d",
			tgif_colour, tgif_nv, tgif_v[0].x, tgif_v[0].y );
		for( i = 1; i < tgif_nv; i++ )
			fprintf( outfile, ",%d,%d", tgif_v[i].x, tgif_v[i].y );
		fprintf( outfile, "],%d,%d,%d,%d,%d,%d,%d,[\n]).\n",
			lstyle, tgif_linewidth, 1,	/* style, width, pen */
			tgif_objId++,
			0, 0,		/* curved, fill */
			tgif_dash );
	}
	tgif_nv = 0;
}
	
	


TGIF_init()
{
	tgif_just = JUST_L;
	tgif_ang = ROTATE0;
	tgif_font = FONT_HEL;
	tgif_style = STYLE_NR;
	tgif_linewidth = LINE_MEDIUM;
	tgif_dash = 0;
	tgif_nv = 0; 
}


TGIF_graphics()
{
	fprintf(outfile,"state(0,%d,0,0,0,16,1,0,2,1,0,0,1,0,1,0,1,0,3,0,0).\n", tgif_fileVersion);
	fprintf(outfile,"%%\n");
	fprintf(outfile,"%% @(#)$Header: /a/woo/src/gwork/term/RCS/tgif.trm%v 3.50 1993/07/09 05:35:24 woo Exp $\n");
	fprintf(outfile,"%%\n");
	tgif_nv = 0;
}


TGIF_text()
{
	TGIF_Flush(LS_PLAIN);
}


TGIF_reset()
{
	TGIF_Flush(LS_PLAIN);
}


TGIF_linetype(linetype)
int linetype;
{
	TGIF_Flush(LS_PLAIN);
	if( linetype == -2 )		/* use thinner lines for border and axis */
		tgif_dash = 0, tgif_linewidth = LINE_THIN;
	else if( linetype == -1 )
		tgif_dash = 1, tgif_linewidth = LINE_THIN;
	else
		tgif_dash = linetype % MAXDASHES, tgif_linewidth = LINE_MEDIUM;
}


TGIF_move(x,y)
unsigned int x,y;
{
	TGIF_Flush(LS_PLAIN);
	TGIF_AddVertex( x, y );
}


TGIF_vector(x,y)
unsigned int x,y;
{
	TGIF_AddVertex( x, y );
}


TGIF_put_text(x,y,str)
unsigned int x, y;
char *str;
{
	TGIF_Flush(LS_PLAIN);
	fprintf( outfile, "text('%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,[\n\t\"%s\"]).\n",
		tgif_colour, 
		x, YTRANS(y)-(TGIF_CHAR_DESC+(TGIF_CHAR_ASC/2)),
		tgif_font, tgif_style, tgif_size,
		1,	/* number of lines */
		tgif_just, tgif_ang,
		1,    	/* pen */
		TGIF_HCHAR1*strlen(str), TGIF_VCHAR1,
		tgif_objId++,
                0, 	/* dpi */
		tgif_asc, tgif_des,
		str );
}

int TGIF_text_angle(ang)
int ang;
{
	TGIF_Flush(LS_PLAIN);
	if( ang == 1 )
		tgif_ang = ROTATE270;
	else
		tgif_ang = ROTATE0;
	return TRUE;
}

int TGIF_justify_text(mode)
enum JUSTIFY mode;
{
	TGIF_Flush(LS_PLAIN);
	switch(mode) {
	case LEFT:	
		tgif_just = JUST_L; 	
		break;
	case CENTRE:	
		tgif_just = JUST_C;	
		break;
	case RIGHT:	
		tgif_just = JUST_R;	
		break;
	default:	
		tgif_just = JUST_L;	
		break;
	}
	return TRUE;
}

TGIF_arrow( sx, sy, ex, ey )
int	sx, sy, ex, ey;
{
	TGIF_Flush(LS_PLAIN);
	TGIF_AddVertex( sx, sy );
	TGIF_AddVertex( ex, ey );
	TGIF_Flush(LS_RIGHT);
}
