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 Interpreter Application Programming Interface (API)</title> 5<!-- $Id: API.htm,v 1.11.2.3 2002/02/01 06:16:22 giles Exp $ --> 6<!-- Supercedes the API in DLL.htm --> 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 Interpreter Application Programming Interface (API)</h1> 16 17<!-- [1.1 end headline] ==================================================== --> 18 19<!-- [1.2 begin table of contents] ========================================= --> 20 21<h2>Table of contents</h2> 22 23<ul> 24<li><a href="#API">What is the Ghostscript Interpreter API?</a> 25<li><a href="#Exported_functions ">Exported functions</a> 26<ul> 27<li><a href="#revision"><b><tt>gsapi_revision</tt></b></a> 28<li><a href="#new_instance"><b><tt>gsapi_new_instance</tt></b></a> 29<li><a href="#delete_instance"><b><tt>gsapi_delete_instance</tt></b></a> 30<li><a href="#set_stdio"><b><tt>gsapi_set_stdio</tt></b></a> 31<li><a href="#set_poll"><b><tt>gsapi_set_poll</tt></b></a> 32<li><a href="#set_display_callback"><b><tt>gsapi_set_display_callback</tt></b></a> 33<li><a href="#init"><b><tt>gsapi_init_with_args</tt></b></a> 34<li><a href="#run"><b><tt>gsapi_run_*</tt></b></a> 35<li><a href="#exit"><b><tt>gsapi_exit</tt></b></a> 36<li><a href="#return_codes">Return codes</a> 37</ul> 38<li><a href="#Typical_usage">Typical usage</a> 39<li><a href="#stdio">Standard input and output</a> 40<li><a href="#display">Display device</a> 41</ul> 42 43<!-- [1.2 end table of contents] =========================================== --> 44 45<!-- [1.3 begin hint] ====================================================== --> 46 47<p>For other information, see the <a href="Readme.htm">Ghostscript 48overview</a>. 49 50<p> 51<b>WARNING:</b> The API described in this document is subject to changes in 52future releases, possibly ones that are not backward compatible with what 53is described here. 54 55<!-- [1.3 end hint] ======================================================== --> 56 57<hr> 58 59<!-- [1.0 end visible header] ============================================== --> 60 61<!-- [2.0 begin contents] ================================================== --> 62 63 64<h2><a name="API"></a>What is the Ghostscript Interpreter API?</h2> 65 66<p> 67The Ghostscript interpreter can be built as a dynamic link library 68(DLL) on the Windows or OS/2 platforms, as a shared object on the 69Linux platform and as a framework on MacOS X. 70With some changes, it could be built as a static library. 71This document describes the Application Programming Interface (API) 72for the Ghostscript interpreter library. 73This should not be confused with the 74<a href="Lib.htm">Ghostscript library</a> which provides a graphics 75library but not the interpreter. 76<p> 77This supercedes the old <a href="DLL.htm">DLL</a> interface. 78<p> 79To provide the interface described in the 80<a href="Use.htm">usage documentation</a>, a smaller independent 81executable loads the DLL/shared object. 82This executable provides all the interaction with the windowing system, 83including image windows and, if necessary, a text window. 84 85<p> 86The Ghostscript interpreter library's name and characteristics differ 87for each platform: 88 89<ul> 90<li>The Win32 DLL <b><tt>gsdll32.dll</tt></b> has 91MULTIPLE NONSHARED data segments. Under Win32s it can be used by only one 92program at a time, but under Windows 95/98 or Windows NT it can be called by 93multiple programs simultaneously. 94 95<li>The OS/2 DLL <b><tt>gsdll2.dll</tt></b> has 96MULTIPLE NONSHARED data segments and can be called by multiple programs 97simultaneously. 98 99<li>The Linux shared object <b><tt>libgs.so</tt></b> 100can be used by multiple programs simultaneously. 101 102<li>The MacOS X <b><tt>Ghostscript.framework</tt></b> can also be used 103by multiple applications at once. 104</ul> 105 106<p> 107The source for the executable is in <b><tt>dw</tt></b>*.* (Windows), 108<b><tt>dp</tt></b>*.* (OS/2) and <b><tt>dx</tt></b>*.* (Linux). 109See these source files for examples of how to use the DLL. 110 111The source file <p><tt>dxmainc.c</tt> can serve as an example of how to use the 112framework shared component on MacOS X. Just change the header includes to use 113the Ghostscript namespace: 114<blockquote><pre><tt>#include <Ghostscript/errors.h> 115#include <Ghostscript/iapi.h></tt></pre></blockquote> 116and link with: 117<blockquote><pre><tt>cc -o gsc dxmainc.c -framework Ghostscript</tt></pre></blockquote> 118To get a useable executable. <tt>Ghostscript.framework</tt> must be properly 119installed in the search path for this to work. 120 121<p> 122At this stage, Ghostscript does not support multiple instances 123of the interpreter within a single process. 124 125<hr> 126 127<h2><a name="Exported_functions"></a>Exported functions</h2> 128 129<p> 130The functions exported by the DLL/shared object are described 131in the header file <a href="../src/iapi.h"><b><tt>iapi.h</tt></b></a> 132and are summarised below. Omitted from the summary are 133the calling convention (e.g. __stdcall), details of return 134values and error handling. 135 136 137<ul> 138<li><b><tt> 139int 140<a href="#revision">gsapi_revision</a> 141 (gsapi_revision_t *pr, int len)); 142</tt></b> 143 144<li><b><tt> 145int 146<a href="#new_instance">gsapi_new_instance</a> 147(gs_main_instance **pinstance, void *caller_handle); 148</tt></b> 149 150<li><b><tt> 151void 152<a href="#delete_instance">gsapi_delete_instance</a> 153(gs_main_instance *instance); 154</tt></b> 155 156<li><b><tt> 157int 158<a href="#set_stdio">gsapi_set_stdio</a> 159(gs_main_instance *instance, 160 int(*stdin_fn)(void *caller_handle, char *buf, int len), 161 int(*stdout_fn)(void *caller_handle, const char *str, int len), 162 int(*stderr_fn)(void *caller_handle, const char *str, int len)); 163</tt></b> 164 165<li><b><tt> 166int 167<a href="#set_poll">gsapi_set_poll</a> 168(gs_main_instance *instance, int(*poll_fn)(void *caller_handle)); 169</tt></b> 170 171<li><b><tt> 172int 173<a href="#set_display_callback">gsapi_set_display_callback</a> 174(gs_main_instance *instance, display_callback *callback); 175</tt></b> 176 177<li><b><tt> 178int 179<a href="#init">gsapi_init_with_args</a> 180(gs_main_instance *instance, int argc, char **argv); 181</tt></b> 182 183<li><b><tt> 184int 185<a href="#run">gsapi_run_string_begin</a> 186(gs_main_instance *instance, int user_errors, int *pexit_code); 187</tt></b> 188 189<li><b><tt> 190int 191<a href="#run">gsapi_run_string_continue</a> 192(gs_main_instance *instance, 193 const char *str, unsigned int length, int user_errors, int *pexit_code); 194</tt></b> 195 196<li><b><tt> 197int 198<a href="#run">gsapi_run_string_end</a> 199(gs_main_instance *instance, int user_errors, int *pexit_code); 200</tt></b> 201 202<li><b><tt> 203int 204<a href="#run">gsapi_run_string_with_length</a> 205(gs_main_instance *instance, 206 const char *str, unsigned int length, int user_errors, int *pexit_code); 207</tt></b> 208 209<li><b><tt> 210int 211<a href="#run">gsapi_run_string</a> 212(gs_main_instance *instance, 213 const char *str, int user_errors, int *pexit_code); 214</tt></b> 215 216<li><b><tt> 217int 218<a href="#run">gsapi_run_file</a> 219(gs_main_instance *instance, 220 const char *file_name, int user_errors, int *pexit_code); 221</tt></b> 222 223<li><b><tt> 224int 225<a href="#exit">gsapi_exit</a> 226(gs_main_instance *instance); 227</tt></b> 228 229</ul> 230 231<h3><a name="revision"></a><b><tt>gsapi_revision()</tt></b></h3> 232 233<blockquote> 234This function returns the revision numbers and strings of the Ghostscript 235interpreter library; you should call it before any other interpreter 236library functions to make sure that the correct version of the 237Ghostscript interpreter has been loaded. 238 239<blockquote> 240<pre> 241typedef struct gsapi_revision_s { 242 const char *product; 243 const char *copyright; 244 long revision; 245 long revisiondate; 246} gsapi_revision_t; 247gsapi_revision_t r; 248 249if (gsapi_revision(&r, sizeof(r)) == 0) { 250 if (r.revision < 650) 251 printf("Need at least Ghostscript 6.50"); 252} 253else { 254 printf("revision structure size is incorrect"); 255} 256</pre></blockquote> 257</blockquote> 258 259 260<h3><a name="new_instance"></a><b><tt>gsapi_new_instance()</tt></b></h3> 261<blockquote> 262Create a new instance of Ghostscript. 263This instance is passed to most other gsapi functions. 264The caller_handle will be provided to callback functions. 265<b>At this stage, Ghostscript supports only one instance</b>. 266</blockquote> 267 268 269<h3><a name="delete_instance"></a><b><tt>gsapi_delete_instance()</tt></b></h3> 270<blockquote> 271Destroy an instance of Ghostscript. 272Before you call this, Ghostscript must have finished. 273If Ghostscript has been initialised, you must call 274<b><tt>gsapi_exit</tt></b> before <b><tt>gsapi_delete_instance</tt></b>. 275</blockquote> 276 277 278<h3><a name="set_stdio"></a><b><tt>gsapi_set_stdio()</tt></b></h3> 279<blockquote> 280Set the callback functions for stdio 281The stdin callback function should return the number of 282characters read, 0 for EOF, or -1 for error. 283The stdout and stderr callback functions should return 284the number of characters written. 285</blockquote> 286 287 288<h3><a name="set_poll"></a><b><tt>gsapi_set_poll()</tt></b></h3> 289<blockquote> 290Set the callback function for polling. 291This is used for handling window events or cooperative 292multitasking. This function will only be called if 293the Ghostscript interpreter was compiled with <b><tt>CHECK_INTERRUPTS</tt></b> 294as described in <b><tt><a href="../src/gpcheck.h">gpcheck.h</a></tt></b>. 295</blockquote> 296 297<h3><a name="set_display_callback"></a><b><tt>gsapi_set_display_callback()</tt></b></h3> 298<blockquote> 299Set the callback structure for the <a href="#display">display</a> 300device. If the <a href="#display">display</a> device is used, 301this must be called after 302<b><tt>gsapi_new_instance()</tt></b> 303and before <b><tt>gsapi_init_with_args()</tt></b>. 304See <b><tt><a href="../src/gdevdsp.h">gdevdsp.h</a></tt></b> 305for more details. 306</blockquote> 307 308<h3><a name="init"></a><b><tt>gsapi_init_with_args()</tt></b></h3> 309<blockquote> 310Initialise the interpreter. 311This calls <b><tt>gs_main_init_with_args()</tt></b> in 312<b><tt><a href="../src/imainarg.c">imainarg.c</a></tt></b>. 313See below for <a href="#return_codes">return codes</a>. 314</blockquote> 315 316<h3><a name="run"></a><b><tt>gsapi_run_*()</tt></b></h3> 317<blockquote> 318The <b><tt>gsapi_run_*</tt></b> functions are like 319<b><tt>gs_main_run_*</tt></b> except that the error_object is omitted. 320If these functions return <= -100, either quit or a fatal 321error has occured. You must call <b><tt>gsapi_exit()</tt></b> next. 322The only exception is <b><tt>gsapi_run_string_continue()</tt></b> 323which will return <b><tt>e_NeedInput</tt></b> if all is well. 324See below for <a href="#return_codes">return codes</a>. 325</blockquote> 326 327<h3><a name="exit"></a><b><tt>gsapi_exit()</tt></b></h3> 328<blockquote> 329Exit the interpreter. 330This must be called on shutdown if <b><tt>gsapi_init_with_args()</tt></b> 331has been called, and just before <b><tt>gsapi_delete_instance()</tt></b>. 332</blockquote> 333 334<h3><a name="return_codes"></a>Return codes</h3> 335 336<p> 337The <b><tt>gsapi_init_with_args</tt></b>, <b><tt>gsapi_run_*</tt></b> and 338<b><tt>gsapi_exit</tt></b> functions return an integer code. 339 340<p> 341<table width="80%" align="center" cellpadding=0 cellspacing=0> 342<tr><th colspan=3 bgcolor="#CCCC00"><hr><font size="+1">Return codes from <b><tt>gsapi_*()</tt></b></font><hr> 343<tr valign=bottom> 344 <th align=left>Code 345 <td> 346 <th align=left>Status 347<tr> <td colspan=3><hr> 348<tr valign=top> <td align=left>0 349 <td> 350 <td>No errors 351<tr valign=top> <td align=left>e_Quit 352 <td> 353 <td>"<b><tt>quit</tt></b>" has been executed. 354 This is not an error. 355 <b><tt>gsapi_exit()</tt></b> must be called next. 356<tr valign=top> <td align=left>e_NeedInput 357 <td> 358 <td>More input is needed by 359 <b><tt>gsapi_run_string_continue()</tt></b>. 360 This is not an error. 361<tr valign=top> <td align=left>e_Info 362 <td> 363 <td>"<b><tt>gs -h</tt></b>" has been executed. 364 This is not an error. 365 <b><tt>gsapi_exit()</tt></b> must be called next. 366<tr valign=top> <td align=left>< 0 367 <td> 368 <td>Error 369<tr valign=top> <td align=left><= -100 370 <td> 371 <td>Fatal error. 372<b><tt>gsapi_exit()</tt></b> must be called next. 373</table> 374</blockquote> 375 376<p> 377The <b><tt>gsapi_run_*()</tt></b> functions do not flush stdio. 378If you want to see output from Ghostscript you 379must do this explicitly as shown in the example below. 380 381<p> 382When executing a string with <b><tt>gsapi_run_string_*()</tt></b>, 383<b><tt>currentfile</tt></b> is the input from the string. 384Reading from <b><tt>%stdin</tt></b> uses the stdin callback. 385</blockquote> 386 387 388 389<h2><a name="Typical_usage"></a>Typical Usage</h2> 390A really short example of using the Ghostscript interpreter library is: 391 392<pre> 393#include <stdio.h> 394#include "errors.h" 395#include "iapi.h" 396 397/* stdio functions */ 398static int 399gsdll_stdin(void *instance, char *buf, int len) 400{ 401 int ch; 402 int count = 0; 403 while (count < len) { 404 ch = fgetc(stdin); 405 if (ch == EOF) 406 return 0; 407 *buf++ = ch; 408 count++; 409 if (ch == '\n') 410 break; 411 } 412 return count; 413} 414 415static int 416gsdll_stdout(void *instance, const char *str, int len) 417{ 418 fwrite(str, 1, len, stdout); 419 fflush(stdout); 420 return len; 421} 422 423static int 424gsdll_stderr(void *instance, const char *str, int len) 425{ 426 fwrite(str, 1, len, stderr); 427 fflush(stderr); 428 return len; 429} 430 431gs_main_instance *minst; 432const char start_string[] = "systemdict /start get exec\n"; 433 434int main(int argc, char *argv[]) 435{ 436 int code; 437 int exit_code; 438 439 code = gsapi_new_instance(&minst, NULL); 440 if (code < 0) 441 return 1; 442 gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr); 443 code = gsapi_init_with_args(minst, argc, argv); 444 if (code == 0) 445 code = gsapi_run_string(minst, start_string, 0, &exit_code); 446 gsapi_exit(minst); 447 448 gsapi_delete_instance(minst); 449 450 if ((code == 0) || (code == e_Quit)) 451 return 0; 452 return 1; 453} 454</pre> 455 456 457<p> 458Another partial code example showing how you can feed Ghostscript piecemeal: 459<pre> 460 char *command = "1 2 add == flush\n"; 461 int exit_code; 462 gsapi_run_string_begin(minst, 0, &exit_code); 463 gsapi_run_string_continue(minst, command, strlen(command), 0, &exit_code); 464 gsapi_run_string_continue(minst, "qu", 2, 0, &exit_code); 465 gsapi_run_string_continue(minst, "it", 2, 0, &exit_code); 466 gsapi_run_string_end(minst, 0, &exit_code); 467</pre> 468 469 470<h2><a name="Multiple_threads"></a>Multiple threads</h2> 471The Ghostscript library should have been compiled with a 472thread safe run time library. 473Synchronisation of threads is entirely up to the caller. 474 475<h2><a name="stdio"></a>Standard input and output</h2> 476<p> 477When using the Ghostscript interpreter library interface, you have a 478choice of two standard input/output methods. 479<ul> 480<li>If you do nothing, the "C" stdio will be used. 481<li>If you use <b><tt>gsapi_set_stdio()</tt></b>, all stdio will 482 be redirected to the callback functions you provide. 483 This would be used in a graphical user interface environment 484 where stdio is not available, or where you wish to process 485 Ghostscript input or output. 486</ul> 487<p> 488The callback functions are described in 489<a href="../src/iapi.h"><b><tt>iapi.h</tt></b></a>. 490 491 492<h2><a name="display"></a>Display device</h2> 493<p> 494The <b><tt>display</tt></b> device is available for use with 495the Ghostscript interpreter library. This is described in the file 496<b><tt><a href="../src/gdevdsp.h">gdevdsp.h</a></tt></b>. 497This device provides you with access to the raster output of 498Ghostscript. It is your responsibility to copy this raster 499to a display window or printer. 500<p> 501To use this device, you must provide a callback structure 502with addresses of a number of callback functions. 503The address of the callback structure is provided using 504<b><tt>gsapi_set_display_callback()</tt></b>. 505This must be called after 506<b><tt>gsapi_new_instance()</tt></b> 507and before 508<b><tt>gsapi_init_with_args()</tt></b>. 509<p> 510The callbacks are for device open, close, resize, sync, page, 511memory allocation and updating. 512Each callback function contains a handle can be set using 513<blockquote> 514 -dDisplayHandle=1234 515</blockquote> 516<p> 517The device raster format can be configured using 518<blockquote> 519 -dDisplayFormat=NNNN 520</blockquote> 521Options include 522<ul> 523<li> native, gray, RGB or CMYK color spaces. 524<li> alpha byte (ignored). 525<li> 1 to 16 bits/component. 526<li> bigendian (RGB) or littleendian (BGR) order. 527<li> top first or bottom first raster. 528<li> 16 bits/pixel with 555 or 565 bitfields. 529</ul> 530The format values are described in 531<b><tt><a href="../src/gdevdsp.h">gdevdsp.h</a></tt></b>. 532The format is flexible enough to support common Windows, OS/2, Linux 533and Mac raster formats. To select the display device with a 534Windows 24-bit RGB raster: 535<pre> 536 char **nargv; 537 char arg1[64]; 538 char arg2[64]; 539 char arg3[64]; 540 code = gsapi_new_instance(&minst, NULL); 541 gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr); 542 code = gsapi_set_display_callback(minst, &display_callback); 543 sprintf(arg1, "-sDEVICE=display"); 544 sprintf(arg2, "-dDisplayHandle=%d", 0); 545 sprintf(arg3, "-dDisplayFormat=%d", 546 DISPLAY_COLORS_RGB | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 | 547 DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST); 548 nargv = (char **)malloc((argc + 4) * sizeof(char *)); 549 nargv[0] = argv[0]; 550 nargv[1] = arg1; 551 nargv[2] = arg2; 552 nargv[3] = arg3; 553 memcpy(nargv + 4, argv + 1, argc * sizeof(char *)); 554 argc += 3; 555 code = gsapi_init_with_args(minst, argc, nargv); 556</pre> 557 558<p> 559The display device provides you with the address and size of the 560raster using the <b><tt>display_size()</tt></b> callback. 561You are then responsible for displaying this raster. 562Some examples are in 563<b><tt><a href="../src/dwmain.c">dwmain.c</a></tt></b> (Windows), 564<b><tt><a href="../src/dpmain.c">dpmain.c</a></tt></b> (OS/2) and 565<b><tt><a href="../src/dxmain.c">dxmain.c</a></tt></b> (X11/Linux). 566 567<p> 568 569<!-- [2.0 end contents] ==================================================== --> 570 571<!-- [3.0 begin visible trailer] =========================================== --> 572<hr> 573 574 575<p> 576<small>Copyright © 2001 artofcode LLC. All rights reserved.</small> 577 578<p> 579<small>This file is part of AFPL Ghostscript. See the 580<a href="Public.htm">Aladdin Free Public License</a> (the "License") for 581full details of the terms of using, copying, modifying, and redistributing 582AFPL Ghostscript.</small> 583 584<p> 585<small>Ghostscript version 7.04, 31 January 2002 586 587 588<!-- [3.0 end visible trailer] ============================================= --> 589 590</body> 591</html> 592