API

What is the Ghostscript Interpreter API?

The Ghostscript interpreter can be built as a dynamic link library (DLL) on Microsoft Windows, as a shared object on the Linux, Unix and MacOS X platforms. 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 must provide 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 Win32 DLL gsdll32.dll can be used by multiple programs simultaneously, but only once within each process

  • The OS/2 DLL gsdll2.dll has MULTIPLE NONSHARED data segments and can be called by multiple programs simultaneously

  • The Linux shared object libgs.so can be used by multiple programs simultaneously

The source for the executable is in dw*.* (Windows), dp*.* (OS/2) and dx*.* (Linux/Unix). See these source files for examples of how to use the DLL.

The source file dxmainc.c can also serve as an example of how to use the shared library component on MacOS X, providing the same command-line tool it does on any linux, bsd or similar operating system.

At this stage, Ghostscript does not support multiple instances of the interpreter within a single process.

Exported functions

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.

  • int gsapi_revision (gsapi_revision_t *pr, int len);

  • int gsapi_new_instance (void **pinstance, void *caller_handle);

  • void gsapi_delete_instance (void *instance);

  • int gsapi_set_stdio_with_handle (void *instance, int(*stdin_fn)(void *caller_handle, char *buf, int len), int(*stdout_fn)(void *caller_handle, const char *str, int len), int(*stderr_fn)(void *caller_handle, const char *str, int len), void *caller_handle);

  • int gsapi_set_stdio (void *instance, int(*stdin_fn)(void *caller_handle, char *buf, int len), int(*stdout_fn)(void *caller_handle, const char *str, int len), int(*stderr_fn)(void *caller_handle, const char *str, int len));

  • int gsapi_set_poll_with_handle (void *instance, int(*poll_fn)(void *caller_handle), void *caller_handle);

  • int gsapi_set_poll (void *instance, int(*poll_fn)(void *caller_handle));

  • int gsapi_set_display_callback (void *instance, display_callback *callback);

  • int gsapi_register_callout (void *instance, gs_callout callout, void *callout_handle);

  • void gsapi_deregister_callout (void *instance, gs_callout callout, void *callout_handle);

  • int gsapi_set_arg_encoding (void *instance, int encoding);

  • int gsapi_get_default_device_list(void *instance, char **list, int *listlen);

  • int gsapi_set_default_device_list(void *instance, const char *list, int listlen);

  • int gsapi_run_string_begin (void *instance, int user_errors, int *pexit_code);

  • int gsapi_run_string_continue (void *instance, const char *str, unsigned int length, int user_errors, int *pexit_code);

  • int gsapi_run_string_end (void *instance, int user_errors, int *pexit_code);

  • int gsapi_run_string_with_length (void *instance, const char *str, unsigned int length, int user_errors, int *pexit_code);

  • int gsapi_run_string (void *instance, const char *str, int user_errors, int *pexit_code);

  • int gsapi_run_file (void *instance, const char *file_name, int user_errors, int *pexit_code);

  • int gsapi_init_with_args (void *instance, int argc, char **argv);

  • int gsapi_exit (void *instance);

  • int gsapi_set_param(void *instance, const char *param, const void *value, gs_set_param_type type);

  • int gsapi_get_param(void *instance, const char *param, void *value, gs_set_param_type type);

  • int gsapi_enumerate_params(void *instance, void **iter, const char **key, gs_set_param_type *type);

  • int gsapi_add_control_path(void *instance, int type, const char *path);

  • int gsapi_remove_control_path(void *instance, int type, const char *path);

  • void gsapi_purge_control_paths(void *instance, int type);

  • void gsapi_activate_path_control(void *instance, int enable);

  • int gsapi_is_path_control_active(void *instance);

  • int gsapi_add_fs (void *instance, gsapi_fs_t *fs, void *secret);

  • void gsapi_remove_fs (void *instance, gsapi_fs_t *fs, void *secret);

gsapi_revision()

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");
}

gsapi_new_instance()