On Thursday, January 30, 2003, at 12:31 PM, Dan Mills wrote:

Now, everything is fine and dandy as long as I'm using plain ol' ascii text. I set the NSText contents using the 'setString' function. But, if the text is some japanese, say, euc-jp or shift-jis or utf-8 encoded, then all I get is garbage.
That's a limitation of the current version. Let me begin by saying that I'm not very happy with the way that CB handles NSString objects right now - I made some faulty assumptions and bad decisions where that's concerned. Mea culpa.

As it stands now, NSStrings are automatically converted to and from Perl scalars. I had assumed - wrongly, as it turns out - that Perl being what it is, there was no possible reason for calling NSString's methods, so the conversion is a one-way copy - in other words, what you get back from a Cocoa method that returns an NSString object is a simple scalar, not an object.

For compatibility, this will continue to be the case in the next version of CB - at least on the surface. But, in the next version, the scalars you get from Cocoa will be tied to their NSString counterparts instead of merely copied, so you'll be able to get at the underlying NSString object using the "tied" function. This will give the best of both worlds; Perl string handling wherever that's appropriate, and Cocoa's NSString object methods when you need them.

Another problem with the current version relates, as you've found, with encoding. Whenever Perl strings are converted to NSString objects, the NSString objects are created with the initWithCString: method. Likewise, when NSString objects are converted to Perl strings, the cString method is used.

The problem with these methods is that they rely on the current locale settings to determine the encoding to use, and they've been deprecated as of 10.2 for that reason. So, the next version of CB will use the initWithUTF8String: and UTF8String methods instead whenever conversions are needed.

As I said, you'll be able to create NSString objects and work with them directly, instead of relying on automatic conversion back and forth. Another nice benefit of that is, if you have a chunk of bytes that aren't encoded with UTF8, you'll be able to create an NSString object with the initWithData:encoding: method. Conversely, if you need to convert an NSString object you receive to a particular encoding, you'll be able to use the dataUsingEncoding: method to do that.

So I gave up on that and decided to take a look at the RTF handling functions, since RTF seems to include information about what encoding to use. But using 'setString' just gives me the raw RTF in the NSTextView.
Since setString: takes an NSString argument, that's what I would expect; an NSString is just plain text, with no attributes attached, so the setString: method simply takes it as-is, without trying to parse out the RTF formatting. It would do the same with HTML or any other markup.

Now, finally, my questions ;-)

a) How can I explicitly tell the NSText object what encoding to use?
For right now, I'm afraid you can't. Like I said above, though, you'll be able to use the initWithData:encoding: method to create an NSString object in the next CB version, and pass that to NSText's setString: method.

b) How can I shove RTF data into an NSText obj. using CB? (w/o using the 'readRTFDFromFile' function).
Well, the glib answer to that is to use replaceCharactersInRange:withRTF: - but the only way to create an NSData object at the moment is to use NSData's initWithContentsOfFile: method, and from your comment I assume your data isn't coming from a file.

The next version of CB (I'll bet you're tired of hearing that...) will support raw pointers - i.e. "void *". Whenever a method needs a raw pointer, you'll pass it a scalar, and the ObjC method will get a pointer to the data area of the scalar. So, you'll be able to construct an NSData object from the bytes in a scalar with NSData's initWithBytes:length: or initWithBytesNoCopy:length: methods.

c) Is there more detailed documentation on CB itself? All I can find are short tutorials on the web.
Soon!

sherm--

"I have no special gift, I am only passionately curious." - Albert Einstein

Reply via email to