/* GNUPLOT -- gnugraph.trm */
/*
 * Copyright (C) 1993
 *
 * 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:
 *  GNU plot(5) graphics language
 *
 * AUTHORS
 *  Tony Richardson from the unixplot.trm by Colin Kelley, Thomas Williams,
 *  and Russell Lang and from post.trm by Russell Lang.
 * 
 * send your comments or suggestions to (info-gnuplot@ames.arc.nasa.gov).
 */

/*
 * adapted to the new terminal layout by Stefan Bodewig (Dec. 1995)
 */

/*
 * This version of the 'unixplot' driver produces device independent
 * output.  I've chosen parameter values so that the PostScript output
 * produced by plot2ps is 5" x 3".  You can use the 'set size' command
 * to get output up to 8.25" x 8.25", i.e. size values larger than
 * 1 are okay.
 */

/*
Unixplot library writes to stdout.  A fix was put in place by
..!arizona!naucse!jdc to let set term and set output redirect
stdout.  All other terminals write to outfile.
*/

/* This is a device independent format, so the output should look
 * look "reasonable" on any output device.  I set things up there so
 * that the output of plot2ps is 5" x 3" (standard GNUPLOT size).
 * You can use GNUPLOT's size command to obtain plots to almost the
 * 8.25" x 8.25" limit.
 */

#ifndef GOT_DRIVER_H
#include "driver.h"
#endif

#ifdef TERM_REGISTER
register_term(unixplot)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void UP_options __P((void));
TERM_PUBLIC void UP_init __P((void));
TERM_PUBLIC void UP_graphics __P((void));
TERM_PUBLIC void UP_text __P((void));
TERM_PUBLIC void UP_linetype __P((int linetype));
TERM_PUBLIC void UP_move __P((unsigned int x, unsigned int y));
TERM_PUBLIC void UP_vector __P((unsigned int x, unsigned int y));
TERM_PUBLIC void UP_put_text __P((unsigned int x, unsigned int y, char str[]));
TERM_PUBLIC int UP_text_angle __P((int ang));
TERM_PUBLIC int UP_justify_text __P((enum JUSTIFY mode));
TERM_PUBLIC void UP_reset __P((void));
#define UP_XMAX 19859
#define UP_YMAX 11565
/* UP_VCHAR = ((UP_FONTSIZE*UP_YMAX)/(UP_YINCHES*72)) 
            = UP_FONTSIZE*UP_VFONTSC
   UP_HCHAR = ((UP_FONTSIZE/2)*UP_XMAX)/(UP_XINCHES*72))
            = UP_FONTSIZE*UP_HFONTSC
*/

#define UP_VFONTSC  53.5
#define UP_VCHAR    535   /* 10 * VFONTSC */
#define UP_HFONTSC  27.6
#define UP_HCHAR    276   /* 10 * HFONTSC */

#define UP_VTIC (UP_YMAX/80)
#define UP_HTIC (UP_XMAX/80)
#endif /* TERM_PROTO */

#ifndef TERM_PROTO_ONLY
#ifdef TERM_BODY


char up_font[MAX_ID_LEN+1] = "Courier" ; /* name of font */
int up_fontsize = 10;

/* plot2ps produces a 8.25" x 8.25" square. */
#define UP_SCREENX 32768
#define UP_SCREENY 32768
#define UP_SCRXINC 8.25
#define UP_SCRYINC 8.25

/* We want a 5" x 3" graph by default. */
#define UP_XINCHES 5
#define UP_YINCHES 3
/* UP_XMAX = (UP_SCREENX*UP_XINCHES)/UP_SCRXINC
   UP_YMAX (UP_SCREENY*UP_YINCHES)/UP_SCRYINC */

#define UP_XLAST (UP_XMAX - 1)
#define UP_YLAST (UP_YMAX - 1)

/* These offsets center plot2ps output in the middle of the page.  The
 * amount of resizing that can be done is limited. */
/*
 * #define UP_XOFF 6454
 * #define UP_YOFF 10601
 */

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

enum JUSTIFY up_justify=LEFT;

