I usually look at ghostscript
as a command line tool; however, I never cease to be amazed at the sheer amount of settings and options present there - which is due to the fact that ghostscript
is a full blown PostScript language interpreter (which I often forget).
For instance, in Querying Ghostscript for the default options/settings of an output device (such as 'pdfwrite' or 'tiffg4'); one learns how to retrieve default options for a given output device. However, what I'd like to know is - are these options related to so-called PostScript dictionaries?
Or, to put it in other words - what are PostScript dictionaries; and what facilities does ghostscript
have, to query (and possibly) modify this data?
To put it in the most simple terms: In PostScript, a dictionary is a list of key (name) + value pairs. Dictionaries allow the PostScript interpreter to lookup if a key exists and fetch its value to use it in any procedure. The interpreter also can create keys, store or modify values and even create complete custom dictionaries (dictated by the PostScript code its processing). Keys usually are of type name (but they may be of any other type as well with the exception of null).
Two of these dictionaries must always be present, for any implementation of a PostScript interpreter:
systemdict
This one holds pre-defined PostScript operators (and the implementations to make them do what the PostScript specification expects them to).
userdict
This one holds variables and procedures of a PostScript program (think of 'procedures' as being functions or subroutines which are constructed by the combination of language-defined operators and program-defined values and parameters).
One word about names: names are what to other programming languages are uniq identifiers (and they are case-sensitive). These identifiers may be variables or procedure names. They may be made up of any combination of the 256 characters of ASCII (but they are no strings).
As you may be aware, PostScript is a stack-oriented language. It uses several stacks:
operand stack This stack holds every single operand and every result of intermediate operations (turning the last result temporarily into the top-most element of the operand stack).
dictionary stack As the name says: this stack holds only dictionaries. As such the stack defines the current context for any key/name lookup.
execution stack This one holds executable objects, i.e. mainly procedures and files which are currently being executed. If the interpreter interrupts the execution of a current object, it puts the interrupted object onto this stack. After an object was completely executed, it is removed from the stack and execution continues with the one that is top-most now.
graphics state stack
This stack hosts the current context for the ejection of graphical elements: current line width setting, current font, current color or grayscale value, current path... Current graphic states may be saved (gsave
) and restored (grestore
) later. The top-most graphics state is always the current graphics state.
All these stacks are independent from each other. However, the operand, dictionary and graphics state stacks are under the control of the PostScript program (that is, may be manipulated by it). The execution stack is the sole property of the interpreter.
For each stack there are certain limitations (as for the number of elements which may be stored on it, etc.). PostScript knows operators which can manipulate stacks: put a new element on the stack, remove the top-most element (pop
), duplicate the top-most element (dup
), shuffle the order of elements on the stack (roll
), swap the two top-moste elements (exch
), and quite some more (a good intro into PostScript programming is the 'Bluebook' from Adobe).
As I already said, dictionaries have their own stack which holds all dictionaries a PostScript interpreter may use.
On that stack there may be a separate dictionary of fonts, or any number of dictionaries a PostScript program wants to create (using the dict
keyword) and use privately, or some dictionaries that are specific to a certain PostScript interpreter, such as Ghostscript.
The systemdict
always is the bottom-most one; above this is the userdict
. These two cannot be removed from the dictionary stack, wheres all the other ones can be subject to any stack manipulation operator (such as pop
which removes the topmost element from a stack).
Whenever the interpreter is looking up a name, it searches the dictionaries for that name, starting with the top-most dictionary. Hence userdict
is searched before systemdict
. As soon as the name is found (a key), the interpreter stops searching and uses that key (or rather, the value it holds). The consequence of this architecture is that the PostScript programmer may overwrite any PostScript operator that is pre-defined in systemdict
with his own variant.
Also, some dictionaries can be for the PS program 'private' (no-access, such as font dictionaries) or 'read-only'.
Update -- More answers:
userdict
or systemdict
?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With