Sunday, March 11, 2007

It's Time for Carbon Apps to Support RTF on the Clipboard

I don't know when the 'styl' resource was defined, but I do know it is very, very old. For those not in the know, back in the days of the Mac II, if you wanted to copy and paste bold/italic/underlined/colored/text from your application to another, you put both a 'TEXT' Handle on the clipboard, and a Handle filled with individual style records (the 'styl' flavor), with each record corresponding to a range of the raw text in the 'TEXT' resource. You could even put in foreign words and phrases by switching to lets say a Japanese font. This was what was done in 1987, it was what was done in 1997, and it is what most Carbon apps still do in 2007. I fired up iTunes Friday, copied something from a dialog, and looked at what was on the clipboard: a 'TEXT' resource and a 'styl' resource.

Well, what's wrong with it?

Here is an individual style record from TextEdit.h:
struct ScrpSTElement {
long scrpStartChar;
short scrpHeight;
short scrpAscent;
short scrpFont;
StyleField scrpFace;
short scrpSize;
RGBColor scrpColor;
};

And here are the things you can put in a StyleField:
normal = 0,
bold = 1,
italic = 2,
underline = 4,
outline = 8,
shadow = 0x10,
condense = 0x20,
extend = 0x40

What's wrong?

  • No superscript or subscript, not even one level. Try pasting data from a scientific app to MS Word. H+2SO4-2 becomes H+2SO4-2.
  • It takes an old fashioned QuickDraw font ID, not an ATSUI ID, so it may be difficult to map to the fonts you're actually using.
  • No paragraph alignment information. You can have any justification you want as long as it's left.
  • Text encoding is by necessity implied by the QuickDraw font encoding.
  • The OS has to go through and endian swap every element before pasting across Rosetta.


So what is the alternative?

There is nothing preventing you from supporting the 'RTF ' (Rich Text Format) flavor on the clipboard. It's a Microsoft invention, so you know Word will like it, and it satisfies all the problems I enumerated above. Plus, it's the favored flavor of any Cocoa application, so your text should be more accurately transferred to newer applications.

It's just a matter of implementing it. As I was already mixing in Cocoa into my Carbon application, I built up an NSAttributedString, extracted the string's content using the RTFFromRange:documentAttributes message and stored that raw data in a Handle. When I, inversely, wanted to paste, I extracted styled text information from an NSAttributedString created from the data in an RTF Handle. I did it all in a day for my application. (Sorry, can't share the code.) This has the added advantage of using the exact RTF flavor Cocoa applications get for free.

Alternatively, you could pretty easily create simple RTF documents from scratch using the description of the format in O'Reilly's RTF Pocket Guide. Parsing such documents looks to be a lot harder, but doable, just ignore the tags you aren't interested in.

I could hardly be happier with the result. My users will be getting a nice little present come the next release, and I'm one step closer to expunging the last bit of QuickDraw from my application. If you are maintaining a Carbon application which has anything but the simplest text formatting requirements, do your users a favor and look into supporting the 'RTF ' clipboard flavor.
[Entry has been edited for content since first publication]