TweetFollow Us on Twitter

The Perils Of PostScript--The Sequel

The Perils Of PostScript--The Sequel


Developers are discovering the advantages of using PostScript ® dictionaries in applications, but along with the advantages come some perils. One peril awaits if you download a dictionary using PostScriptHandle. Another can trip you up after downloading a dictionary if you then download a font using the SetFont procedure I described in develop, Issue 1. How to avoid these perils? Read on to learn some tricks for dicts in picts.

More and more developers are beginning to use direct PostScript code in their applications. In my "Perils of PostScript" article in develop, Issue 1, I addressed a couple of problems that arise when you use PostScript code to print documents. In this sequel, we'll look at some problems you will encounter if you attempt to use PostScript dictionaries in your applications.


A PostScript dictionary is a collection of predefined variables and/or procedures. Using a PostScript dictionary can significantly reduce the size of the PostScript code generated by your application and make it more efficient. For instance, consider a large PostScript file in which the operatorcurrentpoint is used frequently. You can define in your dictionary a PostScript procedure called cp that makes a call to the currentpoint operator. You can then replace currentpoint with cp throughout the file, thus reducing its size. Similarly, by defining a PostScript procedure to represent a series of operators, you can express a compound operation much more efficiently. And storing procedures in a dictionary that you create can also prevent you from inadvertently redefining something that has already been defined.

One great example of a PostScript dictionary is the one used by the LaserWriter ® driver, variously called LaserPrep (after the file it resides in, at least until System 7), AppleDict (the Apple name for it), and good ol' md (the PostScript name for it, and the one I prefer to use). The LaserWriter driver generally uses one or more md

routines to perform a particular QuickDraw operation. (See the sidebar on the next page for a review of how the LaserWriter driver works.) For example, a call to the QuickDraw CopyBits routine is translated by the driver into a call to the db or cdb operators stored in md. As another example, during font downloading the LaserWriter driver uses bn and bu , both stored in md, to call save and restore ( bn calls save and bu calls restore).If you want to record a piece of PostScript code that references procedures contained in a dictionary, you must also record the dictionary. I describe how to download a dictionary, and how to avoid the pitfalls involved, in the next section.

Once your dictionary has been downloaded, you should be able to continue to reference it until the end of the job. But alas, this is not so, at least until the new printing architecture ships sometime after System 7. Under the current architecture, font downloading interferes with PostScript dictionaries. I discuss this problem and how to get around it under "The Perils of Font Downloading."


