TweetFollow Us on Twitter

December 96 - Print Hints: Safe Travel Through the Printing Jungle

Print Hints:
Safe Travel Through the Printing Jungle

Dave Polaschek

Implementing printing in a Macintosh application should be pretty straightforward, right? There are currently 18 high-level printing calls (listed on pages 9-92 and 9-93 of Inside Macintosh: Imaging With QuickDraw), which is only three more than were listed in Inside Macintosh Volume II. Calling them in the right order gives you a printing port that you can treat just like a graphics port -- and every Macintosh application knows (or at least ought to know) how to draw into a graphics port.

But in spite of this apparent simplicity, there are an astounding number of Macintosh applications that have problems printing. (Even products from Apple make the list once in a while.) I think one of the reasons for this is that, while basic QuickDraw printing is simple, printing is something that can be -- and has been -- made more complex by various "extensions" to the original printing architecture. These extensions offer greater control of the printing process, allowing you to take advantage of special features available on some printers and to draw in more sophisticated ways than QuickDraw allows. But they also introduce complexities that can get you in trouble if you're not careful. In this column I'll give a few examples of places where control comes only at the price of complexity, and therefore places where you need to tread very carefully, if at all.


PICTURE COMMENTS

Picture comments are, on the face of it, wonderful things. They let you embed commands in your output that can take advantage of particular printer features if they're available, and they're automatically ignored by printer drivers that don't support them. But there's a flip side: for every picture comment you use, you have to provide an alternative for those printers that don't support it. There are also a number of picture comments that should be avoided, as listed on page B-40 of Inside Macintosh: Imaging With QuickDraw. As with any complex and powerful tool, the potential for getting things wrong with picture comments is ever-present.

The SetLineWidth picture comment is a perfect example: not only is it supported by only a few printer drivers, but it's implemented slightly differently in each of them. On some printers the value you pass for the line width is used to modify the current line width (for instance, passing 1/2 will halve the current line width), and on others it's used as an "absolute" value (passing 1/2 will set the line width to 1/2 point, regardless of the previous width). To obtain the desired results, you have to write your code very carefully, and even then the SetLineWidth picture comment may not work on the printer driver that the user happens to be using -- and there's no QuickDraw alternative. The territory here is treacherous. Unless you really need fractional line widths, it may be better to take the nice safe QuickDraw path.


PRGENERAL

The PrGeneral call added complexity to the Printing Manager -- and even more complexity could be added by driver developers, often without accompanying documentation. After all, since supporting the various PrGeneral opcodes (in fact, supporting PrGeneral itself) is optional, printer drivers can define their own new opcodes and nobody need be the wiser -- nobody, that is, except for the one developer who needs the new opcode and the functionality it provides. Things get even more confusing when the same added functionality is available via a different mechanism in a different printer driver, so the application has to start using special-case code for each printer driver it knows about. If you find yourself writing special-case code for particular printer drivers, stop! Back up and look for another solution.

One commonly used PrGeneral capability, provided by the getRslOp and setRslOp opcodes, is finding the resolution(s) supported by the printer you're using and setting the resolution you want to print with. There's clearly a need for this sort of capability. An application that shows graphs of curves or of raw data gathered from some source wants the graphs to look good. Plotting individual pixels at 72 dpi doesn't make for smooth-looking curves, so an application might be justified in asking to print at the highest resolution the printer is capable of. But is PrGeneral the right approach?

A potential problem with using PrGeneral to get and set resolutions is that you're depending on the printer driver to keep up with the times. The LaserWriter driver, for example, is used for printers from the original LaserWriter all the way up to high-end typesetters. The driver reports that the maximum physical resolution of the printer is 300 dpi, even if you're printing to a typesetter that's capable of 1270 or even 2540 dpi. The reason for this is that reporting a higher resolution could cause applications that create bitmaps at the printer's resolution to run into QuickDraw's limitations, such as the limit on rowBytes and the 32K maximum region size. This is something that we plan to address in future versions of LaserWriter 8, but currently an application that wants to know a PostScript(TM) printer's real maximum resolution has to either parse the PostScript Printer Description file (PPD) associated with it or query the printer directly, both of which are functions that the driver should have to worry about, not the application.

In this case, there's an alternative to PrGeneral: If you're going to be generating your data in a GWorld, just make sure the GWorld's resolution is whatever you need for best results. Then take that same GWorld and use CopyBits to copy the PixMap in it to the printer. If you provide appropriate source and destination rectangles, the implementation of CopyBits in the printer driver will scale the PixMap, and you'll be taking advantage of the resolution of the printer without having to worry about new coordinate systems.

