   Copyright (C) 1989, 1996 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer to
  the GNU General Public License for full details.
  
  Everyone is granted permission to copy, modify and redistribute GNU
  Ghostscript, but only under the conditions described in the GNU General
  Public License.  A copy of this license is supposed to have been given to
  you along with GNU Ghostscript so you can know your rights and
  responsibilities.  It should be in a file named COPYING.  Among other
  things, the copyright notice and this notice must be preserved on all
  copies.
  
  Aladdin Enterprises is not affiliated with the Free Software Foundation or
  the GNU Project.  GNU Ghostscript, as distributed by Aladdin Enterprises,
  does not depend on any other GNU software.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This file, lib.txt, describes the Ghostscript library, a collection of C
procedures that implement the primitive graphic operations of the
Ghostscript language.

For an overview of Ghostscript and a list of the documentation files, see
README.

********
******** The Ghostscript library ********
********

Ghostscript is actually two programs: a language interpreter, and a graphics
library.  The library provides, in the form of C procedures, all the
graphics functions of the language, i.e., approximately those facilities
listed in section 8.1 of the PostScript Language Reference Manual, Second
Edition, starting with the graphics state operators.  In addition, the
library provides some lower-level graphics facilities that offer higher
performance in exchange for less generality.

PostScript operator API
-----------------------

The highest level of the library, which is the one that most clients will
use, directly implements the PostScript graphics operators with procedures
named gs_XXX, e.g., gs_moveto, gs_fill.  Nearly all of these procedures take
a graphics state object as their first argument, e.g.,
	int gs_moveto(gs_state *, double, double);

Nearly all procedures return an integer code which is >= 0 for a successful
return or <0 for a failure.  The failure codes correspond directly to
PostScript errors, and are defined in gserrors.h.

The library implements all the operators in the following sections of the
PostScript Language Reference Manual, Second Edition, with the indicated
omissions and with the APIs defined in the indicated .h files.  A header of
the form A.h(B.h) indicates that A.h is included in B.h, so A.h need not be
included explicitly if B.h is included.  Operators marked with * in the
Omissions column are not implemented directly; the library provides
lower-level procedures that can be used to implement the operator.

There are slight differences in the operators that return multiple values,
since C's provisions for this are awkward.  Also, the control structure for
the operators involving (a) callback procedure(s) (pathforall, image,
colorimage, imagemask) is partly inverted: the client calls a procedure to
set up an enumerator object, and then calls another procedure for each
iteration.  The ...show operators, charpath, and stringwidth also use an
inverted control structure.

Section		Headers			Omissions
-------		-------			---------

Graphics State Operators --
Device Independent
		gscolor.h(gsstate.h)
		gscolor1.h
		gscolor2.h
		gscspace.h
		gshsb.h
		gsline.h(gsstate.h)
		gsstate.h

Graphics State Operators --
Device Dependent
		gscolor.h(gsstate.h)
		gscolor1.h
		gscolor2.h
		gsht.h(gsht1.h,gsstate.h)
		gsht1.h
		gsline.h(gsstate.h)

Coordinate System and
Matrix Operators
		gscoord.h		*matrix, *identmatrix,
		gsmatrix.h		  *concatmatrix, *invertmatrix

Path Construction Operators
		gspath.h		*arct, *pathforall,
		gspath2.h		  ustrokepath, uappend, upath, ucache

Painting Operators
		gsimage.h		*image, *colorimage, *imagemask,
		gspaint.h		  ufill, ueofill, ustroke
		gspath2.h

Form and Pattern Operators
		gscolor2.h		execform

Device Setup and Output
Operators
		gsdevice.h		*showpage, *set/currentpagedevice

Character and Font Operators
		gschar.h		*(all the show operators),
		gsfont.h		  definefont, undefinefont,
					  findfont, *scalefont, *makefont,
					  selectfont, [Global]FontDirectory,
					  Standard/ISOLatin1Encoding,
					  findencoding

<< -------------------------------- end -------------------------------- >>

The following procedures in the above list operate differently from their
PostScript operator counterparts:

Procedure(header)	Difference(s)
-----------------	-------------

gs_makepattern(gscolor2.h)
			Takes an explicit current color, rather than using
			the current color in the graphics state.
			Takes an explicit allocator for allocating the
			pattern implementation.  See below for more details
			on gs_makepattern.

