TextArea.StyledText rendering to RTF still extremely slow

[quote=203533:@Gerd Wilmer]strings, numbers, dates, sometimes longer text strings like a newspaper paragraph

There are 8 sql queries. I know the fields for each query, but I have to test which cursors
contain rows and then which fields contain data.
That is why this is step by step approach
The queries are in no set order and are being repeated with different criteria as required[/quote]

Sorry Gerd, but I believe you are missing the point here.
The problem is not appending text to a TextArea but getting Rich Text Format (RTF) code and insert it into the TextArea as StyledText.

[quote=203543:@Massimo Valle]Sorry Gerd, but I believe you are missing the point here.
The problem is not appending text to a TextArea but getting Rich Text Format (RTF) code and insert it into the TextArea as StyledText.[/quote]
Hi Massimo
that is exactly how I do it.
I manipulate the text in Textarea2. Once the formatting is done I then append this snippet to TextArea1.
The the next set of data and so on

[quote=203625:@Gerd Wilmer]Hi Massimo
that is exactly how I do it.
I manipulate the text in Textarea2. Once the formatting is done I then append this snippet to TextArea1.
The the next set of data and so on[/quote]

Can you please explain how you can do this:

  1. Open TextEdit
  2. Write some rich text (bold, centered paragraph, etc.)
  3. Save from TextEdit as RTF (default)
  4. Open the file in Xojo and import the rich text into a TextArea

Thanks.

He may be doing something like that

TextArea1.StyledText.RTFData = TextArea1.StyledText.RTFData+ TextArea2.StyledText.RTFData

Works for small text, but for bigger chunks, sure way to bump in the beachball of death, without MacOSLib routines :wink:

I also think adding two RTFs is not formally correct. You are joining two source codes delimited from tags.
Example:

joining this

[code]{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

\f0\fs24 \cf0 FIRST FILE}[/code]

with this

[code]{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

\f0\fs24 \cf0 SECOND FILE}[/code]

will produce a malformed RTF document (duplicate RTF tag).
Chances are the second part is ignored, if not bigger problems.
If it works, is just for luck and I’d not rely on this.

[quote=203673:@Massimo Valle]I also think adding two RTFs is not formally correct. You are joining two source codes delimited from tags.
Example:

joining this

[code]{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

\f0\fs24 \cf0 FIRST FILE}[/code]

with this

[code]{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural

\f0\fs24 \cf0 SECOND FILE}[/code]

will produce a malformed RTF document (duplicate RTF tag).
Chances are the second part is ignored, if not bigger problems.
If it works, is just for luck and I’d not rely on this.[/quote]

Indeed I had thought about doing that in the past but stripping the header of the second block. Nevertheless, it does work. Apparently the Xojo RTF parser is able to gobble the concatenation. I did not try with the Cocoa declare, though.

I wonder why he needs to build his text in a second TextArea instead of simply doing that at the end of the text in the first one.

Using an invisible TextArea can speed things up immensly …

[quote=203657:@Massimo Valle]Can you please explain how you can do this:

  1. Open TextEdit
  2. Write some rich text (bold, centered paragraph, etc.)
  3. Save from TextEdit as RTF (default)
  4. Open the file in Xojo and import the rich text into a TextArea

Thanks.[/quote]
I am not sure who is on the wrong track here. the original question was : However I am getting the spinning wheel of death with that line: ‘myStr = TextArea.StyledText.RTFData’ with a text of 200 lines…
what has that to do with TextEdit?

Gerd

[quote=204076:@Gerd Wilmer]I am not sure who is on the wrong track here. the original question was : However I am getting the spinning wheel of death with that line: ‘myStr = TextArea.StyledText.RTFData’ with a text of 200 lines…
what has that to do with TextEdit?

Gerd[/quote]

TextEdit was an easy way to create a RTF file. You can use Microsoft Word if you prefer or just take some RTF already made.
The point is getting RTF code (not plain text) and insert it into a TextArea. The StyledText.RTFData does just this, converting RTF text into a Styled text.

I found nothing in my local xDev index nor at xDevmag.com.

There is something to Print styled text, there is the [ann] of the RTFData addition (in REALbasic Magazine Issue 5.4), and, at last an article about… StyledText.RTFDataMBS.

There can be something, but I do not find it.

Talking about how long it is to deal with rtf, the code in Xojo may have been done with pure Xojo instructions la old fashioned way (the kind of development done in the 70s, well before OOP was created) ?

