Desktop Printing Revealed
Volume Number: 13
Issue Number: 8
Column Tag: develop
by Dave Polaschek
With the introduction of System 6 and MultiFinder in 1989, Apple introduced
background printing. For the first time, a user could regain control of the Macintosh
almost immediately after printing a document, while the printer driver processed the
document in the background. Classic background printing has been far more robust
than anyone may have imagined, and it's still the basis of much of today's printing
technology, including desktop printing. Unfortunately for developers interested in
supporting desktop printing, the interfaces to background printing have remained
largely undocumented.
In 1994 the desktop printing architecture was introduced with the StyleWriter 1200
driver and LaserWriter 8.3. From its initial release to the present, desktop printing
has existed as a collection of extensions and invisible applications. While this has not
created a clear and simple picture for developers interested in supporting desktop
printing, this technology shows every sign of thriving for a long time to come, so
understanding its architecture is more vital than ever.
While the complete story is far from written, and things will certainly change again
with the introduction of Rhapsody, this article will cover background printing for all
pre-Rhapsody versions of the MacOS. This includes the significant changes that will
make Desktop Printing accessible to third party developers. If you've currently got a
printer driver, this article describes everything you need to add support for desktop
printing.
Classic Background Printing
The way early background printing worked was that printer drivers would save each
page as a QuickDraw picture in the data fork of a temporary file (a spool file) in a
special folder (the Spool Folder) within the System Folder. There was also information
stored in the resource fork describing the page format, document name, and other job
information, as well as offsets to the beginning of the data for each page. A special
application (Backgrounder) launched by MultiFinder at startup time (in System 7,
Backgrounder was incorporated into Finder) would see the document created by the
printer driver and would launch PrintMonitor, which would run in the background,
feeding the spool file to the printer driver.
PrintMonitor performs its magic by calling the printer driver in much the same way
as an application would, except rather than your driver's 'PDEF' 0 resource getting
called, its 'PDEF' 126 resource -- which has the same format as the 'PDEF' 0
resource -- is called. A special PrGeneral call is sent just after PrOpen has been
called (before PrOpenDoc has been called), to provide the printer driver with the
pointers to notification functions it will need to call. PrintMonitor then prints each
page by replaying the stored page data to the driver. This method of background
printing is still used by the Printing Manager today in two cases: when desktop
printing isn't installed (or has been disabled) and when the Finder isn't running
(usually because At Ease is running instead).
Getting to know classic background printing means you need to know the structure of
its spool file. The resource fork of the spool file contains the following resources (note
that all structures mentioned in this document have 68k alignment):
* 'PREC' 3 -- the print record
* 'alis' -8192 -- an alias to the driver that created the spool file
* 'ics#' 131 -- the small icon to display for the spool file in the
PrintMonitor window
* 'PREC' 124 -- the printer name
* 'PREC' 126 -- the job information
short version; // always 0
short flags; // always 0
short numPages; // total number of pages in the spool file
short numCopies; // total number of copies for the spool file
OSType crtr; // the creator type of the driver used to
// create the spool file
Str31 appName; // the application name used to print the
// spool file
} PREC236Record, **PREC126Handle;
* 'STR ' -8192 -- the filename of the printer driver
* 'STR ' -8189 -- the document name (always padded to 80 bytes)
The data fork of the spool file begins with a SpoolHeader structure,
followed by the pages.
short version; // should always be 1
long fileLen; // length of file including header
long fileFlags; // should always be 0
short numPages;
TPrint printRecord; // used only if PREC 3 can't be read
} SpoolHeader, *SpoolHeaderPtr, **SpoolHeaderHdle;
long pictFlags; // should always be 0
Picture thePict; // variable length
long pageOffset; // offset to the beginning of this page's
// PICT
The spool file is created by the driver in the Spool Folder or PrintMonitor Documents
folder within the System Folder (or in the current default desktop printer folder if
MacOS 8 desktop printing is active -- you can find the correct folder with FindFolder
and the kPrintMonitorDocsFolderType selector). The spool file has the name of the
document being printed. If you're putting the file directly into the desktop printer
folder, you'll need to append "(print)" to the filename (which means you may need to
shorten the document name). Spool files that are being written have a type of '?job'
and a creator of 'prmt'. Once the file is completely written, the driver changes the type
to 'pjob'. When the version of Desktop Printing that supports third-party drivers is
available, your driver also needs to send an Apple event to the Finder telling it that a
new spool file was created (more on this below).
When PrintMonitor (or Desktop PrintMonitor) prints a job, it calls the driver's
PrOpen routine and then calls PrGeneral with the structure shown below (see the
DTPNotification.h header file, available on MacTech's web site at
http://www.mactech.com/magazine/features/filearchives.html, for specifics).
PrintMonitor then calls PrOpenDoc with a pIdleProc that the driver needs to call
periodically. PrOpenPage and PrClosePage will get called for each page of the
document, and the page will be printed to the driver.
// Function prototypes
typedef pascal void (*AsynchErrorNotificationProcPtr)
(StringHandle string);
typedef pascal void (*EndNotificationProcPtr) ();
typedef pascal Boolean (*InForegroundProcPtr) ();
typedef pascal void (*StatusMessageProcPtr) (StringHandle string);
const short kPrintMonitorPrGeneral = -3;
short iOpCode; // kPrintMonitorPrGeneral
short iError;
long iReserved; // 0 = PrintMonitor,