|
|
For other information, see the Ghostscript overview.
WARNING: The API described in this document is subject to changes in future releases, possibly ones that are not backward compatible with what is described here.
The Ghostscript interpreter can be built as a dynamic link library (DLL) on the Windows or OS/2 platforms, as a shared object on the GNU/Linux platform and as a framework on MacOS X. With some changes, it could be built as a static library. This document describes the Application Programming Interface (API) for the Ghostscript interpreter library. This should not be confused with the Ghostscript library which provides a graphics library but not the interpreter.
This supercedes the old DLL interface.
To provide the interface described in the usage documentation, a smaller independent executable loads the DLL/shared object. This executable provides all the interaction with the windowing system, including image windows and, if necessary, a text window.
The Ghostscript interpreter library's name and characteristics differ for each platform:
The source for the executable is in dw*.* (Windows), dp*.* (OS/2) and dx*.* (GNU/Linux). See these source files for examples of how to use the DLL.
The source file dxmainc.c can serve as an example of how to use the framework shared component on MacOS X. Just change the header includes to use the Ghostscript namespace:
and link with:#include <Ghostscript/errors.h> #include <Ghostscript/iapi.h>
To get a useable executable. Ghostscript.framework must be properly installed in the search path for this to work.cc -o gsc dxmainc.c -framework Ghostscript
At this stage, Ghostscript does not support multiple instances of the interpreter within a single process.
The functions exported by the DLL/shared object are described in the header file iapi.h and are summarised below. Omitted from the summary are the calling convention (e.g. __stdcall), details of return values and error handling.
This function returns the revision numbers and strings of the Ghostscript interpreter library; you should call it before any other interpreter library functions to make sure that the correct version of the Ghostscript interpreter has been loaded.typedef struct gsapi_revision_s { const char *product; const char *copyright; long revision; long revisiondate; } gsapi_revision_t; gsapi_revision_t r; if (gsapi_revision(&r, sizeof(r)) == 0) { if (r.revision < 650) printf("Need at least Ghostscript 6.50"); } else { printf("revision structure size is incorrect"); }
Create a new instance of Ghostscript. This instance is passed to most other gsapi functions. The caller_handle will be provided to callback functions. At this stage, Ghostscript supports only one instance.
Destroy an instance of Ghostscript. Before you call this, Ghostscript must have finished. If Ghostscript has been initialised, you must call gsapi_exit before gsapi_delete_instance.
Set the callback functions for stdio The stdin callback function should return the number of characters read, 0 for EOF, or -1 for error. The stdout and stderr callback functions should return the number of characters written.
Set the callback function for polling. This is used for handling window events or cooperative multitasking. This function will only be called if the Ghostscript interpreter was compiled with CHECK_INTERRUPTS as described in gpcheck.h.
Set the callback structure for the display device. If the display device is used, this must be called after gsapi_new_instance() and before gsapi_init_with_args(). See gdevdsp.h for more details.
Initialise the interpreter. This calls gs_main_init_with_args() in imainarg.c. See below for return codes. The arguments are the same as the "C" main function: argv[0] is ignored and the user supplied arguments are argv[1] to argv[argc-1].
The gsapi_run_* functions are like gs_main_run_* except that the error_object is omitted. If these functions return <= -100, either quit or a fatal error has occured. You must call gsapi_exit() next. The only exception is gsapi_run_string_continue() which will return e_NeedInput if all is well. See below for return codes.
Exit the interpreter. This must be called on shutdown if gsapi_init_with_args() has been called, and just before gsapi_delete_instance().
The gsapi_init_with_args, gsapi_run_* and gsapi_exit functions return an integer code.
Return codes from gsapi_*() | ||
---|---|---|
Code | Status | |
0 | No errors | |
e_Quit | "quit" has been executed. This is not an error. gsapi_exit() must be called next. | |
e_NeedInput | More input is needed by gsapi_run_string_continue(). This is not an error. | |
e_Info | "gs -h" has been executed. This is not an error. gsapi_exit() must be called next. | |
< 0 | Error | |
<= -100 | Fatal error. gsapi_exit() must be called next. |
The gsapi_run_*() functions do not flush stdio. If you want to see output from Ghostscript you must do this explicitly as shown in the example below.
When executing a string with gsapi_run_string_*(), currentfile is the input from the string. Reading from %stdin uses the stdin callback.
/* Example of using GS DLL as a ps2pdf converter. */ #ifdef _Windows /* Compile with: * cl -D_Windows -Isrc -Febin\ps2pdf.exe ps2pdf.c bin\gsdll32.lib */ #include <windows.h> #define GSDLLEXPORT __declspec(dllimport) #endif #include "errors.h" #include "iapi.h" gs_main_instance *minst; int main(int argc, char *argv[]) { int code; int exit_code; const char * gsargv[10]; int gsargc; gsargv[0] = "ps2pdf"; /* actual value doesn't matter */ gsargv[1] = "-dNOPAUSE"; gsargv[2] = "-dBATCH"; gsargv[3] = "-dSAFER"; gsargv[4] = "-sDEVICE=pdfwrite"; gsargv[5] = "-sOutputFile=out.pdf"; gsargv[6] = "-c"; gsargv[7] = ".setpdfwrite"; gsargv[8] = "-f"; gsargv[9] = "input.ps"; gsargc=10; code = gsapi_new_instance(&minst, NULL); if (code < 0) return 1; code = gsapi_init_with_args(minst, gsargc, gsargv); gsapi_exit(minst); gsapi_delete_instance(minst); if ((code == 0) || (code == e_Quit)) return 0; return 1; }
/* Similar to command line gs */ #include <stdio.h> #include "errors.h" #include "iapi.h" /* stdio functions */ static int GSDLLCALL gsdll_stdin(void *instance, char *buf, int len) { int ch; int count = 0; while (count < len) { ch = fgetc(stdin); if (ch == EOF) return 0; *buf++ = ch; count++; if (ch == '\n') break; } return count; } static int GSDLLCALL gsdll_stdout(void *instance, const char *str, int len) { fwrite(str, 1, len, stdout); fflush(stdout); return len; } static int GSDLLCALL gsdll_stderr(void *instance, const char *str, int len) { fwrite(str, 1, len, stderr); fflush(stderr); return len; } gs_main_instance *minst; const char start_string[] = "systemdict /start get exec\n"; int main(int argc, char *argv[]) { int code; int exit_code; code = gsapi_new_instance(&minst, NULL); if (code < 0) return 1; gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr); code = gsapi_init_with_args(minst, argc, argv); if (code == 0) code = gsapi_run_string(minst, start_string, 0, &exit_code); gsapi_exit(minst); gsapi_delete_instance(minst); if ((code == 0) || (code == e_Quit)) return 0; return 1; }
const char *command = "1 2 add == flush\n"; int main(int argc, char *argv[]) { int code; int exit_code; code = gsapi_new_instance(&minst, NULL); if (code < 0) return 1; code = gsapi_init_with_args(minst, argc, argv); if (code == 0) { gsapi_run_string_begin(minst, 0, &exit_code); gsapi_run_string_continue(minst, command, strlen(command), 0, &exit_code); gsapi_run_string_continue(minst, "qu", 2, 0, &exit_code); gsapi_run_string_continue(minst, "it", 2, 0, &exit_code); gsapi_run_string_end(minst, 0, &exit_code); } gsapi_exit(minst); gsapi_delete_instance(minst); if ((code == 0) || (code == e_Quit)) return 0; return 1; }
When using the Ghostscript interpreter library interface, you have a choice of two standard input/output methods.
The callback functions are described in iapi.h.
The display device is available for use with the Ghostscript interpreter library. This is described in the file gdevdsp.h. This device provides you with access to the raster output of Ghostscript. It is your responsibility to copy this raster to a display window or printer.
To use this device, you must provide a callback structure with addresses of a number of callback functions. The address of the callback structure is provided using gsapi_set_display_callback(). This must be called after gsapi_new_instance() and before gsapi_init_with_args().
The callbacks are for device open, close, resize, sync, page, memory allocation and updating. Each callback function contains a handle can be set using
-dDisplayHandle=1234
The device raster format can be configured using
-dDisplayFormat=NNNNOptions include
char **nargv; char arg1[64]; char arg2[64]; char arg3[64]; code = gsapi_new_instance(&minst, NULL); gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr); code = gsapi_set_display_callback(minst, &display_callback); sprintf(arg1, "-sDEVICE=display"); sprintf(arg2, "-dDisplayHandle=%d", 0); sprintf(arg3, "-dDisplayFormat=%d", DISPLAY_COLORS_RGB | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 | DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST); nargv = (char **)malloc((argc + 4) * sizeof(char *)); nargv[0] = argv[0]; nargv[1] = arg1; nargv[2] = arg2; nargv[3] = arg3; memcpy(nargv + 4, argv + 1, argc * sizeof(char *)); argc += 3; code = gsapi_init_with_args(minst, argc, nargv);
The display device provides you with the address and size of the raster using the display_size() callback. You are then responsible for displaying this raster. Some examples are in dwmain.c (Windows), dpmain.c (OS/2) and dxmain.c (X11/GNU/Linux).
Copyright © 2001 artofcode LLC. All rights reserved.
This software is provided AS-IS with no warranty, either express or implied. This software is distributed under license and may not be copied, modified or distributed except as expressly authorized under the terms of the license contained in the file LICENSE in this distribution.
Ghostscript version 7.07, 17 May 2003