home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gs333ini / gs3.33 / drivers.doc < prev    next >
Text File  |  1995-12-09  |  35KB  |  799 lines

  1.    Copyright (C) 1989, 1995 Aladdin Enterprises.
  2.      All rights reserved.
  3.   
  4.   This file is part of Aladdin Ghostscript.
  5.   
  6.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  7.   or distributor accepts any responsibility for the consequences of using it,
  8.   or for whether it serves any particular purpose or works at all, unless he
  9.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  10.   License (the "License") for full details.
  11.   
  12.   Every copy of Aladdin Ghostscript must include a copy of the License,
  13.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  14.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  15.   under certain conditions described in the License.  Among other things, the
  16.   License requires that the copyright notice and this notice be preserved on
  17.   all copies.
  18.  
  19. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  20.  
  21. This file, drivers.doc, describes the interface between Ghostscript and
  22. device drivers.
  23.  
  24. For an overview of Ghostscript and a list of the documentation files, see
  25. README.
  26.  
  27. ********
  28. ******** Adding a driver ********
  29. ********
  30.  
  31. To add a driver to Ghostscript, all you need to do is edit devs.mak in
  32. two places.  The first is the list of devices, in the section headed
  33.  
  34. # -------------------------------- Catalog ------------------------------- #
  35.  
  36. Pick a name for your device, say smurf, and add smurf to the list.
  37. (Device names must be 1 to 8 characters, consisting of only letters,
  38. digits, and underscores, of which the first character must be a letter.
  39. Case is significant: all current device names are lower case.)
  40. The second is the section headed
  41.  
  42. # ---------------------------- Device drivers ---------------------------- #
  43.  
  44. Suppose the files containing the smurf driver are called joe and fred.
  45. Then you should add the following lines:
  46.  
  47. # ------ The SMURF device ------ #
  48.  
  49. smurf_=joe.$(OBJ) fred.$(OBJ)
  50. smurf.dev: $(smurf_)
  51.     $(SHP)gssetdev smurf $(smurf_)
  52.  
  53. joe.$(OBJ): joe.c ...and whatever it depends on
  54.  
  55. fred.$(OBJ): fred.c ...and whatever it depends on
  56.  
  57. If the smurf driver also needs special libraries, e.g., a library named
  58. gorf, then the gssetdev line should look like
  59.     $(SHP)gssetdev smurf $(smurf_)
  60.     $(SHP)gsaddmod smurf -lib gorf
  61.  
  62. ********
  63. ******** Keeping things simple
  64. ********
  65.  
  66. If you want to add a simple device (specifically, a black-and-white
  67. printer), you probably don't need to read the rest of this document; just
  68. use the code in an existing driver as a guide.  The Epson and BubbleJet
  69. drivers (gdevepsn.c and gdevbj10.c) are good models for dot-matrix printers,
  70. which require presenting the data for many scan lines at once; the
  71. DeskJet/LaserJet drivers (gdevdjet.c) are good models for laser printers,
  72. which take a single scan line at a time but support data compression.  For
  73. color printers, the DeskJet 500C driver (gdevcdj.c) may be a good place to
  74. start, although it is large and complex.  gdevcdj.c is also a good example
  75. of a device that has additional settable attributes (in this case, different
  76. output quality modes that aren't just a matter of resolution).
  77.  
  78. On the other hand, if you're writing a driver for some more esoteric
  79. device, you probably do need at least some of the information in the rest
  80. of this document.  It might be a good idea for you to read it in
  81. conjunction with one of the existing drivers.
  82.  
  83. ********
  84. ******** Driver structure ********
  85. ********
  86.  
  87. A device is represented by a structure divided into three parts:
  88.  
  89.     - procedures that are shared by all instances of each device;
  90.  
  91.     - parameters that are present in all devices but may be different
  92.       for each device or instance; and
  93.  
  94.     - device-specific parameters that may be different for each instance.
  95.  
  96. Normally, the procedure structure is defined and initialized at compile
  97. time.  A prototype of the parameter structure (including both generic and
  98. device-specific parameters) is defined and initialized at compile time,
  99. but is copied and filled in when an instance of the device is created.
  100.  
  101. The gx_device_common macro defines the common structure elements, with the
  102. intent that devices define and export a structure along the following
  103. lines:
  104.  
  105.     typedef struct smurf_device_s {
  106.         gx_device_common;
  107.         ... device-specific parameters ...
  108.     } smurf_device;
  109.     smurf_device gs_smurf_device = {
  110.         sizeof(smurf_device),        /* params_size */
  111.         0,                /* static_procs, obsolete */
  112.         ... generic parameter values ...
  113.         { ... procedures ... },        /* std_procs */
  114.         ... device-specific parameter values ...
  115.     };
  116.  
  117. The device structure instance *must* have the name gs_smurf_device, where
  118. smurf is the device name used in devs.mak.
  119.  
  120. All the device procedures are called with the device as the first
  121. argument.  Since each device type is actually a different structure type,
  122. the device procedures must be declared as taking a gx_device * as their
  123. first argument, and must cast it to smurf_device * internally.  For
  124. example, in the code for the "memory" device, the first argument to all
  125. routines is called dev, but the routines actually use md to reference
  126. elements of the full structure, by virtue of the definition
  127.  
  128.     #define md ((gx_device_memory *)dev)
  129.  
  130. (This is a cheap version of "object-oriented" programming: in C++, for
  131. example, the cast would be unnecessary, and in fact the procedure table
  132. would be constructed by the compiler.)
  133.  
  134. Structure definition
  135. --------------------
  136.  
  137. This essentially duplicates the first part of the structure definition in
  138. gxdevice.h.
  139.  
  140. typedef struct gx_device_s {
  141.     int params_size;        /* size of this structure */
  142.     gx_device_procs *static_procs;    /* (obsolete) */
  143.     const char *dname;        /* the device name */
  144.     int width;            /* width in pixels */
  145.     int height;            /* height in pixels */
  146.     ...
  147.     gx_device_color_info color_info;    /* color information */
  148.     ...
  149.     int is_open;            /* true if device has been opened */
  150. } gx_device;
  151.  
  152. The name in the structure should be the same as the name in devs.mak.
  153.  
  154. gx_device_common is a macro consisting of just the element definitions.
  155.  
  156. For sophisticated developers only
  157. ---------------------------------
  158.  
  159. If for any reason you need to change the definition of the basic device
  160. structure, or add procedures, you must change the following places:
  161.  
  162.     - This document and NEWS (if you want to keep the
  163.         documentation up to date).
  164.     - The definition of gx_device_common and/or the procedures
  165.         in gxdevice.h.
  166.     - Possibly, the default forwarding procedures in gxdevice.h
  167.         and gsdevice.c.
  168.     - The following devices that must have complete (non-defaulted)
  169.         procedure vectors:
  170.         - The null device in gsdevice.c.
  171.         - The command list "device" in gxclist.c.  This is
  172.             not an actual device; it only defines procedures.
  173.         - The "memory" devices in gdevmem.h and gdevm*.c.
  174.     - The procedure record completion routine in gsdevice.c.
  175.     - The clip list accumulation "device" in gxacpath.c.
  176.     - The clipping "devices" in gxcpath.c and gxclip2.c.
  177.     - The hit detection "device" in zupath.c.
  178.     - The generic printer device macros in gdevprn.h.
  179.     - The generic printer device code in gdevprn.c.
  180.     - All the real devices in the standard Ghostscript distribution,
  181.         as listed in devs.mak.  (All of them are supposed to use
  182.         the macros in gxdevice.h or gdevprn.h to initialize their
  183.         state, so you may not have to edit the source code for
  184.         them.)
  185.     - Any other drivers you have that aren't part of the standard
  186.         Ghostscript distribution.
  187.  
  188. You may also have to change the code for gx_default_get_params and/or
  189. gx_default_put_params (in gsdparam.c).
  190.  
  191. Note that if all you are doing is adding optional procedures, you do NOT
  192. have to modify any of the real device drivers listed in devs.mak;
  193. Ghostscript will substitute the default procedures properly.
  194.  
  195. ********
  196. ******** Types and coordinates ********
  197. ********
  198.  
  199. Coordinate system
  200. -----------------
  201.  
  202. Since each driver specifies the initial transformation from user to device
  203. coordinates, the driver can use any coordinate system it wants, as long as
  204. a device coordinate will fit in an int.  (This is only an issue on MS-DOS
  205. systems, where ints are only 16 bits.  User coordinates are represented as
  206. floats.)  Typically the coordinate system will have (0,0) in the upper
  207. left corner, with X increasing to the right and Y increasing toward the
  208. bottom.  This happens to be the coordinate system that all the currently
  209. supported devices use.  However, there is supposed to be nothing in the
  210. rest of Ghostscript that assumes this.
  211.  
  212. Drivers must check (and, if necessary, clip) the coordinate parameters
  213. given to them: they should not assume the coordinates will be in bounds.
  214. The fit_fill and fit_copy macros in gxdevice.h are very helpful in doing
  215. this.
  216.  
  217. Color definition
  218. ----------------
  219.  
  220. Ghostscript represents colors internally as RGB or CMYK values.  In
  221. communicating with devices, however, it assumes that each device has a
  222. palette of colors identified by integers (to be precise, elements of type
  223. gx_color_index).  Drivers may provide a uniformly spaced gray ramp or
  224. color cube for halftoning, or they may do their own color approximation,
  225. or both.
  226.  
  227. The color_info member of the device structure defines the color and
  228. gray-scale capabilities of the device.  Its type is defined as follows:
  229.  
  230. typedef struct gx_device_color_info_s {
  231.     int num_components;        /* 1 = gray only, 3 = RGB, */
  232.                     /* 4 = CMYK */
  233.     int depth;            /* # of bits per pixel */
  234.     gx_color_value max_gray;    /* # of distinct gray levels -1 */
  235.     gx_color_value max_rgb;        /* # of distinct color levels -1 */
  236.                     /* (only relevant if num_comp. > 1) */
  237.     gx_color_value dither_gray;    /* size of gray ramp for halftoning */
  238.     gx_color_value dither_rgb;    /* size of color cube ditto */
  239.                     /* (only relevant if num_comp. > 1) */
  240. } gx_device_color_info;
  241.  
  242. The following macros (in gxdevice.h) provide convenient shorthands for
  243. initializing this structure for ordinary black-and-white or color devices:
  244.  
  245. #define dci_black_and_white ...
  246. #define dci_color(depth,maxv,dither) ...
  247.  
  248. The idea is that a device has a certain number of gray levels (max_gray +1)
  249. and a certain number of colors (max_rgb +1) that it can produce directly.
  250. When Ghostscript wants to render a given RGB or CMYK color as a device
  251. color, it first tests whether the color is a gray level.  (If
  252. num_components is 1, it converts all colors to gray levels.)  If so:
  253.  
  254.     - If max_gray is large (>= 31), Ghostscript asks the device to
  255. approximate the gray level directly.  If the device returns a valid
  256. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  257. the device can represent dither_gray distinct gray levels, equally spaced
  258. along the diagonal of the color cube, and uses the two nearest ones to the
  259. desired color for halftoning.
  260.  
  261. If the color is not a gray level:
  262.  
  263.     - If max_rgb is large (>= 31), Ghostscript asks the device to
  264. approximate the color directly.  If the device returns a valid
  265. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  266. the device can represent dither_rgb * dither_rgb * dither_rgb distinct
  267. colors, equally spaced throughout the color cube, and uses two of the
  268. nearest ones to the desired color for halftoning.
  269.  
  270. Types
  271. -----
  272.  
  273. Here is a brief explanation of the various types that appear as parameters
  274. or results of the drivers.
  275.  
  276. gx_color_value (defined in gxdevice.h)
  277.  
  278.     This is the type used to represent RGB or CMYK color values.  It is
  279. currently equivalent to unsigned short.  However, Ghostscript may use less
  280. than the full range of the type to represent color values:
  281. gx_color_value_bits is the number of bits actually used, and
  282. gx_max_color_value is the maximum value (equal to 2^gx_max_color_value_bits
  283. - 1).
  284.  
  285. gx_device (defined in gxdevice.h)
  286.  
  287.     This is the device structure, as explained above.
  288.  
  289. gs_matrix (defined in gsmatrix.h)
  290.  
  291.     This is a 2-D homogenous coordinate transformation matrix, used by
  292. many Ghostscript operators.
  293.  
  294. gx_color_index (defined in gxdevice.h)
  295.  
  296.     This is meant to be whatever the driver uses to represent a device
  297. color.  For example, it might be an index in a color map, or it might be
  298. R, G, and B values packed into a single integer.  Ghostscript doesn't ever
  299. do any computations with gx_color_index values: it gets them from
  300. map_rgb_color or map_cmyk_color and hands them back as arguments to
  301. several other procedures.  The special value gx_no_color_index (defined as
  302. (gx_color_index)(-1)) means "transparent" for some of the procedures.  The
  303. type definition is simply:
  304.  
  305.     typedef unsigned long gx_color_index;
  306.  
  307. gs_param_list (defined in gsparam.h)
  308.  
  309.     This is a parameter list, which is used to read and set attributes
  310. in a device.  See the comments in gsparam.h, and the description of the
  311. get_params and put_params procedures below, for more detail.
  312.  
  313. gx_bitmap (defined in gxbitmap.h)
  314.  
  315.     This structure type represents a bitmap to be used as a tile for
  316. filling a region (rectangle).  Here is a copy of the relevant part of the
  317. file:
  318.  
  319. /*
  320.  * Structure for describing stored bitmaps.
  321.  * Bitmaps are stored bit-big-endian (i.e., the 2^7 bit of the first
  322.  * byte corresponds to x=0), as a sequence of bytes (i.e., you can't
  323.  * do word-oriented operations on them if you're on a little-endian
  324.  * platform like the Intel 80x86 or VAX).  Each scan line must start on
  325.  * a (32-bit) word boundary, and hence is padded to a word boundary,
  326.  * although this should rarely be of concern, since the raster and width
  327.  * are specified individually.  The first scan line corresponds to y=0
  328.  * in whatever coordinate system is relevant.
  329.  *
  330.  * For bitmaps used as halftone tiles, we may replicate the tile in
  331.  * X and/or Y, but it is still valuable to know the true tile dimensions.
  332.  */
  333. typedef struct gx_bitmap_s {
  334.     byte *data;
  335.     int raster;            /* bytes per scan line */
  336.     gs_int_point size;        /* width, height */
  337.     gx_bitmap_id id;
  338.     ushort rep_width, rep_height;    /* true size of tile */
  339. } gx_bitmap;
  340.  
  341. ********
  342. ******** Coding conventions ********
  343. ********
  344.  
  345. While most drivers (especially printer drivers) follow a very similar
  346. template, there is one important coding convention that is not obvious from
  347. reading the code for existing drivers: Driver procedures must not use
  348. malloc to allocate any storage that stays around after the procedure
  349. returns.  Instead, they must use gs_malloc and gs_free, which have slightly
  350. different calling conventions.  (The prototypes for these are in
  351. gsmemory.h, which is included in gx.h, which is included in gdevprn.h.)
  352. This is necessary so that Ghostscript can clean up all allocated memory
  353. before exiting, which is essential in environments that provide only
  354. single-address-space multi-tasking (specifically, Microsoft Windows).
  355.  
  356. char *gs_malloc(uint num_elements, uint element_size,
  357.   const char *client_name);
  358.  
  359.     Like calloc, but unlike malloc, gs_malloc takes an element count
  360. and an element size.  For structures, num_elements is 1 and element_size is
  361. sizeof the structure; for byte arrays, num_elements is the number of bytes
  362. and element_size is 1.  Unlike calloc, gs_malloc does NOT clear the block
  363. of storage.
  364.  
  365.     The client_name is used for tracing and debugging.  It must be a
  366. real string, not NULL.  Normally it is the name of the procedure in which
  367. the call occurs.
  368.  
  369. void gs_free(char *data, uint num_elements, uint element_size,
  370.   const char *client_name);
  371.  
  372.     Unlike free, gs_free demands that num_elements and element_size be
  373. supplied.  It also requires a client name, like gs_malloc.
  374.  
  375. All the driver procedures defined below that return int results return 0 on
  376. success, or an appropriate negative error code in the case of error
  377. conditions.  The error codes are defined in gserrors.h.  The relevant ones
  378. for drivers are as follows:
  379.  
  380.     gs_error_invalidfileaccess
  381.         An attempt to open a file failed.
  382.  
  383.     gs_error_limitcheck
  384.         An otherwise valid parameter value was too large for
  385.         the implementation.
  386.  
  387.     gs_error_rangecheck
  388.         A parameter was outside the valid range.
  389.  
  390.     gs_error_VMerror
  391.         An attempt to allocate memory failed.  (If this
  392.         happens, the procedure should release all memory it
  393.         allocated before it returns.)
  394.  
  395. If a driver does return an error, it should use the return_error
  396. macro rather than a simple return statement, e.g.,
  397.  
  398.     return_error(gs_error_VMerror);
  399.  
  400. This macro is defined in gx.h, which is automatically included by
  401. gdevprn.h but not by gserrors.h.
  402.  
  403. ********
  404. ******** Printer drivers ********
  405. ********
  406.  
  407. Printer drivers (which include drivers that write some kind of raster
  408. file) are especially simple to implement.  Of the driver procedures
  409. defined in the next section, they only need implement two: map_rgb_color
  410. (or map_cmyk_color) and map_color_rgb.  In addition, they must implement a
  411. print_page or print_page_copies procedure.  There are a set of macros in
  412. gdevprn.h that generate the device structure for such devices, of which
  413. the simplest is prn_device; for an example, see gdevbj10.c.  If you are
  414. writing a printer driver, we suggest you start by reading gdevprn.h and
  415. the subsection on "Color mapping" below; you may be able to ignore all the
  416. rest of the driver procedures.
  417.  
  418. The print_page procedures are defined as follows:
  419.  
  420. int (*print_page)(P2(gx_device_printer *, FILE *))
  421. int (*print_page_copies)(P3(gx_device_printer *, FILE *, int))
  422.  
  423.     This procedure must read out the rendered image from the device and
  424. write whatever is appropriate to the file.  To read back one or more scan
  425. lines of the image, the print_page procedure must call one of the following
  426. procedures:
  427.  
  428.     int gdev_prn_copy_scan_lines(P4(gx_device_printer *pdev, int y, byte *str,
  429.       uint size)
  430.  
  431. For this procedure, str is where the data should be copied to, and size is
  432. the size of the buffer starting at str.  This procedure returns the number
  433. of scan lines copied, or <0 for an error.
  434.  
  435.     int gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str,
  436.       byte **actual_data)
  437.  
  438. This procedure reads out exactly one scan line.  If the scan line is
  439. available in the correct format already, *actual_data is set to point to
  440. it; otherwise, the scan line is copied to the buffer starting at str, and
  441. *actual_data is set to str.  This saves a copying step most of the time.
  442.  
  443. In either case, each row of the image is stored in the form described in
  444. the comment under gx_bitmap above; each pixel takes the number of bits
  445. specified as color_info.depth in the device structure, and holds values
  446. returned by the device's map_{rgb,cmyk}_color procedure.
  447.  
  448. The print_page procedure can determine the number of bytes required to hold
  449. a scan line by calling:
  450.  
  451.     uint gdev_prn_raster(P1(gx_device_printer *))
  452.  
  453. For a very simple concrete example, we suggest reading the code in
  454. bit_print_page in gdevbit.c.
  455.  
  456. If the device provides print_page, Ghostscript will call print_page the
  457. requisite number of times to print the desired number of copies; if the
  458. device provides print_page_copies, Ghostscript will call print_page_copies
  459. once per page, passing it the desired number of copies.
  460.  
  461. ********
  462. ******** Driver procedures ********
  463. ********
  464.  
  465. Most of the procedures that a driver may implement are optional.  If a
  466. device doesn't supply an optional procedure <proc>, the entry in the
  467. procedure structure may be either gx_default_<proc>, e.g.
  468. gx_default_tile_rectangle, or NULL or 0.  (The device procedure must also
  469. call the gx_default_ procedure if it doesn't implement the function for
  470. particular values of the arguments.)  Since C compilers supply 0 as the
  471. value for omitted structure elements, this convention means that
  472. statically initialized procedure structures will continue to work even if
  473. new (optional) members are added.
  474.  
  475. Life cycle
  476. ----------
  477.  
  478. A device instance start out life in a closed state.  In this state, no
  479. output operations will occur.  Only the following procedures may be called:
  480.     open_device
  481.     get_initial_matrix
  482.     get_params
  483.     put_params
  484.  
  485. When setdevice installs a device instance in the graphics state, it checks
  486. whether the instance is closed or open.  If the instance is closed,
  487. setdevice calls the open routine, and then sets the state to open.
  488.  
  489. There is currently no user-accessible operation to close a device instance.
  490. Device instances are only closed when they are about to be freed, which
  491. occurs in three situations:
  492.  
  493.     - When a 'restore' occurs, if the instance was created since the
  494. corresponding 'save';
  495.  
  496.     - By the garbage collector, if the instance is no longer accessible;
  497.  
  498.     - When Ghostscript exits (terminates).
  499.  
  500. Open/close/sync
  501. ---------------
  502.  
  503. int (*open_device)(P1(gx_device *)) [OPTIONAL]
  504.  
  505.     Open the device: do any initialization associated with making the
  506. device instance valid.  This must be done before any output to the device.
  507. The default implementation does nothing.
  508.  
  509. void (*get_initial_matrix)(P2(gx_device *, gs_matrix *)) [OPTIONAL]
  510.  
  511.     Construct the initial transformation matrix mapping user
  512. coordinates (nominally 1/72" per unit) to device coordinates.  The default
  513. procedure computes this from width, height, and x/y_pixels_per_inch on the
  514. assumption that the origin is in the upper left corner, i.e.
  515.         xx = x_pixels_per_inch/72, xy = 0,
  516.         yx = 0, yy = -y_pixels_per_inch/72,
  517.         tx = 0, ty = height.
  518.  
  519. int (*sync_output)(P1(gx_device *)) [OPTIONAL]
  520.  
  521.     Synchronize the device.  If any output to the device has been
  522. buffered, send / write it now.  Note that this may be called several times
  523. in the process of constructing a page, so printer drivers should NOT
  524. implement this by printing the page.  The default implementation does
  525. nothing.
  526.  
  527. int (*output_page)(P3(gx_device *, int num_copies, int flush)) [OPTIONAL]
  528.  
  529.     Output a fully composed page to the device.  The num_copies
  530. argument is the number of copies that should be produced for a hardcopy
  531. device.  (This may be ignored if the driver has some other way to specify
  532. the number of copies.)  The flush argument is true for showpage, false for
  533. copypage.  The default definition just calls sync_output.  Printer drivers
  534. should implement this by printing and ejecting the page.
  535.  
  536. int (*close_device)(P1(gx_device *)) [OPTIONAL]
  537.  
  538.     Close the device: release any associated resources.  After this,
  539. output to the device is no longer allowed.  The default implementation
  540. does nothing.
  541.  
  542. Color/alpha mapping
  543. -------------------
  544.  
  545. A given driver normally will implement either map_rgb_color or
  546. map_cmyk_color, but not both.  Black-and-white drivers do not need to
  547. implement either one.
  548.  
  549. gx_color_index (*map_rgb_color)(P4(gx_device *, gx_color_value red,
  550.   gx_color_value green, gx_color_value blue)) [OPTIONAL]
  551.  
  552.     Map a RGB color to a device color.  The range of legal values of
  553. the RGB arguments is 0 to gx_max_color_value.  The default algorithm uses
  554. the map_cmyk_color procedure if the driver supplies one, otherwise returns
  555. 1 if any of the values exceeds gx_max_color_value/2, 0 otherwise.
  556.  
  557.     Ghostscript assumes that for devices that have color capability
  558. (i.e., color_info.num_components > 1), map_rgb_color returns a color index
  559. for a gray level (as opposed to a non-gray color) iff red = green = blue.
  560.  
  561. gx_color_index (*map_cmyk_color)(P5(gx_device *, gx_color_value cyan,
  562.   gx_color_value magenta, gx_color_value yellow, gx_color_value black))
  563.   [OPTIONAL]
  564.  
  565.     Map a CMYK color to a device color.  The range of legal values of
  566. the CMYK arguments is 0 to gx_max_color_value.  The default algorithm
  567. calls the map_rgb_color procedure, with suitably transformed arguments.
  568.  
  569.     Ghostscript assumes that for devices that have color capability
  570. (i.e., color_info.num_components > 1), map_cmyk_color returns a color
  571. index for a gray level (as opposed to a non-gray color) iff cyan = magenta
  572. = yellow.
  573.  
  574. int (*map_color_rgb)(P3(gx_device *, gx_color_index color,
  575.   gx_color_value rgb[3])) [OPTIONAL]
  576.  
  577.     Map a device color code to RGB values.  The default algorithm
  578. returns (0 if color==0 else gx_max_color_value) for all three components.
  579.  
  580. gx_color_index (*map_rgb_alpha_color)(P5(gx_device *, gx_color_value red,
  581.   gx_color_value green, gx_color_value blue, gx_color_value alpha)) [OPTIONAL]
  582.  
  583.     Map a RGB color and an opacity value to a device color.  The range
  584. of legal values of the RGB and alpha arguments is 0 to gx_max_color_value;
  585. alpha = 0 means transparent, alpha = gx_max_color_value means fully
  586. opaque.  The default is to use the map_rgb_color procedure and ignore
  587. alpha.
  588.  
  589.     Note that if a driver implements map_rgb_alpha_color, it must also
  590. implement map_rgb_color, and must implement them in such a way that
  591. map_rgb_alpha_color(dev, r, g, b, gx_max_color_value) returns the same
  592. value as map_rgb_color(dev, r, g, b).
  593.  
  594.     Note that there is no map_cmyk_alpha_color procedure.  CMYK
  595. devices currently do not support variable opacity; alpha is ignored on
  596. such devices.
  597.  
  598. typedef enum { go_text, go_graphics } graphic_object_type;
  599. int (*get_alpha_bits)(P4(gx_device *dev, graphic_object_type type)) [OPTIONAL]
  600.  
  601.     Return the number of alpha (opacity) bits that should be used in
  602. rendering an object of the given type.  The default value is 1.
  603.  
  604. Drawing
  605. -------
  606.  
  607. All drawing operations use device coordinates and device color values.
  608.  
  609. int (*fill_rectangle)(P6(gx_device *, int x, int y,
  610.   int width, int height, gx_color_index color))
  611.  
  612.     Fill a rectangle with a color.  The set of pixels filled is
  613. {(px,py) | x <= px < x + width and y <= py < y + height}.  In other words,
  614. the point (x,y) is included in the rectangle, as are (x+w-1,y), (x,y+h-1),
  615. and (x+w-1,y+h-1), but *not* (x+w,y), (x,y+h), or (x+w,y+h).  If width <=
  616. 0 or height <= 0, fill_rectangle should return 0 without drawing anything.
  617.  
  618. int (*draw_line)(P6(gx_device *, int x0, int y0, int x1, int y1,
  619.   gx_color_index color)) [OPTIONAL]
  620.  
  621.     Draw a minimum-thickness line from (x0,y0) to (x1,y1).  The
  622. precise set of points to be filled is defined as follows.  First, if y1 <
  623. y0, swap (x0,y0) and (x1,y1).  Then the line includes the point (x0,y0)
  624. but not the point (x1,y1).  If x0=x1 and y0=y1, draw_line should return 0
  625. without drawing anything.
  626.  
  627. Bitmap imaging
  628. --------------
  629.  
  630. Bitmap (or pixmap) images are stored in memory in a nearly standard way.
  631. The first byte corresponds to (0,0) in the image coordinate system: bits
  632. (or polybit color values) are packed into it left-to-right.  There may be
  633. padding at the end of each scan line: the distance from one scan line to
  634. the next is always passed as an explicit argument.
  635.  
  636. int (*copy_mono)(P11(gx_device *, const unsigned char *data, int data_x,
  637.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  638.   gx_color_index color0, gx_color_index color1))
  639.  
  640.     Copy a monochrome image (similar to the PostScript image
  641. operator).  Each scan line is raster bytes wide.  Copying begins at
  642. (data_x,0) and transfers a rectangle of the given width at height to the
  643. device at device coordinate (x,y).  (If the transfer should start at some
  644. non-zero y value in the data, the caller can adjust the data address by
  645. the appropriate multiple of the raster.)  The copying operation writes
  646. device color color0 at each 0-bit, and color1 at each 1-bit: if color0 or
  647. color1 is gx_no_color_index, the device pixel is unaffected if the image
  648. bit is 0 or 1 respectively.  If id is different from gx_no_bitmap_id, it
  649. identifies the bitmap contents unambiguously; a call with the same id will
  650. always have the same data, raster, and data contents.
  651.  
  652.     This operation, with color0 = gx_no_color_index, is the workhorse
  653. for text display in Ghostscript, so implementing it efficiently is very
  654. important.
  655.  
  656. int (*tile_rectangle)(P10(gx_device *, const gx_bitmap *tile,
  657.   int x, int y, int width, int height,
  658.   gx_color_index color0, gx_color_index color1,
  659.   int phase_x, int phase_y)) [OPTIONAL]
  660.  
  661.     Tile a rectangle.  Tiling consists of doing multiple copy_mono
  662. operations to fill the rectangle with copies of the tile.  The tiles are
  663. aligned with the device coordinate system, to avoid "seams".
  664. Specifically, the (phase_x, phase_y) point of the tile is aligned with the
  665. origin of the device coordinate system.  (Note that this is backwards from
  666. the PostScript definition of halftone phase.)  phase_x and phase_y are
  667. guaranteed to be in the range [0..tile->width) and [0..tile->height)
  668. respectively.
  669.  
  670.     If color0 and color1 are both gx_no_color_index, then the tile is
  671. a color pixmap, not a bitmap: see the next section.
  672.  
  673.     This operation is the workhorse for halftone filling in
  674. Ghostscript, so implementing it efficiently for solid tiles (where either
  675. color0 and color1 are both gx_no_color_index, for colored halftones, or
  676. neither one is gx_no_color_index, for monochrome halftones) is very
  677. important.
  678.  
  679. Pixmap imaging
  680. --------------
  681.  
  682. Pixmaps are just like bitmaps, except that each pixel occupies more than
  683. one bit.  All the bits for each pixel are grouped together (this is
  684. sometimes called "chunky" or "Z" format).  For copy_color, the number of
  685. bits per pixel is given by the color_info.depth parameter in the device
  686. structure: the legal values are 1, 2, 4, 8, 16, 24, or 32.  The pixel
  687. values are device color codes (i.e., whatever it is that map_rgb_color
  688. returns).
  689.  
  690. int (*copy_color)(P9(gx_device *, const unsigned char *data, int data_x,
  691.   int raster, gx_bitmap_id id, int x, int y, int width, int height))
  692.  
  693.     Copy a color image with multiple bits per pixel.  The raster is in
  694. bytes, but x and width are in pixels, not bits.  If the device doesn't
  695. actually support color, this is OPTIONAL; the default is equivalent to
  696. copy_mono with color0 = 0 and color1 = 1.  If id is different from
  697. gx_no_bitmap_id, it identifies the bitmap contents unambiguously; a call
  698. with the same id will always have the same data, raster, and data
  699. contents.
  700.  
  701. We do not provide a separate procedure for tiling with a pixmap; instead,
  702. tile_rectangle can also take colored tiles.  This is indicated by the
  703. color0 and color1 arguments both being gx_no_color_index.  In this case,
  704. as for copy_color, the raster and height in the "bitmap" are interpreted
  705. as for real bitmaps, but the x and width are in pixels, not bits.
  706.  
  707. int (*copy_alpha)(P11(gx_device *dev, const unsigned char *data, int data_x,
  708.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  709.   gx_color_index color, int depth)) [OPTIONAL]
  710.  
  711.     Fill a given region with a given color modified by an individual
  712. alpha value for each pixel.  depth, the number of bits per pixel, is
  713. either 2 or 4, and in any case is always a value returned by a previous
  714. call on the get_alpha_bits procedure.  Note that if get_alpha_bits always
  715. returns 1, this procedure will never be called.
  716.  
  717. Reading bits back
  718. -----------------
  719.  
  720. int (*get_bits)(P4(gx_device *, int y, byte *str, byte **actual_data))
  721.   [OPTIONAL]
  722.  
  723.     Read one scan line of bits back from the device into the area
  724. starting at str, namely, scan line y.  If the bits cannot be read back
  725. (e.g., from a printer), return -1; otherwise return 0.  The contents of
  726. the bits beyond the last valid bit in the scan line (as defined by the
  727. device width) are unpredictable.
  728.  
  729.     If actual_data is NULL, the bits are always returned at str.  If
  730. actual_data is not NULL, get_bits may either copy the bits to str and set
  731. *actual_data = str, or it may leave the bits where they are and return a
  732. pointer to them in *actual_data.  In the latter case, the bits are
  733. guaranteed to start on a 32-bit boundary and to be padded to a multiple of
  734. 32 bits; also in this case, the bits are not guaranteed to still be there
  735. after the next call on get_bits.
  736.  
  737. Parameters
  738. ----------
  739.  
  740. Devices may have an open-ended set of parameters, which are simply pairs
  741. consisting of a name and a value.  The value may be of various types:
  742. integer (int or long), boolean, float, string, name, null, array of integer,
  743. or array of float.  For example, the Name of a device is a string; the
  744. Margins of a device is an array of 2 floats.  See gsparam.h for more
  745. details.
  746.  
  747. If a device has parameters other than the ones applicable to all devices
  748. (or, in the case of printer devices, all printer devices), it must provide
  749. get_params and put_params procedures.  If your device has parameters beyond
  750. those of a straightforward display or printer, we strongly advise using the
  751. _get_params and _put_params procedures in an existing device (for example,
  752. gdevcdj.c or gdevbit.c) as a model for your own code.
  753.  
  754. int (*get_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  755.  
  756.     Read the parameters of the device into the parameter list at plist,
  757. using the param_write_* macros/procedures defined in gsparam.h.
  758.     
  759. int (*put_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  760.  
  761.     Set the parameters of the device from the parameter list at plist,
  762. using the param_read_* macros/procedures defined in gsparam.h.  All
  763. put_params procedures must use a "two-phase commit" algorithm; see gsparam.h
  764. for details.
  765.  
  766. External fonts
  767. --------------
  768.  
  769. Drivers may include the ability to display text.  More precisely, they may
  770. supply a set of procedures that in turn implement some font and text
  771. handling capabilities.  These procedures are documented in another file,
  772. xfonts.doc.  The link between the two is the driver procedure that
  773. supplies the font/text procedures:
  774.  
  775. xfont_procs *(*get_xfont_procs)(P1(gx_device *dev)) [OPTIONAL]
  776.  
  777.     Return a structure of procedures for handling external fonts and
  778. text display.  A NULL value means that this driver doesn't provide this
  779. capability.
  780.  
  781. For technical reasons, a second procedure is also needed:
  782.  
  783. gx_device *(*get_xfont_device)(P1(gx_device *dev)) [OPTIONAL]
  784.  
  785.     Return the device that implements get_xfont_procs in a non-default
  786. way for this device, if any.  Except for certain special internal devices,
  787. this is always the device argument.
  788.  
  789. Page devices
  790. ------------
  791.  
  792. gx_device (*get_page_device)(P1(gx_device *dev)) [OPTIONAL]
  793.  
  794.     According to the Adobe specifications, some devices are "page
  795. devices" and some are not.  This procedure returns NULL if the device is
  796. not a page device, or the device itself if it is a page device.  In the
  797. case of forwarding devices, get_page_device returns the underlying page
  798. device (or NULL if the underlying device is not a page device).
  799.