Mediascripting on the Web (CSC 195 2014F) : Outlines

Outline 04: Postscript


Held: Thursday, 13 February 2014

Back to Outline 03 - Processing (1). On to Outline 05 - HTML and Cascading Style Sheets.

Summary

We explore the basics of the graphics language PostScript.

Related Pages

Overview

Administrivia

PostScript

General File Format

This is for encapsulated PostScript, which is really nice for figures.

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 576 792
%%DocumentNeededResources: font Times-Roman
%%EndComments

...

showpage
%%EOF

Drawing Commands

Setting the color

*r* *g* *b* setrgbcolor
1 0 0 setrgbcolor % red

*g* setgray
0 setgray % black

Setting the line width for strokes

*w* setlinewidth
1.0 setlinewidth

Moving to a particular position

*x* *y* moveto
100 100 moveto

Specifying lines (not shown until stroked)

*x* *y* lineto
100 120 lineto

*x* *y* rlineto
15 15 rlineto

Stroking everything since the last stroke or fill

stroke

Filling figures (if possible

closepath
fill

Saving and restoring context

gsave
grestore

Creating a circle (or arc)

*x* *y* *r* *theta1* *theta2* arc

Creating a bezier curve

*x1* *y1* *xc* *yc* *x2* *y2* curveto

Typical mechanism for setting the font

/Times-Roman findfont
12 scalefont setfont

Showing text

(TEXT) show

Changing the Environment

*a* rotate

*x* *y* scale

*x* *y* translate

More Fun with Programming

Define a variable

/*var* *exp* def
/destx 72 def

Group instructions (which can go on the stack)

{ *instructions * }

Repeat instructions

*n* { *instructions* } repeat

Define a set of instructions

/*name* { *instructions* } def

Rotate the top of the stack

*size* *amt* roll

Copy an element at some offset

*offset* index

Copy the top element

dup

Switch the top two elements of the stack

exch

Negate the top value on the stack

neg

Sample Code

How Sam Draws Rectangles

% left bottom width height rect
%   Draw the specified rectangle.
/rect {     % l b w h
  4 2 roll  % w h l b
  moveto    % w h
  1 index   % w h w
  0         % w h w 0
  rlineto   % w h
  dup       % w h h
  0 exch    % w h 0 h
  rlineto   % w h
  exch      % h w
  neg 0     % h -w 0
  rlineto   % h
  neg       % -h
  0 exch    % 0 -h
  rlineto   % 
  stroke    % 
} def

Right-justifying text

% string showright
/showright {    % str 
  dup           % str str
  stringwidth   % str str-width str-height
  pop           % str str-width
  neg           % str -str-width
  0             % str -str-width 0
  rmoveto       % str 
  show
} def

Why Study PostScript?

Copyright (c) 2014 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.