Postscript

Here’s a popular graphics language you may not have heard of before.

Overview

You’ve likely drawn vector graphics with SVG. But there’s a language out there called PostScript. It’s concatenative (meaning stack-based). It is used to describe pages of text and vector graphics. It has a fascinating history.

We won’t go into any details of the language. Instead, we’ll just look at two brief example programs.

A Business Card

I found this example in a book a long time ago. I don’t remember where, exactly.

diamond.ps
% ****************************************************************************
%
%  diamond.ps
%
%  Example program from "PostScript Language Tutorial and Cookbook", p. 42.
%
% ****************************************************************************

% First, declare some variables.  It is always a good idea to assign scaled
% font dictionaries to variables that change fonts, because finding and
% scaling fonts take some time.

/MainFont
  /Helvetica-Bold findfont 15 scalefont def

/SloganFont
  /Helvetica-Oblique findfont 7 scalefont def

/OwnerFont
  /Helvetica findfont 10 scalefont def

% rightshow: s -> _   Shows string s right justified in an
%                     area that extends  from the  current
%                     point right 120 points.             

/rightshow {
  dup stringwidth pop            % width of string
  120 exch sub                   % 120 - width of string
  0 rmoveto                      % move that far to the right
  show
} def

% CardOutline: _ -> _   Draws a  252 x 144 box  with lower
%                       left corner (90,90), 1/2-pt wide.

/CardOutline {
  newpath
    90 90 moveto
    0 144 rlineto
    252 0 rlineto
    0 -144 rlineto
  closepath
  .5 setlinewidth
  stroke
} def

% doBorder: _ -> _   Draws a 234 x 126 box with lower left
%                    corner (99,99), 2-pts wide.

/doBorder {
  newpath
    99 99 moveto
    0 126 rlineto
    234 0 rlineto
    0 -126 rlineto
  closepath
  2 setlinewidth
  stroke
} def

% Diamond: _ -> _   Draws a diamond in a 72 x 108 box with
%                   top (207, 216), in gray level 0.8.

/Diamond {
  newpath
    207 216 moveto
    36 -54 rlineto
    -36 -54 rlineto
    -36 54 rlineto
  closepath
  .8 setgray
  fill
} def

% doText: _ -> _   Writes all the text, in black.

/doText {
  0 setgray
  90 180 moveto MainFont setfont (Diamond Cafe) rightshow
  90 168 moveto SloganFont setfont ("The Club of Lonely Hearts") rightshow
  216 126 moveto OwnerFont setfont (Sam Spade) show
  216 111 moveto (Owner) show
} def

% Main Program

CardOutline
doBorder
Diamond
doText

showpage
Exercise: Find an online PostScript emulator and run this program.

A Little Experiment

I remember, long ago, struggling with the concept of the “character origin,” so I wrote this to learn about it a little more:

charorig.ps
% ****************************************************************************
%
%  charorig.ps
%
%  This program tests to see where characters are printed relative to the
%  the point you put them at.
%
% ****************************************************************************

/Times-Roman findfont 144 scalefont setfont      % About 2 inch tall text
200 300 moveto
.24 setlinewidth                                 % thin as possible w/ 300dpi
0 72 rmoveto
0 -144 rlineto
72 72 rmoveto
-144 0 rlineto
200 300 moveto
(Efgh) show
200 300 144 sub moveto
("Efg") show
stroke
showpage