henesyinit from p9fce6d0435 years agoHistory
806 lines · 33.5 KB · Text
RawDownload
   Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994 Aladdin Enterprises.     All rights reserved.    This file is part of Aladdin Ghostscript.    Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author  or distributor accepts any responsibility for the consequences of using it,  or for whether it serves any particular purpose or works at all, unless he  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public  License (the "License") for full details.    Every copy of Aladdin Ghostscript must include a copy of the License,  normally in a plain ASCII text file named PUBLIC.  The License grants you  the right to copy, modify and redistribute Aladdin Ghostscript, but only  under certain conditions described in the License.  Among other things, the  License requires that the copyright notice and this notice be preserved on  all copies. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This file, drivers.doc, describes the interface between Ghostscript anddevice drivers. For an overview of Ghostscript and a list of the documentation files, seeREADME. **************** Adding a driver **************** To add a driver to Ghostscript, all you need to do is edit devs.mak intwo places.  The first is the list of devices, in the section headed # -------------------------------- Catalog ------------------------------- # Pick a name for your device, say smurf, and add smurf to the list.(Device names must be 1 to 8 characters, consisting of only letters,digits, and underscores, of which the first character must be a letter.Case is significant: all current device names are lower case.)The second is the section headed # ---------------------------- Device drivers ---------------------------- # Suppose the files containing the smurf driver are called joe and fred.Then you should add the following lines: # ------ The SMURF device ------ # smurf_=joe.$(OBJ) fred.$(OBJ)smurf.dev: $(smurf_)	$(SHP)gssetdev smurf $(smurf_) joe.$(OBJ): joe.c ...and whatever it depends on fred.$(OBJ): fred.c ...and whatever it depends on If the smurf driver also needs special libraries, e.g., a library namedgorf, then the gssetdev line should look like	$(SHP)gssetdev smurf $(smurf_)	$(SHP)gsaddmod smurf -lib gorf **************** Keeping things simple******** If you want to add a simple device (specifically, a black-and-whiteprinter), you probably don't need to read the rest of this document; justuse the code in an existing driver as a guide.  The Epson and BubbleJetdrivers (gdevepsn.c and gdevbj10.c) are good models for dot-matrixprinters, which require presenting the data for many scan lines at once;the DeskJet/LaserJet drivers (gdevdjet.c) are good models for laserprinters, which take a single scan line at a time but support datacompression.  For color printers, the DeskJet 500C driver (gdevcdj.c) is agood place to start.  gdevcdj.c is also a good example of a device thathas additional settable attributes (in this case, different output qualitymodes that aren't just a matter of resolution). On the other hand, if you're writing a driver for some more esotericdevice, you probably do need at least some of the information in the restof this document.  It might be a good idea for you to read it inconjunction with one of the existing drivers. **************** Driver structure **************** A device is represented by a structure divided into three parts: 	- procedures that are shared by all instances of each device; 	- parameters that are present in all devices but may be different	  for each device or instance; and 	- device-specific parameters that may be different for each instance. Normally, the procedure structure is defined and initialized at compiletime.  A prototype of the parameter structure (including both generic anddevice-specific parameters) is defined and initialized at compile time,but is copied and filled in when an instance of the device is created. The gx_device_common macro defines the common structure elements, with theintent that devices define and export a structure along the followinglines: 	typedef struct smurf_device_s {		gx_device_common;		... device-specific parameters ...	} smurf_device;	smurf_device gs_smurf_device = {		sizeof(smurf_device),		/* params_size */		0,				/* static_procs, obsolete */		... generic parameter values ...		{ ... procedures ... },		/* std_procs */		... device-specific parameter values ...	}; The device structure instance *must* have the name gs_smurf_device, wheresmurf is the device name used in devs.mak. All the device procedures are called with the device as the firstargument.  Since each device type is actually a different structure type,the device procedures must be declared as taking a gx_device * as theirfirst argument, and must cast it to smurf_device * internally.  Forexample, in the code for the "memory" device, the first argument to allroutines is called dev, but the routines actually use md to referenceelements of the full structure, by virtue of the definition 	#define md ((gx_device_memory *)dev) (This is a cheap version of "object-oriented" programming: in C++, forexample, the cast would be unnecessary, and in fact the procedure tablewould be constructed by the compiler.) Structure definition-------------------- This essentially duplicates the first part of the structure definition ingxdevice.h. typedef struct gx_device_s {	int params_size;		/* size of this structure */	gx_device_procs *static_procs;	/* (obsolete) */	const char *dname;		/* the device name */	int width;			/* width in pixels */	int height;			/* height in pixels */	...	gx_device_color_info color_info;	/* color information */	...	int is_open;			/* true if device has been opened */} gx_device; The name in the structure should be the same as the name in devs.mak. gx_device_common is a macro consisting of just the element definitions. For sophisticated developers only--------------------------------- If for any reason you need to change the definition of the basic devicestructure, or add procedures, you must change the following places: 	- This document and NEWS (if you want to keep the	    documentation up to date).	- The definition of gx_device_common and/or the procedures	    in gxdevice.h.	- Possibly, the default forwarding procedures in gxdevice.h	    and gsdevice.c.	- The following devices that must have complete (non-defaulted)	    procedure vectors:		- The null device in gsdevice.c.		- The command list "device" in gxclist.c.  This is		    not an actual device; it only defines procedures.		- The "memory" devices in gdevmem.h and gdevm*.c.	- The procedure record completion routine in gsdevice.c.	- The clip list accumulation "device" in gxacpath.c.	- The clipping "devices" in gxcpath.c and gxclip2.c.	- The hit detection "device" in zupath.c.	- The generic printer device macros in gdevprn.h.	- The generic printer device code in gdevprn.c.	- All the real devices in the standard Ghostscript distribution,	    as listed in devs.mak.  (All of them are supposed to use	    the macros in gxdevice.h or gdevprn.h to initialize their	    state, so you may not have to edit the source code for	    them.)	- Any other drivers you have that aren't part of the standard	    Ghostscript distribution. You may also have to change the code for gx_default_get_params and/orgx_default_put_params (in gsdparam.c). Note that if all you are doing is adding optional procedures, you do NOThave to modify any of the real device drivers listed in devs.mak;Ghostscript will substitute the default procedures properly. **************** Types and coordinates **************** Coordinate system----------------- Since each driver specifies the initial transformation from user to devicecoordinates, the driver can use any coordinate system it wants, as long asa device coordinate will fit in an int.  (This is only an issue on MS-DOSsystems, where ints are only 16 bits.  User coordinates are represented asfloats.)  Typically the coordinate system will have (0,0) in the upperleft corner, with X increasing to the right and Y increasing toward thebottom.  This happens to be the coordinate system that all the currentlysupported devices use.  However, there is supposed to be nothing in therest of Ghostscript that assumes this. Drivers must check (and, if necessary, clip) the coordinate parametersgiven to them: they should not assume the coordinates will be in bounds.The fit_fill and fit_copy macros in gxdevice.h are very helpful in doingthis. Color definition---------------- Ghostscript represents colors internally as RGB or CMYK values.  Incommunicating with devices, however, it assumes that each device has apalette of colors identified by integers (to be precise, elements of typegx_color_index).  Drivers may provide a uniformly spaced gray ramp orcolor cube for halftoning, or they may do their own color approximation,or both. The color_info member of the device structure defines the color andgray-scale capabilities of the device.  Its type is defined as follows: typedef struct gx_device_color_info_s {	int num_components;		/* 1 = gray only, 3 = RGB, */					/* 4 = CMYK */	int depth;			/* # of bits per pixel */	gx_color_value max_gray;	/* # of distinct gray levels -1 */	gx_color_value max_rgb;		/* # of distinct color levels -1 */					/* (only relevant if num_comp. > 1) */	gx_color_value dither_gray;	/* size of gray ramp for halftoning */	gx_color_value dither_rgb;	/* size of color cube ditto */					/* (only relevant if num_comp. > 1) */} gx_device_color_info; The following macros (in gxdevice.h) provide convenient shorthands forinitializing this structure for ordinary black-and-white or color devices: #define dci_black_and_white ...#define dci_color(depth,maxv,dither) ... The idea is that a device has a certain number of gray levels (max_gray +1)and a certain number of colors (max_rgb +1) that it can produce directly.When Ghostscript wants to render a given RGB or CMYK color as a devicecolor, it first tests whether the color is a gray level.  (Ifnum_components is 1, it converts all colors to gray levels.)  If so: 	- If max_gray is large (>= 31), Ghostscript asks the device toapproximate the gray level directly.  If the device returns a validgx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes thatthe device can represent dither_gray distinct gray levels, equally spacedalong the diagonal of the color cube, and uses the two nearest ones to thedesired color for halftoning. If the color is not a gray level: 	- If max_rgb is large (>= 31), Ghostscript asks the device toapproximate the color directly.  If the device returns a validgx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes thatthe device can represent dither_rgb * dither_rgb * dither_rgb distinctcolors, equally spaced throughout the color cube, and uses two of thenearest ones to the desired color for halftoning. Types----- Here is a brief explanation of the various types that appear as parametersor results of the drivers. gx_color_value (defined in gxdevice.h) 	This is the type used to represent RGB or CMYK color values.  It iscurrently equivalent to unsigned short.  However, Ghostscript may use lessthan the full range of the type to represent color values:gx_color_value_bits is the number of bits actually used, andgx_max_color_value is the maximum value (equal to 2^gx_max_color_value_bits- 1). gx_device (defined in gxdevice.h) 	This is the device structure, as explained above. gs_matrix (defined in gsmatrix.h) 	This is a 2-D homogenous coordinate transformation matrix, used bymany Ghostscript operators. gx_color_index (defined in gxdevice.h) 	This is meant to be whatever the driver uses to represent a devicecolor.  For example, it might be an index in a color map, or it might beR, G, and B values packed into a single integer.  Ghostscript doesn't everdo any computations with gx_color_index values: it gets them frommap_rgb_color or map_cmyk_color and hands them back as arguments toseveral other procedures.  The special value gx_no_color_index (defined as(gx_color_index)(-1)) means "transparent" for some of the procedures.  Thetype definition is simply: 	typedef unsigned long gx_color_index; gs_param_list (defined in gsparam.h) 	This is a parameter list, which is used to read and set attributesin a device.  See the comments in gsparam.h, and the description of theget_params and put_params procedures below, for more detail. gx_bitmap (defined in gxbitmap.h) 	This structure type represents a bitmap to be used as a tile forfilling a region (rectangle).  Here is a copy of the relevant part of thefile: /* * Structure for describing stored bitmaps. * Bitmaps are stored bit-big-endian (i.e., the 2^7 bit of the first * byte corresponds to x=0), as a sequence of bytes (i.e., you can't * do word-oriented operations on them if you're on a little-endian * platform like the Intel 80x86 or VAX).  Each scan line must start on * a (32-bit) word boundary, and hence is padded to a word boundary, * although this should rarely be of concern, since the raster and width * are specified individually.  The first scan line corresponds to y=0 * in whatever coordinate system is relevant. * * For bitmaps used as halftone tiles, we may replicate the tile in * X and/or Y, but it is still valuable to know the true tile dimensions. */typedef struct gx_bitmap_s {	byte *data;	int raster;			/* bytes per scan line */	gs_int_point size;		/* width, height */	gx_bitmap_id id;	ushort rep_width, rep_height;	/* true size of tile */} gx_bitmap; **************** Coding conventions **************** While most drivers (especially printer drivers) follow a very similartemplate, there is one important coding convention that is not obvious fromreading the code for existing drivers: Driver procedures must not usemalloc to allocate any storage that stays around after the procedurereturns.  Instead, they must use gs_malloc and gs_free, which have slightlydifferent calling conventions.  (The prototypes for these are ingsmemory.h, which is included in gx.h, which is included in gdevprn.h.)This is necessary so that Ghostscript can clean up all allocated memorybefore exiting, which is essential in environments that provide onlysingle-address-space multi-tasking (specifically, Microsoft Windows). char *gs_malloc(uint num_elements, uint element_size,  const char *client_name); 	Like calloc, but unlike malloc, gs_malloc takes an element countand an element size.  For structures, num_elements is 1 and element_size issizeof the structure; for byte arrays, num_elements is the number of bytesand element_size is 1.  Unlike calloc, gs_malloc does NOT clear the blockof storage. 	The client_name is used for tracing and debugging.  It must be areal string, not NULL.  Normally it is the name of the procedure in whichthe call occurs. void gs_free(char *data, uint num_elements, uint element_size,  const char *client_name); 	Unlike free, gs_free demands that num_elements and element_size besupplied.  It also requires a client name, like gs_malloc. All the driver procedures defined below that return int results return 0 onsuccess, or an appropriate negative error code in the case of errorconditions.  The error codes are defined in gserrors.h.  The relevant onesfor drivers are as follows: 	gs_error_invalidfileaccess		An attempt to open a file failed. 	gs_error_limitcheck		An otherwise valid parameter value was too large for		the implementation. 	gs_error_rangecheck		A parameter was outside the valid range. 	gs_error_VMerror		An attempt to allocate memory failed.  (If this		happens, the procedure should release all memory it		allocated before it returns.) If a driver does return an error, it should use the return_errormacro rather than a simple return statement, e.g., 	return_error(gs_error_VMerror); This macro is defined in gx.h, which is automatically included bygdevprn.h but not by gserrors.h. **************** Printer drivers **************** Printer drivers (which include drivers that write some kind of rasterfile) are especially simple to implement.  Of the driver proceduresdefined in the next section, they only need implement two: map_rgb_color(or map_cmyk_color) and map_color_rgb.  In addition, they must implement aprint_page or print_page_copies procedure.  There are a set of macros ingdevprn.h that generate the device structure for such devices, of whichthe simplest is prn_device; for an example, see gdevbj10.c.  If you arewriting a printer driver, we suggest you start by reading gdevprn.h andthe subsection on "Color mapping" below; you may be able to ignore all therest of the driver procedures. The print_page procedures are defined as follows: int (*print_page)(P2(gx_device_printer *, FILE *))int (*print_page_copies)(P3(gx_device_printer *, FILE *, int)) 	This procedure must read out the rendered image from the device andwrite whatever is appropriate to the file.  To read back one or more scanlines of the image, the print_page procedure must call one of the followingprocedures:     int gdev_prn_copy_scan_lines(P4(gx_device_printer *pdev, int y, byte *str,      uint size) For this procedure, str is where the data should be copied to, and size isthe size of the buffer starting at str.  This procedure returns the numberof scan lines copied, or <0 for an error.     int gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str,      byte **actual_data) This procedure reads out exactly one scan line.  If the scan line isavailable in the correct format already, *actual_data is set to point toit; otherwise, the scan line is copied to the buffer starting at str, and*actual_data is set to str.  This saves a copying step most of the time. In either case, each row of the image is stored in the form described inthe comment under gx_bitmap above; each pixel takes the number of bitsspecified as color_info.depth in the device structure, and holds valuesreturned by the device's map_{rgb,cmyk}_color procedure. The print_page procedure can determine the number of bytes required to holda scan line by calling: 	uint gdev_prn_raster(P1(gx_device_printer *)) For a very simple concrete example, we suggest reading the code inbit_print_page in gdevbit.c. If the device provides print_page, Ghostscript will call print_page therequisite number of times to print the desired number of copies; if thedevice provides print_page_copies, Ghostscript will call print_page_copiesonce per page, passing it the desired number of copies. **************** Driver procedures **************** Most of the procedures that a driver may implement are optional.  If adevice doesn't supply an optional procedure <proc>, the entry in theprocedure structure may be either gx_default_<proc>, e.g.gx_default_tile_rectangle, or NULL or 0.  (The device procedure must alsocall the gx_default_ procedure if it doesn't implement the function forparticular values of the arguments.)  Since C compilers supply 0 as thevalue for omitted structure elements, this convention means thatstatically initialized procedure structures will continue to work even ifnew (optional) members are added. Life cycle---------- Ghostscript "opens" and "closes" drivers explicitly; a driver can assumethat no output operations will be done through it while it is closed.Ghostscript keeps track of whether a given driver is open, so a driverwill never be opened when it is already open, or closed when it is alreadyclosed. The following are the only driver procedures that may be called when thedriver is closed:	open_device	get_initial_matrix	get_params	put_params Open/close/sync--------------- int (*open_device)(P1(gx_device *)) [OPTIONAL] 	Open the device: do any initialization associated with making thedevice instance valid.  This must be done before any output to the device.The default implementation does nothing. void (*get_initial_matrix)(P2(gx_device *, gs_matrix *)) [OPTIONAL]

Lines 1–500 of 806.