/* Graphics render for Okidata ML 92 */

#include "exec/types.h"
#include "exec/nodes.h"
#include "exec/lists.h"
#include "exec/memory.h"
#include "devices/prtbase.h"

extern struct PrinterData *PD;

int Render(ct, x, y, status)
UBYTE ct ;	/* color type = 0 for b&w printers */
UWORD x, y ;	/* x and y coordinates of pixel */
		/* or pc and pr print values, or special */
UBYTE status ;	/* print status (0-init, 1-enter_pixel, 2-dump) */
{
	static UWORD BUFSIZE;
	static char *SpoolBuffer ;
	char *malloc() ;
	UWORD i, k;			/* mics. var */
	BYTE err;			/* the error # */

	switch(status) {

	case 0 : /* allocate memory for printer buffer */
		BUFSIZE = x;
		PD->pd_PrintBuf = (UBYTE *)AllocMem(BUFSIZE,MEMF_PUBLIC);
		SpoolBuffer = (char *)AllocMem(BUFSIZE*2+5,MEMF_PUBLIC);
					/* used to actually print */
		if (err=(PD->pd_PrintBuf == 0)) return(err);
		(*(PD->pd_PWrite))("\30\33%C000\34",8);
			/* non graphic, clear, clear left margin, 72 DPI */
		return(0) ;
		break ;

	case 1 : /* put pixel in buffer */
		PD->pd_PrintBuf[x] = PD->pd_PrintBuf[x] | (1 << (y % 7));
		return(0) ; /* flag all ok */
		break ;

	case 2 : /* dump buffer to printer */
		k = 1 ;
		for (i=0 ; i < BUFSIZE; i++ ) {
			SpoolBuffer[k] = PD->pd_PrintBuf[i]&127 ;
			if (SpoolBuffer[k++] == '\3') /* double ETX */
				SpoolBuffer[k++] = '\3' ;
		}
		SpoolBuffer[0]   = '\03' ; /* Begin graphic mode */
		SpoolBuffer[k++] = '\03' ; /* Graphic linefeed */
		SpoolBuffer[k++] = '\16' ;
		SpoolBuffer[k++] = '\03' ; /* End graphic mode */
		SpoolBuffer[k++] = '\02' ;

		(*(PD->pd_PWrite))(SpoolBuffer,k); /* write out Buffer */

		return(0) ; /* flag all ok */
		break ;

	case 3 : /* clear and init buffer */
		for (i=0; i< BUFSIZE; i++)
			PD->pd_PrintBuf[i] = 0 ;
		return(0) ; /* flag all ok */
		break ;

	case 4 : /* free print buffer memory */
		(*(PD->pd_PBothReady))() ;
		FreeMem(PD->pd_PrintBuf,BUFSIZE) ;
		FreeMem(SpoolBuffer,BUFSIZE*2+5) ;
		return(0) ;
		break ;

	default:
		return(0) ;
	}
}

