Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать

Templates aren’t just a learning tool, they are sound practice. The very best programmers use this technique every day, whether they are writing a simple example program for a book on PostScript or developing a quick hack to turn off the start page on the printer. Using templates will save you enormous amounts of time in the long run, and it is a highly recommended technique. If you learn nothing else from this book, please develop a habit of laying out templates as you program. Close those curly braces right after you open them, then go back and fill them in. Or, if you hate to use templates, at least develop your own techniques for laying out and maintaining the structure and indentation of the program source.

DECLARING AND USING VARIABLES

Variables serve two distinct purposes in a computer program. They provide a place to put data, especially data that might change, and they make the program much more readable than if you filled it up with numbers or string data. In PostScript, it is especially important to consider for which of these two purposes your variables were designed. In an interpreted language, you can pay a fairly high premium in execution time for only a little more readability.

There really is no such thing as a variable in PostScript, at least not in the sense that other languages have them. In C, for example, if you declare a variable of type int, the compiler will actually set aside a portion of memory big enough to hold an integer, and whenever you refer to that variable, the program knows to look in that location in memory. PostScript has no mechanism for declaring variables before they are used. It also does not require you to give names to any data structures. You can use data directly from the operand stack without ever storing the data into a variable, which you typically cannot do in other languages.

A variable in PostScript is just a name in a dictionary; what you really mean by “variable” probably is just a named bit of data. You refer to it by name instead of explicitly referring to the number or its location in memory. In that sense, any object can be a variable in a PostScript program, if you choose to define it in a dictionary. Let’s look at a simple example (see Example 3.7).

26

Chapter 3: FOUNDATIONS

Example 3.7: Declaring Variables

/LeftMargin 72 def /TopMargin 792 72 sub def

/DocumentName (Thinking in PostScript) def /ScaleFactor 300 72 div def

/DefaultFont /Courier findfont 12 scalefont def

In order to reference a variable, you simply use its name (without the slash). Whenever you use the name, the value will be looked up and executed. As long as the variable you have defined is not a procedure, the value of the variable will placed on the operand stack. In Example 3.8 you can see all the variables from Example 3.7 being used in some typical situations.

Example 3.8: Using Variables

ScaleFactor dup scale

LeftMargin TopMargin moveto

DefaultFont setfont

DocumentName show

Arithmetic with Numeric Variables

In most programming languages, you can easily perform arithmetic with variables, as in Example 3.9. But in the PostScript language, you have to do all your arithmetic on the operand stack, perhaps calling up the values of some variables to do so, then use the def operator to put the new value back into your variable. There is no operator that performs assignment like the = notation does in C. Example 3.10 shows the same arithmetic problems from Example 3.9 carried out in PostScript. Assume, of course, that in both examples all of the variables have been set up to contain reasonable values.

Example 3.9: Arithmetic in C

grade = (test1 + test2 + test3 + final ) / 4; counter = counter + 1;

Chapter 3: FOUNDATIONS

27

Example 3.10: Arithmetic in PostScript

/grade test1 test2 add test3 add final add 4 div def /counter counter 1 add def

Remember, since all PostScript operators require their operands to be on the operand stack, the use of variables is never required. The same results can always be obtained using just the operand stack to hold your data. You can choose when you use a variable and when you don’t.

Using the // Notation for Constants

PostScript has a special syntax for immediate name lookup and evaluation: the double slash. Any name that is preceded by a double slash ( // ) is looked up immediately when encountered in the program, even if it is inside a procedure body, where names are not normally looked up until the procedure is executed.

One excellent use of this notation is to set up constants that you want to work like variables but will never change. That way you can obtain the readability of having named variables, but when the program executes, they will actually be replaced by their constant value and the name lookup at run-time will be avoided. For a simple illustration of this technique, see Example 3.11.

Example 3.11: Evaluation of Constants with //

/LeftMargin 108 def % constant definitions /TopMargin 792 72 sub def

/RightMargin 612 72 sub def

/BottomMargin 72 def

 

/Leading 12 def

 

/space

% - space -

{ %def

 

currentpoint exch //RightMargin gt { %ifelse //Leading sub dup //BottomMargin lt { %ifelse

pop showpage

//LeftMargin //TopMargin moveto }{ %else

//LeftMargin exch moveto

} ifelse }{ pop } ifelse

} bind def

28

Chapter 3: FOUNDATIONS