home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / gs403osk.tgz / gs403osk.tar / drivers.txt < prev    next >
Text File  |  1996-10-17  |  58KB  |  1,347 lines

  1.    Copyright (C) 1989, 1996 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.  
  20. This file, drivers.txt, describes the interface between Ghostscript and
  21. device drivers.
  22.  
  23. For an overview of Ghostscript and a list of the documentation files, see
  24. README.
  25.  
  26. ********
  27. ******** Adding a driver ********
  28. ********
  29.  
  30. To add a driver to Ghostscript, all you need to do is edit devs.mak in
  31. two places.  The first is the list of devices, in the section headed
  32.  
  33. # -------------------------------- Catalog ------------------------------- #
  34.  
  35. Pick a name for your device, say smurf, and add smurf to the list.
  36. (Device names must be 1 to 8 characters, consisting of only letters,
  37. digits, and underscores, of which the first character must be a letter.
  38. Case is significant: all current device names are lower case.)
  39. The second is the section headed
  40.  
  41. # ---------------------------- Device drivers ---------------------------- #
  42.  
  43. Suppose the files containing the smurf driver are called joe and fred.
  44. Then you should add the following lines:
  45.  
  46. # ------ The SMURF device ------ #
  47.  
  48. smurf_=joe.$(OBJ) fred.$(OBJ)
  49. smurf.dev: $(smurf_)
  50.     $(SETDEV) smurf $(smurf_)
  51.  
  52. joe.$(OBJ): joe.c ...and whatever it depends on
  53.  
  54. fred.$(OBJ): fred.c ...and whatever it depends on
  55.  
  56. If the smurf driver also needs special libraries, e.g., a library named
  57. gorf, then the entry should look like this:
  58.  
  59. smurf.dev: $(smurf_)
  60.     $(SETDEV) smurf $(smurf_)
  61.     $(ADDMOD) smurf -lib gorf
  62.  
  63. If, as will usually be the case, your driver is a printer driver (as
  64. discussed below), the device entry should look like this:
  65.  
  66. smurf.dev: $(smurf_) page.dev
  67.     $(SETPDEV) smurf $(smurf_)
  68.  
  69. or
  70.  
  71. smurf.dev: $(smurf_) page.dev
  72.     $(SETPDEV) smurf $(smurf_)
  73.     $(ADDMOD) smurf -lib gorf
  74.  
  75. ********
  76. ******** Keeping things simple
  77. ********
  78.  
  79. If you want to add a simple device (specifically, a black-and-white
  80. printer), you probably don't need to read the rest of this document; just
  81. use the code in an existing driver as a guide.  The Epson and BubbleJet
  82. drivers (gdevepsn.c and gdevbj10.c) are good models for dot-matrix
  83. printers, which require presenting the data for many scan lines at once;
  84. the DeskJet/LaserJet drivers (gdevdjet.c) are good models for laser
  85. printers, which take a single scan line at a time but support data
  86. compression.  (For color printers, there are unfortunately no good models:
  87. the two major color inkjet printer drivers, gdevcdj.c and gdevstc.c, are
  88. far too complex to read.)
  89.  
  90. On the other hand, if you're writing a driver for some more esoteric
  91. device, you probably do need at least some of the information in the rest
  92. of this document.  It might be a good idea for you to read it in
  93. conjunction with one of the existing drivers.
  94.  
  95. Duplication of code, and sheer code volume, is a serious maintenance and
  96. distribution problem for Ghostscript.  If your device is similar to an
  97. existing one, try to implement your driver by adding some parameterization
  98. to an existing driver rather than copying code.  gdevepsn.c and gdevdjet.c
  99. are good examples of this approach.
  100.  
  101. ********
  102. ******** Driver structure ********
  103. ********
  104.  
  105. A device is represented by a structure divided into three parts:
  106.  
  107.     - procedures that are shared by all instances of each device;
  108.  
  109.     - parameters that are present in all devices but may be different
  110.       for each device or instance; and
  111.  
  112.     - device-specific parameters that may be different for each instance.
  113.  
  114. Normally, the procedure structure is defined and initialized at compile
  115. time.  A prototype of the parameter structure (including both generic and
  116. device-specific parameters) is defined and initialized at compile time, but
  117. is copied and filled in when an instance of the device is created.  Both of
  118. these structures should be declared as const, but for backward
  119. compatibility reasons are not.
  120.  
  121. The gx_device_common macro defines the common structure elements, with the
  122. intent that devices define and export a structure along the following
  123. lines.  Do not fill in the individual generic parameter values in the usual
  124. way for C structures: use the macros defined for this purpose in gxdevice.h
  125. or, if applicable, gdevprn.h.
  126.  
  127.     typedef struct smurf_device_s {
  128.         gx_device_common;
  129.         ... device-specific parameters ...
  130.     } smurf_device;
  131.     smurf_device far_data gs_smurf_device = {
  132.         ... macro for generic parameter values ...,
  133.         { ... procedures ... },     /* std_procs */
  134.         ... device-specific parameter values if any ...
  135.     };
  136.  
  137. The device structure instance *must* have the name gs_smurf_device, where
  138. smurf is the device name used in devs.mak.
  139.  
  140. All the device procedures are called with the device as the first argument.
  141. Since each device type is actually a different structure type, the device
  142. procedures must be declared as taking a gx_device * as their first
  143. argument, and must cast it to smurf_device * internally.  For example, in
  144. the code for the "memory" device, the first argument to all routines is
  145. called dev, but the routines actually use mdev to reference elements of the
  146. full structure, by virtue of the definition
  147.  
  148.     #define mdev ((gx_device_memory *)dev)
  149.  
  150. (This is a cheap version of "object-oriented" programming: in C++, for
  151. example, the cast would be unnecessary, and in fact the procedure table
  152. would be constructed by the compiler.)
  153.  
  154. Structure definition
  155. --------------------
  156.  
  157. You should consult the definition of struct gx_device_s in gxdevice.h for
  158. the complete details of the generic device structure.  Some of the most
  159. important members of this structure for ordinary drivers are:
  160.  
  161.     const char *dname;      /* the device name */
  162.     bool is_open;           /* true if device has been opened */
  163.     gx_device_color_info color_info;    /* color information */
  164.     int width;          /* width in pixels */
  165.     int height;         /* height in pixels */
  166.  
  167. The name in the structure (dname) should be the same as the name in
  168. devs.mak.
  169.  
  170. gx_device_common is a macro consisting of just the element definitions.
  171.  
  172. For sophisticated developers only
  173. ---------------------------------
  174.  
  175. If for any reason you need to change the definition of the basic device
  176. structure, or add procedures, you must change the following places:
  177.  
  178.     - This document and NEWS (if you want to keep the
  179.         documentation up to date).
  180.     - The definition of gx_device_common and/or the procedures
  181.         in gxdevice.h.
  182.     - Possibly, the default forwarding procedures declared in gxdevice.h
  183.         and implemented in gdevnfwd.c.
  184.     - The device procedure record completion routines in gdevdflt.c.
  185.     - Possibly, the default device implementation in gdevdflt.c and
  186.         gdevddrw.c.
  187.     - If you are adding procedures that produce output, the bounding box
  188.         device in gdevbbox.c.
  189.     - The following devices that must have complete (non-defaulted)
  190.         procedure vectors:
  191.         - The null device in gdevnfwd.c.
  192.         - The command list "device" in gxclist.c.  This is
  193.             not an actual device; it only defines procedures.
  194.         - The "memory" devices in gdevmem.h and gdevm*.c.
  195.         - The halftoning device in gdevht.c.
  196.     - The clip list accumulation "device" in gxacpath.c.
  197.     - The clipping "devices" in gxcpath.c and gxclip2.c.
  198.     - The Pattern accumulation "device" in gxpcmap.c.
  199.     - The generic printer device macros in gdevprn.h.
  200.     - The generic printer device code in gdevprn.c.
  201.  
  202. You may also have to change the code for gx_default_get_params and/or
  203. gx_default_put_params (in gsdparam.c).
  204.  
  205. You should not have to change any of the real devices in the standard
  206. Ghostscript distribution (listed in devs.mak) or any of your own devices,
  207. because all of them are supposed to use the macros in gxdevice.h or
  208. gdevprn.h to define and initialize their state.
  209.  
  210. ********
  211. ******** Types and coordinates ********
  212. ********
  213.  
  214. Coordinate system
  215. -----------------
  216.  
  217. Since each driver specifies the initial transformation from user to device
  218. coordinates, the driver can use any coordinate system it wants, as long as a
  219. device coordinate will fit in an int.  (This is only an issue on MS-DOS
  220. systems, where ints are only 16 bits.  User coordinates are represented as
  221. floats.)  Most current drivers use a coordinate system with (0,0) in the
  222. upper left corner, with X increasing to the right and Y increasing toward
  223. the bottom.  However, there is supposed to be nothing in the rest of
  224. Ghostscript that assumes this, and indeed some drivers use a coordinate
  225. system with (0,0) in the lower left corner.
  226.  
  227. Drivers must check (and, if necessary, clip) the coordinate parameters
  228. given to them: they should not assume the coordinates will be in bounds.
  229. The fit_fill and fit_copy macros in gxdevice.h are very helpful in doing
  230. this.
  231.  
  232. Color definition
  233. ----------------
  234.  
  235. Ghostscript represents colors internally as RGB or CMYK values.  In
  236. communicating with devices, however, it assumes that each device has a
  237. palette of colors identified by integers (to be precise, elements of type
  238. gx_color_index).  Drivers may provide a uniformly spaced gray ramp or
  239. color cube for halftoning, or they may do their own color approximation,
  240. or both.
  241.  
  242. The color_info member of the device structure defines the color and
  243. gray-scale capabilities of the device.  Its type is defined as follows:
  244.  
  245. typedef struct gx_device_color_info_s {
  246.     int num_components;     /* 1 = gray only, 3 = RGB, */
  247.                     /* 4 = CMYK */
  248.     int depth;          /* # of bits per pixel */
  249.     gx_color_value max_gray;    /* # of distinct gray levels -1 */
  250.     gx_color_value max_rgb;     /* # of distinct color levels -1 */
  251.                     /* (only relevant if num_comp. > 1) */
  252.     gx_color_value dither_gray; /* size of gray ramp for halftoning */
  253.     gx_color_value dither_rgb;  /* size of color cube ditto */
  254.                     /* (only relevant if num_comp. > 1) */
  255. } gx_device_color_info;
  256.  
  257. The following macros (in gxdevice.h) provide convenient shorthands for
  258. initializing this structure for ordinary black-and-white or color devices:
  259.  
  260. #define dci_black_and_white ...
  261. #define dci_color(depth,maxv,dither) ...
  262.  
  263. The idea is that a device has a certain number of gray levels (max_gray +1)
  264. and a certain number of colors (max_rgb +1) that it can produce directly.
  265. When Ghostscript wants to render a given RGB or CMYK color as a device
  266. color, it first tests whether the color is a gray level.  (If
  267. num_components is 1, it converts all colors to gray levels.)  If so:
  268.  
  269.     - If max_gray is large (>= 31), Ghostscript asks the device to
  270. approximate the gray level directly.  If the device returns a valid
  271. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  272. the device can represent dither_gray distinct gray levels, equally spaced
  273. along the diagonal of the color cube, and uses the two nearest ones to the
  274. desired color for halftoning.
  275.  
  276. If the color is not a gray level:
  277.  
  278.     - If max_rgb is large (>= 31), Ghostscript asks the device to
  279. approximate the color directly.  If the device returns a valid
  280. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  281. the device can represent dither_rgb * dither_rgb * dither_rgb distinct
  282. colors, equally spaced throughout the color cube, and uses two of the
  283. nearest ones to the desired color for halftoning.
  284.  
  285. Types
  286. -----
  287.  
  288. Here is a brief explanation of the various types that appear as parameters
  289. or results of the drivers.
  290.  
  291. gx_color_value (defined in gxdevice.h)
  292.  
  293.     This is the type used to represent RGB or CMYK color values.  It is
  294. currently equivalent to unsigned short.  However, Ghostscript may use less
  295. than the full range of the type to represent color values:
  296. gx_color_value_bits is the number of bits actually used, and
  297. gx_max_color_value is the maximum value (equal to 2^gx_max_color_value_bits
  298. - 1).
  299.  
  300. gx_device (defined in gxdevice.h)
  301.  
  302.     This is the device structure, as explained above.
  303.  
  304. gs_matrix (defined in gsmatrix.h)
  305.  
  306.     This is a 2-D homogenous coordinate transformation matrix, used by
  307. many Ghostscript operators.
  308.  
  309. gx_color_index (defined in gxdevice.h)
  310.  
  311.     This is meant to be whatever the driver uses to represent a device
  312. color.  For example, it might be an index in a color map, or it might be
  313. R, G, and B values packed into a single integer.  Ghostscript doesn't ever
  314. do any computations with gx_color_index values: it gets them from
  315. map_rgb_color or map_cmyk_color and hands them back as arguments to
  316. several other procedures.  The special value gx_no_color_index (defined as
  317. (gx_color_index)(-1)) means "transparent" for some of the procedures.  The
  318. type definition is simply:
  319.  
  320.     typedef unsigned long gx_color_index;
  321.  
  322. gs_param_list (defined in gsparam.h)
  323.  
  324.     This is a parameter list, which is used to read and set attributes
  325. in a device.  See the comments in gsparam.h, and the description of the
  326. get_params and put_params procedures below, for more detail.
  327.  
  328. gx_tile_bitmap (defined in gxbitmap.h)
  329. gx_strip_bitmap (defined in gxbitmap.h)
  330.  
  331.     These structure types represent bitmaps to be used as a tile for
  332. filling a region (rectangle).  gx_tile_bitmap is an older type lacking shift
  333. and rep_shift; gx_strip_bitmap has superseded it, and it should not be used
  334. in new code.  Here is a copy of the relevant part of the file:
  335.  
  336. /*
  337.  * Structure for describing stored bitmaps.
  338.  * Bitmaps are stored bit-big-endian (i.e., the 2^7 bit of the first
  339.  * byte corresponds to x=0), as a sequence of bytes (i.e., you can't
  340.  * do word-oriented operations on them if you're on a little-endian
  341.  * platform like the Intel 80x86 or VAX).  Each scan line must start on
  342.  * a (32-bit) word boundary, and hence is padded to a word boundary,
  343.  * although this should rarely be of concern, since the raster and width
  344.  * are specified individually.  The first scan line corresponds to y=0
  345.  * in whatever coordinate system is relevant.
  346.  *
  347.  * For bitmaps used as halftone tiles, we may replicate the tile in
  348.  * X and/or Y, but it is still valuable to know the true tile dimensions
  349.  * (i.e., the dimensions prior to replication).  Requirements:
  350.  *  width % rep_width = 0
  351.  *  height % rep_height = 0
  352.  *
  353.  * For halftones at arbitrary angles, we provide for storing the halftone
  354.  * data as a strip that must be shifted in X for different values of Y.
  355.  * For an ordinary (non-shifted) halftone that has a repetition width of
  356.  * W and a repetition height of H, the pixel at coordinate (X,Y)
  357.  * corresponds to halftone pixel (X mod W, Y mod H), ignoring phase;
  358.  * for a shifted halftone with shift S, the pixel at (X,Y) corresponds
  359.  * to halftone pixel ((X + S * floor(Y/H)) mod W, Y mod H).  Requirements:
  360.  *  strip_shift < rep_width
  361.  *  strip_height % rep_height = 0
  362.  *  shift = (strip_shift * (size.y / strip_height)) % rep_width
  363.  */
  364. typedef struct gx_strip_bitmap_s {
  365.     byte *data;
  366.     int raster;         /* bytes per scan line */
  367.     gs_int_point size;      /* width, height */
  368.     gx_bitmap_id id;
  369.     ushort rep_width, rep_height;   /* true size of tile */
  370.     ushort strip_height;
  371.     ushort strip_shift;
  372.     ushort shift;
  373. } gx_strip_bitmap;
  374.  
  375. ********
  376. ******** Coding conventions ********
  377. ********
  378.  
  379. All the driver procedures defined below that return int results return 0 on
  380. success, or an appropriate negative error code in the case of error
  381. conditions.  The error codes are defined in gserrors.h; they correspond
  382. directly to the errors defined in the PostScript language reference
  383. manuals.  The most common ones for drivers are:
  384.  
  385.     gs_error_invalidfileaccess
  386.         An attempt to open a file failed.
  387.  
  388.     gs_error_ioerror
  389.         An error occurred in reading or writing a file.
  390.  
  391.     gs_error_limitcheck
  392.         An otherwise valid parameter value was too large for
  393.         the implementation.
  394.  
  395.     gs_error_rangecheck
  396.         A parameter was outside the valid range.
  397.  
  398.     gs_error_VMerror
  399.         An attempt to allocate memory failed.  (If this
  400.         happens, the procedure should release all memory it
  401.         allocated before it returns.)
  402.  
  403. If a driver does return an error, it should use the return_error
  404. macro rather than a simple return statement, e.g.,
  405.  
  406.     return_error(gs_error_VMerror);
  407.  
  408. This macro is defined in gx.h, which is automatically included by
  409. gdevprn.h but not by gserrors.h.
  410.  
  411. Allocating storage
  412. ------------------
  413.  
  414. While most drivers (especially printer drivers) follow a very similar
  415. template, there is one important coding convention that is not obvious from
  416. reading the code for existing drivers: Driver procedures must not use
  417. malloc to allocate any storage that stays around after the procedure
  418. returns.  Instead, they must use gs_malloc and gs_free, which have slightly
  419. different calling conventions.  (The prototypes for these are in
  420. gsmemory.h, which is included in gx.h, which is included in gdevprn.h.)
  421. This is necessary so that Ghostscript can clean up all allocated memory
  422. before exiting, which is essential in environments that provide only
  423. single-address-space multi-tasking (some versions of Microsoft Windows).
  424.  
  425. char *gs_malloc(uint num_elements, uint element_size,
  426.   const char *client_name);
  427.  
  428.     Like calloc, but unlike malloc, gs_malloc takes an element count
  429. and an element size.  For structures, num_elements is 1 and element_size is
  430. sizeof the structure; for byte arrays, num_elements is the number of bytes
  431. and element_size is 1.  Unlike calloc, gs_malloc does NOT clear the block
  432. of storage.
  433.  
  434.     The client_name is used for tracing and debugging.  It must be a
  435. real string, not NULL.  Normally it is the name of the procedure in which
  436. the call occurs.
  437.  
  438. void gs_free(char *data, uint num_elements, uint element_size,
  439.   const char *client_name);
  440.  
  441.     Unlike free, gs_free demands that num_elements and element_size be
  442. supplied.  It also requires a client name, like gs_malloc.
  443.  
  444. Driver instance allocation
  445. --------------------------
  446.  
  447. All driver instances allocated by Ghostscript's standard allocator must
  448. point to a "structure descriptor" that tells the garbage collector how to
  449. trace pointers in the structure.  For drivers that are registered in the
  450. normal way (using the makefile approach described above), no special care
  451. is needed as long as instances are only created by calling the
  452. gs_copydevice procedure defined in gsdevice.h.  If you have a need to
  453. define devices that are not registered in this way, you must fill in the
  454. stype member in any dynamically allocated instances with a pointer to the
  455. same structure descriptor used to allocate the instance.  For more
  456. information about structure descriptors, see gsmemory.h and gsstruct.h.
  457.  
  458. ********
  459. ******** Printer drivers ********
  460. ********
  461.  
  462. Printer drivers (which include drivers that write some kind of raster file)
  463. are especially simple to implement.  Of the driver procedures defined in the
  464. next section, they only need implement two: map_rgb_color (or
  465. map_cmyk_color) and map_color_rgb.  In addition, they must implement a
  466. print_page or print_page_copies procedure.  There are a set of macros in
  467. gdevprn.h that generate the device structure for such devices, of which the
  468. simplest is prn_device; for an example, see gdevbj10.c.  If you are writing
  469. a printer driver, we suggest you start by reading gdevprn.h and the
  470. subsection on "Color mapping" below; you may be able to ignore all the rest
  471. of the driver procedures.
  472.  
  473. The print_page procedures are defined as follows:
  474.  
  475. int (*print_page)(P2(gx_device_printer *, FILE *))
  476. int (*print_page_copies)(P3(gx_device_printer *, FILE *, int))
  477.  
  478.     This procedure must read out the rendered image from the device and
  479. write whatever is appropriate to the file.  To read back one or more scan
  480. lines of the image, the print_page procedure must call one of the following
  481. procedures:
  482.  
  483.     int gdev_prn_copy_scan_lines(P4(gx_device_printer *pdev, int y, byte *str,
  484.       uint size)
  485.  
  486. For this procedure, str is where the data should be copied to, and size is
  487. the size of the buffer starting at str.  This procedure returns the number
  488. of scan lines copied, or <0 for an error.  str need not be aligned.
  489.  
  490.     int gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str,
  491.       byte **actual_data)
  492.  
  493. This procedure reads out exactly one scan line.  If the scan line is
  494. available in the correct format already, *actual_data is set to point to it;
  495. otherwise, the scan line is copied to the buffer starting at str, and
  496. *actual_data is set to str.  This saves a copying step most of the time.
  497. str need not be aligned; however, if *actual_data is set to point to an
  498. existing scan line, it will be aligned.  (See the description of the
  499. get_bits procedure below for more details.)
  500.  
  501. In either case, each row of the image is stored in the form described
  502. in the comment under gx_tile_bitmap above; each pixel takes the
  503. number of bits specified as color_info.depth in the device structure,
  504. and holds values returned by the device's map_{rgb,cmyk}_color
  505. procedure.
  506.  
  507. The print_page procedure can determine the number of bytes required to hold
  508. a scan line by calling:
  509.  
  510.     uint gdev_prn_raster(P1(gx_device_printer *))
  511.  
  512. For a very simple concrete example, we suggest reading the code in
  513. bit_print_page in gdevbit.c.
  514.  
  515. If the device provides print_page, Ghostscript will call print_page the
  516. requisite number of times to print the desired number of copies; if the
  517. device provides print_page_copies, Ghostscript will call print_page_copies
  518. once per page, passing it the desired number of copies.
  519.  
  520. ********
  521. ******** Driver procedures ********
  522. ********
  523.  
  524. Most of the procedures that a driver may implement are optional.  If a
  525. device doesn't supply an optional procedure <proc>, the entry in the
  526. procedure structure may be either gx_default_<proc>, e.g.
  527. gx_default_tile_rectangle, or NULL or 0.  (The device procedure must also
  528. call the gx_default_ procedure if it doesn't implement the function for
  529. particular values of the arguments.)  Since C compilers supply 0 as the
  530. value for omitted structure elements, this convention means that
  531. statically initialized procedure structures will continue to work even if
  532. new (optional) members are added.
  533.  
  534. Life cycle
  535. ----------
  536.  
  537. A device instance start out life in a closed state.  In this state, no
  538. output operations will occur.  Only the following procedures may be called:
  539.     open_device
  540.     get_initial_matrix
  541.     get_params
  542.     put_params
  543.  
  544. When setdevice installs a device instance in the graphics state, it checks
  545. whether the instance is closed or open.  If the instance is closed,
  546. setdevice calls the open routine, and then sets the state to open.
  547.  
  548. There is currently no user-accessible operation to close a device instance.
  549. Device instances are only closed when they are about to be freed, which
  550. occurs in three situations:
  551.  
  552.     - When a 'restore' occurs, if the instance was created since the
  553. corresponding 'save';
  554.  
  555.     - By the garbage collector, if the instance is no longer accessible;
  556.  
  557.     - When Ghostscript exits (terminates).
  558.  
  559. Open/close/sync
  560. ---------------
  561.  
  562. int (*open_device)(P1(gx_device *)) [OPTIONAL]
  563.  
  564.     Open the device: do any initialization associated with making the
  565. device instance valid.  This must be done before any output to the device.
  566. The default implementation does nothing.
  567.  
  568. void (*get_initial_matrix)(P2(gx_device *, gs_matrix *)) [OPTIONAL]
  569.  
  570.     Construct the initial transformation matrix mapping user
  571. coordinates (nominally 1/72" per unit) to device coordinates.  The default
  572. procedure computes this from width, height, and x/y_pixels_per_inch on the
  573. assumption that the origin is in the upper left corner, i.e.
  574.         xx = x_pixels_per_inch/72, xy = 0,
  575.         yx = 0, yy = -y_pixels_per_inch/72,
  576.         tx = 0, ty = height.
  577.  
  578. int (*sync_output)(P1(gx_device *)) [OPTIONAL]
  579.  
  580.     Synchronize the device.  If any output to the device has been
  581. buffered, send / write it now.  Note that this may be called several times
  582. in the process of constructing a page, so printer drivers should NOT
  583. implement this by printing the page.  The default implementation does
  584. nothing.
  585.  
  586. int (*output_page)(P3(gx_device *, int num_copies, int flush)) [OPTIONAL]
  587.  
  588.     Output a fully composed page to the device.  The num_copies
  589. argument is the number of copies that should be produced for a hardcopy
  590. device.  (This may be ignored if the driver has some other way to specify
  591. the number of copies.)  The flush argument is true for showpage, false for
  592. copypage.  The default definition just calls sync_output.  Printer drivers
  593. should implement this by printing and ejecting the page.
  594.  
  595. int (*close_device)(P1(gx_device *)) [OPTIONAL]
  596.  
  597.     Close the device: release any associated resources.  After this,
  598. output to the device is no longer allowed.  The default implementation
  599. does nothing.
  600.  
  601. Color/alpha mapping
  602. -------------------
  603.  
  604. A given driver normally will implement either map_rgb_color or
  605. map_cmyk_color, but not both.  Black-and-white drivers do not need to
  606. implement either one.  Note that the map_xxx_color procedures must not
  607. return gx_no_color_index (all 1's).
  608.  
  609. gx_color_index (*map_rgb_color)(P4(gx_device *, gx_color_value red,
  610.   gx_color_value green, gx_color_value blue)) [OPTIONAL]
  611.  
  612.     Map a RGB color to a device color.  The range of legal values of
  613. the RGB arguments is 0 to gx_max_color_value.  The default algorithm uses
  614. the map_cmyk_color procedure if the driver supplies one, otherwise returns
  615. 1 if any of the values exceeds gx_max_color_value/2, 0 otherwise.
  616.  
  617.     Ghostscript assumes that for devices that have color capability
  618. (i.e., color_info.num_components > 1), map_rgb_color returns a color index
  619. for a gray level (as opposed to a non-gray color) iff red = green = blue.
  620.  
  621. gx_color_index (*map_cmyk_color)(P5(gx_device *, gx_color_value cyan,
  622.   gx_color_value magenta, gx_color_value yellow, gx_color_value black))
  623.   [OPTIONAL]
  624.  
  625.     Map a CMYK color to a device color.  The range of legal values of
  626. the CMYK arguments is 0 to gx_max_color_value.  The default algorithm
  627. calls the map_rgb_color procedure, with suitably transformed arguments.
  628.  
  629.     Ghostscript assumes that for devices that have color capability
  630. (i.e., color_info.num_components > 1), map_cmyk_color returns a color
  631. index for a gray level (as opposed to a non-gray color) iff cyan = magenta
  632. = yellow.
  633.  
  634. int (*map_color_rgb)(P3(gx_device *, gx_color_index color,
  635.   gx_color_value rgb[3])) [OPTIONAL]
  636.  
  637.     Map a device color code to RGB values.  The default algorithm
  638. returns (0 if color==0 else gx_max_color_value) for all three components.
  639.  
  640. gx_color_index (*map_rgb_alpha_color)(P5(gx_device *, gx_color_value red,
  641.   gx_color_value green, gx_color_value blue, gx_color_value alpha)) [OPTIONAL]
  642.  
  643.     Map a RGB color and an opacity value to a device color.  The range
  644. of legal values of the RGB and alpha arguments is 0 to gx_max_color_value;
  645. alpha = 0 means transparent, alpha = gx_max_color_value means fully
  646. opaque.  The default is to use the map_rgb_color procedure and ignore
  647. alpha.
  648.  
  649.     Note that if a driver implements map_rgb_alpha_color, it must also
  650. implement map_rgb_color, and must implement them in such a way that
  651. map_rgb_alpha_color(dev, r, g, b, gx_max_color_value) returns the same
  652. value as map_rgb_color(dev, r, g, b).
  653.  
  654.     Note that there is no map_cmyk_alpha_color procedure.  CMYK
  655. devices currently do not support variable opacity; alpha is ignored on
  656. such devices.
  657.  
  658. typedef enum { go_text, go_graphics } graphic_object_type;
  659. int (*get_alpha_bits)(P4(gx_device *dev, graphic_object_type type)) [OPTIONAL]
  660.  
  661.     Return the number of alpha (opacity) bits that should be used in
  662. rendering an object of the given type.  The default value is 1; the only
  663. values allowed are 1, 2, and 4.
  664.  
  665. Drawing
  666. -------
  667.  
  668. All drawing operations use device coordinates and device color values.
  669.  
  670. int (*fill_rectangle)(P6(gx_device *, int x, int y,
  671.   int width, int height, gx_color_index color))
  672.  
  673.     Fill a rectangle with a color.  The set of pixels filled is
  674. {(px,py) | x <= px < x + width and y <= py < y + height}.  In other words,
  675. the point (x,y) is included in the rectangle, as are (x+w-1,y), (x,y+h-1),
  676. and (x+w-1,y+h-1), but *not* (x+w,y), (x,y+h), or (x+w,y+h).  If width <=
  677. 0 or height <= 0, fill_rectangle should return 0 without drawing anything.
  678.  
  679.     Note that fill_rectangle is the only non-optional procedure in the
  680. driver interface.
  681.  
  682. int (*draw_line)(P6(gx_device *, int x0, int y0, int x1, int y1,
  683.   gx_color_index color)) [OPTIONAL]
  684.  
  685.     Draw a minimum-thickness line from (x0,y0) to (x1,y1).  The
  686. precise set of points to be filled is defined as follows.  First, if y1 <
  687. y0, swap (x0,y0) and (x1,y1).  Then the line includes the point (x0,y0)
  688. but not the point (x1,y1).  If x0=x1 and y0=y1, draw_line should return 0
  689. without drawing anything.
  690.  
  691. Bitmap imaging
  692. --------------
  693.  
  694. Bitmap (or pixmap) images are stored in memory in a nearly standard way.
  695. The first byte corresponds to (0,0) in the image coordinate system: bits
  696. (or polybit color values) are packed into it left-to-right.  There may be
  697. padding at the end of each scan line: the distance from one scan line to
  698. the next is always passed as an explicit argument.
  699.  
  700. int (*copy_mono)(P11(gx_device *, const unsigned char *data, int data_x,
  701.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  702.   gx_color_index color0, gx_color_index color1)) [OPTIONAL]
  703.  
  704.     Copy a monochrome image (similar to the PostScript image
  705. operator).  Each scan line is raster bytes wide.  Copying begins at
  706. (data_x,0) and transfers a rectangle of the given width at height to the
  707. device at device coordinate (x,y).  (If the transfer should start at some
  708. non-zero y value in the data, the caller can adjust the data address by
  709. the appropriate multiple of the raster.)  The copying operation writes
  710. device color color0 at each 0-bit, and color1 at each 1-bit: if color0 or
  711. color1 is gx_no_color_index, the device pixel is unaffected if the image
  712. bit is 0 or 1 respectively.  If id is different from gx_no_bitmap_id, it
  713. identifies the bitmap contents unambiguously; a call with the same id will
  714. always have the same data, raster, and data contents.
  715.  
  716.     This operation, with color0 = gx_no_color_index, is the workhorse
  717. for text display in Ghostscript, so implementing it efficiently is very
  718. important.
  719.  
  720. int (*tile_rectangle)(P10(gx_device *, const gx_tile_bitmap *tile,
  721.   int x, int y, int width, int height,
  722.   gx_color_index color0, gx_color_index color1,
  723.   int phase_x, int phase_y)) [OPTIONAL] [OBSOLETE]
  724.  
  725.     This procedure is still supported, but has been superseded by
  726. strip_tile_rectangle.  New drivers should implement strip_tile_rectangle; if
  727. they cannot cope with non-zero shift values, they should test for this
  728. explicitly and call the default implementation
  729. (gx_default_strip_tile_rectangle) if shift != 0.  Clients should call
  730. strip_tile_rectangle, not tile_rectangle.
  731.  
  732. int (*strip_tile_rectangle)(P10(gx_device *, const gx_strip_bitmap *tile,
  733.   int x, int y, int width, int height,
  734.   gx_color_index color0, gx_color_index color1,
  735.   int phase_x, int phase_y)) [OPTIONAL]
  736.  
  737.     Tile a rectangle.  Tiling consists of doing multiple copy_mono
  738. operations to fill the rectangle with copies of the tile.  The tiles are
  739. aligned with the device coordinate system, to avoid "seams".
  740. Specifically, the (phase_x, phase_y) point of the tile is aligned with the
  741. origin of the device coordinate system.  (Note that this is backwards from
  742. the PostScript definition of halftone phase.)  phase_x and phase_y are
  743. guaranteed to be in the range [0..tile->width) and [0..tile->height)
  744. respectively.
  745.  
  746.     If color0 and color1 are both gx_no_color_index, then the tile is
  747. a color pixmap, not a bitmap: see the next section.
  748.  
  749.     This operation is the workhorse for halftone filling in Ghostscript,
  750. so implementing it efficiently for solid tiles (i.e., where either color0
  751. and color1 are both gx_no_color_index, for colored halftones, or neither one
  752. is gx_no_color_index, for monochrome halftones) is very important.
  753.  
  754. Pixmap imaging
  755. --------------
  756.  
  757. Pixmaps are just like bitmaps, except that each pixel occupies more than
  758. one bit.  All the bits for each pixel are grouped together (this is
  759. sometimes called "chunky" or "Z" format).  For copy_color, the number of
  760. bits per pixel is given by the color_info.depth parameter in the device
  761. structure: the legal values are 1, 2, 4, 8, 16, 24, or 32.  The pixel
  762. values are device color codes (i.e., whatever it is that map_rgb_color
  763. returns).
  764.  
  765. int (*copy_color)(P9(gx_device *, const unsigned char *data, int data_x,
  766.   int raster, gx_bitmap_id id, int x, int y, int width, int height))
  767.   [OPTIONAL]
  768.  
  769.     Copy a color image with multiple bits per pixel.  The raster is in
  770. bytes, but x and width are in pixels, not bits.  If id is different from
  771. gx_no_bitmap_id, it identifies the bitmap contents unambiguously; a call
  772. with the same id will always have the same data, raster, and data contents.
  773.  
  774. We do not provide a separate procedure for tiling with a pixmap; instead,
  775. tile_rectangle can also take colored tiles.  This is indicated by the
  776. color0 and color1 arguments both being gx_no_color_index.  In this case,
  777. as for copy_color, the raster and height in the "bitmap" are interpreted
  778. as for real bitmaps, but the x and width are in pixels, not bits.
  779.  
  780. int (*copy_alpha)(P11(gx_device *dev, const unsigned char *data, int data_x,
  781.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  782.   gx_color_index color, int depth)) [OPTIONAL]
  783.  
  784.     Fill a given region with a given color modified by an individual
  785. alpha value for each pixel.  depth, the number of bits per pixel, is
  786. either 2 or 4, and in any case is always a value returned by a previous
  787. call on the get_alpha_bits procedure.  Note that if get_alpha_bits always
  788. returns 1, this procedure will never be called.
  789.  
  790. Bitmap/pixmap imaging
  791. ---------------------
  792.  
  793. int (*copy_rop)(P15(gx_device *dev,
  794.   const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  795.   const gx_color_index *scolors,
  796.   const gx_tile_bitmap *texture, const gx_color_index *tcolors,
  797.   int x, int y, int width, int height,
  798.   int phase_x, int phase_y, int command)) [OPTIONAL]
  799.  
  800.     This procedure is still supported, but has been superseded by
  801. strip_copy_rop.  New drivers should implement strip_copy_rop; if they cannot
  802. cope with non-zero shift values in the texture, they should test for this
  803. explicitly and call the default implementation (gx_default_strip_copy_rop)
  804. if shift != 0.  Clients should call strip_copy_rop, not copy_rop.
  805.  
  806. int (*strip_copy_rop)(P15(gx_device *dev,
  807.   const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  808.   const gx_color_index *scolors,
  809.   const gx_strip_bitmap *texture, const gx_color_index *tcolors,
  810.   int x, int y, int width, int height,
  811.   int phase_x, int phase_y, int command)) [OPTIONAL]
  812.  
  813.     Combine an optional source image S (as for copy_mono or copy_color)
  814. and an optional texture T (a tile, as for tile_rectangle) with the existing
  815. bitmap or pixmap D held by the driver, pixel by pixel, using any 3-input
  816. Boolean operation as modified by "transparency" flags: schematically, set D
  817. = f(D,S,T), computing f in RGB space rather than using actual device pixel
  818. values.  S and T may each (independently) be a solid color, a bitmap with
  819. "foreground" and "background" colors, or a pixmap.  This is a complex (and
  820. currently rather slow) operation.  The arguments are as follows:
  821.  
  822.     dev - the device, as for all driver procedures
  823.  
  824.     sdata, sourcex, sraster, id, scolors - specify S, see below
  825.  
  826.     texture, tcolors - specify T, see below
  827.  
  828.     x, y, width, height - as for the other copy/fill procedures
  829.  
  830.     phase_x, phase_y - part of T specification, see below
  831.  
  832.     command - see below
  833.  
  834. S specification
  835. ...............
  836.  
  837. As noted above, the source (S) may be a solid color, a bitmap, or a pixmap.
  838.  
  839. If S is a solid color:
  840.  
  841.     - sdata, sourcex, sraster, and id are irrelevant.
  842.  
  843.     - scolors points to two gx_color_index values; scolors[0] =
  844.     scolors[1] = the color.
  845.  
  846. If S is a bitmap:
  847.  
  848.     - sdata, sourcex, sraster, and id arguments are as for copy_mono or
  849.     copy_color (data, data_x, raster, id), and specify a source bitmap.
  850.  
  851.     - scolors points to two gx_color_index values; scolors[0] is the
  852.     background color (the color corresponding to 0-bits in the bitmap),
  853.     scolors[1] is the foreground color (the color corresponding to
  854.     1-bits in the bitmap).
  855.  
  856. If S is a pixmap:
  857.  
  858.     - sdata, sourcex, sraster, and id arguments are as for copy_mono or
  859.     copy_color (data, data_x, raster, id), and specify a source pixmap
  860.     whose depth is the same as the depth of the destination.
  861.  
  862.     - scolors is NULL.
  863.  
  864. Note that if the source is a bitmap with background=0 and foreground=1, and
  865. the destination is 1 bit deep, then the source can be treated as a pixmap
  866. (scolors=NULL).
  867.  
  868. T specification
  869. ...............
  870.  
  871. Similar to the source, the texture (T) may be a solid color, a bitmap, or a
  872. pixmap.
  873.  
  874. If T is a solid color:
  875.  
  876.     - The texture pointer is irrelevant.
  877.  
  878.     - tcolors points to two gx_color_index values; tcolors[0] =
  879.     tcolors[1] = the color.
  880.  
  881. If T is a bitmap:
  882.  
  883.     - The texture argument points to a gx_tile_bitmap, as for the
  884.     tile_rectangle procedure.  Similarly, phase_x and phase_y specify
  885.     the offset of the texture relative to the device coordinate system
  886.     origin, again as for tile_rectangle.  The tile is a bitmap (1 bit
  887.     per pixel).
  888.  
  889.     - tcolors points to two gx_color_index values; tcolors[0] is the
  890.     background color (the color corresponding to 0-bits in the bitmap),
  891.     tcolors[1] is the foreground color (the color corresponding to
  892.     1-bits in the bitmap).
  893.  
  894. If T is a pixmap:
  895.  
  896.     - The texture argument pointer to a gx_tile_bitmap whose depth is
  897.     the same as the depth of the destination.
  898.  
  899.     - tcolors is NULL.
  900.  
  901. Again, if the texture is a bitmap with background=0 and foreground=1, and
  902. the destination depth is 1, the texture bitmap can be treated as a pixmap
  903. (tcolors=NULL).
  904.  
  905. Note that while a source bitmap or pixmap has the same width and height as
  906. the destination, a texture bitmap or pixmap has its own width and height
  907. specified in the gx_tile_bitmap structure, and is replicated and/or clipped
  908. as needed.
  909.  
  910. f specification
  911. ...............
  912.  
  913. Command indicates the raster operation and transparency as follows:
  914.  
  915.     bits 7-0: raster op
  916.     bit 8: 0 if source opaque, 1 if source transparent
  917.     bit 9: 0 if texture opaque, 1 if texture transparent
  918.     bits ?-10: unused, must be 0
  919.  
  920. The raster operation follows the Microsoft and H-P specification.  It is an
  921. 8-element truth table that specifies the output value for each of the
  922. possible 2x2x2 input values as follows:
  923.  
  924.     Bit Texture Source  Destination
  925.     7   1   1   1
  926.     6   1   1   0
  927.     5   1   0   1
  928.     4   1   0   0
  929.     3   0   1   1
  930.     2   0   1   0
  931.     1   0   0   1
  932.     0   0   0   0
  933.  
  934. Transparency affects the output in the following way.  A source or texture
  935. pixel is considered transparent if its value is all 1's (e.g., 1 for
  936. bitmaps, 0xffffff for 24-bit RGB pixmaps) *and* the corresponding
  937. transparency bit is set in the command.  For each pixel, the result of the
  938. Boolean operation is written into the destination iff neither the source nor
  939. the texture pixel is transparent.  (Note that the H-P RasterOp
  940. specification, on which this is based, specifies that if the source and
  941. texture are both all 1's and the command specifies transparent source and
  942. opaque texture, the result *should* be written in the output.  We think this
  943. is an error in the documentation.)
  944.  
  945. Notes
  946. .....
  947.  
  948. Note that copy_rop is defined to operate on pixels in RGB space, again
  949. following the H-P and Microsoft specification.  For devices that don't use
  950. RGB (or gray-scale with black=0, white=all 1's) as their native color
  951. representation, the implementation of copy_rop must convert to RGB or gray
  952. space, do the operation, and convert back (or do the equivalent of this).
  953.  
  954. Here are the copy_rop equivalents of the most important previous imaging
  955. calls.  Note that rop3_S may be replaced by any other Boolean operation.
  956. For monobit devices, we assume that black = 1.  We assume the following
  957. declaration:
  958.     static const gx_color_index white2[2] = { 1, 1 };
  959.  
  960. /* For all devices: */
  961. (*fill_rectangle)(dev, x, y, w, h, color) ==>
  962.  
  963.     { gx_color_index colors[2];
  964.       colors[0] = colors[1] = color;
  965.       (*dev_proc(dev, copy_rop))(dev, NULL, 0, 0, gx_no_bitmap_id, colors,
  966.                      NULL, colors /*irrelevant*/,
  967.                      x, y, w, h, 0, 0, rop3_S);
  968.     }
  969.  
  970. /* For black-and-white devices only: */
  971. (*copy_mono)(dev, base, sourcex, sraster, id,
  972.          x, y, w, h, (gx_color_index)0, (gx_color_index)1) ==>
  973.  
  974.     (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, NULL,
  975.                    NULL, white2 /*irrelevant*/,
  976.                    x, y, w, h, 0, 0, rop3_S);
  977.  
  978. /* For color devices, where neither color0 nor color1 is gx_no_color_index: */
  979. (*copy_mono)(dev, base, sourcex, sraster, id,
  980.          x, y, w, h, color0, color1) ==>
  981.  
  982.     { gx_color_index colors[2];
  983.       colors[0] = color0, colors[1] = color1;
  984.       (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, colors,
  985.                      NULL, white2 /*irrelevant*/,
  986.                      x, y, w, h, 0, 0, rop3_S);
  987.     }
  988.  
  989. /* For black-and-white devices only: */
  990. (*copy_mono)(dev, base, sourcex, sraster, id,
  991.          x, y, w, h, gx_no_color_index, (gx_color_index)1) ==>
  992.  
  993.     (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, NULL,
  994.                    NULL, white2 /*irrelevant*/,
  995.                    x, y, w, h, 0, 0,
  996.                    rop3_S | lop_S_transparent);
  997.  
  998. /* For all devices: */
  999. (*copy_color)(dev, base, sourcex, sraster, id,
  1000.           x, y, w, h) ==> [same as first copy_mono above]
  1001.  
  1002. /* For black-and-white devices only: */
  1003. (*tile_rectangle)(dev, tile, x, y, w, h,
  1004.           (gx_color_index)0, (gx_color_index)1, px, py) ==>
  1005.  
  1006.     (*dev_proc(dev, copy_rop))(dev, NULL, 0, 0, gx_no_bitmap_id,
  1007.                    white2 /*irrelevant*/,
  1008.                    tile, NULL,
  1009.                    x, y, w, h, px, py, rop3_T)
  1010.  
  1011. Polygons
  1012. --------
  1013.  
  1014. In addition to the lowest-level drawing operations that take integer device
  1015. coordinates and pure device colors, the driver interface includes
  1016. higher-level operations that draw polygons using fixed-point coordinates,
  1017. possibly halftoned colors, and possibly a non-default logical operation.
  1018.  
  1019. The fill_* drawing operations all use the center-of-pixel rule: a pixel is
  1020. colored iff its center falls within the polygonal region being filled.  If a
  1021. pixel center (X+0.5,Y+0.5) falls exactly on the boundary, the pixel is
  1022. filled iff the boundary is horizontal and the filled region is above it, or
  1023. the boundary is not horizontal and the filled region is to the right of it.
  1024.  
  1025. int (*fill_trapezoid)(P10(gx_device *dev,
  1026.   fixed fx0, fixed fw0, fixed fy0,
  1027.   fixed fx1, fixed fw1, fixed fh, bool swap_axes,
  1028.   const gx_drawing_color *pdcolor, gs_logical_operation_t lop)) [OPTIONAL]
  1029.  
  1030.     Fill a trapezoid whose parallel sides are parallel to a coordinate
  1031. axis.  The corners are (fx0,fy0), (fx0+fw0,fy0), (fx1,fy0+fh), and
  1032. (fx1+fw1,fy0+fy).  We require fw0 >= 0, fw1 >= 0, and fh >= 0; if fw0 = 0 or
  1033. fw1 = 0, the trapezoid is actually a triangle.  If swap_axes is set, the
  1034. meanings of X and Y are interchanged.
  1035.  
  1036. int (*fill_parallelogram)(P9(gx_device *dev,
  1037.   fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
  1038.   const gx_drawing_color *pdcolor, gs_logical_operation_t lop)) [OPTIONAL]
  1039.  
  1040.     Fill a parallelogram whose corners are (px,py), (px+ax,py+ay),
  1041. (px+bx,py+by), and (px+ax+bx,py+ay+by).  There are no constraints on the
  1042. values of any of the parameters, so the parallelogram may have any
  1043. orientation relative to the coordinate axes.
  1044.  
  1045. int (*fill_triangle)(P9(gx_device *dev,
  1046.   fixed px, fixed py, fixed ax, fixed ay, fixed bx, fixed by,
  1047.   const gx_drawing_color *pdcolor, gs_logical_operation_t lop)) [OPTIONAL]
  1048.  
  1049.     Fill a triangle whose corners are (px,py), (px+ax,py+ay), and
  1050. (px+bx,py+by).
  1051.  
  1052. int (*draw_thin_line)(P7(gx_device *dev,
  1053.   fixed fx0, fixed fy0, fixed fx1, fixed fy1,
  1054.   const gx_drawing_color *pdcolor, gs_logical_operation_t lop)) [OPTIONAL]
  1055.  
  1056.     Draw a one-pixel-wide line from (fx0,fy0) to (fx1,fy1).  This
  1057. replaces the older draw_line procedure, which required integer coordinates,
  1058. a pure color, and no logical operation.
  1059.  
  1060. High-level drawing
  1061. ------------------
  1062.  
  1063. In addition to the low-level drawing operations described above, the driver
  1064. interface provides a set of high-level operations.  Normally these will have
  1065. their default implementation, which converts the high-level operation to the
  1066. low-level ones just described; however, drivers that generate high-level
  1067. output formats such as CGM, or communicate with devices that have firmware
  1068. for higher-level operations such as polygon fills, may implement these
  1069. high-level operations directly.
  1070.  
  1071. For more details, please consult the source code, specifically:
  1072.     gxpaint.h - defines gx_fill_params, gx_stroke_params
  1073.     gxfixed.h - defines fixed, gs_fixed_point (used by gx_*_params)
  1074.     gxistate.h - defines gs_imager_state (used by gx_*_params)
  1075.     gxline.h - defines gx_line_params (used by gs_imager_state)
  1076.     gslparam.h - defines line cap/join values (used by gx_line_params)
  1077.     gxmatrix.h - defines gs_matrix_fixed (used by gs_imager_state)
  1078.     g[s,x,z]path.h - defines gx_path
  1079.     g[x,z]cpath.h - defines gx_clip_path
  1080.  
  1081. int (*fill_path)(P6(gx_device *dev,
  1082.   const gx_imager_state *pis, gx_path *ppath, const gx_fill_params *params,
  1083.   const gx_drawing_color *pdcolor, const gx_clip_path *pcpath)) [OPTIONAL]
  1084.  
  1085.     Fill the given path, clipped by the given clip path, according to
  1086. the given parameters, with the given color.  The clip path pointer may be
  1087. NULL, meaning do not clip.
  1088.  
  1089. int (*stroke_path)(P6(gx_device *dev,
  1090.   const gx_imager_state *pis, gx_path *ppath, const gx_stroke_params *params,
  1091.   const gx_drawing_color *pdcolor, const gx_clip_path *pcpath)) [OPTIONAL]
  1092.  
  1093.     Stroke the given path, clipped by the given clip path, according to
  1094. the given parameters, with the given color.  The clip path pointer may be
  1095. NULL, meaning do not clip.
  1096.  
  1097. int (*fill_mask)(P13(gx_device *dev,
  1098.   const byte *data, int data_x, int raster, gx_bitmap_id id,
  1099.   int x, int y, int width, int height,
  1100.   const gx_drawing_color *pdcolor, int depth,
  1101.   int command, const gx_clip_path *pcpath)) [OPTIONAL]
  1102.  
  1103.     Color the 1-bits in the given mask (or according to the alpha
  1104. values, if depth > 1), clipped by the given clip path, with the given color
  1105. and logical operation.  The clip path pointer may be NULL, meaning do not
  1106. clip.  The parameters data, ..., height are as for copy_mono; depth is as
  1107. for copy_alpha; command is as for copy_rop.
  1108.  
  1109. High-level bitmap imaging
  1110. -------------------------
  1111.  
  1112. Similar to the high-level interface for fill/stroke graphics, a high-level
  1113. interface exists for bitmap images.  All of the procedures in this part of
  1114. the interface are optional; however, if any one of them is defined, they all
  1115. must be.  THIS INTERFACE IS NOT FULLY DEFINED YET, AND THE INFORMATION
  1116. PRESENTED HERE IS SUBJECT TO CHANGE.
  1117.  
  1118. A bitmap image is defined by a gs_image_t structure.  We only summarize this
  1119. structure here.
  1120.  
  1121. typedef struct gs_image_s {
  1122.     int Width;      /* Width of source image in pixels */
  1123.     int Height;     /* Height ditto */
  1124.     gs_matrix ImageMatrix;  /* transformation from 1x1 image space to */
  1125.                 /* user space defined by imager CTM */
  1126.     int BitsPerComponent;
  1127.     const gs_color_space *ColorSpace;
  1128.     float Decode[8];    /* linear remapping of input values */
  1129.     bool Interpolate;   /* smooth image if true */
  1130.     bool ImageMask;     /* true = mask, false = solid image */
  1131.     bool adjust;        /* expand bits if mask */
  1132.     bool CombineWithColor;  /* use drawing color as RasterOp texture */
  1133. } gs_image_t;
  1134. typedef enum {
  1135.     gs_image_format_chunky = 0,
  1136.     gs_image_format_component_planar = 1
  1137. } gs_image_format_t;
  1138.  
  1139. For more details, consult the source code in
  1140.     gsiparam.h - defines parameters for an image
  1141.  
  1142. int (*begin_image)(P9(gx_device *dev,
  1143.   const gs_imager_state *pis, const gs_image_t *pim, gs_image_format_t format,
  1144.   gs_image_shape_t shape, const gx_drawing_color *pdcolor,
  1145.   const gx_clip_path *pcpath, gs_memory_t *memory, void **pinfo)) [OPTIONAL]
  1146.  
  1147.     Begin the transmission of an image.  Zero or more calls of
  1148. image_data will follow, and then a call of end_image.  The parameters of
  1149. begin_image are as follows:
  1150.  
  1151.     pis - pointer to an imager state.  The only relevant elements of the
  1152. imager state are the CTM (coordinate transformation matrix), the logical
  1153. operation (RasterOp/transparency), and the color rendering information.
  1154.  
  1155.     pim - pointer to the gs_image_t structure that defines the image
  1156. parameters.
  1157.  
  1158.     format - defines how pixels are represented for image_data.  See the
  1159. description of image_data below.
  1160.  
  1161.     shape - a bit mask that defines whether the client promises to
  1162. supply the full Width x Height array of pixels, or whether only a
  1163. subrectangle or irregular subset may be supplied; see the source code for
  1164. details.
  1165.  
  1166.     pdcolor - defines a drawing color, only needed for masks or if
  1167. CombineWithColor is true.
  1168.  
  1169.     pcpath - defines an optional clipping path, may be NULL.
  1170.  
  1171.     memory - defines the allocator to be used for allocating bookkeeping
  1172. information.
  1173.  
  1174.     pinfo - the implementation should return a pointer to its
  1175. bookkeeping structure here.
  1176.  
  1177.     begin_image is expected to allocate a structure for its bookkeeping
  1178. needs, using the allocator defined by the memory parameter, and return it in
  1179. *pinfo.  begin_image should not assume that the structures in *pim or
  1180. *pdcolor will survive the call on begin_image (except for the color space in
  1181. *pim->ColorSpace): it should copy any necessary parts of them into its own
  1182. bookkeeping structure.  It may, however, assume that *pis, *pcpath, and of
  1183. course *memory will live at least until end_image is called.
  1184.  
  1185. int (*image_data)(P8(gx_device *dev,
  1186.   void *info, const byte **planes, uint raster,
  1187.   int x, int y, int width, int height)) [OPTIONAL]
  1188.  
  1189.     This call provides more of the image source data.  Because of
  1190. banding, image_data will not necessarily receive complete rows, or all the
  1191. rows of an image; that is why the call supplies the source x, y, width, and
  1192. height parameters.  However, if x1/y1/w1/h1 and x2/y2/w2/h2 are the values
  1193. of these parameters for two successive calls on image_data, the following
  1194. properties are guaranteed:
  1195.  
  1196.     - 0 <= xi <= xi + wi <= params->Width.
  1197.     - 0 <= yi <= yi + hi <= params->Height.
  1198.  
  1199.     - If h1 > 1, then y2 = y1 + h1.
  1200.  
  1201.     - If h1 = 1, then either y2 = y1 + h1, or y2 = y1 and
  1202.     x2 = x1 + w1 and h2 = 1.
  1203.  
  1204. In other words, either a call passes 1 or more consecutive rows, or several
  1205. calls pass adjacent parts of a single row.  Note that the x/y/width/height
  1206. values refer to the source image data, not to the destination as they do for
  1207. copy_mono, copy_color, etc.
  1208.  
  1209.     The data for each row are packed big-endian within each byte.  The
  1210. raster (number of bytes per row) may include some padding at the end of each
  1211. row.  Note that for non-mask images, the input data may be in any color
  1212. space and may have any number of bits per component (1, 2, 4, 8, 12).
  1213. (Currently mask images always have 1 bit per component, but in the future,
  1214. they might allow multiple bits of alpha.)  Note also that each call of
  1215. image_data passes complete pixels: e.g., for a chunky image with 24 bits per
  1216. pixel, each call of image_data passes 3N bytes of data (specifically, 3 *
  1217. width * height).
  1218.  
  1219.     The interpretation of planes depends on the format argument of
  1220. begin_image.  If format is gs_image_format_chunky, planes[0] points to data
  1221. in "chunky" format, in which the components follow each other (e.g.,
  1222. RGBRGBRGB....)  If format is gs_image_format_component_planar, planes[0
  1223. .. N-1] point to data for the N components (e.g., N=3 for RGB data); each
  1224. plane contains samples for a single component, e.g., RR..., GG..., BB....
  1225. Note that the planes are divided by component, not by bit: for example, for
  1226. 24-bit RGB data, N=3, with 8-bit values in each plane of data.  Someday we
  1227. may add gs_image_format_bit_planar, specifying that each plane would contain
  1228. only a single bit of the pixel value, but this is not currently implemented.
  1229.  
  1230.     image_data, unlike most other driver procedures that take bitmaps as
  1231. arguments, does not require the data to be aligned in any way.
  1232.  
  1233. int end_image(P3(gx_device *dev,
  1234.   void *info, bool draw_last)) [OPTIONAL]
  1235.  
  1236.     Finish processing an image, either because all data have been
  1237. supplied or because the caller has decided to abandon this image.  end_image
  1238. may be called at any time after begin_image.  It should free the info
  1239. structure and any subsidiary structures.  If draw_last is true, it should
  1240. finish drawing any buffered lines of the image.
  1241.  
  1242.     While there will almost never be more than one image enumeration in
  1243. progress -- i.e., after a begin_image, end_image will almost always be
  1244. called before the next begin_image -- driver code should not rely on this
  1245. property; in particular, it should store all information regarding the image
  1246. in the info structure, not in the driver structure.
  1247.  
  1248.     Note that if begin_image saves its parameters in the info structure,
  1249. it can decide on each call whether to use its own algorithms or to use the
  1250. default implementation.  (It may need to call gx_default_begin/end_image
  1251. partway through.)  [A later revision of this document may include an example
  1252. here.]
  1253.  
  1254. Reading bits back
  1255. -----------------
  1256.  
  1257. int (*get_bits)(P4(gx_device *, int y, byte *str, byte **actual_data))
  1258.   [OPTIONAL]
  1259.  
  1260.     Read one scan line of bits back from the device into the area
  1261. starting at str, namely, scan line y.  If the bits cannot be read back
  1262. (e.g., from a printer), return -1; otherwise return 0.  The contents of the
  1263. bits beyond the last valid bit in the scan line (as defined by the device
  1264. width) are unpredictable.  str need not be aligned in any particular way.
  1265.  
  1266.     If actual_data is NULL, the bits are always returned at str.  If
  1267. actual_data is not NULL, get_bits may either copy the bits to str and set
  1268. *actual_data = str, or it may leave the bits where they are and return a
  1269. pointer to them in *actual_data.  In the latter case, the bits are
  1270. guaranteed to start on a 32-bit boundary and to be padded to a multiple of
  1271. 32 bits; also in this case, the bits are not guaranteed to still be there
  1272. after the next call on get_bits.
  1273.  
  1274. Parameters
  1275. ----------
  1276.  
  1277. Devices may have an open-ended set of parameters, which are simply pairs
  1278. consisting of a name and a value.  The value may be of various types:
  1279. integer (int or long), boolean, float, string, name, null, array of integer,
  1280. or array of float.  For example, the Name of a device is a string; the
  1281. Margins of a device is an array of 2 floats.  See gsparam.h for more
  1282. details.
  1283.  
  1284. If a device has parameters other than the ones applicable to all devices
  1285. (or, in the case of printer devices, all printer devices), it must provide
  1286. get_params and put_params procedures.  If your device has parameters beyond
  1287. those of a straightforward display or printer, we strongly advise using the
  1288. _get_params and _put_params procedures in an existing device (for example,
  1289. gdevcdj.c or gdevbit.c) as a model for your own code.
  1290.  
  1291. int (*get_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  1292.  
  1293.     Read the parameters of the device into the parameter list at plist,
  1294. using the param_write_* macros/procedures defined in gsparam.h.
  1295.     
  1296. int (*put_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  1297.  
  1298.     Set the parameters of the device from the parameter list at plist,
  1299. using the param_read_* macros/procedures defined in gsparam.h.  All
  1300. put_params procedures must use a "two-phase commit" algorithm; see gsparam.h
  1301. for details.
  1302.  
  1303. External fonts
  1304. --------------
  1305.  
  1306. Drivers may include the ability to display text.  More precisely, they may
  1307. supply a set of procedures that in turn implement some font and text
  1308. handling capabilities.  These procedures are documented in another file,
  1309. xfonts.txt.  The link between the two is the driver procedure that
  1310. supplies the font/text procedures:
  1311.  
  1312. xfont_procs *(*get_xfont_procs)(P1(gx_device *dev)) [OPTIONAL]
  1313.  
  1314.     Return a structure of procedures for handling external fonts and
  1315. text display.  A NULL value means that this driver doesn't provide this
  1316. capability.
  1317.  
  1318. For technical reasons, a second procedure is also needed:
  1319.  
  1320. gx_device *(*get_xfont_device)(P1(gx_device *dev)) [OPTIONAL]
  1321.  
  1322.     Return the device that implements get_xfont_procs in a non-default
  1323. way for this device, if any.  Except for certain special internal devices,
  1324. this is always the device argument.
  1325.  
  1326. Page devices
  1327. ------------
  1328.  
  1329. gx_device *(*get_page_device)(P1(gx_device *dev)) [OPTIONAL]
  1330.  
  1331.     According to the Adobe specifications, some devices are "page
  1332. devices" and some are not.  This procedure returns NULL if the device is
  1333. not a page device, or the device itself if it is a page device.  In the
  1334. case of forwarding devices, get_page_device returns the underlying page
  1335. device (or NULL if the underlying device is not a page device).
  1336.  
  1337. Miscellaneous
  1338. -------------
  1339.  
  1340. int (*get_band)(P3(gx_device *dev, int y, int *band_start)) [OPTIONAL]
  1341.  
  1342.     If the device is a band device, this procedure stores in *band_start
  1343. the scan line (device Y coordinate) of the band that includes the given Y
  1344. coordinate, and returns the number of scan lines in the band.  If the device
  1345. is not a band device, this procedure returns 0.  The latter is the default
  1346. implementation.
  1347.