One of the easiest methods for downloading a PostScript dictionary is by using the PostScriptHandle picture comment. You can use this comment to download directly to the LaserWriter a block of PostScript code stored in a handle. (See Technical Note #91, Optimizing for the LaserWriter--PicComments, for more information.) When you use the PostScriptHandle comment, you must insert the PostScriptBegin and PostScriptEndpicture comments around the block of PostScript code you are trying to download, like this:

PicComment(PostScriptBegin, 0, NIL);
(*** Your PostScript representation of document goes here.***)
PicComment(PostScriptHandle, size, handle);
(*** Your QuickDraw representation of document goes here.***)
PicComment(PostScriptEnd, 0, NIL);

As described in my first article, the PostScriptBegin/Endcomments are markers that ensure that the right piece of code will execute on the right device. When the LaserWriter driver sees PostScriptBegin, it ignores all QuickDraw drawing calls and just executes picture comments. When a PostScriptEndis received, the LaserWriter driver will once again interpret QuickDraw calls. So when printing to a LaserWriter printer, only the picture comments are executed, while the QuickDraw code between PostScriptBeginand PostScriptEnd is ignored. But PostScriptBegin and PostScriptEnd also save and restore at least part of the state of the device, which can cause problems for your dictionary. To avoid this, you should use the picture comment PostScriptBeginNoSave (comment kind = 196) to prevent the save and restore from occurring, like this:

(*** Your definition of the dictionary goes here.***)

PicComment(PostScriptBeginNoSave, 0, NIL);
PicComment(PostScriptHandle, dictsize, dicthandle);
PicComment(PostScriptEnd, 0, NIL);
(*** Now you send the document. ***)

PicComment(PostScriptBegin, 0 NIL);
(*** Your PostScript representation of document goes here.***)
PicComment(PostScriptHandle, size, handle);
(*** Your QuickDraw representation of document goes here.***)
PicComment(PostScriptEnd, 0, NIL);

If you don't need to export your dictionary into picture files, you can get the LaserWriter driver to auto-download your dictionary by keeping the dictionary code in a PREC(103) resource. After the LaserWriter has saved its state, it does a blind GetResource (that is, from any open resource file) on PREC(103). If one is found, it is downloaded to the printer after the md dictionary, and before the job. You can use this method of downloading for dictionaries that are used only to contain state information about the current job. (When a graphic is copied onto the clipboard, only the state information, not the entire dictionary, is required for the code to execute.)

However, use of the PREC(103) resource does have some limitations. It only works at print time, and there can be only one. That is, the LaserWriter driver does not attempt to download all the PREC(103) resources in all the open resource files. The first one it finds wins. (This method of downloading dictionaries is documented in Technical Note #192, Surprises in LaserWriter 5.2 and Newer.)


In my "Perils of PostScript" article in Issue 1 of develop, I showed a small procedure called SetFont that downloaded a font using QuickDraw, while maintaining the ability to reference that font using PostScript. The problem with that method is that the process of font downloading executes the PostScript restore operator. This operator restores the state of the printer to a state that was saved before your dictionary was defined. Because of this, any reference to your dictionary is lost.

Another way to understand what happens in this case is to look at what the LaserWriter driver does during printing. At the start of a print job, the LaserWriter driver configures the LaserWriter's graphics state to look more like QuickDraw. This includes moving the origin (0,0) from the bottom left (PostScript style) to the top left (QuickDraw style), and setting the default resolution to 72 dpi. After the driver has configured the printer, it performs a save, which saves the complete state of the device. The driver then begins downloading the rest of the job, containing the PostScript code generated by the LaserWriter as well as any additional PostScript code sent by the application.

The LaserWriter driver fully restores the state of the device, by executing the PostScript restore operator, before downloading a font. During font downloading, the characters of the font are actually defined, sometimes using normal PostScript drawing operators. Because of this, the LaserWriter driver restores the state of the printer before defining the characters. Once the characters have been defined, the state is saved again. This way, the LaserWriter driver can assume it knows the state of the device. Since the state saved by the LaserWriter driver does not contain any of the symbols defined by the application, all of them are lost after any attempt to download a font.

Now that we understand the problem, let's discuss potential solutions. The restore operator affects everything that has changed except two areas: some of the PostScript stacks (specifically the operand, dict, and execution stacks), and the contents of PostScript strings. This suggests that to save small units of information, you can simply push them onto the stack, or convert them and store them as PostScript strings.

Unfortunately, it's not quite that easy.

PostScript makes a distinction between simple and composite objects. Simple objects (like numerical values and booleans) contain their value within the object. Composite objects (like strings, procedures, and dictionaries) contain only a pointer to the real data, which is stored elsewhere in PostScript Virtual Memory. Simple objects on the stack are indeed preserved across a restore, but if there are composite objects on the stack that are new (that is, newer than the state being restored), an invalidrestore error is generated. If your dictionary only contains simple objects, then you can indeed push each of the variables defined in the dictionary onto the stack separately and rebuild the dictionary after the restore. The overhead here is obviously enormous, though, and most useful dictionaries contain procedures and/or strings, rendering this technique useless.

In the case of strings being preserved across a restore, let me quote from the PostScript Language Reference Manual , p. 44: "In the current PostScript design, restore actually does not undo changes made to the elements of strings. We consider this behavior to be a defect, and do not recommend that PostScript programs take advantage of it." Beyond this easily ignored admonishment, though, is another problem. The strings in question must be preexisting: strings you create just before the restore will, of course, be destroyed by the restore, or, if they are on the stack, will cause an error. You could probably find some scratch strings in one of the standard dictionaries to use, but this is not recommended, for obvious reasons.

Another way to solve the problem would be to redefine save and restore to not do anything. This way, font downloading would not cause the state to be restored. This would make the application developer responsible for preserving the state, which is easily done using other PostScript operators. But unfortunately, the definitions of save and restore cannot be changed without exiting the server loop. That is, you cannot override their definitions from within a job. Because of this, you have to fall back on plan B: override the operators that call save and restore. In the case of font downloading, these operators are bn and bu, as mentioned earlier.

This method is the most widely used solution to our problem, has the fewest limitations, and is the method recommended here. Please note, however, that tinkering with md operators outside of this specific use is strongly discouraged. (See the sidebar on the next page.)

The main job of bu and bn is to preserve the state of the PostScript device. As long as your PostScript code preserves the state, these calls aren't even required. In the fragment that follows, we first create our own dictionary, called mydict, with room for ten symbols, although we don't define them all. Next we define killbu. killbu is responsible for first saving the old definition of the buroutine, and then setting its value to the empty procedure ( {}), which does nothing. The original definition of bu is simply pushed onto the stack. Next we write a routine restorebu, to restore the definition of bu when we are through. This routine is responsible for popping the original value off the stack and storing it back into the bu symbol; it assumes that the definition of bu is on the top of the stack. Then we define two similar routines, killbn and restorebn, which take care of the bn operator. Finally, we define a fun little routine to call to make sure our dictionary is actually being preserved after font downloading. We call this one titleshow. So now we have a dictionary, all ready to use.

SendPostScript('/mydict 10 dict def');
SendPostScript('mydict begin');
SendPostScript('/killbu {//md /bu get //md /bu {} put} def');
SendPostScript('/restorebu {//md exch /bu exch put} def');
SendPostScript('/killbn {//md /bn get //md /bn {} put} def');
SendPostScript('/restorebn {//md exch /bn exch put} def');
SendPostScript('/titleshow {dup gsave');
SendPostScript('currentscreen 3 -1 roll pop 120 3 1 roll setscreen');
SendPostScript('.5 setgray show grestore true charpath gsave');
SendPostScript('1 setlinewidth 0 setgray stroke grestore');
SendPostScript('.5 setlinewidth 1 setgray stroke }def');

Okay, now that we have the routines for killing bu and bn, we need to call them. It's very important at this point to check for their existence before attempting to alter their definitions. This is because, as mentioned earlier, the new printing architecture that will ship sometime after System 7 will handle font downloading differently. The bu and bn operators will no longer exist; in fact, it's not clear that the md dictionary will still exist. The following PostScript commands check for the existence of both the dictionary and the symbol. If they don't exist, our code assumes it is running under the new printing architecture, and does nothing to insulate the dictionary. The code fragment executes fine on LaserWriter drivers up to and including System 7.0. It has also been tested in both foreground and background. Considering the future of bn and bu, it is very likely that this code will continue to work even under the new printing architecture. Here, then, is the code to check for and kill bn and bu:

SendPostScript('mydict begin');
SendPostScript('//md /bu known {killbu} if');
SendPostScript('//md /bn known {killbn} if');

Pretty straightforward: if the routine exists, call the correct routine to kill it. The most important thing to note here is the order of the routines. Since killbuand killbnpush things onto the stack, restorebu and restorebnmust be called in opposite order to get the correct results. So after the job is finished, we call:

SendPostScript('mydict begin');
SendPostScript('//md /bn known {restorebn} if');
SendPostScript('//md /bu known {restorebu} if');


PostScript dictionaries are useful because they can significantly reduce the size of the PostScript code generated by your application, and can be exported into pictures. Perhaps the easiest way to record PostScript into a picture is by using the PostScriptHandlepicture comment. In this case, remember to use the PostScript BeginNoSave comment to prevent PostScriptBegin and PostScriptEnd from saving and restoring at least part of the state of the device, which can cause problems for your dictionary. To prevent font downloading from interfering with your PostScript dictionaries, you can override bn and bu, the PostScript operators that call save and restore. Outside of this solution, you should absolutely avoid using md operators.

The code included in the Perils of PS II folder on the Developer Essentials disc is basically the same code that has been shown here, rolled into an application shell that opens and initializes the Printing Manager. Also included is the definition of the SendPostScriptprocedure referenced in this article.


The LaserWriter driver is a complex piece of software that handles communications between an application and the LaserWriter printer. To print a document, the application opens the Printing Manager, which in turn loads and initializes the LaserWriter driver. The application then makes standard QuickDraw calls similar to those used to render the document on the screen. The LaserWriter driver intercepts these calls and converts them into the equivalent PostScript code for rendering the document on the LaserWriter printer.

(See Figure 1.) In some cases, one QuickDraw operation translates into one PostScript operation, but more frequently, the QuickDraw operation translates into several PostScript operations. To abbreviate these operations, the LaserWriter driver stores them as procedures in a PostScript dictionary.

When the LaserWriter driver first connects to the LaserWriter printer, it checks to see if its dictionary exists and if the version of the dictionary matches the version of the driver being used. If not, it downloads the correct dictionary before proceeding. (This is what the message "initializing printer" means when you print for the first time after turning on the printer.)

Once the correct dictionary is in place, the job of translation becomes much easier. Each QuickDraw operation now becomes one line of PostScript code, referencing a procedure defined by the dictionary. Complex QuickDraw operations (like font downloading) still require many lines of PostScript code, but in general, the translation is one for one. Since the QuickDraw code is translated rather than rendered, the LaserWriter driver doesn't need to spool the data to disk. Instead, each operation is translated and sent to the printer as it is received.

[IMAGE postscript1.GIF]

Figure 1 How the LaserWriter Driver Works


Many developers have started to call md routines from within the PostScript code generated by their applications. This is dangerous, for a number of reasons.

The first is that the md dictionary is defined and maintained by the LaserWriter driver. This means that it is always subject to change, and code that depends on the md dictionary must be version dependent. This is possible, but far from elegant.

Another problem with using md operators is that they may not work the same way on all devices. Remember that the LaserWriter driver is used to drive a lot more devices than just an Apple LaserWriter.

Use of md operators has already led to compatibility problems with major applications, and most developers have realized the danger in using them. The easiest way to avoid problems with these routines is to not call them. If you really need the functionality of a particular md operator, simply redefine it in your own dictionary. Using tools like LaserTalk (formerly from Emerald City Software, now from Adobe), you can "disassemble" md operators back to their PostScript primitives. You can then redefine them using a different name in your own dictionary. Now you have a routine that does exactly what the md routine did, but you remain in control of its definition. Most of the md operators are very small, so the storage penalty of redefining them in your own dictionary is minimal.

Now that I've warned you, I'm going to show you how to tinker with two operators stored in md: bn and bu. All routines, including these two, are subject to change; by special arrangement with engineering, bn and bu will change in a compatible way, but this isnot true for any of the other routines defined in md. This article shows a specific use of bn and bu, and checks for their existence before attempting to access them. This is not meant to endorse other uses of these or any other md routines.

SCOTT "ZZ" ZIMMERMAN is a DTS printing guru. After two and a half years at Apple he says he's particularly impressed with the strictly enforced dress code. In his spare time he sails, scuba dives for lobsters, and plays the piano, guitar, and saxophone. Zz has a penchant for pets. His doorway is adorned by a melted gummy rat, a good luck charm left over from his Intel days. At home, atop his monitor is perched a rare Asian black scorpion (behind glass, we hope). His other cuddly pets include two geckos and a lovable iguana. *

The definitive references on the PostScript language are the PostScript Language Reference Manual (Addison- Wesley, 1985, available from APDA--#T0182LL/A), the PostScript Language Tutorial and Cookbook (Addison-Wesley, 1985), and PostScript Language Program Design (Addison-Wesley, 1988). *

Thanks to Our Technical Reviewers Pete "Luke" Alexander, Jay Patel, David Williams*


Community Search:
MacTech Search:

Software Updates via MacUpdate

GarageSale 7.0.7 - Create outstanding eB...
GarageSale is a slick, full-featured client application for the eBay online auction system. Create and manage your auctions with ease. With GarageSale, you can create, edit, track, and manage... Read more
SpamSieve 2.9.28 - Robust spam filter fo...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
Thunderbird 45.7.1 - Email client from M...
As of July 2012, Thunderbird has transitioned to a new governance model, with new features being developed by the broader free software and open source community, and security fixes and improvements... Read more
Opera 43.0.2442.991 - High-performance W...
Opera is a fast and secure browser trusted by millions of users. With the intuitive interface, Speed Dial and visual bookmarks for organizing favorite sites, news feature with fresh, relevant content... Read more
OnyX 3.2.4 - Maintenance and optimizatio...
OnyX is a multifunction utility that you can use to verify the startup disk and the structure of its system files, to run miscellaneous maintenance and cleaning tasks, to configure parameters in the... Read more
VueScan 9.5.71 - Scanner software with a...
VueScan is a scanning program that works with most high-quality flatbed and film scanners to produce scans that have excellent color fidelity and color balance. VueScan is easy to use, and has... Read more
Slack 2.5.1 - Collaborative communicatio...
Slack is a collaborative communication app that simplifies real-time messaging, archiving, and search for modern working teams. Version 2.5.1: New The way we load teams you don't view often has been... Read more
HandBrake 1.0.3 - Versatile video encode...
HandBrake is a tool for converting video from nearly any format to a selection of modern, widely supported codecs. Features Supported Sources VIDEO_TS folder, DVD image or real DVD (unencrypted... Read more
Vivaldi 1.7.735.46 - An advanced browser...
Vivaldi is a browser for our friends. In 1994, two programmers started working on a web browser. Our idea was to make a really fast browser, capable of running on limited hardware, keeping in mind... Read more
Vivaldi 1.7.735.46 - An advanced browser...
Vivaldi is a browser for our friends. In 1994, two programmers started working on a web browser. Our idea was to make a really fast browser, capable of running on limited hardware, keeping in mind... Read more

Mudd Masher arrives this week
Atooi Games, the minds behind Totes the Goat and Mutant Mudds, have a new game in the works -- Mudd Masher. The game, a hybrid of the independent studio's first two titles, is expected to launch this week on March 2. [Read more] | Read more »
The best sales on the App Store this wee...
The App Store has quite an exciting lineup of discount games this week that range across a variety of genres. It's a great opportunity to catch up on some of the premium games you may have been holding off on -- and some you can even grab for free... | Read more »
The best new games we played this week
Ah, here we are again at the close of another busy week. Don't rest too easy, though. We had a lot of great new releases in mobile games this week, and now you're going to have to spend all weekend playing them. That shouldn't be too much of a... | Read more »
Rollercoaster Tycoon Touch Guide: How to...
| Read more »
Rabbids Crazy Rush Guide: How to unlock...
The Rabbids are back in a new endless running adventure, Rabbids Crazy Rush. It's more ridiculous cartoon craziness as you help the little furballs gather enough fuel (soda) to get to the moon. Sure, it's a silly idea, but everyone has dreams --... | Read more »
Tavern Guardians (Games)
Tavern Guardians 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Tavern Guardians is a Hack-and-Slash action game played in the style of a match-three. You can experience high pace action... | Read more »
Slay your way to glory in idle RPG Endle...
It’s a golden age for idle games on the mobile market, and those addictive little clickers have a new best friend. South Korean developer Ekkorr released Endless Frontier last year, and players have been idling away the hours in the company of its... | Read more »
Tiny Striker: World Football Guide - How...
| Read more »
Good news everyone! Futurama: Worlds of...
Futurama is finding a new home on mobile in TinyCo and Fox Interactive's new game, Futurama: Worlds of Tomorrow. They're really doing it up, bringing on board Futurama creator Matt Groening along with the original cast and writers. TinyCo wants... | Read more »
MUL.MASH.TAB.BA.GAL.GAL 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ENDLESS UPGRADES. CONSTANT DANGER. ANCIENT WISDOM. BOUNCY BALLS. Launch Sale, 40% OFF for a very limited time!!! MUL.... | Read more »

Price Scanner via

27-inch 3.3GHz 5K iMac on sale for $2099, sav...
B&H Photo has the 27″ 3.3GHz 5K Apple iMac on sale for $2099.99 including free shipping plus NY sales tax only. Their price is $200 off MSRP. Amazon also has the 27″ 3.3GHz 5K iMac on sale for $... Read more
21-inch iMacs on sale for up to $111 off MSRP
B&H Photo has select 21″ Apple iMacs on sale for up to $110 off MSRP, each including free shipping plus NY sales tax only: - 21″ 2.8GHz iMac: $1189 $110 off MSRP - 21″ 1.6GHz iMac: $999 $100 off... Read more
12-inch 1.2GHz Retina MacBooks on sale for $2...
Newegg has the 12″ 1.2GHz Space Gray Retina MacBook (sku MLH82LL/A) on sale for $1349.99 including free shipping. Their price is $250 off MSRP, and it’s the lowest price available for this model.... Read more
13-inch MacBook Airs on sale for $100 off MSR...
B&H Photo has 13″ MacBook Airs on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 13″ 1.6GHz/128GB MacBook Air (MMGF2LL/A): $899 $100 off MSRP - 13″ 1.6GHz/... Read more
9-inch 32GB Silver iPad Pro on sale for $549,...
B&H Photo has the 9.7″ 32GB Silver Apple iPad Pro on sale for $549 for a limited time. Shipping is free, and B&H charges NY sales tax only. Their price is $50 off standard MSRP for this model... Read more
13-inch 2.0GHz Apple MacBook Pros on sale for...
B&H has the non-Touch Bar 13″ 2.0GHz MacBook Pros in stock today and on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 13″ 2.0GHz MacBook Pro Space Gray (... Read more
15-inch Touch Bar MacBook Pros on sale for up...
B&H Photo has the new 2016 15″ Apple Touch Bar MacBook Pros in stock today and on sale for up to $150 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.7GHz Touch Bar... Read more
12-inch Retina MacBooks on sale for $1150, $1...
B&H has 12″ 1.1GHz Retina MacBooks on sale for $150 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 12″ 1.1GHz Space Gray Retina MacBook: $1149 $150 off MSRP - 12″ 1.1GHz... Read more
Apple restocks refurbished 11-inch MacBook Ai...
Apple has Certified Refurbished 11″ MacBook Airs (the latest models recently discontinued by Apple), available for up to $170 off original MSRP. An Apple one-year warranty is included with each... Read more
Apple Park Opens to Employees in April With T...
Apple has announced that Apple Park, the company’s new 175-acre campus, will be ready for employees to begin occupying in April. The process of moving more than 12,000 people will take over six... Read more

Jobs Board

*Apple* Solutions Consultant - Apple (United...
# Apple Solutions Consultant Job Number: 55676865 Los Angeles, California, United States Posted: Feb. 22, 2017 Weekly Hours: 40.00 **Job Summary** As an Apple Read more
Programmer/Editor *Apple* Music Dance - App...
# Programmer/Editor Apple Music Dance Job Number: 55565967 Culver City, California, United States Posted: Feb. 23, 2017 Weekly Hours: **Job Summary** Apple Music Read more
Digital Marketing Specialist - *Apple* iClo...
# Digital Marketing Specialist - Apple iCloud Job Number: 54729233 Culver City, California, United States Posted: Feb. 22, 2017 Weekly Hours: 40.00 **Job Summary** Read more
Marketing Specialist, iTunes & *Apple*...
# Marketing Specialist, iTunes & Apple Music Job Number: 55704205 Culver City, California, United States Posted: Feb. 23, 2017 Weekly Hours: 40.00 **Job Summary** Read more
*Apple* Wireless Lead - T-ROC - The Retail O...
…of knowledge in wireless sales and activations to the Beautiful and NEW APPLE Experiencestore within MACYS. THIS role, APPLE Wireless Lead, isbrandnewas MACYS Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.