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

Example 8.1: Simple Loop to Draw an Octagon

4 setlinewidth

300 200 moveto

7 { %repeat

0 100 rlineto

45rotate

}bind repeat closepath stroke

Figure 8.1: Output of Example 8.1

output page

starting point 300,200

USING THE LOOP INDEX

Looping operators are useful because they let a task be performed more than once. They can be even more useful when you keep track of the loop index and use it effectively.

Chapter 8: USING LOOPING CONSTRUCTS

95

Example 8.2: Ignoring Loop Index

gsave

250 250 translate

0 10 360 { %for

pop % do not use loop index this time 0 -50 moveto 200 0 lineto 0 50 lineto

10 rotate

} for stroke

grestore

Example 8.2 sets up the loop index to give 10-degree increments around a circle, but actually does not use the loop index explicitly within the loop. The results are shown in Figure 8.2.

Figure 8.2:Output of Example 8.2

output page

In Example 8.3 is a short program that uses the loop index to set the line weight of a series of stroked lines and to control their spacing; its results are shown in Figure 8.3.

96

Chapter 8: USING LOOPING CONSTRUCTS

% must stroke to use line weight

Example 8.3: Using Loop Index for Line Weight

00 100 translate

2 3 div setgray 0 0 612 250 rectfill

0 setgray

2 2 20 { %for

dup setlinewidth % use loop index as line weight 0 exch 12 mul moveto% and use for line spacing 612 0 rlineto

stroke

} for

Figure 8.3: Output of Example 8.3

output page

When using a loop index, make sure you either pop the unwanted data off the stack or use it within the loop body, or you will leave unwanted data on the operand stack which may affect other portions of the program.

Several looping operators push data onto the stack each time around the loop. In particular, the forall operator pushes each element of an array or string onto the stack; it pushes both the key and value for each dictionary entry onto the stack. Table 8.2 provides a summary of each looping operator and the data that are pushed onto the stack for each iteration of the loop.

Chapter 8: USING LOOPING CONSTRUCTS

97

TIP

Don’t forget that the for, forall, kshow, and filenameforall looping oper-

 

ators push values onto the operand stack that must either be used or

 

popped from the stack.

 

 

 

Table 8.2: Arguments Supplied by Looping Operators

 

Operator

Data Pushed onto Operand Stack for Each Iteration

 

for

loop index (integer)

 

forall

for strings: integer ASCII code for each byte of string

 

 

for arrays: each element of the array, of arbitrary type

 

 

for dictionaries: both the key and the value for each entry

 

kshow

character code for character just printed and for the

 

 

character about to be printed

 

loop

none

 

repeat

none

 

filenameforall

a string containing each file name that matches the pattern

Example 8.4 shows both a forall loop and a filenameforall loop being used to find out all the font names defined in FontDirectory and on the disk of a printer. Notice the way that the data are used inside the loop bodies.

Example 8.4: Finding Font Names with Looping Operators

/scratch 128 string def FontDirectory { %forall

pop % get rid of “value” part of dictionary entry scratch cvs print (\n) print

} bind forall

(fonts/*) { %filenameforall

dup length 6 sub 6 exch getinterval print (\n) print } scratch filenameforall

As you can see, there are a lot of ways to use the looping operators effectively, especially if you take advantage of the operators that push data onto the operand stack during execution of the loop body.

98

Chapter 8: USING LOOPING CONSTRUCTS