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

Chapter 4

Some Typical Programs

This chapter presents some of the most common variations in PostScript programs. It provides you with a glimpse of different programming styles and lets you know when they might be appropriate. Not all PostScript programs draw pictures or print pages. Some of them are purely utilitarian, some are exploratory, some may delete files from your desk, download a font, or query a printer for a list of built-in typefaces.

A lot has been written about PostScript page descriptions, since that was the original purpose of the language. Many of the recommended techniques and program structural rules were intended mostly for page descriptions. Part of the purpose of showing some other kinds of programs is to address squarely the issue of creating PostScript programs that are not simply documents.

The samples in this chapter are not necessarily intended for you to copy as great programming examples. The idea is to present several different classes of programs, to point out that the programming style and the

37

problems faced may differ dramatically between, say, a font program and an Encapsulated PostScript program (which is explained later in this chapter).

A TYPICAL PAGE DESCRIPTION PROGRAM

Although PostScript is a full-featured programming language, its primary purpose was originally for page description. In this sense, the program is only the means to an end. The point of a page description is purely graphical, and the marks on the page or on the screen are the intent; the program’s role is to facilitate the rendering of graphics.

Let’s look at a very simplistic page description from a programming point of view. Typically page descriptions are produced by a document processing application, and are not generally written by hand. Machinegenerated PostScript programs always have some amount of hand-tuned code, especially in the procedure definitions, but the body of the document is usually generated directly by the document production system.

For instance, if you wrote a drawing program that allows the user to draw boxes, you might create a PostScript procedure to help you draw boxes efficiently, but this procedure would be very general, and would require information about where to draw the box and how big it should be. This data would be supplied by the drawing program itself, based on the boxes drawn by the user. The box procedure would then be invoked as many times as necessary to render all the boxes drawn by the user.

Since documents can be composed of many, many elements, and since the set of possible graphic elements is normally fixed, it makes sense to make the representation for each individual object as compact and efficient as possible, using the ability to define procedures as a mechanism. Example 4.1 shows the output of a fictitious text-setting application, with a typical blend of procedure definitions and invocations in the body of the document; Figure 4.1 shows the text generated by the code.

38

Chapter 4: SOME TYPICAL PROGRAMS

Example 4.1: Sample Page Description Program

%!PS-Adobe-2.1

%%Title: sample document %%Creator: textset v-1.0

%%DocumentFonts: Times-Roman Times-Italic %%BoundingBox: 0 0 612 792

%%Pages: 2 %%EndComments %%BeginProcSet: textset 1.0 0

/selectfont where { %ifelse pop

}{ %else

/selectfont { %def

exch findfont exch scalefont setfont } bind def

/F /selectfont load def

/T { moveto show } bind def %%EndProcSet

%%EndProlog %%Page: 1 1

/Times-Roman 24 F

(Now is the time for all good) 72 72 T /Times-Italic 24 F

(people) 349 72 T

/Times-Roman 24 F (to come) 416 72 T

(to the aid of their country.) 72 46 T showpage

%%Page: 2 2 /Times-Roman 24 F

(Now is the time for all good) 72 72 T /Times-Italic 24 F

(people) 349 72 T

/Times-Roman 24 F (to come) 416 72 T

(to the aid of their country.) 72 46 T showpage

%%Trailer

Chapter 4: SOME TYPICAL PROGRAMS

39

Figure 4.1: Output from Example 4.1

output page

Now is the time for all good people to come to the aid of their country.

There are several things that affect the programming style in page descriptions. First, there is usually a great deal of structure in the program, and in fact the whole program can be thought of as a document containing pages, illustrations, and so on. In Example 4.1, the structure is delineated according to document structuring conventions, where comments like %%EndProlog mark the boundaries between the elements of the document. In this case, there are really only two major components, known as the prologue and the script. The prologue is simply a few PostScript language definitions that permit more powerful and compact representations to be made in the document. The script is the body of the document, and contains mostly data from the document processing system such as fonts, locations on the page, and bits of text. The prologue is usually the same for all documents, and is hand-written, whereas the script is generated by the document processing program.

FONT PROGRAMS

A font program is simply a PostScript program that creates a font dictionary and registers it as a font. Since it is a program, there is no required font file format to adhere to. The dictionary structure of the font

40

Chapter 4: SOME TYPICAL PROGRAMS

is important, but it can be built in a variety of ways. (See the PostScript Language Reference Manual for a complete specification of the required font dictionary structure.) Example 4.2 is a very minimal program that illustrates the structure of a font program. The characters in this font draw completely random line segments (see Figure 4.2); its practical merit is questionable.

Example 4.2: Sample Font Program

%%BeginFont: UserFont 14 dict begin

/FontName /UserFont def /FontType 3 def

/FontMatrix [ .001 0 0 .001 0 0 ] def /FontBoundingBox [ 0 0 1000 1000 ] def /Encoding /StandardEncoding load def

/BuildChar % fontdict charcode BuildChar - { %def

exch begin

Encoding exch get 500 0 setcharwidth

CharDefs exch get exec

end } bind def

/CharDefs 256 dict def CharDefs begin

Encoding { %forall { %def

4 { rand 1000 mod } repeat moveto lineto stroke

}def

}bind forall

end

currentdict end dup /FontName get exch definefont pop %%EndFont

/UserFont 48 selectfont

10 10 moveto (abcdefg) show showpage

Chapter 4: SOME TYPICAL PROGRAMS

41