Determining just what resolution you need is, however, still a tricky issue. For example, if you're printing a color image to a LaserWriter that can print only black-and-white images and only at 300 dpi, the color image you're displaying onscreen already has more detail than the printer can reproduce, so you don't need to worry about sending a higher-resolution image at all. The way to tell for sure if you have enough data is that your pixel density (in dpi) should be between one and two times the "screen frequency" (in lpi) for the printer. The default screen frequency for PostScript printers is listed in the PPD file for the printer, and in the future we'll be providing access to the PPD file parsing code that's contained in LaserWriter 8's PrintingLib, but for now you may just want to ask the user rather than parse it out yourself.

If you're generating line art or other data that needs to have "hard edges" in a GWorld that's going to be sent to the printer, you've got a different problem: unless you specify the data at the printer's resolution (or higher), it will need to be scaled up to the printer's resolution, producing large, blocky pixels. Your users will think you're a bozo, unless of course your product is supposed to make large, blocky pixels. The right solution is to avoid sending data that needs to have hard edges as bitmapped images, if at all possible. This is the sort of data that really should be maintained as objects. If you want to draw the letter A, for instance, ask QuickDraw to draw it to the printer for you if possible, rather than image it into a bitmap first. If you really need to generate bitmaps of hard-edged data, be aware that you'd better have your machete sharpened and ready, since you're heading into the brush. On the other hand, this may be a great opportunity to generate your own PostScript code.


POSTSCRIPT CODE

Generating your own PostScript code is another powerful technique that can get your application into trouble. In many cases, it's the right answer to a thorny dilemma; for instance, if you need drawing primitives that QuickDraw doesn't supply, this may be the only way to get them. After all, cubic Bézier curves are neat and powerful. The problem arises when the application developer either doesn't understand how to write compatible PostScript code or takes shortcuts in the PostScript code.

An excellent example is an old version of a certain very popular graphics program that saved its pictures with an EPS version of the graphic embedded in the PICT data. Unfortunately, the PostScript code in the EPS version depended on the md dictionary, a private dictionary used by the LaserWriter driver. After warning developers for years that the md dictionary was private, Apple Engineering felt justified in changing it. When the new version of the LaserWriter driver shipped, suddenly many graphics quit printing. The problem was made even worse by the fact that many of these graphics had been shipped as clip art, and they still occasionally pop up to bedevil us today.

The solution isn't to avoid PostScript code entirely. Just make sure that if you do generate it, the code is compatible and portable. Obviously, it shouldn't use any of the LaserWriter driver's private PostScript operators. If you make graphics with PostScript code embedded in them, be sure that the PostScript code they contain conforms to the EPS specification that's described by Adobe(TM) in the PostScript Language Reference Manual, Second Edition, Appendix H. Also, be sure to send the PostScript code with the PostScriptBegin, PostScriptHandle, and PostScriptEnd picture comments, as described beginning on page B-38 of Inside Macintosh: Imaging With QuickDraw.

Another thing application developers have tried over the years (with mixed success) is to do their own PostScript font management by talking directly to the printer. This is something applications really need to avoid. The LaserWriter driver knows how to handle the PostScript fonts needed to print a page (or series of pages). Applications that attempt to manage the fonts themselves are more likely to get poor font management for their efforts, since the LaserWriter driver (or the LaserWriter GX driver) will have a much harder time recognizing which fonts are needed on which page. When this happens, the drivers err on the side of safety: if there's any doubt about when a font is used, it will be included for the whole job, which is usually exactly what the developer was trying to avoid. Let the driver handle font management.


HAVE A SAFE TRIP

I've given a few of the more common examples of how printing has grown in complexity over the years, and how application developers can sometimes get in trouble by trying to take advantage of it. Printing doesn't need to be much harder than drawing to the screen if you stick to the rules, and even when you want to take advantage of particular printer capabilities, you can usually do so in safe, compatible ways. As tempting as it sometimes is to wander off the known path and plunge headlong into the uncharted jungle of possibilities, doing so usually just results in trouble -- for you and for your users. In printing, as in all programming, remember: keep it simple.


    RECOMMENDED READING

    • Technote PR 10, "A Printing Loop That Cares."

    • Writing Solid Code by Steve Maguire (Microsoft Press, 1993).

    • The History of the English-Speaking Peoples (4 volumes), by Sir Winston S. Churchill (Dodd, Mead, & Co., 1958 & 1959).