gs_setpattern(gscolor2.h)
gs_setcolor(gscolor2.h)
gs_currentcolor(gscolor2.h)
			Use gs_client_color rather than a set of color
			parameter values.  See below for more details on
			gs_setpattern.

gs_currentdash_length/pattern/offset(gsline.h)
			Splits up currentdash into 3 separate procedures.

gs_screen_init/currentpoint/next/install(gsht.h)
			Provide an "enumeration style" interface to
			setscreen.  (gs_setscreen is also implemented.)

gs_rotate/scale/translate(gscoord.h)
gs_[i][d]transform(gscoord.h)
			These always operate on the graphics state CTM.
			The corresponding operations on free-standing
			matrices are in gsmatrix.h and have different names.

gs_path_enum_alloc/init/next/cleanup(gspath.h)
			Provide an "enumeration style" implementation of
			pathforall.

gs_image_enum_alloc(gsimage.h)
gs_image_init/next/cleanup(gsimage.h)
			Provide an "enumeration style" interface to the
			equivalent of image, imagemask, and colorimage.
			In the gs_image_t, ColorSpace provides an explicit
			color space, rather than using the current color
			space in the graphics state; ImageMask distinguishes
			imagemask from [color]image.

gs_get/putdeviceparams(gsdevice.h)
			Take a gs_param_list for specifying or receiving
			the parameter values.  See gsparam.h for more
			details.

gs_show_enum_alloc/release(gschar.h)
gs_xxxshow_[n_]init(gschar.h)
gs_show_next(gschar.h)
			Provide an "enumeration style" interface to writing
			text.  Note that control returns to the caller if
			the character must be rasterized.

<< -------------------------------- end -------------------------------- >>

This level of the library also implements the following operators from other
sections of the Manual:

Section		Headers			Operators
-------		-------			---------

Interpreter Parameter
Operators
		gsfont.h		cachestatus, setcachelimit,
					  *set/currentcacheparams

Display PostScript Operators
		gsstate.h		set/currenthalftonephase

<< -------------------------------- end -------------------------------- >>

In order to obtain the full PostScript Level 2 functionality listed above,
FEATURE_DEVS must be set in the makefile to include at least the following:

  FEATURE_DEVS=patcore.dev cmykcore.dev psl2core.dev dps2core.dev ciecore.dev\
    path1core.dev hsbcore.dev

The *lib.mak makefiles mentioned below do not always include all of these
features.

Files named gs*.c implement the higher level of the graphics library.  As
might be expected, all procedures, variables, and structures available at
this level begin with gs_.  Structures that appear in these interfaces, but
whose definitions may be hidden from clients, also have names beginning with
gs_, i.e., the prefix reflects at what level the abstraction is made
available, not the implementation.

Patterns
--------

Patterns are the most complicated PostScript language objects that the
library API deals with.  As in PostScript, defining a pattern color and
using the color are two separate operations.

gs_makepattern defines a pattern color.  Its arguments are as follows:

	gs_client_color * -- the resulting Pattern color is stored here.
	  This is different from PostScript, which has no color objects per
	  se and hence returns a modified copy of the dictionary.

	const gs_client_pattern * -- the analogue of the original Pattern
	  dictionary, described in detail just below.

	const gs_matrix * -- corresponds to the matrix argument of the
	  makepattern operator.

	gs_state * -- the current graphics state.

	gs_memory_t * -- the allocator to use for allocating the saved data
	  for the Pattern color.  If this is NULL, gs_makepattern uses the
	  same allocator that allocated the graphics state.  Library clients
	  should probably always use NULL.

The gs_client_pattern structure defined in gscolor2.h corresponds to the
Pattern dictionary that is the argument to the PostScript language
makepattern operator.  This structure has one extra member, void
*client_data, which is a place for clients to store a pointer to additional
data for the PaintProc; this provides the same functionality as putting
additional keys in the Pattern dictionary at the PostScript language level.
The PaintProc is an ordinary C procedure that takes as parameters a
gs_client_color *, which is the Pattern color that is being used for
painting, and a gs_state *, which is the same graphics state that would be
presented to the PaintProc in PostScript.  Currently the gs_client_color *
is always the current color in the graphics state, but the PaintProc should
not rely on this.  The PaintProc can retrieve the gs_client_pattern * from
the gs_client_color * with the gs_getpattern procedure, also defined in
gscolor2.h, and from there, it can retrieve the client_data pointer.

