home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSDEVICE.C < prev    next >
C/C++ Source or Header  |  1994-07-30  |  22KB  |  709 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsdevice.c */
  20. /* Device operators for Ghostscript library */
  21. #include "math_.h"            /* for fabs */
  22. #include "memory_.h"            /* for memcpy */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gsparam.h"
  27. #include "gspath.h"            /* gs_initclip prototype */
  28. #include "gspaint.h"            /* gs_erasepage prototype */
  29. #include "gsmatrix.h"            /* for gscoord.h */
  30. #include "gscoord.h"            /* for gs_initmatrix */
  31. #include "gxarith.h"
  32. #include "gzstate.h"
  33. #include "gxcmap.h"
  34. #include "gxdevmem.h"
  35.  
  36. /* Import the device list from gconfig.c */
  37. extern gx_device *gx_device_list[];
  38. extern uint gx_device_list_count;
  39.  
  40. /* Other imports */
  41. extern void gx_set_cmap_procs(P1(gs_state *));
  42.  
  43. /* Structure descriptors */
  44. public_st_device();
  45. public_st_device_forward();
  46. public_st_device_null();
  47.  
  48. /* GC utilities */
  49. /* Enumerate or relocate a device pointer for a client. */
  50. gx_device *
  51. gx_device_enum_ptr(gx_device *dev)
  52. {    if ( dev == 0 || dev->memory == 0 )
  53.       return 0;
  54.     return dev;
  55. }
  56. gx_device *
  57. gx_device_reloc_ptr(gx_device *dev, gc_state_t *gcst)
  58. {    if ( dev == 0 || dev->memory == 0 )
  59.       return dev;
  60.     return gs_reloc_struct_ptr(dev, gcst);
  61. }
  62. /* GC procedures */
  63. #define fdev ((gx_device_forward *)vptr)
  64. private ENUM_PTRS_BEGIN(device_forward_enum_ptrs) return 0;
  65.     case 0:
  66.       *pep = gx_device_enum_ptr(fdev->target);
  67.       break;
  68. ENUM_PTRS_END
  69. private RELOC_PTRS_BEGIN(device_forward_reloc_ptrs) {
  70.     fdev->target = gx_device_reloc_ptr(fdev->target, gcst);
  71. } RELOC_PTRS_END
  72. #undef fdev
  73.  
  74. /* The null device */
  75. private dev_proc_fill_rectangle(null_fill_rectangle);
  76. private dev_proc_copy_mono(null_copy_mono);
  77. private dev_proc_put_params(null_put_params);
  78.  
  79. private gx_device_null gs_null_device = {
  80.     sizeof(gx_device),
  81.     0,                /* &gs_null_device.std_procs */
  82.     "null",
  83.     0, 0,
  84.     72, 72,
  85.     no_margins,
  86.     dci_black_and_white,
  87.     dev_init_misc_open(true),
  88.     {    gx_default_open_device,
  89.         gx_forward_get_initial_matrix,
  90.         gx_default_sync_output,
  91.         gx_default_output_page,
  92.         gx_default_close_device,
  93.         gx_forward_map_rgb_color,
  94.         gx_forward_map_color_rgb,
  95.         null_fill_rectangle,
  96.         gx_default_tile_rectangle,
  97.         null_copy_mono,
  98.         gx_default_copy_color,
  99.         gx_default_draw_line,
  100.         gx_default_get_bits,
  101.         gx_forward_get_params,
  102.         null_put_params,
  103.         gx_forward_map_cmyk_color,
  104.         gx_forward_get_xfont_procs,
  105.         gx_forward_get_xfont_device,
  106.         gx_forward_map_rgb_alpha_color,
  107.         gx_default_get_page_device    /* not a page device */
  108.     },
  109.     0                /* target */
  110. };
  111.  
  112. /* Set up the device procedures in the device structure. */
  113. void
  114. gx_device_set_procs(register gx_device *dev)
  115. {    if ( dev->procs != &dev->std_procs )    /* BACKWARD COMPATIBILITY */
  116.     {    if ( dev->procs != 0 )        /* 0 if already populated */
  117.             dev->std_procs = *dev->procs;
  118.         dev->procs = &dev->std_procs;
  119.     }
  120. }
  121.  
  122. /* Initialize a device just after allocation. */
  123. int
  124. gdev_initialize(gx_device *dev)
  125. {    *dev = *(gx_device *)&gs_null_device;
  126.     dev->procs = &dev->std_procs;
  127.     return 0;
  128. }
  129.  
  130. /* Fill in NULL procedures in a device procedure record. */
  131. void
  132. gx_device_fill_in_procs(register gx_device *dev)
  133. {    gx_device_set_procs(dev);
  134.     fill_dev_proc(dev, open_device, gx_default_open_device);
  135.     fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
  136.     fill_dev_proc(dev, sync_output, gx_default_sync_output);
  137.     fill_dev_proc(dev, output_page, gx_default_output_page);
  138.     fill_dev_proc(dev, close_device, gx_default_close_device);
  139.     fill_dev_proc(dev, map_rgb_color, gx_default_map_rgb_color);
  140.     fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
  141.     /* NOT fill_rectangle */
  142.     fill_dev_proc(dev, tile_rectangle, gx_default_tile_rectangle);
  143.     /* NOT copy_mono */
  144.     fill_dev_proc(dev, copy_color, gx_default_copy_color);    /* Bogus? */
  145.     fill_dev_proc(dev, draw_line, gx_default_draw_line);
  146.     fill_dev_proc(dev, get_bits, gx_default_get_bits);
  147.     fill_dev_proc(dev, get_params, gx_default_get_params);
  148.     fill_dev_proc(dev, put_params, gx_default_put_params);
  149.     fill_dev_proc(dev, map_cmyk_color, gx_default_map_cmyk_color);
  150.     fill_dev_proc(dev, get_xfont_procs, gx_default_get_xfont_procs);
  151.     fill_dev_proc(dev, get_xfont_device, gx_default_get_xfont_device);
  152.     fill_dev_proc(dev, map_rgb_alpha_color, gx_default_map_rgb_alpha_color);
  153.     fill_dev_proc(dev, get_page_device, gx_default_get_page_device);
  154. }
  155. /* Fill in NULL procedures in a forwarding device procedure record. */
  156. void
  157. gx_device_forward_fill_in_procs(register gx_device_forward *dev)
  158. {    gx_device_set_procs((gx_device *)dev);
  159.     fill_dev_proc(dev, get_initial_matrix, gx_forward_get_initial_matrix);
  160.     fill_dev_proc(dev, map_rgb_color, gx_forward_map_rgb_color);
  161.     fill_dev_proc(dev, map_color_rgb, gx_forward_map_color_rgb);
  162.     fill_dev_proc(dev, get_params, gx_forward_get_params);
  163.     fill_dev_proc(dev, put_params, gx_forward_put_params);
  164.     fill_dev_proc(dev, map_cmyk_color, gx_forward_map_cmyk_color);
  165.     fill_dev_proc(dev, get_xfont_procs, gx_forward_get_xfont_procs);
  166.     fill_dev_proc(dev, get_xfont_device, gx_forward_get_xfont_device);
  167.     fill_dev_proc(dev, map_rgb_alpha_color, gx_forward_map_rgb_alpha_color);
  168.     fill_dev_proc(dev, get_page_device, gx_forward_get_page_device);
  169.     gx_device_fill_in_procs((gx_device *)dev);
  170. }
  171.  
  172. /* Flush buffered output to the device */
  173. int
  174. gs_flushpage(gs_state *pgs)
  175. {    gx_device *dev = gs_currentdevice(pgs);
  176.     return (*dev_proc(dev, sync_output))(dev);
  177. }
  178.  
  179. /* Make the device output the accumulated page description */
  180. int
  181. gs_copypage(gs_state *pgs)
  182. {    return gs_output_page(pgs, 1, 0);
  183. }
  184. int
  185. gs_output_page(gs_state *pgs, int num_copies, int flush)
  186. {    gx_device *dev = gs_currentdevice(pgs);
  187.     int code = (*dev_proc(dev, output_page))(dev, num_copies, flush);
  188.     if ( code >= 0 )
  189.     {    dev->page_count++;
  190.         if ( flush )
  191.             dev->showpage_count++;
  192.     }
  193.     return code;
  194. }
  195.  
  196. /* Copy scan lines from an image device */
  197. int
  198. gs_copyscanlines(gx_device *dev, int start_y, byte *data, uint size,
  199.   int *plines_copied, uint *pbytes_copied)
  200. {    uint line_size = gx_device_raster(dev, 0);
  201.     uint count = size / line_size;
  202.     uint i;
  203.     byte *dest = data;
  204.     for ( i = 0; i < count; i++, dest += line_size )
  205.     {    int code = (*dev_proc(dev, get_bits))(dev, start_y + i, dest, NULL);
  206.         if ( code < 0 )
  207.         {    /* Might just be an overrun. */
  208.             if ( start_y + i == dev->height ) break;
  209.             return_error(code);
  210.         }
  211.     }
  212.     if ( plines_copied != NULL )
  213.       *plines_copied = i;
  214.     if ( pbytes_copied != NULL )
  215.       *pbytes_copied = i * line_size;
  216.     return 0;
  217. }
  218.  
  219. /* Get the current device from the graphics state */
  220. gx_device *
  221. gs_currentdevice(const gs_state *pgs)
  222. {    return pgs->device;
  223. }
  224.  
  225. /* Get the name of a device */
  226. const char *
  227. gs_devicename(const gx_device *dev)
  228. {    return dev->dname;
  229. }
  230.  
  231. /* Get the initial matrix of a device. */
  232. void
  233. gs_deviceinitialmatrix(gx_device *dev, gs_matrix *pmat)
  234. {    fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
  235.     (*dev_proc(dev, get_initial_matrix))(dev, pmat);
  236. }
  237.  
  238. /* Get the N'th device from the known device list */
  239. gx_device *
  240. gs_getdevice(int index)
  241. {    if ( index < 0 || index >= gx_device_list_count )
  242.         return 0;        /* index out of range */
  243.     return gx_device_list[index];
  244. }
  245.  
  246. /* Clone an existing device. */
  247. int
  248. gs_copydevice(gx_device **pnew_dev, const gx_device *dev, gs_memory_t *mem)
  249. {    register gx_device *new_dev;
  250.     new_dev = (gx_device *)gs_alloc_bytes(mem, dev->params_size, "gs_copydevice");
  251.     if ( new_dev == 0 )
  252.         return_error(gs_error_VMerror);
  253.     memcpy(new_dev, dev, dev->params_size);
  254.     new_dev->procs = &new_dev->std_procs;
  255.     new_dev->memory = mem;
  256.     new_dev->is_open = false;
  257.     *pnew_dev = new_dev;
  258.     return 0;
  259. }
  260.  
  261. /* Make a memory (image) device. */
  262. /* If colors_size = -16, -24, or -32, this is a true-color device; */
  263. /* otherwise, colors_size is the size of the palette in bytes */
  264. /* (2^N for gray scale, 3*2^N for RGB color). */
  265. int
  266. gs_makeimagedevice(gx_device **pnew_dev, const gs_matrix *pmat,
  267.   uint width, uint height, const byte *colors, int colors_size,
  268.   gs_memory_t *mem)
  269. {    const gx_device_memory *old_dev;
  270.     register gx_device_memory *new_dev;
  271.     int palette_count = colors_size;
  272.     int num_components = 1;
  273.     int pcount;
  274.     int bits_per_pixel;
  275.     float x_pixels_per_unit, y_pixels_per_unit;
  276.     byte palette[256 * 3];
  277.     byte *dev_palette;
  278.     int has_color;
  279.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  280.     switch ( colors_size )
  281.        {
  282.     case 3*2:
  283.         palette_count = 2; num_components = 3;
  284.     case 2:
  285.         bits_per_pixel = 1; break;
  286.     case 3*4:
  287.         palette_count = 4; num_components = 3;
  288.     case 4:
  289.         bits_per_pixel = 2; break;
  290.     case 3*16:
  291.         palette_count = 16; num_components = 3;
  292.     case 16:
  293.         bits_per_pixel = 4; break;
  294.     case 3*256:
  295.         palette_count = 256; num_components = 3;
  296.     case 256:
  297.         bits_per_pixel = 8; break;
  298.     case -16:
  299.         bits_per_pixel = 16; palette_count = 0; break;
  300.     case -24:
  301.         bits_per_pixel = 24; palette_count = 0; break;
  302.     case -32:
  303.         bits_per_pixel = 32; palette_count = 0; break;
  304.     default:
  305.         return_error(gs_error_rangecheck);
  306.        }
  307.     old_dev = gdev_mem_device_for_bits(bits_per_pixel);
  308.     if ( old_dev == 0 )        /* no suitable device */
  309.         return_error(gs_error_rangecheck);
  310.     pcount = palette_count * 3;
  311.     /* Check to make sure the palette contains white and black, */
  312.     /* and, if it has any colors, the six primaries. */
  313.     if ( bits_per_pixel <= 8 )
  314.        {    const byte *p;
  315.         byte *q;
  316.         int primary_mask = 0;
  317.         int i;
  318.         has_color = 0;
  319.         for ( i = 0, p = colors, q = palette;
  320.               i < palette_count; i++, q += 3
  321.             )
  322.            {    int mask = 1;
  323.             switch ( num_components )
  324.                {
  325.             case 1:            /* gray */
  326.                 q[0] = q[1] = q[2] = *p++;
  327.                 break;
  328.             default /* case 3 */:    /* RGB */
  329.                 q[0] = p[0], q[1] = p[1], q[2] = p[2];
  330.                 p += 3;
  331.                }
  332. #define shift_mask(b,n)\
  333.   switch ( b ) { case 0xff: mask <<= n; case 0: break; default: mask = 0; }
  334.             shift_mask(q[0], 4);
  335.             shift_mask(q[1], 2);
  336.             shift_mask(q[2], 1);
  337. #undef shift_mask
  338.             primary_mask |= mask;
  339.             if ( q[0] != q[1] || q[0] != q[2] )
  340.                 has_color = 1;
  341.            }
  342.         switch ( primary_mask )
  343.            {
  344.         case 129:        /* just black and white */
  345.             if ( has_color )    /* color but no primaries */
  346.                 return_error(gs_error_rangecheck);
  347.         case 255:        /* full color */
  348.             break;
  349.         default:
  350.             return_error(gs_error_rangecheck);
  351.            }
  352.        }
  353.     else
  354.         has_color = 1;
  355.     /*
  356.      * The initial transformation matrix must map 1 user unit to
  357.      * 1/72".  Let W and H be the width and height in pixels, and
  358.      * assume the initial matrix is of the form [A 0 0 B X Y].
  359.      * Then the size of the image in user units is (W/|A|,H/|B|),
  360.      * hence the size in inches is ((W/|A|)/72,(H/|B|)/72), so
  361.      * the number of pixels per inch is
  362.      * (W/((W/|A|)/72),H/((H/|B|)/72)), or (|A|*72,|B|*72).
  363.      * Similarly, if the initial matrix is [0 A B 0 X Y] for a 90
  364.      * or 270 degree rotation, the size of the image in user
  365.      * units is (W/|B|,H/|A|), so the pixels per inch are
  366.      * (|B|*72,|A|*72).  We forbid non-orthogonal transformation
  367.      * matrices.
  368.      */
  369.     if ( is_fzero2(pmat->xy, pmat->yx) )
  370.         x_pixels_per_unit = pmat->xx, y_pixels_per_unit = pmat->yy;
  371.     else if ( is_fzero2(pmat->xx, pmat->yy) )
  372.         x_pixels_per_unit = pmat->yx, y_pixels_per_unit = pmat->xy;
  373.     else
  374.         return_error(gs_error_undefinedresult);
  375.     /* All checks done, allocate the device. */
  376.     new_dev = gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  377.                   "gs_makeimagedevice(device)");
  378.     dev_palette = gs_alloc_string(mem, pcount, "gs_makeimagedevice(palette)");
  379.     if ( new_dev == 0 || dev_palette == 0 )
  380.       {    gs_free_object(mem, new_dev, "gs_makeimagedevice(device)");
  381.         gs_free_object(mem, dev_palette, "gs_makeimagedevice(palette)");
  382.         return_error(gs_error_VMerror);
  383.       }
  384.     gs_make_mem_device(new_dev, old_dev, mem, 1);
  385.     new_dev->initial_matrix = *pmat;
  386.     new_dev->width = width;
  387.     new_dev->height = height;
  388.     new_dev->x_pixels_per_inch = fabs(x_pixels_per_unit) * 72;
  389.     new_dev->y_pixels_per_inch = fabs(y_pixels_per_unit) * 72;
  390.     if ( !has_color )
  391.       {    new_dev->color_info.num_components = 1;
  392.         new_dev->color_info.max_rgb = 0;
  393.         new_dev->color_info.dither_rgb = 0;
  394.       }
  395.     new_dev->invert = (palette[0] | palette[1] | palette[2] ? -1 : 0);    /* bogus */
  396.     new_dev->palette.size = pcount;
  397.     new_dev->palette.data = dev_palette;
  398.     memcpy(dev_palette, palette, pcount);
  399.     /* The bitmap will be allocated when the device is opened. */
  400.     new_dev->memory = mem;
  401.     new_dev->is_open = false;
  402.     new_dev->bitmap_memory = mem;
  403.     *pnew_dev = (gx_device *)new_dev;
  404.     return 0;
  405. }
  406.  
  407. /* Set the device in the graphics state */
  408. int
  409. gs_setdevice(gs_state *pgs, gx_device *dev)
  410. {    int code = gs_setdevice_no_erase(pgs, dev);
  411.     if ( code == 1 )
  412.         code = gs_erasepage(pgs);
  413.     return code;
  414. }
  415. int
  416. gs_setdevice_no_erase(gs_state *pgs, gx_device *dev)
  417. {    bool was_open = dev->is_open;
  418.     int code;
  419.     /* Initialize the device */
  420.     if ( !was_open )
  421.     {    gx_device_fill_in_procs(dev);
  422.         if ( gs_device_is_memory(dev) )
  423.         {    /* Set the target to the current device. */
  424.             gx_device *odev = gs_currentdevice_inline(pgs);
  425.             while ( odev != 0 && gs_device_is_memory(odev) )
  426.                 odev = ((gx_device_memory *)odev)->target;
  427.             ((gx_device_memory *)dev)->target = odev;
  428.         }
  429.         code = (*dev_proc(dev, open_device))(dev);
  430.         if ( code < 0 ) return_error(code);
  431.         dev->is_open = true;
  432.     }
  433.     /* Compute device white and black codes */
  434.     dev->cached.black = gx_map_cmyk_color(dev, 0, 0, 0, gx_max_color_value);
  435.     dev->cached.white = gx_map_cmyk_color(dev, 0, 0, 0, 0);
  436.     pgs->device = dev;
  437.     gx_set_cmap_procs(pgs);
  438.     if (    (code = gs_initmatrix(pgs)) < 0 ||
  439.         (code = gs_initclip(pgs)) < 0
  440.        )
  441.         return code;
  442.     gx_unset_dev_color(pgs);
  443.     /* If we were in a charpath or a setcachedevice, */
  444.     /* we aren't any longer. */
  445.     pgs->in_cachedevice = 0;
  446.     pgs->in_charpath = 0;
  447.     return (was_open ? 0 : 1);
  448. }
  449.  
  450. /* Make a null device. */
  451. void
  452. gs_make_null_device(gx_device_null *dev, gs_memory_t *mem)
  453. {    *dev = gs_null_device;
  454.     dev->memory = mem;
  455.     dev->procs = &dev->std_procs;
  456. }
  457.  
  458. /* Select the null device.  This is just a convenience. */
  459. void
  460. gs_nulldevice(gs_state *pgs)
  461. {    gs_null_device.procs = &gs_null_device.std_procs;
  462.     gs_setdevice(pgs, (gx_device *)&gs_null_device);
  463. }
  464.  
  465. /* Close a device.  The client is responsible for ensuring that */
  466. /* this device is not current in any graphics state. */
  467. int
  468. gs_closedevice(gx_device *dev)
  469. {    int code = 0;
  470.     if ( dev->is_open )
  471.        {    code = (*dev_proc(dev, close_device))(dev);
  472.         if ( code < 0 ) return_error(code);
  473.         dev->is_open = false;
  474.        }
  475.     return code;
  476. }
  477.  
  478. /* Install enough of a null device to suppress graphics output */
  479. /* during the execution of stringwidth. */
  480. void
  481. gx_device_no_output(gs_state *pgs)
  482. {    pgs->device = (gx_device *)&gs_null_device;
  483. }
  484.  
  485. /* Just set the device without reinitializing. */
  486. /* (For internal use only.) */
  487. void
  488. gx_set_device_only(gs_state *pgs, gx_device *dev)
  489. {    pgs->device = dev;
  490. }
  491.  
  492. /* Compute the size of one scan line for a device, */
  493. /* with or without padding to a word boundary. */
  494. uint
  495. gx_device_raster(const gx_device *dev, int pad)
  496. {    ulong bits = (ulong)dev->width * dev->color_info.depth;
  497.     return (pad ? bitmap_raster(bits) : (uint)((bits + 7) >> 3));
  498. }
  499.  
  500. /* Adjust the resolution for devices that only have a fixed set of */
  501. /* geometries, so that the apparent size in inches remains constant. */
  502. /* If fit=1, the resolution is adjusted so that the entire image fits; */
  503. /* if fit=0, one dimension fits, but the other one is clipped. */
  504. int
  505. gx_device_adjust_resolution(gx_device *dev,
  506.   int actual_width, int actual_height, int fit)
  507. {    double width_ratio = (double)actual_width / dev->width ;
  508.     double height_ratio = (double)actual_height / dev->height ;
  509.     double ratio =
  510.         (fit ? min(width_ratio, height_ratio) :
  511.          max(width_ratio, height_ratio));
  512.     dev->x_pixels_per_inch *= ratio;
  513.     dev->y_pixels_per_inch *= ratio;
  514.     dev->width = actual_width;
  515.     dev->height = actual_height;
  516.     return 0;
  517. }
  518.  
  519. /* ------ Default device procedures ------ */
  520.  
  521. int
  522. gx_default_open_device(gx_device *dev)
  523. {    return 0;
  524. }
  525.  
  526. void
  527. gx_default_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  528. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  529.     pmat->xy = 0;
  530.     pmat->yx = 0;
  531.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  532.     pmat->tx = 0;
  533.     pmat->ty = dev->height;    /****** WRONG for devices with ******/
  534.                 /****** arbitrary initial matrix ******/
  535. }
  536.  
  537. int
  538. gx_default_sync_output(gx_device *dev)
  539. {    return 0;
  540. }
  541.  
  542. int
  543. gx_default_output_page(gx_device *dev, int num_copies, int flush)
  544. {    return (*dev_proc(dev, sync_output))(dev);
  545. }
  546.  
  547. int
  548. gx_default_close_device(gx_device *dev)
  549. {    return 0;
  550. }
  551.  
  552. int
  553. gx_default_copy_color(gx_device *dev, const byte *data,
  554.   int data_x, int raster, gx_bitmap_id id,
  555.   int x, int y, int width, int height)
  556. {    return (*dev_proc(dev, copy_mono))(dev, data, data_x, raster, id,
  557.         x, y, width, height, (gx_color_index)0, (gx_color_index)1);
  558. }
  559.  
  560. int
  561. gx_default_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  562. {    return -1;
  563. }
  564.  
  565. gx_xfont_procs *
  566. gx_default_get_xfont_procs(gx_device *dev)
  567. {    return NULL;
  568. }
  569.  
  570. gx_device *
  571. gx_default_get_xfont_device(gx_device *dev)
  572. {    return dev;
  573. }
  574.  
  575. gx_device *
  576. gx_default_get_page_device(gx_device *dev)
  577. {    return NULL;
  578. }
  579. gx_device *
  580. gx_page_device_get_page_device(gx_device *dev)
  581. {    return dev;
  582. }
  583.  
  584. /* ------ Default per-instance procedures ------ */
  585.  
  586. int
  587. gx_default_install(gx_device *dev, gs_state *pgs)
  588. {    return 0;
  589. }
  590.  
  591. int
  592. gx_default_begin_page(gx_device *dev, gs_state *pgs)
  593. {    return 0;
  594. }
  595.  
  596. int
  597. gx_default_end_page(gx_device *dev, int reason, gs_state *pgs)
  598. {    return (reason != 2 ? 1 : 0);
  599. }
  600.  
  601. /* ------ Default forwarding procedures ------ */
  602.  
  603. #define fdev ((gx_device_forward *)dev)
  604.  
  605. void
  606. gx_forward_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  607. { gx_device *tdev = fdev->target;
  608.     if ( tdev == 0 )
  609.         gx_default_get_initial_matrix(dev, pmat);
  610.     else
  611.         (*dev_proc(tdev, get_initial_matrix))(tdev, pmat);
  612. }
  613.  
  614. gx_color_index
  615. gx_forward_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  616.   gx_color_value b)
  617. {    gx_device *tdev = fdev->target;
  618.     return (tdev == 0 ? gx_default_map_rgb_color(dev, r, g, b) :
  619.         (*dev_proc(tdev, map_rgb_color))(tdev, r, g, b));
  620. }
  621.  
  622. int
  623. gx_forward_map_color_rgb(gx_device *dev, gx_color_index color,
  624.   gx_color_value prgb[3])
  625. {    gx_device *tdev = fdev->target;
  626.     return (tdev == 0 ? gx_default_map_color_rgb(dev, color, prgb) :
  627.         (*dev_proc(tdev, map_color_rgb))(tdev, color, prgb));
  628. }
  629.  
  630. int
  631. gx_forward_get_params(gx_device *dev, gs_param_list *plist)
  632. {    gx_device *tdev = fdev->target;
  633.     return (tdev == 0 ? gx_default_get_params(dev, plist) :
  634.         (*dev_proc(tdev, get_params))(tdev, plist));
  635. }
  636.  
  637. int
  638. gx_forward_put_params(gx_device *dev, gs_param_list *plist)
  639. {    gx_device *tdev = fdev->target;
  640.     return (tdev == 0 ? gx_default_put_params(dev, plist) :
  641.         (*dev_proc(tdev, put_params))(tdev, plist));
  642. }
  643.  
  644. gx_color_index
  645. gx_forward_map_cmyk_color(gx_device *dev, gx_color_value c, gx_color_value m,
  646.   gx_color_value y, gx_color_value k)
  647. {    gx_device *tdev = fdev->target;
  648.     return (tdev == 0 ? gx_default_map_cmyk_color(dev, c, m, y, k) :
  649.         (*dev_proc(tdev, map_cmyk_color))(tdev, c, m, y, k));
  650. }
  651.  
  652. gx_xfont_procs *
  653. gx_forward_get_xfont_procs(gx_device *dev)
  654. {    gx_device *tdev = fdev->target;
  655.     return (tdev == 0 ? gx_default_get_xfont_procs(dev) :
  656.         (*dev_proc(tdev, get_xfont_procs))(tdev));
  657. }
  658.  
  659. gx_device *
  660. gx_forward_get_xfont_device(gx_device *dev)
  661. {    gx_device *tdev = fdev->target;
  662.     return (tdev == 0 ? gx_default_get_xfont_device(dev) :
  663.         (*dev_proc(tdev, get_xfont_device))(tdev));
  664. }
  665.  
  666. gx_color_index
  667. gx_forward_map_rgb_alpha_color(gx_device *dev, gx_color_value r,
  668.   gx_color_value g, gx_color_value b, gx_color_value alpha)
  669. {    gx_device *tdev = fdev->target;
  670.     return (tdev == 0 ?
  671.         gx_default_map_rgb_alpha_color(dev, r, g, b, alpha) :
  672.         (*dev_proc(tdev, map_rgb_alpha_color))(tdev, r, g, b, alpha));
  673. }
  674.  
  675. gx_device *
  676. gx_forward_get_page_device(gx_device *dev)
  677. {    gx_device *tdev = fdev->target;
  678.     return (tdev == 0 ? gx_default_get_page_device(dev) :
  679.         (*dev_proc(tdev, get_page_device))(tdev));
  680. }
  681.  
  682. /* ------ The null device ------ */
  683.  
  684. private int
  685. null_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  686.   gx_color_index color)
  687. {    return 0;
  688. }
  689. private int
  690. null_copy_mono(gx_device *dev, const byte *data,
  691.   int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  692.   gx_color_index zero, gx_color_index one)
  693. {    return 0;
  694. }
  695. private int
  696. null_put_params(gx_device *dev, gs_param_list *plist)
  697. {    /* We must defeat attempts to reset the size; */
  698.     /* otherwise this is equivalent to gx_forward_put_params. */
  699.     gx_device *tdev = fdev->target;
  700.     int code;
  701.     if ( tdev != 0 )
  702.         return (*dev_proc(tdev, put_params))(tdev, plist);
  703.     code = gx_default_put_params(dev, plist);
  704.     if ( code < 0 )
  705.         return code;
  706.     dev->width = dev->height = 0;
  707.     return code;
  708. }
  709.