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

Figure 4.2: Output from Example 4.2

output page

PostScript error (invalidfo

PROGRAMS THAT READ DATA

One of the most common operations in a computer program is to read and write data from a file. In the past, most PostScript programs have not had access to a file system anywhere other than on the printer, if the printer happened to have a hard disk attached. In window system environments based on PostScript, it is much more likely that a file system is available.

Example 4.3: Sample Program that Reads Data

%!PS-Adobe-2.0

%%Title: data reading example /infile (input.ps) (r) file def /outfile (output.ps) (w) file def /buffer 128 string def

{ %loop

infile buffer readstring { %ifelse outfile exch writestring

}{ outfile exch writestring exit } ifelse

}bind loop

infile closefile outfile closefile

42

Chapter 4: SOME TYPICAL PROGRAMS

The program in Example 4.3 reads lines from one text file and writes them to another output file; in other words, it just copies the file.

QUERY PROGRAMS

Sometimes you need to retrieve some information about a printer or network server, like a list of the currently installed fonts. This is an example of a query program. Query programs always generate a response of some kind as part of their execution. Example 4.4 is a query program that returns a list of the fonts currently defined in the FontDirectory dictionary in a PostScript interpreter.

Example 4.4: Sample Query Program

%!PS-Adobe-2.0 Query %%EndComments %?BeginFontListQuery FontDirectory { pop == } forall flush

(*) = flush %?EndFontListQuery: *

Since query programs are intended to write results to the standard output communications channel (terminology borrowed from UNIX and C), their behavior depends to a great degree upon the environment in which they are executed. Some printers do not have two-way communications channels; in such cases the results will be lost. Some operating environments think that anything coming back from a printer must be an error message, so the results may be written to an error log file somewhere, and they may even have extra text added to them by the printer control program.

ENCAPSULATED POSTSCRIPT PROGRAMS

Encapsulated PostScript files (EPS files) are illustrations or other selfcontained programs that can be included into another document. They are restricted in a few ways to keep them from disturbing the environment in which they are included, but generally can use the entire expressive power of the PostScript language (see Example 4.5).

Chapter 4: SOME TYPICAL PROGRAMS

43

Example 4.5: Sample Encapsulated PostScript Program

/EPSsave save def

.25 .25 scale .7 .7 scale 200 200 translate 20 rotate /showpage { } def

% begin included EPS file %!PS-Adobe-2.0 EPSF-1.2 %%Title: EPSF example program %%BoundingBox: 100 100 500 500

%%DocumentFonts: Palatino-BoldItalic %%EndComments

%%BeginProcSet: adobe_DPSrectanglepack 1.0 /rectfill where { pop }{ %ifelse

/*buildrect {

dup type /integertype eq 1 index type /realtype eq or { %ifelse 4 -2 roll moveto dup 0 exch rlineto

exch 0 rlineto neg 0 exch rlineto closepath

}{ %else

dup type /arraytype eq {

aload length 4 div cvi { %repeat *buildrect

} repeat

} if

}ifelse

}bind def /rectfill { %def

gsave *buildrect fill grestore

}bind def

/rectstroke { %def

gsave *buildrect stroke grestore

}bind def /rectclip { %def

newpath *buildrect clip newpath

}bind def

} ifelse

%%EndProcSet: adobe_DPSrectanglepack 1.0 %%EndProlog

gsave

.75 setgray 100 100 500 500 rectfill

0 setgray 3 setlinewidth 100 100 500 500 rectstroke grestore

120 400 moveto /Palatino-BoldItalic findfont 75 scalefont setfont gsave (Encapsulated) show grestore 0 -100 rmoveto (PostScript) show

% end included EPS file EPSsave restore

44

Chapter 4: SOME TYPICAL PROGRAMS

In Figure 4.3 you can see the output of Example 4.5; it has been scaled smaller, translated to another position on the page, and rotated 20 degrees. All of these changes reflect the way EPS files are typically used, and are designed to reinforce the fact that the code in an Encapsulated PostScript file should not disturb the environment into which it is placed.

Figure 4.3: Output from Example 4.5

output page

Encapsulated

PostScript

The specification for Encapsulated PostScript files allows for an optional preview component of the file, to accommodate weak display systems that cannot execute the PostScript code directly. This preview component is usually a bit map in a format appropriate to the native environment on which the document was created (it may be a Macintosh PICT resource, for example). The preview component of an Encapsulated PostScript file is strictly optional and is in no way connected to the PostScript code, except that it is supposed to represent the same picture.

Chapter 4: SOME TYPICAL PROGRAMS

45