- •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
Figure 4.2: Output from Example 4.2
output page
PostScript error (invalidfo
PROGRAMS THAT READ DATA
One of the most common operations in a computer program is to read and write data from a file. In the past, most PostScript programs have not had access to a file system anywhere other than on the printer, if the printer happened to have a hard disk attached. In window system environments based on PostScript, it is much more likely that a file system is available.
Example 4.3: Sample Program that Reads Data
%!PS-Adobe-2.0
%%Title: data reading example /infile (input.ps) (r) file def /outfile (output.ps) (w) file def /buffer 128 string def
{ %loop
infile buffer readstring { %ifelse outfile exch writestring
}{ outfile exch writestring exit } ifelse
}bind loop
infile closefile outfile closefile
42 |
Chapter 4: SOME TYPICAL PROGRAMS |
The program in Example 4.3 reads lines from one text file and writes them to another output file; in other words, it just copies the file.
QUERY PROGRAMS
Sometimes you need to retrieve some information about a printer or network server, like a list of the currently installed fonts. This is an example of a query program. Query programs always generate a response of some kind as part of their execution. Example 4.4 is a query program that returns a list of the fonts currently defined in the FontDirectory dictionary in a PostScript interpreter.
Example 4.4: Sample Query Program
%!PS-Adobe-2.0 Query %%EndComments %?BeginFontListQuery FontDirectory { pop == } forall flush
(*) = flush %?EndFontListQuery: *
Since query programs are intended to write results to the standard output communications channel (terminology borrowed from UNIX and C), their behavior depends to a great degree upon the environment in which they are executed. Some printers do not have two-way communications channels; in such cases the results will be lost. Some operating environments think that anything coming back from a printer must be an error message, so the results may be written to an error log file somewhere, and they may even have extra text added to them by the printer control program.
ENCAPSULATED POSTSCRIPT PROGRAMS
Encapsulated PostScript files (EPS files) are illustrations or other selfcontained programs that can be included into another document. They are restricted in a few ways to keep them from disturbing the environment in which they are included, but generally can use the entire expressive power of the PostScript language (see Example 4.5).
Chapter 4: SOME TYPICAL PROGRAMS |
43 |
Example 4.5: Sample Encapsulated PostScript Program
/EPSsave save def
.25 .25 scale .7 .7 scale 200 200 translate 20 rotate /showpage { } def
% begin included EPS file %!PS-Adobe-2.0 EPSF-1.2 %%Title: EPSF example program %%BoundingBox: 100 100 500 500
%%DocumentFonts: Palatino-BoldItalic %%EndComments
%%BeginProcSet: adobe_DPSrectanglepack 1.0 /rectfill where { pop }{ %ifelse
/*buildrect {
dup type /integertype eq 1 index type /realtype eq or { %ifelse 4 -2 roll moveto dup 0 exch rlineto
exch 0 rlineto neg 0 exch rlineto closepath
}{ %else
dup type /arraytype eq {
aload length 4 div cvi { %repeat *buildrect
} repeat
} if
}ifelse
}bind def /rectfill { %def
gsave *buildrect fill grestore
}bind def
/rectstroke { %def
gsave *buildrect stroke grestore
}bind def /rectclip { %def
newpath *buildrect clip newpath
}bind def
} ifelse
%%EndProcSet: adobe_DPSrectanglepack 1.0 %%EndProlog
gsave
.75 setgray 100 100 500 500 rectfill
0 setgray 3 setlinewidth 100 100 500 500 rectstroke grestore
120 400 moveto /Palatino-BoldItalic findfont 75 scalefont setfont gsave (Encapsulated) show grestore 0 -100 rmoveto (PostScript) show
% end included EPS file EPSsave restore
44 |
Chapter 4: SOME TYPICAL PROGRAMS |
In Figure 4.3 you can see the output of Example 4.5; it has been scaled smaller, translated to another position on the page, and rotated 20 degrees. All of these changes reflect the way EPS files are typically used, and are designed to reinforce the fact that the code in an Encapsulated PostScript file should not disturb the environment into which it is placed.
Figure 4.3: Output from Example 4.5
output page
Encapsulated
PostScript
The specification for Encapsulated PostScript files allows for an optional preview component of the file, to accommodate weak display systems that cannot execute the PostScript code directly. This preview component is usually a bit map in a format appropriate to the native environment on which the document was created (it may be a Macintosh PICT resource, for example). The preview component of an Encapsulated PostScript file is strictly optional and is in no way connected to the PostScript code, except that it is supposed to represent the same picture.
Chapter 4: SOME TYPICAL PROGRAMS |
45 |