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 / GDEVPRN.C < prev    next >
C/C++ Source or Header  |  1992-09-17  |  12KB  |  383 lines

  1. /* Copyright (C) 1990, 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. /* gdevprn.c */
  21. /* Generic printer support for Ghostscript */
  22. #include "gdevprn.h"
  23. #include "gp.h"
  24. #include "gsprops.h"
  25.  
  26. /* Define the scratch file name prefix for mktemp */
  27. #define SCRATCH_TEMPLATE gp_scratch_file_name_prefix
  28.  
  29. /* Internal routine for opening a scratch file */
  30. private int
  31. open_scratch(char *fname, FILE **pfile)
  32. {    char fmode[4];
  33.     strcpy(fmode, "w+");
  34.     strcat(fmode, gp_fmode_binary_suffix);
  35.     *pfile = gp_open_scratch_file(SCRATCH_TEMPLATE, fname, fmode);
  36.     if ( *pfile == NULL )
  37.        {    eprintf1("Could not open the scratch file %s.\n", fname);
  38.         return_error(gs_error_invalidfileaccess);
  39.        }
  40.     return 0;
  41. }
  42.  
  43. /* ------ Standard device procedures ------ */
  44.  
  45. /* Macros for casting the pdev argument */
  46. #define ppdev ((gx_device_printer *)pdev)
  47. #define pmemdev ((gx_device_memory *)pdev)
  48. #define pcldev ((gx_device_clist *)pdev)
  49.  
  50. gx_device_procs prn_std_procs =
  51.   prn_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close);
  52.  
  53. /* Generic initialization for the printer device. */
  54. /* Specific devices may wish to extend this. */
  55. int
  56. gdev_prn_open(gx_device *pdev)
  57. {    const gx_device_memory *mdev =
  58.       gdev_mem_device_for_bits(pdev->color_info.depth);
  59.     gx_device_procs *pprocs = pdev->procs;
  60.     ulong mem_space;
  61.     byte *base = 0;
  62.     char *left = 0;
  63.     if ( mdev == 0 )
  64.         return_error(gs_error_rangecheck);
  65.     memset(ppdev->skip, 0, sizeof(ppdev->skip));
  66.     ppdev->orig_procs = pprocs;
  67.     ppdev->page_count = 0;
  68.     ppdev->file = ppdev->ccfile = ppdev->cbfile = NULL;
  69.     mem_space = gdev_mem_bitmap_size(pmemdev);
  70.     if ( mem_space >= ppdev->max_bitmap ||
  71.          mem_space != (uint)mem_space ||    /* too big to allocate */
  72.          (base = (byte *)gs_malloc((uint)mem_space, 1, "printer buffer(open)")) == 0 ||    /* can't allocate */
  73.          (PRN_MIN_MEMORY_LEFT != 0 &&
  74.           (left = gs_malloc(PRN_MIN_MEMORY_LEFT, 1, "printer memory left")) == 0)    /* not enough left */
  75.        )
  76.     {    /* Buffer the image in a command list. */
  77.         uint space;
  78.         int code;
  79.         /* Release the buffer if we allocated it. */
  80.         if ( base != 0 )
  81.             gs_free((char *)base, (uint)mem_space, 1,
  82.                 "printer buffer(open)");
  83.         for ( space = ppdev->use_buffer_space; ; )
  84.         {    base = (byte *)gs_malloc(space, 1,
  85.                          "command list buffer(open)");
  86.             if ( base != 0 ) break;
  87.             if ( (space >>= 1) < PRN_MIN_BUFFER_SPACE )
  88.                 return_error(gs_error_VMerror);    /* no hope */
  89.         }
  90. open_c:        pcldev->data = base;
  91.         pcldev->data_size = space;
  92.         pcldev->target = pdev;
  93.         pcldev->mdev = *mdev;
  94.         ppdev->buf = base;
  95.         ppdev->buffer_space = space;
  96.         /* Try opening the command list, to see if we allocated */
  97.         /* enough buffer space. */
  98.         code = (*gs_clist_device_procs.open_device)((gx_device *)pcldev);
  99.         if ( code < 0 )
  100.         {    /* If there wasn't enough room, and we haven't */
  101.             /* already shrunk the buffer, try enlarging it. */
  102.             if ( code == gs_error_limitcheck &&
  103.                  space >= ppdev->use_buffer_space
  104.                )
  105.             {    gs_free((char *)base, space, 1,
  106.                     "command list buffer(retry open)");
  107.                 space <<= 1;
  108.                 base = (byte *)gs_malloc(space, 1,
  109.                          "command list buffer(retry open)");
  110.                 ppdev->buf = base;
  111.                 if ( base != 0 ) goto open_c;
  112.             }
  113.             return code;
  114.         }
  115.         if ( (code = open_scratch(ppdev->ccfname, &ppdev->ccfile)) < 0 )
  116.             return code;
  117.         if ( (code = open_scratch(ppdev->cbfname, &ppdev->cbfile)) < 0 )
  118.             return code;
  119.         pcldev->cfile = ppdev->ccfile;
  120.         pcldev->bfile = ppdev->cbfile;
  121.         pcldev->bfile_end_pos = 0;
  122.         ppdev->mod_procs = gs_clist_device_procs;
  123.     }
  124.     else
  125.     {    /* Render entirely in memory. */
  126.         /* Release the leftover memory. */
  127.         gs_free(left, PRN_MIN_MEMORY_LEFT, 1,
  128.             "printer memory left");
  129.         ppdev->buffer_space = 0;
  130.         ppdev->ccfile = NULL;
  131.         ppdev->cbfile = NULL;
  132.         pmemdev->base = base;
  133.         ppdev->mod_procs = *mdev->procs;
  134.     }
  135.     /* Synthesize the procedure vector. */
  136.     /* Rendering operations come from the memory or clist device, */
  137.     /* non-rendering come from the printer device. */
  138.     pdev->procs = &ppdev->mod_procs;
  139. #define copy_proc(p) ppdev->mod_procs.p = pprocs->p
  140.     copy_proc(get_initial_matrix);
  141.     copy_proc(output_page);
  142.     copy_proc(close_device);
  143.     copy_proc(map_rgb_color);
  144.     copy_proc(map_color_rgb);
  145.     copy_proc(get_props);
  146.     copy_proc(put_props);
  147. #undef copy_proc
  148.     return (*pdev->procs->open_device)(pdev);
  149. }
  150.  
  151. /* Added properties for printers */
  152.  
  153. private const gs_prop_item props_prn[] = {
  154.         /* Read-write properties. */
  155.     prop_def("BufferSpace", prt_int),
  156.     prop_def("MaxBitmap", prt_int),
  157.     prop_def("OutputFile", prt_string),
  158.         /* Read-only properties. */
  159.     prop_def("PageCount", prt_int)
  160. };
  161.  
  162. /* Get properties.  In addition to the standard properties, */
  163. /* we supply the max bitmap size, buffer size, and output name. */
  164. int
  165. gdev_prn_get_props(gx_device *pdev, gs_prop_item *plist)
  166. {    int start = gx_default_get_props(pdev, plist);
  167.     if ( plist != 0 )
  168.        {    register gs_prop_item *pi = plist + start;
  169.         memcpy(pi, props_prn, sizeof(props_prn));
  170.         pi[0].value.i = ppdev->use_buffer_space;
  171.         pi[1].value.i = ppdev->max_bitmap;
  172.         pi[2].value.a.p.s = ppdev->fname;
  173.         pi[2].value.a.size = -1;
  174.         pi[3].value.i = ppdev->page_count;
  175.        }
  176.     return start + sizeof(props_prn) / sizeof(gs_prop_item);
  177. }
  178.  
  179. /* Put properties. */
  180. /* Note that setting the buffer sizes closes the device. */
  181. int
  182. gdev_prn_put_props(gx_device *pdev, gs_prop_item *plist, int count)
  183. {    gs_prop_item *known[3];
  184.     int code = 0;
  185.     props_extract(plist, count, props_prn, 3, known, 0);
  186.     code = gx_default_put_props(pdev, plist, count);
  187.     if ( code < 0 ) return code;
  188.     if ( known[0] != 0 )
  189.        {    gs_prop_item *pi = known[0];
  190.         if ( pi->value.i < 10000 )
  191.             pi->status = pv_rangecheck,
  192.             code = gs_error_rangecheck;
  193.         else
  194.         {    ppdev->use_buffer_space = known[0]->value.i;
  195.             if ( code == 0 ) code = 1;
  196.         }
  197.        }
  198.     if ( known[1] != 0 )
  199.         ppdev->max_bitmap = known[1]->value.i;
  200.     if ( known[2] != 0 )
  201.        {    gs_prop_item *pn = known[2];
  202.         int size = pn->value.a.size;
  203.         if ( size >= sizeof(ppdev->fname) )
  204.             pn->status = pv_limitcheck,
  205.             code = gs_error_limitcheck;
  206.         else
  207.            {    /* Close the file if it's open. */
  208.             if ( ppdev->file != NULL && ppdev->file != stdout )
  209.                 gp_close_printer(ppdev->file, ppdev->fname);
  210.             ppdev->file = NULL;
  211.             memcpy(ppdev->fname, pn->value.a.p.s, size);
  212.             ppdev->fname[size] = 0;
  213.             if ( code == 0 ) code = 1;
  214.            }
  215.        }
  216.     if ( code < 0 )
  217.         return_error(code);
  218.     /* If we're changing the buffer sizes, close the device; */
  219.     /* gs_putdeviceprops will reopen it. */
  220.     if ( pdev->is_open && code )
  221.     {    int ccode = gs_closedevice(pdev);
  222.         if ( ccode < 0 ) return ccode;
  223.     }
  224.     return code;
  225. }
  226.  
  227. /* Generic routine to send the page to the printer. */
  228. /****** Note that num_copies is currently ignored: this is wrong. ******/
  229. int
  230. gdev_prn_output_page(gx_device *pdev, int num_copies, int flush)
  231. {    int code;
  232.  
  233.     ppdev->page_count++;
  234.     code = gdev_prn_open_printer(pdev);
  235.     if ( code < 0 ) return code;
  236.  
  237.     /* print the accumulated page description */
  238.     code = (*ppdev->print_page)(ppdev, ppdev->file);
  239.     if ( code < 0 ) return code;
  240.  
  241.     code = gdev_prn_close_printer(pdev);
  242.     if ( code < 0 ) return code;
  243.  
  244.     if ( ppdev->buffer_space ) /* reinitialize clist for writing */
  245.       code = (*gs_clist_device_procs.output_page)(pdev, num_copies, flush);
  246.  
  247.     return code;
  248. }
  249.  
  250. /* Generic closing for the printer device. */
  251. /* Specific devices may wish to extend this. */
  252. int
  253. gdev_prn_close(gx_device *pdev)
  254. {    if ( ppdev->ccfile != NULL )
  255.     {    fclose(ppdev->ccfile);
  256.         fclose(ppdev->cbfile);
  257.         ppdev->ccfile = NULL;
  258.         unlink(ppdev->ccfname);
  259.         unlink(ppdev->cbfname);
  260.     }
  261.     if ( ppdev->buffer_space != 0 )
  262.     {    /* Free the buffer */
  263.         gs_free((char *)ppdev->buf, (uint)ppdev->buffer_space, 1,
  264.             "command list buffer(close)");
  265.     }
  266.     else
  267.     {    /* Free the memory device bitmap */
  268.         gs_free((char *)pmemdev->base,
  269.             (uint)gdev_mem_bitmap_size(pmemdev),
  270.             1, "printer buffer(close)");
  271.     }
  272.     if ( ppdev->file != NULL )
  273.     {    if ( ppdev->file != stdout )
  274.            gp_close_printer(ppdev->file, ppdev->fname);
  275.         ppdev->file = NULL;
  276.     }
  277.     pdev->procs = ppdev->orig_procs;
  278.     return 0;
  279. }
  280.  
  281. /* Map colors.  This is different from the default, because */
  282. /* printers write black, not white. */
  283.  
  284. gx_color_index
  285. gdev_prn_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  286.   gx_color_value b)
  287. {    /* Map values >= 1/2 to 0, < 1/2 to 1. */
  288.     return ((r | g | b) > gx_max_color_value / 2 ?
  289.         (gx_color_index)0 : (gx_color_index)1);
  290. }
  291.  
  292. int
  293. gdev_prn_map_color_rgb(gx_device *dev, gx_color_index color,
  294.   gx_color_value prgb[3])
  295. {    /* Map 0 to max_value, 1 to 0. */
  296.     prgb[0] = prgb[1] = prgb[2] = -((gx_color_value)color ^ 1);
  297.     return 0;
  298. }
  299.  
  300. /* ------ Driver services ------ */
  301.  
  302. /* Open the current page for printing. */
  303. int
  304. gdev_prn_open_printer(gx_device *pdev)
  305. {    char *fname = ppdev->fname;
  306.     char pfname[sizeof(ppdev->fname) + 10];
  307.     if ( strchr(fname, '%') )
  308.        {    sprintf(pfname, fname, ppdev->page_count);
  309.         fname = pfname;
  310.        }
  311.     if ( ppdev->file == NULL )
  312.        {    if ( !strcmp(fname, "-") )
  313.            ppdev->file = stdout;
  314.         else
  315.          { ppdev->file = gp_open_printer(fname);
  316.            if ( ppdev->file == NULL )
  317.                return_error(gs_error_invalidfileaccess);
  318.          }
  319.        }
  320.     return 0;
  321. }
  322.  
  323. /* Get the size of a scan line for copying. */
  324. uint
  325. gdev_prn_raster(gx_device_printer *pdev)
  326. {    return gx_device_raster((gx_device *)pdev, 0);
  327. }
  328.  
  329. /* Copy scan lines from the buffer to the printer. */
  330. int
  331. gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str, uint size, int pad)
  332. {    int swap = (*pdev->procs->get_bits)((gx_device *)pdev, y, str, size, pad);
  333.     uint line_size = gdev_prn_raster(pdev);
  334.     /* If monobit, trim off trailing garbage. */
  335.     if ( pad )
  336.        {    int extra_bits = -(pdev->width * pdev->color_info.depth) & 31;
  337.         if ( extra_bits )
  338.            {    ulong last_mask = 0xffffffffL << extra_bits;
  339.             byte *line_end = str + line_size - 4;
  340.             int n;
  341.             if ( swap )    /* swap the mask */
  342.               memswab4((byte *)&last_mask, (byte *)&last_mask,
  343.                    sizeof(last_mask));
  344.             for ( n = size; n >= line_size;
  345.                   n -= line_size, line_end += line_size
  346.                 )
  347.                 *(ulong *)line_end &= last_mask;
  348.            }
  349.        }
  350.     else                /* know !swap */
  351.        {    byte last_mask =
  352.           0xff << (-(pdev->width * pdev->color_info.depth) & 7);
  353.         if ( last_mask != 0xff )
  354.            {    byte *line_end = str + line_size - 1;
  355.             int n;
  356.             for ( n = size; n >= line_size;
  357.                   n -= line_size, line_end += line_size
  358.                 )
  359.                 *line_end &= last_mask;
  360.            }
  361.        }
  362.     return swap;
  363. }
  364. /* Copy scan lines to a buffer.  Return the number of scan lines, */
  365. /* or <0 if error. */
  366. int
  367. gdev_prn_copy_scan_lines(gx_device_printer *pdev, int y, byte *str, uint size)
  368. {    int code;
  369.     code = gdev_prn_get_bits(pdev, y, str, size, 0);
  370.     if ( code < 0 ) return code;
  371.     return size / gdev_prn_raster(pdev);
  372. }
  373.  
  374. /* Close the current page. */
  375. int
  376. gdev_prn_close_printer(gx_device *pdev)
  377. {    if ( strchr(ppdev->fname, '%') )    /* file per page */
  378.        {    gp_close_printer(ppdev->file, ppdev->fname);
  379.         ppdev->file = NULL;
  380.        }
  381.     return 0;
  382. }
  383.