xref: /plan9/sys/src/cmd/gs/doc/Lib.htm (revision ff8c3af2f44d95267f67219afa20ba82ff6cf7e4)
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<title>The Ghostscript library</title>
5<!-- $Id: Lib.htm,v 1.14.2.2 2002/02/01 05:31:25 raph Exp $ -->
6<!-- Originally: lib.txt -->
7<link rel="stylesheet" type="text/css" href="gs.css" title="Ghostscript Style">
8</head>
9
10<body>
11<!-- [1.0 begin visible header] ============================================ -->
12
13<!-- [1.1 begin headline] ================================================== -->
14
15<h1>The Ghostscript Library</h1>
16
17<!-- [1.1 end headline] ==================================================== -->
18
19<!-- [1.2 begin table of contents] ========================================= -->
20
21<h2>Table of contents</h2>
22
23<blockquote><ul>
24<li><a href="#GS_library">The Ghostscript library</a>
25<li><a href="#PS_operator_API">PostScript operator API</a>
26<ul>
27<li><a href="#Patterns">Patterns</a>
28<li><a href="#Lower_level_API">Lower-level API</a>
29</ul>
30<li><a href="#Full_example">A full example</a>
31</ul></blockquote>
32
33<!-- [1.2 end table of contents] =========================================== -->
34
35<!-- [1.3 begin hint] ====================================================== -->
36
37<p>For other information, see the <a href="Readme.htm">Ghostscript
38overview</a>.
39
40<!-- [1.3 end hint] ======================================================== -->
41
42<hr>
43
44<!-- [1.0 end visible header] ============================================== -->
45
46<!-- [2.0 begin contents] ================================================== -->
47
48<h2><a name="GS_library"></a>The Ghostscript library</h2>
49
50<p>
51This document describes the Ghostscript library, a set of procedures
52to implement the graphics and filtering capabilities that are primitive
53operations in the PostScript language and in Adobe Portable Document Format
54(PDF).
55
56<p>
57Ghostscript is actually two programs: a language interpreter, and a
58graphics library.  The library provides, in the form of C procedures, all
59the graphics functions of the language, that is, approximately those
60facilities listed in section 8.1 of the <cite>PostScript
61Language Reference Manual</cite>, starting with the
62graphics state operators.  In addition, the library provides some
63lower-level graphics facilities that offer higher performance in exchange
64for less generality.
65
66<hr>
67
68<h2><a name="PS_operator_API"></a>PostScript operator API</h2>
69
70<p>
71The highest level of the library, which is the one that most clients will
72use, directly implements the PostScript graphics operators with procedures
73named <b><tt>gs_XXX</tt></b>, for instance <b><tt>gs_moveto</tt></b> and
74<b><tt>gs_fill</tt></b>.  Nearly all of these procedures take graphics
75state objects as their first arguments, such as
76
77<blockquote><b><tt>
78int gs_moveto(gs_state *, double, double);
79</tt></b></blockquote>
80
81<p>
82Nearly every procedure returns an integer code which is &gt;= 0 for a
83successful return or &lt;0 for a failure.  The failure codes correspond
84directly to PostScript errors, and are defined in
85<b><tt>gserrors.h</tt></b>.
86
87<p>
88The library implements all the operators in the following sections of the
89<cite>PostScript Language Reference Manual</cite>, with the indicated
90omissions and with the APIs defined in the indicated <b><tt>.h</tt></b>
91files.  A header of the form <b><em>A.h(B.h)</em></b> indicates that
92<b><em>A.h</em></b> is included in <b><em>B.h</em></b>, so
93<b><em>A.h</em></b> need not be included explicitly if <b><em>B.h</em></b>
94is included.  Operators marked with * in the "omissions" column are not
95implemented directly; the library provides lower-level procedures that can
96be used to implement the operator.
97
98<p>
99There are slight differences in the operators that return multiple values,
100since C's provisions for this are awkward.  Also, the control structure for
101the operators involving callback procedures (<b><tt>pathforall</tt></b>,
102<b><tt>image</tt></b>, <b><tt>colorimage</tt></b>,
103<b><tt>imagemask</tt></b>) is partly inverted: the client calls a procedure
104to set up an enumerator object, and then calls another procedure for each
105iteration.  The <b><tt>...show</tt></b> operators,
106<b><tt>charpath</tt></b>, and <b><tt>stringwidth</tt></b> also use an
107inverted control structure.
108
109<blockquote><table cellpadding=0 cellspacing=0>
110<tr valign=bottom>
111	<th align=left>Section<br>(operators)
112	<td>&nbsp;&nbsp;
113	<th align=left>Headers
114	<td>&nbsp;&nbsp;
115	<th align=left>Omissions
116<tr>	<td colspan=5><hr>
117<tr valign=top>	<td>Graphics state -- device-independent
118	<td>&nbsp;
119	<td><b><tt>gscolor.h</tt></b>(<b><tt>gsstate.h</tt></b>)<br><b><tt>gscolor1.h</tt></b><br><b><tt>gscolor2.h</tt></b><br><b><tt>gscspace.h</tt></b><br><b><tt>gshsb.h</tt></b><br><b><tt>gsline.h</tt></b>(<b><tt>gsstate.h</tt></b>)<br><b><tt>gsstate.h</tt></b>
120	<td>&nbsp;
121	<td>&nbsp;
122<tr>	<td>&nbsp;
123<tr valign=top>	<td>Graphics state -- device-dependent
124	<td>&nbsp;
125	<td><b><tt>gscolor.h</tt></b>(<b><tt>gsstate.h</tt></b>)<br><b><tt>gscolor1.h</tt></b><br><b><tt>gscolor2.h</tt></b><br><b><tt>gsht.h</tt></b>(<b><tt>gsht1.h</tt></b>,<b><tt>gsstate.h</tt></b>)<br><b><tt>gsht1.h</tt></b><br><b><tt>gsline.h</tt></b>(<b><tt>gsstate.h</tt></b>)
126	<td>&nbsp;
127	<td>&nbsp;
128<tr>	<td>&nbsp;
129<tr valign=top>	<td>Coordinate system and matrix
130	<td>&nbsp;
131	<td><b><tt>gscoord.h</tt></b><br><b><tt>gsmatrix.h</tt></b>
132	<td>&nbsp;
133	<td><b><tt>*matrix</tt></b>, <b><tt>*identmatrix</tt></b>, <b><tt>*concatmatrix</tt></b>, <b><tt>*invertmatrix</tt></b>
134<tr>	<td>&nbsp;
135<tr valign=top>	<td>Path construction
136	<td>&nbsp;
137	<td><b><tt>gspath.h</tt></b><br><b><tt>gspath2.h</tt></b>
138	<td>&nbsp;
139	<td><b><tt>*arct</tt></b>, <b><tt>*pathforall</tt></b>, <b><tt>ustrokepath</tt></b>, <b><tt>uappend</tt></b>, <b><tt>upath</tt></b>, <b><tt>ucache</tt></b>
140<tr>	<td>&nbsp;
141<tr valign=top>	<td>Painting
142	<td>&nbsp;
143	<td><b><tt>gsimage.h</tt></b><br><b><tt>gspaint.h</tt></b><br><b><tt>gspath2.h</tt></b>
144	<td>&nbsp;
145	<td><b><tt>*image</tt></b>, <b><tt>*colorimage</tt></b>, <b><tt>*imagemask</tt></b>, <b><tt>ufill</tt></b>, <b><tt>ueofill</tt></b>, <b><tt>ustroke</tt></b>
146<tr>	<td>&nbsp;
147<tr valign=top>	<td>Form and pattern
148	<td>&nbsp;
149	<td><b><tt>gscolor2.h</tt></b>
150	<td>&nbsp;
151	<td><b><tt>execform</tt></b>
152<tr>	<td>&nbsp;
153<tr valign=top>	<td>Device setup and output
154	<td>&nbsp;
155	<td><b><tt>gsdevice.h</tt></b>
156	<td>&nbsp;
157	<td><b><tt>*showpage</tt></b>, <b><tt>*set</tt></b>/<b><tt>currentpagedevice</tt></b>
158<tr>	<td>&nbsp;
159<tr valign=top>	<td>Character and font
160	<td>&nbsp;
161	<td><b><tt>gschar.h</tt></b><br><b><tt>gsfont.h</tt></b>
162	<td>&nbsp;
163	<td>*(all the <b><tt>show</tt></b> operators), <b><tt>definefont</tt></b>, <b><tt>undefinefont</tt></b>, <b><tt>findfont</tt></b>, <b><tt>*scalefont</tt></b>, <b><tt>*makefont</tt></b>, <b><tt>selectfont</tt></b>, <b><tt>[Global]FontDirectory</tt></b>, <b><tt>Standard</tt></b>/<b><tt>ISOLatin1Encoding</tt></b>, <b><tt>findencoding</tt></b>
164</table></blockquote>
165
166<p>
167The following procedures from the list above operate differently from their
168PostScript operator counterparts, as explained here:
169
170<dl>
171<dt><b><tt>gs_makepattern(gscolor2.h)</tt></b>
172<dd>Takes an explicit current color, rather than using the current color in
173the graphics state.  Takes an explicit allocator for allocating the pattern
174implementation.  See below for more details on
175<b><tt>gs_makepattern</tt></b>.
176</dl>
177
178<dl>
179<dt><b><tt>gs_setpattern(gscolor2.h)</tt></b>
180<dt><b><tt>gs_setcolor(gscolor2.h)</tt></b>
181<dt><b><tt>gs_currentcolor(gscolor2.h)</tt></b>
182<dd>Use <b><tt>gs_client_color</tt></b> rather than a set of color
183parameter values.  See below for more details on
184<b><tt>gs_setpattern</tt></b>.
185</dl>
186
187<dl>
188<dt><b><tt>gs_currentdash_length/pattern/offset(gsline.h)</tt></b>
189<dd>Splits up <b><tt>currentdash</tt></b> into three separate procedures.
190</dl>
191
192<dl>
193<dt><b><tt>gs_screen_init/currentpoint/next/install(gsht.h)</tt></b>
194<dd>Provide an "enumeration style" interface to <b><tt>setscreen</tt></b>.
195(<b><tt>gs_setscreen</tt></b> is also implemented.)
196</dl>
197
198<dl>
199<dt><b><tt>gs_rotate/scale/translate(gscoord.h)</tt></b>
200<dt><b><tt>gs_[i][d]transform(gscoord.h)</tt></b>
201<dd>These always operate on the graphics state CTM.  The corresponding
202operations on free-standing matrices are in <b><tt>gsmatrix.h</tt></b> and
203have different names.
204</dl>
205
206<dl>
207<dt><b><tt>gs_path_enum_alloc/init/next/cleanup(gspath.h)</tt></b>
208<dd>Provide an "enumeration style" implementation of
209<b><tt>pathforall</tt></b>.
210</dl>
211
212<dl>
213<dt><b><tt>gs_image_enum_alloc(gsimage.h)</tt></b>
214<dt><b><tt>gs_image_init/next/cleanup(gsimage.h)</tt></b>
215<dd>Provide an "enumeration style" interface to the equivalent of
216<b><tt>image</tt></b>, <b><tt>imagemask</tt></b>, and
217<b><tt>colorimage</tt></b>.  In the <b><tt>gs_image_t</tt></b>,
218<b><tt>ColorSpace</tt></b> provides an explicit color space, rather than
219using the current color space in the graphics state;
220<b><tt>ImageMask</tt></b> distinguishes <b><tt>imagemask</tt></b> from
221<b><tt>[color]image</tt></b>.
222</dl>
223
224<dl>
225<dt><b><tt>gs_get/putdeviceparams(gsdevice.h)</tt></b>
226<dd>Take a <b><tt>gs_param_list</tt></b> for specifying or receiving the
227parameter values.  See <b><tt>gsparam.h</tt></b> for more details.
228</dl>
229
230<dl>
231<dt><b><tt>gs_show_enum_alloc/release(gschar.h)</tt></b>
232<dt><b><tt>gs_xxxshow_[n_]init(gschar.h)</tt></b>
233<dt><b><tt>gs_show_next(gschar.h)</tt></b>
234<dd>Provide an "enumeration style" interface to writing text.  Note that
235control returns to the caller if the character must be rasterized.
236</dl>
237
238<p>
239This level of the library also implements the following operators from other
240sections of the Manual:
241
242<blockquote><table cellpadding=0 cellspacing=0>
243<tr valign=bottom>
244	<th align=left>Section<br>(operators)
245	<td>&nbsp;&nbsp;
246	<th align=left>Headers
247	<td>&nbsp;&nbsp;
248	<th align=left>Operators
249<tr>	<td colspan=5><hr>
250<tr valign=top>	<td>Interpreter parameter
251	<td>&nbsp;
252	<td><b><tt>gsfont.h</tt></b>
253	<td>&nbsp;
254	<td><b><tt>cachestatus</tt></b>, <b><tt>setcachelimit</tt></b>, <b><tt>*set/currentcacheparams</tt></b>
255<tr valign=top>	<td>Display PostScript
256	<td>&nbsp;
257	<td><b><tt>gsstate.h</tt></b>
258	<td>&nbsp;
259	<td><b><tt>set/currenthalftonephase</tt></b>
260</table></blockquote>
261
262<p>
263In order to obtain the full PostScript Level 2 functionality listed above,
264<b><tt>FEATURE_DEVS</tt></b> must be set in the makefile to include at least the following:
265
266<blockquote><b><tt>
267FEATURE_DEVS=patcore.dev cmykcore.dev psl2core.dev dps2core.dev ciecore.dev path1core.dev hsbcore.dev
268</tt></b></blockquote>
269
270<p>
271The <b><tt>*lib.mak</tt></b> makefiles mentioned below do not always
272include all of these features.
273
274<p>
275Files named <b><tt>gs*.c</tt></b> implement the higher level of the
276graphics library.  As might be expected, all procedures, variables, and
277structures available at this level begin with "<b><tt>gs_</tt></b>".
278Structures that appear in these interfaces, but whose definitions may be
279hidden from clients, also have names beginning with "<b><tt>gs_</tt></b>",
280that is, the prefix, not the implementation, reflects at what level the
281abstraction is made available.
282
283<h3><a name="Patterns"></a>Patterns</h3>
284
285<p>
286Patterns are the most complicated PostScript language objects that the
287library API deals with.  As in PostScript, defining a pattern color and
288using the color are two separate operations.
289
290<p>
291<b><tt>gs_makepattern</tt></b> defines a pattern color.  Its arguments are as follows:
292
293<blockquote><table cellpadding=0 cellspacing=0>
294<tr valign=top>	<td><b><tt>gs_client_color *</tt></b>
295	<td>&nbsp;&nbsp;&nbsp;
296	<td>The resulting <b><tt>Pattern</tt></b> color is stored here.  This is different from PostScript, which has no color objects <em>per se</em>, and hence returns a modified copy of the dictionary.
297<tr valign=top>	<td><b><tt>const gs_client_pattern *</tt></b>
298	<td>&nbsp;
299	<td>The analogue of the original <b><tt>Pattern</tt></b> dictionary, described in detail just below.
300<tr valign=top>	<td><b><tt>const gs_matrix *</tt></b>
301	<td>&nbsp;
302	<td>Corresponds to the matrix argument of the <b><tt>makepattern</tt></b> operator.
303<tr valign=top>	<td><b><tt>gs_state *</tt></b>
304	<td>&nbsp;
305	<td>The current graphics state.
306<tr valign=top>	<td><b><tt>gs_memory_t *</tt></b>
307	<td>&nbsp;
308	<td>The allocator to use for allocating the saved data for the
309	    <b><tt>Pattern</tt></b> color.  If this is
310	    <b><tt>NULL</tt></b>, <b><tt>gs_makepattern</tt></b> uses the
311	    same allocator that allocated the graphics state.  Library
312	    clients should probably always use <b><tt>NULL</tt></b>.
313
314</table></blockquote>
315
316<p>
317The <b><tt>gs_client_pattern</tt></b> structure defined in
318<b><tt>gscolor2.h</tt></b> corresponds to the <b><tt>Pattern</tt></b>
319dictionary that is the argument to the PostScript language
320<b><tt>makepattern</tt></b> operator.  This structure has one extra member,
321<b><tt>void&nbsp;*client_data</tt></b>, which is a place for clients to
322store a pointer to additional data for the <b><tt>PaintProc</tt></b>; this
323provides the same functionality as putting additional keys in the
324<b><tt>Pattern</tt></b> dictionary at the PostScript language level.  The
325<b><tt>PaintProc</tt></b> is an ordinary C procedure that takes as
326parameters a <b><tt>gs_client_color&nbsp;*</tt></b>, which is the
327<b><tt>Pattern</tt></b> color that is being used for painting, and a
328<b><tt>gs_state&nbsp;*</tt></b>, which is the same graphics state that
329would be presented to the <b><tt>PaintProc</tt></b> in PostScript.
330Currently the <b><tt>gs_client_color&nbsp;*</tt></b> is always the current
331color in the graphics state, but the <b><tt>PaintProc</tt></b> should not
332rely on this.  The <b><tt>PaintProc</tt></b> can retrieve the
333<b><tt>gs_client_pattern&nbsp;*</tt></b> from the
334<b><tt>gs_client_color&nbsp;*</tt></b> with the
335<b><tt>gs_getpattern</tt></b> procedure, also defined in
336<b><tt>gscolor2.h</tt></b>, and from there, it can retrieve the
337<b><tt>client_data</tt></b> pointer.
338
339<p>
340The normal way to set a <b><tt>Pattern</tt></b> color is to call
341<b><tt>gs_setpattern</tt></b> with the graphics state and with the
342<b><tt>gs_client_color</tt></b> returned by <b><tt>gs_makepattern</tt></b>.
343After that, one can use <b><tt>gs_setcolor</tt></b> to set further
344<b><tt>Pattern</tt></b> colors (colored, or uncolored with the same
345underlying color space); the rules are the same as those in PostScript.
346Note that for <b><tt>gs_setpattern</tt></b>, the
347<b><tt>paint.values</tt></b> in the <b><tt>gs_client_color</tt></b> must be
348filled in for uncolored patterns; this corresponds to the additional
349arguments for the PostScript <b><tt>setpattern</tt></b> operator in the
350uncolored case.
351
352<p>
353There is a special procedure <b><tt>gs_makebitmappattern</tt></b> for creating bitmap-based
354patterns.  Its API is documented in <b><tt>gscolor2.h</tt></b>; its implementation, in
355<b><tt>gspcolor.c</tt></b>, may be useful as an example of a pattern using a particularly
356simple <b><tt>PaintProc.</tt></b>
357
358<h3><a name="Lower_level_API"></a>Lower-level API</h3>
359
360<p>
361Files named <b><tt>gx*.c</tt></b> implement the lower level of the graphics
362library.  The interfaces at the <b><tt>gx</tt></b> level are less stable,
363and expose more of the implementation detail, than those at the
364<b><tt>gs</tt></b> level: in particular, the <b><tt>gx</tt></b> interfaces
365generally use device coordinates in an internal fixed-point representation,
366as opposed to the <b><tt>gs</tt></b> interfaces that use floating point
367user coordinates.  Named entities at this level begin with
368<b><tt>gx_</tt></b>.
369
370<p>
371Files named <b><tt>gz*.c</tt></b> and <b><tt>gz*.h</tt></b> are internal to
372the Ghostscript implementation, and are not designed to be called by
373clients.
374
375<hr>
376
377<h2><a name="Full_example"></a>A full example</h2>
378
379<p>
380The file <b><tt>gslib.c</tt></b> in the Ghostscript fileset is a complete
381example program that initializes the library and produces output using it;
382files named <b><tt>*lib.mak</tt></b> (such as <b><tt>ugcclib.mak</tt></b>
383and <b><tt>bclib.mak</tt></b>) are makefiles using <b><tt>gslib.c</tt></b>
384as the main program.  The following annotated excerpts from this file are
385intended to provide a roadmap for applications that call the library.
386
387<blockquote><pre>/* Capture stdin/out/err before gs.h redefines them. */
388#include &lt;stdio.h&gt;
389static FILE *real_stdin, *real_stdout, *real_stderr;
390static void
391get_real(void)
392{       real_stdin = stdin, real_stdout = stdout, real_stderr = stderr;
393}</pre></blockquote>
394
395<p>
396Any application using Ghostscript should include the fragment above at the
397very beginning of the main program.
398
399<blockquote><pre>#include "gx.h"</pre></blockquote>
400
401<p>
402The <b><tt>gx.h</tt></b> header includes a wealth of declarations related
403to the Ghostscript memory manager, portability machinery, debugging
404framework, and other substrate facilities.  Any application file that calls
405any Ghostscript API functions should probably include <b><tt>gx.h</tt></b>.
406
407<blockquote><pre>/* Configuration information imported from gconfig.c. */
408extern gx_device *gx_device_list[];
409
410/* Other imported procedures */
411        /* from gsinit.c */
412extern void gs_lib_init(P1(FILE *));
413extern void gs_lib_finit(P2(int, int));
414        /* from gsalloc.c */
415extern gs_ref_memory_t *ialloc_alloc_state(P2(gs_memory_t *, uint));</pre></blockquote>
416
417<p>
418The externs above are needed for initializing the library.
419
420<blockquote><pre>        gs_ref_memory_t *imem;
421#define mem ((gs_memory_t *)imem)
422        gs_state *pgs;
423        gx_device *dev = gx_device_list[0];
424
425        gp_init();
426        get_real();
427        gs_stdin = real_stdin;
428        gs_stdout = real_stdout;
429        gs_stderr = real_stderr;
430        gs_lib_init(stdout);
431        ....
432        imem = ialloc_alloc_state(&amp;gs_memory_default, 20000);
433        imem-&gt;space = 0;
434        ....
435        pgs = gs_state_alloc(mem);</pre></blockquote>
436
437<p>
438The code above initializes the library and its memory manager.  <b><tt>pgs</tt></b> now
439points to the graphics state that will be passed to the drawing routines in
440the library.
441
442<blockquote><pre>        gs_setdevice_no_erase(pgs, dev);    /* can't erase yet */
443        {   gs_point dpi;
444            gs_screen_halftone ht;
445            gs_dtransform(pgs, 72.0, 72.0, &amp;dpi);
446            ht.frequency = min(fabs(dpi.x), fabs(dpi.y)) / 16.001;
447            ht.angle = 0;
448            ht.spot_function = odsf;
449            gs_setscreen(pgs, &amp;ht);
450        }</pre></blockquote>
451
452<p>
453The code above initializes the default device and sets a default halftone
454screen.  (For brevity, we have omitted the definition of odsf, the spot
455function.)
456
457<blockquote><pre>        /* gsave and grestore (among other places) assume that */
458        /* there are at least 2 gstates on the graphics stack. */
459        /* Ensure that now. */
460        gs_gsave(pgs);</pre></blockquote>
461
462<p>
463The call above completes initializing the graphics state.  When the program
464is finished, it should execute:
465
466<blockquote><pre>        gs_lib_finit(0, 0);</pre></blockquote>
467
468<!-- [2.0 end contents] ==================================================== -->
469
470<!-- [3.0 begin visible trailer] =========================================== -->
471<hr>
472
473<p>
474<small>Copyright &copy; 1996, 1997, 1998 Aladdin Enterprises.
475All rights reserved.</small>
476
477<p>
478<small>This file is part of AFPL Ghostscript.  See the
479<a href="Public.htm">Aladdin Free Public License</a> (the "License") for
480full details of the terms of using, copying, modifying, and redistributing
481AFPL Ghostscript.</small>
482
483<p>
484<small>Ghostscript version 7.04, 31 January 2002
485
486<!-- [3.0 end visible trailer] ============================================= -->
487
488</body>
489</html>
490