home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GXDEVICE.H < prev    next >
C/C++ Source or Header  |  1994-07-27  |  17KB  |  454 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. /* gxdevice.h */
  20. /* Device description structure for Ghostscript library */
  21.  
  22. #ifndef gxdevice_INCLUDED
  23. #  define gxdevice_INCLUDED
  24.  
  25. #include "gsmatrix.h"
  26. #include "gsxfont.h"
  27. #include "gxbitmap.h"
  28. #include "gxdcolor.h"
  29.  
  30. /* See drivers.doc for documentation of the driver interface. */
  31. #ifndef gx_device_DEFINED
  32. #  define gx_device_DEFINED
  33. typedef struct gx_device_s gx_device;
  34. #endif
  35.  
  36. /* We need at least an abstract type for a graphics state, */
  37. /* which is passed to the page device procedures. */
  38. #ifndef gs_state_DEFINED
  39. #  define gs_state_DEFINED
  40. typedef struct gs_state_s gs_state;
  41. #endif
  42.  
  43. /* Define the type for gray or RGB values at the driver interface. */
  44. typedef unsigned short gx_color_value;
  45. #define arch_sizeof_gx_color_value arch_sizeof_short
  46. /* We might use less than the full range someday. */
  47. /* ...bits must lie between 8 and 16. */
  48. #define gx_color_value_bits (sizeof(gx_color_value) * 8)
  49. #define gx_max_color_value ((gx_color_value)((1L << gx_color_value_bits) - 1))
  50. #define gx_color_value_to_byte(cv)\
  51.   ((cv) >> (gx_color_value_bits - 8))
  52. #define gx_color_value_from_byte(cb)\
  53.   (((cb) << (gx_color_value_bits - 8)) + ((cb) >> (16 - gx_color_value_bits)))
  54.  
  55. /* Define the structure for device color capabilities. */
  56. typedef struct gx_device_color_info_s {
  57.     int num_components;        /* 1 = gray only, 3 = RGB, */
  58.                     /* 4 = CMYK */
  59.     int depth;            /* # of bits per pixel */
  60.     gx_color_value max_gray;    /* # of distinct gray levels -1 */
  61.     gx_color_value max_rgb;        /* # of distinct color levels -1 */
  62.                     /* (only relevant if num_comp. > 1) */
  63.     gx_color_value dither_gray;    /* size of gray ramp for dithering */
  64.     gx_color_value dither_rgb;    /* size of color cube ditto */
  65.                     /* (only relevant if num_comp. > 1) */
  66. } gx_device_color_info;
  67. #define dci_black_and_white { 1, 1, 1, 0, 2, 0 }
  68. #define dci_color(depth,maxv,dither) { 3, depth, maxv, maxv, dither, dither }
  69. #define gx_device_has_color(dev) ((dev)->color_info.num_components > 1)
  70.  
  71. /* Structure for device procedures */
  72. typedef struct gx_device_procs_s gx_device_procs;
  73.  
  74. /* Structure for standard "page device" properties. */
  75. typedef struct gx_pagedevice_s {
  76.  
  77.     float page_size[2];        /* PageSize */
  78.     bool imaging_bbox_set;
  79.     float imaging_bbox[4];        /* ImagingBBox */
  80. #if 0
  81.     /* We use x/y_pixels_per_inch instead of hw_resolution, */
  82.     /* for backward compatibility. */
  83.     float hw_resolution[2];        /* HWResolution */
  84. #endif
  85.     float margins[2];        /* Margins */
  86.     bool orientation_set;
  87.     int orientation;        /* Orientation */
  88.  
  89.     /* "Page device" procedures. */
  90.     /* Note that these take the graphics state as a parameter. */
  91.  
  92. #define dev_page_proc_install(proc)\
  93.   int proc(P2(gx_device *dev, gs_state *pgs))
  94.     dev_page_proc_install((*install));
  95.  
  96. #define dev_page_proc_begin_page(proc)\
  97.   int proc(P2(gx_device *dev, gs_state *pgs))
  98.     dev_page_proc_begin_page((*begin_page));
  99.  
  100. #define dev_page_proc_end_page(proc)\
  101.   int proc(P3(gx_device *dev, int reason, gs_state *pgs))
  102.     dev_page_proc_end_page((*end_page));
  103.  
  104. } gx_pagedevice;
  105. /* Default procedures */
  106. dev_page_proc_install(gx_default_install);
  107. dev_page_proc_begin_page(gx_default_begin_page);
  108. dev_page_proc_end_page(gx_default_end_page);
  109. /* Default pagedevice value */
  110. #define gx_pagedevice_defaults\
  111.  { {612, 792}, false, {0, 0, 612, 792}, /*{300, 300},*/ {0, 0}, false, 0,\
  112.    gx_default_install, gx_default_begin_page, gx_default_end_page\
  113.  }
  114.  
  115. /*
  116.  * Structure for generic device description.  The device procedures can
  117.  * have three different configurations:
  118.  * 
  119.  *    - Statically initialized devices predating release 2.8.1
  120.  *    set the procs pointer to point to a separate procedure record,
  121.  *    and do not initialize std_procs.
  122.  *
  123.  *    - Statically initialized devices starting with release 2.8.1
  124.  *    set the procs pointer to 0, and initialize std_procs.
  125.  *
  126.  *    - Dynamically initialized devices set the procs pointer to
  127.  *    point to the embedded std_procs record.
  128.  *
  129.  * The gx_device_set_procs procedure converts the first two of these to
  130.  * the third, which is what all client code starting in 2.8.1 expects
  131.  * (using the std_procs record, not the procs pointer, to call the
  132.  * driver procedures).
  133.  */
  134. #define gx_device_common\
  135.     int params_size;        /* OBSOLETE */\
  136.                     /* size of this structure */\
  137.     gx_device_procs *procs;        /* OBSOLETE */\
  138.                     /* pointer to std_procs */\
  139.     const char *dname;        /* the device name */\
  140.     int width;            /* width in pixels */\
  141.     int height;            /* height in pixels */\
  142.     float x_pixels_per_inch;    /* x density */\
  143.     float y_pixels_per_inch;    /* y density */\
  144.     float l_margin, b_margin, r_margin, t_margin;    /* margins around */\
  145.                     /* imageable area, in inches */\
  146.     gx_device_color_info color_info;    /* color information */\
  147.         /* dev_init_misc initializes all values between */\
  148.         /* this point and std_procs */\
  149.     gs_memory_t *memory;        /* (0 if statically allocated instance) */\
  150.     bool is_open;            /* true if device has been opened */\
  151.     int page_count;            /* number of pages written */\
  152.     int showpage_count;        /* number of calls on showpage */\
  153.     struct {\
  154.         gx_color_index white;    /* (cache) device white color */\
  155.         gx_color_index black;    /* (cache) device black color */\
  156.     } cached;\
  157.     gx_pagedevice page;\
  158.         /* end of dev_init_misc */\
  159.     gx_device_procs std_procs    /* standard procedures */
  160. #define no_margins 0, 0, 0, 0
  161. #define dev_init_misc_open(preopen)\
  162.   NULL, preopen, 0, 0,\
  163.   { (preopen ? 1 : gx_no_color_index), (preopen ? 0 : gx_no_color_index) },\
  164.   gx_pagedevice_defaults
  165. #define dev_init_misc dev_init_misc_open(false)
  166. /* Accessors for device procedures */
  167. #define dev_proc(dev, p) ((dev)->std_procs.p)
  168. #define set_dev_proc(dev, p, proc) ((dev)->std_procs.p = (proc))
  169. #define fill_dev_proc(dev, p, dproc)\
  170.   if ( dev_proc(dev, p) == 0 ) set_dev_proc(dev, p, dproc)
  171.  
  172. /* Define an opaque type for parameter lists. */
  173. #ifndef gs_param_list_DEFINED
  174. #  define gs_param_list_DEFINED
  175. typedef struct gs_param_list_s gs_param_list;
  176. #endif
  177.  
  178. /* Definition of device procedures. */
  179. /* Note that the gx_device * argument is not declared const, */
  180. /* because many drivers maintain dynamic state in the device structure. */
  181.  
  182. struct gx_device_procs_s {
  183.  
  184. #define dev_proc_open_device(proc)\
  185.   int proc(P1(gx_device *dev))
  186.     dev_proc_open_device((*open_device));
  187.  
  188. #define dev_proc_get_initial_matrix(proc)\
  189.   void proc(P2(gx_device *dev, gs_matrix *pmat))
  190.     dev_proc_get_initial_matrix((*get_initial_matrix));
  191.  
  192. #define dev_proc_sync_output(proc)\
  193.   int proc(P1(gx_device *dev))
  194.     dev_proc_sync_output((*sync_output));
  195.  
  196. #define dev_proc_output_page(proc)\
  197.   int proc(P3(gx_device *dev, int num_copies, int flush))
  198.     dev_proc_output_page((*output_page));
  199.  
  200. #define dev_proc_close_device(proc)\
  201.   int proc(P1(gx_device *dev))
  202.     dev_proc_close_device((*close_device));
  203.  
  204. #define dev_proc_map_rgb_color(proc)\
  205.   gx_color_index proc(P4(gx_device *dev,\
  206.     gx_color_value red, gx_color_value green, gx_color_value blue))
  207.     dev_proc_map_rgb_color((*map_rgb_color));
  208.  
  209. #define dev_proc_map_color_rgb(proc)\
  210.   int proc(P3(gx_device *dev,\
  211.     gx_color_index color, gx_color_value rgb[3]))
  212.     dev_proc_map_color_rgb((*map_color_rgb));
  213.  
  214. #define dev_proc_fill_rectangle(proc)\
  215.   int proc(P6(gx_device *dev,\
  216.     int x, int y, int width, int height, gx_color_index color))
  217.     dev_proc_fill_rectangle((*fill_rectangle));
  218.  
  219. #define dev_proc_tile_rectangle(proc)\
  220.   int proc(P10(gx_device *dev,\
  221.     const gx_tile_bitmap *tile, int x, int y, int width, int height,\
  222.     gx_color_index color0, gx_color_index color1,\
  223.     int phase_x, int phase_y))
  224.     dev_proc_tile_rectangle((*tile_rectangle));
  225.  
  226. #define dev_proc_copy_mono(proc)\
  227.   int proc(P11(gx_device *dev,\
  228.     const unsigned char *data, int data_x, int raster, gx_bitmap_id id,\
  229.     int x, int y, int width, int height,\
  230.     gx_color_index color0, gx_color_index color1))
  231.     dev_proc_copy_mono((*copy_mono));
  232.  
  233. #define dev_proc_copy_color(proc)\
  234.   int proc(P9(gx_device *dev,\
  235.     const unsigned char *data, int data_x, int raster, gx_bitmap_id id,\
  236.     int x, int y, int width, int height))
  237.     dev_proc_copy_color((*copy_color));
  238.  
  239. #define dev_proc_draw_line(proc)\
  240.   int proc(P6(gx_device *dev,\
  241.     int x0, int y0, int x1, int y1, gx_color_index color))
  242.     dev_proc_draw_line((*draw_line));
  243.  
  244.         /* Added in release 2.4 */
  245.  
  246. #define dev_proc_get_bits(proc)\
  247.   int proc(P4(gx_device *dev,\
  248.     int y, unsigned char *data, unsigned char **actual_data))
  249.     dev_proc_get_bits((*get_bits));
  250.  
  251.         /* Added in release 2.4, changed in 2.8, */
  252.         /* renamed in 2.9.6 */
  253.  
  254. #define dev_proc_get_params(proc)\
  255.   int proc(P2(gx_device *dev, gs_param_list *plist))
  256.     dev_proc_get_params((*get_params));
  257.     
  258. #define dev_proc_put_params(proc)\
  259.   int proc(P2(gx_device *dev, gs_param_list *plist))
  260.     dev_proc_put_params((*put_params));
  261.  
  262.         /* Added in release 2.6 */
  263.  
  264. #define dev_proc_map_cmyk_color(proc)\
  265.   gx_color_index proc(P5(gx_device *dev,\
  266.     gx_color_value cyan, gx_color_value magenta, gx_color_value yellow,\
  267.     gx_color_value black))
  268.     dev_proc_map_cmyk_color((*map_cmyk_color));
  269.     
  270. #define dev_proc_get_xfont_procs(proc)\
  271.   gx_xfont_procs *proc(P1(gx_device *dev))
  272.     dev_proc_get_xfont_procs((*get_xfont_procs));
  273.  
  274.         /* Added in release 2.6.1 */
  275.  
  276. #define dev_proc_get_xfont_device(proc)\
  277.   gx_device *proc(P1(gx_device *dev))
  278.     dev_proc_get_xfont_device((*get_xfont_device));
  279.  
  280.         /* Added in release 2.7.1 */
  281.  
  282. #define dev_proc_map_rgb_alpha_color(proc)\
  283.   gx_color_index proc(P5(gx_device *dev,\
  284.     gx_color_value red, gx_color_value green, gx_color_value blue,\
  285.     gx_color_value alpha))
  286.     dev_proc_map_rgb_alpha_color((*map_rgb_alpha_color));
  287.  
  288.         /* Added in release 2.8.1 */
  289.  
  290. #define dev_proc_get_page_device(proc)\
  291.   gx_device *proc(P1(gx_device *dev))
  292.     dev_proc_get_page_device((*get_page_device));
  293.  
  294. };
  295.  
  296. /* A generic device */
  297. struct gx_device_s {
  298.     gx_device_common;
  299. };
  300. extern_st(st_device);
  301. #define public_st_device()    /* in gsdevice.c */\
  302.   gs_public_st_simple(st_device, gx_device, "gx_device")
  303. #define st_device_max_ptrs 0
  304.  
  305. /* Enumerate or relocate a pointer to a device. */
  306. /* These take the containing space into account properly. */
  307. gx_device *gx_device_enum_ptr(P1(gx_device *));
  308. gx_device *gx_device_reloc_ptr(P2(gx_device *, gc_state_t *));
  309.  
  310. /* Define typedefs for some of the device procedures, because */
  311. /* ansi2knr can't handle dev_proc_xxx((*xxx)) in a formal argument list. */
  312. typedef dev_proc_map_rgb_color((*dev_proc_map_rgb_color_t));
  313. typedef dev_proc_map_color_rgb((*dev_proc_map_color_rgb_t));
  314.  
  315. /* A device that forwards non-display operations to another device */
  316. /* called the "target".  This is used for clipping, banding, image, */
  317. /* and null devices. */
  318. #define gx_device_forward_common\
  319.     gx_device_common;\
  320.     gx_device *target
  321. /* A generic forwarding device. */
  322. typedef struct gx_device_forward_s {
  323.     gx_device_forward_common;
  324. } gx_device_forward;
  325. extern_st(st_device_forward);
  326. #define public_st_device_forward()    /* in gsdevice.c */\
  327.   gs_public_st_composite(st_device_forward, gx_device_forward,\
  328.     "gx_device_forward", device_forward_enum_ptrs, device_forward_reloc_ptrs)
  329. #define st_device_forward_max_ptrs (st_device_max_ptrs + 1)
  330.  
  331. /* A null device.  This is used to temporarily disable output. */
  332. typedef struct gx_device_null_s {
  333.     gx_device_forward_common;
  334. } gx_device_null;
  335. extern_st(st_device_null);
  336. #define public_st_device_null()    /* in gsdevice.c */\
  337.   gs_public_st_suffix_add0(st_device_null, gx_device_null, "gx_device_null",\
  338.     device_forward_enum_ptrs, device_forward_reloc_ptrs)
  339. #define st_device_null_max_ptrs st_device_forward_max_ptrs
  340. /* Make a null device. */
  341. /* The gs_memory_t argument is 0 if the device is temporary and local, */
  342. /* or the allocator that was used to allocate it if it is a real object. */
  343. void    gs_make_null_device(P2(gx_device_null *, gs_memory_t *));
  344.  
  345. /* Calculate the raster (number of bytes in a scan line), */
  346. /* with byte or word padding. */
  347. uint    gx_device_raster(P2(const gx_device *dev, int pad_to_word));
  348.  
  349. /* Adjust the resolution for devices that only have a fixed set of */
  350. /* geometries, so that the apparent size in inches remains constant. */
  351. /* If fit=1, the resolution is adjusted so that the entire image fits; */
  352. /* if fit=0, one dimension fits, but the other one is clipped. */
  353. int    gx_device_adjust_resolution(P4(gx_device *dev, int actual_width, int actual_height, int fit));
  354.  
  355. /*
  356.  * Macros to help the drawing procedures clip coordinates to
  357.  * fit into the drawing region.  Note that these may modify
  358.  * x, y, w, h, data, data_x, and id.
  359.  */
  360.  
  361. /* Macro for fill_rectangle and tile_rectangle. */
  362. #define fit_fill(dev, x, y, w, h)\
  363.     if ( (x | y) < 0 )\
  364.     {    if ( x < 0 ) w += x, x = 0;\
  365.         if ( y < 0 ) h += y, y = 0;\
  366.     }\
  367.     if ( x > dev->width - w ) w = dev->width - x;\
  368.     if ( y > dev->height - h ) h = dev->height - y;\
  369.     if ( w <= 0 || h <= 0 ) return 0
  370.  
  371. /* Macro for copy_mono and copy_color. */
  372. #define fit_copy(dev, data, data_x, raster, id, x, y, w, h)\
  373.     if ( (x | y) < 0 )\
  374.     {    if ( x < 0 ) w += x, data_x -= x, x = 0;\
  375.         if ( y < 0 ) h += y, data -= y * raster, id = gx_no_bitmap_id, y = 0;\
  376.     }\
  377.     if ( x > dev->width - w ) w = dev->width - x;\
  378.     if ( y > dev->height - h ) h = dev->height - y;\
  379.     if ( w <= 0 || h <= 0 ) return 0
  380.  
  381. /* Default implementations of optional procedures. */
  382. /* Note that the default map_xxx_color routines assume black-and-white. */
  383. dev_proc_open_device(gx_default_open_device);
  384. dev_proc_get_initial_matrix(gx_default_get_initial_matrix);
  385. dev_proc_sync_output(gx_default_sync_output);
  386. dev_proc_output_page(gx_default_output_page);
  387. dev_proc_close_device(gx_default_close_device);
  388. dev_proc_map_rgb_color(gx_default_map_rgb_color);
  389. dev_proc_map_color_rgb(gx_default_map_color_rgb);
  390. dev_proc_tile_rectangle(gx_default_tile_rectangle);
  391. dev_proc_copy_color(gx_default_copy_color);
  392. dev_proc_draw_line(gx_default_draw_line);
  393. dev_proc_get_bits(gx_default_get_bits);
  394. dev_proc_get_params(gx_default_get_params);
  395. dev_proc_put_params(gx_default_put_params);
  396. dev_proc_put_params(gx_default_put_params_only);    /* no close/reopen */
  397. dev_proc_map_cmyk_color(gx_default_map_cmyk_color);
  398. dev_proc_get_xfont_procs(gx_default_get_xfont_procs);
  399. dev_proc_get_xfont_device(gx_default_get_xfont_device);
  400. dev_proc_map_rgb_alpha_color(gx_default_map_rgb_alpha_color);
  401. dev_proc_get_page_device(gx_default_get_page_device);    /* returns NULL */
  402. dev_proc_get_page_device(gx_page_device_get_page_device);    /* returns dev */
  403. /* Color mapping routines for gray scale, true RGB, and true CMYK color. */
  404. dev_proc_map_rgb_color(gx_default_gray_map_rgb_color);
  405. dev_proc_map_color_rgb(gx_default_gray_map_color_rgb);
  406. dev_proc_map_rgb_color(gx_default_rgb_map_rgb_color);
  407. dev_proc_map_color_rgb(gx_default_rgb_map_color_rgb);
  408. dev_proc_map_cmyk_color(gx_default_cmyk_map_cmyk_color);
  409.  
  410. /* Default implementations for forwarding devices */
  411. dev_proc_get_initial_matrix(gx_forward_get_initial_matrix);
  412. dev_proc_map_rgb_color(gx_forward_map_rgb_color);
  413. dev_proc_map_color_rgb(gx_forward_map_color_rgb);
  414. dev_proc_get_params(gx_forward_get_params);
  415. dev_proc_put_params(gx_forward_put_params);
  416. dev_proc_map_cmyk_color(gx_forward_map_cmyk_color);
  417. dev_proc_get_xfont_procs(gx_forward_get_xfont_procs);
  418. dev_proc_get_xfont_device(gx_forward_get_xfont_device);
  419. dev_proc_map_rgb_alpha_color(gx_forward_map_rgb_alpha_color);
  420. dev_proc_get_page_device(gx_forward_get_page_device);
  421.  
  422. /* Convert the device procedures to the proper form (see above). */
  423. void    gx_device_set_procs(P1(gx_device *));
  424.  
  425. /* Fill in defaulted procedures in a device procedure record. */
  426. void    gx_device_fill_in_procs(P1(gx_device *));
  427. void    gx_device_forward_fill_in_procs(P1(gx_device_forward *));
  428.  
  429. /* Temporarily install a null device, or a special device such as */
  430. /* a clipping device. */
  431. void gx_device_no_output(P1(gs_state *));
  432. void gx_set_device_only(P2(gs_state *, gx_device *));
  433.  
  434. /* Close a device. */
  435. int gs_closedevice(P1(gx_device *));
  436.  
  437. /* ------ Device types ------ */
  438.  
  439. #define dev_type_proc_initialize(proc)\
  440.   int proc(P1(gx_device *))
  441.  
  442. typedef struct gx_device_type_s {
  443.     gs_memory_type_ptr_t stype;
  444.     dev_type_proc_initialize((*initialize));
  445. } gx_device_type;
  446.  
  447. #define device_type(dtname, stype, initproc)\
  448. private dev_type_proc_initialize(initproc);\
  449. const gx_device_type dtname = { &stype, initproc }
  450.  
  451. dev_type_proc_initialize(gdev_initialize);
  452.  
  453. #endif                    /* gxdevice_INCLUDED */
  454.