/* GNUPLOT -- gdgif.trm */
/*
 * Copyright (C) 1995
 *
 * 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:
 *  GD GIF library
 *
 * AUTHORS
 *  Alex Woo based on gnugraph.trm

 * send your comments or suggestions to (info-gnuplot@cs.dartmouth.edu).
 * 
 * This version outputs either color or monochrome GIFs.  The default
 * is 640x480 pixels.  This can be changed with the  'set size' command
 * i.e. size values larger than are okay.
 *
 * link with -Lterm/gd -lgd if your directory structure is gnuplot/term/gd
 *
 * gd is not distributed with gnuplot, because of the UNISYS license thing.
 *
 * find out about gd from http://siva.cshl.org/gd/gd.html
 */
#ifndef GOT_DRIVER_H
#include "driver.h"
#endif

#ifdef TERM_REGISTER
register_term(gdgif)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void GD_options __P((void));
TERM_PUBLIC void GD_init __P((void));
TERM_PUBLIC void GD_graphics __P((void));
TERM_PUBLIC void GD_text __P((void));
TERM_PUBLIC void GD_linetype __P((int linetype));
TERM_PUBLIC void GD_move __P((unsigned int x, unsigned int y));
TERM_PUBLIC void GD_vector __P((unsigned int x, unsigned int y));
TERM_PUBLIC void GD_put_text __P((unsigned int x, unsigned int y, char str[]));
TERM_PUBLIC int GD_text_angle __P((int ang));
TERM_PUBLIC int GD_justify_text __P((enum JUSTIFY mode));
TERM_PUBLIC void GD_reset __P((void));

#define GD_XMAX 640
#define GD_YMAX 480

#define GD_XLAST (GD_XMAX - 1)
#define GD_YLAST (GD_YMAX - 1)

#define GD_VCHAR    16   /* default */
#define GD_HCHAR    8   /* default */

#define GD_VTIC (GD_YMAX/80)
#define GD_HTIC (GD_XMAX/80)
#define GOT_NEXT_PROTO
#endif

#ifndef TERM_PROTO_ONLY
#ifdef TERM_BODY

#include "./gd.h"
extern gdFontPtr gdFontSmall;  /* 6x12 */
extern gdFontPtr gdFontLarge;  /* 8x16 */
static gdImagePtr im;
static gdFontPtr GD_font;
static int 	GD_color, GD_coltable[8],GD_black,GD_white,GD_red,GD_green,GD_blue,GD_violet,GD_brown;
static int 	GD_style[8][6];
static int 	GD_posx,GD_posy,GD_moved;	/* position pointer */
static int   	GD_ang;			/* rotation flag */


/* These offsets give a 1" offset from the lower left corner.  This
 * gives a greater range of permissible values in GNUPLOT's size
 * command. */

static enum JUSTIFY GD_justify=LEFT;

TERM_PUBLIC void GD_options()
{
	if(!END_OF_COMMAND) {
	  if(almost_equals(c_token,"d$efault")) {
		GD_font=gdFontLarge;
		term->v_char = (unsigned int)(16);
		term->h_char = (unsigned int)(8);
		c_token++;
	  }
	}

}

TERM_PUBLIC void GD_init()
{
	im = gdImageCreate(GD_XMAX,GD_YMAX);
	GD_white = GD_coltable[0]  = gdImageColorAllocate(im,255,255,255); /* white background */
	GD_black = GD_coltable[1]  = gdImageColorAllocate(im,  0,  0,  0); /* black */
	GD_red   = GD_coltable[2]  = gdImageColorAllocate(im,255,  0,  0); /* red   */
	GD_green = GD_coltable[6]  = gdImageColorAllocate(im,  0,255,  0); /* green */
	GD_blue  = GD_coltable[3]  = gdImageColorAllocate(im,  0,  0,255); /* blue  */
	GD_violet= GD_coltable[4]  = gdImageColorAllocate(im,255,  0,255); /* violet  */
	GD_brown = GD_coltable[5]  = gdImageColorAllocate(im,  0,255,255); /* brown  */
	GD_coltable[7]  = gdImageColorAllocate(im,255,255,  0); /* yellow  */
	 /* Line Styles */
	GD_style[0][0] = GD_black; GD_style[0][1] = GD_black;
	GD_style[0][2] = GD_black; GD_style[0][3] = GD_black;
	GD_style[0][4] = GD_black; GD_style[0][5] = GD_black;  /* solid black */
	GD_style[1][0] = GD_black; GD_style[1][2] = GD_black; 
	GD_style[1][2] = GD_black; GD_style[1][3] = GD_black; 
	GD_style[1][4] = GD_black; GD_style[1][5] = gdTransparent; /*longdashed */
	GD_style[2][0] = GD_black; GD_style[2][1] = GD_black;
	GD_style[2][2] = GD_black; GD_style[2][3] = GD_black;
	GD_style[2][4] = GD_black; GD_style[2][5] = GD_black;  /* solid black */
	GD_style[3][0] = GD_black; GD_style[3][1] = gdTransparent; 
	GD_style[3][2] = GD_black; GD_style[3][3] = gdTransparent; 
	GD_style[3][4] = GD_black; GD_style[3][5] = gdTransparent;/* dotted */
	GD_style[4][0] = GD_red; GD_style[4][1] = GD_red; 
	GD_style[4][2] = GD_red; GD_style[4][3] = gdTransparent; 
	GD_style[4][0] = gdTransparent; GD_style[4][5] = gdTransparent; /* shortdash */
	GD_style[5][0] =  GD_blue; GD_style[5][1] = gdTransparent; 
	GD_style[5][2] =  GD_blue; GD_style[5][3] = GD_blue; 
	GD_style[5][4] =  GD_blue; GD_style[5][5] = gdTransparent; /* dotdash */
	GD_style[6][0] =GD_violet; GD_style[6][1] = GD_violet; 
	GD_style[6][2] =GD_violet; GD_style[6][3] = GD_violet; 
	GD_style[6][4] = gdTransparent; GD_style[6][5] = gdTransparent; /* mediandash */
	GD_style[7][0] = GD_green; GD_style[7][1] = GD_green; 
	GD_style[7][2] = GD_green; GD_style[7][3] = gdTransparent; 
	GD_style[7][4] = gdTransparent; GD_style[7][5] = gdTransparent; /* halfdash */
	
	gdImageSetStyle(im,GD_style[0],6);
	GD_font        = gdFontLarge;
	GD_color       = GD_black;
}


