1. Basic Usage

Essential concepts and techniques for PostScript programming.

2. Core Concepts

2.1. Stack Operations

PostScript uses a stack for all data manipulation. Understanding stack operations is fundamental:

% Push values
10 20 30

% Duplicate top
dup            % Stack: 10 20 30 30

% Exchange top two
exch           % Stack: 10 20 30 30

% Remove top
pop            % Stack: 10 20 30

See Stack Manipulation Commands for complete reference.

2.2. Coordinate Systems

PostScript uses two coordinate systems:

User Space: Your drawing coordinates Device Space: Physical device pixels

% Default: 72 units = 1 inch
100 200 moveto     % 100/72 inch right, 200/72 inch up

Transform user space with: * translate - Move origin * scale - Change unit size * rotate - Rotate axes

2.3. Graphics State

The graphics state stores rendering parameters:

gsave                % Save state
  1 setlinewidth     % Change line width
  0.5 setgray        % Change color
  stroke             % Draw
grestore             % Restore state

2.4. Path Construction

Paths are built then painted:

newpath              % Start fresh
100 100 moveto       % Set start point
200 100 lineto       % Add line segment
200 200 lineto       % Add another
closepath            % Close the path
stroke               % Paint it

2.5. Painting Operations

Two main operations:

stroke - Draw along the path fill - Fill inside the path

% Filled rectangle
newpath
0 0 moveto
100 0 rlineto
0 100 rlineto
-100 0 rlineto
closepath
0.5 setgray fill

% Stroked circle
newpath
150 150 50 0 360 arc
0 setgray stroke

3. Common Patterns

3.1. Drawing a Box

/box {  % x y width height
  gsave
    newpath
    moveto      % x y
    dup 0 rlineto    % width 0
    exch 0 exch rlineto  % 0 height
    neg 0 rlineto    % -width 0
    closepath stroke
  grestore
} def

100 100 50 75 box

3.2. Centered Text

/centerShow {  % string x y
  moveto
  dup stringwidth pop 2 div neg 0 rmoveto
  show
} def

(Hello) 300 400 centerShow

4. See Also


Table of contents


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.