1. Data Types
PostScript has a rich type system with distinct types for different kinds of data. Understanding the type system is essential for writing robust PostScript programs and avoiding type-related errors.
1.1. Overview
Every PostScript object has a type that determines what operations can be performed on it. The type system includes simple types (stored by value) and composite types (stored by reference), with clear rules for type checking, conversion, and compatibility.
1.2. Type Hierarchy
PostScript’s type system organizes types into a clear hierarchy:
PostScript Types
├── Simple Types (stored by value)
│ ├── Numeric Types
│ │ ├── integertype Integer numbers
│ │ └── realtype Floating-point numbers
│ ├── Boolean Types
│ │ └── booleantype True/false values
│ ├── Name Types
│ │ └── nametype Identifiers
│ ├── Operator Types
│ │ └── operatortype Built-in operations
│ ├── Mark Types
│ │ └── marktype Stack delimiter
│ ├── Null Types
│ │ └── nulltype Empty value
│ └── Save Types
│ └── savetype VM snapshot
└── Composite Types (stored by reference)
├── Collection Types
│ ├── stringtype Byte sequences
│ ├── arraytype Object arrays
│ ├── packedarraytype Compact arrays (Level 2+)
│ └── dicttype Key-value maps
├── I/O Types
│ └── filetype Input/output streams
├── Graphics Types
│ ├── fonttype Font dictionaries
│ └── gstatetype Graphics state (Level 2+)
└── Condition Types
└── conditiontype Synchronization (Level 2+)
1.3. Simple Types
Simple types store their values directly in the object.
1.3.1. Integer Type
Whole numbers within implementation-defined range.
| Property | Description |
|---|---|
Type Name |
|
Range |
Typically -2³¹ to 2³¹-1 (32-bit signed) |
Literal Syntax |
|
Radix Notation |
|
Storage |
By value |
Mutability |
Immutable |
42 type % Returns /integertype
-17 type % Returns /integertype
16#FF type % Returns /integertype
% Type checking
42 dup type /integertype eq % Returns true
% Operations
10 20 add % 30 (integer)
10 3 div % 3.33... (becomes real)
10 3 idiv % 3 (stays integer)
1.3.2. Real Type
Floating-point numbers with fractional parts.
| Property | Description |
|---|---|
Type Name |
|
Range |
Implementation-dependent (typically IEEE 754) |
Precision |
Implementation-dependent (typically single or double) |
Literal Syntax |
|
Storage |
By value |
Mutability |
Immutable |
3.14 type % Returns /realtype
-2.5 type % Returns /realtype
6.02e23 type % Returns /realtype
% Type checking
3.14 dup type /realtype eq % Returns true
% Operations
3.14 2.0 mul % 6.28 (real)
10 3.0 div % 3.33... (real)
1.5 ceiling % 2.0 (real result)
1.3.3. Boolean Type
Logical true/false values.
| Property | Description |
|---|---|
Type Name |
|
Values |
|
Literal Syntax |
|
Storage |
By value |
Mutability |
Immutable |
true type % Returns /booleantype
false type % Returns /booleantype
% Type checking
true dup type /booleantype eq % Returns true
% Operations
true false and % false
true false or % true
true not % false
5 3 gt % true
1.3.4. Name Type
Identifiers used as dictionary keys and references.
| Property | Description |
|---|---|
Type Name |
|
Literal Syntax |
|
Storage |
By value (internal integer ID) |
Mutability |
Immutable |
Max Length |
128 chars (Level 1), 65535 (Level 2+) |
/myname type % Returns /nametype
/add type % Returns /nametype
% Type checking
/x dup type /nametype eq % Returns true
% Name operations
/myname == % Prints /myname
(string) cvn type % Returns /nametype
/name 20 string cvs % Returns (name)
1.3.5. Operator Type
Built-in primitive operations.
| Property | Description |
|---|---|
Type Name |
|
Examples |
|
Storage |
By value (reference to built-in) |
Mutability |
Immutable |
Executable |
Always executable |
/add load type % Returns /operatortype
/moveto load type % Returns /operatortype
% Type checking
/add load dup type /operatortype eq % Returns true
% Cannot be made literal
/add load cvlit type % Still /operatortype
1.3.6. Mark Type
Special stack marker object.
| Property | Description |
|---|---|
Type Name |
|
Literal Syntax |
|
Storage |
By value |
Purpose |
Stack delimiter for array construction |
mark type % Returns /marktype
[ type % Returns /marktype
% Usage
mark 1 2 3 counttomark % Returns 3
mark 1 2 3 ] % Creates [1 2 3]
1.3.7. Null Type
Empty or undefined value.
| Property | Description |
|---|---|
Type Name |
|
Literal Syntax |
|
Storage |
By value |
Purpose |
Represent "no value" or initialization |
null type % Returns /nulltype
% Type checking
null dup type /nulltype eq % Returns true
% Usage
/uninitialized null def
uninitialized null eq % Returns true
1.3.8. Save Type
VM snapshot for save/restore operations.
| Property | Description |
|---|---|
Type Name |
|
Creation |
|
Storage |
By value (reference to snapshot) |
Purpose |
Mark VM state for restoration |
save type % Returns /savetype
% Usage
save
/temp 42 def
restore % temp is undefined again
1.4. Composite Types
Composite types store their values indirectly through references.
1.4.1. String Type
Sequences of 8-bit bytes.
| Property | Description |
|---|---|
Type Name |
|
Literal Syntax |
|
Max Length |
65,535 bytes |
Storage |
By reference |
Mutability |
Mutable |
Indexed Access |
0-based byte access |
(Hello) type % Returns /stringtype
<4142> type % Returns /stringtype
% Type checking
(text) dup type /stringtype eq % Returns true
% Operations
(Hello) length % 5
(Hello) 0 get % 72 (ASCII 'H')
(Hello) dup 0 88 put % Modifies to (Xello)
1.4.2. Array Type
Ordered collections of objects.
| Property | Description |
|---|---|
Type Name |
|
Literal Syntax |
|
Max Length |
65,535 elements |
Storage |
By reference |
Mutability |
Mutable |
Indexed Access |
0-based element access |
[1 2 3] type % Returns /arraytype
{ 1 2 add } type % Returns /arraytype (executable)
% Type checking
[1 2] dup type /arraytype eq % Returns true
% Operations
[1 2 3] length % 3
[1 2 3] 1 get % 2
[1 2 3] dup 0 99 put % Modifies to [99 2 3]
1.4.3. Packed Array Type (Level 2+)
Space-efficient read-only arrays.
| Property | Description |
|---|---|
Type Name |
|
Creation |
|
Storage |
By reference (compact) |
Mutability |
Read-only |
Space |
More compact than regular arrays |
mark 1 2 3 ] packedarray type % Returns /packedarraytype
% Read-only
mark 1 2 3 ] packedarray
dup 0 99 put % Error: invalidaccess
1.4.4. Dictionary Type
Associative arrays (key-value pairs).
| Property | Description |
|---|---|
Type Name |
|
Creation |
|
Storage |
By reference |
Mutability |
Mutable |
Key Types |
Any object (typically names) |
10 dict type % Returns /dicttype
<< /a 1 >> type % Returns /dicttype
% Type checking
10 dict dup type /dicttype eq % Returns true
% Operations
<< /x 42 >> /x get % 42
<< >> dup /y 10 put % Add entry
1.4.5. File Type
Input/output streams.
| Property | Description |
|---|---|
Type Name |
|
Creation |
|
Storage |
By reference |
Mutability |
State changes |
Purpose |
Reading/writing data |
(file.txt) (r) file type % Returns /filetype
stdin type % Returns /filetype
% Type checking
stdin dup type /filetype eq % Returns true
1.4.6. Font Type
Font dictionaries.
| Property | Description |
|---|---|
Type Name |
|
Creation |
|
Storage |
By reference (special dictionary) |
Mutability |
Varies |
Purpose |
Text rendering |
/Times-Roman findfont type % Returns /fonttype
% Type checking
currentfont type % Returns /fonttype
1.4.7. Graphics State Type (Level 2+)
Captured graphics state.
| Property | Description |
|---|---|
Type Name |
|
Creation |
|
Storage |
By reference |
Purpose |
Save/restore graphics state |
gstate type % Returns /gstatetype
% Usage
gstate /mygs exch def
mygs setgstate
1.5. Type Checking
1.5.1. type Operator
Returns the type name of an object:
42 type % /integertype
3.14 type % /realtype
(text) type % /stringtype
[1 2 3] type % /arraytype
true type % /booleantype
/name type % /nametype
1.5.2. Type Comparison
% Check if integer
/CheckInteger {
type /integertype eq
} def
42 CheckInteger % true
3.14 CheckInteger % false
% Check if numeric
/CheckNumeric {
type dup /integertype eq
exch /realtype eq or
} def
42 CheckNumeric % true
3.14 CheckNumeric % true
(text) CheckNumeric % false
1.6. Type Conversion
1.6.1. Numeric Conversions
| Operator | Conversion | Example |
|---|---|---|
|
To integer |
|
|
To real |
|
|
Round to nearest |
|
|
Truncate to integer |
|
|
Round down |
|
|
Round up |
|
% Real to integer
3.14 cvi % 3
-2.8 cvi % -2
% Integer to real
42 cvr % 42.0
% Rounding
3.7 round cvi % 4
3.2 floor cvi % 3
3.2 ceiling cvi % 4
1.6.2. String Conversions
| Operator | Conversion | Example |
|---|---|---|
|
To string |
|
|
String to name |
|
|
String to integer |
|
|
String to real |
|
% Number to string
42 10 string cvs % (42)
3.14 20 string cvs % (3.14)
% String to number
(42) cvi % 42
(3.14) cvr % 3.14
% String to name
(myname) cvn % /myname
% Name to string
/myname 20 string cvs % (myname)
1.6.3. Literal/Executable Conversion
| Operator | Conversion | Example |
|---|---|---|
|
To literal |
|
|
To executable |
|
% Make literal
{ 1 2 add } cvlit % Literal array
add cvlit % /add (name)
% Make executable
/add cvx % add (operator)
[1 2 /add] cvx % Executable array
1.7. Type Compatibility
1.7.1. Numeric Type Mixing
Arithmetic operations automatically promote integers to reals when needed:
% Integer + Integer → Integer
10 20 add % 30 (integer)
% Integer + Real → Real
10 20.0 add % 30.0 (real)
% Integer / Integer → Real
10 3 div % 3.33... (real)
% Integer idiv Integer → Integer
10 3 idiv % 3 (integer)
1.8. Type-Specific Operations
1.8.1. Integer-Only Operations
| Operator | Description |
|---|---|
|
Integer division |
|
Modulo (remainder) |
|
Bit shift |
|
Bitwise AND |
|
Bitwise OR |
|
Bitwise XOR |
|
Bitwise NOT |
10 3 idiv % 3 (integer division)
10 3 mod % 1 (remainder)
4 2 bitshift % 16 (left shift)
12 10 and % 8 (bitwise AND)
1.8.2. Real-Only Operations
| Operator | Description |
|---|---|
|
Sine (degrees) |
|
Cosine (degrees) |
|
Arctangent |
|
Square root |
|
Exponential |
|
Natural logarithm |
|
Base-10 logarithm |
90 sin % 1.0
0 cos % 1.0
1 atan % 45.0
9.0 sqrt % 3.0
1 exp % 2.71828...
1.8.3. String-Specific Operations
| Operator | Description |
|---|---|
|
Find substring |
|
Search from start |
|
Parse token from string |
1.9. Type Error Handling
1.9.1. Common Type Errors
| Error | Cause |
|---|---|
|
Wrong operand type for operation |
|
Value out of valid range |
|
Not enough operands |
|
Access permission violation |
% typecheck
(text) 5 add % Error: can't add string
% rangecheck
-5 array % Error: negative size
% stackunderflow
add % Error: needs 2 operands
% invalidaccess
[1 2 3] readonly
dup 0 99 put % Error: read-only
1.9.2. Type-Safe Programming
/SafeAdd {
% in: a b
% out: sum or error
% Check both are numeric
1 index type dup /integertype eq
exch /realtype eq or
1 index type dup /integertype eq
exch /realtype eq or
and {
add
} {
(Type error: expected numbers) =
pop pop 0
} ifelse
} def
3 4 SafeAdd % 7
3 (x) SafeAdd % Error message, returns 0
1.10. Type System Best Practices
1.10.1. Type Documentation
/WellTyped {
% in: int int string
% out: array
% Description: Creates array from parameters
% ... implementation ...
} def
1.10.2. Type Validation
/ValidateParams {
% Validate all parameters are correct type
% before proceeding with operation
} def
1.11. Type System Summary
1.11.1. Type Categories
| Category | Storage | Types |
|---|---|---|
Simple |
By value |
integer, real, boolean, name, operator, mark, null, save |
Composite |
By reference |
string, array, dict, file, font, gstate, packedarray |
Numeric |
By value |
integer, real |
Collection |
By reference |
string, array, packedarray, dict |
1.11.2. Type Mutability
| Type | Mutable | Notes |
|---|---|---|
integer |
No |
Immutable value |
real |
No |
Immutable value |
boolean |
No |
Immutable value |
name |
No |
Immutable value |
string |
Yes |
Mutable bytes |
array |
Yes |
Mutable elements |
packedarray |
No |
Read-only |
dict |
Yes |
Mutable entries |
file |
State |
Mutable state |
1.11.3. Type Checking Quick Reference
% Get type
obj type % Returns type name
% Check type
obj type /typename eq % Boolean result
% Validate type
obj type /typename ne {
/typecheck cvx signalerror
} if
% Convert type
obj cvi % To integer
obj cvr % To real
obj cvs % To string
obj cvn % To name
obj cvlit % To literal
obj cvx % To executable
1.12. See Also
-
Objects - Object model and attributes
-
Tokens - Type literal syntax
-
Operators - Type-specific operations
-
Strings - String type details
-
Arrays - Array type details
-
Dictionaries - Dictionary type details
-
Procedures - Array type as procedures
-
type - Get object type
-
cvi - Convert to integer
-
cvr - Convert to real
-
cvs - Convert to string
-
cvn - Convert to name
-
cvlit - Make literal
-
cvx - Make executable