/*
 * $Id: hpljii.trm,v 3.26 92/03/24 22:34:57 woo Exp Locker: woo $
 */

/* GNUPLOT - hpljii.trm */
/*
 * Copyright (C) 1990, 1991, 1992   
 *
 * 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:
 *  hpljii, hpdj
 *
 * AUTHORS
 *  John Engels
 *  Russell Lang
 *  Maurice Castro
 *
 * send your comments or suggestions to (info-gnuplot@ames.arc.nasa.gov).
 * 
 */

/* The following HP laserjet series II driver uses generic bit mapped graphics
   routines from bitmap.c to build up a bit map in memory.  The driver
   interchanges colomns and lines in order to access entire lines
   easily and returns the lines to get bits in the right order :
   (x,y) -> (y,XMAX-1-x). */
/* This interchange is done by calling b_makebitmap() with reversed 
   xmax and ymax, and then setting b_rastermode to TRUE.  b_setpixel()
   will then perform the interchange before each pixel is plotted */
/* by John Engels JENGELS@BNANDP51.BITNET, inspired by the hpljet driver
   of Jyrki Yli-Nokari */

#ifdef HPLJII

/* We define 4 different print qualities : 300ppi, 150ppi, 100ppi and
   75ppi.  (Pixel size = 1, 2, 3, 4 dots) */

#define HPLJII_DPP (hplj_dpp)   /* dots per pixel */
#define HPLJII_PPI (300/HPLJII_DPP) /* pixel per inch */
/* make XMAX and YMAX a multiple of 8 */
#define HPLJII_XMAX (8*(unsigned int)(xsize*1920/HPLJII_DPP/8.0+0.9))
#define HPLJII_YMAX (8*(unsigned int)(ysize*1920/HPLJII_DPP/8.0+0.9))

#define HPLJII_VCHAR (HPLJII_PPI/6) /* Courier font with 6 lines per inch */
#define HPLJII_HCHAR (HPLJII_PPI/10) /* Courier font with 10 caracters
                                        per inch */

/* default values for term_tbl */
#define HPLJII_75PPI_XMAX (1920/4)
#define HPLJII_75PPI_YMAX (1920/4)
#define HPLJII_75PPI_HCHAR (1920/4/6)
#define HPLJII_75PPI_VCHAR (1920/4/10)
#define HPLJII_75PPI_VTIC 5
#define HPLJII_75PPI_HTIC 5


#define HPLJII_PUSH_CURSOR fprintf(outfile,"\033&f0S") /* Save current
                  cursor position */
#define HPLJII_POP_CURSOR fprintf(outfile,"\033&f1S") /* Restore
                  cursor position */
#define HPLJII_COURIER fprintf(outfile,"\033(0N\033(s0p10.0h12.0v0s0b3T\033&l6D")
         /* be sure to use courier font with 6lpi and 10cpi */

static int hplj_dpp=4;
/* bm_pattern not appropriate for 300ppi graphics */
static unsigned int b_300ppi_pattern[] = {0xffff, 0x1111,
        0xffff, 0x3333, 0x0f0f, 0x3f3f, 0x0fff, 0x00ff, 0x33ff};

HPLJIIoptions()
{
char opt[4];

#define HPDJERROR "expecting dots per inch size 75, 100, 150 or 300"
	if (!END_OF_COMMAND) {
		if (token[c_token].length>3)
			int_error(HPDJERROR,c_token);

		/* almost_equals() won't accept numbers - use strcmp() instead */
		capture(opt,c_token,c_token);
		if (!strcmp(opt,"75")) {
   			hplj_dpp = 4;
		}
		else if (!strcmp(opt,"100")) {
   			hplj_dpp = 3;
		}
		else if (!strcmp(opt,"150")) {
   			hplj_dpp = 2;
		}
		else if (!strcmp(opt,"300")) {
   			hplj_dpp = 1;
		} else {
			int_error(HPDJERROR,c_token);
		}
		c_token++;
	}

	term_tbl[term].xmax = HPLJII_XMAX;
	term_tbl[term].ymax = HPLJII_YMAX;
	switch(hplj_dpp) {
		case 1:
			strcpy(term_options,"300");
			term_tbl[term].v_tic = 15;
			term_tbl[term].h_tic = 15;
			break;
		case 2:
			strcpy(term_options,"150");
			term_tbl[term].v_tic = 8;
			term_tbl[term].h_tic = 8;
			break;
		case 3:
			strcpy(term_options,"100");
			term_tbl[term].v_tic = 6;
			term_tbl[term].h_tic = 6;
			break;
		case 4:
			strcpy(term_options,"75");
			term_tbl[term].v_tic = 5;
			term_tbl[term].h_tic = 5;
			break;
	}
}


HPLJIIinit()
{
#ifdef vms
   reopen_binary();
#endif /* vms */
#ifdef PC
   reopen_binary();
#endif /* PC */
}


HPLJIIgraphics()
{
   term_tbl[term].v_char = HPLJII_VCHAR;
   term_tbl[term].h_char = HPLJII_HCHAR;
   HPLJII_COURIER;
   HPLJII_PUSH_CURSOR;
   /* rotate plot -90 degrees by reversing XMAX and YMAX and by 
      setting b_rastermode to TRUE */
   b_makebitmap(HPLJII_YMAX,HPLJII_XMAX,1);
   b_rastermode = TRUE;
}


/* HPLJIItext by rjl - no compression */
HPLJIItext()
{
  register int x,j,row;

   fprintf(outfile,"\033*t%dR", HPLJII_PPI);
   HPLJII_POP_CURSOR;
   fprintf(outfile, "\033*r1A");

   /* dump bitmap in raster mode */
   for (x = b_xsize-1; x >= 0; x--) {
      row = (b_ysize/8)-1;
      fprintf(outfile, "\033*b0m%dW", b_ysize/8);
      for (j = row; j >= 0; j--) {
         (void) fputc( (char)(*((*b_p)[j]+x)), outfile );
      }
   }
   fprintf(outfile, "\033*rB");

   b_freebitmap();

#ifndef vms  /* most vms spoolers add a formfeed character */
   fprintf(outfile,"\f");
#endif /* not vms */
}



HPLJIIlinetype(linetype)
int linetype;
{

   if (hplj_dpp == 1) {
      if (linetype>=7)
          linetype %= 7;
      /* b_pattern not appropriate for 300ppi graphics */
      b_linemask = b_300ppi_pattern[linetype+2];
      b_maskcount=0;
   }
   else {
      b_setlinetype(linetype);
   }
}

#define HPLJIImove b_move
#define HPLJIIvector b_vector
#define HPLJIItext_angle b_text_angle

HPLJIIput_text(x,y,str)
unsigned int x, y;
char *str;
{
   switch (b_angle) {
      case 0:
         y -= HPLJII_VCHAR/5;
         HPLJII_POP_CURSOR;
         HPLJII_PUSH_CURSOR;
         /* (0,0) is the upper left point of the paper */
         fprintf(outfile, "\033*p%+dx%+dY", x*HPLJII_DPP
                                         ,  (HPLJII_YMAX-y-1)*HPLJII_DPP );
         fputs(str, outfile);
/*       for (; *str; ++str, x += HPLJII_HCHAR)
            HPLJIIputc (x, y, *str, b_angle);*/
         break;
      case 1:
         y += (HPLJII_HCHAR-2*HPLJII_VCHAR)/2;
         y += (HPLJII_VCHAR+HPLJII_HCHAR)*strlen(str)/2;
         for (; *str; ++str, y -= HPLJII_VCHAR)
            HPLJIIputc (x, y, *str, b_angle);
         break;
   }
}

HPLJIIputc(x,y,c,angle)
unsigned int x,y;
int angle;
char c;
{
   HPLJII_POP_CURSOR;
   HPLJII_PUSH_CURSOR;
   /* (0,0) is the upper left point of the paper */
   fprintf(outfile, "\033*p%+dx%+dY", x*HPLJII_DPP
                                   ,  (HPLJII_YMAX-y-1)*HPLJII_DPP );
   fputc(c, outfile);
}


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


/* HP DeskJet routines */
HPDJgraphics()
{
	switch(hplj_dpp) {
		case 1:
			b_charsize(FNT13X25);
			term_tbl[term].v_char = FNT13X25_VCHAR;
			term_tbl[term].h_char = FNT13X25_HCHAR;
			break;
		case 2:
			b_charsize(FNT13X25);
			term_tbl[term].v_char = FNT13X25_VCHAR;
			term_tbl[term].h_char = FNT13X25_HCHAR;
			break;
		case 3:
			b_charsize(FNT9X17);
			term_tbl[term].v_char = FNT9X17_VCHAR;
			term_tbl[term].h_char = FNT9X17_HCHAR;
			break;
		case 4:
			b_charsize(FNT5X9);
			term_tbl[term].v_char = FNT5X9_VCHAR;
			term_tbl[term].h_char = FNT5X9_HCHAR;
			break;
	}
	/* rotate plot -90 degrees by reversing XMAX and YMAX and by 
	setting b_rastermode to TRUE */
	b_makebitmap(HPLJII_YMAX,HPLJII_XMAX,1);
	b_rastermode = TRUE;
}


/* 0 compression raster bitmap dump. Compatible with HP DeskJet 500
   hopefully compatible with other HP Deskjet printers */
HPDJtext()
{
  register int x,j,row;

   fprintf(outfile,"\033*b0M");
   fprintf(outfile,"\033*t%dR", HPLJII_PPI);
   fprintf(outfile, "\033*r0A");

   /* dump bitmap in raster mode */
   for (x = b_xsize-1; x >= 0; x--) {
      row = (b_ysize/8)-1;
      fprintf(outfile, "\033*b%dW", b_ysize/8);
      for (j = row; j >= 0; j--) {
         (void) fputc( (char)(*((*b_p)[j]+x)), outfile );
      }
   }
   fprintf(outfile, "\033*rbC");

   b_freebitmap();

#ifndef vms  /* most vms spoolers add a formfeed character */
   fprintf(outfile,"\f");
#endif /* not vms */
}

#define HPDJtext_angle b_text_angle
#define HPDJput_text b_put_text

#endif /* HPLJII */

