The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
/*
 * $Id: gnugraph.trm,v 1.18 2002/07/26 16:42:29 mikulik Exp $
 */

/* GNUPLOT -- gnugraph.trm */

/*[
 * Copyright 1993, 1998
 *
 * 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 complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/

/*
 * 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 gpoutfile.
*/

/* 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.
 */

#include "driver.h"

#ifdef TERM_REGISTER
register_term(unixplot)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void UP_options __PROTO((void));
TERM_PUBLIC void UP_init __PROTO((void));
TERM_PUBLIC void UP_graphics __PROTO((void));
TERM_PUBLIC void UP_text __PROTO((void));
TERM_PUBLIC void UP_linetype __PROTO((int linetype));
TERM_PUBLIC void UP_move __PROTO((unsigned int x, unsigned int y));
TERM_PUBLIC void UP_vector __PROTO((unsigned int x, unsigned int y));
TERM_PUBLIC void UP_put_text __PROTO((unsigned int x, unsigned int y, const char str[]));
TERM_PUBLIC int UP_text_angle __PROTO((int ang));
TERM_PUBLIC int UP_justify_text __PROTO((enum JUSTIFY mode));
TERM_PUBLIC void UP_reset __PROTO((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

#define DEFAULT_GNUGRAPHFONT "Courier"

/* Name of font */
char up_font[MAX_ID_LEN+1] = DEFAULT_GNUGRAPHFONT;
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;

#define ROTATE(angle) pl_textangle(90*angle)

static int handle_global;              /* T.Walter 1999-05-23 */

/* We don't include plot.h from plotutils because of
 * the name clash with ../plot.h.
 */
extern int pl_openpl __PROTO((void));
extern int pl_space __PROTO((int, int, int, int));
extern int pl_fontname __PROTO((const char *));
extern int pl_fontsize __PROTO((int));
extern int pl_erase __PROTO((void));
extern int pl_linemod __PROTO((const char *));
extern int pl_move __PROTO((int, int));
extern int pl_cont __PROTO((int, int));
extern int pl_alabel __PROTO((int, int, const char *));
extern int pl_textangle __PROTO((int));
extern int pl_closepl __PROTO((void));
/* T.Walter added: */
extern int pl_parampl __PROTO((const char *parameter, void *value));
extern int pl_newpl __PROTO((const char *type, FILE *infile, FILE *outfile, FILE *errfile));
extern int pl_selectpl __PROTO((int handle));
extern int pl_deletepl __PROTO((int handle));
extern double pl_ffontsize __PROTO((double size));

/* Formats supported by GNU plotutils */
enum PL_types {
    PL_X, PL_PNM, PL_GIF, PL_AI, PL_PS, PL_CGM, PL_FIG, PL_PCL5,
    PL_HPGL, PL_TEK, PL_META, PL_INVALID
};

struct gen_table term_up_pt_tbl[] =
{
    { "X", PL_X },
    { "pnm", PL_PNM },
    { "gif", PL_GIF },
    { "ai", PL_AI },
    { "ps", PL_PS },
    { "cgm", PL_CGM },
    { "fig", PL_FIG },
    { "pcl5", PL_PCL5 },
    { "hpgl", PL_HPGL },
    { "tek", PL_TEK },
    { "meta", PL_META },
    { NULL, PL_INVALID }
};

/* Plot type, default */
static int pl_plottype = PL_META;

/* Plot size, default */
static char pl_pagesize[MAX_ID_LEN+1] = "a4";

enum UP_id { UP_DEFAULT, UP_PAGESIZE, UP_PLOTTYPE, UP_OTHER };

static struct gen_table UP_opts[] =
{
    { "d$efault", UP_DEFAULT },
    { "s$ize", UP_PAGESIZE },
    { "t$ype", UP_PLOTTYPE },
    { NULL, UP_OTHER }
};

TERM_PUBLIC void
UP_options()
{
    struct value a;
  
    while (!END_OF_COMMAND) {
	switch(lookup_table(&UP_opts[0],c_token)) {
	case UP_DEFAULT:
	    strcpy(up_font, DEFAULT_GNUGRAPHFONT);
	    up_fontsize = 10;
	    term->v_char = (unsigned int) (up_fontsize * UP_VFONTSC);
	    term->h_char = (unsigned int) (up_fontsize * UP_HFONTSC);
	    c_token++;
	    break;
	case UP_PAGESIZE:
	    c_token++;
	    /* user is responsible for valid page size specification, e.g.
	     * "a4", "letter", "b5", "a4,xoffset=-5mm,yoffset=2.0cm".
	     * See GNU plotutils documentation for details. */
	    quote_str(pl_pagesize, c_token, MAX_ID_LEN);
	    /* else use default quietly */
	    c_token++;
	    break;
	case UP_PLOTTYPE:
	    c_token++;
	    pl_plottype = lookup_table(&term_up_pt_tbl[0],c_token);
	    if (pl_plottype == PL_INVALID)
		pl_plottype = PL_META;
	    c_token++;
	    break;
	case UP_OTHER:
	default:
	    if (isstring(c_token)) {
		quote_str(up_font, c_token, MAX_ID_LEN);
		c_token++;
	    }
	    if (!END_OF_COMMAND) {
		/* We have font size specified */
		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 size \"%s\" type %s",
	    up_font,
	    up_fontsize,
	    pl_pagesize,
	    term_up_pt_tbl[pl_plottype].key);
}

TERM_PUBLIC void
UP_init()
{
#if 1
    pl_parampl ("PAGESIZE", pl_pagesize);

    if ((handle_global = pl_newpl (term_up_pt_tbl[pl_plottype].key,
				   NULL, gpoutfile, stderr)) < 0) {
	int_error(NO_CARET, "GNU plotutils (gnugraph) failed to create \"%s\" plotter.",
		  term_up_pt_tbl[pl_plottype].key);
    }

    pl_selectpl (handle_global);         /* select the Plotter for use */

    pl_openpl ();
    pl_space (0, 0, UP_SCREENX - 1, UP_SCREENY - 1);
    pl_fontname (up_font);

    /* T.Walter: Try to find the correct font size */
# if 0
    pl_fontsize (up_fontsize);  too small
    pl_fontsize (up_fontsize*10);  too small
    pl_fontsize (up_fontsize*100);  too large
    pl_fontsize (up_fontsize*0.0525*GPMIN((UP_XMAX), (UP_YMAX)));  too large
    pl_fontsize (up_fontsize/72.0*GPMIN((UP_XMAX), (UP_YMAX)));  too large
    pl_fontsize (up_fontsize/72.0*0.0525*GPMIN((UP_XMAX), (UP_YMAX)));  too small
    pl_fontsize (up_fontsize/72.0*0.0525*GPMIN((UP_SCREENX), (UP_SCREENY))); too small
    pl_fontsize (up_fontsize/72.0*GPMIN((UP_SCREENX), (UP_SCREENY)));  too large
    pl_fontsize (up_fontsize/72.0*8*0.0525*GPMIN((UP_XMAX), (UP_YMAX))); just about too large
# endif /* 0 */
    pl_fontsize (up_fontsize/72.0*0.0525*GPMIN((UP_SCREENX), (UP_SCREENY)));

#else  /* Old code */
    pl_openpl();
    pl_space (0, 0, UP_SCREENX - 1, UP_SCREENY - 1);
    pl_fontname (up_font);
#endif
    pl_fontsize(up_fontsize);
}


TERM_PUBLIC void
UP_graphics()
{
    pl_erase();
}


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


TERM_PUBLIC void
UP_linetype(linetype)
int linetype;
{
    /* T.Walter, 1999-05-23 */
#if 0
    static char *lt[2+7] =
    {
	"solid", "longdashed",
	"solid", "longdashed", "shortdashed", "dotted",
	"dotdashed", "dotdotdashed", "dotdotdotdashed"
    };

    if (linetype >= 7)
	linetype %= 7;
    }

    pl_linemod (lt[linetype + 2]);

#else /* original */
    static char *lt[2+5] = { "solid", "longdashed", "solid", "dotted",
			     "shortdashed", "dotdashed", "longdashed"};

    if (linetype >= 5)
	linetype %= 5;

    pl_linemod(lt[linetype + 2]);

#endif /* original */
}


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


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


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

}

TERM_PUBLIC int
UP_text_angle(ang)
int ang;
{
    ROTATE(ang ? 1 : 0);
    return TRUE;
}

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

TERM_PUBLIC void
UP_reset()
{
    pl_closepl();

    /* T.Walter, 1999-05-23 */

    pl_selectpl (0);                     /* default plotter */

    if (pl_deletepl (handle_global) < 0) /* delete plotter we used above */
	fprintf (stderr, "GNU Plotutils (gnugraph) is not able to delete Plotter\n");

}

#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 */

#define TERM_HELP_GNUGRAPH
#ifdef TERM_HELP
START_HELP(gnugraph)
"1 Gnugraph(GNU plotutils)",
"?commands set terminal unixplot",
"?set terminal unixplot",
"?set term unixplot",
"?terminal unixplot",
"?term unixplot",
"?unixplot",
" The `unixplot` driver produces device-independent output in the GNU plot",
" graphics language.  The default size of the PostScript results generated by",
" \"plot2ps\" is 5 x 3 inches; this can be increased up to about 8.25 x 8.25 by",
" `set size`.",
"",
" Syntax:",
"       set terminal unixplot {\"<fontname>\"} {<fontsize>}",
"                             {type <pt>} {size \"<size>\"}",
"",
" which defaults to 10-point \"Courier\".",
"",
" For `type`, the following options are accepted: `X`, `pnm`, `gif`, `ai`,",
" `ps`, `cgm`, `fig`, `pcl5`, `hpgl`, `tek`, and `meta` (default). The",
" `size` option (default is a4) is passed straight through to plotutils, it's",
" the user's responsibility to provide correct values. Details can be found",
" in the plotutils documentation.",
"",
" Examples:",
"       set terminal unixplot type hpgl size \"a4\"",
"       set terminal unixplot size \"a4,xoffset=-5mm,yoffset=2.0cm\" type pnm",
"",
" There is a non-GNU version of the `unixplot` driver which cannot be compiled",
" unless this version is left out."
END_HELP(gnugraph)
#endif