/* Copyright (C) 1989-1994 Aladdin Enterprises.  All rights reserved.
  
  This file is part of Aladdin Ghostscript.
  
  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.
  
  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/

/*
 * This is a modification of Mark Wedel's Apple DMP driver to
 * support 2 higher resolutions:
 *      appledmp:  120dpi x  72dpi is still supported (yuck)
 *	iwlo:	   160dpi x  72dpi
 *	iwhi:	   160dpi x 144dpi
 *
 * The Imagewriter II is a bit odd.  In pinfeed mode, it thinks its
 * First line is 1 inch from the top of the page. If you set the top
 * form so that it starts printing at the top of the page, and print
 * to near the bottom, it thinks it has run onto the next page and
 * the formfeed will skip a whole page.  As a work around, I reverse
 * the paper about a 1.5 inches at the end of the page before the
 * formfeed to make it think its on the 'right' page.  bah. hack!
 * 
 * This is  my first attempt to work with gs, so your milage may vary
 *
 * Jonathan Luckey (luckey@rtfm.mlb.fl.us)
 */

/* This is a bare bones driver I developed for my apple Dot Matrix Printer.
 * This code originally was from the epson driver, but I removed a lot
 * of stuff that was not needed.
 *
 * The Dot Matrix Printer was a predecessor to the apple Imagewriter.  Its
 * main difference being that it was parallel.
 *
 * This code should work fine on Imagewriters, as they have a superset
 * of commands compared to the DMP printer.
 *
 * This driver does not produce the smalles output files possible.  To
 * do that, it should look through the output strings and find repeat
 * occurances of characters, and use the escape sequence that allows
 * printing repeat sequences.  However, as I see it, this the limiting
 * factor in printing is not transmission speed to the printer itself,
 * but rather, how fast the print head can move.  This is assuming the
 * printer is set up with a reasonable speed (9600 bps)
 *
 * WHAT THE CODE DOES AND DOES NOT DO:
 *
 * To print out images, it sets the printer for unidirection printing
 * and 15 cpi (120 dpi). IT sets line feed to 1/9 of an inch (72 dpi).
 * When finished, it sets things back to bidirection print, 1/8" line
 * feeds, and 12 cpi.  There does not appear to be a way to reset
 * things to initial values.
 *
 * This code does not set for 8 bit characters (which is required). It
 * also assumes that carriage return/newline is needed, and not just
 * carriage return.  These are all switch settings on the DMP, and
 * I have configured them for 8 bit data and cr only.
 *
 * You can search for the strings Init and Reset to find the strings
 * that set up the printer and clear things when finished, and change
 * them to meet your needs.
 *
 * Also, you need to make sure that the printer daemon (assuming unix)
 * doesn't change the data as it is being printed.  I have set my
 * printcap file (sunos 4.1.1) with the string:
 * ms=pass8,-opost
 * and it works fine.
 *
 * Feel free to improve this code if you want.  However, please make
 * sure that the old DMP will still be supported by any changes.  This
 * may mean making an imagewriter device, and just copying this file
 * to something like gdevimage.c.
 *
 * The limiting factor of the DMP is the vertical resolution.  However, I
 * see no way to do anything about this.  Horizontal resolution could
 * be increased by using 17 cpi (136 dpi).  I believe the Imagewriter
 * supports 24 cpi (192 dpi).  However, the higher dpi, the slower
 * the printing.
 *
 * Dot Matrix Code by Mark Wedel (master@cats.ucsc.edu)
 */


#include "gdevprn.h"

/* The device descriptors */
private dev_proc_print_page(dmp_print_page);

/* Standard DMP device */
gx_device_printer far_data gs_appledmp_device =
  prn_device(prn_std_procs, "appledmp",
	85,				/* width_10ths, 8.5" */
	110,				/* height_10ths, 11" */
	120, 72,			/* X_DPI, Y_DPI */
	0, 0.5, 0.5, 0,		/* margins */
	1, dmp_print_page);


/*  lowrez Imagewriter device */
gx_device_printer far_data gs_iwlo_device =
  prn_device(prn_std_procs, "iwlo",
	85,				/* width_10ths, 8.5" */
	110,				/* height_10ths, 11" */
	160, 72,			/* X_DPI, Y_DPI */
	0, 0.5, 0.5, 0,		/* margins */
	1, dmp_print_page);


/*  hirez Imagewriter device */
gx_device_printer far_data gs_iwhi_device =
  prn_device(prn_std_procs, "iwhi",
	85,				/* width_10ths, 8.5" */
	110,				/* height_10ths, 11" */
	160, 144,			/* X_DPI, Y_DPI */
	0, 0.5, 0.5, 0,		/* margins */
	1, dmp_print_page);


/* ------ Internal routines ------ */


