Writing PostScript files

versions0095+
contributorswatz
started on2006-02-18 14:35

Adobe PostScript is a page description language which can easily be used to provide a format for saving vector graphics from Processing. To understand how PostScript works, take a look at Zach Lieberman's introduction (see the heading PostScript .) Peter Weingarter has written a very useful PostScript tutorial. Download it as a ZIP file or read it online.

SimplePostscript is a simple library for writing PostScript files. We will use it here to write PostScript files. In an ideal world, SimplePostscript would be an extension of the PGraphics class, but for now it is separate from the built-in Processing graphics commands.

Installation

Unzip the archive and place the SimplePostscript folder into your Processing libraries folder, then restart Processing.

Usage

At the top of your sketch, type:

import SimplePostscript.*;

Initialize the SimplePostscript instance and open a file with as follows. The parameters given to the open() command are the coordinates of the bounding box.

SimplePostscript ps=new SimplePostscript(this);
ps.open("test.ps",0,0, 300,300);

Source code

Download the SimplePostscript library from the download section below.

/**
ps taken from http://processinghacks.com/hacks:ps
@author Marius Watz
*/
 
import SimplePostscript.*;
 
SimplePostscript ps;
 
void setup() {
  size(200,200);
  noLoop();
}
 
void draw() {
  background(100,0,0);
  ps=new SimplePostscript(this);
  ps.open("test.ps",0,0, 300,300);
 
// Grayscale colors are float values in the 0..1 range
  ps.setgray(0.5f);
  ps.moveto(0,0);
  ps.lineto(300,300);
  ps.stroke();
 
  // RGB colors are integers in the 0..255 range
  ps.setrgb(255,200,0);
  ps.circle(0,50, 30);
  ps.fill();
 
  // CMYK colors are float values in the 0..1 range
  ps.setcmyk(1,0,0,0);
  ps.setfont("Arial",12);
  ps.text(0,100, "Testing text");
  ps.fill();
 
  ps.setcmyk(0,1,1,0);
  ps.setlinewidth(3);
  ps.moveto(0,200);
  ps.curveto(50,100, 100,100, 150,200);
  ps.curveto(200,300, 200,50, 300,0);
  ps.stroke();
 
  // Close file when done. If not closed, file output may not
  // be complete.
  ps.close();
}

Downloads

Related Links

hacks/ps.txt · Last modified: 2007-07-13 02:12 (external edit)