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

Example 3.5: Indentation Style for Dictionaries and Structure

/mydict 24 dict def mydict begin

/draft % - draft - { %def

/saveobj save def

500 600 moveto 10 rotate /Helvetica 24 selectfont 0.5 setgray

gsave

(DRAFT) show grestore

0 setgray 1 setlinewidth (DRAFT) false charpath stroke

saveobj restore } bind def

end %mydict

SETTING UP TEMPLATES

A very strong technique for building a structured PostScript program is to create an empty template for each construct in your program and then to go back and fill in the body of the template. This keeps you from having to remember to close all your delimiters and lets you perform your housekeeping all at once, so you can concentrate on other things. Anything that inherently has a beginning and an end will benefit from this technique, including making entries in a dictionary, creating an array, a procedure, a string, a loop, or a conditional.

The advantage to the template approach for ifelse statements, procedure definitions, loops, or other structural components is that you never have a partially written program with mismatched braces, which can be difficult to debug. The procedure body will already “work” when you finish with the template, although it may not do anything very interesting. At least you won’t have a break in the structure of your program, even for the time it takes you to finish writing the procedure. Even if you are interrupted in the middle of building your true clause, the braces will still be balanced. It is also highly readable this way.

Example 3.6 shows how to lay out a template for a procedure definition, a dictionary, an array, an ifelse statement, and a loop. Once the template is

24

Chapter 3: FOUNDATIONS

in place, you can then come back to fill it in. The blank lines in the templates are where you add code later.

Example 3.6: Setting Up Templates

% dictionary template: /mydict 24 dict def mydict begin

end %mydict

% procedure template:

/newline % - newline - { %def

%this procedure will simulate “newline”

%on a line printer

}bind def

% conditional template: currentgray 0.5 gt { %ifelse

}{ %else

} ifelse

% loop template: 0 10 360 { %for

} for

% combination template: /mydict 24 dict def mydict begin

/newline % - newline - { %def

currentpoint pop 550 gt { %ifelse }{ %else

0 10 360 { %for } for

}ifelse

}bind def

end

Chapter 3: FOUNDATIONS

25