DAVE POLASCHEK (davep@best.com), formerly of Apple's Developer Technical Support group, got so confused by the lack of weather in California that he moved back to Minnesota. This probably won't seem like such a smart move when Celsius and Fahrenheit show the same temperature and Dave starts singing that verse from Jimmy Buffett's "Boat Drinks" that goes, "This morning, I shot six holes in my freezer. I think I've got cabin fever. Somebody sound the alarm."*

Thanks to Rich Blanchard, Paul Danbold, Dan Lipton, and Steve Simon for reviewing this column.*

 
AAPL
$108.00
Apple Inc.
+1.02
MSFT
$46.95
Microsoft Corpora
+0.90
GOOG
$559.08
Google Inc.
+8.77

MacTech Search:
Community Search:

Software Updates via MacUpdate

Vitamin-R 2.20b1 - Personal productivity...
Vitamin-R creates the optimal conditions for your brain to work at its best by structuring your work into short bursts of distraction-free, highly focused activity alternating with opportunities for... Read more
Dropbox 2.10.44 - Cloud synchronization...
Dropbox is an application that creates a special Finder folder that automatically syncs online and between your computers. It allows you to both backup files and keep them up-to-date between systems... Read more
Sandvox 2.9.2 - Easily build eye-catchin...
Sandvox is for Mac users who want to create a professional looking website quickly and easily. With Sandvox, you don't need to be a Web genius to build a stylish, feature-rich, standards-compliant... Read more
Cocktail 8.0.1 - General maintenance and...
Cocktail is a general purpose utility for OS X that lets you clean, repair and optimize your Mac. It is a powerful digital toolset that helps hundreds of thousands of Mac users around the world get... Read more
LibreOffice 4.3.3.2 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
VMware Fusion 7.0.1 - Run Windows apps a...
VMware Fusion allows you to create a Virtual Machine on your Mac and run Windows (including Windows 8.1) and Windows software on your Mac. Run your favorite Windows applications alongside Mac... Read more
OneNote 15.3.2 - Free digital notebook f...
OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that's too important to forget. Whether you're at... Read more
Audio Hijack Pro 2.11.4 - Record and enh...
Audio Hijack Pro drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio with Audio Hijack... Read more
Iridient Developer 3.0.0 beta 3 - Powerf...
Iridient Developer (was RAW Developer) is a powerful image conversion application designed specifically for OS X. Iridient Developer gives advanced photographers total control over every aspect of... Read more
TextWrangler 4.5.11 - Free general purpo...
TextWrangler is the powerful general purpose text editor, and Unix and server administrator's tool. Oh, and also, like the best things in life, it's free. TextWrangler is the "little brother" to... Read more

Latest Forum Discussions

See All

Monster Flash Review
Monster Flash Review By Jordan Minor on October 31st, 2014 Our Rating: :: ALONE IN THE DARKUniversal App - Designed for iPhone and iPad Solid shooting and a surprising amount of spooky tension make Monster Flash a great portable... | Read more »
Retry Review
Retry Review By Rob Thomas on October 31st, 2014 Our Rating: :: SOARING HIGHUniversal App - Designed for iPhone and iPad Flappy who? Let Retry wash all those bad bird-related memories away on a cool retro-flavored flight… right... | Read more »
Dementia: Book of the Dead Review
Dementia: Book of the Dead Review By Lee Hamlet on October 31st, 2014 Our Rating: :: A TOUGH READUniversal App - Designed for iPhone and iPad A witch hunter is sent after a demonic book in the spooky but short-lived Dementia: Book... | Read more »
Card Dungeon, the Semi-Board Game Roguel...
Card Dungeon, the Semi-Board Game Roguelike, Has Been Renovated Posted by Jessica Fisher on October 31st, 2014 [ permalink ] | Read more »
Logitech Protection + Power iPhone5/5S C...
Made by: Logitech Price: $99.99 Hardware/iOS Integration Rating: 3 out of 5 stars Usability Rating: 0.5 out of 5 stars Reuse Value Rating: 0.75 out of 5 stars Build Quality Rating: 0.75 out of 5 stars Overall Rating: 1.25 out of 5 stars | Read more »
This Is Not a Test Goes Free, Permanentl...
This Is Not a Test Goes Free, Permanently Posted by Jessica Fisher on October 31st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Swap Heroes Review
Swap Heroes Review By Campbell Bird on October 31st, 2014 Our Rating: :: STRATEGIC SWAPPINGUniversal App - Designed for iPhone and iPad Rotate a cast of heroes to fend of waves of monsters in this difficult, puzzle rpg.   | Read more »
Night Sky Pro™ (Reference)
Night Sky Pro™ 3.0.1 Device: iOS Universal Category: Reference Price: $2.99, Version: 3.0.1 (iTunes) Description: Night Sky Pro™Wonder No More™ Night Sky Pro™ is the ultimate stargazing experience. From the creators of the original... | Read more »
Audio Defence : Zombie Arena (Games)
Audio Defence : Zombie Arena 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: A zombie shooter audio game. Made from gut-wrenching 3D binaural sound, for a new kind of weird immersion. You... | Read more »
RPG Asdivine Hearts (Games)
RPG Asdivine Hearts 1.1.0 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1.0 (iTunes) Description: SPECIAL PRICE50% OFF (USD 7.99 -> USD 3.99)!!! Travel alongside four companions and a cat in the adventure of a... | Read more »

