home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GDEVEPSN.C < prev    next >
C/C++ Source or Header  |  1992-08-02  |  11KB  |  377 lines

  1. /* Copyright (C) 1989-1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevepsn.c */
  21. /*
  22.  * Epson dot-matrix printer driver for Ghostscript.
  23.  *
  24.  * Two devices are defined here: 'epson' and 'eps9high'.  The 'epson' device
  25.  * is the generic device, for 9-pin and 24-pin printers.  'eps9high' is
  26.  * a special mode for 9-pin printers where scan lines are interleaved in
  27.  * multiple passes to produce high vertical resolution at the expense of
  28.  * several passes of the print head.
  29.  *
  30.  * Thanks to David Wexelblat (dwex@mtgzfs3.att.com) for the 'eps9high' code.
  31.  */
  32. #include "gdevprn.h"
  33.  
  34. /*
  35.  * Valid values for X_DPI:
  36.  *
  37.  *    For 9-pin printers: 60, 120, 240
  38.  *    For 24-pin printers: 60, 120, 180, 240, 360
  39.  *
  40.  * The value specified at compile time is the default value used if the
  41.  * user does not specify a resolution at runtime.
  42.  */
  43. #ifndef X_DPI
  44. #  define X_DPI 240
  45. #endif
  46.  
  47. /*
  48.  * For Y_DPI, a given printer will support a base resolution of 60 or 72;
  49.  * check the printer manual.  The Y_DPI value must be a multiple of this
  50.  * base resolution.  Valid values for Y_DPI:
  51.  *
  52.  *    For 9-pin printers: 1*base_res
  53.  *    For 24-pin printers: 1*base_res, 3*base_res
  54.  *
  55.  * The value specified at compile time is the default value used if the
  56.  * user does not specify a resolution at runtime.
  57.  */
  58.  
  59. #ifndef Y_BASERES
  60. #  define Y_BASERES 72
  61. #endif
  62. #ifndef Y_DPI
  63. #  define Y_DPI (1*Y_BASERES)
  64. #endif
  65.  
  66. /* The device descriptors */
  67. private dev_proc_print_page(eps_print_page1);
  68. private dev_proc_print_page(eps_print_page2);
  69.  
  70. /* Standard Epson device */
  71. gx_device_printer gs_epson_device =
  72.   prn_device(prn_std_procs, "epson",
  73.     85,                /* width_10ths, 8.5" */
  74.     110,                /* height_10ths, 11" */
  75.     X_DPI, Y_DPI,
  76.     0, 0, 0.5, 0,        /* margins */
  77.     1, eps_print_page1);
  78.  
  79. /* High-res (interleaved) 9-pin device */
  80. gx_device_printer gs_eps9high_device = 
  81.   prn_device(prn_std_procs, "eps9high",
  82.     85,                /* width_10ths, 8.5" */
  83.     110,                /* height_10ths, 11" */
  84.     X_DPI, 3*Y_BASERES,
  85.     0, 0, 0.5, 0,        /* margins */
  86.     1, eps_print_page2);
  87.  
  88. /* ------ Internal routines ------ */
  89.  
  90. /* Forward references */
  91. private void eps_output_run(P6(byte *, int, int, char, FILE *, int));
  92.  
  93. /* Send the page to the printer. */
  94. #define DD 0x80                /* double density flag */
  95. private int
  96. eps_print_page(gx_device_printer *pdev, FILE *prn_stream, int y_9pin_high)
  97. {    
  98.     static const char graphics_modes_9[5] =
  99.     {    
  100.     -1, 0 /*60*/, 1 /*120*/, -1, DD+3 /*240*/
  101.     };
  102.  
  103.     static const char graphics_modes_24[7] =
  104.     {    
  105.         -1, 32 /*60*/, 33 /*120*/, 39 /*180*/,
  106.     -1, -1, DD+40 /*360*/
  107.     };
  108.  
  109.     int y_24pin = (y_9pin_high ? 0 : pdev->y_pixels_per_inch > 72);
  110.     int in_y_mult = ((y_24pin | y_9pin_high) ? 3 : 1);
  111.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  112.     /* Note that in_size is a multiple of 8. */
  113.     int in_size = line_size * (8 * in_y_mult);
  114.     byte *buf1 = (byte *)gs_malloc(in_size, 1, "eps_print_page(buf1)");
  115.     byte *buf2 = (byte *)gs_malloc(in_size, 1, "eps_print_page(buf2)");
  116.     byte *in = buf1;
  117.     byte *out = buf2;
  118.     int out_y_mult = (y_24pin ? 3 : 1);
  119.     int x_dpi = pdev->x_pixels_per_inch;
  120.     char start_graphics =
  121.         (y_24pin ? graphics_modes_24 : graphics_modes_9)[x_dpi / 60];
  122.     int first_pass = (start_graphics & DD ? 1 : 0);
  123.     int last_pass = first_pass * 2;
  124.     int y_passes = (y_9pin_high ? 3 : 1);
  125.     int dots_per_space = x_dpi / 10;    /* pica space = 1/10" */
  126.     int bytes_per_space = dots_per_space * out_y_mult;
  127.     int skip = 0, lnum = 0, pass, ypass;
  128.  
  129.     /* Check allocations */
  130.     if ( buf1 == 0 || buf2 == 0 )
  131.     {    if ( buf1 ) 
  132.           gs_free((char *)buf1, in_size, 1, "eps_print_page(buf1)");
  133.         if ( buf2 ) 
  134.           gs_free((char *)buf2, in_size, 1, "eps_print_page(buf2)");
  135.         return_error(gs_error_VMerror);
  136.     }
  137.  
  138.     /* Initialize the printer and reset the margins. */
  139.     fwrite("\033@\033P\033l\000\r\033Q", 1, 10, prn_stream);
  140.     fputc((int)(pdev->width / pdev->x_pixels_per_inch * 10) + 2,
  141.           prn_stream);
  142.  
  143.     /* Print lines of graphics */
  144.     while ( lnum < pdev->height )
  145.     {    
  146.         byte *inp;
  147.         byte *in_end;
  148.         byte *out_end;
  149.         byte *out_blk;
  150.         register byte *outp;
  151.         int lcnt;
  152.  
  153.         /* Copy 1 scan line and test for all zero. */
  154.         gdev_prn_copy_scan_lines(pdev, lnum, in, line_size);
  155.         if ( in[0] == 0 &&
  156.              !memcmp((char *)in, (char *)in + 1, line_size - 1)
  157.            )
  158.             {    
  159.             lnum++;
  160.             skip += 3 / in_y_mult;
  161.             continue;
  162.         }
  163.  
  164.         /* Vertical tab to the appropriate position. */
  165.         while ( skip > 255 )
  166.         {    
  167.             fputs("\033J\377", prn_stream);
  168.             skip -= 255;
  169.         }
  170.         if ( skip )
  171.         {
  172.             fprintf(prn_stream, "\033J%c", skip);
  173.         }
  174.  
  175.         /* Copy the rest of the scan lines. */
  176.             lcnt = 1 + gdev_prn_copy_scan_lines(pdev, lnum + 1,
  177.                             in + line_size, 
  178.                                 in_size - line_size);
  179.         if ( lcnt < 8 * in_y_mult )
  180.         {    /* Pad with lines of zeros. */
  181.             memset(in + lcnt * line_size, 0,
  182.                    in_size - lcnt * line_size);
  183.         }
  184.  
  185.         if ( y_9pin_high )
  186.         {    /* Shuffle the scan lines */
  187.             byte *p;
  188.             int i;
  189.             static const char index[] =
  190.             {  0,  8, 16,  1,  9, 17,  
  191.                2, 10, 18,  3, 11, 19,
  192.                4, 12, 20,  5, 13, 21,
  193.                6, 14, 22,  7, 15, 23
  194.             };
  195.  
  196.             for ( i = 0; i < 24; i++ )
  197.             {
  198.                 memcpy(out+(index[i]*line_size),
  199.                        in+(i*line_size), line_size);
  200.             }
  201.             p = in;
  202.             in = out;
  203.             out = p;
  204.         }
  205.  
  206.     for ( ypass = 0; ypass < y_passes; ypass++ )
  207.     {
  208.         for ( pass = first_pass; pass <= last_pass; pass++ )
  209.         {
  210.         /* We have to 'transpose' blocks of 8 pixels x 8 lines, */
  211.         /* because that's how the printer wants the data. */
  212.         /* If we are in a 24-pin mode, we have to transpose */
  213.         /* groups of 3 lines at a time. */
  214.  
  215.             if ( pass == first_pass )
  216.             {
  217.             out_end = out;
  218.             inp = in;
  219.             in_end = inp + line_size;
  220.     
  221.             if ( y_24pin )
  222.                     { 
  223.                         for ( ; inp < in_end; inp++, out_end += 24 )
  224.                         { 
  225.                             gdev_prn_transpose_8x8(inp, line_size, out_end, 3);
  226.                             gdev_prn_transpose_8x8(inp + line_size * 8, 
  227.                                line_size, out_end + 1, 3);
  228.                             gdev_prn_transpose_8x8(inp + line_size * 16, 
  229.                                line_size, out_end + 2, 3);
  230.                 }
  231.                 /* Remove trailing 0s. */
  232.                 while ( out_end > out && out_end[-1] == 0 &&
  233.                                out_end[-2] == 0 && out_end[-3] == 0)
  234.                     {
  235.                      out_end -= 3;
  236.                 }
  237.                     }
  238.             else
  239.                     { 
  240.                         for ( ; inp < in_end; inp++, out_end += 8 )
  241.                         { 
  242.                         gdev_prn_transpose_8x8(inp + (ypass * 8*line_size), 
  243.                                line_size, out_end, 1);
  244.                 }
  245.             /* Remove trailing 0s. */
  246.             while ( out_end > out && out_end[-1] == 0 )
  247.                 {
  248.                        out_end--;
  249.             }
  250.                     }
  251.         }
  252.  
  253.         for ( out_blk = outp = out; outp < out_end; )
  254.                 { 
  255.                  /* Skip a run of leading 0s. */
  256.                     /* At least 10 are needed to make tabbing */
  257.                 /* worth it.  We do everything by 3's to */
  258.                 /* avoid having to make different cases */
  259.                 /* for 9- and 24-pin. */
  260.  
  261.            if ( *outp == 0 && outp + 12 <= out_end &&
  262.             outp[1] == 0 && outp[2] == 0 &&
  263.             (outp[3] | outp[4] | outp[5]) == 0 &&
  264.             (outp[6] | outp[7] | outp[8]) == 0 &&
  265.                     (outp[9] | outp[10] | outp[11]) == 0 )
  266.                     {
  267.                     byte *zp = outp;
  268.             int tpos;
  269.             byte *newp;
  270.            
  271.             outp += 12;
  272.                     while ( outp + 3 <= out_end && 
  273.                              *outp == 0 &&
  274.                         outp[1] == 0 && outp[2] == 0 )
  275.                     {
  276.                 outp += 3;
  277.                     }
  278.             tpos = (outp - out) / bytes_per_space;
  279.             newp = out + tpos * bytes_per_space;
  280.             if ( newp > zp + 10 )
  281.                     { 
  282.                     /* Output preceding bit data.*/
  283.                        if ( zp > out_blk )    
  284.                     {
  285.                         /* only false at beginning of line */
  286.                      eps_output_run(out_blk, (int)(zp - out_blk),
  287.                                    out_y_mult, start_graphics, 
  288.                            prn_stream, pass);
  289.                 }
  290.                 /* Tab over to the appropriate position. */
  291.                 fprintf(prn_stream, "\033D%c%c\t", tpos, 0);
  292.                 out_blk = outp = newp;
  293.             }
  294.             }
  295.             else
  296.             {
  297.                     outp += out_y_mult;
  298.             }
  299.                 }
  300.         if ( outp > out_blk )
  301.             {
  302.             eps_output_run(out_blk, (int)(outp - out_blk),
  303.                        out_y_mult, start_graphics,
  304.                    prn_stream, pass);
  305.             }
  306.  
  307.         fputc('\r', prn_stream);
  308.         }
  309.         if ( ypass < y_passes - 1 )
  310.         fputs("\033J\001", prn_stream);
  311.     }
  312.     skip = 24 - y_passes + 1;        /* no skip on last Y pass */
  313.     lnum += 8 * in_y_mult;
  314.     }
  315.  
  316.     /* Eject the page and reinitialize the printer */
  317.     fputs("\f\033@", prn_stream);
  318.     fflush(prn_stream);
  319.  
  320.     gs_free((char *)buf2, in_size, 1, "eps_print_page(buf2)");
  321.     gs_free((char *)buf1, in_size, 1, "eps_print_page(buf1)");
  322.     return 0;
  323. }
  324.  
  325. /* Output a single graphics command. */
  326. /* pass=0 for all columns, 1 for even columns, 2 for odd columns. */
  327. private void
  328. eps_output_run(byte *data, int count, int y_mult,
  329.   char start_graphics, FILE *prn_stream, int pass)
  330. {    
  331.     int xcount = count / y_mult;
  332.  
  333.     fputc(033, prn_stream);
  334.     if ( !(start_graphics & ~3) )
  335.     {    
  336.         fputc("KLYZ"[start_graphics], prn_stream);
  337.     }
  338.     else
  339.     {    
  340.         fputc('*', prn_stream);
  341.         fputc(start_graphics & ~DD, prn_stream);
  342.     }
  343.     fputc(xcount & 0xff, prn_stream);
  344.     fputc(xcount >> 8, prn_stream);
  345.     if ( !pass )
  346.     {
  347.         fwrite(data, 1, count, prn_stream);
  348.     }
  349.     else
  350.     {    
  351.         /* Only write every other column of y_mult bytes. */
  352.         int which = pass;
  353.         register byte *dp = data;
  354.         register int i, j;
  355.  
  356.         for ( i = 0; i < xcount; i++, which++ )
  357.         {
  358.             for ( j = 0; j < y_mult; j++, dp++ )
  359.             {
  360.                 putc(((which & 1) ? *dp : 0), prn_stream);
  361.             }
  362.         }
  363.     }
  364. }
  365.  
  366. private int
  367. eps_print_page1(gx_device_printer *pdev, FILE *prn_stream)
  368. {
  369.     return(eps_print_page(pdev, prn_stream, 0));
  370. }
  371.  
  372. private int
  373. eps_print_page2(gx_device_printer *pdev, FILE *prn_stream)
  374. {
  375.     return(eps_print_page(pdev, prn_stream, 1));
  376. }
  377.