home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVADMP.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  10KB  |  312 lines

  1. /* Copyright (C) 1989-1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /*
  20.  * This is a modification of Mark Wedel's Apple DMP driver to
  21.  * support 2 higher resolutions:
  22.  *      appledmp:  120dpi x  72dpi is still supported (yuck)
  23.  *    iwlo:       160dpi x  72dpi
  24.  *    iwhi:       160dpi x 144dpi
  25.  *
  26.  * The Imagewriter II is a bit odd.  In pinfeed mode, it thinks its
  27.  * First line is 1 inch from the top of the page. If you set the top
  28.  * form so that it starts printing at the top of the page, and print
  29.  * to near the bottom, it thinks it has run onto the next page and
  30.  * the formfeed will skip a whole page.  As a work around, I reverse
  31.  * the paper about a 1.5 inches at the end of the page before the
  32.  * formfeed to make it think its on the 'right' page.  bah. hack!
  33.  * 
  34.  * This is  my first attempt to work with gs, so your milage may vary
  35.  *
  36.  * Jonathan Luckey (luckey@rtfm.mlb.fl.us)
  37.  */
  38.  
  39. /* This is a bare bones driver I developed for my apple Dot Matrix Printer.
  40.  * This code originally was from the epson driver, but I removed a lot
  41.  * of stuff that was not needed.
  42.  *
  43.  * The Dot Matrix Printer was a predecessor to the apple Imagewriter.  Its
  44.  * main difference being that it was parallel.
  45.  *
  46.  * This code should work fine on Imagewriters, as they have a superset
  47.  * of commands compared to the DMP printer.
  48.  *
  49.  * This driver does not produce the smalles output files possible.  To
  50.  * do that, it should look through the output strings and find repeat
  51.  * occurances of characters, and use the escape sequence that allows
  52.  * printing repeat sequences.  However, as I see it, this the limiting
  53.  * factor in printing is not transmission speed to the printer itself,
  54.  * but rather, how fast the print head can move.  This is assuming the
  55.  * printer is set up with a reasonable speed (9600 bps)
  56.  *
  57.  * WHAT THE CODE DOES AND DOES NOT DO:
  58.  *
  59.  * To print out images, it sets the printer for unidirection printing
  60.  * and 15 cpi (120 dpi). IT sets line feed to 1/9 of an inch (72 dpi).
  61.  * When finished, it sets things back to bidirection print, 1/8" line
  62.  * feeds, and 12 cpi.  There does not appear to be a way to reset
  63.  * things to initial values.
  64.  *
  65.  * This code does not set for 8 bit characters (which is required). It
  66.  * also assumes that carriage return/newline is needed, and not just
  67.  * carriage return.  These are all switch settings on the DMP, and
  68.  * I have configured them for 8 bit data and cr only.
  69.  *
  70.  * You can search for the strings Init and Reset to find the strings
  71.  * that set up the printer and clear things when finished, and change
  72.  * them to meet your needs.
  73.  *
  74.  * Also, you need to make sure that the printer daemon (assuming unix)
  75.  * doesn't change the data as it is being printed.  I have set my
  76.  * printcap file (sunos 4.1.1) with the string:
  77.  * ms=pass8,-opost
  78.  * and it works fine.
  79.  *
  80.  * Feel free to improve this code if you want.  However, please make
  81.  * sure that the old DMP will still be supported by any changes.  This
  82.  * may mean making an imagewriter device, and just copying this file
  83.  * to something like gdevimage.c.
  84.  *
  85.  * The limiting factor of the DMP is the vertical resolution.  However, I
  86.  * see no way to do anything about this.  Horizontal resolution could
  87.  * be increased by using 17 cpi (136 dpi).  I believe the Imagewriter
  88.  * supports 24 cpi (192 dpi).  However, the higher dpi, the slower
  89.  * the printing.
  90.  *
  91.  * Dot Matrix Code by Mark Wedel (master@cats.ucsc.edu)
  92.  */
  93.  
  94.  
  95. #include "gdevprn.h"
  96.  
  97. /* The device descriptors */
  98. private dev_proc_print_page(dmp_print_page);
  99.  
  100. /* Standard DMP device */
  101. gx_device_printer far_data gs_appledmp_device =
  102.   prn_device(prn_std_procs, "appledmp",
  103.     85,                /* width_10ths, 8.5" */
  104.     110,                /* height_10ths, 11" */
  105.     120, 72,            /* X_DPI, Y_DPI */
  106.     0, 0.5, 0.5, 0,        /* margins */
  107.     1, dmp_print_page);
  108.  
  109.  
  110. /*  lowrez Imagewriter device */
  111. gx_device_printer far_data gs_iwlo_device =
  112.   prn_device(prn_std_procs, "iwlo",
  113.     85,                /* width_10ths, 8.5" */
  114.     110,                /* height_10ths, 11" */
  115.     160, 72,            /* X_DPI, Y_DPI */
  116.     0, 0.5, 0.5, 0,        /* margins */
  117.     1, dmp_print_page);
  118.  
  119.  
  120. /*  hirez Imagewriter device */
  121. gx_device_printer far_data gs_iwhi_device =
  122.   prn_device(prn_std_procs, "iwhi",
  123.     85,                /* width_10ths, 8.5" */
  124.     110,                /* height_10ths, 11" */
  125.     160, 144,            /* X_DPI, Y_DPI */
  126.     0, 0.5, 0.5, 0,        /* margins */
  127.     1, dmp_print_page);
  128.  
  129.  
  130. /* ------ Internal routines ------ */
  131.  
  132.  
  133. /* Send the page to the printer. */
  134. private int
  135. dmp_print_page(gx_device_printer *pdev, FILE *prn_stream)
  136. {    
  137.  
  138.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  139.     /* Note that in_size is a multiple of 8. */
  140.     int in_size = line_size * 8;
  141.     byte *buf1 = (byte *)gs_malloc(in_size, 1, "dmp_print_page(buf1)");
  142.     byte *buf2 = (byte *)gs_malloc(in_size, 1, "dmp_print_page(buf2)");
  143.     byte *in = buf1;
  144.     byte *out = buf2;
  145.     int lnum = 0;
  146.     int x_dpi,y_dpi;
  147.  
  148.     x_dpi= (int)(pdev->x_pixels_per_inch);
  149.     y_dpi= (int)(pdev->y_pixels_per_inch);
  150.  
  151.     /* Check allocations */
  152.     if ( buf1 == 0 || buf2 == 0 )
  153.     {    if ( buf1 ) 
  154.           gs_free((char *)buf1, in_size, 1, "dmp_print_page(buf1)");
  155.         if ( buf2 ) 
  156.           gs_free((char *)buf2, in_size, 1, "dmp_print_page(buf2)");
  157.         return_error(gs_error_VMerror);
  158.     }
  159.  
  160.     /* Initialize the printer and reset the margins. */
  161.     /* fwrite("\r\n\033>\033q\033T16", 1, 10, prn_stream); */
  162.     switch( x_dpi )
  163.     {
  164.         case 136: fwrite("\r\n\033>\033Q\033T16", 1, 10, prn_stream); break;
  165.         case 144: fwrite("\r\n\033>\033p\033T16", 1, 10, prn_stream); break;
  166.         case 160: fwrite("\r\n\033>\033P\033T16", 1, 10, prn_stream); break;
  167.         case 120:
  168.         default:  fwrite("\r\n\033>\033q\033T16", 1, 10, prn_stream); break;
  169.     }
  170.  
  171.     /* Print lines of graphics */
  172.     while ( lnum < pdev->height )
  173.     {    
  174.         byte *inp;
  175.         byte *in_end;
  176.         byte *out_end;
  177.         int lcnt,ltmp;
  178.         register byte *out_blk;
  179.  
  180. /* The apple DMP printer seems to be odd in that the bit order on
  181.  * each line is reverse what might be expected.  Meaning, an
  182.  * underscore would be done as a series of 0x80, while on overscore
  183.  * would be done as a series of 0x01.  So we get each
  184.  * scan line in reverse order.
  185.  */
  186.  
  187.         /* do even field */
  188.         for (lcnt=0; lcnt<8; lcnt++)
  189.         {
  190.             if ( y_dpi==144) ltmp= 2*lcnt+0;
  191.             else ltmp= lcnt;
  192.  
  193.             if ((lnum+ltmp)>pdev->height) 
  194.                 memset(in+lcnt*line_size, 0, line_size);
  195.             else
  196.                 gdev_prn_copy_scan_lines(pdev, lnum+ltmp,
  197.                     in + line_size*(7 - lcnt), line_size);
  198.         }
  199.  
  200.         out_end = out;
  201.         inp = in;
  202.         in_end = inp + line_size;
  203.     
  204.                    for ( ; inp < in_end; inp++, out_end += 8 )
  205.                        { 
  206.                         gdev_prn_transpose_8x8(inp, 
  207.                                line_size, out_end, 1);
  208.                 }
  209.             /* Remove trailing 0s. */
  210.              while ( out_end > out && out_end[-1] == 0 )
  211.                 {
  212.                        out_end--;
  213.             }
  214.         for (out_blk = out; out_blk< out_end; ) {
  215.             /* skip leading 0s */
  216.             if (*out_blk) break;
  217.             out_blk ++;
  218.         }
  219.         /* write out however many blank's as we got nulls.
  220.          * In fact, because of the overhead, this is only really
  221.          * efficient if you have 8 or more leading 0's.
  222.          */
  223.         if ((out_blk!=out) && ((out_blk-out)>7))
  224.             fprintf(prn_stream,"\033V%04d%c", (int) (out_blk-out), 0);
  225.         else
  226.             out_blk=out;
  227.  
  228.         if ((int)(out_end-out_blk)) {
  229.             fprintf(prn_stream,"\033G%04d",(int)(out_end - out_blk));
  230.             fwrite(out_blk, 1, (int)(out_end-out_blk), prn_stream);
  231.         }
  232.  
  233.         /* move down 1/144th if hirez */
  234.         if ( y_dpi==144 ) fprintf(prn_stream,"\033T01\r\n");
  235.         else fprintf(prn_stream,"\r\n");
  236.     
  237.         /* do odd field */
  238.  
  239.         if ( y_dpi==144 )
  240.         {
  241.             for (lcnt=0; lcnt<8; lcnt++)
  242.             {
  243.                 ltmp= 2*lcnt+1;
  244.                 if ((lnum+ltmp)>pdev->height) 
  245.                     memset(in+lcnt*line_size, 0, line_size);
  246.                 else
  247.                     gdev_prn_copy_scan_lines(pdev, lnum+ltmp,
  248.                         in + line_size*(7 - lcnt), line_size);
  249.             }
  250.     
  251.             out_end = out;
  252.             inp = in;
  253.             in_end = inp + line_size;
  254.     
  255.                     for ( ; inp < in_end; inp++, out_end += 8 )
  256.                     { 
  257.                     gdev_prn_transpose_8x8(inp, line_size, out_end, 1);
  258.                 }
  259.  
  260.             /* Remove trailing 0s. */
  261.              while ( out_end > out && out_end[-1] == 0 )
  262.                    {
  263.                        out_end--;
  264.             }
  265.             for (out_blk = out; out_blk< out_end; )
  266.             {
  267.                 /* skip leading 0s */
  268.                 if (*out_blk) break;
  269.                 out_blk ++;
  270.             }
  271.  
  272.             /* write out however many blank's as we got nulls.
  273.              * In fact, because of the overhead, this is only really
  274.              * efficient if you have 8 or more leading 0's.
  275.              */
  276.             if ((out_blk!=out) && ((out_blk-out)>7))
  277.                 fprintf(prn_stream,"\033V%04d%c", (int) (out_blk-out), 0);
  278.             else
  279.                 out_blk=out;
  280.  
  281.             if ((int)(out_end-out_blk))
  282.             {
  283.                 fprintf(prn_stream,"\033G%04d",
  284.                                         (int)(out_end - out_blk));
  285.                 fwrite(out_blk, 1,
  286.                     (int)(out_end-out_blk), prn_stream);
  287.             }
  288.  
  289.             if ( y_dpi==144 )
  290.                 fprintf(prn_stream,"\033T15\r\n");
  291.             else    fprintf(prn_stream,"\r\n");
  292.  
  293.         } /* end of odd field code */    
  294.  
  295.         if ( y_dpi==144) lnum += 16 ;
  296.         else lnum += 8 ;
  297.     }
  298.  
  299.     /* ImageWriter will skip a whole page if too close to end */
  300.     /* so skip back more than an inch */
  301.     if ( x_dpi!=120 ) fputs("\033T99\n\n\033r\n\n\n\n\033f", prn_stream);
  302.  
  303.  
  304.     /* Formfeed and Reset printer */
  305.     fputs("\033T16\f\033<\033B\033E", prn_stream);
  306.     fflush(prn_stream);
  307.  
  308.     gs_free((char *)buf2, in_size, 1, "dmp_print_page(buf2)");
  309.     gs_free((char *)buf1, in_size, 1, "dmp_print_page(buf1)");
  310.     return 0;
  311. }
  312.