PostScript


Most designers and programmers whose paths I've crossed have heard of PostScript but don't know much about it. I think it's a hidden gem. Not only is it a fascinating programming language which can be used to create generative drawings and save you hours of time through automation, but you can also use it as an intermediary format to transfer output from other languages such as Flash ActionScript or Java / Processing into Adobe Illustrator.

A simple PostScript program

Copy and paste this into a text editor (NotePad or TextEdit), save it as 'simple.ps' and then open it in Illustrator.

%!PS-Adobe-3.0 

0 0 moveto
100 100 lineto
stroke

showpage

This little snippet gives the following result:

ps-line.gif

A line from 0,0 to 100,100. Amazing, eh? Not really, but the point is that the code is really simple to understand and we're only using the line drawing command. If you go slow and learn one command at a time, you can eventually do some pretty interesting and useful things.

Learning PostScript

There are lots of PostScript tutorials on the web that will explain the basic concepts. One thing to note is that PostScript is stack based which means you push numbers onto the stack before operating on them. In raw code it makes it look like you're doing things backwards and can seem off-putting. In practise it's very simple and logical.

The essential guide to PostScript is the PLRM or 'red book'. I bought a copy of this years ago and it's pretty big at 800 pages! But thanks to the generosity of Adobe you can now download it online;

[PDF] PostScript Language Reference Manual, third edition

A lot, for a little

Once you've learned a few commands you'll be able to achieve a lot with very little. Here's something more advanced using a for-loop in conjunction with graphics transformation;

%!PS-Adobe-3.0 

0.75 setlinewidth
0.75 setgray

0 3 360 {
	newpath
	gsave
	0 0 moveto
	rotate
	100 0 rlineto
	stroke
	grestore
} for

showpage

Produces the following result:

picture-4.gif

Which is starting to look a bit more interesting, and like something that might be tricky to create purely 'by hand' in Illustrator. I've used techniques based on this to create images for quite a few projects over the years. The Design Week book cover was created using PostScript as were the Kingston University posters.

I've put the source for these into a small download. You'll need Illustrator or some other PostScript viewer to see them.

[ZIP] Download PostScript examples

Output from Flash into PostScript

Probably the best use of PostScript from a modern designer/programmer's perspective is as an intermediary format. With not much programming you can easily take output from Flash and get it into Illustrator.

generationpress.jpg
For the Generation Press website I created a simple toy on the homepage which lets visitors generate their own pattern. It's a nice example of something created in Flash which would look great as a high-resolution printout.

Demo Application

Here's the toy wrapped into a demo application.

[ZIP] Download Source Code

To use, click to start drawing. Once the drawing has finished, click 'Show PostScript' to see the code. This code doesn't update live because Flash isn't fast enough, so wait until the drawing has finished before viewing it. All the application does is output the same drawing commands as PostScript-style commands into the text window;

Actionscript:
  1. this["output"].lineTo(xp, yp);
  2. ps(xp+" "+yp+" lineto");

The ps() function outputs the code to the text window;

ps-code-example.jpg

Now you can copy and paste this code into a text editor. Save it with a '.ps' extension and then load into Illustrator:

ps-mockup.jpg

The image on the right is magnified to show that the drawing is made up of a series of small lines created by the lineto command in the PostScript code.

Conclusion

So that demonstrates the basic principle. PostScript is a very powerful language and you can do all kinds of other fun things with it, particularly when you start to redefine the built in lineto and curveto commands to do something else... but that's a story for another day.


5 June 2007 in ActionScript, PostScript, Programming, Source Code

10 Comments

    • 1
    • Comment by MsBrittle
    • 5 June, 2007 at 7:22 pm

    That rocks! I am going to give it a go.

    • 2
    • Comment by bruno
    • 20 July, 2007 at 12:41 am

    lovely. is there a way to pre input the values so that the resulting flash animation runs without user imput?

    • 3
    • Comment by Simon
    • 20 July, 2007 at 5:12 am

    Hi bruno - yes there is, that’s how the original works on the Generation Press website. You just need your own variation on the following (excuse the short var names!):

    //insert at end of begin() function
    
    // circle radii - edit these
    _r1 = 42;
    _r2 = 50;
    _rp = 58;
    
    // set up the system
    xc = _r1 + _r2;
    yc = 0;
    xp = xc + _rp;
    yp = 0;
    this["circle1"]._visible = false;
    _status = DRAWING;
    initDraw();
    
    • 4
    • Comment by bruno
    • 20 July, 2007 at 5:32 am

    wonderful, thank you.

    • 5
    • Comment by Adam Follett
    • 1 May, 2008 at 12:39 pm

    Hi Simon
    I’ve tried to copy the postscript code into TextEdit, as you suggest, but it won’t let me save with a .ps suffix.

    Any tips as to what I’m doing wrong?

    Thanks
    Adam

    • 6
    • Comment by Simon
    • 1 May, 2008 at 6:52 pm

    Adam - no idea what’s wrong there. Does it help if you enable ’show all file extensions’ in the Finder preferences? Can you try just saving the file and then rename it in the Finder?

    • 7
    • Comment by Rick Weiss
    • 5 May, 2008 at 4:53 pm

    Hi Simon,
    Awesome! Thanks for making this available. I was able to copy the postscript text from Simon’s web page into an application called Editra that I downloaded from VersionTracker for Mac OS, (it’s free) then save that as .ps document. Those opened fine in Illustrator CS3 if the object was fairly simple. If the rendering on the web site was complex I kept getting an error message in Illustrator saying couldn’t open for an unknown reason when I tried to open the .ps file. Any suggestions for getting a more complex object to open?

    • 8
    • Comment by Simon
    • 5 May, 2008 at 7:11 pm

    Hi Rick

    Hmm - haven’t really pushed the limits like you have! If you can mail me a file that won’t open in Illustrator, I’ll have a play around. There are other programs that will open .ps files by the way - Apple Preview on the Mac for one, but also GhostView/GSView might be worth a try; http://pages.cs.wisc.edu/~ghost/

    • 9
    • Comment by Andy
    • 9 May, 2008 at 4:51 pm

    If you want to put code in to Textedit on Mac OS X you need to make sure it is set to plain text note RTF.

    You can set the default in preferences I think although I’m on a PC so can’t check right now.

    • 10
    • Comment by Marco
    • 30 July, 2008 at 7:23 am

    Hey, Simon, I got the same problem here. I guess you’re using MacOS X as like as I do. I solved using Komodo Edit, a freeware text editor, that is able to save as Postscript file, highlighting syntax and showing values in various colors.
    Hope this may help you.


Leave a Comment


(required)
(will not be published) (required)

Follow comments through the RSS 2.0 feed. Trackback from your own site.

Previous: Site Update / Next: Sizer