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

The Dictionary Stack

The dictionary stack controls name lookup and dictates where an entry is made when you use the def operator. You can only place dictionary objects onto this stack, and only with the begin operator. You must use the end operator to remove entries from the dictionary stack. There is also a discussion of these operations in the section entitled Maintaining the Dictionary Stack in Chapter 10.

The Execution Stack

The execution stack is used only internally by the interpreter, to keep track of your program’s execution. It contains any portion of the program that has not yet been executed, including the input file stream, any partially-executed procedure bodies, and loops. There are very few ways to take advantage of the execution stack in a user program, but it can help in complicated debugging situations.

The Graphics State Stack

The graphics state stack is used only by the gsave and grestore operators to maintain the graphics state. The gsave operator effectively pushes the current graphics state onto the graphics state stack, making it available later for grestore. The stack-based nature of the graphics state stack makes gsave and grestore nestable up to the limits of the graphics state stack, which is typically about 20 levels or more.

In the Display PostScript extensions to the PostScript language, the notion of a graphics state object has been introduced, which allows you to capture the current graphics state into an object that can be treated like any other object. There are companion operators like setgstate that will take a graphics state object and set all the current graphics state parameters according to its contents. This is much more flexible than the graphics state stack, since instantaneous changes can be made to the graphics state without having to push and pop the current state.

CONCLUDING THOUGHTS

This chapter provided you with a brief overview of data types and some examples of how operators use the operand stack. The stack and its

Chapter 5: UNDERSTANDING THE STACK

63

various uses have been presented in some depth, yet there is a lot to learn about the stack, and you may not yet feel you fully understand it. The next chapter sets forth still more wisdom about the operand stack, to help you learn to trust and rely upon this important resource.

EXERCISES

1. What is left on the operand stack after the following code is executed?

/Times-Roman 24 selectfont 100 dup moveto

(Vocabulary) stringwidth 2 div neg rmoveto (Vocabulary) show

2.For each of the following code segments, determine what error, if any, would arise (not all of the code segments raise errors):

a.(string one) (string2) copy

b.12 /Times-Roman findfont scalefont setfont

c.% center some text

306 500 moveto /Times-Bold 14 selectfont (Report Title) dup stringwidth 2 div 0 rmoveto show

d.save

/Optima 12 selectfont 20 40 moveto (Save me!) dup show

restore

3.Rewrite the following code using just stack operators, without storing anything into a dictionary.

64

Chapter 5: UNDERSTANDING THE STACK

36 750 moveto /Times-Roman 24 selectfont

% works like “show”, leaving current point at proper location /ushow % linethickness lineposition (words) ushow - { %def

LOCAL begin /text exch def

/linepos exch def /linethick exch def gsave

0 linepos rmoveto text stringwidth rlineto

linethick setlinewidth stroke grestore

text show

end

} dup 0 4 dict put def

0.5 -4 (test underlined text) ushow

Chapter 5: UNDERSTANDING THE STACK

65

66

Chapter 5: UNDERSTANDING THE STACK