% This file was converted from HTML to LaTeX with Nathan Torkington's
% html2latex program
% Version 0.9b
\chapter{Examples}

\section*{Clipping Text to a Box}
\begin{verbatim}
%!
% operator box: xcoord ycoord box -
% Creates one inch box at xcoord, ycoord
/box {
   newpath
   moveto
   72 0 rlineto
   0 72 rlineto
   -72 0 rlineto
   closepath
} def
/Times-Roman findfont 30 scalefont setfont
gsave                      % Save the old clip path
  72 72 box                % Set up our box
  gsave                    % Don't allow box to be lost after stroke
    stroke
  grestore                 % Restore the box path
  clip                     % Clip to the box
  60 60 moveto
  (This is Times-Roman clipped to a box) show
  70 90 moveto
  (This is Times-Roman clipped to a box) show
  50 120 moveto
  (This is Times-Roman clipped to a box) show
grestore                   % Get the clip path back
showpage
\end{verbatim}

\section*{Clipping to Text}
\begin{verbatim}
%!
gsave                                     % Save old clip path
  /Times-Roman findfont 60 scalefont setfont
  72 72 moveto (Clipping) true charpath   % Set up the text's path
  clip                                    % Clip to it
  174 72 translate                        % Set our origin to middle
  0 2 360 {                               % For every second degree of circle
    newpath
    gsave
      rotate                              % Rotate to angle
      0 0 moveto                          % From new origin
      300 0 rlineto                       % Setup a 300 point long line
      stroke                              % ... and draw it
    grestore
  } for
grestore                                  % Restore old clip path
showpage
\end{verbatim}

\section*{Drawing a Box}
\begin{verbatim}
%!
% Draws a one square inch box and inch in from the bottom left
/inch {72 mul} def      % Convert inches->points (1/72 inch)
newpath                 % Start a new path
1 inch 1 inch moveto    % an inch in from the lower left
2 inch 1 inch lineto    % bottom side
2 inch 2 inch lineto    % right side
1 inch 2 inch lineto    % top side
closepath               % Automatically add left side to close path
stroke                  % Draw the box on the paper
showpage                % We're done... eject the page
\end{verbatim}

\section*{Drawing a Box with rlineto}
\begin{verbatim}
%!
%% Draws a one square inch box and inch in from the bottom left
%% This example uses relative coordinates on the lines
/inch {72 mul} def      % Convert inches->points (1/72 inch)
newpath                 % Start a new path
1 inch 1 inch moveto    % an inch in from the lower left
1 inch 0 inch rlineto   % bottom side
0 inch 1 inch rlineto   % right side
-1 inch 0 inch rlineto  % top side
closepath               % Automatically add left side to close path
stroke                  % Draw the box on the paper
showpage                % We're done... eject the page
\end{verbatim}

\section*{Filled Box}
\begin{verbatim}
%!
%% Draws a one square inch box and inch in from the bottom left
/inch {72 mul} def      % Convert inches->points (1/72 inch)
newpath                 % Start a new path
1 inch 1 inch moveto    % an inch in from the lower left
2 inch 1 inch lineto    % bottom side
2 inch 2 inch lineto    % right side
1 inch 2 inch lineto    % top side
closepath               % Automatically add left side to close path
fill                    % Fill in the box on the paper
showpage                % We're done... eject the page
\end{verbatim}

\section*{Text}
\begin{verbatim}
%!
% Sample of printing text
/Times-Roman findfont % Get the basic font
20 scalefont          % Scale the font to 20 points
setfont               % Make it the current font
newpath               % Start a new path
72 72 moveto          % Lower left corner of text at (72, 72)
(Hello, world!) show  % Typeset ``Hello, world!''
showpage
\end{verbatim}

\section*{Rotation}
\begin{verbatim}
%!
% Example of rotation... draws 36 lines in a circular pattern
0 10 360 {       % Go from 0 to 360 degrees in 10 degree steps
  newpath        % Start a new path
  gsave          % Keep rotations temporary
    144 144 moveto
    rotate       % Rotate by degrees on stack from 'for'
    72 0 rlineto
    stroke
  grestore       % Get back the unrotated state
} for            % Iterate over angles
showpage
\end{verbatim}

