Possibly useful code: SplitPlainTextAtLineFeed

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Possibly useful code: SplitPlainTextAtLineFeed

Bob Zawalich-3
If you have a Text or SystemTextitem object and the text is separated by
line feeds, it is difficult to write out the plain text as separate lines.
In the .Text field, which is plain text, line feeds are replaced by spaces,
so you cannot even look in the text for line feeds.

Here is a rather arcane bit of code that returns an array of plain text with
one entry for each line of text. If anyone has a simpler way to do it I
would be pleased to see it.

What I do is.

1. Save off the TextWithFormattingAsString from the Text object for later
restoration

e.g., strOrig = txt.TextWithFormattingAsString;

2. Look through the TextWithFormatting array, which will separate out the
formatted fields, including line fields (shown as "\n\"). Here is an example
of some formatted multi line text, with the  TextWithFormatting array at the
end:

unformatted, formatted as string, formatted array:
 
This is text nnnn with multiple  line breaks that we want   to split
This is \B\text \b\nnnn\n\with \I\multiple\n\\n\\i\line\n\breaks that
\fPapyrus\we want\n\\n\\n\\f_\to split\n\
{
         "This is "
         "\B\"
         "text "
         "\b\"
         "nnnn"
         "\n\"
         "with "
         "\I\"
         "multiple"
         "\n\"
         "\n\"
         "\i\"
         "line"
         "\n\"
         "breaks that "
         "\fPapyrus\"
         "we want"
         "\n\"
         "\n\"
         "\n\"
         "\f_\"
         "to split"
         "\n\"
}

Note that SplitString() on the TextWithFormattingAsString field does not
work as well.

3. Go through the array looking for \n\. If I do not find it, I concatenate
all the array entries into a string. If I find a linefeed, I take the string
I had built up, and assign it to the text object, then get the text object's
.Text field, which effectively discards the formatting. I add that string to
the output array.

4. Repeat for all text, being careful to get the last one, and then restore
the original text to the text object.

Here is the routine, which I am using in the recent update to the Export
Text In Score plugin.

Bob

********************************************************
SplitPlainTextAtLineFeed(txt)
{

//return array with 0 or more lines of plain text

strOrig = txt.TextWithFormattingAsString;

strLF = "\\n\\";
arrNoLF = CreateArray();

// new lines (line feeds) are turned to spaces in the plain Text field.
// combine string up to new line. If NL found, set text to the string then
get unformatted text
// from object and add to array (use text object as text-plainifier)
// at end restore text

arrFmt = txt.TextWithFormatting;
strHolding = "";
for i = 0 to arrFmt.NumChildren
{
        str = "" & arrFmt[i];
        //trace(str);
        if (str != strLF)
        {
                strHolding = strHolding & str;
        }
        else
        {
                if (strHolding != "")
                {
                        txt.Text = strHolding;
                        arrNoLF[arrNoLF.NumChildren] = txt.Text; //
converted to normal plain text
                        strHolding = "";
                }
        }

}

if (strHolding != "")
{
        txt.Text = strHolding;
        arrNoLF[arrNoLF.NumChildren] = txt.Text; // converted to normal
plain text
}

txt.Text = strOrig; // restore text object
return arrNoLF;
}

_______________________________________________
Plugin-dev mailing list
[hidden email]
https://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev