/* This file contains the Laser Jet II device dependent subroutines for */
/* use with plplot. Only the 150 dpi mode is supported.  The others     */
/* (75,100,300) should work by just changing the value of DPI and       */
/* changing the values passed to setphy in DEVICE.f77                   */

#include "plplot.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define DPI      150            /* Resolution Dots per Inch */
#define CURX     51
#define CURY     61
#define ESC      0x1b
#define FF       0x0c
#define XDOTS    1104           /* # dots across */
#define YDOTS    1410           /* # dots down   */
#define BPROW    XDOTS/8        /* # bytes across */
#define NBYTES   BPROW*YDOTS    /* total number of bytes */

static FILE *OutFile;

/* bitmap contains a pointer to an area of memory NBYTES in size */
static char *bitmap;
static int FirstClear=1;

/* Opens the file for binary mode. */

void jetini()
{
  char FileName[80];

  printf("Enter file name for LaserJet II graphics commands. ");
  scanf("%s",FileName);

  if((OutFile = fopen(FileName,"w")) == NULL) {
   printf("Error opening %s \n",FileName);
   exit(1);
  }

  /* Allocate storage for bit map matrix */
  if((bitmap = (char *)calloc(NBYTES,sizeof(char))) == NULL)
   printf("Out of memory in call to calloc \n");

  /* Reset Printer */
  fprintf(OutFile,"%cE",ESC);
}

/* Set JET to test mode */
void jettex()
{
  /* do nothing here */
}

/* Set JET to graphics mode */
void jetgra()
{
  /* Do nothing here */
}

/* Print out page */
void jetclr()
{
  int i,j;

  if(FirstClear)
    FirstClear=0;
  else {
    /* First move cursor to origin */
    fprintf(OutFile,"%c*p%dX",ESC,CURX);
    fprintf(OutFile,"%c*p%dY",ESC,CURY);

    /* Then put Laser Printer in 150 dpi mode */
    fprintf(OutFile,"%c*t%dR",ESC,DPI);
    fprintf(OutFile,"%c*r1A",ESC);

    /* Write out raster data */
    for(j=0;j<YDOTS;j++){
      fprintf(OutFile,"%c*b%dW",ESC,BPROW);
      for(i=0;i<BPROW;i++)
        putc(*(bitmap+i+j*BPROW),OutFile);
    }

    /* End raster graphics and send Form Feed */
    fprintf(OutFile,"%c*rB",ESC);
    fprintf(OutFile,"%c",FF);

    /* Finally, clear out bitmap storage area */
    memset(bitmap,'\0',NBYTES);
  }
}

/* Change color */
void jetcol(colour)
int colour;
{
}

/* Function to draw the line in the bitmap */
void jetlin(x1,y1,x2,y2)
int x1,y1,x2,y2;
{
 int i;
 float length,fx,fy,dx,dy;
 void setpoint();
 
 length = (float)sqrt((double)((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
 if(length == 0.)
   length = 1.;
 dx = (x2 - x1)/length;
 dy = (y2 - y1)/length;

 fx = x1;
 fy = y1;
 setpoint(x1,y1);
 setpoint(x2,y2);

 for(i=1;i<=(int)length;i++)
  setpoint((int)(fx+=dx),(int)(fy+=dy));
}

static char mask[8] = {'\200','\100','\040','\020','\010','\004','\002','\001'};

/* Function to set a bit in the bitmap */
static void setpoint(x,y)
int x,y;
{
 int index;
 index = x/8 + y*BPROW;
 *(bitmap+index) = *(bitmap+index) | mask[x%8];
}

/* Reset printer and close file */
void jettid()
{
  jetclr();
  /* Reset Printer */
  fprintf(OutFile,"%cE",ESC);
  fclose(OutFile);
  free((void *)bitmap);
}


