
- •Preface
- •DESIGN FEATURES
- •STRUCTURED PROGRAMMING TECHNIQUES
- •PROGRAMMING TASKS
- •WINDOW SYSTEMS, COMMUNICATIONS, AND DISPLAYS
- •DATA STRUCTURES AND ALGORITHMS
- •CONCLUDING THOUGHTS
- •PostScript is Not Like C
- •COMPARISON OF LANGUAGE MECHANISMS
- •EXPRESSING AN ALGORITHM AS A PROGRAM
- •THE UNIX SHELL AND OPERATING SYSTEM
- •INPUT, OUTPUT, AND THROUGHPUT
- •CONCLUDING THOUGHTS
- •Foundations
- •POSTSCRIPT LANGUAGE SYNTAX
- •SIMPLE PROGRAM STRUCTURE
- •Make Definitions First
- •Indentation Style
- •SETTING UP TEMPLATES
- •DECLARING AND USING VARIABLES
- •Arithmetic with Numeric Variables
- •Using the // Notation for Constants
- •ALLOCATING MEMORY
- •GETTING MEMORY BACK
- •OPENING AND CLOSING FILES
- •COMPARISONS AND EQUALITY OF OBJECTS
- •CONCLUDING THOUGHTS
- •Some Typical Programs
- •A TYPICAL PAGE DESCRIPTION PROGRAM
- •FONT PROGRAMS
- •PROGRAMS THAT READ DATA
- •QUERY PROGRAMS
- •ENCAPSULATED POSTSCRIPT PROGRAMS
- •PERSISTENTLY RESIDENT PROGRAMS
- •CONCLUDING THOUGHTS
- •Understanding the Stack
- •A QUICK OVERVIEW OF DATA TYPES
- •NAME LOOKUP
- •HOW OPERATORS USE THE STACK
- •GROUPING AND VISUAL CHUNKING
- •THINKING BACKWARD AND SIDEWAYS
- •COMPOSITE OBJECTS
- •THE OTHER STACKS
- •The Dictionary Stack
- •The Execution Stack
- •The Graphics State Stack
- •CONCLUDING THOUGHTS
- •Trusting the Stack
- •SAFETY OF DATA ON THE STACK
- •WHERE ARE THE DATA GOING?
- •REARRANGING THE STACK
- •Using the dup and index Operators
- •Using the roll Operator
- •CONDITIONALS AND LOOPS
- •RECURSION AND LOCAL VARIABLES
- •CONCLUDING THOUGHTS
- •Building Conditional Statements
- •SIMPLE CONDITIONALS
- •SETTING UP THE CONDITION
- •CONDITIONALS ARE NOT MAGIC
- •NESTED CONDITIONALS AND ELSE CLAUSES
- •COMPOUND CONDITIONALS
- •CONCLUDING THOUGHTS
- •Using Looping Constructs
- •LOOP BASICS
- •USING THE LOOP INDEX
- •LOOPS ARE PROCEDURE BODIES
- •LOOPS OF INSTRUCTIONS
- •EXITING LOOPS PREMATURELY
- •CONCLUDING THOUGHTS
- •Procedures
- •WHAT EXACTLY IS A PROCEDURE?
- •PARAMETER PASSING
- •CONSTRUCTING GOOD PROCEDURES
- •What to Name Your Procedure
- •A Useful Naming Convention
- •SELF-MODIFYING PROCEDURES
- •CONCLUDING THOUGHTS
- •Using Dictionaries
- •DICTIONARIES FOR NAME SCOPING
- •LOCAL DICTIONARIES
- •GLOBAL DICTIONARIES OF PROCEDURES
- •MAINTAINING THE DICTIONARY STACK
- •INTO AND OUT OF DICTIONARIES
- •LOOKING INTO DICTIONARIES
- •Using the forall Operator
- •Using the where and known Operators
- •REDEFINING OPERATORS
- •Changing the Behavior of Operators
- •Debugging with Redefined Names
- •Proper Nesting of Redefinitions
- •CONCLUDING THOUGHTS
- •Creating and Manipulating Data
- •CONSTRUCTING AN ARRAY
- •CONSTRUCTING A STRING
- •MANIPULATING DATA WITH PUT AND GET
- •CONCATENATING ARRAYS AND STRINGS
- •INPUT AND OUTPUT OF STRING DATA
- •ARRAYS VERSUS DICTIONARIES
- •ADVANCED TECHNIQUES
- •CONCLUDING THOUGHTS
- •Storing and Using Data
- •Data and the Operand Stack
- •Data and Algorithms for Underlining
- •CLASSICAL DATA STRUCTURES
- •Linked Lists
- •Using Arrays to Form Lists
- •Using Dictionaries to Form Lists
- •Queues, Trees, and Other Data Structures
- •CONCLUDING THOUGHTS
- •Program Data and Instructions
- •TURNING DATA INTO INSTRUCTIONS
- •TURNING INSTRUCTIONS INTO DATA
- •DATA CONVERSIONS
- •CONCLUDING THOUGHTS
- •File Objects
- •Streams and Files
- •PostScript File Operators
- •OPENING AND CLOSING FILES
- •READING AND WRITING FILES
- •Reading from a File
- •Writing to a File
- •Copying and Renaming Files
- •WRITING FORMATTED DATA TO FILES
- •Writing Out Various Data Types
- •Spaces, Tabs, Returns, and Special Characters
- •FILE STATUS INFORMATION
- •RANDOM VERSUS SEQUENTIAL ACCESS
- •CONCLUDING THOUGHTS
- •Appendix
- •Answers to Exercises

