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 / GDEVEPSC.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  14KB  |  461 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. /* gdevepsc.c */
  21. /* Epson color dot-matrix printer driver for Ghostscript by dave@exlog.com */
  22. #include "gdevprn.h"
  23.  
  24. /*
  25.  * For 9-pin printers, you may select
  26.  *   X_DPI = 60, 120, or 240
  27.  *   Y_DPI = 60 or 72
  28.  * For 24-pin printers, you may select
  29.  *   X_DPI = 60, 120, 180, 240, or 360
  30.  *   Y_DPI = 60, 72, 180, or 216
  31.  * Note that a given printer implements *either* Y_DPI = 60 | 180 *or*
  32.  * Y_DPI = 72 | 216; no attempt is made to check this here.
  33.  * Note that X_DPI = 180 or 360 requires Y_DPI > 100;
  34.  * this isn't checked either.  Finally, note that X_DPI=240 and
  35.  * X_DPI=360 are double-density modes requiring two passes to print.
  36.  *
  37.  * The values of X_DPI and Y_DPI may be set at compile time:
  38.  * see gdevs.mak.
  39.  * 
  40.  * At some time in the future, we could simulate 24-bit output on
  41.  * 9-pin printers by using fractional vertical positioning;
  42.  * we could even implement an X_DPI=360 mode by using the
  43.  * ESC++ command that spaces vertically in units of 1/360"
  44.  * (not supported on many printers.)
  45.  */
  46.  
  47. #ifndef X_DPI
  48. #  define X_DPI 180            /* pixels per inch */
  49. #endif
  50. #ifndef Y_DPI
  51. #  define Y_DPI 180            /* pixels per inch */
  52. #endif
  53.  
  54. /*
  55. **    Colors for EPSON LQ-2550.
  56. **
  57. **    We map VIOLET to BLUE since this is the best we can do.
  58. */
  59. #define BLACK    0
  60. #define MAGENTA 1
  61. #define CYAN    2
  62. #define VIOLET    3
  63. #define YELLOW    4
  64. #define RED        5
  65. #define GREEN    6
  66. #define WHITE    7
  67.  
  68. /*
  69. **    The offset in this array correspond to
  70. **    the ESC-r n value
  71. */
  72. static char rgb_color[2][2][2] =    {
  73.     BLACK, VIOLET, GREEN,
  74.     CYAN, RED, MAGENTA,
  75.     YELLOW, WHITE,
  76.     };
  77.  
  78. /* Map an RGB color to a printer color. */
  79. #define cv_shift (sizeof(gx_color_value) * 8 - 1)
  80. private gx_color_index
  81. epson_map_rgb_color(gx_device *dev,
  82.   gx_color_value r, gx_color_value g, gx_color_value b)
  83. {
  84. if (gx_device_has_color(dev))
  85.     {
  86. /* use ^7 so WHITE is 0 for internal calculations */
  87.     return (gx_color_index)rgb_color[r >> cv_shift][g >> cv_shift][b >> cv_shift] ^ 7;    
  88.     }
  89. else
  90.     {
  91.     return gx_default_map_rgb_color(dev, r, g, b);
  92.     }
  93. }
  94.  
  95. /* Map the printer color back to RGB. */
  96. private int
  97. epson_map_color_rgb(gx_device *dev, gx_color_index color,
  98.   gx_color_value prgb[3])
  99. {
  100. #define c1 gx_max_color_value
  101. if (gx_device_has_color(dev))
  102.     switch ((ushort)color ^ 7)
  103.         {
  104.         case BLACK:
  105.             prgb[0] = 0; prgb[1] = 0; prgb[2] = 0; break;
  106.         case VIOLET:
  107.             prgb[0] = 0; prgb[1] = 0; prgb[2] = c1; break;
  108.         case GREEN:
  109.             prgb[0] = 0; prgb[1] = c1; prgb[2] = 0; break;
  110.         case CYAN:
  111.             prgb[0] = 0; prgb[1] = c1; prgb[2] = c1; break;
  112.         case  RED:
  113.             prgb[0] = c1; prgb[1] = 0; prgb[2] = 0; break;
  114.         case  MAGENTA:
  115.             prgb[0] = c1; prgb[1] = 0; prgb[2] = c1; break;
  116.         case YELLOW:
  117.             prgb[0] = c1; prgb[1] = c1; prgb[2] = 0; break;
  118.         case  WHITE:
  119.             prgb[0] = c1; prgb[1] = c1; prgb[2] = c1; break;
  120.         }
  121.     else
  122.         return gx_default_map_color_rgb(dev, color, prgb);
  123.     return 0;
  124. }
  125.  
  126. /* The device descriptor */
  127. private dev_proc_print_page(eps_print_page);
  128.  
  129. private gx_device_procs epson_procs =
  130.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  131.     epson_map_rgb_color, epson_map_color_rgb); 
  132.  
  133. gx_device_printer gs_epsonc_device =
  134.   prn_device(epson_procs, "epsonc",
  135.     85,                /* width_10ths, 8.5" */
  136.     110,                /* height_10ths, 11" */
  137.     X_DPI, Y_DPI,
  138.     0, 0, 0.25, 0,        /* margins */
  139.     3, eps_print_page);
  140.  
  141. /* ------ Internal routines ------ */
  142.  
  143. /* Forward references */
  144. private void eps_output_run(P6(byte *, int, int, char, FILE *, int));
  145.  
  146. /* Send the page to the printer. */
  147. #define DD 0x80                /* double density flag */
  148. private int
  149. eps_print_page(gx_device_printer *pdev, FILE *prn_stream)
  150. {    static char graphics_modes_9[5] =
  151.        {    -1, 0 /*60*/, 1 /*120*/, -1, DD+3 /*240*/
  152.        };
  153.     static char graphics_modes_24[7] =
  154.        {    -1, 32 /*60*/, 33 /*120*/, 39 /*180*/,
  155.         -1, -1, DD+40 /*360*/
  156.        };
  157.     int y_24pin = pdev->y_pixels_per_inch > 72;
  158.     int y_mult = (y_24pin ? 3 : 1);
  159.     int line_size = (pdev->width + 7) >> 3;    /* always mono */
  160.     int in_size = line_size * (8 * y_mult);
  161.     byte *in = (byte *)gs_malloc(in_size, 1, "eps_print_page(in)");
  162.     int out_size = ((pdev->width + 7) & -8) * y_mult;
  163.     byte *out = (byte *)gs_malloc(out_size, 1, "eps_print_page(out)");
  164.     int x_dpi = pdev->x_pixels_per_inch;
  165.     char start_graphics =
  166.         (y_24pin ? graphics_modes_24 : graphics_modes_9)[x_dpi / 60];
  167.     int first_pass = (start_graphics & DD ? 1 : 0);
  168.     int last_pass = first_pass * 2;
  169.     int dots_per_space = x_dpi / 10;    /* pica space = 1/10" */
  170.     int bytes_per_space = dots_per_space * y_mult;
  171.     int skip = 0, lnum = 0, pass;
  172. /* declare color buffer and related vars */
  173.     byte *color_in;
  174.     int color_line_size, color_in_size;
  175.     int spare_bits = (pdev->width % 8);    /* left over bits to go to margin */
  176.     int whole_bits = pdev->width - spare_bits;
  177.  
  178.     /* Check allocations */
  179.     if ( in == 0 || out == 0 )
  180.        {    if ( in ) gs_free((char *)in, in_size, 1, "eps_print_page(in)");
  181.         if ( out ) gs_free((char *)out, out_size, 1, "eps_print_page(out)");
  182.         return -1;
  183.        }
  184.  
  185.     /* Initialize the printer and reset the margins. */
  186.     fwrite("\033@\033P\033l\000\033Q\377\033U\001\r", 1, 14, prn_stream);
  187.  
  188. /*    Create color buffer */
  189.     if (gx_device_has_color(pdev))
  190.         {
  191.         color_line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  192.         color_in_size = color_line_size * (8 * y_mult);
  193.         if((color_in = (byte *)gs_malloc(color_in_size, 1,
  194.             "eps_print_page(color)")) == 0)
  195.             {
  196.             gs_free((char *)in, in_size, 1, "eps_print_page(in)");
  197.             gs_free((char *)out, out_size, 1, "eps_print_page(out)");
  198.             return(-1);
  199.             }
  200.         }
  201.     else
  202.         {
  203.         color_in = in;
  204.         color_in_size = in_size;
  205.         color_line_size = line_size;
  206.         }
  207.  
  208.     /* Print lines of graphics */
  209.     while ( lnum < pdev->height )
  210.        {
  211.         int lcnt;
  212.         byte *nextcolor = NULL;    /* position where next color appears */
  213.         byte *nextmono = NULL;    /* position to map next color */
  214.  
  215.         /* Copy 1 scan line and test for all zero. */
  216.         gdev_prn_copy_scan_lines(pdev, lnum, color_in, color_line_size);
  217.  
  218.         if ( color_in[0] == 0 &&
  219.              !memcmp((char *)color_in, (char *)color_in + 1, color_line_size - 1)
  220.            )
  221.            {    lnum++;
  222.             skip += 3 / y_mult;
  223.             continue;
  224.            }
  225.  
  226.         /* Vertical tab to the appropriate position. */
  227.         while ( skip > 255 )
  228.            {    fputs("\033J\377", prn_stream);
  229.             skip -= 255;
  230.            }
  231.         if ( skip )
  232.             fprintf(prn_stream, "\033J%c", skip);
  233.  
  234.         /* Copy the rest of the scan lines. */
  235.         lcnt = 1 + gdev_prn_copy_scan_lines(pdev, lnum + 1, 
  236.             color_in + color_line_size, color_in_size - color_line_size);
  237.  
  238.         if ( lcnt < 8 * y_mult )
  239.             {
  240.             memset((char *)(color_in + lcnt * color_line_size), 0,
  241.                 color_in_size - lcnt * color_line_size);
  242.             if (gx_device_has_color(pdev))    /* clear the work buffer */
  243.                 memset((char *)(in + lcnt * line_size), 0,
  244.                     in_size - lcnt * line_size);
  245.             }
  246.             
  247. /*
  248. **    We need to create a normal epson scan line from our color scan line
  249. **    We do this by setting a bit in the "in" buffer if the pixel byte is set
  250. **    to any color.  We then search for any more pixels of that color, setting
  251. **    "in" accordingly.  If any other color is found, we save it for the next
  252. **    pass.  There may be up to 7 passes.
  253. **    In the future, we should make the passes so as to maximize the
  254. **    life of the color ribbon (i.e. go lightest to darkest).
  255. */
  256.         do
  257.         {
  258.         byte *inp = in;
  259.         byte *in_end = in + line_size;
  260.         byte *out_end = out;
  261.         byte *out_blk;
  262.         register byte *outp;
  263.  
  264.         if (gx_device_has_color(pdev))
  265.             {
  266.             register i,j;
  267.             register byte *outbuf, *realbuf;
  268.             byte current_color;
  269.             int end_next_bits = whole_bits;
  270.             int lastbits;
  271.             
  272. /*    Move to the point in the scanline that has a new color */
  273.             if (nextcolor)
  274.                 {
  275.                 realbuf = nextcolor;
  276.                 outbuf = nextmono;
  277.                 memset((char *)in, 0, (nextmono - in));
  278.                 i = nextcolor - color_in;
  279.                 nextcolor = NULL;
  280.                 end_next_bits = (i / color_line_size) * color_line_size
  281.                     + whole_bits;
  282.                 }
  283.             else
  284.                 {
  285.                 i = 0;
  286.                 realbuf = color_in;
  287.                 outbuf = in;
  288.                 nextcolor = NULL;
  289.                 }
  290. /*    move thru the color buffer, turning on the appropriate
  291. **    bit in the "mono" buffer", setting pointers to the next
  292. **    color and changing the color output of the epson
  293. */
  294.             for (current_color = 0; i <= color_in_size; outbuf++)
  295.                 {
  296. /*    Remember, line_size is rounded up to next whole byte
  297. **    whereas color_line_size is the proper length
  298. **    We only want to set the proper bits in the last line_size byte.
  299. */
  300.                 if (spare_bits && i == end_next_bits)
  301.                     {
  302.                     end_next_bits = whole_bits + i + spare_bits;
  303.                     lastbits = 8 - spare_bits;
  304.                     }
  305.                 else
  306.                     lastbits = 0;
  307.  
  308.                 for (*outbuf = 0, j = 8; --j >= lastbits && i <= color_in_size;
  309.                     realbuf++,i++)
  310.                     {
  311.                     if (*realbuf)
  312.                         {
  313.                         if (current_color > 0)
  314.                             {
  315.                             if (*realbuf == current_color)
  316.                                 {
  317.                                 *outbuf |= 1 << j;
  318.                                 *realbuf = 0;    /* throw this byte away */
  319.                                 }
  320.                     /* save this location for next pass */
  321.                             else if (nextcolor == NULL)
  322.                                 {
  323.                                 nextcolor = realbuf - (7 - j);
  324.                                 nextmono = outbuf;
  325.                                 }
  326.                             }
  327.                         else 
  328.                             {
  329.                             *outbuf |= 1 << j;
  330.                             current_color = *realbuf;    /* set color */
  331.                             *realbuf = 0;
  332.                             }
  333.                         }
  334.                     }
  335.                 }
  336.             *outbuf = 0;        /* zero the end, for safe keeping */
  337. /*    Change color on the EPSON, current_color must be set
  338. **    but lets check anyway
  339. */
  340.             if (current_color)
  341.                 fprintf(prn_stream,"\033r%d",current_color ^ 7);
  342.             }
  343.  
  344.         /* We have to 'transpose' blocks of 8 pixels x 8 lines, */
  345.         /* because that's how the printer wants the data. */
  346.         /* If we are in a 24-pin mode, we have to transpose */
  347.         /* groups of 3 lines at a time. */
  348.  
  349.         if ( y_24pin )
  350.          { for ( ; inp < in_end; inp++, out_end += 24 )
  351.             { gdev_prn_transpose_8x8(inp, line_size, out_end, 3);
  352.               gdev_prn_transpose_8x8(inp + line_size * 8, line_size, out_end + 1, 3);
  353.               gdev_prn_transpose_8x8(inp + line_size * 16, line_size, out_end + 2, 3);
  354.             }
  355.            /* Remove trailing 0s. */
  356.            while ( out_end > out && out_end[-1] == 0 &&
  357.                out_end[-2] == 0 && out_end[-3] == 0
  358.              )
  359.              out_end -= 3;
  360.          }
  361.         else
  362.          { for ( ; inp < in_end; inp++, out_end += 8 )
  363.             { gdev_prn_transpose_8x8(inp, line_size, out_end, 1);
  364.             }
  365.            /* Remove trailing 0s. */
  366.            while ( out_end > out && out_end[-1] == 0 )
  367.              out_end--;
  368.          }
  369.  
  370.         for ( pass = first_pass; pass <= last_pass; pass++ )
  371.            {
  372.         for ( out_blk = outp = out; outp < out_end; )
  373.          { /* Skip a run of leading 0s. */
  374.            /* At least 10 are needed to make tabbing worth it. */
  375.            /* We do everything by 3's to avoid having to make */
  376.            /* different cases for 9- and 24-pin. */
  377.  
  378.            if ( *outp == 0 && outp + 12 <= out_end &&
  379.             outp[1] == 0 && outp[2] == 0 &&
  380.             (outp[3] | outp[4] | outp[5]) == 0 &&
  381.             (outp[6] | outp[7] | outp[8]) == 0 &&
  382.             (outp[9] | outp[10] | outp[11]) == 0
  383.               )
  384.             {    byte *zp = outp;
  385.             int tpos;
  386.             byte *newp;
  387.             outp += 12;
  388.             while ( outp + 3 <= out_end && *outp == 0 &&
  389.                 outp[1] == 0 && outp[2] == 0
  390.                   )
  391.                 outp += 3;
  392.             tpos = (outp - out) / bytes_per_space;
  393.             newp = out + tpos * bytes_per_space;
  394.             if ( newp > zp + 10 )
  395.              { /* Output preceding bit data. */
  396.                if ( zp > out_blk )    /* only false at */
  397.                         /* beginning of line */
  398.                  eps_output_run(out_blk, (int)(zp - out_blk),
  399.                         y_mult, start_graphics,
  400.                         prn_stream, pass);
  401.                /* Tab over to the appropriate position. */
  402.                fprintf(prn_stream, "\033D%c%c\t", tpos, 0);
  403.                out_blk = outp = newp;
  404.              }
  405.            }
  406.           else
  407.             outp += y_mult;
  408.          }
  409.         if ( outp > out_blk )
  410.             eps_output_run(out_blk, (int)(outp - out_blk),
  411.                        y_mult, start_graphics,
  412.                        prn_stream, pass);
  413.  
  414.             fputc('\r', prn_stream);
  415.            }
  416.             } while (nextcolor);
  417.         skip = 24;
  418.         lnum += 8 * y_mult;
  419.        }
  420.  
  421.     /* Eject the page and reinitialize the printer */
  422.     fputs("\f\033@", prn_stream);
  423.  
  424.  
  425.     gs_free((char *)out, out_size, 1, "eps_print_page(out)");
  426.     gs_free((char *)in, in_size, 1, "eps_print_page(in)");
  427.     if (gx_device_has_color(pdev))
  428.         gs_free((char *)color_in, color_in_size, 1, "eps_print_page(rin)");
  429.     return 0;
  430. }
  431.  
  432. /* Output a single graphics command. */
  433. /* pass=0 for all columns, 1 for even columns, 2 for odd columns. */
  434. private void
  435. eps_output_run(byte *data, int count, int y_mult,
  436.   char start_graphics, FILE *prn_stream, int pass)
  437. {    int xcount = count / y_mult;
  438.     fputc(033, prn_stream);
  439.     if ( !(start_graphics & ~3) )
  440.        {    fputc("KLYZ"[start_graphics], prn_stream);
  441.        }
  442.     else
  443.        {    fputc('*', prn_stream);
  444.         fputc(start_graphics & ~DD, prn_stream);
  445.        }
  446.     fputc(xcount & 0xff, prn_stream);
  447.     fputc(xcount >> 8, prn_stream);
  448.     if ( !pass )
  449.         fwrite((char *)data, 1, count, prn_stream);
  450.     else
  451.        {    /* Only write every other column of y_mult bytes. */
  452.         int which = pass;
  453.         byte *dp = data;
  454.         register int i, j;
  455.         for ( i = 0; i < xcount; i++, which++ )
  456.           for ( j = 0; j < y_mult; j++, dp++ )
  457.            {    putc(((which & 1) ? *dp : 0), prn_stream);
  458.            }
  459.        }
  460. }
  461.