Penguin
Note: You are viewing an old revision of this page. View the current version.

A programming language for printers, based on forth. See ghostscript (a linux implementation) (gs(1))

The following is copied from the HowToPrintingHOWTO?


Unix software, and the publishing industry in general, have standardized upon Postscript as the printer control language of choice. This happened for several reasons:

Timing

Postscript arrived as part of the Apple Laserwriter, a perfect companion to the Macintosh, the system largely responsible for the desktop publishing revolution of the 80s.

It's device-independent

Postscript programs can be run to generate output on a pixel screen, a vector screen, a fax machine, or almost any sort of printer mechanism, without the original program needing to be changed. Postscript output will look the same on any Postscript device, at least within the limits of the device's capabilities. Before the creation of PDF, people exchanged complex documents online as Postscript files. The only reason this standard didn't "stick" was because Windows machines didn't usually include a Postscript previewer, so Adobe specified hyperlinks and compression for Postscript, called the result PDF, distributed previewers for it, and invented a market for their "distiller" tools (the functionality of which is also provided by ghostscript's ps2pdf and pdf2ps programs).

It's a real programming language

Postscript is a complete programming language; you can write software to do most anything in it. This is mostly useful for defining subroutines at the start of your program to reproduce complex things over and over throughout your document, like a logo or a big "DRAFT" in the background. But there's no reason you couldn't compute #960 in a Postscript program.

It's open

Postscript is fully specified in a publically available series of books (which you can find at any good bookstore). Although Adobe invented it and provides the dominant commercial implementation, other vendors like Aladdin produce independently coded implementations as well.


Here is a very simple PostScript file that hopefully demonstrates very basic postscript commands and functionality:

%!PS-Adobe-2.0
%%BoundingBox: 0 0 400 400
%%Pages: 1
%%EndComments

% 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
blue

% 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

black
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-Roma 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

% 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)



%%EndPage

showpage % render the page

%%EOF