A stack-based ProgrammingLanguage that resembles Forth, but mixes in some functional programming principles to allow a considerably higher level of abstraction. Although a complete, general purpose language, it was developed for typesetting and is accompanied by a complex, powerful graphical model. Adobe Systems, the creators of PostScript, also produced a version for use on-screen, called Display PostScript, but that was never very popular, and was eventually abandoned.

PostScript programs come in .ps files. GhostScript is a Free software-only implementation of the language and graphics model, allowing users to view such files onscreen as well as send them to cheap printers that do not understand PostScript natively. PostScript is also the language used at the core of PDF.

The Printing HOWTO explains why PostScript happened to become so well established.

Here's a simple example of what PostScript code looks like:

%%BoundingBox: 0 0 400 400
%%Pages: 1

% any line starting with % is treated as a comment. "%!" as the first two
% bytes of the file is the "magic marker" that implies postscript.

% Exception...
% need postscript version 2 or later to understand the above-style
% "special" comments starting with "%%". Also can use like that per-page

%%Page: 1 1

% disclaimer: the commands and examples given here aren't taken from a
% reference manual or anything, so some of the comments might be wrong or
% mis-understand what is actually happening...

save % save the current colour, scale factor, etc variables to the stack

% set the active colour to red (this is an R G B setting, between 0 and 1)
1.0 0 0 setrgbcolor

% scale is global... if you double the scale, you need to halve it again
% later, unless you use save and restore...

2 1 scale % set the scaling factor to 2x for x and 1x (ie normal) for y

% draw a circle (well, it would be a circle if scaling was 1x)

%  x-origin y-origin radius startangle endangle arc (before scaling)

100 100 50 0 360 arc % draws an arc (might be invisible)

fill % fills the previous object with the active colour

restore % go back to colour, scale, etc of the last save

% create some constants
/x 100 def
/y 100 def
/radius 50 def

% draw a circle using the saved state
x y radius 0 360 arc fill

% make some alias commands
/black {0 0 0 setrgbcolor} bind def
/blue {0 0 1 setrgbcolor} bind def

% use them

% move the "cursor"
100 300 moveto

% print some text
% before we can print text, we need to select the active font.
/Times-Roman findfont % font name
12 scalefont % font size
setfont % make that font the active font

(Hello. This is blue text) show

100 250 moveto ( You need to escape \( and \) as \\\( and \\\)) show

% the "translate" command draws everything offset by this x and y,
% save and restore this - takes effect until the next translate or restore
% 200 0 translate % causes everything from here-on to be drawn
% 200 pts to the right

50 200 moveto % set current position
3 -5 (diagonal) ashow

% print out the memory usage info

/Times-Roman-Bold findfont % choose font
15 scalefont % font size
setfont % make active

200 225 moveto (Memory Info:) show

% print out information about the memory usage
vmstatus % ps function that puts 3 numbers on the stack...
% to be honest, I'm not sure what the numbers are. I suspect
% the 2nd is the mem used, and the 3rd is mem allocated. But ghostscript
% can dynamically increase the avail mem, so it isn't really a very useful
% number in most cases. If you send this file to a postscript printer it
% might actually mean something...
3 1 roll % take top value on stack and put it 3rd
exch % swap the top 2 values
%%% - now have reversed the order of the 3 numbers from vmstatus
200 204 moveto

/Times-Roman findfont 12 scalefont setfont
(Min(?): ) show
10 string % create a 10-byte string
cvs % fill the string with the previous number on the stack
show ( bytes) show
200 192 moveto
(Used: ) show
10 string cvs show ( bytes) show
200 180 moveto
(Max: ) show 10 string cvs show ( bytes) show

%% draw a small irregular polygon
0 setlinewidth %
260 300 moveto
270 315 lineto
300 315 lineto
310 280 lineto
300 260 lineto
closepath % back to the origin
1 0 1 setrgbcolor % red + blue = purple

% print something to gv's pop-up informative window, or stdout for gs
(Example simple postscript commands\n) print
flush % cause it to be sent (from buffered)


showpage % render the page


CategoryProgrammingLanguages, CategorySpecialPurposeProgrammingLanguages