home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / GS23A.ZIP / DRIVERS.DOC < prev    next >
Text File  |  1991-05-25  |  18KB  |  429 lines

  1.    Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.
  19.  
  20. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  21.  
  22. This file, drivers.doc, describes the interface between Ghostscript and
  23. device drivers.
  24.  
  25. For an overview of Ghostscript and a list of the documentation files, see
  26. README.
  27.  
  28. ********
  29. ******** Adding a driver ********
  30. ********
  31.  
  32. To add a driver to Ghostscript, all you need to do is edit gdevs.mak in
  33. two places.  The first is the list of devices, in the section headed
  34. # -------------------------------- Catalog ------------------------------- #
  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. # ---------------------------- Device drivers ---------------------------- #
  41. Suppose the files containing the smurf driver are called joe and fred.
  42. Then you should add the following lines:
  43.  
  44. # ------ The SMURF device ------ #
  45.  
  46. smurf_=joe.$(OBJ) fred.$(OBJ)
  47. smurf.dev: $(smurf_)
  48.     gssetdev smurf.dev $(smurf_)
  49.  
  50. joe.$(OBJ): joe.c ...and whatever it depends on
  51.  
  52. fred.$(OBJ): fred.c ...and whatever it depends on
  53.  
  54. If joe uses platform-specific, non-ANSI-compatible features (like inline
  55. assembly code), then the line should read
  56.  
  57. joe.$(OBJ): joe.c ...and whatever it depends on
  58.     $(CCNA) $*
  59.  
  60. ********
  61. ******** Driver structure ********
  62. ********
  63.  
  64. A device is represented by a structure divided into three parts:
  65.  
  66.     - procedures that are shared by all instances of each device;
  67.  
  68.     - parameters that are present in all devices but may be different
  69.       for each device or instance; and
  70.  
  71.     - device-specific parameters that may be different for each instance.
  72.  
  73. Normally, the procedure structure is defined and initialized at compile
  74. time.  A prototype of the parameter structure (including both generic and
  75. device-specific parameters) is defined and initialized at compile time,
  76. but is copied and filled in when an instance of the device is created.
  77.  
  78. The gx_device_common macro defines the common structure elements, with the
  79. intent that devices define and export a structure along the following
  80. lines:
  81.  
  82.     typedef struct smurf_device_s {
  83.         gx_device_common;
  84.         ... device-specific parameters ...
  85.     } smurf_device;
  86.     smurf_device gs_smurf_device = {
  87.         sizeof(smurf_device),        * params_size
  88.         { ... procedures ... },        * procs
  89.         ... generic parameter values ...
  90.         ... device-specific parameter values ...
  91.     };
  92.  
  93. The device structure instance *must* have the name gs_smurf_device, where
  94. smurf is the device name used in gdevs.mak.
  95.  
  96. All the device procedures are called with the device as the first
  97. argument.  Since each device type is actually a different structure type,
  98. the device procedures must be declared as taking a gx_device * as their
  99. first argument, and must cast it to smurf_device * internally.  For
  100. example, in the code for the "memory" device, the first argument to all
  101. routines is called dev, but the routines actually use md to reference
  102. elements of the full structure, by virtue of the definition
  103.  
  104.     #define md ((gx_device_memory *)dev)
  105.  
  106. (This is a cheap version of "object-oriented" programming: in C++, for
  107. example, the cast would be unnecessary, and in fact the procedure table
  108. would be constructed by the compiler.)
  109.  
  110. Structure definition
  111. --------------------
  112.  
  113. This essentially duplicates the structure definition in gxdevice.h.
  114.  
  115. typedef struct gx_device_s {
  116.     int params_size;        /* size of this structure */
  117.     gx_device_procs *procs;        /* pointer to procedure structure */
  118.     char *name;            /* the device name */
  119.     int width;            /* width in pixels */
  120.     int height;            /* height in pixels */
  121.     float x_pixels_per_inch;    /* x density */
  122.     float y_pixels_per_inch;    /* y density */
  123.     gs_rect margin_inches;        /* margins around imageable area, */\
  124.                     /* in inches */\
  125.     int has_color;            /* true if device supports color */
  126.     unsigned short max_rgb_value;    /* max r, g, b value */
  127.     int bits_per_color_pixel;    /* for copy_color */
  128.     int is_open;            /* true if device has been opened */
  129. } gx_device;
  130.  
  131. The name in the structure should be the same as the name in gdevs.mak.
  132.  
  133. gx_device_common is a macro consisting of just the element definitions.
  134.  
  135. If for any reason you need to change the definition of the basic device
  136. structure, or add procedures, you must change the following places:
  137.  
  138.     - This document and history.doc (if you want to keep the
  139.         documentation up to date).
  140.     - The definition of gx_device_common and/or the procedures
  141.         in gxdevice.h.
  142.     - The null device in gsdevice.c.
  143.     - The tracing "device" in gstdev.c.
  144.     - The "memory" devices in gdevmem.c.
  145.     - The command list "device" in gxclist.c.
  146.     - All the real devices in the standard Ghostscript distribution,
  147.         as listed in gdevs.mak.
  148.     - Any other drivers you have that aren't part of the standard
  149.         Ghostscript distribution.
  150.  
  151. You may also have to change the code for zmakedevice (in zdevice.c) and
  152. gs_makedevice (in gsdevice.c).
  153.  
  154. ********
  155. ******** Types and coordinates ********
  156. ********
  157.  
  158. Coordinate system
  159. -----------------
  160.  
  161. Since each driver specifies the initial transformation from user to device
  162. coordinates, the driver can use any coordinate system it wants, as long as
  163. a device coordinate will fit in an int.  (This is only an issue on MS-DOS
  164. systems, where ints are only 16 bits.  User coordinates are represented as
  165. floats.)  Typically the coordinate system will have (0,0) in the upper
  166. left corner, with X increasing to the right and Y increasing toward the
  167. bottom.  This happens to be the coordinate system that all the currently
  168. supported devices use.  However, there is supposed to be nothing in the
  169. rest of Ghostscript that assumes this.
  170.  
  171. Drivers must check the coordinate parameters given to them: they should
  172. not assume the coordinates will be in bounds.
  173.  
  174. Types
  175. -----
  176.  
  177. Here is a brief explanation of the various types that appear as parameters
  178. or results of the drivers.
  179.  
  180. gx_device (defined in gxdevice.h)
  181.  
  182.     This is the device structure, as explained above.
  183.  
  184. gs_matrix (defined in gsmatrix.h)
  185.  
  186.     This is a 2-D homogenous coordinate transformation matrix, used by
  187. many Ghostscript operators.
  188.  
  189. gs_rect (defined in gs.h)
  190.  
  191.     This defines a rectangle by giving the coordinates of its minimal
  192. and maximal corners.  It is only used (perhaps misused) to define the
  193. margins of the imageable area.
  194.  
  195. gx_color_index (defined in gxdevice.h)
  196.  
  197.     This is meant to be whatever the driver uses to represent a device
  198. color.  For example, it might be an index in a color map.  Ghostscript
  199. doesn't ever do any computations with these values: it gets them from
  200. map_rgb_color and hands them back as arguments to several other
  201. procedures.  The special value gx_no_color_index (defined as
  202. (gx_color_index)(-1)) means "transparent" for some of the procedures.  The
  203. type definition is simply:
  204.  
  205.     typedef unsigned long gx_color_index;
  206.  
  207. gx_bitmap (defined in gxbitmap.h)
  208.  
  209.     This structure type represents a bitmap to be used as a tile for
  210. filling a region (rectangle or trapezoid).  Here is a copy of the relevant
  211. part of the file:
  212.     /*
  213.      * Structure for describing stored bitmaps.
  214.      * Bitmaps are stored bit-big-endian (i.e., the 2^7 bit of the first
  215.      * byte corresponds to x=0), as a sequence of bytes (i.e., you can't
  216.      * do word-oriented operations on them if you're on a little-endian
  217.      * platform like the Intel 80x86 or VAX).  Each scan line is normally
  218.      * padded only to a byte boundary, although this should be of no
  219.      * concern since the raster and width are specified separately.
  220.      * The first scan line corresponds to y=0 in whatever coordinate
  221.      * system is relevant.
  222.      */
  223.     struct gx_bitmap_s {
  224.         byte *data;
  225.         int raster;        /* bytes per scan line */
  226.         int width, height;
  227.     };
  228.  
  229. ********
  230. ******** Driver procedures ********
  231. ********
  232.  
  233. All the procedures that return int results return 0 on success, or -1 on
  234. invalid arguments.
  235.  
  236. Most procedures are optional.  If a device doesn't supply an optional
  237. procedure proc, the entry in the procedure structure should be
  238. gx_default_proc, e.g. gx_default_tile_rectangle.  The device procedure can
  239. also call this procedure if it doesn't implement the function for
  240. particular values of the arguments; alternatively, it can return -1, since
  241. all callers of optional procedures will call the default procedure in this
  242. case.
  243.  
  244. Open/close/sync
  245. ---------------
  246.  
  247. int (*open_device)(P1(struct gx_device_s *)) [OPTIONAL]
  248.  
  249.     Open the device: do any initialization associated with making the
  250. device instance valid.  This must be done before any output to the device.
  251.  
  252. void (*get_matrix_and_clip)(P3(struct gx_device_s *, gs_matrix *)) [OPTIONAL]
  253.  
  254.     Construct the initial transformation matrix mapping user
  255. coordinates (nominally 1/72" per unit) to device coordinates.  The default
  256. procedure computes this from width, height, and x/y_pixels_per_inch on the
  257. assumption that the origin is in the upper left corner, i.e.
  258.         xx = x_pixels_per_inch/72, xy = 0,
  259.         yx = 0, yy = -y_pixels_per_inch/72,
  260.         tx = 0, ty = height.
  261.  
  262. int (*sync_output)(P1(struct gx_device_s *)) [OPTIONAL]
  263.  
  264.     Synchronize the device.  If any output to the device has been
  265. buffered, send / write it now.  Note that this may be called several times
  266. in the process of constructing a page, so printer drivers should NOT
  267. implement this by printing the page.
  268.  
  269. int (*output_page)(P1(struct gx_device_s *)) [OPTIONAL]
  270.  
  271.     Output a fully composed page to the device.  The default
  272. definition just calls sync_output.  Printer drivers should implement this
  273. by printing and ejecting the page.
  274.  
  275. int (*close_device)(P1(struct gx_device_s *)) [OPTIONAL]
  276.  
  277.     Close the device: release any associated resources.  After this,
  278. output to the device is no longer allowed.
  279.  
  280. Color mapping
  281. -------------
  282.  
  283. Ghostscript represents colors internally as RGB values.  In communicating
  284. with devices, however, it assumes that each device has a fixed palette of
  285. colors identified by integers (to be precise, elements of type
  286. gx_color_index), with the following properties:
  287.  
  288.     - The palette contains at least white and black.
  289.  
  290.     - If the device has gray-scale but not color capabilities, and
  291. max_rgb_value is less than 255, the palette consists of an arbitrary
  292. number of gray shades, uniformly spaced on the diagonal of the RGB color
  293. cube.
  294.  
  295.     - If the device has color capabilities, and max_rgb_value is less
  296. than 255, the palette consists of a color cube uniformly spaced in RGB
  297. space (obviously including at least the three primary colors (red, green,
  298. blue)).
  299.  
  300. These are very strong conditions: they are required by the algorithm that
  301. Ghostscript uses to create spatial halftones to approximate unavailable
  302. colors or gray levels.  In the future, we envision giving drivers much
  303. more control over color selection, and these conditions will disappear.
  304.  
  305. gx_color_index (*map_rgb_color)(P4(struct gx_device_s *, unsigned short red,
  306.   unsigned short green, unsigned short blue)) [OPTIONAL]
  307.  
  308.     Map a RGB color to a device color.  The default algorithm simply
  309. returns the brightness, i.e. max(red, green, blue).  The range of legal
  310. values of the RGB arguments is given by the max_rgb_value element of the
  311. device parameter structure.  Ghostscript assumes that for devices that
  312. have color capability (i.e., has_color is true), map_rgb_color returns a
  313. color index for a gray level (as opposed to a non-gray color) iff red =
  314. green = blue.
  315.  
  316. int (*map_color_rgb)(P3(struct gx_device_s *, gx_color_index color,
  317.   unsigned short rgb[3])) [OPTIONAL]
  318.  
  319.     Map a device color code to RGB values.  The default algorithm
  320. simply sets all three elements of the result to the color.
  321.  
  322. Drawing
  323. -------
  324.  
  325. All drawing operations use device coordinates and device color values.
  326.  
  327. int (*fill_rectangle)(P6(struct gx_device_s *, int x, int y,
  328.   int width, int height, gx_color_index color))
  329.  
  330.     Fill a rectangle with a color.  The set of pixels filled is
  331. {(px,py) | x <= px < x + width and y <= py < y + height}.  In other words,
  332. the point (x,y) is included in the rectangle, as are (x+w-1,y), (x,y+h-1),
  333. and (x+w-1,y+h-1), but *not* (x+w,y), (x,y+h), or (x+w,y+h).  If width <=
  334. 0 or height <= 0, fill_rectangle should return 0 without drawing anything.
  335.  
  336. int (*draw_line)(P6(struct gx_device_s *, int x0, int y0, int x1, int y1,
  337.   gx_color_index color)) [OPTIONAL]
  338.  
  339.     Draw a minimum-thickness line from (x0,y0) to (x1,y1).  The
  340. precise set of points to be filled is defined as follows.  First, if y1 <
  341. y0, swap (x0,y0) and (x1,y1).  Then the line includes the point (x0,y0)
  342. but not the point (x1,y1).  If x0=y0 and x1=y1, draw_line should return 0
  343. without drawing anything.
  344.  
  345. int (*fill_trapezoid)(P8(struct gx_device_s *, int x0, int y0, int width0,
  346.   int x1, int y1, int width1, gx_color_index color)) [OPTIONAL]
  347.  
  348.     Fill a trapezoid whose corners are (x0,y0), (x0+width0,y0),
  349. (x1,y1), and (x1+width1,y1), where y0 < y1.  If width0 < 0, width1 < 0,
  350. width0 = width1 = 0, or y0 >= y1, fill_trapezoid should return 0 without
  351. drawing anything; however, width0 = 0 or width1 = 0 is valid, and should
  352. result in a triangle being filled.  As for fill_rectangle, the coordinates
  353. are "half-open": the points (x0+width0,y0) and (*,y1) are not included, so
  354. the extremal points that *are* included are
  355.     (x0, y0)    (x0 + width0 - 1, y0)
  356.     (x1, y1 - 1)    (x1 + width1 - 1, y1 - 1)
  357.  
  358. Bitmap imaging
  359. --------------
  360.  
  361. Bitmap (or pixmap) images are stored in memory in a nearly standard way.
  362. The first byte corresponds to (0,0) in the image coordinate system: bits
  363. (or polybit color values) are packed into it left-to-right.  There may be
  364. padding at the end of each scan line: the distance from one scan line to
  365. the next is always passed as an explicit argument.
  366.  
  367. int (*copy_mono)(P10(struct gx_device_s *, unsigned char *data, int data_x,
  368.   int raster, int x, int y, int width, int height,
  369.   gx_color_index color0, gx_color_index color1))
  370.  
  371.     Copy a monochrome image (similar to the PostScript image
  372. operator).  Each scan line is raster bytes wide.  Copying begins at
  373. (data_x,0) and transfers a rectangle of the given width at height to the
  374. device at device coordinate (x,y).  (If the transfer should start at some
  375. non-zero y value in the data, the caller can adjust the data address by
  376. the appropriate multiple of the raster.)  The copying operation writes
  377. device color color0 at each 0-bit, and color1 at each 1-bit: if color0 or
  378. color1 is gx_no_color_index, the device pixel is unaffected if the image
  379. bit is 0 or 1 respectively.
  380.  
  381.     This operation is the workhorse for text display in Ghostscript,
  382. so implementing it efficiently is very important.
  383.  
  384. int (*tile_rectangle)(P10(struct gx_device_s *, gx_bitmap *tile,
  385.   int x, int y, int width, int height,
  386.   gx_color_index color0, gx_color_index color1,
  387.   int phase_x, int phase_y)) [OPTIONAL]
  388.  
  389.     Tile a rectangle.  Tiling consists of doing multiple copy_mono
  390. operations to fill the rectangle with copies of the tile.  The tiles are
  391. aligned with the device coordinate system, to avoid "seams".
  392. Specifically, the (phase_x, phase_y) point of the tile is aligned with the
  393. origin of the device coordinate system.  (Note that this is backwards from
  394. the PostScript definition of halftone phase.)  phase_x and phase_y are
  395. guaranteed to be in the range [0..tile->width) and [0..tile->height)
  396. respectively.
  397.  
  398.     If color0 and color1 are both gx_no_color_index, then the tile is
  399. a color pixmap, not a bitmap: see the next section.
  400.  
  401. int (*tile_trapezoid)(P12(struct gx_device_s *, gx_bitmap *tile,
  402.   int x0, int y0, int width0, int x1, int y1, int width1,
  403.   gx_color_index color0, gx_color_index color1,
  404.   int phase_x, int phase_y)) [OPTIONAL]
  405.  
  406.     Tile a trapezoid similarly.
  407.  
  408. Pixmap imaging
  409. --------------
  410.  
  411. Pixmaps are just like bitmaps, except that each pixel occupies more than
  412. one bit.  All the bits for each pixel are grouped together (this is
  413. sometimes called "chunky" or "Z" format).  The number of bits per pixel is
  414. given by the bits_per_color_pixel parameter in the device structure: the
  415. legal values are 1, 2, 4, 8, 16, 24, or 32.  The pixel values are device
  416. color codes (i.e., whatever it is that map_rgb_color returns).
  417.  
  418. int (*copy_color)(P8(struct gx_device_s *, unsigned char *data, int data_x,
  419.   int raster, int x, int y, int width, int height))
  420.  
  421.     Copy a color image with multiple bits per pixel.  The raster is in
  422. bytes, but x and width are in pixels, not bits.
  423.  
  424. tile_rectangle and tile_trapezoid can also take colored tiles.  This is
  425. indicated by the color0 and color1 arguments both being gx_no_color_index.
  426. In this case, as for copy_color, the raster and height in the "bitmap" are
  427. interpreted as for real bitmaps, but the x and width are in pixels, not
  428. bits.
  429.