qd.pl: Quickdraw for Perl

qd.pl is a small contribution to the world of perl hackery. It is a library of perl routines for creating Macintosh PICT files. It replaces a large number of the MacOS Quickdraw routines with equivalent perl subroutines. It handles text drawing, line drawing, rectangles, ovals, polygons, round-cornered rectangles, color and pattern transfer modes, and various scaling and warping operations.

Why would anyone want such a thing? The main advantage that PICT format has over more powerful graphics languages, such as Postscript, is that the pictures generated in this way can be manipulated on an object-by-object basis in any of the Macintosh drawing programs. Further, PICT is pretty standard at this point and many MS-WINDOWS and even a few X11-based drawing programs can read and write this type of file. It is also easy to translate the images into GIF, JPEG, TIFF or Postscript using the NetPBM utilities (Note: NetPBM is available many places. If this particular ftp site is busy, use Archie to find another site. The picttoppm that came with earlier pbm releases was broken and will produce a boring black rectangle with my example program).

In addition, Quickdraw has a simple programmatic interface. It is very easy to create complex drawings. It is well described in Apple's Inside Macintosh books, and many third party books as well.

There are a few limitations, including the fact that I never got around to writing the powerful region-manipulation calls. Maybe in the next release.

Installation

  1. Download qd.pl here
  2. Install it into /usr/local/lib/perl (or whatever is appropriate for your site)
  3. To do accurate text measurement, qd.pl needs the variable $qd'X11FONTS to point to a directory holding bdf font files. Change this variable to whatever is appropriate (not necessary if you're willing to work with "approximate" text width measurements).
  4. In your scripts, add the line require "qd.pl"
  5. Capture the output: you might want to pipe it through ppmtogif to create a GIF file, or through BinHex to create a mailable/downloadable Macintosh PICT file.

A Quick Example

Code to draw a simple picture:

File test_qd.pl #/usr/local/lib/perl require "qd.pl"; &qd'SetRect(*myRect,0,0,500,500); # Define a 500 pixel square &qd'OpenPicture($myRect); # Begin defining the picture &qd'ClipRect($myRect); # Always a good idea &qd'MoveTo(5,5); # Move the pen to a starting point &qd'LineTo(400,400); # A diagonal line &qd'TextFont($qd'COURIER); # Set the font &qd'MoveTo(50,20); # Move the pen to a new starting point &qd'DrawString("Hello there!"); # Friendly greeting &qd'SetRect(*myRect,80,80,250,250); # New rectangle &qd'RGBForeColor(0x0000,0x0000,0xFFFF); # Set the foregroune color to blue &qd'PaintRect($myRect); # Fill rectangle with that color $data = &qd'ClosePicture; # Close picture and retrieve data print $data; # Print the data

The command line to create a GIF image

1> test_qd.pl | picttoppm | ppmtogif -transparent white > test.gif

The command line to create a PICT image and mail it to yourself

2> test_qd.pl | BinHex -t PICT -c JVWR -n 'Test Picture'| mail `whoami`

Redistributon

Feel free to redistribute this.

BinHex: Create a Macintosh BinHex File from a UNIX File

This is a little C filter that takes a UNIX file and encodes it into the Macintosh BinHex format, suitable for transmission via e-mail, ftp, or the World Wide Web. The nice thing about this format is that it allows you to specify the Macintosh file name, file type and creator. The nice thing about this program is that, unlike other utilities that do similar things, it works on STDIN as well as on files, and is therefore suitable for use in a UNIX pipe.

Download it here.

Instructions for compiling and installing it are in the tar'red source code.

Lincoln Stein, lstein@genome.wi.mit.edu

Whitehead Institute/MIT Center for Genome Research

This document last modified November 11, 1994