Initial release of Maemo 5 port of gnuplot
[gnuplot] / docs / psdoc / ps_file.doc
1 A short guide to PostScript files created by gnuplot's "postscript" terminal
2
3 by Dick Crawford, aka rccrawford@lanl.gov
4
5 Before we begin, let me say a few words about PostScript.  It is highly ver-
6 satile, but with versatility comes complexity.  Syntax is extremely important;
7 a misplaced command can do unexpected things.  Thus the adventurous soul who
8 wishes to alter the PostScript but has not been initiated into the arcana of
9 PostScript would be well-advised to change only those items (like numerical
10 values, font names or heights, or the letters defining symbols) that are
11 fairly obvious as to their meaning.
12
13 PostScript is stack-oriented and works in Reverse Polish notation:
14 'a b add' takes 'a' and 'b' off of the stack and replaces them with 'a+b'.
15
16 The 'stroke' command actually draws lines on the page.  It uses whatever line
17 width and style are currently defined.  Thus if we specify a bunch of commands
18 like 'LT1 1 2 moveto 3 4 lineto LT3 stroke' [LT1 and LT3 are line type defi-
19 nitions, 'x y moveto' moves the (virtual) pen to (x,y) and 'x y lineto' draws
20 a line from wherever the pen was to (x,y)], the line connecting points (1,2)
21 and (3,4) will be in type LT3.
22
23 PostScript is case-sensitive.
24
25 The sample file below has been pruned of repeated commands, just to keep its
26 length down.  Comment lines begin with '%'.  Lines beginning with '%%' are
27 inserted for "encapsulated PostScript" -- these lines are read by applications
28 that import PostScript.  Lines beginning with '%#' have been added to the
29 sample by me for explanatory purposes.
30
31 I have merged files created by the postscript terminal with no options, with
32 the "eps" option and with the "enhanced" option.  The differences will be
33 clearly pointed out when appropriate.
34
35 Here we go.
36
37 %!PS-Adobe-2.0
38 %%Title: psgenh
39 %%Creator: gnuplot 3.5 (pre 3.6) patchlevel beta 338
40 %%CreationDate: Fri Jan 16 13:18:18 1998
41 %%DocumentFonts: (atend)
42 %%BoundingBox: 50 50 554 770
43 %%Orientation: Landscape
44 %# In 'eps' mode, the preceding two lines would look like:
45 %# %%BoundingBox: 50 50 410 302
46 %# %%Orientation: Portrait
47 %%Pages: (atend)
48 %%EndComments
49 %# 
50 %#  The 'dictionary' contains stuff defined by the user.
51 %# 
52 /gnudict 120 dict def
53 gnudict begin
54 %# 
55 %#  The following switch toggles between color and monochromatic.
56 %# 
57 /Color false def
58 %# 
59 %#  The following switch toggles between solid and dot-dash lines.
60 %# 
61 /Solid false def
62 %# 
63 %#  The following parameter scales all linewidths in the plot.
64 %# 
65 /gnulinewidth 5.000 def
66 /userlinewidth gnulinewidth def
67 %# 
68 %#  The following parameter specifies the vertical displacement of the labels
69 %#  and titles (it is used in the 'show' commands defined below).  It should
70 %#  be about 1/3 of the font height.
71 %# 
72 /vshift -46 def
73 %# 
74 %#  The following parameter scales the lengths of all dot-dash patterns.
75 %# 
76 /dl {10 mul} def
77 %# 
78 %#  The following two parameters scale the horizontal and vertical sizes of
79 %#  the symbols used by 'plot with points'.
80 %# 
81 /hpt_ 31.5 def
82 /vpt_ 31.5 def
83 /hpt hpt_ def
84 /vpt vpt_ def
85 %# 
86 %#  The following four commands are aliases of the four 'pen movement'
87 %#  commands. Use of these aliases significantly shortens the file.  Note
88 %#  that the first two are 'absolute' movements and the latter two are
89 %#  'relative'.  M and R move to the specified position; L and V draw a
90 %#  straight line (from the current position) to it.
91 %# 
92 /M {moveto} bind def
93 /L {lineto} bind def
94 /R {rmoveto} bind def
95 /V {rlineto} bind def
96 %#
97 %#  These are a couple more parameters used in plotting symbols.  Why
98 %#  they are here instead of up with 'hpt' and 'vpt' I know not.
99 %#
100 /vpt2 vpt 2 mul def
101 /hpt2 hpt 2 mul def
102 %# 
103 %#  The 'show' command writes out a character string.  The following three
104 %#  varieties do so as left-, right-, and center-justified.  [Remember,
105 %#  we're still in the dictionary--the font doesn't need to be specified
106 %#  until we actually use one of these.]
107 %# 
108 /Lshow { currentpoint stroke M
109   0 vshift R show } def
110 /Rshow { currentpoint stroke M
111   dup stringwidth pop neg vshift R show } def
112 /Cshow { currentpoint stroke M
113   dup stringwidth pop -2 div vshift R show } def
114 %# 
115 %#  The following commands define the various line types (normal, bold,
116 %#  dashed, etc.) used by gnuplot.
117 %# 
118 %#  UP, DL, and UL are busywork commands used by others here
119 %#
120 /UP { dup vpt_ mul /vpt exch def hpt_ mul /hpt exch def
121   /hpt2 hpt 2 mul def /vpt2 vpt 2 mul def } def
122 /DL { Color {setrgbcolor Solid {pop []} if 0 setdash }
123  {pop pop pop Solid {pop []} if 0 setdash} ifelse } def
124 /BL { stroke gnulinewidth 2 mul setlinewidth } def      %# twice the linewidth
125 /AL { stroke gnulinewidth 2 div setlinewidth } def      %# half the linewidth
126 /UL { gnulinewidth mul /userlinewidth exch def } def
127 /PL { stroke userlinewidth setlinewidth } def           %# normal linewidth
128 /LTb { BL [] 0 0 0 DL } def
129 /LTa { AL [1 dl 2 dl] 0 setdash 0 0 0 setrgbcolor } def
130 /LT0 { PL [] 0 1 0 DL } def
131 /LT1 { PL [4 dl 2 dl] 0 0 1 DL } def
132 %# 
133 %#  ...and a bunch more.
134 %#  In the LT's, the first command ('PL' for LT1) sets the linewidth,
135 %#  the stuff in [...] defines the dot-dash pattern, and the three numbers
136 %#  define the rgb color.
137 %# 
138 %#  The following commands define the symbols used to plot data points.
139 %# 
140 /Pnt { stroke [] 0 setdash
141    gsave 1 setlinecap M 0 0 V stroke grestore } def
142 /Dia { stroke [] 0 setdash 2 copy vpt add M
143   hpt neg vpt neg V hpt vpt neg V
144   hpt vpt V hpt neg vpt V closepath stroke
145   Pnt } def
146 %# 
147 %#  ...and a bunch (dozens) more.
148 %# 
149 %#  The 'MF...' commands are used to handle the 'enhanced' syntax.  If the
150 %#  'enhanced' mode is not invoked, these commands won't appear in the file.
151 %# 
152 /MFshow {{dup dup 0 get findfont exch 1 get scalefont setfont
153      [ currentpoint ] exch dup 2 get 0 exch rmoveto dup dup 5 get exch 4 get
154      {show} {stringwidth pop 0 rmoveto}ifelse dup 3 get
155      {2 get neg 0 exch rmoveto pop} {pop aload pop moveto}ifelse} forall} bind def
156 /MFwidth {0 exch {dup 3 get{dup dup 0 get findfont exch 1 get scalefont setfont
157       5 get stringwidth pop add}
158     {pop} ifelse} forall} bind def
159 /MLshow { currentpoint stroke M
160   0 exch R MFshow } bind def
161 /MRshow { currentpoint stroke M
162   exch dup MFwidth neg 3 -1 roll R MFshow } def
163 /MCshow { currentpoint stroke M
164   exch dup MFwidth -2 div 3 -1 roll R MFshow } def
165 end
166 %# 
167 %#  The dictionary is now complete.  We activate it, save what went before
168 %#  (the 'gsave' command saves everything on a different stack) and begin.
169 %# 
170 %%EndProlog
171 %%Page: 1 1
172 gnudict begin
173 gsave
174 %# 
175 %#  First we position the plot on the page and scale it.
176 %# 
177 %#  The 'translate' command moves the origin to the specified position.
178 %#  [The PostScript default origin is near the lower left-hand corner.]
179 %#  The 'scale' command changes the units used in the plot.
180 %#  [The PostScript default unit is the point: 72 points equal one inch.] 
181 %#  The 'rotation' command rotates the coordinates clockwise through the
182 %#  specified angle (degrees).
183 %#  [The PostScript default orientation is profile.]
184 %# 
185 %#  This sample changes the orientation to landscape (the gnuplot default)
186 %#  and the unit to tenths of a point.  Note that if the first two commands
187 %#  were interchanged, the translation would be only five points in each
188 %#  direction, instead of fifty.
189 %#
190 %#  If this had been generated in 'eps' mode, the "90 rotate" and "0 -5040
191 %#  translate" commands would not appear and the units would be 0.050 instead
192 %#  of 0.100.
193 %# 
194 %#  If you want to change the size or the position of the plot, this is where
195 %#  to do it.
196 %# 
197 50 50 translate
198 0.100 0.100 scale
199 90 rotate
200 0 -5040 translate
201 0 setgray
202 newpath
203 %# 
204 %#  Define the default font.  The number is the height.  As usual, fonts
205 %#  used primarily for text have characters only about 70% the specified
206 %#  height, because spacing between lines is built-in.  The Symbol font and
207 %#  others that are normally not used for text produce characters that are
208 %#  more nearly the specified height.  Thus if you want to intersperse, for
209 %#  example, Greek and Roman letters (as you might when writing an equation),
210 %#  you'll need to play with the heights of the two fonts in order for them
211 %#  to look reasonably proportioned.
212 %# 
213 %#  The font defined here is used for the tick labels first, then the axis
214 %#  labels and plot titles, and finally the key.  If you want to change the
215 %#  font, simply insert the modified line after you are done with the old one
216 %#  (i.e. the last 'show' command to use it) but before the new one is needed.
217 %#  Note that if you change the font height, you'll also want to change the
218 %#  value of 'vshift' (it's near the top of the dictionary).  I'll give an
219 %#  example of this later on...
220 %# 
221 (Helvetica) findfont 140 scalefont setfont
222 LTb
223 %# 
224 %#  Label and draw the ticks along the y-axis.  I've given you three
225 %#  y-tics in different formats...
226 %# 
227 %#  The tick length is 63 units.
228 %#  
229 %#  This first y-tic is in 'normal' mode:
230 %#
231 728 560 M
232 63 0 V
233 6325 0 R
234 -63 0 V
235 644 560 M
236 (-10) Rshow
237 %#  
238 %#  This y-tic is in 'enhanced' mode:
239 %#
240 728 1645 M
241 63 0 V
242 6325 0 R
243 -63 0 V
244  stroke
245 644 1645 M
246 [ [(Helvetica) 140.0 0.0 true true (-5)]
247 ] -46.7 MRshow
248 %#
249 %#  This one is in 'normal' mode, but is rotated:
250 %#
251 434 2730 M
252 63 0 V
253 6619 0 R
254 -63 0 V
255 -6759 0 R
256 currentpoint gsave translate 90 rotate 0 0 M
257 (0) Cshow
258 grestore
259 %#
260 %#  Now the x-tics.  Here's one:
261 %#
262 728 560 M
263 0 63 V
264 0 4277 R
265 0 -63 V
266  stroke
267 728 420 M
268 [ [(Helvetica) 140.0 0.0 true true (-10)]
269 ] -46.7 MCshow
270 LTb
271 %# 
272 %#  Now draw the left and lower axes.  Were this drawn by 'splot'
273 %#  instead of 'plot', there might be some other axes drawn.
274 %# 
275 728 560 M
276 6388 0 V
277 0 4340 V
278 -6388 0 V
279 728 560 L
280  stroke
281 %#  Now come labels (both for the axes and those on "set label" commands)
282 %#  and other titles.  I just give you axis labels here, in 'enhanced' mode:
283 %#
284 140 2730 M
285 currentpoint gsave translate 90 rotate 0 0 moveto
286 [ [(Helvetica) 140.0 0.0 true true (ylabel)]
287 ] -46.7 MCshow
288 grestore
289 3922 210 M
290 [ [(Helvetica) 140.0 0.0 true true (xlabel)]
291 ] -46.7 MCshow
292 %#
293 %#  We're finally ready to plot functions and/or data.
294 %# 
295 %#  The key is drawn just before the applicable data.
296 %# 
297 %#  Choose a line type and write the key.
298 %#  In this sample, it is drawn with lines.
299 %# 
300 1.000 UL
301 LT0
302 6465 4767 M (x) Rshow       %# the function label in the key
303 6549 4767 M                 %# the sample line in the key
304 399 0 V
305 %#
306 %#  Now the data (this is drawn with some combination of absolute and
307 %#  relative lineto's and moveto's):
308 %#
309 728 560 M
310 2043 560 V
311 4926 911 L
312 2043 560 V
313 7116 4900 L
314 %#
315 %#  ...and more.
316 %# 
317 %#  We don't need to eplicitly 'stroke' to draw the lines for each function
318 %#  because the 'stroke' command is included in the line-type definition
319 %#  commands.  Thus switching line types automatically 'strokes' previous
320 %#  lines.  Clever program, that gnuplot...
321 %# 
322 %#  We can now repeat commands for the key and data for as many items
323 %#  as were specified on the "plot" or "splot" command.
324 %#
325 %#  Here's a second function, plotted with dots:
326 %#
327 LT1
328 6486 4486 M
329 (function 2) Rshow
330 6654 4486 Pnt
331 %# 
332 %#  The data:
333 %# 
334 840 911 Pnt
335 2883 2030 Pnt
336 4926 2590 Pnt
337 6969 3710 Pnt
338 %#
339 %#  ...and more.
340 %# 
341 %#  Since we're now done, we 'stroke' the last lines, close the dictionary
342 %#  and restore the previous settings (those saved by the 'gsave' at the top).
343 %#  [The 'gsave'/'grestore' pair is included so that if this file is embedded
344 %#  in another PostScript file, this patch won't mess up the other parts of
345 %#  the picture.  It's considered good PostScript style to do this.]
346 %# 
347 stroke
348 grestore
349 end
350 %# 
351 %#  And, finally, we send the page to the printer.  [If we do embed this
352 %#  file into another PostScript file, we'd remove this 'showpage'.  Unless,
353 %#  of course, this was appended to the other file, in which case we'd
354 %#  remove the 'showpage' from the end of that file.]
355 %# 
356 %#
357 showpage
358 %%Trailer
359 %%DocumentFonts: Helvetica
360 %%Pages: 1