This kind of things MAY explain why this is so slow (8 years after its release, in REALbasic 2007 Release 2).

Nota: this was the time when the decision to quit Carbon / implement Cocoa was done / announced. This may explain why RTFData was never reworked for speed.

Warning: all this is just assumptions, I really can be totally wrong and I do not have any Xojo inside information.

[quote=204187:@Emile Schwarz]I found nothing in my local xDev index nor at xDevmag.com.
There can be something, but I do not find it.[/quote]
Do you mean xDev 12.4 Tips&Tricks? On page 71-72 it says

RTF: Speeding up saving and loading RTF files on the Mac

RTF is the standard text document exchange format for many apps. It is for word processors what tab delimited files are for spreadsheets. If you are using RTFdata under Windows then you might not have noticed, but under MacOS it is simply unusable. Getting and setting a TextArea’s RTFdata is mostly fine for small amounts of text, but it gets exponentially slower as the length grows, becoming unusable surprisingly quickly: getting RTFdata from a TextArea containing just 8000 characters (not words) takes 25 seconds on a fairly speedy Mac.
Most people will encounter this when reading or saving RTF files (for example those produced by Word). Bob Keeney did some extensive testing and found that the built-in Xojo RegEx class is extremely slow. Something in the neighborhood of 80ms for every search, and some of the test files that people have been giving him have proven that this is THE bottleneck when reading RTF - and the larger the file, the worse it gets.
But this isn’t the only problem. In my testing I deliberately created an RTF file from Word where the original file included items that the TextArea can’t display like (among other things) a text box with a picture and some text. Unsurprisingly the text box and its contents did not show up (neither do they in TextEdit or any other app I tried except Word), and it took 11 seconds for the text to appear in the TextArea. What did however surprise me was that only a tiny amount of the original text arrived in the TextArea! Now what? You can hardly ask users to clean up their RTF files before trying to open them in your app!
As so often before it was MacOSlib (https://github.com/macoslib/macoslib) which came to the rescue. It turns out that it contains TextArea.RTFValue functions (in its TextAreaExtensions module) that let you get/set a TextArea’s RTFdata extremely fast: it took just 0.116 seconds for the text to appear. Not only that, all the text (minus the text box) appeared.

native Xojo: 11.103 seconds (very little text loaded)
using MacOSlib: 0.116 seconds (full text loaded)

Unfortunately you can’t use this to get RTFdata into and out of StyledText objects (for example to do things like concatenate styled text runs) as StyledText objects seem to have their own data structures and are not simply NSAttributedStrings. However the slow thing with StyledText is querying the properties with all the StyleRuns and paragraphs (thanks to Christian Schmitz for pointing this out) so the old trick of using an invisible TextArea off-window to manipulate the RTF will be much faster (thanks to Tim Hare for reminding me).

and on page 73:

Joe Ranieri posted a very nice speed tip for the TextArea on the REALsoftware blog back in 2012 (http://www.realsoftwareblog.com/2012/11/speeding-up-textarea-modifications.html). When doing a lot of manipulation to a TextArea’s contents under Cocoa, performance can suffer due to the underlying NSTextView doing work on layout and glyph selection. This can be sped up enormously by using Declares that give you access to methods for enabling “batch-editing mode” for the underlying NSTextView. With MacOSLib (https://github.com/macoslib/macoslib) installed you don’t even have to use the declares to speed things up as these are wrapped into easy to use functions. You simply tell the text view when you’re going to begin and end editing, and all the changes are applied in one go. For example, this simple For…Next loop
For i As Integer = 0 To 5000
MyTextArea.AppendText(“Lorem ipsum dolor sit amet.”)
Next

by itself takes about 4.3 seconds to complete. With MacOSLib installed you can simply do:??
dim tv as new NSTextView( myTextArea )?
dim ts as NSTextStorage = tv.TextStorage?
ts.BeginEditing?
// put your code here?
For i As Integer = 0 To 5000
MyTextArea.AppendText(“Lorem ipsum dolor sit amet.”)
Next
ts.EndEditing??

and the loop completes in just 0.02 seconds! That is about 200x faster!

Nope

you can use MBS Plugin extensions for TextArea:

https://www.monkeybreadsoftware.net/class-textarea.shtml

or Styled Text here:

https://www.monkeybreadsoftware.net/class-styledtext.shtml

We do have StyledText.RTFDataMBS, TextArea.RTFDataMBS and TextArea.WinRTFDataMBS.