Please fix extraordinarily slow parser for RTFData

I don’t see ugliness in my code. Do you?

Create a Structure in a module and name it NSRange, then add to the structure the two fields location and length.


I see you using length but where is location being used?

NSRange location as Integer length as Integer

even if location is not used it’s part of the structure and must be defined.

Hi Massimo

It is nothing personal, the ugliness is not your code as such, I am sure that it is as beautifully coded as can be. My point was voiced in the context of achieving ‘cross-platform’ solutions. I would like to use Xojo-only solutions as much as is possible and let Xojo hide away the OS specific solutions when necessary - I am sure that Xojo would prefer this too. Of course if you need a solution and none are forthcoming (quickly enough) then a quick fix may be necessary.

Carl, I agree on the fact a definitive and cross platform solution is need from Xojo, but this is exactly a workaround for the problem in Cocoa, not more and not less.

However there are cases where using declares we can achieve more functionalities from one platform where a counterpart is not available for another platform. Take for example the ability of Cocoa to read a RTFD (with pictures) and place it into a TextArea.

Hi Massimo,

thanks for the answer.

How would I use it? For example read a file into a textarea?

For RTF, just open a binaryStream (or TextInputStream if you prefer), read and put the contents into a string. The call the TextArea extension method RTFValue.
Supposing we have a string named MyRTFSource, to be put into a TextArea instance named MyTextArea, just write:

MyTextArea.RTFValue = MyRTFSource

to get the content of a TextArea into a string:

MyRTFSource = MyTextArea.RTFValue

For RTFD, just provide the file path:

if MyTextArea. ReadRTFDFromFile("/path/to/my/rtfdfile") then // all went OK else // damn… can't read. perhaps corrupted file or wrong path end if

Hmmm, that’s what I had tried but didn’t work when I tried loading an RTF file created in MS Word. My code is:

dim f as new FolderItem f = GetOpenFolderItem(FileTypes1.Any) // get the folderitem if TextArea1.ReadRTFDFromFile( f.AbsolutePath ) then end if

I had also tried

without success.

You must pass a POSIX path. Not a ShellPath, which escapes spaces and special chars.
If you have MBS plugins use folderItem.UNIXpathMBS

Hmm, I tried folderitem.NativePath which is the unescaped Posix path in Xojo, but still no luck

Wrong path or wrong file.

This function is for RTFD not RTF.

This function is for RTFD not RTF.

Thanks - that explains it then.

To open a RTF file, use the other method:

dim f as folderItem = GetOpenFolderItem("") if f <> nil then dim bs as BinaryStream = BinaryStream.Open(f) dim rtf as string = TextArea1.RTFValue = rtf end if

be aware I wrote the code above without testing it.

Hmm, needed to fix one error but still not opening a Word rtf file

dim f as folderItem = GetOpenFolderItem("") if f <> nil then dim bs as BinaryStream = BinaryStream.Open( f ) dim rtf as string = bs.Read( bs.Length ) TextArea1.RTFValue = rtf end if

rtf does contain RTF code (checked with a msgbox) but nothing shows up in the TextArea. Also tried a rtf from TextEdit but no luck.

It works! Sorry, my stupid mistake. Had it set for Carbon in RB2012R2.

Can someone please provide an example project for this?

I have used WordGuise and Bob Keeney’s control. I like them both, but there are a couple quirks. I have some RTF files created on Windows over 10 years ago. Neither control will display the embedded images on MAC. Bob Keeney’s control displays the images on a MAC if I re-create the RTF using his control on a MAC, this does not work for wordguise. Both controls display all embedded images on Windows systems. When I use Bob Keeney’s control, it requires TextInputCanvas.xojo_plugin, which needs the file msvcr100.dll, which does not get put in the library folder automatically when building.

[quote=80186:@Lennox Jacob]Can someone please provide an example project for this?
Forum Post

Thanks Axel.

Bump - any update to this? I see that MBS plugins has a call RTFDatMBS but in testing it I’m still seeing unusably slow performance (e.g. 5 seconds to get the RTFData for a styled textArea with about 5000 characters in it). Discussion here: