summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'doc/API.htm')
-rw-r--r--doc/API.htm733
1 files changed, 680 insertions, 53 deletions
diff --git a/doc/API.htm b/doc/API.htm
index faee4d58..feffd7e7 100644
--- a/doc/API.htm
+++ b/doc/API.htm
@@ -1,15 +1,6 @@
<!doctype html>
<html>
<head>
-<!-- Global site tag (gtag.js) - Google Analytics -->
-<script async src="https://www.googletagmanager.com/gtag/js?id=UA-54391264-2"></script>
-<script>
- window.dataLayer = window.dataLayer || [];
- function gtag(){dataLayer.push(arguments);}
- gtag('js', new Date());
-
- gtag('config', 'UA-54391264-2');
-</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet">
@@ -72,21 +63,37 @@
<li><a href="#revision"><code>gsapi_revision</code></a></li>
<li><a href="#new_instance"><code>gsapi_new_instance</code></a></li>
<li><a href="#delete_instance"><code>gsapi_delete_instance</code></a></li>
+<li><a href="#set_stdio_with_handle"><code>gsapi_set_stdio_with_handle</code></a></li>
<li><a href="#set_stdio"><code>gsapi_set_stdio</code></a></li>
+<li><a href="#set_poll_with_handle"><code>gsapi_set_poll_with_handle</code></a></li>
<li><a href="#set_poll"><code>gsapi_set_poll</code></a></li>
<li><a href="#set_display_callback"><code>gsapi_set_display_callback</code></a></li>
+<li><a href="#register_callout"><code>gsapi_register_callout</code></a></li>
+<li><a href="#deregister_callout"><code>gsapi_deregister_callout</code></a></li>
<li><a href="#set_arg_encoding"><code>gsapi_set_arg_encoding</code></a></li>
+<li><a href="#get_default_device_list"><code>gsapi_get_default_device_list</code></a></li>
+<li><a href="#set_default_device_list"><code>gsapi_set_default_device_list</code></a></li>
<li><a href="#run"><code>gsapi_run_string_begin</code></a></li>
<li><a href="#run"><code>gsapi_run_string_continue</code></a></li>
<li><a href="#run"><code>gsapi_run_string_end</code></a></li>
<li><a href="#run"><code>gsapi_run_string_with_length</code></a></li>
<li><a href="#run"><code>gsapi_run_string</code></a></li>
+<li><a href="#run"><code>gsapi_run_file</code></a></li>
<li><a href="#init"><code>gsapi_init_with_args</code></a></li>
-<li><a href="#run"><code>gsapi_run_*</code></a></li>
<li><a href="#exit"><code>gsapi_exit</code></a></li>
+<li><a href="#set_param"><code>gsapi_set_param</code></a></li>
+<li><a href="#get_param"><code>gsapi_get_param</code></a></li>
+<li><a href="#enumerate_params"><code>gsapi_enumerate_params</code></a></li>
+<li><a href="#add_control_path"><code>gsapi_add_control_path</code></a></li>
+<li><a href="#remove_control_path"><code>gsapi_remove_control_path</code></a></li>
+<li><a href="#purge_control_paths"><code>gsapi_purge_control_paths</code></a></li>
+<li><a href="#activate_path_control"><code>gsapi_activate_path_control</code></a></li>
+<li><a href="#is_path_control_active"><code>gsapi_is_path_control_active</code></a></li>
<li><a href="#add_fs"><code>gsapi_add_fs</code></a></li>
<li><a href="#remove_fs"><code>gsapi_remove_fs</code></a></li>
<li><a href="#return_codes">Return codes</a></li>
+<li><a href="#gsapi_fs_t">gsapi_fs_t</a></li>
+<li><a href="#callout">Callouts</a></li>
</ul>
<li><a href="#Example_usage">Example usage</a></li>
<li><a href="#stdio">Standard input and output</a></li>
@@ -195,6 +202,16 @@ void
<li><code>
int
+<a href="#set_stdio_with_handle">gsapi_set_stdio_with_handle</a>
+(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);
+</code></li>
+
+<li><code>
+int
<a href="#set_stdio">gsapi_set_stdio</a>
(void *instance,
int(*stdin_fn)(void *caller_handle, char *buf, int len),
@@ -204,6 +221,12 @@ int
<li><code>
int
+<a href="#set_poll_with_handle">gsapi_set_poll_with_handle</a>
+(void *instance, int(*poll_fn)(void *caller_handle), void *caller_handle);
+</code></li>
+
+<li><code>
+int
<a href="#set_poll">gsapi_set_poll</a>
(void *instance, int(*poll_fn)(void *caller_handle));
</code></li>
@@ -216,14 +239,30 @@ int
<li><code>
int
+<a href="#register_callout">gsapi_register_callout</a>
+(void *instance, gs_callout callout, void *callout_handle);
+</code></li>
+
+<li><code>
+void
+<a href="#deregister_callout">gsapi_deregister_callout</a>
+(void *instance, gs_callout callout, void *callout_handle);
+</code></li>
+
+<li><code>
+int
<a href="#set_arg_encoding">gsapi_set_arg_encoding</a>
(void *instance, int encoding);
</code></li>
<li><code>
int
-<a href="#init">gsapi_init_with_args</a>
-(void *instance, int argc, char **argv);
+<a href="#get_default_device_list">gsapi_get_default_device_list</a>(void *instance, char **list, int *listlen);
+</code></li>
+
+<li><code>
+int
+<a href="#set_default_device_list">gsapi_set_default_device_list</a>(void *instance, const char *list, int listlen);
</code></li>
<li><code>
@@ -268,12 +307,58 @@ int
<li><code>
int
+<a href="#init">gsapi_init_with_args</a>
+(void *instance, int argc, char **argv);
+</code></li>
+
+<li><code>
+int
<a href="#exit">gsapi_exit</a>
(void *instance);
</code></li>
<li><code>
int
+<a href="#set_param">gsapi_set_param</a>(void *instance, const char *param, const void *value, gs_set_param_type type);
+</code></li>
+
+<li><code>
+ int
+ <a href="#get_param">gsapi_get_param</a>(void *instance, const char *param, void *value, gs_set_param_type type);
+</code></li>
+
+<li><code>
+int
+<a href="#enumerate_params">gsapi_enumerate_params</a>(void *instance, void **iter, const char **key, gs_set_param_type *type);
+</code></li>
+
+<li><code>
+int
+<a href="#add_control_path">gsapi_add_control_path</a>(void *instance, int type, const char *path);
+</code></li>
+
+<li><code>
+int
+<a href="#remove_control_path">gsapi_remove_control_path</a>(void *instance, int type, const char *path);
+</code></li>
+
+<li><code>
+void
+<a href="#purge_control_paths">gsapi_purge_control_paths</a>(void *instance, int type);
+</code></li>
+
+<li><code>
+void
+<a href="#activate_path_control">gsapi_activate_path_control</a>(void *instance, int enable);
+</code></li>
+
+<li><code>
+int
+<a href="#is_path_control_active">gsapi_is_path_control_active</a>(void *instance);
+</code></li>
+
+<li><code>
+int
<a href="#add_fs">gsapi_add_fs</a>
(void *instance,
gsapi_fs_t *fs, void *secret);
@@ -321,7 +406,7 @@ else {
<blockquote>
Create a new instance of Ghostscript.
This instance is passed to most other gsapi functions.
-The caller_handle will be provided to callback functions.
+The caller_handle is the default value that will be provided to callback functions.
<b>Unless Ghostscript has been compiled with the </code>GS_THREADSAFE</code>
define, only one instance at a time is supported.</b>
@@ -353,19 +438,33 @@ If Ghostscript has been initialised, you must call
</blockquote>
-<h3><a name="set_stdio"></a><code>gsapi_set_stdio()</code></h3>
+<h3><a name="set_stdio_with_handle"></a><code>gsapi_set_stdio_with_handle()</code></h3>
<blockquote>
-Set the callback functions for stdio
+<p>Set the callback functions for stdio, together with the
+handle to use in the callback functions.
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.
+<p><b>NOTE:</b> These callbacks do not affect output device I/O when
+using &quot;<code>%stdout</code>&quot; as the output file. In that
+case, device output will still be directed to the process &quot;stdout&quot;
+file descriptor, not to the stdio callback.
</blockquote>
+<h3><a name="set_stdio"></a><code>gsapi_set_stdio()</code></h3>
+<blockquote>
+<p>Set the callback functions for stdio. The handle used
+in the callbacks will be taken from the value passed
+to <code><a href="#new_instance">gsapi_new_instance</a></code>. Otherwise the behaviour of this
+function matches <code><a href="#set_stdio_with_handle">gsapi_set_stdio_with_handle</a></code>.
+</blockquote>
-<h3><a name="set_poll"></a><code>gsapi_set_poll()</code></h3>
+
+<h3><a name="set_poll_with_handle"></a><code>gsapi_set_poll_with_handle()</code></h3>
<blockquote>
-Set the callback function for polling. This function will only be called if
+Set the callback function for polling, together with the handle to pass
+to the callback function. This function will only be called if
the Ghostscript interpreter was compiled with <code>CHECK_INTERRUPTS</code>
as described in <code><a href="../base/gpcheck.h">gpcheck.h</a></code>.
<p>
@@ -380,10 +479,25 @@ to return 0 immediately some number of times can be used to reduce the
performance impact.</p>
</blockquote>
+<h3><a name="set_poll"></a><code>gsapi_set_poll()</code></h3>
+<blockquote>
+Set the callback function for polling. The handle passed to the
+callback function will be taken from the handle passed to
+<code><a href="#new_instance">gsapi_new_instance</a></code>.
+Otherwise the behaviour of this function matches
+<code><a href="#set_poll_with_handle">gsapi_set_poll_with_handle</a></code>.
+</blockquote>
+
<h3><a name="set_display_callback"></a><code>gsapi_set_display_callback()</code></h3>
<blockquote>
+<b>This call is deprecated; please use
+<code><a href="#register_callout">gsapi_register_callout</a></code>
+to register a <a href="#callout">callout</a> handler for the
+<a href="#display">display</a> device in preference.</b>
Set the callback structure for the <a href="#display">display</a>
-device. If the <a href="#display">display</a> device is used,
+device. The handle passed in the callback functions is taken from
+the <code>DisplayHandle</code> parameter (or NULL if there is no
+such parameter). If the <a href="#display">display</a> device is used,
this must be called after
<code>gsapi_new_instance()</code>
and before <code>gsapi_init_with_args()</code>.
@@ -391,6 +505,20 @@ See <code><a href="../base/gdevdsp.h">gdevdsp.h</a></code>
for more details.
</blockquote>
+<h3><a name="register_callout"></a><code>gsapi_register_callout()</code></h3>
+<blockquote>
+<p>This call registers a <code><a href="#callout">callout</a></code>
+handler.</p>
+</blockquote>
+
+<h3><a name="deregister_callout"></a><code>gsapi_deregister_callout()</code></h3>
+<blockquote>
+<p>This call deregisters a <code><a href="#callout">callout</a></code> handler
+previously registered with <code><a href="#register_callout">gsapi_register_callout</a></code>.
+All three arguments must match exactly for the callout handler to
+be deregistered.</p>
+</blockquote>
+
<h3><a name="set_arg_encoding"></a><code>gsapi_set_arg_encoding()</code></h3>
<blockquote>
Set the encoding used for the interpretation of all subsequent args
@@ -405,6 +533,23 @@ This must be called after <code>gsapi_new_instance()</code>
and before <code>gsapi_init_with_args()</code>.
</blockquote>
+<h3><a name="set_default_device_list"></a><code>set_default_device_list()</code></h3>
+<blockquote>
+Set the string containing the list of default device names,
+for example "display x11alpha x11 bbox". Allows the calling
+application to influence which device(s) gs will try, in order,
+in it's selection of the default device.
+This must be called after <code>gsapi_new_instance()</code>
+and before <code>gsapi_init_with_args()</code>.
+</blockquote>
+
+<h3><a name="get_default_device_list"></a><code>get_default_device_list()</code></h3>
+<blockquote>
+Returns a pointer to the current default device string.
+This must be called after <code>gsapi_new_instance()</code>
+and before <code>gsapi_init_with_args()</code>.
+</blockquote>
+
<h3><a name="init"></a><code>gsapi_init_with_args()</code></h3>
<blockquote>
Initialise the interpreter.
@@ -451,6 +596,119 @@ This must be called on shutdown if <code>gsapi_init_with_args()</code>
has been called, and just before <code>gsapi_delete_instance()</code>.
</blockquote>
+<h3><a name="set_param"></a><code>gsapi_set_param()</code></h3>
+<blockquote>
+Set a parameter.
+Broadly, this is equivalent to setting a parameter using <code>-d</code>, <code>-s</code> or <code>-p</code> on the command line. This call cannot be made during a <code>run_string</code> operation.
+<p>
+Parameters in this context are not the same as 'arguments' as processed by <code>gsapi_init_with_args</code>, but often the same thing can be achieved. For example, with <code>gsapi_init_with_args</code>, we can pass &quot;<code>-r200</code>&quot; to change the resolution. Broadly the same thing can be achieved by using <code>gsapi_set_param</code> to set a parsed value of &quot;<code>&lt;&lt;/HWResolution [ 200.0 200.0 ]&gt;&gt;</code>&quot;.
+<p>
+Note, that internally, when we set a parameter, we perform an <code>initgraphics</code> operation. This means that using <code>set_param</code> other than at the start of a page is likely to give unexpected results.
+<p>Further, note that attempting to set a parameter that the device does not recognise will be silently ignored, and that parameter will not be found in subsequent <code>gsapi_get_param</code> calls.
+<p>The <code>type</code> argument dictates the kind of object that <code>value</code> points to:
+<pre><code>typedef enum {
+ gs_spt_invalid = -1,
+ gs_spt_null = 0, /* void * is NULL */
+ gs_spt_bool = 1, /* void * is a pointer to an int (0 false,
+ * non-zero true). */
+ gs_spt_int = 2, /* void * is a pointer to an int */
+ gs_spt_float = 3, /* void * is a float * */
+ gs_spt_name = 4, /* void * is a char * */
+ gs_spt_string = 5, /* void * is a char * */
+ gs_spt_long = 6, /* void * is a long * */
+ gs_spt_i64 = 7, /* void * is an int64_t * */
+ gs_spt_size_t = 8, /* void * is a size_t * */
+ gs_spt_parsed = 9, /* void * is a pointer to a char * to be parsed */
+
+ /* Setting a typed param causes it to be instantly fed to to the
+ * device. This can cause the device to reinitialise itself. Hence,
+ * setting a sequence of typed params can cause the device to reset
+ * itself several times. Accordingly, if you OR the type with
+ * gs_spt_more_to_come, the param will held ready to be passed into
+ * the device, and will only actually be sent when the next typed
+ * param is set without this flag (or on device init). Not valid
+ * for get_typed_param. */
+ gs_spt_more_to_come = 1<<31
+} gs_set_param_type;
+</code></pre>
+<p>Combining a type value by ORRing it with the <code>gs_spt_more_to_come</code> flag will cause the <code>set_param</code> operation to be queued internally, but not actually be sent to the device. Thus a series of <code>set_param</code> operations can be queued, for example as below:
+<pre>
+ int code = gsapi_set_param(instance,
+ "HWResolution",
+ "[300 300]",
+ gs_spt_parsed | gs_spt_more_to_come);
+ if (code >= 0) {
+ int i = 1;
+ code = gsapi_set_param(instance,
+ "FirstPage",
+ &i,
+ gs_spt_int | gs_spt_more_to_come);
+ }
+ if (code >= 0) {
+ int i = 3;
+ code = gsapi_set_param(instance,
+ "DownScaleFactor",
+ &i,
+ gs_spt_int);
+ }
+</pre>
+<p>This enables a series of set operations to be performed 'atomically'. This can be useful for performance, in that any reconfigurations to the device (such as page size changes or memory reallocations) will only happen when all the parameters are sent, rather than potentially each time each one is sent.
+</blockquote>
+
+<h3><a name="get_param"></a><code>gsapi_get_param()</code></h3>
+<blockquote>
+Get a parameter.
+Retrieve the current value of a parameter.
+<p>If an error occurs, the return value is negative. Otherwise the return value is the number of bytes required for storage of the value. Call once with <code>value = NULL</code> to get the number of bytes required, then call again with <code>value</code> pointing to at least the required number of bytes where the value will be copied out. Note that the caller is required to know the type of value in order to get it. For all types other than <code>string</code>, <code>name</code>, and <code>parsed</code> knowing the type means you already know the size required.
+<p>
+This call retrieves parameters/values that have made it to the device. Thus, any values set using the <code>gs_spt_more_to_come</code> without a following call without that flag will not be retrieved. Similarly, attempting to get a parameter before <code>gsapi_init_with_args</code> has been called will not list any, even if <code>gsapi_set_param</code> has been used.
+<p>
+Attempting to read a parameter that is not set will return <code>gs_error_undefined (-21)</code>. Note that calling <code>gsapi_set_param</code> followed by <code>gsapi_get_param</code> may not find the value, if the device did not recognise the key as being one of its configuration keys.
+</blockquote>
+
+<h3><a name="enumerate_params"></a><code>gsapi_enumerate_params()</code></h3>
+<blockquote>
+Enumerate the current parameters.
+Call repeatedly to list out the current parameters.
+<p>
+ The first call should have <code>*iter = NULL</code>. Subsequent calls should pass the same pointer in so the iterator can be updated. Negative return codes indicate error, 0 success, and 1 indicates that there are no more keys to read. On success, <code>key</code> will be updated to point to a null terminated string with the key name that is guaranteed to be valid until the next call to <code>gsapi_enumerate_params</code>. If <code>type</code> is non <code>NULL</code> then <code>*type</code> will be updated to have the type of the parameter.
+<p>
+Note that only one enumeration can happen at a time. Starting a second enumeration will reset the first.
+<p>
+The enumeration only returns parameters/values that have made it to the device. Thus, any values set using the <code>gs_spt_more_to_come</code> without a following call without that flag will not be retrieved. Similarly, attempting to enumerate parameters before <code>gsapi_init_with_args</code> has been called will not list any, even if <code>gsapi_set_param</code> has been used.
+</blockquote>
+
+<h3><a name="add_control_path"></a><code>gsapi_add_control_path()</code></h3>
+<blockquote>
+Add a (case sensitive) path to one of the lists of permitted paths for file access.
+See <a href="Use.htm#Safer">here</a> for more information about permitted paths.
+</blockquote>
+
+<h3><a name="remove_control_path"></a><code>gsapi_remove_control_path()</code></h3>
+<blockquote>
+Remove a (case sensitive) path from one of the lists of permitted paths for file access.
+See <a href="Use.htm#Safer">here</a> for more information about permitted paths.
+</blockquote>
+
+<h3><a name="purge_control_paths"></a><code>gsapi_purge_control_paths()</code></h3>
+<blockquote>
+Clear all the paths from one of the lists of permitted paths for file access.
+See <a href="Use.htm#Safer">here</a> for more information about permitted paths.
+</blockquote>
+
+<h3><a name="activate_path_control"></a><code>gsapi_activate_path_control()</code></h3>
+<blockquote>
+Enable/Disable path control (i.e. whether paths are checked against permitted paths
+before access is granted).
+See <a href="Use.htm#Safer">here</a> for more information about permitted paths.
+</blockquote>
+
+<h3><a name="is_path_control_active"></a><code>gsapi_is_path_control_active()</code></h3>
+<blockquote>
+Query whether path control is activated or not.
+See <a href="Use.htm#Safer">here</a> for more information about permitted paths.
+</blockquote>
+
<h3><a name="add_fs"></a><code>gsapi_add_fs()</code></h3>
<blockquote>
Adds a new 'Filing System' to the interpreter.
@@ -793,6 +1051,47 @@ Reopen a stream with a different mode. Behaves like
<code>freopen(fname, mode, FILE *)</code>.
</dl>
+<h3><a name="callout"></a>Callouts</h3>
+<blockquote>
+<p>Callouts are a mechanism
+for the core code (specifically devices) to communicate with the
+user of gsapi. This communication can take the form of passing
+information out vis-a-vis what devices are doing, or requesting
+configuration from the caller to affect exactly how the device
+itself works.</p>
+<p>This is deliberately an extensible system, so exact details of
+callouts should be documented with the device in question. In general
+however a callout handler will be of the form:</p>
+<pre>
+typedef int (*gs_callout)(void *callout_handle,
+ const char *device_name,
+ int id,
+ int size,
+ void *data);
+</pre>
+<p>The <code>callout_handle</code> value passed to the callout will
+be the value passed in at registration. The <code>device_name</code>
+should be a null-terminated string giving the name of the device
+(though care should be taken to cope with the case where
+<code>device_name</code> is NULL for potential future uses).
+The <code>id</code> value will have a (device-specific) meaning; see
+the documentation for the device in question for more details. The
+same <code>id</code> value may be used to mean different things in
+different devices.
+Finally, <code>size</code> and <code>data</code> have callout
+specific meanings, but typically, <code>data</code> will be a pointer
+to data block (which may either be uninitialised or wholly/partially
+initialised on entry, and may be updated on exit), and <code>size</code>
+will be the size (in bytes) of the block pointed to by <code>data</code>.
+</p>
+<p>A return value of -1 (<code>gs_error_unknownerror</code>) means
+the callout was not recognised by the handler, and should be passed
+to more handlers. Other negative values are interpreted as standard
+Ghostscript error values, and stop the propagation of the callout.
+Non-negative return codes mean the callout was handled and should
+not be passed to any more registered callout handlers.</p>
+</blockquote>
+
<hr>
<h2><a name="Example_usage"></a>Example Usage</h2>
<p>To try out the following examples in a development environment like Microsoft's
@@ -1016,24 +1315,38 @@ The callback functions are described in
<h2><a name="display"></a>Display device</h2>
<p>
The <code>display</code> device is available for use with
-the Ghostscript interpreter library. This is described in the file
+the Ghostscript interpreter library. While originally designed
+for allowing screen display of rendered output from Ghostscript,
+this is now powerful enough to provide a simple mechanism for
+getting rendered output suitable for use in all manner of
+output scenarios, including printing.</p>
+<p>Details of the API and options are given in the file
<code><a href="../base/gdevdsp.h">gdevdsp.h</a></code>.
This device provides you with access to the raster output of
-Ghostscript. It is your responsibility to copy this raster
+Ghostscript. It is the callers responsibility to copy this raster
to a display window or printer.</p>
<p>
-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
-<code>gsapi_set_display_callback()</code>.
+In order for this device to operate, it needs access to a structure
+containing a set of callback functions, and a callback handle (an
+opaque <code>void *</code> that can be used by caller to locate its
+own state). There are 2 ways that the device can get this
+information, a legacy method, and a modern method.
+</p>
+<dl>
+<dt>Legacy method</dt>
+<dd>
+<p>The address of the callback structure, is provided
+using <code>gsapi_set_display_callback()</code>.
This must be called after
<code>gsapi_new_instance()</code>
and before
<code>gsapi_init_with_args()</code>.</p>
<p>
-The callbacks are for device open, close, resize, sync, page,
-memory allocation and updating.
-Each callback function contains a handle can be set using</p>
+With this call, the callback handle is passed as NULL by default, but can
+be overridden by using a parameter. We actively dislike
+this way of working, as we consider passing addresses
+via the command line distasteful. The handle can be
+set using</p>
<blockquote>
-sDisplayHandle=1234
</blockquote>
@@ -1071,6 +1384,31 @@ The previous API, using a number value:</p>
is still supported on 32 bit systems, but will cause a "typecheck"
error on 64 bit systems, and is considered deprecated. It should
not be used in new code.</p>
+</dd>
+<dt>Modern method
+<dd>
+<p>The preferred method is to register a callout handler using
+<code><a href="#register_callout">gsapi_register_callout</a></code>.
+When this handler is called for the <code>&quot;display&quot;</code>
+device, with <code>id = 0</code> (= <code>DISPLAY_CALLOUT_GET_CALLBACK</code>),
+then <code>data</code> should point to an empty <code>gs_display_get_callback_t</code>
+block, with <code>size = sizeof(gs_display_get_callback_t)</code>.
+</p>
+<pre>
+typedef struct {
+ display_callback *callback;
+ void *caller_handle;
+} gs_display_get_callback_t;
+</pre>
+<p>The handler should fill in the structure before returning,
+with a return code of 0.</p>
+</dd>
+</dl>
+<p>
+Note, that the <code>DisplayHandle</code> value is only consulted for
+display device callbacks registered using the (legacy, now deprecated)
+<code>gsapi_set_display_callback</code> API, not the preferred
+<code>gsapi_register_callout</code> based mechanism.
<p>
The device raster format can be configured using</p>
@@ -1085,44 +1423,333 @@ The device raster format can be configured using</p>
<li> bigendian (RGB) or littleendian (BGR) order.</li>
<li> top first or bottom first raster.</li>
<li> 16 bits/pixel with 555 or 565 bitfields.</li>
+<li> Chunky, Planar and Planar interleaved formats.</li>
+<li> "Full screen" or "Rectangle Request" modes of operation.</li>
</ul>
-<p>The format values are described in
-<code><a href="../base/gdevdsp.h">gdevdsp.h</a></code>.
-The format is flexible enough to support common Windows, OS/2, Linux
-and Mac raster formats. To select the display device with a
-Windows 24-bit RGB raster:</p>
+<p>The operation of the device is best described with a walkthrough
+of some example code that uses it. For simplicity and clarity, we
+have omitted the error handling code in this example; in production
+code, every place where we get a <code>code</code> value returned
+we should check it for failure (a negative value) and clean up
+accordingly. First, we create an instance of Ghostscript:</p>
<pre>
- char **nargv;
- char arg1[64];
- char arg2[64];
- char arg3[64];
+ void *minst = NULL;
code = gsapi_new_instance(&amp;minst, NULL);
- gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr);
+ code = gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr);
+</pre>
+<p>Next, we have to give the display device the address of our
+callback structure. In old code, we would do so using something
+like this:</p>
+<pre>
code = gsapi_set_display_callback(minst, &amp;display_callback);
- sprintf(arg1, "-sDEVICE=display");
- sprintf(arg2, "-dDisplayHandle=%d", 0);
- sprintf(arg3, "-dDisplayFormat=%d",
+</pre>
+<p>We strongly recommend that you don't do that, but instead use the
+more modern <a href="callout">callout</a> mechanism:</p>
+<pre>
+ code = gsapi_register_callout(minst, my_callout_handler, state);
+</pre>
+<p>where <code>state</code> is any <code>void *</code> value you like,
+usually a pointer to help you reach any internal state you may need.
+Earlier in your code you would have the definition of
+<code>my_callout_handler</code> that might look like this:</p>
+<pre>
+ static int
+ my_callout_handler(void *instance,
+ void *callout_handle,
+ const char *device_name,
+ int id,
+ int size,
+ void *data)
+{
+ /* On entry, callout_handle == the value of state passed in
+ * to gsapi_register_callout. */
+ /* We are only interested in callouts from the display device. */
+ if (device_name == NULL || strcmp(device_name, "display"))
+ return -1;
+
+ if (id == DISPLAY_CALLOUT_GET_CALLBACK)
+ {
+ /* Fill in the supplied block with the details of our callback
+ * handler, and the handle to use. In this instance, the handle
+ * is the pointer to our test structure. */
+ gs_display_get_callback_t *cb = (gs_display_get_callback_t *)data;
+ cb->callback = &display_callback;
+ cb->caller_handle = callout_handle;
+ return 0;
+ }
+ return -1;
+}
+</pre>
+<p>As you can see, this callout handler only responds to callouts
+for the display device, and then only for one particular function
+(<code>id</code>). It returns the same <code>display_callback</code>
+structure as the deprecated, legacy mechanism passed in using
+<code>gsapi_set_display_callback</code>, with the added benefit that
+the <code>caller_handle</code> value can be passed in too. In this
+example we pass in the same value as was used for <code>callout_handle</code>,
+but implementations are free to use any value they want.</p>
+<p>Returning to our example, we now set up a set of arguments to
+setup Ghostscript:</p>
+<pre>
+ int argc = 0;
+ /* Allow for up to 32 args of up to 64 chars each. */
+ char argv[32][64];
+ sprintf(argc[argc++], "gs");
+ sprintf(argv[argc++], "-sDEVICE=display");
+</pre>
+<p>The zeroth arg is a dummy argument to match the standard C mechanism
+for passing arguments to a program. Traditionally this is the name of
+the program being invoked. Next, we tell Ghostscript to use the display
+device.</p>
+<pre>
+ sprintf(argv[argc++], "-sDEVICE=display");
+</pre>
+Next we tell the display device what output format to use. The
+format is flexible enough to support common Windows, OS/2, Linux
+and Mac raster formats. <p>The format values are described in
+<code><a href="../base/gdevdsp.h">gdevdsp.h</a></code>.
+To select the display device with a Windows 24-bit RGB raster:</p>
+<pre>
+ sprintf(argv[argc++], "-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);
+</pre>
+<p>If (and only if) you used the legacy mechanism described above, you
+will need another argument to pass in the <code>caller_handle</code>
+value to be parroted back to the functions listed within
+<code>display_callback</code>:</p>
+<pre>
+ sprintf(arg2, "-dDisplayHandle=%d", callout_handle);
+</pre>
+<p>Any other arguments that you want can be added to the end of
+the command line, typically including a file to run. Then we pass
+that all to Ghostscript:
+<pre>
+ code = gsapi_init_with_args(minst, argc, argv);
</pre>
+<p><a name="display_callback"></a>At this point you should start to
+see your display callback functions being called. Exactly which callback
+functions are provided, and how they respond will determine exactly
+how the display device operates. The primary choice will be whether the
+device runs in &quot;full page&quot; or &quot;rectangle request&quot;
+ mode. Details of these are given below.</p>
+
+<p>Once we have finished processing the file, we can process
+other files using <code>gsapi_run_file</code>, or feed in data
+using <code>gsapi_run_string</code>. Once you have finished, you
+can shut the interpreter down and exit, using:</p>
+
+<pre>
+ code = gsapi_exit(minst);
+ gsapi_delete_instance(minst);
+</pre>
+
+<p>A full list of the display callback functions can be found in
+<code><a href="../devices/gdevdsp.h">gdevdsp.h</a></code>. There
+are several different versions of the callback, corresponding to
+different &quot;generations&quot; of the device. In general
+you should use the latest one. The <code>size</code> field of
+the structure should be initialised to the size of the structure
+in bytes.</p>
+
+<h3><a name="display_open"></a>display_open</h3>
+
+<pre>int (*display_open)(void *handle, void *device);</pre>
+
+<p>This function will be called when the display device is
+opened. The device may be opened and closed many times,
+sometimes without any output being produced.</p>
+
+<h3><a name="display_preclose"></a>display_preclose</h3>
+
+<pre>int (*display_preclose)(void *handle, void *device);</pre>
+
+<p>This function will be called when the display device is
+about to be closed. The device will not actually be closed
+until this function returns.</p>
+
+<h3><a name="display_close"></a>display_close</h3>
+
+<pre>int (*display_close)(void *handle, void *device);</pre>
+
+<p>This function will be called once the display device has
+been closed. There will be no more events from the device
+unless/until it is reopened.</p>
+
+<h3><a name="display_presize"></a>display_presize</h3>
+
+<pre>int (*display_presize)(void *handle, void *device,
+ int width, int height, int raster, unsigned int format);</pre>
+
+<p>This function will be called when the display device is
+about to be resized. The device will only be resized if this
+function returns 0.</p>
+
+<h3><a name="display_size"></a>display_size</h3>
+
+<pre>int (*display_size)(void *handle, void *device, int width, int height,
+ int raster, unsigned int format, unsigned char *pimage);</pre>
+
+<p>This function will be called when the display device is
+has been resized. The pointer to the raster image is pimage.</p>
+
+<h3><a name="display_sync"></a>display_sync</h3>
+
+<pre>int (*display_sync)(void *handle, void *device);</pre>
+
+<p>This function may be called periodically during display
+to flush the page to the display.</p>
+
+<h3><a name="display_page"></a>display_page</h3>
+
+<pre>int (*display_page)(void *handle, void *device, int copies, int flush);</pre>
+
+<p>This function is called on a &quot;showpage&quot; operation
+(i.e. at the end of every page). Operation will continue as soon as
+this function returns.</p>
+
+<h3><a name="display_update"></a>display_update</h3>
+
+<pre>int (*display_update)(void *handle, void *device,
+ int x, int y, int w, int h);</pre>
+
+<p>This function <strong>may</strong> get called repeatedly
+during rendering to indicate that an area of the output has
+been updated. Certain types of rendering will not see this
+function called back at all (in particular files using
+transparency).</p>
+
+<h3><a name="display_memalloc"></a>display_memalloc</h3>
+
+<pre>int (*display_memalloc)(void *handle, void *device,
+ size_t long size);</pre>
+
+<p><strong>Note:</strong> In older versions of this API,
+size is an <code>unsigned long</code> rather than a
+<code>size_t</code>.</p>
+<p>If this function pointer is sent as NULL, then the
+display device will handle all the memory allocations
+internally, and will always work in full page rendering
+mode.</p>
+<p>Otherwise, this function will be called to allocate
+the storage for the page to be rendered into. If a non-NULL
+value is returned, then the device will proceed to render
+the full page into it. If NULL is returned, then the device
+will check a) whether we are using a V2 or greater display
+callback structure and b) whether that structure specifies
+a <code>rectangle_request</code> function pointer.</p>
+<p>If both of those conditions are true, then the device
+will continue in rectangle request mode. Otherwise it will
+fail with an out of memory error.</p>
+
+
+<h3><a name="display_memfree"></a>display_memfree</h3>
+
+<pre>int (*display_memfree)(void *handle, void *device, void *ptr);</pre>
+
+<p>This function should be NULL if and only if
+<code><a href="#display_memalloc">display_memalloc</a></code> is
+NULL. Any memory allocated using <code>display_memalloc</code> will be
+freed via this function.</p>
+
+<h3><a name="display_separation"></a>display_separation</h3>
+<pre>int (*display_separation)(void *handle, void *device,
+ int component, const char *component_name,
+ unsigned short c, unsigned short m,
+ unsigned short y, unsigned short k);</pre>
+
+<p>When using <code>DISPLAY_COLORS_SEPARATION</code>, this function
+ will be called once for every separation component - first
+ &quot;Cyan&quot;, &quot;Magenta&quot;, &quot;Yellow&quot; and
+ &quot;Black&quot;, then any spot colors used. The supplied
+ <code>c</code>, <code>m</code>, <code>y</code> and <code>k</code>
+ values give the equivalent color for each spot. Each colorant
+ value ranges from 0 (for none) to 65535 (full).</p>
+<p>In separation color mode you are expected to count the number
+of calls you get to this function after each
+<code><a href="#display_size">display_size</a></code> to know how many
+colors you are dealing with.</p>
+
+<h3><a name="display_adjust_band_height"></a>display_adjust_band_height</h3>
+<pre>int (*display_adjust_band_height)(void *handle, void *device,
+ int bandheight);</pre>
+
+<p>When running in &quot;rectangle request mode&quot; the device
+first renders the page to a display list internally. It can then
+be played back repeatedly so that different regions (rectangles)
+of the page can be extracted in sequence. A common use of this is
+to support &quot;banded&quot; operation, where the page is divided
+into multiple non-overlapping bands of a fixed height.</p>
+<p>The display device itself will pick an appropriate band height
+for it to use. If this function pointer is left as NULL then this
+value will be used unchanged. Otherwise, the proposed value will
+be offered to this function. This function can override the choice
+of bandheight, by returning the value that it would like to be
+used in preference.</p>
+<p>In general, this figure should (as much as possible) only be
+adjusted downwards. For example, a device targeting an inkjet printer
+with 200 nozzles in the print head might like to extract bands
+that are a multiple of 200 lines high. So the function might
+return <code>max(200, 200*(bandheight/200))</code>. If the function
+returns 0, then the existing value will be used unchanged.</p>
+<p>Any size rectangle can be chosen with any size bandheight,
+so ultimately the value chosen here will not matter much. It
+may make some small difference in speed in some cases.</p>
+
+<h3><a name="display_rectangle_request"></a>display_rectangle_request</h3>
+<pre>int (*display_rectangle_request)(void *handle, void *device,
+ void **memory, int *ox, int *oy,
+ int *raster, int *plane_raster,
+ int *x, int *y, int *w, int *h);</pre>
+
+<p>If the display device chooses to use rectangle request mode,
+this function will be called repeatedly to request a rectangle
+to render. Ghostscript will render the rectangle, and call this
+function again. The implementer is expected to handle the rectangle
+that has just been rendered, and to return the details of another
+rectangle to render. This will continue until a rectangle with
+zero height or width is returned, whereupon Ghostscript will
+continue operation.</p>
+<p>On entry, <code>*raster</code> and <code>*plane_raster</code>
+are set to the values expected by the format in use. All the
+other pointers point to uninitialised values.</p>
+<p>On exit, the values should be updated appropriately. The
+implementor is expected to store the values returned so that
+the rendered output given can be correctly interpreted when
+ control returns to this function.</p>
+<p><code>memory</code> should be updated to point to a block of
+memory to use for the rendered output. Pixel (<code>*ox</code>,
+<code>*oy</code>) is the first pixel represented in that block.
+<code>*raster</code> is the number of bytes difference between
+the address of component 0 of Pixel(<code>*ox</code>, <code>*oy</code>)
+and the address of component 0 of Pixel(<code>*ox</code>,
+<code>1+*oy</code>). <code>*plane_raster</code> is the number of
+bytes difference between the address of component 0 of
+Pixel(<code>*ox</code>, <code>*oy</code>) and the address of
+component 1 of Pixel(<code>*ox</code>, <code>*oy</code>), if in
+planar mode, 0 otherwise. <code>*x</code>, <code>*y</code>,
+<code>*w</code> and <code>*h</code> give the rectangle requested
+within that memory block.</p>
+<p>Any set of rectangles can be rendered with this method, so this
+can be used to drive Ghostscript in various ways. Firstly, it is
+simple to request a set of non-overlapping &quot;bands&quot; that
+cover the page, to drive a printer. Alternatively, rectangles can
+be chosen to fill a given block of memory to implement a window
+panning around a larger page. Either the whole image could be
+redrawn each time, or smaller rectangles around the edge of the
+panned area could be requested. The choice is down to the caller.</p>
+
<p>
-The display device provides you with the address and size of the
-raster using the <code>display_size()</code> callback.
-You are then responsible for displaying this raster.
-Some examples are in
+Some examples of driving this code in full page mode are in
<code><a href="../psi/dwmain.c">dwmain.c</a></code> (Windows),
<code><a href="../psi/dpmain.c">dpmain.c</a></code> (OS/2) and
<code><a href="../psi/dxmain.c">dxmain.c</a></code> (X11/Linux), and
<code><a href="../psi/dmmain.c">dmmain.c</a></code> (MacOS Classic or Carbon).</p>
+<p>Alternatively an example that drives this code in both full page
+and rectangle request mode can be found in
+<code><a href="../toolbin/displaydevice_test.c">displaydevice_test.c</a></code>.</p>
+
<p>
On some platforms, the calling convention for the display device callbacks in
<code><a href="../base/gdevdsp.h">gdevdsp.h</a></code>
@@ -1134,7 +1761,7 @@ functions in <a href="../psi/iapi.h"><code>iapi.h</code></a>.</p></p>
<!-- [3.0 begin visible trailer] =========================================== -->
<hr>
<p>
-<small>Copyright &copy; 2000-2019 Artifex Software, Inc. All rights reserved.</small></p>
+<small>Copyright &copy; 2000-2020 Artifex Software, Inc. All rights reserved.</small></p>
<p>
This software is provided AS-IS with no warranty, either express or