/***********************************************************************

   DOSPECIAL.C for an Amiga printer driver for the Hewlett Packard DeskJet

                    Copywrite © 1989 Creative Focus

   This software is freely redistributable, as long as all code,
   comments, and documentation are included unaltered, and no other
   conditions are imposed on its redistribution.

   THIS SOFTWARE IS PROVIDED "AS IS," AND NO GUARANTEE IS MADE OF ITS
   FITNESS FOR ANY PURPOSE.  CREATIVE FOCUS MAY NOT BE HELD ACCOUNTABLE
   FOR ANY DAMAGE, REAL OR IMAGINED, RESULTING FROM ITS USE OR MISUSE.

                           Creative Focus
                           Dr. Gerald Hull
                           12 White Street
                           Binghamton, New York  13901

                           (607) 648-4082
                           BIX:  ghull
                           PLINK:  DRJERRY

***********************************************************************/

#include "exec/types.h"
#include "devices/printer.h"
#include "devices/prtbase.h"

#define LPI          7
#define QUALITY     14
#define NITLENGTH   21
#define TOP_MARG     2
#define TM           6
#define TL          10
#define FRMLENGTH   11
#define LM           6
#define RM          10
#define MRGLENGTH   11
#define THREE        3

/*
   00-03   \033&d@      underline off
   04-08   \033&l6D     default to 6 lines per inch
   09-13   \033(s0b     normal stroke weight (bold off)
   14-15         1q     default to draft quality
   16-17         0s     italics off
   18-19         0U     sub/superscript off
   20      \015         print position to left margin
*/

char initmain[NITLENGTH] = "\033&d@\033&l6D\033(s0b1q0s0U\015";

char initmarg[MRGLENGTH] = "\033&a000l000M";
extern char initform[FRMLENGTH] = "\033&l000e000F";

extern void anitoa();

extern struct PrinterData *PD;
extern struct PrinterExtendedData *PED;

DoSpecial(command, outbuf, vline, currentVMI, crlfFlag, Parms)
   char outbuf[];
   UWORD *command;
   BYTE *vline;
   BYTE *currentVMI;
   BYTE *crlfFlag;
   UBYTE Parms[];

   {
   UWORD textlength;
   int x = 0, y = 0;
   int retval = 0;

   textlength = PD->pd_Preferences.PaperLength - TOP_MARG;
   switch (*command)
      {
      case aRIN:     /* initialize */
         if (PD->pd_Preferences.PrintSpacing == EIGHT_LPI)
            initmain[LPI] = '8';
         if (PD->pd_Preferences.PrintQuality == LETTER)
            initmain[QUALITY] = '2';
         x = 0;
         while(x < NITLENGTH)
            outbuf[x] = initmain[x++];
         anitoa(TOP_MARG, initform+TM);
         anitoa(textlength, initform+TL);
         while (y < FRMLENGTH)
            outbuf[x++] = initform[y++];
         Parms[0] = PD->pd_Preferences.PrintLeftMargin;
         Parms[1] = PD->pd_Preferences.PrintRightMargin;
 
      case aSLRM:    /* set left & right margins */
         y = 0;
         anitoa(Parms[0]-1, initmarg+LM);
         anitoa(Parms[1]-1, initmarg+RM);
         while(y < MRGLENGTH)
            outbuf[x++] = initmarg[y++];
         retval = x;
         break;

      case aSUS2:    /* superscript off */
         if (*vline == 0)
            *command = aPLU;
         else if (*vline < 0)
            *command = aRI;
         *vline = 1;
         break;

      case aSUS1:    /* superscript on */
         if (*vline > 0)
            {
            *command = aPLD;
            *vline = 0;
            }
         break;

      case aSUS4:    /* subscript on */
         if (*vline == 0)
            *command = aPLD;
         else if (*vline > 0)
            *command = aIND;
         *vline = -1;
         break;

      case aSUS3:    /* subscript off */
         if (*vline < 0)
            {
            *command = aPLU;
            *vline = 0;
            }
         break;

      case aSUS0:    /* normalize line position */
         if (*vline > 0)
            *command = aPLD;
         if (*vline < 0)
            *command = aPLU;
         *vline = 0;
         break;

      case aPLU:     /* partial line up */
         (*vline)++;
         break;

      case aPLD:     /* partial line down */
         (*vline)--;
         break;

      case aSTBM:    /* set top & bottom margins */
         if (Parms[0] == 0)
            Parms[0] = TOP_MARG;
         if (Parms[1] == 0)
            Parms[1] = textlength;
         anitoa(Parms[0], initform+TM);
         anitoa(Parms[1]-Parms[0], initform+TL);
         while (x < FRMLENGTH)
            outbuf[x] = initform[x++];
         retval = x;
         break;

      case aSLPP:    /* set page length */
         anitoa(TOP_MARG, initform+TM);
         anitoa(textlength, initform+TL);
         while(x < FRMLENGTH)
            outbuf[x] = initform[x++];
         retval = x;
         break;

      case aRIS:     /* reset */
         PD->pd_PWaitEnabled = 253;
      }

   return(retval);
   }


void anitoa(value, where)
   int value;        /* value to be ASCIIed */
   char *where;      /* -> end+1 of destination buffer */

   {
   int i;

   for (i = 0; i < THREE; i++)
      {
      *(--where) = (value % 10) + '0';
      value /= 10;
      }
   }


Close(ior)
   struct IODRPReq *ior;
   {
   /* send formfeed unless Render signals not */
   if (PED->ped_PrintMode)
      {
      (*(PD->pd_PWrite))("\014",1);
      /* wait until all buffers finished */
      (*(PD->pd_PBothReady))();
      }
   return(0);
   }