\section*{Scale}
\begin{verbatim}
%!
% Example of scaled image (text)
/Times-Roman findfont 40 scalefont setfont
gsave
  72 72 moveto
  0.5 1 scale          % Make the text narrow
  (Narrow Text) show   % Draw it
grestore
gsave
  72 144 moveto
  1 2 scale              % Make the text tall
  (Tall Text) show       % Draw it
grestore
gsave
  72 216 moveto
  2 0.5 scale          % Make the text wide and short
  (Squeezed Text) show % Draw it
grestore
showpage
\end{verbatim}

\section*{Shade and Width}
\begin{verbatim}
%!
% Demonstrate shading and width in drawing lines and filling shapes
% Define an operator box which builds a path for a one inch square box
% Note that box does not draw or fill the box.
/box {
  newpath
  moveto                    % Current point is on stack
  0 72 rlineto              % Left
  72 0 rlineto              % Top
  0 -72 rlineto             % Right
  closepath                 % Bottom
} def
0 setgray                   % 100% black
1 setlinewidth              % One point thick lines
72 72 moveto 72 144 lineto stroke    % Draw a one inch line
gsave                       % Save a copy of the current settings
  0.5 setgray               % 50% black
  10 setlinewidth           % 10 point wide lines
  144 72 moveto 144 144 lineto stroke  % Draw a one inch wide line
  216 72 box                           % Build a square path...
  0.35 setgray              %    make it a little darker...
  fill                      %    and fill it.
grestore                    % Go back to the original settings
3 setlinewidth              % Make the box lines wider
300 72 box stroke           % Draw a black box
showpage
\end{verbatim}

If you try this example, you should note a couple of things. Firstly,
the black outlined box is a little larger than the gray filled one.
This extra width comes from the 3 point wide lines used to draw
it---they are {\em centered} about the path of the box. The ink
filling the gray box, however, is completely within the path of the
box. Also, when shading objects, you must be careful. PostScript
makes shades through a process called halftoning. Basically, uniform
dots are placed in various patterns to simulate different shades of
grey. Unfortunately, various considerations limit how many shades a
printer can produce. So some grey tones may come out the same. This
may be the case with the filled box and the outlined box when viewed
on your screen or printer.

\section*{Transformations}
\begin{verbatim}
%!
% Example to demonstrate translate, rotate, and scale
% operator box: xcoord ycoord box -
% Creates one inch box at xcoord, ycoord
/box {
   newpath
   moveto
   72 0 rlineto
   0 72 rlineto
   -72 0 rlineto
   closepath
} def
% Specify font for text labels
/Helvetica findfont 40 scalefont setfont
gsave
  40 40 translate       % Set origin to (40, 40)
  0 0 box stroke        % Draw box at new origin...
  77 0 moveto
  (Translated) show     %   and label
grestore
gsave
  100 150 translate     % Translate origin to (100, 150)
  30 rotate             % Rotate counter-clockwise by 30 degrees
  0 0 box stroke        % Draw box...
  75 0 moveto
  (Translated & Rotated) show    %   and label
grestore
gsave
  40 300 translate      % Translate to  (40, 300)
  0.5 1 scale           % Reduce x coord by 1/2, y coord left alone
  0 0 box stroke        % Draw box...
  75 0 moveto
  (Translated & Squished) show    %   and label
grestore
gsave
  100 450 translate     % Set origin to (300, 300)
  30 rotate             % Rotate coordinates by 45 degrees
  0.5 1 scale           % Scale coordinates
  0 0 box stroke        % Draw box
  75 0 moveto
  (Everything) show
grestore
showpage
\end{verbatim}

\section*{Translate}
\begin{verbatim}
%!
% Draw a box at 72, 72 using translate
% operator box: xcoord ycoord box -
% Creates one inch box at xcoord, ycoord
/box {
   newpath
   moveto
   72 0 rlineto
   0 72 rlineto
   -72 0 rlineto
   closepath
} def
gsave               % Preserve the old coordinates
  72 72 translate   % Set origin to (72, 72)
  0 0 box stroke    % Draw the box at the new origin
grestore            % Restore the old coordinates
showpage
\end{verbatim}
