home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevclj.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  22.4 KB  |  681 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /* $Id: gdevclj.c,v 1.2 2000/09/19 19:00:12 lpd Exp $ */
  20. /*
  21.  * H-P Color LaserJet 5/5M device; based on the PaintJet.
  22.  */
  23. #include "math_.h"
  24. #include "gx.h"
  25. #include "gsparam.h"
  26. #include "gdevprn.h"
  27. #include "gdevpcl.h"
  28.  
  29. typedef struct gx_device_clj_s gx_device_clj;
  30. struct gx_device_clj_s {
  31.     gx_device_common;
  32.     gx_prn_device_common;
  33.     bool rotated;
  34. };
  35.  
  36. #define pclj ((gx_device_clj *)pdev)
  37.  
  38. /*
  39.  * The HP Color LaserJet 5/5M provides a rather unexpected speed/performance
  40.  * tradeoff.
  41.  *
  42.  * When generating rasters, only the fixed (simple) color spaces provide
  43.  * reasonable performance (in this case, reasonable != good). However, in
  44.  * these modes, certain of the fully-saturated primary colors (cyan, blue,
  45.  * green, and red) are rendered differently as rasters as opposed to colored
  46.  * geometric objects. Hence, the color of the output will be other than what
  47.  * is expected.
  48.  *
  49.  * Alternatively, the direct color, 1-bit per pixel scheme can be used. This
  50.  * will produce the expected colors, but performance will deteriorate
  51.  * significantly (observed printing time will be about 3 times longer than
  52.  * when using the simple color mode).
  53.  *
  54.  * Note that when using the latter mode to view output from the PCL
  55.  * interpreter, geometric objects and raster rendered with other than
  56.  * geometric color spaces will have the same appearance as if sent directly
  57.  * to the CLJ, but rasters generated from simple color spaces will have a
  58.  * different appearance. To make the latter rasters match in appearance, the
  59.  * faster printing mode must be used (in which the case the other objects
  60.  * will not have the same appearance).
  61.  */
  62. #define USE_FAST_MODE
  63.  
  64. /* X_DPI and Y_DPI must be the same */
  65. #define X_DPI 300
  66. #define Y_DPI 300
  67.  
  68. /*
  69.  * Array of paper sizes, and the corresponding offsets.
  70.  */
  71. typedef struct clj_paper_size_s {
  72.     uint        tag;                /* paper type tag */
  73.     int         orient;             /* logical page orientation to use */
  74.     float       width, height;      /* in pts; +- 5 pts */
  75.     gs_point    offsets;            /* offsets in the given orientation */
  76. } clj_paper_size;
  77.  
  78. /*
  79.  * The Color LaserJet prints page sizes up to 11.8" wide (A4 size) in
  80.  * long-edge-feed (landscape) orientation. Only executive, letter, and
  81.  * A4 size are supported for color, so we don't bother to list the others.
  82.  */
  83. private const clj_paper_size    clj_paper_sizes[] = {
  84.     /* U.S. letter size comes first so it will be the default. */
  85.     {   2,  1, 11.00 * 72.0, 8.50 * 72.0, { .200 * 72.0, 0.0 } },
  86.     {   1,  1, 10.50 * 72.0, 7.25 * 72.0, { .200 * 72.0, 0.0 } },
  87.     {  26,  1, 11.69 * 72.0, 8.27 * 72.0, { .197 * 72.0, 0.0 } }
  88. };
  89.  
  90. /*
  91.  * The supported set of resolutions.
  92.  *
  93.  * The Color LaserJet 5/5M is actually a pseudo-contone device, with hardware
  94.  * capable of providing about 16 levels of intensity. The current code does
  95.  * not take advantage of this feature, because it is not readily controllable
  96.  * via PCL. Rather, the device is modeled as a bi-level device in each of
  97.  * three color planes. The maximum supported resolution for such an arrangement
  98.  * is 300 dpi.
  99.  *
  100.  * The CLJ does support raster scaling, but to invoke that scaling, even for
  101.  * integral factors, involves a large performance penalty. Hence, only those
  102.  * resolutions that can be supported without invoking raster scaling are
  103.  * included here. These resolutions are always the same in the fast and slow
  104.  * scan directions, so only a single value is listed here.
  105.  *
  106.  * All valuse are in dots per inch.
  107.  */
  108. private const float supported_resolutions[] = { 75.0, 100.0, 150.0, 300.0 };
  109.  
  110.  
  111. /* indicate the maximum supported resolution and scan-line length (pts) */
  112. #define CLJ_MAX_RES        300.0
  113. #define CLJ_MAX_SCANLINE   (12.0 * 72.0)
  114.  
  115.  
  116. /*
  117.  * Determine a requested resolution pair is supported.
  118.  */
  119.   private bool
  120. is_supported_resolution(
  121.     const float HWResolution[2]
  122. )
  123. {
  124.     int     i;
  125.  
  126.     for (i = 0; i < countof(supported_resolutions); i++) {
  127.         if (HWResolution[0] == supported_resolutions[i])
  128.             return HWResolution[0] == HWResolution[1];
  129.     }
  130.     return false;
  131. }
  132.  
  133. /* ---------------- Standard driver ---------------- */
  134.  
  135. /*
  136.  * Find the paper size information corresponding to a given pair of dimensions.
  137.  * If rotatep != 0, *rotatep is set to true if the page must be rotated 90
  138.  * degrees to fit.
  139.  *
  140.  * A return value of 0 indicates the paper size is not supported.
  141.  *
  142.  * Note that for the standard driver, rotation is not allowed.
  143.  */
  144.   private const clj_paper_size *
  145. get_paper_size(
  146.     const float             MediaSize[2],
  147.     bool *                  rotatep
  148. )
  149. {
  150.     static const float      tolerance = 5.0;
  151.     float                   width = MediaSize[0];
  152.     float                   height = MediaSize[1];
  153.     const clj_paper_size *  psize = 0;
  154.     int                     i;
  155.  
  156.     for (i = 0, psize = clj_paper_sizes; i < countof(clj_paper_sizes); i++, psize++) {
  157.         if ( (fabs(width - psize->width) <= tolerance)  &&
  158.              (fabs(height - psize->height) <= tolerance)  ) {
  159.             if (rotatep != 0)
  160.                 *rotatep = false;
  161.             return psize;
  162.         } else if ( (fabs(width - psize->height) <= tolerance) &&
  163.                     (fabs(height - psize->width) <= tolerance)   ) {
  164.             if (rotatep != 0)
  165.                 *rotatep = true;
  166.             return psize;
  167.         }
  168.     }
  169.  
  170.     return 0;
  171. }
  172.  
  173. /*
  174.  * Get the (PostScript style) default matrix for the current page size.
  175.  *
  176.  * For all of the supported sizes, the page will be printed with long-edge
  177.  * feed (the CLJ does support some additional sizes, but only for monochrome).
  178.  * As will all HP laser printers, the printable region marin is 12 pts. from
  179.  * the edge of the physical page.
  180.  */
  181. private void
  182. clj_get_initial_matrix( gx_device *pdev, gs_matrix *pmat)
  183. {
  184.     floatp          fs_res = pdev->HWResolution[0] / 72.0;
  185.     floatp          ss_res = pdev->HWResolution[1] / 72.0;
  186.     const clj_paper_size *psize;
  187.  
  188.     psize = get_paper_size(pdev->MediaSize, NULL);
  189.     /* if the paper size is not recognized, not much can be done */
  190.     /* This shouldn't be possible since clj_put_params rejects   */
  191.     /* unknown media sizes.                     */
  192.     if (psize == 0) {
  193.     pmat->xx = fs_res;
  194.     pmat->xy = 0.0;
  195.     pmat->yx = 0.0;
  196.     pmat->yy = -ss_res;
  197.     pmat->tx = 0.0;
  198.     pmat->ty = pdev->MediaSize[1] * ss_res;
  199.     return;
  200.     }
  201.   
  202.     if (pclj->rotated) {
  203.         pmat->xx = 0.0;
  204.         pmat->xy = ss_res;
  205.         pmat->yx = fs_res;
  206.         pmat->yy = 0.0;
  207.         pmat->tx = -psize->offsets.x * fs_res;
  208.         pmat->ty = -psize->offsets.y * ss_res;
  209.     } else {
  210.         pmat->xx = fs_res;
  211.         pmat->xy = 0.0;
  212.         pmat->yx = 0.0;
  213.         pmat->yy = -ss_res;
  214.         pmat->tx = -psize->offsets.x * fs_res;
  215.         pmat->ty = pdev->height + psize->offsets.y * ss_res;
  216.     }
  217. }
  218.  
  219. /*
  220.  * Get parameters, including InputAttributes for all supported page sizes.
  221.  * We associate each page size with a different "media source", since that
  222.  * is currently the only way to register multiple page sizes.
  223.  */
  224. private int
  225. clj_get_params(gx_device *pdev, gs_param_list *plist)
  226. {
  227.     gs_param_dict mdict;
  228.     int code = gdev_prn_get_params(pdev, plist);
  229.     int ecode = code;
  230.     int i;
  231.  
  232.     code = gdev_begin_input_media(plist, &mdict, countof(clj_paper_sizes));
  233.     if (code < 0)
  234.     ecode = code;
  235.     else {
  236.     for (i = 0; i < countof(clj_paper_sizes); ++i) {
  237.         code = gdev_write_input_page_size(i, &mdict,
  238.                           clj_paper_sizes[i].width,
  239.                           clj_paper_sizes[i].height);
  240.         if (code < 0)
  241.         ecode = code;
  242.     }
  243.     code = gdev_end_input_media(plist, &mdict);
  244.     if (code < 0)
  245.         ecode = code;
  246.     }
  247.     return ecode;
  248. }
  249.  
  250. /*
  251.  * Get the media size being set by put_params, if any.  Return 0 if no media
  252.  * size is being set, 1 (and set mediasize[]) if the size is being set, <0
  253.  * on error.
  254.  */
  255. private int
  256. clj_media_size(float mediasize[2], gs_param_list *plist)
  257. {
  258.     gs_param_float_array fres;
  259.     gs_param_float_array fsize;
  260.     gs_param_int_array hwsize;
  261.     int have_pagesize = 0;
  262.  
  263.     if ( (param_read_float_array(plist, "HWResolution", &fres) == 0) &&
  264.           !is_supported_resolution(fres.data) ) 
  265.         return_error(gs_error_rangecheck);
  266.  
  267.     if ( (param_read_float_array(plist, "PageSize", &fsize) == 0) ||
  268.          (param_read_float_array(plist, ".MediaSize", &fsize) == 0) ) {
  269.     mediasize[0] = fsize.data[0];
  270.     mediasize[1] = fsize.data[1];
  271.     have_pagesize = 1;
  272.     }
  273.  
  274.     if (param_read_int_array(plist, "HWSize", &hwsize) == 0) {
  275.         mediasize[0] = ((float)hwsize.data[0]) / fres.data[0];
  276.         mediasize[1] = ((float)hwsize.data[1]) / fres.data[1];
  277.     have_pagesize = 1;
  278.     }
  279.  
  280.     return have_pagesize;
  281. }
  282.  
  283. /*
  284.  * Special put_params routine, to make certain the desired MediaSize and
  285.  * HWResolution are supported.
  286.  */
  287.   private int
  288. clj_put_params(
  289.     gx_device *             pdev,
  290.     gs_param_list *         plist
  291. )
  292. {
  293.     float            mediasize[2];
  294.     bool                    rotate = false;
  295.     int                     have_pagesize = clj_media_size(mediasize, plist);
  296.  
  297.     if (have_pagesize < 0)
  298.     return have_pagesize;
  299.     if (have_pagesize) {
  300.     if (get_paper_size(mediasize, &rotate) == 0 || rotate)
  301.         return_error(gs_error_rangecheck);
  302.     }
  303.     return gdev_prn_put_params(pdev, plist);
  304. }
  305.  
  306. /*
  307.  * Pack and then compress a scanline of data. Return the size of the compressed
  308.  * data produced.
  309.  *
  310.  * Input is arranged with one byte per pixel, but only the three low-order bits
  311.  * are used. These bits are in order ymc, with yellow being the highest order
  312.  * bit.
  313.  *
  314.  * Output is arranged in three planes, with one bit per pixel per plane. The
  315.  * Color LaserJet 5/5M does support more congenial pixel encodings, but use
  316.  * of anything other than the fixed palettes seems to result in very poor
  317.  * performance.
  318.  *
  319.  * Only compresion mode 2 is used. Compression mode 1 (pure run length) has
  320.  * an advantage over compression mode 2 only in cases in which very long runs
  321.  * occur (> 128 bytes). Since both methods provide good compression in that
  322.  * case, it is not worth worrying about, and compression mode 2 provides much
  323.  * better worst-case behavior. Compression mode 3 requires considerably more
  324.  * effort to generate, so it is useful only when it is known a prior that
  325.  * scanlines repeat frequently.
  326.  */
  327.   private void
  328. pack_and_compress_scanline(
  329.     const byte *        pin,
  330.     int                 in_size,
  331.     byte  *             pout[3],
  332.     int                 out_size[3]
  333. )
  334. {
  335. #define BUFF_SIZE                                                           \
  336.     ( ((int)(CLJ_MAX_RES * CLJ_MAX_SCANLINE / 72.0) + sizeof(ulong) - 1)    \
  337.          / sizeof(ulong) )
  338.  
  339.     ulong               buff[3 * BUFF_SIZE];
  340.     byte *              p_c = (byte *)buff;
  341.     byte *              p_m = (byte *)(buff + BUFF_SIZE);
  342.     byte *              p_y = (byte *)(buff + 2 * BUFF_SIZE);
  343.     ulong *             ptrs[3];
  344.     byte                c_val = 0, m_val = 0, y_val = 0;
  345.     ulong               mask = 0x80;
  346.     int                 i;
  347.  
  348.     /* pack the input for 4-bits per index */
  349.     for (i = 0; i < in_size; i++) {
  350.         uint    ival = *pin++;
  351.  
  352.         if (ival != 0) {
  353.             if ((ival & 0x4) != 0)
  354.                 y_val |= mask;
  355.             if ((ival & 0x2) != 0)
  356.                 m_val |= mask;
  357.             if ((ival & 0x1) != 0)
  358.                 c_val |= mask;
  359.         }
  360.  
  361.         if ((mask >>= 1) == 0) {
  362.             /* NB - write out in byte units */
  363.             *p_c++ = c_val;
  364.             c_val = 0L;
  365.             *p_m++ = m_val;
  366.             m_val = 0L;
  367.             *p_y++ = y_val;
  368.             y_val = 0L;
  369.             mask = 0x80;
  370.         }
  371.     }
  372.     if (mask != 0x80) {
  373.         /* NB - write out in byte units */
  374.         *p_c++ = c_val;
  375.         *p_m++ = m_val;
  376.         *p_y++ = y_val;
  377.     }
  378.  
  379.     /* clear to up a longword boundary */
  380.     while ((((ulong)p_c) & (sizeof(ulong) - 1)) != 0) {
  381.         *p_c++ = 0;
  382.         *p_m++ = 0;
  383.         *p_y++ = 0;
  384.     }
  385.  
  386.     ptrs[0] = (ulong *)p_c;
  387.     ptrs[1] = (ulong *)p_m;
  388.     ptrs[2] = (ulong *)p_y;
  389.  
  390.     for (i = 0; i < 3; i++) {
  391.         ulong * p_start = buff + i * BUFF_SIZE;
  392.         ulong * p_end = ptrs[i];
  393.  
  394.         /* eleminate trailing 0's */
  395.         while ((p_end > p_start) && (p_end[-1] == 0))
  396.             p_end--;
  397.  
  398.         if (p_start == p_end)
  399.             out_size[i] = 0;
  400.         else
  401.             out_size[i] = gdev_pcl_mode2compress(p_start, p_end, pout[i]);
  402.     }
  403.  
  404. #undef BUFF_SIZE
  405. }
  406.  
  407. /*
  408.  * Send the page to the printer.  Compress each scan line.
  409.  */
  410.   private int
  411. clj_print_page(
  412.     gx_device_printer *     pdev,
  413.     FILE *                  prn_stream
  414. )
  415. {
  416.     gs_memory_t *mem = pdev->memory;
  417.     bool                    rotate;
  418.     const clj_paper_size *  psize = get_paper_size(pdev->MediaSize, &rotate);
  419.     int                     lsize = pdev->width;
  420.     int                     clsize = (lsize + (lsize + 255) / 128) / 8;
  421.     byte *                  data = 0;
  422.     byte *                  cdata[3];
  423.     int                     blank_lines = 0;
  424.     int                     i;
  425.     floatp                  fs_res = pdev->HWResolution[0] / 72.0;
  426.     floatp                  ss_res = pdev->HWResolution[1] / 72.0;
  427.     int                imageable_width, imageable_height;
  428.  
  429.     /* no paper size at this point is a serious error */
  430.     if (psize == 0)
  431.         return_error(gs_error_unregistered);
  432.  
  433.     /* allocate memory for the raw and compressed data */
  434.     if ((data = gs_alloc_bytes(mem, lsize, "clj_print_page(data)")) == 0)
  435.         return_error(gs_error_VMerror);
  436.     if ((cdata[0] = gs_alloc_bytes(mem, 3 * clsize, "clj_print_page(cdata)")) == 0) {
  437.         gs_free_object(mem, data, "clj_print_page(data)");
  438.         return_error(gs_error_VMerror);
  439.     }
  440.     cdata[1] = cdata[0] + clsize;
  441.     cdata[2] = cdata[1] + clsize;
  442.  
  443.  
  444.     /* Imageable area is without the margins. Note that the actual rotation
  445.      * of page size into pdev->width & height has been done. We just use
  446.      * rotate to access the correct offsets. */
  447.     if (pclj->rotated) {
  448.         imageable_width = pdev->width - (2 * psize->offsets.x) * fs_res;
  449.         imageable_height = pdev->height - (2 * psize->offsets.y) * ss_res;
  450.     }
  451.     else {
  452.         imageable_width = pdev->width - (2 * psize->offsets.y) * ss_res;
  453.         imageable_height = pdev->height - (2 * psize->offsets.x) * fs_res;
  454.     }
  455.  
  456.     /* start the page.  The pcl origin (0, 150 dots by default, y
  457.        increasing down the long edge side of the page) needs to be
  458.        offset such that it coincides with the offsets of the imageable
  459.        area.  This calculation should be independant of rotation but
  460.        only the rotated case has been tested with a real device. */
  461.     fprintf( prn_stream,
  462.              "\033E\033&u300D\033&l%da1x%dO\033*p0x0y+50x-100Y\033*t%dR"
  463. #ifdef USE_FAST_MODE
  464.          "\033*r-3U"
  465. #else
  466.              "\033*v6W\001\002\003\001\001\001"
  467. #endif
  468.              "\033*r0f%ds%dt1A\033*b2M",
  469.              psize->tag,
  470.              pclj->rotated,
  471.              (int)(pdev->HWResolution[0]),
  472.              imageable_width,
  473.              imageable_height
  474.              );
  475.  
  476.     /* process each scanline */
  477.     for (i = 0; i < imageable_height; i++) {
  478.         int     clen[3];
  479.  
  480.         gdev_prn_copy_scan_lines(pdev, i, data, lsize);
  481.  
  482.     /* The 'lsize' bytes of data have the blank margin area at the end due    */
  483.     /* to the 'initial_matrix' offsets that are applied.            */
  484.         pack_and_compress_scanline(data, imageable_width, cdata, clen);
  485.         if ((clen[0] == 0) && (clen[1] == 0) && (clen[2] == 0))
  486.             ++blank_lines;
  487.         else {
  488.             if (blank_lines != 0) {
  489.                 fprintf(prn_stream, "\033*b%dY", blank_lines);
  490.                 blank_lines = 0;
  491.             }
  492.             fprintf(prn_stream, "\033*b%dV", clen[0]);
  493.             fwrite(cdata[0], sizeof(byte), clen[0], prn_stream);
  494.             fprintf(prn_stream, "\033*b%dV", clen[1]);
  495.             fwrite(cdata[1], sizeof(byte), clen[1], prn_stream);
  496.             fprintf(prn_stream, "\033*b%dW", clen[2]);
  497.             fwrite(cdata[2], sizeof(byte), clen[2], prn_stream);
  498.         }
  499.     }
  500.  
  501.     /* PCL will take care of blank lines at the end */
  502.     fputs("\033*rC\f", prn_stream);
  503.  
  504.     /* free the buffers used */
  505.     gs_free_object(mem, cdata[0], "clj_print_page(cdata)");
  506.     gs_free_object(mem, data, "clj_print_page(data)");
  507.  
  508.     return 0;
  509. }
  510.  
  511. /* CLJ device methods */
  512. #define CLJ_PROCS(get_params, put_params)\
  513.     gdev_prn_open,                  /* open_device */\
  514.     clj_get_initial_matrix,         /* get_initial matrix */\
  515.     NULL,                        /* sync_output */\
  516.     gdev_prn_output_page,           /* output_page */\
  517.     gdev_prn_close,                 /* close_device */\
  518.     gdev_pcl_3bit_map_rgb_color,    /* map_rgb_color */\
  519.     gdev_pcl_3bit_map_color_rgb,    /* map_color_rgb */\
  520.     NULL,                        /* fill_rectangle */\
  521.     NULL,                        /* tile_rectangle */\
  522.     NULL,                        /* copy_mono */\
  523.     NULL,                        /* copy_color */\
  524.     NULL,                        /* obsolete draw_line */\
  525.     NULL,                        /* get_bits */\
  526.     get_params,                 /* get_params */\
  527.     put_params,                     /* put_params */\
  528.     NULL,                        /* map_cmyk_color */\
  529.     NULL,                        /* get_xfont_procs */\
  530.     NULL,                        /* get_xfont_device */\
  531.     NULL,                        /* map_rgb_alpha_color */\
  532.     gx_page_device_get_page_device  /* get_page_device */
  533.  
  534. private gx_device_procs cljet5_procs = {
  535.     CLJ_PROCS(clj_get_params, clj_put_params)
  536. };
  537.  
  538. /* CLJ device structure */
  539. #define CLJ_DEVICE_BODY(procs, dname, rotated)\
  540.   prn_device_body(\
  541.     gx_device_clj,\
  542.     procs,                  /* procedures */\
  543.     dname,                  /* device name */\
  544.     110,                    /* width - will be overridden subsequently */\
  545.     85,                     /* height - will be overridden subsequently */\
  546.     X_DPI, Y_DPI,           /* resolutions - current must be the same */\
  547.     0.167, 0.167,           /* margins (left, bottom, right, top */\
  548.     0.167, 0.167,\
  549.     3,                      /* num_components - 3 colors, 1 bit per pixel */\
  550.     8,                /* depth - pack into bytes */\
  551.     1, 1,             /* max_gray=max_component=1 */\
  552.     2, 2,            /* dithered_grays=dithered_components=2 */ \
  553.     clj_print_page          /* routine to output page */\
  554. ),\
  555.     rotated            /* rotated - may be overridden subsequently */
  556.  
  557. gx_device_clj gs_cljet5_device = {
  558.     CLJ_DEVICE_BODY(cljet5_procs, "cljet5", 0 /*false*/)
  559. };
  560.  
  561. /* ---------------- Driver with page rotation ---------------- */
  562.  
  563. /*
  564.  * For use with certain PCL interpreters, which don't implement
  565.  * setpagedevice, we provide a version of this driver that attempts to
  566.  * handle page rotation at the driver level.  This version breaks an
  567.  * invariant that all drivers must obey, namely, that drivers are not
  568.  * allowed to change the parameters passed by put_params (they can only
  569.  * accept or reject them).  Consequently, this driver must not be used in
  570.  * any context other than these specific PCL interpreters.  We support this
  571.  * hack only because these PCL interpreters can't be changed to handle page
  572.  * rotation properly.
  573.  */
  574.  
  575. /*
  576.  * Special get_params routine, to fake MediaSize, width, and height if
  577.  * we were in a 'rotated' state.
  578.  */
  579. private int
  580. clj_pr_get_params( gx_device *pdev, gs_param_list *plist )
  581. {
  582.     int code;
  583.  
  584.     /* First un-rotate the MediaSize, etc. if we were in a rotated mode        */
  585.     if (pclj->rotated) {
  586.         float ftmp;
  587.     int   itmp;
  588.  
  589.     ftmp = pdev->MediaSize[0];
  590.     pdev->MediaSize[0] = pdev->MediaSize[1];
  591.     pdev->MediaSize[1] = ftmp;
  592.     itmp = pdev->width;
  593.     pdev->width = pdev->height;
  594.     pdev->height = itmp;
  595.     }
  596.  
  597.     /* process the parameter list */
  598.     code = gdev_prn_get_params(pdev, plist);
  599.  
  600.     /* Now re-rotate the page size if needed */
  601.     if (pclj->rotated) {
  602.         float ftmp;
  603.     int   itmp;
  604.  
  605.     ftmp = pdev->MediaSize[0];
  606.     pdev->MediaSize[0] = pdev->MediaSize[1];
  607.     pdev->MediaSize[1] = ftmp;
  608.     itmp = pdev->width;
  609.     pdev->width = pdev->height;
  610.     pdev->height = itmp;
  611.     }
  612.  
  613.     return code;
  614. }
  615.  
  616. /*
  617.  * Special put_params routine, to intercept changes in the MediaSize, and to
  618.  * make certain the desired MediaSize and HWResolution are supported.
  619.  *
  620.  * This function will rotate MediaSize if it is needed by the device in
  621.  * order to print this size page.
  622.  */
  623.   private int
  624. clj_pr_put_params(
  625.     gx_device *             pdev,
  626.     gs_param_list *         plist
  627. )
  628. {
  629.     float            mediasize[2];
  630.     int                     code = 0;
  631.     bool                    rotate = false;
  632.     int                     have_pagesize = clj_media_size(mediasize, plist);
  633.  
  634.     if (have_pagesize < 0)
  635.     return have_pagesize;
  636.     if (have_pagesize) {
  637.     if (get_paper_size(mediasize, &rotate) == 0)
  638.         return_error(gs_error_rangecheck);
  639.     if (rotate) {
  640.         /* We need to rotate the requested page size, so synthesize a new    */
  641.         /* parameter list in front of the requestor's list to force the    */
  642.         /* rotated page size.                        */
  643.         gs_param_float_array    pf_array;
  644.         gs_c_param_list        alist;
  645.         float            ftmp = mediasize[0];
  646.  
  647.         mediasize[0] = mediasize[1];
  648.         mediasize[1] = ftmp;
  649.         pf_array.data = mediasize;
  650.         pf_array.size = 2;
  651.         pf_array.persistent = false;
  652.  
  653.         gs_c_param_list_write(&alist, pdev->memory);
  654.         code = param_write_float_array((gs_param_list *)&alist, ".MediaSize", &pf_array);
  655.         gs_c_param_list_read(&alist);
  656.  
  657.         /* stick this synthesized parameter on the front of the existing list */
  658.         gs_c_param_list_set_target(&alist, plist);
  659.         if ((code = gdev_prn_put_params(pdev, (gs_param_list *)&alist)) >= 0)
  660.         pclj->rotated = true;
  661.         gs_c_param_list_release(&alist);
  662.     } else {
  663.         if ((code = gdev_prn_put_params(pdev, plist)) >= 0)
  664.         pclj->rotated = false;
  665.     }
  666.     } else 
  667.     code = gdev_prn_put_params(pdev, plist);
  668.  
  669.     return code;
  670. }
  671.  
  672. /* CLJ device methods -- se above for CLJ_PROCS */
  673. private gx_device_procs cljet5pr_procs = {
  674.     CLJ_PROCS(clj_pr_get_params, clj_pr_put_params)
  675. };
  676.  
  677. /* CLJ device structure -- see above for CLJ_DEVICE_BODY */
  678. gx_device_clj gs_cljet5pr_device = {
  679.     CLJ_DEVICE_BODY(cljet5pr_procs, "cljet5pr", 1 /*true*/)
  680. };
  681.