The normal way to set a Pattern color is to call gs_setpattern with the
graphics state and with the gs_client_color returned by gs_makepattern.
After that, one can use gs_setcolor to set further Pattern colors (colored,
or uncolored with the same underlying color space); the rules are the same
as those in PostScript.  Note that for gs_setpattern, the paint.values in
the gs_client_color must be filled in for uncolored patterns; this
corresponds to the additional arguments for the PostScript setpattern
operator in the uncolored case.

There is a special procedure gs_makebitmappattern for creating bitmap-based
patterns.  Its API is documented in gscolor2.h; its implementation, in
gspcolor.c, may be useful as an example of a pattern using a particularly
simple PaintProc.

Lower-level API
---------------

Files named gx*.c implement the lower level of the graphics library.  The
interfaces at the gx level are less stable, and expose more of the
implementation detail, than those at the gs level: in particular, the gx
interfaces generally use device coordinates in an internal fixed-point
representation, as opposed to the gs interfaces that use floating point user
coordinates.  Named entities at this level begin with gx_.

Files named gz*.c and gz*.h are internal to the Ghostscript implementation,
and are not designed to be called by clients.

A full example
--------------

The file gslib.c in the Ghostscript fileset is a complete example program
that initializes the library and produces output using it; files named
*lib.mak (e.g., ugcclib.mak, bclib.mak) are makefiles using gslib.c as the
main program.  The following annotated excerpts from this file are intended
to provide a roadmap for applications that call the library.

    /* Capture stdin/out/err before gs.h redefines them. */
    #include <stdio.h>
    static FILE *real_stdin, *real_stdout, *real_stderr;
    static void
    get_real(void)
    {	real_stdin = stdin, real_stdout = stdout, real_stderr = stderr;
    }

Any application using Ghostscript should include the above fragment at the
very beginning of the main program.

    #include "gx.h"

The gx.h header includes a wealth of declarations related to the Ghostscript
memory manager, portability machinery, debugging framework, and other
substrate facilities.  Any application file that calls any Ghostscript API
functions should probably include gx.h.

    /* Configuration information imported from gconfig.c. */
    extern gx_device *gx_device_list[];

    /* Other imported procedures */
	    /* from gsinit.c */
    extern void gs_lib_init(P1(FILE *));
    extern void gs_lib_finit(P2(int, int));
	    /* from gsalloc.c */
    extern gs_ref_memory_t *ialloc_alloc_state(P2(gs_memory_t *, uint));

The above externs are needed for initializing the library.

	    gs_ref_memory_t *imem;
    #define mem ((gs_memory_t *)imem)
	    gs_state *pgs;
	    gx_device *dev = gx_device_list[0];

	    gp_init();
	    get_real();
	    gs_stdin = real_stdin;
	    gs_stdout = real_stdout;
	    gs_stderr = real_stderr;
	    gs_lib_init(stdout);
	    ....
	    imem = ialloc_alloc_state(&gs_memory_default, 20000);
	    imem->space = 0;
	    ....
	    pgs = gs_state_alloc(mem);

The above code initializes the library and its memory manager.  pgs now
points to the graphics state that will be passed to the drawing routines in
the library.

	    gs_setdevice_no_erase(pgs, dev);	/* can't erase yet */
	    {	gs_point dpi;
	    	gs_screen_halftone ht;
	    	gs_dtransform(pgs, 72.0, 72.0, &dpi);
	    	ht.frequency = min(fabs(dpi.x), fabs(dpi.y)) / 16.001;
	    	ht.angle = 0;
	    	ht.spot_function = odsf;
	    	gs_setscreen(pgs, &ht);
	    }

The above initializes the default device and sets a default halftone screen.
(For brevity, we have omitted the definition of odsf, the spot function.)

	    /* gsave and grestore (among other places) assume that */
	    /* there are at least 2 gstates on the graphics stack. */
	    /* Ensure that now. */
	    gs_gsave(pgs);

The above call completes initializing the graphics state.

When the program is finished, it should execute:

	    gs_lib_finit(0, 0);