TERM_PUBLIC void GD_graphics()
{

}


TERM_PUBLIC void GD_text()
{
		/* turn on interlace */
	gdImageInterlace(im,1);
	    	/* Output the image to the disk file. */
	gdImageGif(im, outfile);    
		/* Flush here so that output will be complete. */	
	fflush(outfile);
}


TERM_PUBLIC void GD_linetype(linetype)
int linetype;
{
static char *lt[2+5] = {"solid", "longdashed", "solid", "dotted","shortdashed",
	"dotdashed", "longdashed"};
	
	if (linetype >= 5)
		linetype %= 5;
	gdImageSetStyle(im,GD_style[linetype+2],6);
	GD_color	= GD_coltable[linetype+1];
}


TERM_PUBLIC void GD_move(x,y)
unsigned int x,y;
{
	GD_posx = x; GD_posy = y;
	GD_moved = TRUE;
}


TERM_PUBLIC void GD_vector(ux,uy)
unsigned int ux,uy;
{
	gdImageLine(im,GD_posx,GD_YMAX-GD_posy,ux,GD_YMAX-uy,gdStyled);
	GD_move(ux,uy);
}


TERM_PUBLIC void GD_put_text(x,y,str)
unsigned int x,y;
char str[];
{
	
	GD_move(x,y); /* Don't adjust x and y! It's done in GD_move. */
	if (GD_ang == 1) {
		gdImageStringUp(im,GD_font,x,GD_YMAX-y,str,GD_color);
	}else gdImageString(im,GD_font,x,GD_YMAX-y,str,GD_color);

}

TERM_PUBLIC int GD_text_angle(ang)
int ang;
{
	if(ang==1){
		 GD_ang=1;
	}else GD_ang=0;
	return TRUE;
}

TERM_PUBLIC int GD_justify_text(mode)
enum JUSTIFY mode;
{	/* text is always left and bottom justified */
	GD_justify=mode;
	return FALSE;
}

TERM_PUBLIC void GD_reset()
{

}

#endif /* TERM_BODY*/

#ifdef TERM_TABLE

TERM_TABLE_START(gdgif_driver)
	"gif", "GIF format format [\042fontname]",
       GD_XMAX, GD_YMAX, GD_VCHAR, GD_HCHAR,
       GD_VTIC, GD_HTIC, GD_options, GD_init, GD_reset,
       GD_text, null_scale, GD_graphics, GD_move, GD_vector,
       GD_linetype, GD_put_text, GD_text_angle,
       GD_justify_text, do_point, do_arrow
TERM_TABLE_END(gdgif_driver)

#undef LAST_TERM
#define LAST_TERM gdgif_driver

#endif /* TERM_TABLE */
#endif /* TERM_PROTO_ONLY */

/*
 * NAME: gif
 *
 * OPTIONS: claims to have font as option, but if I read the code
 *	    correctly it only supports the setting `default', which
 *	    means 8x16 Font.
 *
 * SUPPORTS: GIF format 
 *
 * Further Info: From top of file:
 *
 * This version outputs either color or monochrome GIFs.  The default
 * is 640x480 pixels.  This can be changed with the  'set size' command
 * i.e. size values larger than are okay.
 *
 * link with -Lterm/gd -lgd if your directory structure is gnuplot/term/gd
 *
 * gd is not distributed with gnuplot, because of the UNISYS license thing.
 *
 * find out about gd from http://siva.cshl.org/gd/gd.html
 *
 */