/* qmsdev.c The qms output driver for dviqms program.
 * Copyright 1985 Massachusetts Institute of Technology.
 * Author: cjl@oz
 */

#include <stdio.h>
#include "dev.h"

#define DEV_DPI             300 /* Device dots per inch */
#define DEV_XOFFSET	    300
#define DEV_YOFFSET	    300

FILE *dev_out;
long dev_h,dev_v;
unsigned long dev_options;
char dev_hex[] = "0123456789ABCDEF";

dev_setc(ch,xsize)
     unsigned long ch;
     int xsize;
{
  if (ch < '\040' || ch == '^' || ch == '\177') {
    putc('^',dev_out);
    putc('$',dev_out);
    putc(dev_hex[(ch >> 4) & 017],dev_out);
    putc(dev_hex[ch & 017],dev_out);
  } else putc(ch,dev_out);
  dev_h += xsize;
}

dev_position(x,y)
     long x,y;
{
  if (dev_h != x) fprintf(dev_out,"^IH%04.4d",(dev_h = x) + DEV_XOFFSET);
  if (dev_v != y) fprintf(dev_out,"^IV%04.4d",(dev_v = y) + DEV_YOFFSET);
}

dev_draw_box(h,w)
     long h,w;
{
  fprintf(dev_out,"^JR-%04.4d^LS%04.4d%04.4d",h,w,h);
  dev_v -= h;
  dev_h += w;
}

dev_eop()
{
  fprintf(dev_out,"^G^-\f");
  fflush(dev_out);
  fprintf(dev_out,"^IH%04.4d^IV%04.4d",DEV_XOFFSET,DEV_YOFFSET);
  dev_h = dev_v = 0;
}

#define QUIC_ON		"\r^PY^-\r"
#define QUIC_OFF	"\r^-^PN^-\r"
#define DEFAULT_FONT	"^IS%05.5d^-"
#define DELETE_FONTS	"^DFEA^G^-"
#define CLEAR_OVERLAYS	"^DOC^Z^-"
#define SYNTAX		"^ISYNTAX%1.1d%1.1d%1.1d%1.1d%1.1d^-"
#define PORTRAIT	"^IOP^-"
#define LANDSCAPE	"^IOL^-"
#define V_MARGINS	"^IMV%04.4d%04.4d^-"
#define H_MARGINS	"^IMH%04.4d%04.4d^-"
#define CHAR_SP		"^IC%04.4d^-"
#define LINE_SP		"^IL%04.4d^-"

hard_init()
{
  fprintf(dev_out,QUIC_ON);		/* Turn on QUIC */
  fprintf(dev_out,DELETE_FONTS);	/* Delete all downloaded fonts */
  fprintf(dev_out,CLEAR_OVERLAYS);	/* Clear all overlays */
  fprintf(dev_out,DEFAULT_FONT,10);	/* Select the font I like */
  soft_init();
}

soft_init()
{
  fprintf(dev_out,QUIC_ON);		/* Turn on QUIC */
  fprintf(dev_out,SYNTAX,0,0,0,1,0);	/* Ensure correct SYNTAX */
  if (dev_options & DEV_INIT_LANDSCAPE) {
    fprintf(dev_out,LANDSCAPE);		/* Landscape page orientation */
    fprintf(dev_out,V_MARGINS,0,2550);	/* Vertical margins */
    fprintf(dev_out,H_MARGINS,0,4200);	/* Horizontal margins */
  } else {
    fprintf(dev_out,PORTRAIT);		/* Portrait page orientation */
    fprintf(dev_out,V_MARGINS,0,4200);	/* Vertical margins */
    fprintf(dev_out,H_MARGINS,0,2550);	/* Horizontal margins */
  }
  fprintf(dev_out,CHAR_SP,0);		/* Turn off char spacing */
  fprintf(dev_out,LINE_SP,0);		/* Turn off line spacing */
  page_init();
}

page_init()
{
  fprintf(dev_out,"^IH%04.4d^IV%04.4d",DEV_XOFFSET,DEV_YOFFSET);
  dev_h = dev_v = 0;
}     

dev_init(f,options,dpi)
     FILE *f;
     unsigned long options;
     long *dpi;
{
  dev_out = f;
  *dpi = DEV_DPI;
  dev_options = options;
  hard_init();
}

dev_term()
{
  fprintf(dev_out,QUIC_OFF);
  fflush(dev_out);
}


dev_print_log(f)
     FILE *f;
{
  register int ch;

  if (dev_h || dev_v) putc('\f',dev_out);
  fprintf(dev_out,V_MARGINS,100,3300);	/* Vertical margins */
  fprintf(dev_out,H_MARGINS,100,2550);	/* Horizontal margins */
  fprintf(dev_out,DEFAULT_FONT,10);	/* Select the font I like */
  fprintf(dev_out,"^IH%04.4d^IV%04.4d",100,100);
  fprintf(dev_out,QUIC_OFF);
  while (!ferror(f) && (ch = getc(f)) != EOF) {
    if (ch == '\n') putc('\r',dev_out);
    if (ch == '^') putc(ch,dev_out);
    putc(ch,dev_out);
  }
  putc('\f',dev_out);
  fflush(dev_out);
  fprintf(dev_out,QUIC_ON);
}

dev_special(nchars,f)
     int nchars;
     FILE *f;
{
  while (nchars-- > 0) putc(getc(f),dev_out);
  soft_init();
}