1. Dictionary Operations
Dictionary operations provide PostScript’s primary mechanism for associating keys with values. Dictionaries serve as namespaces, configuration stores, and the foundation for the language’s scoping and name lookup mechanisms.
2. Overview
Dictionaries in PostScript are:
-
Key-value stores: Associate arbitrary keys (typically names) with values
-
Scoped namespaces: Organized in a dictionary stack for hierarchical lookup
-
Dynamic: Can grow beyond initial capacity (Level 2)
-
Fundamental: Used for variable storage, procedure definitions, and system configuration
4. Command Categories
4.2. Dictionary Stack Operations
| Command | Description |
|---|---|
Push dictionary onto dictionary stack |
|
Pop dictionary from dictionary stack |
|
Get current dictionary (top of stack) |
|
Count dictionaries on stack |
|
Copy dictionary stack to array |
4.3. Dictionary Content Operations
| Command | Description |
|---|---|
Define key-value pair in current dictionary |
|
Look up and return value from dictionary stack |
|
Store value in dictionary stack (create if needed) |
|
Check if key exists in specific dictionary |
|
Find which dictionary contains key |
|
Remove key from dictionary (Level 2) |
5. Common Patterns
5.1. Creating and Using Dictionaries
% Create a dictionary with initial capacity
5 dict begin
/width 100 def
/height 200 def
/color /red def
end
% Use the dictionary
currentdict /width get % Returns 100
6. Important Considerations
6.1. Dictionary Stack Hierarchy
The standard dictionary stack (bottom to top):
-
systemdict- Read-only system definitions -
globaldict- Global definitions (Level 2) -
userdict- User definitions -
User-created dictionaries (via
begin)
6.2. Level 1 vs. Level 2 Differences
Level 1:
* Fixed capacity - [dictfull] error if exceeded
* Must specify sufficient size at creation
* No global VM concept
Level 2: * Auto-expanding - grows beyond initial capacity * Initial size is optimization hint * Global vs. local VM distinctions
7. Common Pitfalls
Fixed Capacity (Level 1) - In Level 1, exceeding dictionary capacity causes [dictfull] error. Always allocate sufficient space.
|
Global/Local VM - Cannot store local VM objects into global VM dictionaries - causes [invalidaccess].
|
Use begin/end Blocks - Bracket dictionary use with begin/end for scoped access:
|
mydict begin
% Definitions go into mydict
/key value def
end
8. See Also
-
Stack Manipulation - For general stack operations
-
Array and String Operations - Similar composite types
-
Control Flow (to be documented) - Scoping with dictionaries