1. Objects
Understanding PostScript objects and their attributes.
1.1. Overview
In PostScript, everything is an object. Objects are the fundamental data entities that the PostScript interpreter manipulates. Each object has a type and may have attributes that control how it behaves.
1.2. Object Types
PostScript defines several fundamental object types:
1.2.1. Simple Objects
- Integer
-
Whole numbers in the range -2³¹ to 2³¹-1
42 -17 0 2147483647 - Real
-
Floating-point numbers with approximately 7 significant digits
3.14 -0.5 6.02e23 1.0E-10 - Boolean
-
Logical true or false values
true false - Name
-
Identifiers used to reference other objects
/Times-Roman /x /myproc - Null
-
A special object representing no value
null
1.2.2. Composite Objects
- String
-
Sequence of bytes, typically representing text
(Hello, World!) <48656C6C6F> % Hexadecimal string - Array
-
Ordered collection of objects
[1 2 3] [/name 42 (text)] - Dictionary
-
Associative array mapping keys to values
<< /FontName /Times-Roman /FontSize 12 >> - Procedure
-
Executable array (code block)
{ dup mul } % Square a number
1.2.3. Special Objects
- Mark
-
Stack delimiter used in array construction
mark % or equivalently [ - Operator
-
Built-in PostScript command
add sub moveto show - File
-
Input/output stream
currentfile (myfile.ps) (r) file - Save
-
VM snapshot for state restoration
save % Returns save object - Font
-
Character shape collection
/Times-Roman findfont % Returns font dictionary
1.3. Object Attributes
Each object has attributes that control its behavior:
1.3.1. Literal vs. Executable
- Literal
-
The object represents itself
/name % Literal name (text) % Literal string [1 2 3] % Literal array - Executable
-
The object is executed when encountered
name % Executable name (looks up and executes) { add } % Executable array (procedure)
Convert between literal and executable:
/name cvx % Make name executable
{ code } cvlit % Make procedure literal
1.3.2. Access Attributes
- No Access
-
Cannot be accessed
- Read-Only
-
Can be read but not modified
- Execute-Only
-
Can only be executed (for arrays)
- Unlimited
-
Can be read, written, and executed
Check and modify access:
dict rcheck % Check if readable
dict wcheck % Check if writable
dict xcheck % Check if executable
dict readonly % Make read-only
dict executeonly % Make execute-only (arrays)
1.4. Type Checking
Get an object’s type:
42 type % Returns: /integertype
3.14 type % Returns: /realtype
(text) type % Returns: /stringtype
[1 2 3] type % Returns: /arraytype
{ add } type % Returns: /arraytype (procedures are arrays)
/name type % Returns: /nametype
true type % Returns: /booleantype
null type % Returns: /nulltype
1.5. Type Conversion
Convert between types:
% String to number
(42) cvi % String to integer: 42
(3.14) cvr % String to real: 3.14
% Number to string
42 20 string cvs % Integer to string: (42)
% String to name
(myname) cvn % String to name: /myname
% Name to string
/myname 20 string cvs % Name to string: (myname)
% Integer to real
42 cvr % Integer to real: 42.0
% Real to integer
3.14 cvi % Real to integer: 3 (truncated)
1.6. Object Equality
PostScript has two notions of equality:
For composite objects, eq tests object identity, not value equality.
1.7. Object Sharing
Composite objects can be shared:
/arr1 [1 2 3] def
/arr2 arr1 def % arr2 and arr1 refer to same array
arr1 0 99 put % Modifies both arr1 and arr2
arr1 == % [99 2 3]
arr2 == % [99 2 3] (same object)
Copy to create independent objects:
/arr1 [1 2 3] def
/arr2 arr1 length array def
arr1 arr2 copy pop % Create independent copy
arr1 0 99 put % Only modifies arr1
arr1 == % [99 2 3]
arr2 == % [1 2 3] (different object)
1.8. Object Memory Management
- Local VM
-
Objects allocated here are temporary
save /local [1 2 3] def % Local allocation restore % local is discarded - Global VM
-
Objects persist across save/restore
true setglobal /global [1 2 3] def % Global allocation false setglobal
1.9. Common Patterns
1.10. Best Practices
Check Types - Use type and comparison to validate object types before operations.
|
| Be Aware of Sharing - Remember that assignment creates references, not copies, for composite objects. |
| Use Appropriate Access - Set proper access attributes to prevent unintended modifications. |
Type Errors - Operating on wrong types causes [typecheck] errors. Always validate types for robustness.
|