|
<< ben fry
creating postscript, illustrator, or pdf files... this page is a handful of tidbits about getting started with writing postscript (or similar) images from software. using postscript allows you to create high resolution images from software, i use a similar process for projects like my print studies, chromosomes, or the shop activity images. postscript postscript is relatively simple to write, and support illustrator and photoshop have continued to get better at reading postscript files (support wasn't always as good as it currently is..) i don't usually use postscript for images, and instead use illustrator file format (which is a sort of postscript derivative). but i learned postscript first as part of another project. rather than writing a tutorial on it, zach lieberman has already done the work for me and put a bunch of useful information on this page, check under the 'postscript' heading for vocabulary and examples. postscript is a full programming language that was designed for printers to be able to render high resolution images and it was in part responsible for kicking off "desktop publishing" around 1985 when apple introduced the laserwriter which rendered images using postscript and its scalable font technology. as a result of it being a full programming language, postscript can be very complex, leading to the situation where other programs have difficulties opening and rendering images. i learned postscript primarily from postscript by example by henry mcgilton, which is an enormous catalog of examples. i see it's out of print now, but this adobe book is another useful one. it appears that a pdf of this book is also available on the adobe site, along with a few others on this page (thanks tonylemesmer for the note about these being broken earlier). some java code to get started: FileOutputStream fos =
new FileOutputStream("something.ps");
PrintWriter out =
new PrintWriter(new OutputStreamWriter(fos);
// the header for postscript
out.println("%!PS-2.0");
// put your drawing code here
// this four line example draws a line
out.println("30 20 moveto");
out.println("50 40 lineto");
out.println("closepath");
out.println("stroke");
// at very end
out.println("showpage");
out.flush(); // important
out.close();
the full postscript language specification can be downloaded from
adobe's developer site as well. this document is a beast at almost 1000
pages, and can also be purchased in printed form. i've never bothered
with this personally, but at least the pdf would be useful to have
around. the pdfs are here
along with a handful of updates since the spec was published.
illustrator i like to use illustrator format because it's not that much more difficult than postscript, and can be opened directly (and more consistently) in illustrator for better control over printing and placement of objects. its support for controlling and moving type is also a bit simpler. in addition, if there is a feature in illustrator that you'd like to be able to output (ie. text on a path, or patterns) you can simply create an example in illustrator, then look at the code it generates to see how you can do it from your program. illustrator versions 9 and 10 use pdf for their output, which is much more difficult to write (more on this later), so it's important to save images for an older verison of illustrator so that you can actually read what's generated. in general, save with the oldest format possible that still supports your feature, so that the file is simpler. to get started, open up illustrator, draw a line, and save as an illustrator 3.0 file. open that file in a text editor and you should see a lot of text garbage.. look for the "EndSetup" line, and following that shortly should be two numbers and an 'm' (for moveto) on one line, and two more numbers and an 'l' (for lineto) that are the postscript-esque commands for drawing your image. the rest of the stuff in the file is illustrator's setup and takedown information, which determines things like page size and what patterns and colors are installed in the palettes for your document. i'll generally try to strip this file down to its bare minimm that still generates the proper image. on windows the line-endings are inconsistent, so i first change all line endings to unix ones, and then re-open the file to see if it still works. then study different lines to see if they seem to be useful. try removing some, and see how it goes when you re-open the file. you can comment out lines (disable them) by placing a single % followed by a space before any line.. this is better than deleting for this trial and error process. to figure out how the different parts work, or what different commands mean, check out the illustrator 7 file format document. it's a full reference for most of the commands you'll come across. thanks to adobe for posting this. they used to have the spec for version 3.0 as well, which was often more useful because most features you'll want are in 3.0 and the spec was simpler. oh well. another useful thing is to compare the contents of two illustrator files to see what the relevant parts are. that is, save an empty document as one file, then draw a single line, and save that. if you compare these with some sort of visual diff (i use metrowerks codewarrior's built in command for it, or the command line 'diff' utility from unix). next change a feature like color, and compare that to the original file. the patterns start making sense after a while. illustrator api tomek zemla also reminded me about a new scripting api that's been built into illustrator 10. though i haven't messed with it myself (i'd rather write in perl or java or whatever, rather than the languages they provide), this is another way to get started: Illustrator 10 has a built in scripting API. I have not played with it much (just got a copy), but it seems to be complete, i.e. whatever you can accomplish through the Illustartor UI you can also access via API. The information about all that is burried at the end of the manual that sends you to a pdf manual burried on the CD ROM. There are actually three ways to script: Visual Basic, Apple Script (depending on the platform) and cross platform JavaScript. I have been exploring the JS from a few provided examples and the over 200 page manual and it seems to be a very convinient way to create images programatically. The API seems straight forward and considering the syntax similarities between Java/Processing and JavaScript it can be a useful way to transfer paper destined designs from one to another. The same scripting system was added to Photoshop 7. thanks to tomek for pointing this out. pdf could be a really cool way to generate images, especially if you don't need to edit them once they're finished (illustrator can edit pdf, support is so-so but continues to get better with each new release). i haven't done this so much yet, but this is where i'd like to go with it, and there are many pdf-generating libraries for any given programming language (java, perl, c++, php) that can do a lot of the gritty work for you. adobe has also posted the pdf spec on its site for people interested in the nerdy details. << ben fry | last updated november 2003 |