/*
 * $Id: hppj.trm%v 3.50.1.11 1993/08/10 03:55:03 woo Exp $
 *
 */

/* GNUPLOT - hppj.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:
 *  hppj
 *
 * AUTHORS
 *  Dan Merget (danm@sr.hp.com)
 *
 * This file was based on the hpljii file by:
 *  John Engels
 *  Russell Lang
 *  Maurice Castro
 *
 * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
 * 
 */

/* The following HP laserjet series II driver uses generic bit mapped graphics
 * routines from bitmap.c to build up a bit map in memory.
 */

#ifdef HPPJ

/* We define 3 different font sizes: 5x9, 9x17, and 13x25 */

#define HPPJ_DPI 180   /* dots per inch */
#define HPPJ_PLANES 3  /* color planes */
#define HPPJ_COLORS (1 << HPPJ_PLANES)
/* make XMAX and YMAX a multiple of 8 */
#define HPPJ_XMAX (8*(unsigned int)(9.5 * HPPJ_DPI / 8.0 + 0.9))
#define HPPJ_YMAX (8 * HPPJ_DPI)

/* default values for term_tbl */
#define HPPJ_9x17_VCHAR FNT9X17_VCHAR
#define HPPJ_9x17_HCHAR FNT9X17_HCHAR
#define HPPJ_9x17_VTIC (FNT9X17_VCHAR / 2)
#define HPPJ_9x17_HTIC (FNT9X17_HCHAR / 2)

static int hppj_font = FNT9X17;

HPPJoptions()
{
    char opt[10];
#define HPPJERROR "expecting font size FNT5X9, FNT9X17, or FNT13X25"

    term_options[0]='\0'; /* default to empty string and 9x17 font */
    hppj_font = FNT9X17;  /* in case of error or empty options     */

    if ( !END_OF_COMMAND ) {
        if ( token[c_token].length > 8 ) {
            int_error(HPPJERROR, c_token);
       }

        capture(opt, c_token, c_token);
        if ( !strcmp(opt, "FNT5X9") ) {
            hppj_font = FNT5X9;
            strcpy(term_options, "FNT5X9");
	} else if ( !strcmp(opt, "FNT9X17") ) {
   	    hppj_font = FNT9X17;
            strcpy(term_options, "FNT9X17");
	} else if ( !strcmp(opt, "FNT13X25") ) {
   	    hppj_font = FNT13X25;
            strcpy(term_options, "FNT13X25");
	} else {
	    int_error(HPPJERROR, c_token);
	}
	c_token++;
    }
}


HPPJinit()
{
#ifdef REOPEN_BINARY
    reopen_binary();
#endif
}


HPPJreset()
{
#ifdef vms
    fflush_binary();
#endif /* vms */
}


HPPJgraphics()
{
    switch ( hppj_font ) {
      case FNT5X9 :
	term_tbl[term].v_char = FNT5X9_VCHAR;
	term_tbl[term].h_char = FNT5X9_HCHAR;
	term_tbl[term].v_tic = FNT5X9_VCHAR / 2;
	term_tbl[term].h_tic = FNT5X9_HCHAR / 2;
	break;
      case FNT9X17 :
	term_tbl[term].v_char = FNT9X17_VCHAR;
	term_tbl[term].h_char = FNT9X17_HCHAR;
	term_tbl[term].v_tic = FNT9X17_VCHAR / 2;
	term_tbl[term].h_tic = FNT9X17_HCHAR / 2;
	break;
      case FNT13X25 :
	term_tbl[term].v_char = FNT13X25_VCHAR;
	term_tbl[term].h_char = FNT13X25_HCHAR;
	term_tbl[term].v_tic = FNT13X25_VCHAR / 2;
	term_tbl[term].h_tic = FNT13X25_HCHAR / 2;
	break;
    }
    b_charsize(hppj_font);

    b_makebitmap(HPPJ_XMAX, HPPJ_YMAX, HPPJ_PLANES);
}


HPPJtext()
{
    int x, plane, y;	/* loop indexes */
    int minRow, maxRow;	/* loop bounds */
    int numBytes;	/* Number of run-length coded bytes to output */
    int numReps;	/* Number of times the current byte is repeated */

    fprintf(outfile, "\033E\033*t%dR\033*r%dS", HPPJ_DPI, HPPJ_YMAX);
    fprintf(outfile, "\033*b0X\033*b0Y\033*r%dU", HPPJ_PLANES);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI", 90, 88, 85, 0);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI", 53,  8, 14, 1);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI",  3, 26, 22, 2);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI",  4,  4, 29, 3);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI", 53,  5, 25, 4);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI",  2, 22, 64, 5);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI", 89, 83, 13, 6);
    fprintf(outfile, "\033*v%dA\033*v%dB\033*v%dC\033*v%dI",  4,  4,  6, 7);
    fprintf(outfile, "\033*b1M\033*r1A");

    /* dump bitmap in raster mode using run-length encoding */
    for ( x = HPPJ_XMAX - 1 ; x >= 0 ; --x ) {
        for ( plane = 0 ; plane < HPPJ_PLANES ; plane++ ) {
            minRow = b_psize * plane;
            maxRow = b_psize * plane + b_psize - 1;

            /* Print column header */
            numBytes = 0;
            for ( y = maxRow ; y >= minRow ; --y ) {
                if ( y == minRow || *((*b_p)[y]+x) != *((*b_p)[y-1]+x) ) {
                    numBytes += 2;
                }
            }
            fprintf(outfile, "\033*b%d", numBytes);
            (void) fputc((char)(plane < HPPJ_PLANES - 1 ? 'V' : 'W'), outfile);

            /* Print remainder of column */
            numReps = 0;
            for ( y = maxRow ; y >= minRow ; --y ) {
                if ( y == minRow || *((*b_p)[y]+x) != *((*b_p)[y-1]+x) ) {
                    (void) fputc( (char)(numReps), outfile );
                    (void) fputc( (char)(*((*b_p)[y]+x)), outfile );
                    numReps = 0;
                } else {
                    numReps++;
                }
            }
        }
    }
    fprintf(outfile, "\033*r1B\033E");

    b_freebitmap();
}


HPPJlinetype(linetype)
    int linetype;
{
    if ( linetype >= 0 ) {
        b_setlinetype(0);
        b_setvalue((linetype % (HPPJ_COLORS-1)) + 1);
    } else {
        b_setlinetype(linetype + 2);
        b_setvalue(HPPJ_COLORS - 1);
    }
}


#define HPPJmove       b_move
#define HPPJvector     b_vector
#define HPPJtext_angle b_text_angle
#define HPPJput_text   b_put_text

#endif /* HPPJ */
