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

/Box

% X Y Width Height Box -

{ %def

 

/Height exch def /Width exch def /Y exch def

/X exch def X Y moveto

Width 0 rlineto 0 Height rlineto

Width neg 0 rlineto closepath stroke

} bind def

end %ProductDict

%%BeginSetup

% make sure “ProductDict” is available at run time: ProductDict begin

%%EndSetup

%%Trailer

% make sure to remove “ProductDict” when done: end %ProductDict

%%EOF

MAINTAINING THE DICTIONARY STACK

The dictionary stack in a PostScript interpreter is not as readily manipulated as the operand stack. There are no equivalent operators for the dictionary stack operators such as exch, roll, and dup. The only way to rearrange dictionaries is to place them temporarily on the operand stack, rearrange them there, then replace them on the dictionary stack.

For the most part, it should not be necessary to rearrange the dictionaries on the dictionary stack, but it is occasionally important to make sure your dictionary is the topmost one. In these circumstances, it might be best to simply use begin to explicitly place your dictionary on the top, even if it might already be present further down on the dictionary stack.

Example 10.5 presents a procedure to implement dictexch, the equivalent of the exch operator for the dictionary stack. This example is intended to give you the flavor of the necessary dictionary manipulation; to see how it works, see Example 10.5. The procedure shown is not necessarily a recommended approach for using the dictionary stack.

124

Chapter 10: USING DICTIONARIES

Example 10.5 Exchanging Topmost Dictionaries with dictexch

/dictexch

% - dictexch -

{ %def

 

currentdict end

% dictionary A moved to operand stack

currentdict end

% dictionary B moved

exch

% exchange A and B on operand stack

begin

% dictionary A back on dictionary stack

begin

% dictionary B is now on top

} bind def

 

Figure 10.1: Mechanics of the dictexch Procedure

A

currentdict end

 

B

B

 

 

A

currentdict end

B

A

begin

 

 

exch

 

 

A

B

begin

One of the most important aspects of maintaining the dictionary stack is to make sure you remove any dictionaries that you may have put onto the dictionary stack. The mechanism for doing this is the end operator, which pops the topmost dictionary off the dictionary stack. Make sure you have thought through any potential conflicts with other programs, embedded illustrations, or other code that might add or subtract dictionaries from the dictionary stack. It is usually best to remove your local dictionaries from the stack before transferring control to an embedded program, then reinstating them when you get control back.

The PostScript dictionary stack operations are not as powerful or flexible as those for the operand stack. The dictionary stack is intended for lighterduty manipulation, and to provide a strong mechanism for name lookup and storage. Keep this in the back of your mind if you are thinking of writing a dictroll, dictdup, or other procedure just for this purpose.

Chapter 10: USING DICTIONARIES

125

INTO AND OUT OF DICTIONARIES

The dictionary stack holds dictionary objects. There are built-in operators in the PostScript language for putting things into or retrieving things from the current dictionary. There are also several other ways to manipulate the contents of dictionaries directly.

A dictionary is a normal PostScript object and can exist on the operand stack as well as on the dictionary stack. The put operator lets you put an entry directly into a dictionary that is on the operand stack, and get lets you retrieve from it. (See Example 10.6.)

Example 10.6: Using put and get for Dictionary Entries

/MyFont

% leave on operand stack

15 dict

% leave on operand stack

dup /FontName /MyFont put

dup /Encoding StandardEncoding put dup /BuildChar { whatever } put

dup /CharStrings 100 dict put definefont pop

% This is not the recommended way to call an operator: statusdict /ledgertray get exec

systemdict /showpage get exec

If your needs are simple, you may be able to avoid having to place a local dictionary on the operand stack at all. You can use the get and put operators to explicitly reference a dictionary without ever placing it on the dictionary stack. In Example 10.7, the save object that results from executing save is stored away into a dictionary, and is retrieved later for restore. Since there is only a single entry to be written into the dictionary, it is a little bit simpler (although perhaps no clearer) to use put rather than begin, def, and end.

Example 10.7: Scoping with put and get

userdict /mysaveobj save put

% embedded illustration here userdict /mysaveobj get restore

126

Chapter 10: USING DICTIONARIES