WrapText

From Second Life Wiki
Revision as of 14:13, 7 July 2008 by Chaz Longstaff (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Function: string WrapText(string source,integer length);

Splits a long text string into lines whose character length you get to specify. Useful, for instance, for long floating text to make it tidier. It will not split words at odd places but instead look for the space in the text nearest to the length you have specified.

Example: string myWrappedText = WrapText(Title,25);

Specification

<lsl> string WrapText(string pcText, integer piWidth) {

   list     laLines = [];
   integer  liIndex;
   integer  liKeep;        // Specifies if we keep the char pointed at or not
   
   while (llStringLength(pcText) > 0) {
       liIndex = piWidth;
       liKeep = 1;
       if (llStringLength(pcText) > piWidth) {
           liKeep = 0;
           while ((liIndex >= 0) && (llGetSubString(pcText,liIndex,liIndex) != " "))
               --liIndex;
           if (liIndex <= 0) {
               liIndex = piWidth;
               liKeep = 1;
           }
       }
       laLines += [llGetSubString(pcText,0,liIndex - 1)];
       pcText = llDeleteSubString(pcText,0,liIndex - liKeep);
   }
   return llDumpList2String(laLines,"\n");

} </lsl>