PERSISTENTLY RESIDENT PROGRAMS
Yet another type of PostScript program is one that makes some persistent definitions that are visible to all subsequent programs. This provides a mechanism for redefining operators, making fonts semipermanent in the interpreter, or defining some procedure definitions once in such a way that they are usable to all subsequent programs.
On most printer implementations, this can be accomplished using the exitserver operator. This operator permits the program to exit the job server loop’s save/restore context, causing any definitions made to stay resident until the printer is rebooted. Example 4.6 shows the use of exitserver, in this case to make a font definition semipermanent.
Example 4.6: Sample Printer-Resident Program
%!PS-Adobe-2.0 ExitServer %%EndComments %%BeginExitServer: 0
serverdict begin 0 exitserver %%EndExitServer
%%BeginFont: UserFont 13 dict begin
/FontName /UserFont def /FontType 3 def /FontMatrix [ .001 0 0 .001 0 0 ] def /FontBoundingBox [ 0 0 1000 1000 ] def /Encoding /StandardEncoding load def
/BuildChar % fontdict charcode BuildChar - { %def
exch begin
Encoding exch get 500 0 setcharwidth CharDefs exch get exec
end } bind def
/CharDefs 256 dict def CharDefs begin Encoding { %forall
{ %def
4 { rand 1000 mod } repeat moveto lineto stroke
}def
}bind forall
end
currentdict end dup /FontName get exch definefont pop %%EndFont
%%EOF
46 |
Chapter 4: SOME TYPICAL PROGRAMS |

In window system environments, the concept of a job server loop is often not present, since a windowed environment is usually not a batch system. With the Display PostScript System, there is a dictionary called shareddict, in which all entries are shared by all processes (and hence visible to all). Example 4.7 shows the same font definition made permanent or public in Display PostScript. Notice the use of the currentshared and setshared operators.
Example 4.7: Sample Display-Server-Resident Program
%!PS-Adobe-2.0 ExitServer %%EndComments %%BeginExitServer: 0
dup 0 eq { pop } if true setshared %%EndExitServer %%BeginFont: UserFont
13 dict begin
/FontName /UserFont def /FontType 3 def /FontMatrix [ .001 0 0 .001 0 0 ] def /FontBoundingBox [ 0 0 1000 1000 ] def /Encoding /StandardEncoding load def
/BuildChar % fontdict charcode BuildChar - { %def
exch begin
Encoding exch get 500 0 setcharwidth
CharDefs exch get exec
end } bind def
/CharDefs 256 dict def CharDefs begin Encoding { %forall
{ %def
4 { rand 1000 mod } repeat moveto lineto stroke
}def
}bind forall
end
currentdict end dup /FontName get exch definefont pop %%EndFont
%%EOF
Chapter 4: SOME TYPICAL PROGRAMS |
47 |

TIP |
|
The exitserver operator is not a standard part of the language. It is de- |
||||||||||||||||||||
|
|
fined in most printers, but is usually not implemented in display servers or |
||||||||||||||||||||
|
|
stand-alone interpreters. In general, use of exitserver is restricted to sys- |
||||||||||||||||||||
|
|
tem administrators. It should never be part of the output of a program, nor |
||||||||||||||||||||
|
|
should it be used routinely in any program. |
||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 4.4: Shared Memory Configuration |
||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shared virtual memory |
|
|
|
|
|
|
|
|
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
shareddict |
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
memory |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
process 3 |
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
process 2 |
|
|
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
virtual |
|
|
|
|
|
|
|
|
private |
|
|
||||||
|
private |
|
|
memory |
|
|
|
|
|
|
|
|
|
|
|
stacks |
|
|
||||
|
stacks |
|
|
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
private |
|
virtual |
|
|
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
memory |
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
stacks |
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 4.4 provides an illustration of the concept of shared virtual memory (often referred to as VM).
48 |
Chapter 4: SOME TYPICAL PROGRAMS |