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 / GDEVDJET.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  13KB  |  418 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. /* gdevdjet.c */
  21. /* HP LaserJet/DeskJet driver for Ghostscript */
  22. #include "gdevprn.h"
  23. #include "gdevpcl.h"
  24.  
  25. /*
  26.  * Thanks to Jim Mayer (mayer@wrc.xerox.com),
  27.  * Jan-Mark Wams (jms@cs.vu.nl), Frans van Hoesel (hoesel@rugr86.rug.nl),
  28.  * and George Cameron (g.cameron@biomed.abdn.ac.uk) for improvements.
  29.  */
  30.  
  31. /*
  32.  * You may select a resolution of 75, 100, 150, or 300 DPI.
  33.  * Normally you would do this in the makefile or on the gs command line,
  34.  * not here.
  35.  *
  36.  * If the preprocessor symbol A4 is defined, the default paper size is
  37.  * the European A4 size; otherwise it is the U.S. letter size (8.5"x11").
  38.  */
  39. /*#define X_DPI 300*/
  40. /*#define Y_DPI 300*/
  41.  
  42. #define X_DPI_MAX 300
  43. #define Y_DPI_MAX 300
  44.  
  45. #ifndef X_DPI
  46. #  define X_DPI X_DPI_MAX
  47. #endif
  48. #ifndef Y_DPI
  49. #  define Y_DPI Y_DPI_MAX
  50. #endif
  51.  
  52. /*
  53.  * For all DeskJet Printers:
  54.  *
  55.  *  Maximum printing width               = 2400 dots = 8"
  56.  *  Maximum recommended printing height  = 3100 dots = 10 1/3"
  57.  *
  58.  * All Deskjets have 1/2" unprintable bottom margin.
  59.  * The recommendation comes from the HP Software Developer's Guide for
  60.  * the DeskJet 500, DeskJet PLUS, and DeskJet printers, version C.01.00
  61.  * of 12/1/90.
  62.  *
  63.  * Note that the margins defined just below here apply only to the DeskJet;
  64.  * the paper size, width and height apply to the LaserJet as well.
  65.  */
  66.  
  67. /* Margins are left, bottom, right, top. */
  68. /* DeskJet values from Jim Mayer mayer@wrc.xerox.com. */
  69. #define DESKJET_MARGINS_LETTER  0.25, 0.597, 0.25, 0.07
  70. #define DESKJET_MARGINS_A4      0.15, 0.597, 0.15, 0.07
  71. /* Similar margins for the LaserJet, */
  72. /* from Eddy Andrews eeandrew@pyr.swan.ac.uk. */
  73. #define LASERJET_MARGINS_A4    0.25, 0.20, 0.25, 0.00
  74. #define LASERJET_MARGINS_LETTER    0.35, 0.20, 0.35, 0.00
  75.  
  76. #ifndef A4
  77. #  define WIDTH_10THS        85
  78. #  define HEIGHT_10THS        110
  79. #  define DESKJET_MARGINS    DESKJET_MARGINS_LETTER
  80. #  define LASERJET_MARGINS    LASERJET_MARGINS_LETTER
  81. #else
  82. #  define WIDTH_10THS        83    /* 210mm */
  83. #  define HEIGHT_10THS        117    /* 297mm */
  84. #  define DESKJET_MARGINS    DESKJET_MARGINS_A4
  85. #  define LASERJET_MARGINS    LASERJET_MARGINS_A4
  86. #endif
  87.  
  88. /* The number of blank lines that make it worthwhile to reposition */
  89. /* the cursor. */
  90. #define MIN_SKIP_LINES 7
  91.  
  92. /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
  93. #define W sizeof(word)
  94. #define LINE_SIZE ((X_DPI_MAX * 85 / 10 + W * 8 - 1) / (W * 8) * W)
  95.  
  96. /* Printer types */
  97. #define LJ    0
  98. #define LJplus    1
  99. #define LJ2p    2
  100. #define LJ3    3
  101. #define DJ    4
  102. #define DJ500    5
  103.  
  104. /* Printer compression capabilities */
  105. typedef enum {
  106.     mode_0,
  107.     mode_2,
  108.     mode_3        /* includes mode 2 */
  109. } compression_modes;
  110.  
  111. /* The printer initialization strings. */
  112. private const char *init_strings[] = {
  113.     /* LaserJet PCL 3, no compression */
  114.         "\033*p0x0Y\033*b0M",
  115.     /* LaserJet Plus PCL 3, no compression */
  116.         "\033*p0x0Y\033*b0M",
  117.     /* LaserJet IIP PCL 4, mode 2 compression */
  118.         "\033*r0F\033*p0x75Y\033*b2M",
  119.     /* LaserJet III PCL 5, mode 2&3 compression */
  120.         "\033*r0F\033*p0x75Y",
  121.     /* DeskJet almost PCL 4, mode 2 compression */
  122.         "\033&k1W\033*p0x37Y\033*b2M",
  123.     /* DeskJet 500 almost PCL 4, mode 2&3 compression */
  124.         "\033&k1W\033*p0x37Y",
  125. };
  126.  
  127. /* The device descriptors */
  128. private dev_proc_open_device(hpjet_open);
  129. private dev_proc_print_page(djet_print_page);
  130. private dev_proc_print_page(djet500_print_page);
  131. private dev_proc_print_page(ljet_print_page);
  132. private dev_proc_print_page(ljetplus_print_page);
  133. private dev_proc_print_page(ljet2p_print_page);
  134. private dev_proc_print_page(ljet3_print_page);
  135.  
  136. gx_device_procs prn_hp_procs =
  137.   prn_matrix_procs(hpjet_open, gdev_pcl_get_initial_matrix,
  138.     gdev_prn_output_page, gdev_prn_close);
  139.  
  140. gx_device_printer gs_deskjet_device =
  141.   prn_device(prn_hp_procs, "deskjet",
  142.     WIDTH_10THS, HEIGHT_10THS,
  143.     X_DPI, Y_DPI,
  144.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  145.     1, djet_print_page);
  146.  
  147. gx_device_printer gs_djet500_device =
  148.   prn_device(prn_hp_procs, "djet500",
  149.     WIDTH_10THS, HEIGHT_10THS,
  150.     X_DPI, Y_DPI,
  151.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  152.     1, djet500_print_page);
  153.  
  154. gx_device_printer gs_laserjet_device =
  155.   prn_device(prn_hp_procs, "laserjet",
  156.     WIDTH_10THS, HEIGHT_10THS,
  157.     X_DPI, Y_DPI,
  158.     0.05, 0.25, 0.55, 0.25,        /* margins */
  159.     1, ljet_print_page);
  160.  
  161. gx_device_printer gs_ljetplus_device =
  162.   prn_device(prn_hp_procs, "ljetplus",
  163.     WIDTH_10THS, HEIGHT_10THS,
  164.     X_DPI, Y_DPI,
  165.     0.05, 0.25, 0.55, 0.25,        /* margins */
  166.     1, ljetplus_print_page);
  167.  
  168. gx_device_printer gs_ljet2p_device =
  169.   prn_device(prn_hp_procs, "ljet2p",
  170.     WIDTH_10THS, HEIGHT_10THS,
  171.     X_DPI, Y_DPI,
  172.     0.20, 0.25, 0.25, 0.25,        /* margins */
  173.     1, ljet2p_print_page);
  174.  
  175. gx_device_printer gs_ljet3_device =
  176.   prn_device(prn_hp_procs, "ljet3",
  177.     WIDTH_10THS, HEIGHT_10THS,
  178.     X_DPI, Y_DPI,
  179.     0.20, 0.25, 0.25, 0.25,        /* margins */
  180.     1, ljet3_print_page);
  181.  
  182. /* Forward references */
  183. private int hpjet_print_page(P4(gx_device_printer *, FILE *, int, compression_modes));
  184.  
  185. /* Open the printer, adjusting the margins if necessary. */
  186. private int
  187. hpjet_open(gx_device *pdev)
  188. {    /* Change the margins if necessary. */
  189.     const float _ds *m;
  190. #define ppdev ((gx_device_printer *)pdev)
  191.     if ( ppdev->print_page == djet_print_page ||
  192.          ppdev->print_page == djet500_print_page
  193.        )
  194. #undef ppdev
  195.     {    static const float m_a4[4] = { DESKJET_MARGINS_A4 };
  196.         static const float m_letter[4] = { DESKJET_MARGINS_LETTER };
  197.         m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? m_a4 :
  198.             m_letter);
  199.     }
  200.     else    /* LaserJet */
  201.     {    static const float m_a4[4] = { LASERJET_MARGINS_A4 };
  202.         static const float m_letter[4] = { LASERJET_MARGINS_LETTER };
  203.         m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? m_a4 :
  204.             m_letter);
  205.     }
  206.     pdev->l_margin = m[0];
  207.     pdev->b_margin = m[1];
  208.     pdev->r_margin = m[2];
  209.     pdev->t_margin = m[3];
  210.     return gdev_prn_open(pdev);
  211. }
  212.  
  213. /* ------ Internal routines ------ */
  214.  
  215. /* The DeskJet can compress (mode 2) */
  216. private int
  217. djet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  218. {    return hpjet_print_page(pdev, prn_stream, DJ, mode_2);
  219. }
  220. /* The DeskJet500 can compress (modes 2&3) */
  221. private int
  222. djet500_print_page(gx_device_printer *pdev, FILE *prn_stream)
  223. {    return hpjet_print_page(pdev, prn_stream, DJ500, mode_3);
  224. }
  225. /* The LaserJet series II can't compress */
  226. private int
  227. ljet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  228. {    return hpjet_print_page(pdev, prn_stream, LJ, mode_0);
  229. }
  230. /* The LaserJet Plus can't compress */
  231. private int
  232. ljetplus_print_page(gx_device_printer *pdev, FILE *prn_stream)
  233. {    return hpjet_print_page(pdev, prn_stream, LJplus, mode_0);
  234. }
  235. /* All LaserJet series IIIs (III,IIId,IIIp,IIIsi) compress (modes 2&3) */
  236. private int
  237. ljet3_print_page(gx_device_printer *pdev, FILE *prn_stream)
  238. {    return hpjet_print_page(pdev, prn_stream, LJ3, mode_3);
  239. }
  240. /* LaserJet series IIp & IId compress (mode 2) */
  241. private int
  242. ljet2p_print_page(gx_device_printer *pdev, FILE *prn_stream)
  243. {    return hpjet_print_page(pdev, prn_stream, LJ2p, mode_2);
  244. }
  245.  
  246. /* Send the page to the printer.  For speed, compress each scan line, */
  247. /* since computer-to-printer communication time is often a bottleneck. */
  248. private int
  249. hpjet_print_page(gx_device_printer *pdev, FILE *prn_stream, int ptype,
  250.   compression_modes cmodes)
  251. {    int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  252.     int line_size_words = (line_size + W - 1) / W;
  253.     uint storage_size_words = line_size_words * 8; /* data, out_row, out_row_alt, prev_row */
  254.     word *storage = (ulong *)gs_malloc(storage_size_words, W,
  255.                        "hpjet_print_page");
  256.     word
  257.       *data_words,
  258.       *out_row_words,
  259.       *out_row_alt_words,
  260.       *prev_row_words;
  261. #define data ((char *)data_words)
  262. #define out_row ((char *)out_row_words)
  263. #define out_row_alt ((char *)out_row_alt_words)
  264. #define prev_row ((char *)prev_row_words)
  265.     char *out_data;
  266.     int x_dpi = pdev->x_pixels_per_inch;
  267.     int y_dots_per_pixel = Y_DPI_MAX / pdev->y_pixels_per_inch;
  268.     int out_count;
  269.     int compression = -1;
  270.     static const char *from2to3 = "\033*b3M";
  271.     static const char *from3to2 = "\033*b2M";
  272.     int penalty_from2to3 = strlen(from2to3);
  273.     int penalty_from3to2 = strlen(from3to2);
  274.     int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  275.  
  276.     if ( storage == 0 )    /* can't allocate working area */
  277.         return_error(gs_error_VMerror);
  278.     data_words = storage;
  279.     out_row_words = data_words + (line_size_words * 2);
  280.     out_row_alt_words = out_row_words + (line_size_words * 2);
  281.     prev_row_words = out_row_alt_words + (line_size_words * 2);
  282.     /* Clear temp storage */
  283.     memset(data, 0, storage_size_words * W);
  284.  
  285.     /* Initialize printer. */
  286.     fputs("\033E", prn_stream);        /* reset printer */
  287.     fputs("\033*rB", prn_stream);        /* end raster graphics */
  288.     fprintf(prn_stream, "\033*t%dR", x_dpi);    /* set resolution */
  289.     /* If the printer supports it, set the paper size */
  290.     /* based on the actual requested size. */
  291.     if ( !(ptype == LJ || ptype == LJplus) )
  292.     {    fprintf(prn_stream, "\033&l%dA", paper_size);
  293.     }
  294.     fputs("\033&l0o0e0L", prn_stream);
  295.     fputs(init_strings[ptype], prn_stream);
  296.  
  297.     /* Send each scan line in turn */
  298.        {    int lnum;
  299.         int num_blank_lines = 0;
  300.         word rmask = ~(word)0 << (-pdev->width & (W * 8 - 1));
  301.  
  302.         /* Transfer raster graphics. */
  303.         for ( lnum = 0; lnum < pdev->height; lnum++ )
  304.            {    register word *end_data = data_words + line_size_words;
  305.             gdev_prn_copy_scan_lines(pdev, lnum,
  306.                          (byte *)data, line_size);
  307.                /* Mask off 1-bits beyond the line width. */
  308.             end_data[-1] &= rmask;
  309.             /* Remove trailing 0s. */
  310.             while ( end_data > data_words && end_data[-1] == 0 )
  311.               end_data--;
  312.             if ( end_data == data_words )
  313.                {    /* Blank line */
  314.                 num_blank_lines++;
  315.                 continue;
  316.                }
  317.  
  318.             /* We've reached a non-blank line. */
  319.             /* Put out a spacing command if necessary. */
  320.             if ( num_blank_lines == lnum )
  321.             {    /* We're at the top of a page. */
  322.                 if ( num_blank_lines > 0 )
  323.                 {    /* move down from current position */
  324.                     fprintf(prn_stream, "\033*p+%dY",
  325.                         num_blank_lines * y_dots_per_pixel);
  326.                 }
  327.                 /* Start raster graphics. */
  328.                 fputs("\033*r1A", prn_stream);
  329.             }
  330.             /* Skip blank lines if any */
  331.             else if ( num_blank_lines != 0 )
  332.             {  if ( num_blank_lines < MIN_SKIP_LINES )
  333.                {    /* Moving down from current position */
  334.                 /* causes head motion on the DeskJet, so */
  335.                 /* if the number of lines is small, */
  336.                 /* we're better off printing blanks. */
  337.                 if ( cmodes == mode_3 )
  338.                 {    /* Must clear the seed row. */
  339.                     fputs("\033*bY", prn_stream);
  340.                 }
  341.                 for ( ; num_blank_lines; num_blank_lines-- )
  342.                     fputs("\033*bW", prn_stream);
  343.                }
  344.                else if ( cmodes == mode_0 )    /* PCL 3 */
  345.                {    fprintf(prn_stream, "\033*p+%dY",
  346.                         num_blank_lines * y_dots_per_pixel);
  347.                }
  348.                else
  349.                {       fprintf(prn_stream, "\033*b%dY",
  350.                        num_blank_lines);
  351.                }
  352.                /* Clear the seed row (only matters for */
  353.                /* mode 3 compression). */
  354.                memset(prev_row, 0, line_size);
  355.             }
  356.             num_blank_lines = 0;
  357.  
  358.             /* Choose the best compression mode */
  359.             /* for this particular line. */
  360.             switch (cmodes)
  361.               {
  362.               case mode_3:
  363.                {    /* Compression modes 2 and 3 are both */
  364.                 /* available.  Try both and see which one */
  365.                 /* produces the least output data. */
  366.                 int count3 = gdev_pcl_mode3compress(line_size, data,
  367.                                prev_row, out_row);
  368.                 int count2 = gdev_pcl_mode2compress(data_words, end_data,
  369.                                out_row_alt);
  370.                 int penalty3 =
  371.                   (compression == 3 ? 0 : penalty_from2to3);
  372.                 int penalty2 =
  373.                   (compression == 2 ? 0 : penalty_from3to2);
  374.                 if ( count3 + penalty3 < count2 + penalty2)
  375.                    {    if ( compression != 3 )
  376.                         fputs(from2to3, prn_stream);
  377.                     compression = 3;
  378.                     out_data = out_row;
  379.                     out_count = count3;
  380.                    }
  381.                 else
  382.                    {    if ( compression != 2 )
  383.                         fputs(from3to2, prn_stream);
  384.                     compression = 2;
  385.                     out_data = out_row_alt;
  386.                     out_count = count2;
  387.                    }
  388.                 break;
  389.                }
  390.               case mode_2:
  391.                 out_data = out_row;
  392.                    out_count = gdev_pcl_mode2compress(data_words, end_data,
  393.                               out_row);
  394.                 break;
  395.               default:
  396.                 out_data = data;
  397.                 out_count = (char *)end_data - data;
  398.               }
  399.  
  400.             /* Transfer the data */
  401.             fprintf(prn_stream, "\033*b%dW", out_count);
  402.             fwrite(out_data, sizeof(char), out_count,
  403.                    prn_stream);
  404.            }
  405.     }
  406.  
  407.     /* end raster graphics */
  408.     fputs("\033*rB", prn_stream);
  409.  
  410.     /* eject page */
  411.     fputs("\033&l0H", prn_stream);
  412.  
  413.     /* free temporary storage */
  414.     gs_free((char *)storage, storage_size_words, W, "hpjet_print_page");
  415.  
  416.     return 0;
  417. }
  418.