- •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
making the basic language easy to learn, but offering a very rich and sometimes intimidating array of possibilities.
Data are not shared in the same way in the UNIX shell as they are in the PostScript language, since the shell provides a pipeline mechanism for communicating between processes, whereas the PostScript language has an operand stack where all operators look for their operands (and leave their results).
The comparison with the UNIX operating system is offered only to trick you into thinking about the language as a set of powerful operators set in the framework of a simple interpreted programming language. If you are an experienced UNIX programmer, you probably have developed a feel for the kinds of problems that are best solved with shell scripts versus those that require the writing of full-fledged C programs. If your PostScript programming task is in an environment where you have the opportunity to balance the work load between a C program and a PostScript program (in a client / server environment for example), this analogy may give you a feel for the kinds of tasks that might be more appropriate for each side of the equation.
INPUT, OUTPUT, AND THROUGHPUT
A program is usually only as good as the data you put through it. Most programs do not come with data already in them, you have to provide the data, and wait for the program to provide output based on the input. This is also true with PostScript, although the input and output may depend on the environment in which the program operates. For example, a PostScript program is often just a vehicle for printing or displaying information. In this sense, the data are provided en masse from a document, and the output of the program is simply the display or printing of the data in the document.
The input and output of a program depend greatly upon the environment in which the program is running. If your PostScript program is running on an interpreter built into your laser printer, there are few choices for input and output. The input can either come from the cable leading into the laser printer (which is also where the program comes from, typically) or it can come from an internal disk system built into the printer. The choices of output are the same, although the output can also be the printed page itself
14 |
Chapter 2: POSTSCRIPT IS NOT LIKE C |
(which is normally the whole point of PostScript programming). In this case, the throughput of the program can be measured by how fast the pages print when you send it your program to draw pictures. And it is very much up to you and your program to control how fast those pages print.
The vast majority of all computer programs in existence today simply read data from a file, let you edit the data in some way, and then write it back to a file again. These are thought of as editors, word processors, spread sheets, drawing programs, statistical packages, and so forth. The files they read and write are documents. For the most part, documents are not interchangeable between systems or programs, so you just read into and write out from your single program, and hope to glean something useful from the data itself when the document is visible in the program.
PostScript programs rarely operate directly on files and usually are not constructed just to rearrange bodies of data. PostScript is a language designed for expression of visual images, communication of data to remote systems, and one-way data manipulation. Throughput is everything. PostScript programs are perhaps more analogous to utility programs or device drivers, where the task is not to perform a specific application task, but to provide a general mechanism for accomplishing data transfer, imaging, or device control. The input may well be arbitrary (as opposed to a carefully constructed data file), and the output may be simply a sheet of paper with some marks on the surface.
CONCLUDING THOUGHTS
This chapter positions PostScript alongside popular conventional languages such as C, to point out the similarities and differences in a slightly abstract sense. The ideas presented may raise more questions than they provide answers, but they should arm you with a sense of perspective on the PostScript language that will help you approach challenging programming tasks with some imagination, confidence, and with open eyes. The comparison with C, in particular, might help by convincing you to revisit some old habits of programming as you implement the guts of your program. Writing loops, making simple ifelse statements, and inventing variables should be done with a full appreciation of the language and understanding of its strengths and weaknesses.
Chapter 2: POSTSCRIPT IS NOT LIKE C |
15 |
The next chapters get increasingly specific about the PostScript language and help you to build a solid foundation of programming skills.
EXERCISES
1. Rewrite the following C program segment in PostScript:
factorial = 1;
for ( index = 10; index > 0; index-- )
{
factorial = factorial * index;
}
printf ( “10 factorial is %d\n”, factorial );
2.What would you say are PostScript’s greatest strengths as a generalpurpose programming language? What are its greatest weakness?
3.Name four basic language constructs that are available in both the C and PostScript languages (or, if you don’t know C, pick any language of your choice).
4.Name three things provided in the PostScript language that are not provided as a standard part of any other language.
16 |
Chapter 2: POSTSCRIPT IS NOT LIKE C |