Price Scanner via MacPrices.net

Tablets Ascendent Again; Global Tablet Market...
The worldwide tablet grew 11.5% year over year in the third quarter of 2014 (3Q14) with shipments reaching 53.8 million units according to preliminary data from the International Data Corporation (... Read more
Apple now offering refurbished 2014 13-inch R...
The Apple Store is now offering Apple Certified Refurbished 2014 13″ Retina MacBook Pros for up to $270 off the cost of new models. An Apple one-year warranty is included with each model, and... Read more
Apple Regains Momentum As Windows Stutters An...
The latest smartphone sales data from Kantar Worldpanel ComTech, for the three months to March 2014, shows Apple performing strongly in the first quarter of the year, with sales bouncing back in... Read more
Worldwide Smartphone Shipments Increase 25.2%...
New smartphone releases and an increased emphasis on emerging markets drove global smartphone shipments above 300 million units for the second consecutive quarter, according to preliminary data from... Read more
Apple now offering refurbished 2014 15-inch M...
The Apple Store is now offering Apple Certified Refurbished 2014 15″ Retina MacBook Pros for up to $400 off the cost of new models. An Apple one-year warranty is included with each model, and... Read more
Apple drops prices on refurbished 2013 Retina...
The Apple Store has dropped prices on 2013 Apple Certified Refurbished 13″ and 15″ Retina MacBook Pros, with Retina models now available starting at $999. Apple’s one-year warranty is standard, and... Read more
New 2.8GHz Mac mini on sale for $949, save $5...
Abt Electronics has the new 2.8GHz Mac mini in stock and on sale for $949.05 including free shipping. Their price is $50 off MSRP, and it’s the lowest price available for this model from any reseller... Read more
Sale! 3.7GHz Quad Core Mac Pro available for...
 B&H Photo has the 3.7GHz Quad Core Mac Pro on sale for $2649 including free shipping plus NY sales tax only. Their price is $350 off MSRP, and it’s the lowest price for this model from any... Read more
Mujjo Steps Up The Game With Refined Touchscr...
Netherlands based Mujjo have just launched their Refined Touchscreen Gloves, stepping up their game. The gloves feature a updated elegant design that takes these knitted gloves to the next level. A... Read more
Sale! Preorder the new 27-inch 5K iMac for $2...
 Abt Electronics has the new 27″ 3.5GHz 5K iMac on sale and available for preorder for $2374.05 including free shipping. Their price is $125 off MSRP, and it’s the lowest price available for this... Read more

Jobs Board

Position Opening at *Apple* - Apple (United...
…Summary** As a Specialist, you help create the energy and excitement around Apple products, providing the right solutions and getting products into customers' hands. You Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** Being a Business Manager at an Apple Store means you're the catalyst for businesses to discover and leverage the power, ease, and flexibility of Apple Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** As more and more people discover Apple , they visit our stores seeking ways to incorporate our products into their lives. It's your job, as a Store Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** At the Apple Store, you connect business professionals and entrepreneurs with the tools they need in order to put Apple solutions to work in their Read more
Solutions Specialist with *Apple* Knowledge...
Company Description: We are an Apple Authorized Sales and Service Provider. We have been selling and servicing Apple computers in the Fairfield County area for over Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.