/* Send the page to the printer. */
private int
dmp_print_page(gx_device_printer *pdev, FILE *prn_stream)
{	

	int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
	/* Note that in_size is a multiple of 8. */
	int in_size = line_size * 8;
	byte *buf1 = (byte *)gs_malloc(in_size, 1, "dmp_print_page(buf1)");
	byte *buf2 = (byte *)gs_malloc(in_size, 1, "dmp_print_page(buf2)");
	byte *in = buf1;
	byte *out = buf2;
	int lnum = 0;
	int x_dpi,y_dpi;

	x_dpi= (int)(pdev->x_pixels_per_inch);
	y_dpi= (int)(pdev->y_pixels_per_inch);

	/* Check allocations */
	if ( buf1 == 0 || buf2 == 0 )
	{	if ( buf1 ) 
		  gs_free((char *)buf1, in_size, 1, "dmp_print_page(buf1)");
		if ( buf2 ) 
		  gs_free((char *)buf2, in_size, 1, "dmp_print_page(buf2)");
		return_error(gs_error_VMerror);
	}

	/* Initialize the printer and reset the margins. */
	/* fwrite("\r\n\033>\033q\033T16", 1, 10, prn_stream); */
	switch( x_dpi )
	{
		case 136: fwrite("\r\n\033>\033Q\033T16", 1, 10, prn_stream); break;
		case 144: fwrite("\r\n\033>\033p\033T16", 1, 10, prn_stream); break;
		case 160: fwrite("\r\n\033>\033P\033T16", 1, 10, prn_stream); break;
		case 120:
		default:  fwrite("\r\n\033>\033q\033T16", 1, 10, prn_stream); break;
	}

	/* Print lines of graphics */
	while ( lnum < pdev->height )
	{	
		byte *inp;
		byte *in_end;
		byte *out_end;
		int lcnt,ltmp;
		register byte *out_blk;

/* The apple DMP printer seems to be odd in that the bit order on
 * each line is reverse what might be expected.  Meaning, an
 * underscore would be done as a series of 0x80, while on overscore
 * would be done as a series of 0x01.  So we get each
 * scan line in reverse order.
 */

		/* do even field */
		for (lcnt=0; lcnt<8; lcnt++)
		{
			if ( y_dpi==144) ltmp= 2*lcnt+0;
			else ltmp= lcnt;

			if ((lnum+ltmp)>pdev->height) 
				memset(in+lcnt*line_size, 0, line_size);
			else
				gdev_prn_copy_scan_lines(pdev, lnum+ltmp,
					in + line_size*(7 - lcnt), line_size);
		}

	    out_end = out;
	    inp = in;
	    in_end = inp + line_size;
    
                   for ( ; inp < in_end; inp++, out_end += 8 )
   	                { 
    		            gdev_prn_transpose_8x8(inp, 
					           line_size, out_end, 1);
		    	}
			/* Remove trailing 0s. */
 		    while ( out_end > out && out_end[-1] == 0 )
	        	{
		       	    out_end--;
			}
		for (out_blk = out; out_blk< out_end; ) {
			/* skip leading 0s */
			if (*out_blk) break;
			out_blk ++;
		}
		/* write out however many blank's as we got nulls.
		 * In fact, because of the overhead, this is only really
		 * efficient if you have 8 or more leading 0's.
		 */
		if ((out_blk!=out) && ((out_blk-out)>7))
			fprintf(prn_stream,"\033V%04d%c", (int) (out_blk-out), 0);
		else
			out_blk=out;

		if ((int)(out_end-out_blk)) {
			fprintf(prn_stream,"\033G%04d",(int)(out_end - out_blk));
			fwrite(out_blk, 1, (int)(out_end-out_blk), prn_stream);
		}

		/* move down 1/144th if hirez */
		if ( y_dpi==144 ) fprintf(prn_stream,"\033T01\r\n");
		else fprintf(prn_stream,"\r\n");
	
		/* do odd field */

		if ( y_dpi==144 )
		{
			for (lcnt=0; lcnt<8; lcnt++)
			{
				ltmp= 2*lcnt+1;
				if ((lnum+ltmp)>pdev->height) 
					memset(in+lcnt*line_size, 0, line_size);
				else
					gdev_prn_copy_scan_lines(pdev, lnum+ltmp,
						in + line_size*(7 - lcnt), line_size);
			}
	
			out_end = out;
			inp = in;
			in_end = inp + line_size;
    
                	for ( ; inp < in_end; inp++, out_end += 8 )
                	{ 
	        	    gdev_prn_transpose_8x8(inp, line_size, out_end, 1);
	    		}

			/* Remove trailing 0s. */
 			while ( out_end > out && out_end[-1] == 0 )
	       		{
	       		    out_end--;
			}
			for (out_blk = out; out_blk< out_end; )
			{
				/* skip leading 0s */
				if (*out_blk) break;
				out_blk ++;
			}

			/* write out however many blank's as we got nulls.
			 * In fact, because of the overhead, this is only really
			 * efficient if you have 8 or more leading 0's.
			 */
			if ((out_blk!=out) && ((out_blk-out)>7))
				fprintf(prn_stream,"\033V%04d%c", (int) (out_blk-out), 0);
			else
				out_blk=out;

			if ((int)(out_end-out_blk))
			{
				fprintf(prn_stream,"\033G%04d",
                                        (int)(out_end - out_blk));
				fwrite(out_blk, 1,
					(int)(out_end-out_blk), prn_stream);
			}

			if ( y_dpi==144 )
				fprintf(prn_stream,"\033T15\r\n");
			else	fprintf(prn_stream,"\r\n");

		} /* end of odd field code */	

		if ( y_dpi==144) lnum += 16 ;
		else lnum += 8 ;
	}

	/* ImageWriter will skip a whole page if too close to end */
	/* so skip back more than an inch */
	if ( x_dpi!=120 ) fputs("\033T99\n\n\033r\n\n\n\n\033f", prn_stream);


	/* Formfeed and Reset printer */
	fputs("\033T16\f\033<\033B\033E", prn_stream);
	fflush(prn_stream);

	gs_free((char *)buf2, in_size, 1, "dmp_print_page(buf2)");
	gs_free((char *)buf1, in_size, 1, "dmp_print_page(buf1)");
	return 0;
}