TERM_PUBLIC void UP_options()
{
	if(!END_OF_COMMAND) {
	  if(almost_equals(c_token,"d$efault")) {
		strcpy(up_font,"Courier");
		up_fontsize = 10;
		term->v_char = (unsigned int)(up_fontsize*UP_VFONTSC);
		term->h_char = (unsigned int)(up_fontsize*UP_HFONTSC);
		c_token++;
	  }
	}

        if (!END_OF_COMMAND && isstring(c_token)) {
                quote_str(up_font,c_token, MAX_ID_LEN);
                c_token++;
        }

        if (!END_OF_COMMAND) {
                /* We have font size specified */
                struct value a;
                up_fontsize = (int)real(const_express(&a));
                term->v_char = (unsigned int)(up_fontsize*UP_VFONTSC);
                term->h_char = (unsigned int)(up_fontsize*UP_HFONTSC);
        }

	sprintf(term_options,"\"%s\" %d",up_font,up_fontsize);
}

TERM_PUBLIC void UP_init()
{
	openpl();
	space(0,0,UP_SCREENX-1,UP_SCREENY-1);
	fontname(up_font);
	fontsize(up_fontsize);
}


TERM_PUBLIC void UP_graphics()
{
	erase();
}


TERM_PUBLIC void UP_text()
{
	/* Flush here so that output will be complete. */
	fflush(stdout);
}


TERM_PUBLIC void UP_linetype(linetype)
int linetype;
{
static char *lt[2+5] = {"solid", "longdashed", "solid", "dotted","shortdashed",
	"dotdashed", "longdashed"};

	if (linetype >= 5)
		linetype %= 5;
	linemod(lt[linetype+2]);
}


TERM_PUBLIC void UP_move(x,y)
unsigned int x,y;
{
	move(x+UP_XOFF,y+UP_YOFF);
}


TERM_PUBLIC void UP_vector(x,y)
unsigned int x,y;
{
	cont(x+UP_XOFF,y+UP_YOFF);
}


TERM_PUBLIC void UP_put_text(x,y,str)
unsigned int x,y;
char str[];
{
	UP_move(x,y); /* Don't adjust x and y! It's done in UP_move. */
	switch(up_justify) {
	 case LEFT:
	  alabel('l','c',str);
	  break;
	 case CENTRE:
	  alabel('c','c',str);
	  break;
	 case RIGHT:
	  alabel('r','c',str);
	  break;
	}

}

TERM_PUBLIC int UP_text_angle(ang)
int ang;
{
	rotate(0,0,90*ang);
	return TRUE;
}

TERM_PUBLIC int UP_justify_text(mode)
enum JUSTIFY mode;
{
	up_justify=mode;
	return TRUE;
}

TERM_PUBLIC void UP_reset()
{
	closepl();
}

#endif /* TERM_BODY */

#ifdef TERM_TABLE 

TERM_TABLE_START(unixplot_driver)
    "unixplot", "GNU plot(1) format [\042fontname\042 font_size]",
	   UP_XMAX, UP_YMAX, UP_VCHAR, UP_HCHAR,
	   UP_VTIC, UP_HTIC, UP_options, UP_init, UP_reset,
	   UP_text, null_scale, UP_graphics, UP_move, UP_vector,
	   UP_linetype, UP_put_text, UP_text_angle,
	   UP_justify_text, line_and_point, do_arrow, set_font_null
TERM_TABLE_END(unixplot_driver)

#undef LAST_TERM
#define LAST_TERM unixplot_driver

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

/*
 * NAME: unixplot
 *
 * OPTIONS: font (default Courier) fontsize (default 10)
 *
 * SUPPORTS: GNU plot graphics language
 *
 * Further Info: From top of file:
 *
 * This version of the 'unixplot' driver produces device independent
 * output.  I've chosen parameter values so that the PostScript output
 * produced by plot2ps is 5" x 3".  You can use the 'set size' command
 * to get output up to 8.25" x 8.25", i.e. size values larger than
 * 1 are okay.
 * 
 * There is also a terminal (unixplot) for the non-GNU version but
 * that can only be compiled if this one is left out.
 */