home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / gdevprn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-18  |  9.0 KB  |  304 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. {    *pfile = gp_open_scratch_file(SCRATCH_TEMPLATE, fname, "w+b");
  33.     if ( *pfile == NULL )
  34.        {    eprintf1("could not open the scratch file %s.\n", fname);
  35.         return -1;
  36.        }
  37.     return 0;
  38. }
  39.  
  40. /* ------ Standard device procedures ------ */
  41.  
  42. /* Macros for casting the pdev argument */
  43. #define ppdev ((gx_device_printer *)pdev)
  44. #define pmemdev ((gx_device_memory *)pdev)
  45. #define pcldev ((gx_device_clist *)pdev)
  46.  
  47. gx_device_procs prn_std_procs =
  48.   prn_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close);
  49.  
  50. /* Generic initialization for the printer device. */
  51. /* Specific devices may wish to extend this. */
  52. int
  53. gdev_prn_open(gx_device *pdev)
  54. {    gx_device_memory *mdev =
  55.       gdev_mem_device_for_bits(pdev->color_info.depth);
  56.     ulong mem_space;
  57.     byte *base;
  58.     if ( mdev == 0 ) return -1;
  59.     memset(ppdev->skip, 0, sizeof(ppdev->skip));
  60.     mem_space = gdev_mem_bitmap_size(pmemdev);
  61.     if ( mem_space >= ppdev->max_bitmap ||
  62.          mem_space != (uint)mem_space ||    /* too big to allocate */
  63.          (base = (byte *)gs_malloc((uint)mem_space, 1, "printer buffer")) == 0    /* can't allocate */
  64.        )
  65.        {    /* Buffer the image in a command list. */
  66.         uint space = ppdev->buffer_space = ppdev->use_buffer_space;
  67.         base = (byte *)gs_malloc(space, 1, "command list buffer");
  68.         if ( base == 0 ) return -1;
  69.         pcldev->mdev = *mdev;
  70.         ppdev->buf = base;
  71.         pcldev->data = base;
  72.         pcldev->data_size = space;
  73.         if ( open_scratch(ppdev->ccfname, &ppdev->ccfile) < 0 )
  74.             return -1;
  75.         if ( open_scratch(ppdev->cbfname, &ppdev->cbfile) < 0 )
  76.             return -1;
  77.         pcldev->target = pdev;
  78.         pcldev->cfile = ppdev->ccfile;
  79.         pcldev->bfile = ppdev->cbfile;
  80.         pcldev->bfile_end_pos = 0;
  81.         ppdev->mod_procs = *gs_clist_device.procs;
  82.        }
  83.     else
  84.        {    /* Render entirely in memory. */
  85.         ppdev->buffer_space = 0;
  86.         ppdev->ccfile = NULL;
  87.         ppdev->cbfile = NULL;
  88.         pmemdev->base = base;
  89.         ppdev->mod_procs = *mdev->procs;
  90.        }
  91.     /* Synthesize the procedure vector. */
  92.     /* Rendering operations come from the memory or clist device, */
  93.     /* non-rendering come from the printer device. */
  94.     { gx_device_procs *pprocs = pdev->procs;
  95.       ppdev->orig_procs = pprocs;
  96.       pdev->procs = &ppdev->mod_procs;
  97. #define copy_proc(p) ppdev->mod_procs.p = pprocs->p
  98.       copy_proc(get_initial_matrix);
  99.       copy_proc(output_page);
  100.       copy_proc(close_device);
  101.       copy_proc(map_rgb_color);
  102.       copy_proc(map_color_rgb);
  103.       copy_proc(get_props);
  104.       copy_proc(put_props);
  105. #undef copy_proc
  106.     }
  107.     ppdev->page_count = 0;
  108.     ppdev->file = 0;
  109.     return (*pdev->procs->open_device)(pdev);
  110. }
  111.  
  112. /* Added properties for printers */
  113.  
  114. private gs_prop_item props_prn[] = {
  115.     prop_def("BufferSpace", prt_int),
  116.     prop_def("MaxBitmap", prt_int),
  117.     prop_def("OutputFile", prt_string)
  118. };
  119.  
  120. /* Get properties.  In addition to the standard properties, */
  121. /* we supply the max bitmap size, buffer size, and output name. */
  122. int
  123. gdev_prn_get_props(gx_device *pdev, gs_prop_item *plist)
  124. {    int start = gx_default_get_props(pdev, plist);
  125.     if ( plist != 0 )
  126.        {    register gs_prop_item *pi = plist + start;
  127.         memcpy(pi, props_prn, sizeof(props_prn));
  128.         pi[0].value.i = ppdev->use_buffer_space;
  129.         pi[1].value.i = ppdev->max_bitmap;
  130.         pi[2].value.a.p.s = ppdev->fname;
  131.         pi[2].value.a.size = -1;
  132.        }
  133.     return start + sizeof(props_prn) / sizeof(gs_prop_item);
  134. }
  135.  
  136. /* Put properties. */
  137. int
  138. gdev_prn_put_props(gx_device *pdev, gs_prop_item *plist, int count)
  139. {    gs_prop_item *known[3];
  140.     props_extract(plist, count, props_prn, 3, known, 0);
  141.     gx_default_put_props(pdev, plist, count);
  142.     if ( known[0] != 0 )
  143.        {    gs_prop_item *pi = known[0];
  144.         if ( pi->value.i < 10000 )
  145.             pi->status = pv_rangecheck;
  146.         else
  147.             ppdev->use_buffer_space = known[0]->value.i;
  148.        }
  149.     if ( known[1] != 0 )
  150.         ppdev->max_bitmap = known[1]->value.i;
  151.     if ( known[2] != 0 )
  152.        {    gs_prop_item *pn = known[2];
  153.         int size = pn->value.a.size;
  154.         if ( size >= sizeof(ppdev->fname) )
  155.             pn->status = pv_limitcheck;
  156.         else
  157.            {    memcpy(ppdev->fname, pn->value.a.p.s, size);
  158.             pn->value.a.p.s[size] = 0;
  159.            }
  160.        }
  161.     return 0;
  162. }
  163.  
  164. /* Generic routine to send the page to the printer. */
  165. /****** Note that num_copies is currently ignored: this is wrong. ******/
  166. int
  167. gdev_prn_output_page(gx_device *pdev, int num_copies, int flush)
  168. {    int code;
  169.  
  170.     ppdev->page_count++;
  171.     code = gdev_prn_open_printer(pdev);
  172.     if ( code < 0 ) return code;
  173.  
  174.     /* print the accumulated page description */
  175.     code = (*ppdev->print_page)(ppdev, ppdev->file);
  176.     if ( code < 0 ) return code;
  177.  
  178.     code = gdev_prn_close_printer(pdev);
  179.     if ( code < 0 ) return code;
  180.  
  181.     if ( ppdev->buffer_space ) /* reinitialize clist for writing */
  182.       code = (*gs_clist_device.procs->output_page)(pdev, num_copies, flush);
  183.  
  184.     return code;
  185. }
  186.  
  187. /* Generic closing for the printer device. */
  188. /* Specific devices may wish to extend this. */
  189. int
  190. gdev_prn_close(gx_device *pdev)
  191. {    if ( ppdev->ccfile != NULL )
  192.        {    fclose(ppdev->ccfile);
  193.         fclose(ppdev->cbfile);
  194.         ppdev->ccfile = NULL;
  195.         unlink(ppdev->ccfname);
  196.         unlink(ppdev->cbfname);
  197.         gs_free((char *)ppdev->buf, (uint)ppdev->buffer_space, 1,
  198.             "command list buffer");
  199.        }
  200.     if ( ppdev->file != NULL )
  201.        {    gp_close_printer(ppdev->file, ppdev->fname);
  202.         ppdev->file = NULL;
  203.        }
  204.     pdev->procs = ppdev->orig_procs;
  205.     return 0;
  206. }
  207.  
  208. /* Map colors.  This is different from the default, because */
  209. /* printers write black, not white. */
  210.  
  211. gx_color_index
  212. gdev_prn_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  213.   gx_color_value b)
  214. {    /* Map values >= 1/2 to 0, < 1/2 to 1. */
  215.     return ((r | g | b) > gx_max_color_value / 2 ?
  216.         (gx_color_index)0 : (gx_color_index)1);
  217. }
  218.  
  219. int
  220. gdev_prn_map_color_rgb(gx_device *dev, gx_color_index color,
  221.   gx_color_value prgb[3])
  222. {    /* Map 0 to max_value, 1 to 0. */
  223.     prgb[0] = prgb[1] = prgb[2] = -((gx_color_value)color ^ 1);
  224.     return 0;
  225. }
  226.  
  227. /* ------ Driver services ------ */
  228.  
  229. /* Open the current page for printing. */
  230. int
  231. gdev_prn_open_printer(gx_device *pdev)
  232. {    char *fname = ppdev->fname;
  233.     char pfname[sizeof(ppdev->fname) + 10];
  234.     if ( strchr(fname, '%') )
  235.        {    sprintf(pfname, fname, ppdev->page_count);
  236.         fname = pfname;
  237.        }
  238.     if ( ppdev->file == NULL )
  239.        {    ppdev->file = gp_open_printer(fname);
  240.         if ( ppdev->file == NULL ) return -1;
  241.        }
  242.     return 0;
  243. }
  244.  
  245. /* Get the size of a scan line for copying. */
  246. uint
  247. gdev_prn_bytes_per_scan_line(gx_device_printer *pdev)
  248. {    return gx_device_bytes_per_scan_line((gx_device *)pdev, 0);
  249. }
  250.  
  251. /* Copy scan lines from the buffer to the printer. */
  252. int
  253. gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str, uint size, int pad)
  254. {    int swap = (*pdev->procs->get_bits)((gx_device *)pdev, y, str, size, pad);
  255.     uint line_size = gdev_prn_bytes_per_scan_line(pdev);
  256.     /* If monobit, trim off trailing garbage. */
  257.     if ( pad )
  258.        {    int extra_bits = -(pdev->width * pdev->color_info.depth) & 31;
  259.         if ( extra_bits )
  260.            {    ulong last_mask = 0xffffffffL << extra_bits;
  261.             byte *line_end = str + line_size - 4;
  262.             int n;
  263.             if ( swap )    /* swap the mask */
  264.               memswab(&last_mask, &last_mask, sizeof(last_mask));
  265.             for ( n = size; n >= line_size;
  266.                   n -= line_size, line_end += line_size
  267.                 )
  268.                 *(ulong *)line_end &= last_mask;
  269.            }
  270.        }
  271.     else                /* know !swap */
  272.        {    byte last_mask =
  273.           0xff << (-(pdev->width * pdev->color_info.depth) & 7);
  274.         if ( last_mask != 0xff )
  275.            {    byte *line_end = str + line_size - 1;
  276.             int n;
  277.             for ( n = size; n >= line_size;
  278.                   n -= line_size, line_end += line_size
  279.                 )
  280.                 *line_end &= last_mask;
  281.            }
  282.        }
  283.     return swap;
  284. }
  285. /* Copy scan lines to a buffer.  Return the number of scan lines, */
  286. /* or <0 if error. */
  287. int
  288. gdev_prn_copy_scan_lines(gx_device_printer *pdev, int y, byte *str, uint size)
  289. {    int code;
  290.     code = gdev_prn_get_bits(pdev, y, str, size, 0);
  291.     if ( code < 0 ) return code;
  292.     return size / gdev_prn_bytes_per_scan_line(pdev);
  293. }
  294.  
  295. /* Close the current page. */
  296. int
  297. gdev_prn_close_printer(gx_device *pdev)
  298. {    if ( strchr(ppdev->fname, '%') )    /* file per page */
  299.        {    gp_close_printer(ppdev->file, ppdev->fname);
  300.         ppdev->file = NULL;
  301.        }
  302.     return 0;
  303. }
  304.